From 2b98e9cfd965516467cb38ff023ccae4e4081952 Mon Sep 17 00:00:00 2001 From: Junping Du Date: Mon, 2 Jun 2014 08:27:08 +0000 Subject: [PATCH] Merge r1599115 from trunk: YARN-2103. Inconsistency between viaProto flag and initial value of SerializedExceptionProto.Builder (Contributed by Binglin Chang) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1599121 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-yarn-project/CHANGES.txt | 3 + .../impl/pb/SerializedExceptionPBImpl.java | 22 +++++- .../pb/TestSerializedExceptionPBImpl.java | 71 +++++++++++++++++++ 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/impl/pb/TestSerializedExceptionPBImpl.java diff --git a/hadoop-yarn-project/CHANGES.txt b/hadoop-yarn-project/CHANGES.txt index 5753f9fe503..20ca49a16c8 100644 --- a/hadoop-yarn-project/CHANGES.txt +++ b/hadoop-yarn-project/CHANGES.txt @@ -165,6 +165,9 @@ Release 2.5.0 - UNRELEASED YARN-1868. YARN status web ui does not show correctly in IE 11. (Chuan Liu via cnauroth) + YARN-2103. Inconsistency between viaProto flag and initial value of + SerializedExceptionProto.Builder (Binglin Chang via junping_du) + Release 2.4.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 5290013a088..e683fd5ac59 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 @@ -32,9 +32,9 @@ import org.apache.hadoop.yarn.proto.YarnProtos.SerializedExceptionProtoOrBuilder public class SerializedExceptionPBImpl extends SerializedException { - SerializedExceptionProto proto = SerializedExceptionProto - .getDefaultInstance(); - SerializedExceptionProto.Builder builder = null; + SerializedExceptionProto proto = null; + SerializedExceptionProto.Builder builder = + SerializedExceptionProto.newBuilder(); boolean viaProto = false; public SerializedExceptionPBImpl() { @@ -135,6 +135,22 @@ public class SerializedExceptionPBImpl extends SerializedException { return proto; } + @Override + public int hashCode() { + return getProto().hashCode(); + } + + @Override + public boolean equals(Object other) { + if (other == null) { + return false; + } + if (other.getClass().isAssignableFrom(this.getClass())) { + return this.getProto().equals(this.getClass().cast(other).getProto()); + } + return false; + } + private void maybeInitBuilder() { if (viaProto || builder == null) { builder = SerializedExceptionProto.newBuilder(proto); 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 new file mode 100644 index 00000000000..b011e3eb6d0 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/api/records/impl/pb/TestSerializedExceptionPBImpl.java @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.api.records.impl.pb; + +import org.junit.Assert; +import org.apache.hadoop.yarn.api.records.impl.pb.SerializedExceptionPBImpl; +import org.apache.hadoop.yarn.exceptions.YarnRuntimeException; +import org.apache.hadoop.yarn.proto.YarnProtos.SerializedExceptionProto; +import org.junit.Test; + +public class TestSerializedExceptionPBImpl { + @Test + public void testSerializedException() throws Exception { + SerializedExceptionPBImpl orig = new SerializedExceptionPBImpl(); + orig.init(new Exception("test exception")); + SerializedExceptionProto proto = orig.getProto(); + SerializedExceptionPBImpl deser = new SerializedExceptionPBImpl(proto); + Assert.assertEquals(orig, deser); + Assert.assertEquals(orig.getMessage(), deser.getMessage()); + Assert.assertEquals(orig.getRemoteTrace(), deser.getRemoteTrace()); + Assert.assertEquals(orig.getCause(), deser.getCause()); + } + + @Test + public void testDeserialize() throws Exception { + Exception ex = new Exception("test exception"); + SerializedExceptionPBImpl pb = new SerializedExceptionPBImpl(); + + try { + pb.deSerialize(); + Assert.fail("deSerialze should throw YarnRuntimeException"); + } catch (YarnRuntimeException e) { + Assert.assertEquals(ClassNotFoundException.class, + e.getCause().getClass()); + } + + pb.init(ex); + Assert.assertEquals(ex.toString(), pb.deSerialize().toString()); + } + + @Test + public void testBeforeInit() throws Exception { + SerializedExceptionProto defaultProto = + SerializedExceptionProto.newBuilder().build(); + + SerializedExceptionPBImpl pb1 = new SerializedExceptionPBImpl(); + Assert.assertNull(pb1.getCause()); + + SerializedExceptionPBImpl pb2 = new SerializedExceptionPBImpl(); + Assert.assertEquals(defaultProto, pb2.getProto()); + + SerializedExceptionPBImpl pb3 = new SerializedExceptionPBImpl(); + Assert.assertEquals(defaultProto.getTrace(), pb3.getRemoteTrace()); + } +}