HADOOP-15178. Generalize NetUtils#wrapException to handle other subclasses with String Constructor. Contributed by Ajay Kumar.
This commit is contained in:
parent
1e85a995d1
commit
28f644bf25
@ -715,9 +715,9 @@ public static boolean isLocalAddress(InetAddress addr) {
|
|||||||
* return an IOException with the input exception as the cause and also
|
* return an IOException with the input exception as the cause and also
|
||||||
* include the host details. The new exception provides the stack trace of the
|
* include the host details. The new exception provides the stack trace of the
|
||||||
* place where the exception is thrown and some extra diagnostics information.
|
* place where the exception is thrown and some extra diagnostics information.
|
||||||
* If the exception is BindException or ConnectException or
|
* If the exception is of type BindException, ConnectException,
|
||||||
* UnknownHostException or SocketTimeoutException, return a new one of the
|
* UnknownHostException, SocketTimeoutException or has a String constructor,
|
||||||
* same type; Otherwise return an IOException.
|
* return a new one of the same type; Otherwise return an IOException.
|
||||||
*
|
*
|
||||||
* @param destHost target host (nullable)
|
* @param destHost target host (nullable)
|
||||||
* @param destPort target port
|
* @param destPort target port
|
||||||
@ -731,6 +731,7 @@ public static IOException wrapException(final String destHost,
|
|||||||
final String localHost,
|
final String localHost,
|
||||||
final int localPort,
|
final int localPort,
|
||||||
final IOException exception) {
|
final IOException exception) {
|
||||||
|
try {
|
||||||
if (exception instanceof BindException) {
|
if (exception instanceof BindException) {
|
||||||
return wrapWithMessage(exception,
|
return wrapWithMessage(exception,
|
||||||
"Problem binding to ["
|
"Problem binding to ["
|
||||||
@ -800,14 +801,20 @@ public static IOException wrapException(final String destHost,
|
|||||||
+ " failed on socket exception: " + exception
|
+ " failed on socket exception: " + exception
|
||||||
+ ";"
|
+ ";"
|
||||||
+ see("SocketException"));
|
+ see("SocketException"));
|
||||||
|
} else {
|
||||||
|
// Return instance of same type if Exception has a String constructor
|
||||||
|
return wrapWithMessage(exception,
|
||||||
|
"DestHost:destPort " + destHost + ":" + destPort
|
||||||
|
+ " , LocalHost:localPort " + localHost
|
||||||
|
+ ":" + localPort + ". Failed on local exception: " +
|
||||||
|
exception);
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
} catch (IOException ex) {
|
||||||
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -817,16 +824,16 @@ private static String see(final String entry) {
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private static <T extends IOException> T wrapWithMessage(
|
private static <T extends IOException> T wrapWithMessage(
|
||||||
T exception, String msg) {
|
T exception, String msg) throws T {
|
||||||
Class<? extends Throwable> clazz = exception.getClass();
|
Class<? extends Throwable> clazz = exception.getClass();
|
||||||
try {
|
try {
|
||||||
Constructor<? extends Throwable> ctor = clazz.getConstructor(String.class);
|
Constructor<? extends Throwable> ctor = clazz.getConstructor(String.class);
|
||||||
Throwable t = ctor.newInstance(msg);
|
Throwable t = ctor.newInstance(msg);
|
||||||
return (T)(t.initCause(exception));
|
return (T)(t.initCause(exception));
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
LOG.warn("Unable to wrap exception of type " +
|
LOG.warn("Unable to wrap exception of type {}: it has no (String) "
|
||||||
clazz + ": it has no (String) constructor", e);
|
+ "constructor", clazz, e);
|
||||||
return exception;
|
throw exception;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.nio.charset.CharacterCodingException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -40,6 +41,7 @@
|
|||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.hadoop.conf.Configuration;
|
import org.apache.hadoop.conf.Configuration;
|
||||||
import org.apache.hadoop.io.IOUtils;
|
import org.apache.hadoop.io.IOUtils;
|
||||||
|
import org.apache.hadoop.security.KerberosAuthException;
|
||||||
import org.apache.hadoop.security.NetUtilsTestResolver;
|
import org.apache.hadoop.security.NetUtilsTestResolver;
|
||||||
import org.junit.Assume;
|
import org.junit.Assume;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -262,6 +264,44 @@ public void testWrapEOFException() throws Throwable {
|
|||||||
assertInException(wrapped, "/EOFException");
|
assertInException(wrapped, "/EOFException");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapKerbAuthException() throws Throwable {
|
||||||
|
IOException e = new KerberosAuthException("socket timeout on connection");
|
||||||
|
IOException wrapped = verifyExceptionClass(e, KerberosAuthException.class);
|
||||||
|
assertInException(wrapped, "socket timeout on connection");
|
||||||
|
assertInException(wrapped, "localhost");
|
||||||
|
assertInException(wrapped, "DestHost:destPort ");
|
||||||
|
assertInException(wrapped, "LocalHost:localPort");
|
||||||
|
assertRemoteDetailsIncluded(wrapped);
|
||||||
|
assertInException(wrapped, "KerberosAuthException");
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrapIOEWithPrivateStringConstructor() throws Throwable {
|
||||||
|
class TestIOException extends CharacterCodingException{
|
||||||
|
private TestIOException(String cause){
|
||||||
|
}
|
||||||
|
TestIOException(){
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrapSocketException() throws Throwable {
|
public void testWrapSocketException() throws Throwable {
|
||||||
IOException wrapped = verifyExceptionClass(new SocketException("failed"),
|
IOException wrapped = verifyExceptionClass(new SocketException("failed"),
|
||||||
|
Loading…
Reference in New Issue
Block a user