Apache Lucene open-source search software
SOLR-9891: Add mkroot command to bin/solr and bin/solr.cmd
Erick Ericksoncb266d5fc775
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 7f83de01cf3..59dde90338a 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -204,6 +204,8 @@ New Features* SOLR-9905: Add NullStream to isolate the performance of the ExportWriter (Joel Bernstein)+* SOLR-9891: Add mkroot command to bin/solr and bin/solr.cmd (Erick Erickson)+Optimizations----------------------* SOLR-9704: Facet Module / JSON Facet API: Optimize blockChildren facets that havediff --git a/solr/bin/solr b/solr/bin/solrindex c1add261bd0..fcf864b405c 100755--- a/solr/bin/solr+++ b/solr/bin/solr@@ -493,6 +493,12 @@ function print_usage() {echo ""echo " Only the node names are listed, not data"echo ""+ echo " mkroot makes a znode on Zookeeper with no data. Can be used to make a path of arbitrary"+ echo " depth but primarily intended to create a 'chroot'."+ echo ""+ echo " <path>: The Zookeeper path to create. Leading slash is assumed if not present."+ echo " Intermediate nodes are created as needed if not present."+ echo ""fi} # end print_usage@@ -507,6 +513,7 @@ function print_short_zk_usage() {echo " solr zk rm [-r] <path> [-z zkHost]"echo " solr zk mv <src> <dest> [-z zkHost]"echo " solr zk ls [-r] <path> [-z zkHost]"+ echo " solr zk mkroot <path> [-z zkHost]"echo ""if [ "$1" == "" ]; then@@ -1001,7 +1008,7 @@ if [[ "$SCRIPT_CMD" == "zk" ]]; thenif [ $# -gt 0 ]; thenwhile true; docase "$1" in- -upconfig|upconfig|-downconfig|downconfig|cp|rm|mv|ls)+ -upconfig|upconfig|-downconfig|downconfig|cp|rm|mv|ls|mkroot)if [ "${1:0:1}" == "-" ]; thenZK_OP=${1:1}else@@ -1042,7 +1049,7 @@ if [[ "$SCRIPT_CMD" == "zk" ]]; thenshiftbreak;;- *) # Pick up <src> <dst> or <path> params for rm, ls, cp, mv.+ *) # Pick up <src> <dst> or <path> params for rm, ls, cp, mv, mkroot.if [ "$1" == "" ]; thenbreak # out-of-args, stop loopingfi@@ -1062,7 +1069,7 @@ if [[ "$SCRIPT_CMD" == "zk" ]]; thenfiif [ -z "$ZK_OP" ]; then- print_short_zk_usage "Zookeeper operation (one of 'upconfig', 'downconfig', 'rm', 'mv', 'cp', 'ls') is required!"+ print_short_zk_usage "Zookeeper operation (one of 'upconfig', 'downconfig', 'rm', 'mv', 'cp', 'ls', 'mkroot') is required!"fiif [ -z "$ZK_HOST" ]; then@@ -1088,6 +1095,13 @@ if [[ "$SCRIPT_CMD" == "zk" ]]; thenfifi+ if [[ "$ZK_OP" == "mkroot" ]]; then+ if [[ -z "$ZK_SRC" ]]; then+ print_short_zk_usage "<path> must be specified when using the 'mkroot' command."+ fi+ fi++case "$ZK_OP" inupconfig)run_tool "$ZK_OP" -confname "$CONFIGSET_CONFNAME" -confdir "$CONFIGSET_CONFDIR" -zkHost "$ZK_HOST" -configsetsDir "$SOLR_TIP/server/solr/configsets"@@ -1113,6 +1127,12 @@ if [[ "$SCRIPT_CMD" == "zk" ]]; thenfirun_tool "$ZK_OP" -path "$ZK_SRC" -recurse "$ZK_RECURSE" -zkHost "$ZK_HOST";;+ mkroot)+ if [ -z "$ZK_SRC" ]; then+ print_short_zk_usage "Zookeeper path to list must be specified when using the 'mkroot' command"+ fi+ run_tool "$ZK_OP" -path "$ZK_SRC" -zkHost "$ZK_HOST"+ ;;*)print_short_zk_usage "Unrecognized Zookeeper operation $ZK_OP";;diff --git a/solr/bin/solr.cmd b/solr/bin/solr.cmdindex 4b6081fd50c..04398bc6754 100644--- a/solr/bin/solr.cmd+++ b/solr/bin/solr.cmd@@ -480,6 +480,13 @@ echo ^<path^>: The Zookeeper path to use as the root.echo.echo Only the node names are listed, not dataecho.+echo mkroot makes a znode in Zookeeper with no data. Can be used to make a path of arbitrary+echo depth but primarily intended to create a 'chroot'."+echo.+echo ^<path^>: The Zookeeper path to create. Leading slash is assumed if not present.+echo Intermediate nodes are created as needed if not present.+echo.+goto done:zk_short_usage@@ -492,6 +499,7 @@ echo solr zk cp [-r] ^<src^> ^<dest^> [-z zkHost]echo solr zk rm [-r] ^<path^> [-z zkHost]echo solr zk mv ^<src^> ^<dest^> [-z zkHost]echo solr zk ls [-r] ^<path^> [-z zkHost]+echo solr zk mkroot ^<path^> [-z zkHost]echo.IF "%ZK_FULL%"=="true" (goto zk_full_usage@@ -1399,6 +1407,8 @@ IF "%1"=="-upconfig" (goto set_zk_op) ELSE IF "%1"=="ls" (goto set_zk_op+) ELSE IF "%1"=="mkroot" (+ goto set_zk_op) ELSE IF "%1"=="-n" (goto set_config_name) ELSE IF "%1"=="-r" (@@ -1561,13 +1571,22 @@ IF "!ZK_OP!"=="upconfig" (org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! -recurse !ZK_RECURSE!) ELSE IF "!ZK_OP!"=="ls" (IF "%ZK_SRC"=="" (- set ERROR_MSG="Zookeeper path to remove must be specified when using the 'rm' command"+ set ERROR_MSG="Zookeeper path to remove must be specified when using the 'ls' command"goto zk_short_usage)"%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^-Dlog4j.configuration="file:%DEFAULT_SERVER_DIR%\scripts\cloud-scripts\log4j.properties" ^-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC! -recurse !ZK_RECURSE!+) ELSE IF "!ZK_OP!"=="mkroot" (+ IF "%ZK_SRC"=="" (+ set ERROR_MSG="Zookeeper path to create must be specified when using the 'mkroot' command"+ goto zk_short_usage+ )+ "%JAVA%" %SOLR_SSL_OPTS% %AUTHC_OPTS% %SOLR_ZK_CREDS_AND_ACLS% -Dsolr.install.dir="%SOLR_TIP%" ^+ -Dlog4j.configuration="file:%DEFAULT_SERVER_DIR%\scripts\cloud-scripts\log4j.properties" ^+ -classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^+ org.apache.solr.util.SolrCLI !ZK_OP! -zkHost !ZK_HOST! -path !ZK_SRC!) ELSE (set ERROR_MSG="Unknown zk option !ZK_OP!"goto zk_short_usagediff --git a/solr/core/src/java/org/apache/solr/util/SolrCLI.java b/solr/core/src/java/org/apache/solr/util/SolrCLI.javaindex 49798489498..bb2d55405aa 100644--- a/solr/core/src/java/org/apache/solr/util/SolrCLI.java+++ b/solr/core/src/java/org/apache/solr/util/SolrCLI.java@@ -364,6 +364,8 @@ public class SolrCLI {return new ZkCpTool();else if ("ls".equals(toolType))return new ZkLsTool();+ else if ("mkroot".equals(toolType))+ return new ZkMkrootTool();else if ("assert".equals(toolType))return new AssertTool();else if ("utils".equals(toolType))@@ -1986,7 +1988,7 @@ public class SolrCLI {if (zkHost == null) {throw new IllegalStateException("Solr at " + cli.getOptionValue("zkHost") +- " is running in standalone server mode, 'zk rm' can only be used when running in SolrCloud mode.\n");+ " is running in standalone server mode, 'zk ls' can only be used when running in SolrCloud mode.\n");}@@ -1999,12 +2001,71 @@ public class SolrCLI {" recurse: " + Boolean.toString(recurse));stdout.print(zkClient.listZnode(znode, recurse));} catch (Exception e) {- log.error("Could not complete rm operation for reason: " + e.getMessage());+ log.error("Could not complete ls operation for reason: " + e.getMessage());throw (e);}}} // End zkLsTool class++ public static class ZkMkrootTool extends ToolBase {++ public ZkMkrootTool() {+ this(System.out);+ }++ public ZkMkrootTool(PrintStream stdout) {+ super(stdout);+ }++ @SuppressWarnings("static-access")+ public Option[] getOptions() {+ return new Option[]{+ OptionBuilder+ .withArgName("path")+ .hasArg()+ .isRequired(true)+ .withDescription("Path to create")+ .create("path"),+ OptionBuilder+ .withArgName("HOST")+ .hasArg()+ .isRequired(true)+ .withDescription("Address of the Zookeeper ensemble; defaults to: " + ZK_HOST)+ .create("zkHost")+ };+ }++ public String getName() {+ return "mkroot";+ }++ protected void runImpl(CommandLine cli) throws Exception {++ String zkHost = getZkHost(cli);++ if (zkHost == null) {+ throw new IllegalStateException("Solr at " + cli.getOptionValue("zkHost") ++ " is running in standalone server mode, 'zk mkroot' can only be used when running in SolrCloud mode.\n");+ }+++ try (SolrZkClient zkClient = new SolrZkClient(zkHost, 30000)) {+ echo("\nConnecting to ZooKeeper at " + zkHost + " ...");++ String znode = cli.getOptionValue("path");+ echo("Creating Zookeeper path " + znode + " on ZooKeeper at " + zkHost);+ zkClient.makePath(znode, true);+ } catch (Exception e) {+ log.error("Could not complete mkroot operation for reason: " + e.getMessage());+ throw (e);+ }+ }+ } // End zkMkrootTool class++++public static class ZkCpTool extends ToolBase {public ZkCpTool() {