You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by no...@apache.org on 2018/11/21 07:21:24 UTC

lucene-solr:branch_7x: SOLR-13006: ZkNodeProps to be able to load from both javabin and JSON

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x bf591a04c -> 84705ccc0


SOLR-13006: ZkNodeProps to be able to load from both javabin and JSON


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/84705ccc
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/84705ccc
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/84705ccc

Branch: refs/heads/branch_7x
Commit: 84705ccc092ebd54fbf18d513479962812cb9a89
Parents: bf591a0
Author: Noble Paul <no...@apache.org>
Authored: Wed Nov 21 18:20:03 2018 +1100
Committer: Noble Paul <no...@apache.org>
Committed: Wed Nov 21 18:21:09 2018 +1100

----------------------------------------------------------------------
 solr/CHANGES.txt                                   |  2 ++
 .../org/apache/solr/cloud/ZkNodePropsTest.java     | 17 ++++++++++-------
 .../org/apache/solr/common/cloud/ZkNodeProps.java  | 13 ++++++++++++-
 .../apache/solr/common/util/FastInputStream.java   |  6 +++++-
 .../org/apache/solr/common/util/JavaBinCodec.java  | 13 +++++++++++++
 5 files changed, 42 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/84705ccc/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 8d73fa3..cb7ee51 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -189,6 +189,8 @@ Other Changes
 * SOLR-12497: Add documentation to use Hadoop credential provider-based keystore/trustsore.
 (Mano Kovacs, Cassandra Targett)
 
+* SOLR-13006: ZkNodeProps to be able to load from both javabin and JSON (noble)
+
 Bug Fixes
 ----------------------
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/84705ccc/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java b/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
index 11e93d6..88ce3c8 100644
--- a/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java
@@ -22,7 +22,9 @@ import java.util.Map;
 
 import org.apache.solr.SolrTestCaseJ4;
 import org.apache.solr.common.cloud.ZkNodeProps;
+import org.apache.solr.common.util.JavaBinCodec;
 import org.apache.solr.common.util.Utils;
+import org.apache.solr.util.SimplePostTool;
 import org.junit.Test;
 
 public class ZkNodePropsTest extends SolrTestCaseJ4 {
@@ -39,13 +41,14 @@ public class ZkNodePropsTest extends SolrTestCaseJ4 {
     
     ZkNodeProps zkProps = new ZkNodeProps(props);
     byte[] bytes = Utils.toJSON(zkProps);
-    
     ZkNodeProps props2 = ZkNodeProps.load(bytes);
-    assertEquals("value1", props2.getStr("prop1"));
-    assertEquals("value2", props2.getStr("prop2"));
-    assertEquals("value3", props2.getStr("prop3"));
-    assertEquals("value4", props2.getStr("prop4"));
-    assertEquals("value5", props2.getStr("prop5"));
-    assertEquals("value6", props2.getStr("prop6"));
+
+    props.forEach((s, o) -> assertEquals(o, props2.get(s)));
+    SimplePostTool.BAOS baos = new SimplePostTool.BAOS();
+    new JavaBinCodec().marshal(zkProps.getProperties(), baos);
+    bytes = baos.toByteArray();
+    System.out.println("BIN size : " + bytes.length);
+    ZkNodeProps props3 = ZkNodeProps.load(bytes);
+    props.forEach((s, o) -> assertEquals(o, props3.get(s)));
   }
 }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/84705ccc/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java
index 93fe59a..752ce45 100644
--- a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java
+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java
@@ -16,11 +16,13 @@
  */
 package org.apache.solr.common.cloud;
 
+import java.io.IOException;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.solr.common.util.JavaBinCodec;
 import org.apache.solr.common.util.Utils;
 import org.noggit.JSONUtil;
 import org.noggit.JSONWriter;
@@ -89,7 +91,16 @@ public class ZkNodeProps implements JSONWriter.Writable {
    * Create Replica from json string that is typically stored in zookeeper.
    */
   public static ZkNodeProps load(byte[] bytes) {
-    Map<String, Object> props = (Map<String, Object>) Utils.fromJSON(bytes);
+    Map<String, Object> props = null;
+    if (bytes[0] == 2) {
+      try {
+        props = (Map<String, Object>) new JavaBinCodec().unmarshal(bytes);
+      } catch (IOException e) {
+        throw new RuntimeException("Unable to parse javabin content");
+      }
+    } else {
+      props = (Map<String, Object>) Utils.fromJSON(bytes);
+    }
     return new ZkNodeProps(props);
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/84705ccc/solr/solrj/src/java/org/apache/solr/common/util/FastInputStream.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/FastInputStream.java b/solr/solrj/src/java/org/apache/solr/common/util/FastInputStream.java
index 06d0738..bbcc129 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/FastInputStream.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/FastInputStream.java
@@ -16,7 +16,10 @@
  */
 package org.apache.solr.common.util;
 
-import java.io.*;
+import java.io.DataInputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
 
 /** Single threaded buffered InputStream
  *  Internal Solr use only, subject to change.
@@ -76,6 +79,7 @@ public class FastInputStream extends DataInputInputStream {
   }
 
   public int readWrappedStream(byte[] target, int offset, int len) throws IOException {
+    if(in == null) return -1;
     return in.read(target, offset, len);
   }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/84705ccc/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
index 2e49cf9..ca8c80f 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/JavaBinCodec.java
@@ -179,6 +179,10 @@ public class JavaBinCodec implements PushWriter {
 
   byte version;
 
+  public Object unmarshal(byte[] buf) throws IOException {
+    FastInputStream dis = initRead(buf);
+    return readVal(dis);
+  }
   public Object unmarshal(InputStream is) throws IOException {
     FastInputStream dis = initRead(is);
     return readVal(dis);
@@ -187,6 +191,15 @@ public class JavaBinCodec implements PushWriter {
   protected FastInputStream initRead(InputStream is) throws IOException {
     assert !alreadyUnmarshalled;
     FastInputStream dis = FastInputStream.wrap(is);
+    return _init(dis);
+  }
+  protected FastInputStream initRead(byte[] buf) throws IOException {
+    assert !alreadyUnmarshalled;
+    FastInputStream dis = new FastInputStream(null, buf, 0, buf.length);
+    return _init(dis);
+  }
+
+  private FastInputStream _init(FastInputStream dis) throws IOException {
     version = dis.readByte();
     if (version != VERSION) {
       throw new RuntimeException("Invalid version (expected " + VERSION +