diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmFilterInitializer.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmFilterInitializer.java index 684161609f1..fa54c794a7d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmFilterInitializer.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmFilterInitializer.java @@ -18,21 +18,26 @@ package org.apache.hadoop.yarn.server.webproxy.amfilter; +import com.google.common.annotations.VisibleForTesting; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.http.FilterContainer; +import org.apache.hadoop.http.FilterInitializer; +import org.apache.hadoop.util.StringUtils; +import org.apache.hadoop.yarn.api.ApplicationConstants; +import org.apache.hadoop.yarn.conf.HAUtil; +import org.apache.hadoop.yarn.conf.YarnConfiguration; +import org.apache.hadoop.yarn.webapp.util.WebAppUtils; + +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.http.FilterContainer; -import org.apache.hadoop.http.FilterInitializer; -import org.apache.hadoop.yarn.api.ApplicationConstants; -import org.apache.hadoop.yarn.webapp.util.WebAppUtils; - -import com.google.common.annotations.VisibleForTesting; - public class AmFilterInitializer extends FilterInitializer { private static final String FILTER_NAME = "AM_PROXY_FILTER"; private static final String FILTER_CLASS = AmIpFilter.class.getCanonicalName(); + public static final String RM_HA_URLS = "RM_HA_URLS"; @Override public void initFilter(FilterContainer container, Configuration conf) { @@ -55,6 +60,29 @@ public class AmFilterInitializer extends FilterInitializer { sb.setLength(sb.length() - 1); params.put(AmIpFilter.PROXY_URI_BASES, sb.toString()); container.addFilter(FILTER_NAME, FILTER_CLASS, params); + + // Handle RM HA urls + List urls = new ArrayList<>(); + + // Include yarn-site.xml in the classpath + YarnConfiguration yarnConf = new YarnConfiguration(conf); + for (String rmId : getRmIds(yarnConf)) { + String url = getUrlByRmId(yarnConf, rmId); + urls.add(url); + } + params.put(RM_HA_URLS, StringUtils.join(",", urls)); + } + + private Collection getRmIds(Configuration conf) { + return conf.getStringCollection(YarnConfiguration.RM_HA_IDS); + } + + private String getUrlByRmId(Configuration conf, String rmId) { + String addressPropertyPrefix = YarnConfiguration.useHttps(conf) ? + YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS : + YarnConfiguration.RM_WEBAPP_ADDRESS; + String host = conf.get(HAUtil.addSuffix(addressPropertyPrefix, rmId)); + return host; } @VisibleForTesting diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmIpFilter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmIpFilter.java index 6febd2dffd0..89e516731d2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmIpFilter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/amfilter/AmIpFilter.java @@ -18,17 +18,12 @@ package org.apache.hadoop.yarn.server.webproxy.amfilter; -import java.io.IOException; -import java.net.InetAddress; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.UnknownHostException; -import java.net.HttpURLConnection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.Collection; +import com.google.common.annotations.VisibleForTesting; +import org.apache.hadoop.classification.InterfaceAudience.Public; +import org.apache.hadoop.yarn.server.webproxy.ProxyUtils; +import org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -39,15 +34,16 @@ import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - -import com.google.common.annotations.VisibleForTesting; -import org.apache.hadoop.classification.InterfaceAudience.Public; -import org.apache.hadoop.yarn.conf.HAUtil; -import org.apache.hadoop.yarn.conf.YarnConfiguration; -import org.apache.hadoop.yarn.server.webproxy.ProxyUtils; -import org.apache.hadoop.yarn.server.webproxy.WebAppProxyServlet; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; @Public public class AmIpFilter implements Filter { @@ -70,6 +66,7 @@ public class AmIpFilter implements Filter { private long lastUpdate; @VisibleForTesting Map proxyUriBases; + String rmUrls[] = null; @Override public void init(FilterConfig conf) throws ServletException { @@ -95,6 +92,10 @@ public class AmIpFilter implements Filter { } } } + + if (conf.getInitParameter(AmFilterInitializer.RM_HA_URLS) != null) { + rmUrls = conf.getInitParameter(AmFilterInitializer.RM_HA_URLS).split(","); + } } protected Set getProxyAddresses() throws ServletException { @@ -196,13 +197,11 @@ public class AmIpFilter implements Filter { if (proxyUriBases.size() == 1) { // external proxy or not RM HA addr = proxyUriBases.values().iterator().next(); - } else { - // RM HA - YarnConfiguration conf = new YarnConfiguration(); - for (String rmId : getRmIds(conf)) { - String url = getUrlByRmId(conf, rmId); - if (isValidUrl(url)) { - addr = url; + } else if (rmUrls != null) { + for (String url : rmUrls) { + String host = proxyUriBases.get(url); + if (isValidUrl(host)) { + addr = host; break; } } @@ -215,26 +214,12 @@ public class AmIpFilter implements Filter { return addr; } - @VisibleForTesting - Collection getRmIds(YarnConfiguration conf) { - return conf.getStringCollection(YarnConfiguration.RM_HA_IDS); - } - - @VisibleForTesting - String getUrlByRmId(YarnConfiguration conf, String rmId) { - String addressPropertyPrefix = YarnConfiguration.useHttps(conf) ? - YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS : - YarnConfiguration.RM_WEBAPP_ADDRESS; - String host = conf.get(HAUtil.addSuffix(addressPropertyPrefix, rmId)); - return proxyUriBases.get(host); - } - @VisibleForTesting public boolean isValidUrl(String url) { boolean isValid = false; try { - HttpURLConnection conn = - (HttpURLConnection) new URL(url).openConnection(); + HttpURLConnection conn = (HttpURLConnection) new URL(url) + .openConnection(); conn.connect(); isValid = conn.getResponseCode() == HttpURLConnection.HTTP_OK; } catch (Exception e) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilter.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilter.java index 12bda8d6d41..383d9885360 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilter.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilter.java @@ -152,14 +152,7 @@ public class TestAmFilter { spy.proxyUriBases = new HashMap<>(); spy.proxyUriBases.put(rm1, rm1Url); spy.proxyUriBases.put(rm2, rm2Url); - - Collection rmIds = new ArrayList<>(Arrays.asList(rm1, rm2)); - Mockito.doReturn(rmIds).when(spy).getRmIds( - Mockito.any(YarnConfiguration.class)); - Mockito.doReturn(rm1Url).when(spy).getUrlByRmId( - Mockito.any(YarnConfiguration.class), Mockito.eq(rm2)); - Mockito.doReturn(rm2Url).when(spy) - .getUrlByRmId(Mockito.any(YarnConfiguration.class), Mockito.eq(rm1)); + spy.rmUrls = new String[] { rm1, rm2 }; // Stub "isValidUrl" and returns false for rm1, returns true for rm2. Mockito.doReturn(false).when(spy).isValidUrl(Mockito.eq(rm1Url)); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilterInitializer.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilterInitializer.java index 63c2cf3bb14..cc60a31ee62 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilterInitializer.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/amfilter/TestAmFilterInitializer.java @@ -53,10 +53,11 @@ public class TestAmFilterInitializer extends TestCase { AmFilterInitializer afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); - assertEquals(2, con.givenParameters.size()); + assertEquals(3, con.givenParameters.size()); assertEquals("host1", con.givenParameters.get(AmIpFilter.PROXY_HOSTS)); assertEquals("http://host1:1000/foo", con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)); + assertEquals("", con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); // Check a single RM_WEBAPP_ADDRESS con = new MockFilterContainer(); @@ -65,10 +66,11 @@ public class TestAmFilterInitializer extends TestCase { afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); - assertEquals(2, con.givenParameters.size()); + assertEquals(3, con.givenParameters.size()); assertEquals("host2", con.givenParameters.get(AmIpFilter.PROXY_HOSTS)); assertEquals("http://host2:2000/foo", con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)); + assertEquals("", con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); // Check multiple RM_WEBAPP_ADDRESSes (RM HA) con = new MockFilterContainer(); @@ -81,7 +83,7 @@ public class TestAmFilterInitializer extends TestCase { afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); - assertEquals(2, con.givenParameters.size()); + assertEquals(3, con.givenParameters.size()); String[] proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS) .split(AmIpFilter.PROXY_HOSTS_DELIMITER); assertEquals(3, proxyHosts.length); @@ -96,6 +98,8 @@ public class TestAmFilterInitializer extends TestCase { assertEquals("http://host2:2000/foo", proxyBases[0]); assertEquals("http://host3:3000/foo", proxyBases[1]); assertEquals("http://host4:4000/foo", proxyBases[2]); + assertEquals("host2:2000,host3:3000,host4:4000", + con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); // Check multiple RM_WEBAPP_ADDRESSes (RM HA) with HTTPS con = new MockFilterContainer(); @@ -109,7 +113,7 @@ public class TestAmFilterInitializer extends TestCase { afi = new MockAmFilterInitializer(); assertNull(con.givenParameters); afi.initFilter(con, conf); - assertEquals(2, con.givenParameters.size()); + assertEquals(3, con.givenParameters.size()); proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS) .split(AmIpFilter.PROXY_HOSTS_DELIMITER); assertEquals(2, proxyHosts.length); @@ -122,6 +126,8 @@ public class TestAmFilterInitializer extends TestCase { Arrays.sort(proxyBases); assertEquals("https://host5:5000/foo", proxyBases[0]); assertEquals("https://host6:6000/foo", proxyBases[1]); + assertEquals("host5:5000,host6:6000", + con.givenParameters.get(AmFilterInitializer.RM_HA_URLS)); } @Test