AmFilterInitializer extends FilterInitializer { @Override public void initFilter(FilterContainer container, Configuration conf) { Map<String, String> params = new HashMap<>(); List<String> proxies = WebAppUtils.getProxyHostsAndPortsForAmFilter(conf); StringBuilder sb = new StringBuilder(); for (String proxy : proxies) { sb.append(proxy.split(":")[0]).append(AmIpFilter.PROXY_HOSTS_DELIMITER); } sb.setLength(sb.length() - 1); params.put(AmIpFilter.PROXY_HOSTS, sb.toString()); String prefix = WebAppUtils.getHttpSchemePrefix(conf); String proxyBase = getApplicationWebProxyBase(); sb = new StringBuilder(); for (String proxy : proxies) { sb.append(prefix).append(proxy).append(proxyBase) .append(AmIpFilter.PROXY_HOSTS_DELIMITER); } sb.setLength(sb.length() - 1); params.put(AmIpFilter.PROXY_URI_BASES, sb.toString()); YarnConfiguration yarnConf = new YarnConfiguration(conf); Collection<String> rmIds = getRmIds(yarnConf); if (rmIds != null) { List<String> urls = new ArrayList<>(); for (String rmId : rmIds) { String url = getUrlByRmId(yarnConf, rmId); urls.add(url); } if (!urls.isEmpty()) { params.put(RM_HA_URLS, StringUtils.join(",", urls)); } } container.addFilter(FILTER_NAME, FILTER_CLASS, params); }  @Override void initFilter(FilterContainer container, Configuration conf); static final String RM_HA_URLS; }
@Test public void testInitFilter() { MockFilterContainer con = new MockFilterContainer(); Configuration conf = new Configuration(false); conf.set(YarnConfiguration.PROXY_ADDRESS, "host1:1000"); AmFilterInitializer afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(2, con.givenParameters.size()); assertEquals("host1", con.givenParameters.get(AmIpFilter.PROXY_HOSTS)); assertEquals("http: con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)); assertEquals(null, con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); con = new MockFilterContainer(); conf = new Configuration(false); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS, "host2:2000"); afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(2, con.givenParameters.size()); assertEquals("host2", con.givenParameters.get(AmIpFilter.PROXY_HOSTS)); assertEquals("http: con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)); assertEquals(null, con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); con = new MockFilterContainer(); conf = new Configuration(false); conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); conf.set(YarnConfiguration.RM_HA_IDS, "rm1,rm2,rm3"); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm1", "host2:2000"); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm2", "host3:3000"); conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm3", "host4:4000"); afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(3, con.givenParameters.size()); String[] proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS) .split(AmIpFilter.PROXY_HOSTS_DELIMITER); assertEquals(3, proxyHosts.length); Arrays.sort(proxyHosts); assertEquals("host2", proxyHosts[0]); assertEquals("host3", proxyHosts[1]); assertEquals("host4", proxyHosts[2]); String[] proxyBases = con.givenParameters.get(AmIpFilter.PROXY_URI_BASES) .split(AmIpFilter.PROXY_URI_BASES_DELIMITER); assertEquals(3, proxyBases.length); Arrays.sort(proxyBases); assertEquals("http: assertEquals("http: assertEquals("http: assertEquals("host2:2000,host3:3000,host4:4000", con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); con = new MockFilterContainer(); conf = new Configuration(false); conf.set(YarnConfiguration.YARN_HTTP_POLICY_KEY, HttpConfig.Policy.HTTPS_ONLY.toString()); conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true); conf.set(YarnConfiguration.RM_HA_IDS, "rm1,rm2"); conf.set(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + ".rm1", "host5:5000"); conf.set(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + ".rm2", "host6:6000"); afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); assertEquals(3, con.givenParameters.size()); proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS) .split(AmIpFilter.PROXY_HOSTS_DELIMITER); assertEquals(2, proxyHosts.length); Arrays.sort(proxyHosts); assertEquals("host5", proxyHosts[0]); assertEquals("host6", proxyHosts[1]); proxyBases = con.givenParameters.get(AmIpFilter.PROXY_URI_BASES) .split(AmIpFilter.PROXY_URI_BASES_DELIMITER); assertEquals(2, proxyBases.length); Arrays.sort(proxyBases); assertEquals("https: assertEquals("https: assertEquals("host5:5000,host6:6000", con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); }