From 6b4564f1d59ab679512be0d244165e1f272cb9bd Mon Sep 17 00:00:00 2001 From: Inigo Goiri Date: Sun, 11 Aug 2019 20:34:36 -0700 Subject: [PATCH] HADOOP-16453. Update how exceptions are handled in NetUtils. Contributed by Lisheng Sun. --- .../main/java/org/apache/hadoop/net/NetUtils.java | 10 +++++++--- .../test/java/org/apache/hadoop/ipc/TestIPC.java | 7 +++---- .../java/org/apache/hadoop/net/TestNetUtils.java | 15 +++++---------- 3 files changed, 15 insertions(+), 17 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 acdec9388e..d98254cb1c 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 @@ -804,7 +804,11 @@ public static IOException wrapException(final String destHost, + ";" + see("SocketException")); } else { - // Return instance of same type if Exception has a String constructor + // 1. Return instance of same type with exception msg if Exception has a + // String constructor. + // 2. Return instance of same type if Exception doesn't have a String + // constructor. + // Related HADOOP-16453. return wrapWithMessage(exception, "DestHost:destPort " + destHost + ":" + destPort + " , LocalHost:localPort " + localHost @@ -832,9 +836,9 @@ private static T wrapWithMessage( Constructor ctor = clazz.getConstructor(String.class); Throwable t = ctor.newInstance(msg); return (T)(t.initCause(exception)); + } catch (NoSuchMethodException e) { + return exception; } catch (Throwable e) { - LOG.trace("Unable to wrap exception of type {}: it has no (String) " - + "constructor", clazz, e); throw exception; } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java index 1c1ad00451..1921a35821 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java @@ -1582,11 +1582,10 @@ public void testRpcResponseLimit() throws Throwable { try { call(client, 0, addr, conf); } catch (IOException ioe) { - Throwable t = ioe.getCause(); - Assert.assertNotNull(t); - Assert.assertEquals(RpcException.class, t.getClass()); + Assert.assertNotNull(ioe); + Assert.assertEquals(RpcException.class, ioe.getClass()); Assert.assertEquals("RPC response exceeds maximum data length", - t.getMessage()); + ioe.getMessage()); return; } Assert.fail("didn't get limit exceeded"); 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 62bd1b142e..b11b1e96de 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 @@ -279,11 +279,9 @@ public void testWrapKerbAuthException() throws Throwable { @Test public void testWrapIOEWithNoStringConstructor() throws Throwable { IOException e = new CharacterCodingException(); - IOException wrapped = verifyExceptionClass(e, IOException.class); - assertInException(wrapped, "Failed on local exception"); - assertNotInException(wrapped, NetUtils.HADOOP_WIKI); - assertInException(wrapped, "Host Details "); - assertRemoteDetailsIncluded(wrapped); + IOException wrapped = + verifyExceptionClass(e, CharacterCodingException.class); + assertEquals(null, wrapped.getMessage()); } @Test @@ -295,11 +293,8 @@ private TestIOException(String cause){ } } IOException e = new TestIOException(); - IOException wrapped = verifyExceptionClass(e, IOException.class); - assertInException(wrapped, "Failed on local exception"); - assertNotInException(wrapped, NetUtils.HADOOP_WIKI); - assertInException(wrapped, "Host Details "); - assertRemoteDetailsIncluded(wrapped); + IOException wrapped = verifyExceptionClass(e, TestIOException.class); + assertEquals(null, wrapped.getMessage()); } @Test