Apache Solr open-source search software
SOLR-10430: Add ls command to ZkCLI for listing only sub-directories.
Mark Millerc4872add5011
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 31f9d247e8c..3cb30067117 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -202,6 +202,8 @@ New Features* SOLR-1485: Add payload support with payload() value source and {!payload_score} and {!payload_check}query parsers. (Erik Hatcher)+* SOLR-10430: Add ls command to ZkCLI for listing only sub-directories. (Peter Szantai-Kis via Mark Miller)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.javaindex 0e2f1892191..2df87a03c72 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java@@ -21,12 +21,14 @@ import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;+import java.io.PrintStream;import java.nio.charset.StandardCharsets;import java.nio.file.Paths;import java.util.List;import java.util.concurrent.TimeoutException;import java.util.regex.Pattern;+import com.google.common.annotations.VisibleForTesting;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.CommandLineParser;import org.apache.commons.cli.HelpFormatter;@@ -72,9 +74,17 @@ public class ZkCLI {private static final String COLLECTION = "collection";private static final String CLEAR = "clear";private static final String LIST = "list";+ private static final String LS = "ls";private static final String CMD = "cmd";private static final String CLUSTERPROP = "clusterprop";private static final String UPDATEACLS = "updateacls";++ @VisibleForTesting+ public static void setStdout(PrintStream stdout) {+ ZkCLI.stdout = stdout;+ }++ private static PrintStream stdout = System.out;/*** Allows you to perform a variety of zookeeper related tasks, such as:@@ -103,14 +113,13 @@ public class ZkCLI {"cmd to run: " + BOOTSTRAP + ", " + UPCONFIG + ", " + DOWNCONFIG+ ", " + LINKCONFIG + ", " + MAKEPATH + ", " + PUT + ", " + PUT_FILE + ","+ GET + "," + GET_FILE + ", " + LIST + ", " + CLEAR- + ", " + UPDATEACLS).create(CMD));+ + ", " + UPDATEACLS + ", " + LS).create(CMD));Option zkHostOption = new Option("z", ZKHOST, true,"ZooKeeper host address");options.addOption(zkHostOption);Option solrHomeOption = new Option("s", SOLRHOME, true,"for " + BOOTSTRAP + ", " + RUNZK + ": solrhome location");- options.addOption(zkHostOption);options.addOption(solrHomeOption);options.addOption("d", CONFDIR, true,@@ -145,20 +154,21 @@ public class ZkCLI {// automatically generate the help statementHelpFormatter formatter = new HelpFormatter();formatter.printHelp(ZK_CLI_NAME, options);- System.out.println("Examples:");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + BOOTSTRAP + " -" + SOLRHOME + " /opt/solr");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + DOWNCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LINKCONFIG + " -" + COLLECTION + " collection1" + " -" + CONFNAME + " myconf");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + MAKEPATH + " /apache/solr");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT + " /solr.conf 'conf data'");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT_FILE + " /solr.xml /User/myuser/solr/solr.xml");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET + " /solr.xml");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET_FILE + " /solr.xml solr.xml.file");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLEAR + " /solr");- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + LIST);- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLUSTERPROP + " -" + NAME + " urlScheme -" + VALUE_LONG + " https" );- System.out.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPDATEACLS + " /solr");+ stdout.println("Examples:");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + BOOTSTRAP + " -" + SOLRHOME + " /opt/solr");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + DOWNCONFIG + " -" + CONFDIR + " /opt/solr/collection1/conf" + " -" + CONFNAME + " myconf");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + LINKCONFIG + " -" + COLLECTION + " collection1" + " -" + CONFNAME + " myconf");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + MAKEPATH + " /apache/solr");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT + " /solr.conf 'conf data'");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + PUT_FILE + " /solr.xml /User/myuser/solr/solr.xml");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET + " /solr.xml");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + GET_FILE + " /solr.xml solr.xml.file");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLEAR + " /solr");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + LIST);+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + LS + " /solr/live_nodes");+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + CLUSTERPROP + " -" + NAME + " urlScheme -" + VALUE_LONG + " https" );+ stdout.println("zkcli.sh -zkhost localhost:9983 -cmd " + UPDATEACLS + " /solr");return;}@@ -169,8 +179,7 @@ public class ZkCLI {String solrPort = null;if (line.hasOption(RUNZK)) {if (!line.hasOption(SOLRHOME)) {- System.out- .println("-" + SOLRHOME + " is required for " + RUNZK);+ stdout.println("-" + SOLRHOME + " is required for " + RUNZK);System.exit(1);}solrPort = line.getOptionValue(RUNZK);@@ -191,7 +200,7 @@ public class ZkCLI {if (line.getOptionValue(CMD).equalsIgnoreCase(BOOTSTRAP)) {if (!line.hasOption(SOLRHOME)) {- System.out.println("-" + SOLRHOME+ stdout.println("-" + SOLRHOME+ " is required for " + BOOTSTRAP);System.exit(1);}@@ -199,7 +208,7 @@ public class ZkCLI {CoreContainer cc = new CoreContainer(solrHome);if(!ZkController.checkChrootPath(zkServerAddress, true)) {- System.out.println("A chroot was specified in zkHost but the znode doesn't exist. ");+ stdout.println("A chroot was specified in zkHost but the znode doesn't exist. ");System.exit(1);}@@ -210,7 +219,7 @@ public class ZkCLI {} else if (line.getOptionValue(CMD).equalsIgnoreCase(UPCONFIG)) {if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) {- System.out.println("-" + CONFDIR + " and -" + CONFNAME+ stdout.println("-" + CONFDIR + " and -" + CONFNAME+ " are required for " + UPCONFIG);System.exit(1);}@@ -219,7 +228,7 @@ public class ZkCLI {final String excludeExpr = line.getOptionValue(EXCLUDE_REGEX, EXCLUDE_REGEX_DEFAULT);if(!ZkController.checkChrootPath(zkServerAddress, true)) {- System.out.println("A chroot was specified in zkHost but the znode doesn't exist. ");+ stdout.println("A chroot was specified in zkHost but the znode doesn't exist. ");System.exit(1);}ZkConfigManager configManager = new ZkConfigManager(zkClient);@@ -227,7 +236,7 @@ public class ZkCLI {configManager.uploadConfigDir(Paths.get(confDir), confName, excludePattern);} else if (line.getOptionValue(CMD).equalsIgnoreCase(DOWNCONFIG)) {if (!line.hasOption(CONFDIR) || !line.hasOption(CONFNAME)) {- System.out.println("-" + CONFDIR + " and -" + CONFNAME+ stdout.println("-" + CONFDIR + " and -" + CONFNAME+ " are required for " + DOWNCONFIG);System.exit(1);}@@ -237,7 +246,7 @@ public class ZkCLI {configManager.downloadConfigDir(confName, Paths.get(confDir));} else if (line.getOptionValue(CMD).equalsIgnoreCase(LINKCONFIG)) {if (!line.hasOption(COLLECTION) || !line.hasOption(CONFNAME)) {- System.out.println("-" + COLLECTION + " and -" + CONFNAME+ stdout.println("-" + COLLECTION + " and -" + CONFNAME+ " are required for " + LINKCONFIG);System.exit(1);}@@ -246,25 +255,38 @@ public class ZkCLI {ZkController.linkConfSet(zkClient, collection, confName);} else if (line.getOptionValue(CMD).equalsIgnoreCase(LIST)) {- zkClient.printLayoutToStdOut();+ zkClient.printLayoutToStream(stdout);+ } else if (line.getOptionValue(CMD).equals(LS)) {++ List argList = line.getArgList();+ if (argList.size() != 1) {+ stdout.println("-" + LS + " requires one arg - the path to list");+ System.exit(1);+ }++ StringBuilder sb = new StringBuilder();+ String path = argList.get(0).toString();+ zkClient.printLayout(path == null ? "/" : path, 0, sb);+ stdout.println(sb.toString());+} else if (line.getOptionValue(CMD).equalsIgnoreCase(CLEAR)) {List arglist = line.getArgList();if (arglist.size() != 1) {- System.out.println("-" + CLEAR + " requires one arg - the path to clear");+ stdout.println("-" + CLEAR + " requires one arg - the path to clear");System.exit(1);}zkClient.clean(arglist.get(0).toString());} else if (line.getOptionValue(CMD).equalsIgnoreCase(MAKEPATH)) {List arglist = line.getArgList();if (arglist.size() != 1) {- System.out.println("-" + MAKEPATH + " requires one arg - the path to make");+ stdout.println("-" + MAKEPATH + " requires one arg - the path to make");System.exit(1);}zkClient.makePath(arglist.get(0).toString(), true);} else if (line.getOptionValue(CMD).equalsIgnoreCase(PUT)) {List arglist = line.getArgList();if (arglist.size() != 2) {- System.out.println("-" + PUT + " requires two args - the path to create and the data string");+ stdout.println("-" + PUT + " requires two args - the path to create and the data string");System.exit(1);}String path = arglist.get(0).toString();@@ -276,7 +298,7 @@ public class ZkCLI {} else if (line.getOptionValue(CMD).equalsIgnoreCase(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");+ stdout.println("-" + PUT_FILE + " requires two args - the path to create in ZK and the path to the local file");System.exit(1);}@@ -295,15 +317,15 @@ public class ZkCLI {} else if (line.getOptionValue(CMD).equalsIgnoreCase(GET)) {List arglist = line.getArgList();if (arglist.size() != 1) {- System.out.println("-" + GET + " requires one arg - the path to get");+ stdout.println("-" + GET + " requires one arg - the path to get");System.exit(1);}byte [] data = zkClient.getData(arglist.get(0).toString(), null, null, true);- System.out.println(new String(data, StandardCharsets.UTF_8));+ stdout.println(new String(data, StandardCharsets.UTF_8));} else if (line.getOptionValue(CMD).equalsIgnoreCase(GET_FILE)) {List arglist = line.getArgList();if (arglist.size() != 2) {- System.out.println("-" + GET_FILE + "requires two args - the path to get and the file to save it to");+ stdout.println("-" + GET_FILE + "requires two args - the path to get and the file to save it to");System.exit(1);}byte [] data = zkClient.getData(arglist.get(0).toString(), null, null, true);@@ -311,13 +333,13 @@ public class ZkCLI {} else if (line.getOptionValue(CMD).equals(UPDATEACLS)) {List arglist = line.getArgList();if (arglist.size() != 1) {- System.out.println("-" + UPDATEACLS + " requires one arg - the path to update");+ stdout.println("-" + UPDATEACLS + " requires one arg - the path to update");System.exit(1);}zkClient.updateACLs(arglist.get(0).toString());} else if (line.getOptionValue(CMD).equalsIgnoreCase(CLUSTERPROP)) {if(!line.hasOption(NAME)) {- System.out.println("-" + NAME + " is required for " + CLUSTERPROP);+ stdout.println("-" + NAME + " is required for " + CLUSTERPROP);}String propertyName = line.getOptionValue(NAME);//If -val option is missing, we will use the null value. This is required to maintain@@ -327,12 +349,12 @@ public class ZkCLI {try {props.setClusterProperty(propertyName, propertyValue);} catch (IOException ex) {- System.out.println("Unable to set the cluster property due to following error : " + ex.getLocalizedMessage());+ stdout.println("Unable to set the cluster property due to following error : " + ex.getLocalizedMessage());System.exit(1);}} else {// If not cmd matches- System.out.println("Unknown command "+ line.getOptionValue(CMD) + ". Use -h to get help.");+ stdout.println("Unknown command "+ line.getOptionValue(CMD) + ". Use -h to get help.");System.exit(1);}} finally {@@ -344,7 +366,7 @@ public class ZkCLI {}}} catch (ParseException exp) {- System.out.println("Unexpected exception:" + exp.getMessage());+ stdout.println("Unexpected exception:" + exp.getMessage());}}diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java b/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.javaindex dc999f1c2c7..d2b6b089f37 100644--- a/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ZkCLITest.java@@ -16,10 +16,12 @@*/package org.apache.solr.cloud;+import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;+import java.io.PrintStream;import java.lang.invoke.MethodHandles;import java.nio.charset.StandardCharsets;import java.util.Collection;@@ -191,7 +193,33 @@ public class ZkCLITest extends SolrTestCaseJ4 {zkClient.makePath("/test", true);String[] args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd","list"};++ ByteArrayOutputStream byteStream = new ByteArrayOutputStream();+ final PrintStream myOut = new PrintStream(byteStream, false, StandardCharsets.UTF_8.name());+ ZkCLI.setStdout(myOut);+ZkCLI.main(args);++ final String standardOutput = byteStream.toString(StandardCharsets.UTF_8.name());+ String separator = System.lineSeparator();+ assertEquals("/ (1)" + separator + " /test (0)" + separator + separator, standardOutput);+ }++ @Test+ public void testLs() throws Exception {+ zkClient.makePath("/test/path", true);+ String[] args = new String[] {"-zkhost", zkServer.getZkAddress(), "-cmd",+ "ls", "/test"};++ ByteArrayOutputStream byteStream = new ByteArrayOutputStream();+ final PrintStream myOut = new PrintStream(byteStream, false, StandardCharsets.UTF_8.name());+ ZkCLI.setStdout(myOut);++ ZkCLI.main(args);++ final String standardOutput = byteStream.toString(StandardCharsets.UTF_8.name());+ String separator = System.lineSeparator();+ assertEquals("/test (1)" + separator + " /test/path (0)" + separator + separator, standardOutput);}@Testdiff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.java b/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.javaindex 3f8deea5ace..66033bc8c0d 100644--- a/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.java+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/SolrZkClient.java@@ -25,6 +25,7 @@ import javax.xml.transform.stream.StreamSource;import java.io.Closeable;import java.io.File;import java.io.IOException;+import java.io.PrintStream;import java.io.StringReader;import java.io.StringWriter;import java.lang.invoke.MethodHandles;@@ -642,9 +643,13 @@ public class SolrZkClient implements Closeable {*/public void printLayoutToStdOut() throws KeeperException,InterruptedException {+ printLayoutToStream(System.out);+ }+ public void printLayoutToStream(PrintStream out) throws KeeperException,+ InterruptedException {StringBuilder sb = new StringBuilder();printLayout("/", 0, sb);- System.out.println(sb.toString());+ out.println(sb.toString());}public static String prettyPrint(String input, int indent) {