diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-unmanaged-am-launcher/src/main/java/org/apache/hadoop/yarn/applications/unmanagedamlauncher/UnmanagedAMLauncher.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-unmanaged-am-launcher/src/main/java/org/apache/hadoop/yarn/applications/unmanagedamlauncher/UnmanagedAMLauncher.java index b9e0625..4c3dd68 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-unmanaged-am-launcher/src/main/java/org/apache/hadoop/yarn/applications/unmanagedamlauncher/UnmanagedAMLauncher.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-unmanaged-am-launcher/src/main/java/org/apache/hadoop/yarn/applications/unmanagedamlauncher/UnmanagedAMLauncher.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.net.InetAddress; import java.util.ArrayList; import java.util.EnumSet; import java.util.Map; @@ -41,6 +42,7 @@ import org.apache.hadoop.yarn.api.records.ApplicationId; import org.apache.hadoop.yarn.api.records.ApplicationReport; import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext; +import org.apache.hadoop.yarn.api.records.ContainerId; import org.apache.hadoop.yarn.api.records.ContainerLaunchContext; import org.apache.hadoop.yarn.api.records.FinalApplicationStatus; import org.apache.hadoop.yarn.api.records.Priority; @@ -81,6 +83,8 @@ // set the classpath explicitly private String classpath = null; + private volatile boolean amCompleted = false; + /** * @param args * Command line arguments @@ -179,8 +183,18 @@ public void launchAM(ApplicationAttemptId attemptId) throws IOException { if(!setClasspath && classpath!=null) { envAMList.add("CLASSPATH="+classpath); } - - envAMList.add(ApplicationConstants.AM_APP_ATTEMPT_ID_ENV + "=" + attemptId); + + ContainerId containerId = Records.newRecord(ContainerId.class); + containerId.setApplicationAttemptId(attemptId); + containerId.setId(0); + + String hostname = InetAddress.getLocalHost().getHostName(); + envAMList.add(ApplicationConstants.AM_CONTAINER_ID_ENV + "=" + containerId); + envAMList.add(ApplicationConstants.NM_HOST_ENV + "=" + hostname); + envAMList.add(ApplicationConstants.NM_HTTP_PORT_ENV + "=0"); + envAMList.add(ApplicationConstants.NM_PORT_ENV + "=0"); + envAMList.add(ApplicationConstants.APP_SUBMIT_TIME_ENV + "=" + + System.currentTimeMillis()); String[] envAM = new String[envAMList.size()]; Process amProc = Runtime.getRuntime().exec(amCmd, envAMList.toArray(envAM)); @@ -234,6 +248,7 @@ public void run() { } catch (InterruptedException e) { e.printStackTrace(); } + amCompleted = true; try { // make sure that the error thread exits @@ -296,6 +311,12 @@ public boolean run() throws IOException { // Monitor the application to wait for launch state ApplicationReport appReport = monitorApplication(appId, EnumSet.of(YarnApplicationState.ACCEPTED)); + if (appReport == null) { + LOG.fatal("Received null application report when monitoring app" + + ". App seems to have failed"); + return false; + } + ApplicationAttemptId attemptId = appReport.getCurrentApplicationAttemptId(); LOG.info("Launching application with id: " + attemptId); @@ -306,6 +327,12 @@ public boolean run() throws IOException { appReport = monitorApplication(appId, EnumSet.of( YarnApplicationState.KILLED, YarnApplicationState.FAILED, YarnApplicationState.FINISHED)); + if (appReport == null) { + LOG.fatal("Received null application report when monitoring app" + + ". App seems to have failed"); + return false; + } + YarnApplicationState appState = appReport.getYarnApplicationState(); FinalApplicationStatus appStatus = appReport.getFinalApplicationStatus(); @@ -341,7 +368,7 @@ public boolean run() throws IOException { private ApplicationReport monitorApplication(ApplicationId appId, Set finalState) throws YarnRemoteException { - while (true) { + while (true && !amCompleted) { // Check app status every 1 second. try { @@ -369,9 +396,8 @@ private ApplicationReport monitorApplication(ApplicationId appId, if (finalState.contains(state)) { return report; } - } - + return null; } }