diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/servlet/HostnameFilter.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/servlet/HostnameFilter.java index 39b7e4fb61..dd395f6749 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/servlet/HostnameFilter.java +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/main/java/org/apache/hadoop/lib/servlet/HostnameFilter.java @@ -29,6 +29,9 @@ import javax.servlet.ServletResponse; import java.io.IOException; import java.net.InetAddress; +import java.net.UnknownHostException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Filter that resolves the requester hostname. @@ -36,6 +39,7 @@ @InterfaceAudience.Private public class HostnameFilter implements Filter { static final ThreadLocal HOSTNAME_TL = new ThreadLocal(); + private static final Logger log = LoggerFactory.getLogger(HostnameFilter.class); /** * Initializes the filter. @@ -66,7 +70,19 @@ public void init(FilterConfig config) throws ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { - String hostname = InetAddress.getByName(request.getRemoteAddr()).getCanonicalHostName(); + String hostname; + try { + String address = request.getRemoteAddr(); + if (address != null) { + hostname = InetAddress.getByName(address).getCanonicalHostName(); + } else { + log.warn("Request remote address is NULL"); + hostname = "???"; + } + } catch (UnknownHostException ex) { + log.warn("Request remote address could not be resolved, {0}", ex.toString(), ex); + hostname = "???"; + } HOSTNAME_TL.set(hostname); chain.doFilter(request, response); } finally { diff --git a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java index 44da0afd70..3148d3a682 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java +++ b/hadoop-hdfs-project/hadoop-hdfs-httpfs/src/test/java/org/apache/hadoop/lib/servlet/TestHostnameFilter.java @@ -64,4 +64,30 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo filter.destroy(); } + @Test + public void testMissingHostname() throws Exception { + ServletRequest request = Mockito.mock(ServletRequest.class); + Mockito.when(request.getRemoteAddr()).thenReturn(null); + + ServletResponse response = Mockito.mock(ServletResponse.class); + + final AtomicBoolean invoked = new AtomicBoolean(); + + FilterChain chain = new FilterChain() { + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) + throws IOException, ServletException { + assertTrue(HostnameFilter.get().contains("???")); + invoked.set(true); + } + }; + + Filter filter = new HostnameFilter(); + filter.init(null); + assertNull(HostnameFilter.get()); + filter.doFilter(request, response, chain); + assertTrue(invoked.get()); + assertNull(HostnameFilter.get()); + filter.destroy(); + } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 575e13425c..bbcbf09fad 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -719,6 +719,9 @@ Release 2.0.3-alpha - Unreleased HDFS-1245. Pluggable block id generation. (shv) + HDFS-4415. HostnameFilter should handle hostname resolution failures and + continue processing. (Robert Kanter via atm) + BREAKDOWN OF HDFS-3077 SUBTASKS HDFS-3077. Quorum-based protocol for reading and writing edit logs.