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

This commit is contained in:
Noble Paul 2018-11-21 18:20:03 +11:00
parent ea304a3a32
commit 492c3440de
5 changed files with 42 additions and 9 deletions

View File

@ -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
----------------------

View File

@ -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)));
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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 +