YARN-3745. SerializedException should also try to instantiate internal

exception with the default constructor. Contributed by Lavkesh Lahngir.
This commit is contained in:
Devaraj K 2015-06-25 17:38:11 +05:30
parent 57f1a01eda
commit b381f88c71
3 changed files with 36 additions and 6 deletions

View File

@ -556,6 +556,9 @@ Release 2.8.0 - UNRELEASED
YARN-3826. Race condition in ResourceTrackerService leads to YARN-3826. Race condition in ResourceTrackerService leads to
wrong diagnostics messages. (Chengbing Liu via devaraj) wrong diagnostics messages. (Chengbing Liu via devaraj)
YARN-3745. SerializedException should also try to instantiate internal
exception with the default constructor. (Lavkesh Lahngir via devaraj)
Release 2.7.1 - UNRELEASED Release 2.7.1 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -158,15 +158,31 @@ public class SerializedExceptionPBImpl extends SerializedException {
viaProto = false; viaProto = false;
} }
private static <T extends Throwable> T instantiateException( private static <T extends Throwable> T instantiateExceptionImpl(
Class<? extends T> cls, String message, Throwable cause) { String message, Class<? extends T> cls, Throwable cause)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Constructor<? extends T> cn; Constructor<? extends T> cn;
T ex = null; T ex = null;
cn =
cls.getConstructor(message == null ? new Class[0]
: new Class[] {String.class});
cn.setAccessible(true);
ex = message == null ? cn.newInstance() : cn.newInstance(message);
ex.initCause(cause);
return ex;
}
private static <T extends Throwable> T instantiateException(
Class<? extends T> cls, String message, Throwable cause) {
T ex = null;
try { try {
cn = cls.getConstructor(String.class); // Try constructor with String argument, if it fails, try default.
cn.setAccessible(true); try {
ex = cn.newInstance(message); ex = instantiateExceptionImpl(message, cls, cause);
ex.initCause(cause); } catch (NoSuchMethodException e) {
ex = instantiateExceptionImpl(null, cls, cause);
}
} catch (SecurityException e) { } catch (SecurityException e) {
throw new YarnRuntimeException(e); throw new YarnRuntimeException(e);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {

View File

@ -18,6 +18,8 @@
package org.apache.hadoop.yarn.api.records.impl.pb; package org.apache.hadoop.yarn.api.records.impl.pb;
import java.nio.channels.ClosedChannelException;
import org.junit.Assert; import org.junit.Assert;
import org.apache.hadoop.yarn.api.records.impl.pb.SerializedExceptionPBImpl; import org.apache.hadoop.yarn.api.records.impl.pb.SerializedExceptionPBImpl;
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
@ -54,6 +56,15 @@ public class TestSerializedExceptionPBImpl {
Assert.assertEquals(ex.toString(), pb.deSerialize().toString()); Assert.assertEquals(ex.toString(), pb.deSerialize().toString());
} }
@Test
public void testDeserializeWithDefaultConstructor() {
// Init SerializedException with an Exception with default constructor.
ClosedChannelException ex = new ClosedChannelException();
SerializedExceptionPBImpl pb = new SerializedExceptionPBImpl();
pb.init(ex);
Assert.assertEquals(ex.getClass(), pb.deSerialize().getClass());
}
@Test @Test
public void testBeforeInit() throws Exception { public void testBeforeInit() throws Exception {
SerializedExceptionProto defaultProto = SerializedExceptionProto defaultProto =