Apache Lucene open-source search software
SOLR-13006: ZkNodeProps to be able to load from both javabin and JSON
Noble Paul492c3440def2
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 79941423890..d323c9dbc12 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -270,6 +270,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----------------------diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.java b/solr/core/src/test/org/apache/solr/cloud/ZkNodePropsTest.javaindex 11e93d67655..88ce3c865d4 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)));}}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.javaindex 93fe59ad873..752ce45de1e 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);}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.javaindex 06d07380f29..bbcc129fd14 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);}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.javaindex 2e49cf9b98c..ca8c80fdecf 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 +