diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java index 03588f146bf..f45465a6ed9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/webapp/util/WebAppUtils.java @@ -87,21 +87,46 @@ public class WebAppUtils { hostName + ":" + port); } } - - public static String getRMWebAppURLWithScheme(Configuration conf) { - return getHttpSchemePrefix(conf) + getRMWebAppURLWithoutScheme(conf); - } - - public static String getRMWebAppURLWithoutScheme(Configuration conf) { - if (YarnConfiguration.useHttps(conf)) { - return conf.get(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS, + + public static String getRMWebAppURLWithoutScheme(Configuration conf, + boolean isHAEnabled) { + YarnConfiguration yarnConfig = new YarnConfiguration(conf); + // set RM_ID if we have not configure it. + if (isHAEnabled) { + String rmId = yarnConfig.get(YarnConfiguration.RM_HA_ID); + if (rmId == null || rmId.isEmpty()) { + List rmIds = new ArrayList<>(HAUtil.getRMHAIds(conf)); + if (rmIds != null && !rmIds.isEmpty()) { + yarnConfig.set(YarnConfiguration.RM_HA_ID, rmIds.get(0)); + } + } + } + if (YarnConfiguration.useHttps(yarnConfig)) { + if (isHAEnabled) { + return HAUtil.getConfValueForRMInstance( + YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS, yarnConfig); + } + return yarnConfig.get(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS, YarnConfiguration.DEFAULT_RM_WEBAPP_HTTPS_ADDRESS); }else { - return conf.get(YarnConfiguration.RM_WEBAPP_ADDRESS, + if (isHAEnabled) { + return HAUtil.getConfValueForRMInstance( + YarnConfiguration.RM_WEBAPP_ADDRESS, yarnConfig); + } + return yarnConfig.get(YarnConfiguration.RM_WEBAPP_ADDRESS, YarnConfiguration.DEFAULT_RM_WEBAPP_ADDRESS); } } + public static String getRMWebAppURLWithScheme(Configuration conf) { + return getHttpSchemePrefix(conf) + getRMWebAppURLWithoutScheme( + conf, HAUtil.isHAEnabled(conf)); + } + + public static String getRMWebAppURLWithoutScheme(Configuration conf) { + return getRMWebAppURLWithoutScheme(conf, false); + } + public static List getProxyHostsAndPortsForAmFilter( Configuration conf) { List addrs = new ArrayList(); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/conf/TestYarnConfiguration.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/conf/TestYarnConfiguration.java index 2e76865ec7f..738942300a0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/conf/TestYarnConfiguration.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/conf/TestYarnConfiguration.java @@ -41,6 +41,22 @@ public class TestYarnConfiguration { // specifically add slashes and Jetty doesn't handle double slashes. Assert.assertNotSame("RM Web Url is not correct", "http://0.0.0.0:8088", rmWebUrl); + + // test it in HA scenario + conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); + conf.set(YarnConfiguration.RM_HA_IDS, "rm1, rm2"); + conf.set("yarn.resourcemanager.webapp.address.rm1", "10.10.10.10:18088"); + conf.set("yarn.resourcemanager.webapp.address.rm2", "20.20.20.20:28088"); + String rmWebUrlinHA = WebAppUtils.getRMWebAppURLWithScheme(conf); + Assert.assertEquals("http://10.10.10.10:18088", rmWebUrlinHA); + + YarnConfiguration conf2 = new YarnConfiguration(); + conf2.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); + conf2.set(YarnConfiguration.RM_HA_IDS, "rm1, rm2"); + conf2.set("yarn.resourcemanager.hostname.rm1", "30.30.30.30"); + conf2.set("yarn.resourcemanager.hostname.rm2", "40.40.40.40"); + String rmWebUrlinHA2 = WebAppUtils.getRMWebAppURLWithScheme(conf2); + Assert.assertEquals("http://30.30.30.30:8088", rmWebUrlinHA2); } @Test