Skip to content

Apache Lucene open-source search software

SOLR-6397: zkcli script put/putfile should allow overwriting an existing znode's data

Timothy Potter007a6a161068
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 19300d66e18..6ca6db372a1 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -357,6 +357,9 @@ Bug Fixes
* SOLR-6850: AutoAddReplicas makes a call to wait to see live replicas that times
out after 30 milliseconds instead of 30 seconds. (Varun Thacker via Mark Miller)
+* SOLR-6397: zkcli script put/putfile should allow overwriting an existing znode's data
+ (Timothy Potter)
+
Optimizations
----------------------
diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java
index ae80832d612..84275d1ce39 100644
--- a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java
+++ b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java
@@ -254,16 +254,27 @@ public class ZkCLI {
System.out.println("-" + PUT + " requires two args - the path to create and the data string");
System.exit(1);
}
- zkClient.create(arglist.get(0).toString(), arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), CreateMode.PERSISTENT, true);
+ String path = arglist.get(0).toString();
+ if (zkClient.exists(path, true)) {
+ zkClient.setData(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), true);
+ } else {
+ zkClient.create(path, arglist.get(1).toString().getBytes(StandardCharsets.UTF_8), CreateMode.PERSISTENT, true);
+ }
} else if (line.getOptionValue(CMD).equals(PUT_FILE)) {
List arglist = line.getArgList();
if (arglist.size() != 2) {
System.out.println("-" + PUT_FILE + " requires two args - the path to create in ZK and the path to the local file");
System.exit(1);
}
+
+ String path = arglist.get(0).toString();
InputStream is = new FileInputStream(arglist.get(1).toString());
try {
- zkClient.create(arglist.get(0).toString(), IOUtils.toByteArray(is), CreateMode.PERSISTENT, true);
+ if (zkClient.exists(path, true)) {
+ zkClient.setData(path, IOUtils.toByteArray(is), true);
+ } else {
+ zkClient.create(path, IOUtils.toByteArray(is), CreateMode.PERSISTENT, true);
+ }
} finally {
IOUtils.closeQuietly(is);
}
diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java b/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java
index 04331105d57..c873287ddbf 100644
--- a/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java
@@ -144,6 +144,13 @@ public class ZkCLITest extends SolrTestCaseJ4 {
zkClient.getData("/data.txt", null, null, true);
assertArrayEquals(zkClient.getData("/data.txt", null, null, true), data.getBytes(StandardCharsets.UTF_8));
+
+ // test re-put to existing
+ data = "my data deux";
+ args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd",
+ "put", "/data.txt", data};
+ ZkCLI.main(args);
+ assertArrayEquals(zkClient.getData("/data.txt", null, null, true), data.getBytes(StandardCharsets.UTF_8));
}
@Test