Master { public static InetSocketAddress getMasterAddress(Configuration conf) { String masterAddress; String framework = conf.get(MRConfig.FRAMEWORK_NAME, MRConfig.YARN_FRAMEWORK_NAME); if (framework.equals(MRConfig.CLASSIC_FRAMEWORK_NAME)) { masterAddress = conf.get(MRConfig.MASTER_ADDRESS, "localhost:8012"); return NetUtils.createSocketAddr(masterAddress, 8012, MRConfig.MASTER_ADDRESS); } else if (framework.equals(MRConfig.YARN_FRAMEWORK_NAME) && HAUtil.isHAEnabled(conf)) { YarnConfiguration yarnConf = new YarnConfiguration(conf); if (yarnConf.get(YarnConfiguration.RM_HA_ID) == null) { String[] rmIds = yarnConf.getStrings(YarnConfiguration.RM_HA_IDS); if (rmIds != null && rmIds.length > 0) { yarnConf.set(YarnConfiguration.RM_HA_ID, rmIds[0]); } else { LOG.warn("RM_HA_IDS is not configured when RM HA is enabled"); } } return yarnConf.getSocketAddr( YarnConfiguration.RM_ADDRESS, YarnConfiguration.DEFAULT_RM_ADDRESS, YarnConfiguration.DEFAULT_RM_PORT); } else { return conf.getSocketAddr( YarnConfiguration.RM_ADDRESS, YarnConfiguration.DEFAULT_RM_ADDRESS, YarnConfiguration.DEFAULT_RM_PORT); } }  static String getMasterUserName(Configuration conf); static InetSocketAddress getMasterAddress(Configuration conf); static String getMasterPrincipal(Configuration conf);  }
@Test public void testGetMasterAddress() { YarnConfiguration conf = new YarnConfiguration(); String masterHostname = Master.getMasterAddress(conf).getHostName(); InetSocketAddress rmAddr = NetUtils.createSocketAddr(YarnConfiguration.DEFAULT_RM_ADDRESS); assertEquals(masterHostname, rmAddr.getHostName()); conf.set(MRConfig.FRAMEWORK_NAME, MRConfig.CLASSIC_FRAMEWORK_NAME); conf.set(MRConfig.MASTER_ADDRESS, "local:invalid"); try { Master.getMasterAddress(conf); fail("Should not reach here as there is a bad master address"); } catch (Exception e) { } conf.set(MRConfig.MASTER_ADDRESS, "bar.com:8042"); masterHostname = Master.getMasterAddress(conf).getHostName(); assertEquals(masterHostname, "bar.com"); conf.set(MRConfig.FRAMEWORK_NAME, MRConfig.YARN_FRAMEWORK_NAME); conf.set(YarnConfiguration.RM_ADDRESS, "foo1.com:8192"); masterHostname = Master.getMasterAddress(conf).getHostName(); assertEquals(masterHostname, "foo1.com"); conf.set(MRConfig.FRAMEWORK_NAME, MRConfig.YARN_FRAMEWORK_NAME); conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); conf.set(YarnConfiguration.RM_HA_IDS, "rm1,rm2"); conf.set(YarnConfiguration.RM_ADDRESS + ".rm1", "rm1.com:8192"); conf.set(YarnConfiguration.RM_ADDRESS + ".rm2", "rm2.com:8192"); masterHostname = Master.getMasterAddress(conf).getHostName(); assertEquals(masterHostname, "rm1.com"); conf.set(YarnConfiguration.RM_HA_ID, "rm2"); masterHostname = Master.getMasterAddress(conf).getHostName(); assertEquals(masterHostname, "rm2.com"); }