From 6bf895b378b90027b14f751a03e3c8d100a575ab Mon Sep 17 00:00:00 2001 From: Aaron Myers Date: Thu, 17 Jan 2013 23:11:18 +0000 Subject: [PATCH] HDFS-4415. HostnameFilter should handle hostname resolution failures and continue processing. Contributed by Robert Kanter. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1434952 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop/lib/servlet/HostnameFilter.java | 18 ++++++++++++- .../lib/servlet/TestHostnameFilter.java | 26 +++++++++++++++++++ hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 +++ 3 files changed, 46 insertions(+), 1 deletion(-) 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 39b7e4fb61a..dd395f67495 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.ServletRequest; 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 @@ import java.net.InetAddress; @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 class HostnameFilter implements Filter { 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 44da0afd705..3148d3a6820 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 class TestHostnameFilter extends HTestCase { 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 58c88066334..546aaa01183 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -420,6 +420,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.