From 7280550a8f668df8aa32e4630db4ead49e9b8b6d Mon Sep 17 00:00:00 2001 From: Arpit Agarwal Date: Mon, 4 Apr 2016 10:50:11 -0700 Subject: [PATCH] HADOOP-11212. NetUtils.wrapException to handle SocketException explicitly. (Contributed by Steve Loughran) --- .../java/org/apache/hadoop/net/NetUtils.java | 15 ++++-- .../org/apache/hadoop/net/TestNetUtils.java | 47 ++++++++++--------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java index 2c3661aaf2..40501073fd 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java @@ -782,12 +782,21 @@ public static IOException wrapException(final String destHost, + ": " + exception + ";" + see("EOFException")); + } else if (exception instanceof SocketException) { + // Many of the predecessor exceptions are subclasses of SocketException, + // so must be handled before this + return wrapWithMessage(exception, + "Call From " + + localHost + " to " + destHost + ":" + destPort + + " failed on socket exception: " + exception + + ";" + + see("SocketException")); } else { return (IOException) new IOException("Failed on local exception: " - + exception - + "; Host Details : " - + getHostDetailsAsString(destHost, destPort, localHost)) + + exception + + "; Host Details : " + + getHostDetailsAsString(destHost, destPort, localHost)) .initCause(exception); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java index c93ede8bf6..e59ac77982 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java @@ -72,7 +72,7 @@ public class TestNetUtils { * This is a regression test for HADOOP-6722. */ @Test - public void testAvoidLoopbackTcpSockets() throws Exception { + public void testAvoidLoopbackTcpSockets() throws Throwable { Configuration conf = new Configuration(); Socket socket = NetUtils.getDefaultSocketFactory(conf) @@ -88,11 +88,11 @@ public void testAvoidLoopbackTcpSockets() throws Exception { fail("Should not have connected"); } catch (ConnectException ce) { System.err.println("Got exception: " + ce); - assertTrue(ce.getMessage().contains("resulted in a loopback")); + assertInException(ce, "resulted in a loopback"); } catch (SocketException se) { // Some TCP stacks will actually throw their own Invalid argument exception // here. This is also OK. - assertTrue(se.getMessage().contains("Invalid argument")); + assertInException(se, "Invalid argument"); } } @@ -188,15 +188,11 @@ public void testVerifyHostnamesException() throws UnknownHostException { } @Test - public void testVerifyHostnamesNoException() { + public void testVerifyHostnamesNoException() throws UnknownHostException { String[] names = {"valid.host.com", "1.com"}; - try { - NetUtils.verifyHostnames(names); - } catch (UnknownHostException e) { - fail("NetUtils.verifyHostnames threw unexpected UnknownHostException"); - } + NetUtils.verifyHostnames(names); } - + /** * Test for {@link NetUtils#isLocalAddress(java.net.InetAddress)} */ @@ -267,7 +263,18 @@ public void testWrapEOFException() throws Throwable { assertRemoteDetailsIncluded(wrapped); assertInException(wrapped, "/EOFException"); } - + + @Test + public void testWrapSocketException() throws Throwable { + IOException wrapped = verifyExceptionClass(new SocketException("failed"), + SocketException.class); + assertInException(wrapped, "failed"); + assertWikified(wrapped); + assertInException(wrapped, "localhost"); + assertRemoteDetailsIncluded(wrapped); + assertInException(wrapped, "/SocketException"); + } + @Test public void testGetConnectAddress() throws IOException { NetUtils.addStaticResolution("host", "127.0.0.1"); @@ -322,8 +329,8 @@ private void assertInException(Exception e, String text) throws Throwable { String message = extractExceptionMessage(e); if (!(message.contains(text))) { throw new AssertionFailedError("Wrong text in message " - + "\"" + message + "\"" - + " expected \"" + text + "\"") + + "\"" + message + "\"" + + " expected \"" + text + "\"") .initCause(e); } } @@ -343,8 +350,8 @@ private void assertNotInException(Exception e, String text) String message = extractExceptionMessage(e); if (message.contains(text)) { throw new AssertionFailedError("Wrong text in message " - + "\"" + message + "\"" - + " did not expect \"" + text + "\"") + + "\"" + message + "\"" + + " did not expect \"" + text + "\"") .initCause(e); } } @@ -353,15 +360,13 @@ private IOException verifyExceptionClass(IOException e, Class expectedClass) throws Throwable { assertNotNull("Null Exception", e); - IOException wrapped = - NetUtils.wrapException("desthost", DEST_PORT, - "localhost", LOCAL_PORT, - e); + IOException wrapped = NetUtils.wrapException("desthost", DEST_PORT, + "localhost", LOCAL_PORT, e); LOG.info(wrapped.toString(), wrapped); if(!(wrapped.getClass().equals(expectedClass))) { throw new AssertionFailedError("Wrong exception class; expected " - + expectedClass - + " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped); + + expectedClass + + " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped); } return wrapped; }