From b381f88c71d18497deb35039372b1e9715d2c038 Mon Sep 17 00:00:00 2001 From: Devaraj K Date: Thu, 25 Jun 2015 17:38:11 +0530 Subject: [PATCH] YARN-3745. SerializedException should also try to instantiate internal exception with the default constructor. Contributed by Lavkesh Lahngir. --- hadoop-yarn-project/CHANGES.txt | 3 ++ .../impl/pb/SerializedExceptionPBImpl.java | 28 +++++++++++++++---- .../pb/TestSerializedExceptionPBImpl.java | 11 ++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/hadoop-yarn-project/CHANGES.txt b/hadoop-yarn-project/CHANGES.txt index 3d608fdbf5..7dc02cd258 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -556,6 +556,9 @@ Release 2.8.0 - UNRELEASED YARN-3826. Race condition in ResourceTrackerService leads to 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 INCOMPATIBLE CHANGES diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/SerializedExceptionPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/SerializedExceptionPBImpl.java index e683fd5ac5..fd9e170dcd 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/SerializedExceptionPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/SerializedExceptionPBImpl.java @@ -158,15 +158,31 @@ private void maybeInitBuilder() { viaProto = false; } - private static T instantiateException( - Class cls, String message, Throwable cause) { + private static T instantiateExceptionImpl( + String message, Class cls, Throwable cause) + throws NoSuchMethodException, InstantiationException, + IllegalAccessException, InvocationTargetException { Constructor cn; 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 instantiateException( + Class cls, String message, Throwable cause) { + T ex = null; try { - cn = cls.getConstructor(String.class); - cn.setAccessible(true); - ex = cn.newInstance(message); - ex.initCause(cause); + // Try constructor with String argument, if it fails, try default. + try { + ex = instantiateExceptionImpl(message, cls, cause); + } catch (NoSuchMethodException e) { + ex = instantiateExceptionImpl(null, cls, cause); + } } catch (SecurityException e) { throw new YarnRuntimeException(e); } catch (NoSuchMethodException e) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/impl/pb/TestSerializedExceptionPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/impl/pb/TestSerializedExceptionPBImpl.java index b011e3eb6d..ac7e40ebb4 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/impl/pb/TestSerializedExceptionPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/impl/pb/TestSerializedExceptionPBImpl.java @@ -18,6 +18,8 @@ package org.apache.hadoop.yarn.api.records.impl.pb; +import java.nio.channels.ClosedChannelException; + import org.junit.Assert; import org.apache.hadoop.yarn.api.records.impl.pb.SerializedExceptionPBImpl; import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; @@ -54,6 +56,15 @@ public void testDeserialize() throws Exception { 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 public void testBeforeInit() throws Exception { SerializedExceptionProto defaultProto =