helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zzh...@apache.org
Subject git commit: HELIX-199: ZNRecord should not publish rawPayload unless it exists, rb=13393
Date Fri, 09 Aug 2013 18:26:39 GMT
Updated Branches:
  refs/heads/master dd136710d -> 2bf12e92c


HELIX-199: ZNRecord should not publish rawPayload unless it exists, rb=13393


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

Branch: refs/heads/master
Commit: 2bf12e92c50b1ac3e8c8cd956933d64863a89b84
Parents: dd13671
Author: zzhang <zzhang5@uci.edu>
Authored: Fri Aug 9 11:26:35 2013 -0700
Committer: zzhang <zzhang5@uci.edu>
Committed: Fri Aug 9 11:26:35 2013 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/helix/ZNRecord.java    | 16 +++++++--
 .../manager/zk/ZNRecordStreamingSerializer.java | 10 ++++--
 .../TestJacksonPayloadSerializer.java           | 35 ++++++++++++++++++++
 3 files changed, 55 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/2bf12e92/helix-core/src/main/java/org/apache/helix/ZNRecord.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/ZNRecord.java b/helix-core/src/main/java/org/apache/helix/ZNRecord.java
index 130d673..4cf5449 100644
--- a/helix-core/src/main/java/org/apache/helix/ZNRecord.java
+++ b/helix-core/src/main/java/org/apache/helix/ZNRecord.java
@@ -32,6 +32,8 @@ import org.codehaus.jackson.annotate.JsonCreator;
 import org.codehaus.jackson.annotate.JsonIgnore;
 import org.codehaus.jackson.annotate.JsonIgnoreProperties;
 import org.codehaus.jackson.annotate.JsonProperty;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
 
 
 /**
@@ -39,6 +41,7 @@ import org.codehaus.jackson.annotate.JsonProperty;
  * simpleFields mapFields listFields
  */
 @JsonIgnoreProperties(ignoreUnknown = true)
+@JsonSerialize(include = Inclusion.NON_NULL)
 public class ZNRecord
 {
   static Logger _logger = Logger.getLogger(ZNRecord.class);
@@ -74,7 +77,7 @@ public class ZNRecord
     simpleFields = new TreeMap<String, String>();
     mapFields = new TreeMap<String, Map<String, String>>();
     listFields = new TreeMap<String, List<String>>();
-    rawPayload = new byte[0];
+    rawPayload = null;
     _serializer = new JacksonPayloadSerializer();
   }
 
@@ -89,8 +92,15 @@ public class ZNRecord
     simpleFields.putAll(record.getSimpleFields());
     mapFields.putAll(record.getMapFields());
     listFields.putAll(record.getListFields());
-    rawPayload = new byte[record.rawPayload.length];
-    System.arraycopy(record.rawPayload, 0, rawPayload, 0, record.rawPayload.length);
+    if (record.rawPayload != null)
+    {
+      rawPayload = new byte[record.rawPayload.length];
+      System.arraycopy(record.rawPayload, 0, rawPayload, 0, record.rawPayload.length);
+    }
+    else
+    {
+      rawPayload = null;
+    }
     _version = record.getVersion();
     _creationTime = record.getCreationTime();
     _modifiedTime = record.getModifiedTime();

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/2bf12e92/helix-core/src/main/java/org/apache/helix/manager/zk/ZNRecordStreamingSerializer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/ZNRecordStreamingSerializer.java
b/helix-core/src/main/java/org/apache/helix/manager/zk/ZNRecordStreamingSerializer.java
index 1504ff8..2f45369 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/ZNRecordStreamingSerializer.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ZNRecordStreamingSerializer.java
@@ -153,9 +153,13 @@ public class ZNRecordStreamingSerializer implements ZkSerializer
       g.writeRaw("\n  ");
       g.writeEndObject(); // for mapFields
 
-      // write rawPayload
-      g.writeRaw("\n  ");
-      g.writeStringField("rawPayload", Base64.encode(record.getRawPayload(), false));
+      byte[] rawPayload = record.getRawPayload();
+      if (rawPayload != null && rawPayload.length > 0)
+      {
+        // write rawPayload
+        g.writeRaw("\n  ");
+        g.writeStringField("rawPayload", Base64.encode(rawPayload, false));
+      }
 
       g.writeRaw("\n");
       g.writeEndObject(); // for whole znrecord

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/2bf12e92/helix-core/src/test/java/org/apache/helix/manager/zk/serializer/TestJacksonPayloadSerializer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/manager/zk/serializer/TestJacksonPayloadSerializer.java
b/helix-core/src/test/java/org/apache/helix/manager/zk/serializer/TestJacksonPayloadSerializer.java
index 8d03ec5..86075f7 100644
--- a/helix-core/src/test/java/org/apache/helix/manager/zk/serializer/TestJacksonPayloadSerializer.java
+++ b/helix-core/src/test/java/org/apache/helix/manager/zk/serializer/TestJacksonPayloadSerializer.java
@@ -19,14 +19,18 @@ package org.apache.helix.manager.zk.serializer;
  * under the License.
  */
 
+import java.io.IOException;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.apache.helix.ZNRecord;
 import org.apache.helix.manager.zk.ZNRecordSerializer;
 import org.apache.helix.manager.zk.ZNRecordStreamingSerializer;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.JsonProcessingException;
 import org.codehaus.jackson.annotate.JsonIgnoreProperties;
 import org.codehaus.jackson.annotate.JsonProperty;
+import org.codehaus.jackson.map.ObjectMapper;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
@@ -88,6 +92,37 @@ public class TestJacksonPayloadSerializer
   }
 
   /**
+   * Test that the payload is not included whenever it is not null. This is mainly to maintain
backward
+   * compatibility.
+   */
+  @Test
+  public void testRawPayloadMissingIfUnspecified()
+  {
+    final String RECORD_ID = "testRawPayloadMissingIfUnspecified";
+    ZNRecord znRecord = new ZNRecord(RECORD_ID);
+    ZNRecordSerializer znRecordSerializer = new ZNRecordSerializer();
+    byte[] serialized = znRecordSerializer.serialize(znRecord);
+    ZNRecordStreamingSerializer znRecordStreamingSerializer = new ZNRecordStreamingSerializer();
+    byte[] streamingSerialized = znRecordStreamingSerializer.serialize(znRecord);
+    ObjectMapper mapper = new ObjectMapper();
+    try
+    {
+      JsonNode jsonNode = mapper.readTree(new String(serialized));
+      Assert.assertFalse(jsonNode.has("rawPayload"));
+      JsonNode streamingJsonNode = mapper.readTree(new String(streamingSerialized));
+      Assert.assertFalse(streamingJsonNode.has("rawPayload"));
+    }
+    catch (JsonProcessingException e)
+    {
+      Assert.fail();
+    }
+    catch (IOException e)
+    {
+      Assert.fail();
+    }
+  }
+
+  /**
    * Get an object which can be tested for serialization success or failure
    * @return Initialized SampleDeserialized object
    */


Mime
View raw message