diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/main/java/org/apache/hadoop/yarn/applications/distributedshell/ApplicationMaster.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/main/java/org/apache/hadoop/yarn/applications/distributedshell/ApplicationMaster.java index 740a720..602e7bb 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/main/java/org/apache/hadoop/yarn/applications/distributedshell/ApplicationMaster.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-applications-distributedshell/src/main/java/org/apache/hadoop/yarn/applications/distributedshell/ApplicationMaster.java @@ -45,6 +45,7 @@ import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.DataOutputBuffer; +import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.security.Credentials; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.token.Token; @@ -481,6 +482,8 @@ public boolean run() throws YarnException, IOException { // Register self with ResourceManager // This will start heartbeating to the RM + appMasterHostname = NetUtils.getHostname(); + appMasterRpcPort = NetUtils.getFreeSocketPort(); RegisterApplicationMasterResponse response = amRMClient .registerApplicationMaster(appMasterHostname, appMasterRpcPort, appMasterTrackingUrl); diff --git hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java index f24a2cd..dc25d3d 100644 --- hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java +++ hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java @@ -39,6 +39,7 @@ import junit.framework.Assert; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.net.NetUtils; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.service.Service.STATE; import org.apache.hadoop.yarn.api.ApplicationMasterProtocol; @@ -101,6 +102,7 @@ static String[] nodes; static String[] racks; private final static int DEFAULT_ITERATION = 3; + private ApplicationId appId; @BeforeClass public static void setup() throws Exception { @@ -134,7 +136,7 @@ public void startApp() throws Exception { // submit new app ApplicationSubmissionContext appContext = yarnClient.createApplication().getApplicationSubmissionContext(); - ApplicationId appId = appContext.getApplicationId(); + appId = appContext.getApplicationId(); // set the application name appContext.setApplicationName("Test"); // Set the priority for the application master @@ -641,6 +643,45 @@ public void testAMRMClient() throws YarnException, IOException { } } } + + @Test + public void testAppMasterHostNameAndPortNumber() throws Exception { + AMRMClient amClient = null; + try { + // start am rm client + amClient = AMRMClient. createAMRMClient(); + amClient.init(conf); + amClient.start(); + + String appMasterHostname = NetUtils.getHostname(); + int appMasterRpcPort = NetUtils.getFreeSocketPort(); + amClient.registerApplicationMaster(appMasterHostname, appMasterRpcPort, + ""); + + int maxWaitTimes = 5; + boolean verified = false; + while (maxWaitTimes > 0 && !verified) { + ApplicationReport appReport = yarnClient.getApplicationReport(appId); + if (appReport.getHost().equals(appMasterHostname) + && appReport.getRpcPort() == appMasterRpcPort) { + verified = true; + } + maxWaitTimes--; + Thread.sleep(500); + } + + if (!verified) { + Assert.fail("The hostName and port number should match."); + } + amClient.unregisterApplicationMaster(FinalApplicationStatus.SUCCEEDED, + null, null); + + } finally { + if (amClient != null && amClient.getServiceState() == STATE.STARTED) { + amClient.stop(); + } + } + } private void testAllocation(final AMRMClientImpl amClient) throws YarnException, IOException {