You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by dh...@apache.org on 2016/07/12 00:52:37 UTC

[2/2] incubator-beam git commit: AvroCoder: fix findbugs errors

AvroCoder: fix findbugs errors

* Updated the AvroCoder class to set unserializable fields as transient.
* Updated the findbugs_filter to remove ignored errors.
* Added tests.


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/d2ad9511
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/d2ad9511
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/d2ad9511

Branch: refs/heads/master
Commit: d2ad95116ab895a429d33f2f3edff897c10db2c6
Parents: 3653c5c
Author: Ilya Ganelin <il...@capitalone.com>
Authored: Mon Jul 11 14:36:45 2016 -0400
Committer: Dan Halperin <dh...@google.com>
Committed: Mon Jul 11 17:52:30 2016 -0700

----------------------------------------------------------------------
 .../src/main/resources/beam/findbugs-filter.xml | 36 --------------------
 .../org/apache/beam/sdk/coders/AvroCoder.java   | 10 +++---
 .../apache/beam/sdk/coders/AvroCoderTest.java   | 26 ++++++++++++++
 3 files changed, 31 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/d2ad9511/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
----------------------------------------------------------------------
diff --git a/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml b/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
index e15cf7b..ee55ef0 100644
--- a/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
+++ b/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
@@ -30,42 +30,6 @@
           the issue.
         -->
   <Match>
-    <Class name="org.apache.beam.sdk.coders.AvroCoder"/>
-    <Field name="decoder"/>
-    <Bug pattern="SE_BAD_FIELD"/>
-    <!--[BEAM-394] Non-transient non-serializable instance field in serializable class-->
-  </Match>
-  <Match>
-    <Class name="org.apache.beam.sdk.coders.AvroCoder"/>
-    <Field name="encoder"/>
-    <Bug pattern="SE_BAD_FIELD"/>
-    <!--[BEAM-394] Non-transient non-serializable instance field in serializable class-->
-  </Match>
-  <Match>
-    <Class name="org.apache.beam.sdk.coders.AvroCoder"/>
-    <Field name="reader"/>
-    <Bug pattern="SE_BAD_FIELD"/>
-    <!--[BEAM-394] Non-transient non-serializable instance field in serializable class-->
-  </Match>
-  <Match>
-    <Class name="org.apache.beam.sdk.coders.AvroCoder"/>
-    <Field name="writer"/>
-    <Bug pattern="SE_BAD_FIELD"/>
-    <!--[BEAM-394] Non-transient non-serializable instance field in serializable class-->
-  </Match>
-  <Match>
-    <Class name="org.apache.beam.sdk.coders.AvroCoder"/>
-    <Field name="reader"/>
-    <Bug pattern="SE_BAD_FIELD_STORE"/>
-    <!--[BEAM-394] Non-serializable value stored into instance field of a serializable class-->
-  </Match>
-  <Match>
-    <Class name="org.apache.beam.sdk.coders.AvroCoder"/>
-    <Field name="writer"/>
-    <Bug pattern="SE_BAD_FIELD_STORE"/>
-    <!--[BEAM-394] Non-serializable value stored into instance field of a serializable class-->
-  </Match>
-  <Match>
     <Class name="org.apache.beam.sdk.coders.Coder$NonDeterministicException"/>
     <Bug pattern="NM_CLASS_NOT_EXCEPTION"/>
     <!--[BEAM-396] Class is not derived from an Exception, even though it is named as such-->

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/d2ad9511/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/AvroCoder.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/AvroCoder.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/AvroCoder.java
index 00c1cbc..873a591 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/AvroCoder.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/AvroCoder.java
@@ -168,7 +168,7 @@ public class AvroCoder<T> extends StandardCoder<T> {
   };
 
   private final Class<T> type;
-  private final Schema schema;
+  private final transient Schema schema;
 
   private final List<String> nonDeterministicReasons;
 
@@ -178,10 +178,10 @@ public class AvroCoder<T> extends StandardCoder<T> {
   // Cache the old encoder/decoder and let the factories reuse them when possible. To be threadsafe,
   // these are ThreadLocal. This code does not need to be re-entrant as AvroCoder does not use
   // an inner coder.
-  private final ThreadLocal<BinaryDecoder> decoder;
-  private final ThreadLocal<BinaryEncoder> encoder;
-  private final ThreadLocal<DatumWriter<T>> writer;
-  private final ThreadLocal<DatumReader<T>> reader;
+  private final transient ThreadLocal<BinaryDecoder> decoder;
+  private final transient ThreadLocal<BinaryEncoder> encoder;
+  private final transient ThreadLocal<DatumWriter<T>> writer;
+  private final transient ThreadLocal<DatumReader<T>> reader;
 
   protected AvroCoder(Class<T> type, Schema schema) {
     this.type = type;

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/d2ad9511/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/AvroCoderTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/AvroCoderTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/AvroCoderTest.java
index 207bfdd..54f7ec1 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/AvroCoderTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/AvroCoderTest.java
@@ -62,6 +62,8 @@ import org.junit.runners.JUnit4;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -148,6 +150,30 @@ public class AvroCoderTest {
         Matchers.containsInAnyOrder("@type", "type", "schema", "encoding_id"));
   }
 
+  /**
+   * Confirm that we can serialize and deserialize an AvroCoder object and still decode after.
+   * (BEAM-349).
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testTransientFieldInitialization() throws Exception {
+    Pojo value = new Pojo("Hello", 42);
+    AvroCoder<Pojo> coder = AvroCoder.of(Pojo.class);
+
+    //Serialization of object
+    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+    ObjectOutputStream out = new ObjectOutputStream(bos);
+    out.writeObject(coder);
+
+    //De-serialization of object
+    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
+    ObjectInputStream in = new ObjectInputStream(bis);
+    AvroCoder<Pojo> copied = (AvroCoder<Pojo>) in.readObject();
+
+    CoderProperties.coderDecodeEncodeEqual(copied, value);
+  }
+
   @Test
   public void testPojoEncoding() throws Exception {
     Pojo value = new Pojo("Hello", 42);