HADOOP-11212. NetUtils.wrapException to handle SocketException explicitly. (Contributed by Steve Loughran)

This commit is contained in:
Arpit Agarwal 2016-04-04 10:50:11 -07:00
parent caaadf9f3e
commit 30e12edb67
2 changed files with 38 additions and 24 deletions

View File

@ -782,12 +782,21 @@ public class NetUtils {
+ ": " + exception + ": " + exception
+ ";" + ";"
+ see("EOFException")); + 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 { else {
return (IOException) new IOException("Failed on local exception: " return (IOException) new IOException("Failed on local exception: "
+ exception + exception
+ "; Host Details : " + "; Host Details : "
+ getHostDetailsAsString(destHost, destPort, localHost)) + getHostDetailsAsString(destHost, destPort, localHost))
.initCause(exception); .initCause(exception);
} }

View File

@ -72,7 +72,7 @@ public class TestNetUtils {
* This is a regression test for HADOOP-6722. * This is a regression test for HADOOP-6722.
*/ */
@Test @Test
public void testAvoidLoopbackTcpSockets() throws Exception { public void testAvoidLoopbackTcpSockets() throws Throwable {
Configuration conf = new Configuration(); Configuration conf = new Configuration();
Socket socket = NetUtils.getDefaultSocketFactory(conf) Socket socket = NetUtils.getDefaultSocketFactory(conf)
@ -88,11 +88,11 @@ public class TestNetUtils {
fail("Should not have connected"); fail("Should not have connected");
} catch (ConnectException ce) { } catch (ConnectException ce) {
System.err.println("Got exception: " + ce); System.err.println("Got exception: " + ce);
assertTrue(ce.getMessage().contains("resulted in a loopback")); assertInException(ce, "resulted in a loopback");
} catch (SocketException se) { } catch (SocketException se) {
// Some TCP stacks will actually throw their own Invalid argument exception // Some TCP stacks will actually throw their own Invalid argument exception
// here. This is also OK. // here. This is also OK.
assertTrue(se.getMessage().contains("Invalid argument")); assertInException(se, "Invalid argument");
} }
} }
@ -188,15 +188,11 @@ public class TestNetUtils {
} }
@Test @Test
public void testVerifyHostnamesNoException() { public void testVerifyHostnamesNoException() throws UnknownHostException {
String[] names = {"valid.host.com", "1.com"}; String[] names = {"valid.host.com", "1.com"};
try { NetUtils.verifyHostnames(names);
NetUtils.verifyHostnames(names);
} catch (UnknownHostException e) {
fail("NetUtils.verifyHostnames threw unexpected UnknownHostException");
}
} }
/** /**
* Test for {@link NetUtils#isLocalAddress(java.net.InetAddress)} * Test for {@link NetUtils#isLocalAddress(java.net.InetAddress)}
*/ */
@ -267,7 +263,18 @@ public class TestNetUtils {
assertRemoteDetailsIncluded(wrapped); assertRemoteDetailsIncluded(wrapped);
assertInException(wrapped, "/EOFException"); 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 @Test
public void testGetConnectAddress() throws IOException { public void testGetConnectAddress() throws IOException {
NetUtils.addStaticResolution("host", "127.0.0.1"); NetUtils.addStaticResolution("host", "127.0.0.1");
@ -322,8 +329,8 @@ public class TestNetUtils {
String message = extractExceptionMessage(e); String message = extractExceptionMessage(e);
if (!(message.contains(text))) { if (!(message.contains(text))) {
throw new AssertionFailedError("Wrong text in message " throw new AssertionFailedError("Wrong text in message "
+ "\"" + message + "\"" + "\"" + message + "\""
+ " expected \"" + text + "\"") + " expected \"" + text + "\"")
.initCause(e); .initCause(e);
} }
} }
@ -343,8 +350,8 @@ public class TestNetUtils {
String message = extractExceptionMessage(e); String message = extractExceptionMessage(e);
if (message.contains(text)) { if (message.contains(text)) {
throw new AssertionFailedError("Wrong text in message " throw new AssertionFailedError("Wrong text in message "
+ "\"" + message + "\"" + "\"" + message + "\""
+ " did not expect \"" + text + "\"") + " did not expect \"" + text + "\"")
.initCause(e); .initCause(e);
} }
} }
@ -353,15 +360,13 @@ public class TestNetUtils {
Class expectedClass) Class expectedClass)
throws Throwable { throws Throwable {
assertNotNull("Null Exception", e); assertNotNull("Null Exception", e);
IOException wrapped = IOException wrapped = NetUtils.wrapException("desthost", DEST_PORT,
NetUtils.wrapException("desthost", DEST_PORT, "localhost", LOCAL_PORT, e);
"localhost", LOCAL_PORT,
e);
LOG.info(wrapped.toString(), wrapped); LOG.info(wrapped.toString(), wrapped);
if(!(wrapped.getClass().equals(expectedClass))) { if(!(wrapped.getClass().equals(expectedClass))) {
throw new AssertionFailedError("Wrong exception class; expected " throw new AssertionFailedError("Wrong exception class; expected "
+ expectedClass + expectedClass
+ " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped); + " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped);
} }
return wrapped; return wrapped;
} }