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 89c9347511
commit 7280550a8f
2 changed files with 38 additions and 24 deletions

View File

@ -782,6 +782,15 @@ public static IOException wrapException(final String destHost,
+ ": " + 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: "

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 void testAvoidLoopbackTcpSockets() throws Exception {
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,13 +188,9 @@ public void testVerifyHostnamesException() throws UnknownHostException {
} }
@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");
}
} }
/** /**
@ -268,6 +264,17 @@ public void testWrapEOFException() throws Throwable {
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");
@ -353,10 +360,8 @@ private IOException verifyExceptionClass(IOException e,
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 "