This is an automated email from the ASF dual-hosted git repository.
janhoy pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/branch_9x by this push:
new 77f20560aae SOLR-17690 Make solr zk tools read ZK_HOST environment
(branch_9x) (#3817)
77f20560aae is described below
commit 77f20560aaeaa6a638f54409a85323a97076f383
Author: Jan Høydahl <[email protected]>
AuthorDate: Tue Oct 28 14:01:12 2025 +0100
SOLR-17690 Make solr zk tools read ZK_HOST environment (branch_9x) (#3817)
(cherry picked from commit d229ee4f2d1dc46898c48d36eff18e5706cd612a)
---
.../feature/SOLR-17690-zkToolZKHostFromEnv.yml | 11 +++++++
.../java/org/apache/solr/cli/RunExampleTool.java | 4 +--
.../core/src/java/org/apache/solr/cli/SolrCLI.java | 34 +++++++++++++++++++-
.../org/apache/solr/cli/ZkSubcommandsTest.java | 6 ++--
solr/packaging/test/test_zk.bats | 24 +++++++++++++-
.../pages/solr-control-script-reference.adoc | 37 +++++++++++-----------
.../pages/zookeeper-utilities.adoc | 11 +++++++
7 files changed, 101 insertions(+), 26 deletions(-)
diff --git a/changelog/unreleased/feature/SOLR-17690-zkToolZKHostFromEnv.yml
b/changelog/unreleased/feature/SOLR-17690-zkToolZKHostFromEnv.yml
new file mode 100644
index 00000000000..b608d08c1a1
--- /dev/null
+++ b/changelog/unreleased/feature/SOLR-17690-zkToolZKHostFromEnv.yml
@@ -0,0 +1,11 @@
+# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc
+title: Make solr bin/solr zk CLI tools read ZK_HOST environment as they did
pre Solr 9.8.
+ With this regression fixed it is no longer necessary to pass the --zk-host
option to the CLI tools if ZK_HOST is set.
+type: fixed # added, changed, fixed, deprecated, removed, dependency_update,
security, other
+authors:
+ - name: Jan Høydahl
+ nick: janhoy
+ url: https://home.apache.org/phonebook.html?uid=janhoy
+links:
+ - name: SOLR-17690
+ url: https://issues.apache.org/jira/browse/SOLR-17690
diff --git a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
index c624f179e31..5623403924e 100644
--- a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
@@ -290,7 +290,7 @@ public class RunExampleTool extends ToolBase {
"techproducts".equals(exampleName) ? "sample_techproducts_configs" :
"_default";
boolean isCloudMode = cli.hasOption('c');
- String zkHost = SolrCLI.getOptionWithDeprecatedAndDefault(cli, "z",
"zkHost", null);
+ String zkHost = SolrCLI.getCliOptionOrPropValue(cli,
SolrCLI.OPTION_ZKHOST, "zkHost", null);
int port =
Integer.parseInt(
cli.getOptionValue('p', System.getenv().getOrDefault("SOLR_PORT",
"8983")));
@@ -565,7 +565,7 @@ public class RunExampleTool extends ToolBase {
}
// deal with extra args passed to the script to run the example
- String zkHost = SolrCLI.getOptionWithDeprecatedAndDefault(cli, "z",
"zkHost", null);
+ String zkHost = SolrCLI.getCliOptionOrPropValue(cli,
SolrCLI.OPTION_ZKHOST, "zkHost", null);
// start the first node (most likely with embedded ZK)
Map<String, Object> nodeStatus =
diff --git a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
index 9300a0d826d..934583704ed 100755
--- a/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
+++ b/solr/core/src/java/org/apache/solr/cli/SolrCLI.java
@@ -441,6 +441,20 @@ public class SolrCLI implements CLIO {
return val == null ? def : val;
}
+ /**
+ * Returns the value of the option with the given name, or the value from a
given system property,
+ * or the value of the deprecated option. If all values are null, then it
returns the default
+ * value.
+ */
+ public static String getOptionWithDeprecatedAndDefault(
+ CommandLine cli, Option opt, String deprecated, String def, String
sysProp) {
+ String val = SolrCLI.getCliOptionOrPropValue(cli, opt, sysProp, def);
+ if (val == null) {
+ val = cli.getOptionValue(deprecated);
+ }
+ return val == null ? def : val;
+ }
+
// TODO: SOLR-17429 - remove the custom logic when Commons CLI is upgraded
and
// makes stderr the default, or makes Option.toDeprecatedString() public.
public static void deprecatedHandlerStdErr(Option o) {
@@ -798,7 +812,7 @@ public class SolrCLI implements CLIO {
*/
public static String getZkHost(CommandLine cli) throws Exception {
- String zkHost = getOptionWithDeprecatedAndDefault(cli, "zk-host",
"zkHost", null);
+ String zkHost = getOptionWithDeprecatedAndDefault(cli, OPTION_ZKHOST,
"zkHost", null, "zkHost");
if (zkHost != null && !zkHost.isBlank()) {
return zkHost;
}
@@ -930,4 +944,22 @@ public class SolrCLI implements CLIO {
CLIO.out(String.valueOf(message));
}
}
+
+ /**
+ * Get the value of the specified CLI option with fallback to system
property and default value.
+ *
+ * @param cli the command line
+ * @param option the commons cli {@link Option}
+ * @param sysprop the system property to fall back to
+ * @param defaultValue the default value. Use null if no default value is
desired
+ * @return the value of the option or system property or the default value
+ */
+ public static String getCliOptionOrPropValue(
+ CommandLine cli, Option option, String sysprop, String defaultValue) {
+ String value = cli.getOptionValue(option);
+ if (value == null) {
+ value = EnvUtils.getProperty(sysprop, defaultValue);
+ }
+ return value;
+ }
}
diff --git a/solr/core/src/test/org/apache/solr/cli/ZkSubcommandsTest.java
b/solr/core/src/test/org/apache/solr/cli/ZkSubcommandsTest.java
index 6bcee41d036..a328458ba70 100644
--- a/solr/core/src/test/org/apache/solr/cli/ZkSubcommandsTest.java
+++ b/solr/core/src/test/org/apache/solr/cli/ZkSubcommandsTest.java
@@ -488,10 +488,8 @@ public class ZkSubcommandsTest extends SolrTestCaseJ4 {
Path file =
tmpDir.resolve("solrtest-getfile-" + this.getClass().getName() + "-" +
System.nanoTime());
- String[] args =
- new String[] {
- "cp", "-z", zkServer.getZkAddress(), "zk:" + getNode,
file.toAbsolutePath().toString()
- };
+ // Not setting --zk-host, will fall back to sysProp 'zkHost'
+ String[] args = new String[] {"cp", "zk:" + getNode,
file.toAbsolutePath().toString()};
assertEquals(0, CLITestHelper.runTool(args, ZkCpTool.class));
assertArrayEquals(data, Files.readAllBytes(file));
diff --git a/solr/packaging/test/test_zk.bats b/solr/packaging/test/test_zk.bats
index 447e1338cfa..1bb77244fea 100644
--- a/solr/packaging/test/test_zk.bats
+++ b/solr/packaging/test/test_zk.bats
@@ -170,10 +170,32 @@ teardown() {
run solr zk cp afile.txt zk:/afile.txt -z localhost:${ZK_PORT} --verbose
--solr-home ${SOLR_TIP}/server/solr
assert_output --partial "Using SolrHome: ${SOLR_TIP}/server/solr"
refute_output --partial 'Failed to load solr.xml from ZK or SolrHome'
-
+
# The -DminStateByteLenForCompression variable substitution on solr start is
not seen
# by the ZkCpTool.java, so therefore we do not have compression unless
solr.xml is directly edited.
#assert_output --partial 'Compression of state.json has been enabled'
rm afile.txt
}
+
+@test "env var ZK_HOST is honored" {
+ sleep 1
+
+ # Need to unset SOLR_PORT to avoid the tool being smart and look at SOLR_PORT
+ export SOLR_PORT_KEEP=$SOLR_PORT
+ unset SOLR_PORT
+
+ # First test a command that will fail (no ZK_HOST set, no SOLR_PORT)
+ run solr zk ls / --recursive
+ assert_output --partial "assuming solr url is http://localhost:8983"
+ refute_output --partial "aliases.json"
+
+ # Then set the ZK_HOST variable and test again
+ export ZK_HOST=localhost:${ZK_PORT}
+ run solr zk ls / --recursive
+ assert_output --partial "aliases.json"
+
+ # Restore SOLR_PORT
+ export SOLR_PORT=$SOLR_PORT_KEEP
+ unset ZK_HOST
+}
diff --git
a/solr/solr-ref-guide/modules/deployment-guide/pages/solr-control-script-reference.adoc
b/solr/solr-ref-guide/modules/deployment-guide/pages/solr-control-script-reference.adoc
index 65e7516a37d..11e54230ae2 100644
---
a/solr/solr-ref-guide/modules/deployment-guide/pages/solr-control-script-reference.adoc
+++
b/solr/solr-ref-guide/modules/deployment-guide/pages/solr-control-script-reference.adoc
@@ -557,13 +557,10 @@ Base Solr URL, which can be used in SolrCloud mode to
determine the ZooKeeper co
+
[%autowidth,frame=none]
|===
-|Optional |Default: `localhost:9983`
+|Optional |Default: `localhost:9983` if env var `ZK_HOST` is not set.
|===
+
ZooKeeper connection string.
-If you are running Solr on a port other than 8983, you will have to specify
the ZooKeeper connection string.
-By default, this will be the Solr port + 1000.
-This parameter is unnecessary if `ZK_HOST` is defined in `solr.in.sh` or
`solr.in.cmd`.
+
*Example*: `bin/solr healthcheck -z localhost:2181`
@@ -1282,6 +1279,17 @@ Usage: solr zk upconfig|downconfig -d <confdir> -n
<configName> [-z zkHost] [-s
NOTE: Solr should have been started at least once before issuing these
commands to initialize ZooKeeper with the znodes Solr expects.
Once ZooKeeper is initialized, Solr doesn't need to be running on any node to
use these commands.
+=== ZooKeeper Connection String
+
+All `bin/solr zk` sub-commands require a ZooKeeper connection string to
function. You can provide this in three ways, in order of precedence:
+
+1. Explicitly pass the `-z` or `--zk-host` option: `bin/solr zk <command> -z
<zkHost>`
+2. Set the `ZK_HOST` environment variable: `export ZK_HOST=<zkHost>`
+3. Define `ZK_HOST` in `solr.in.sh` (or `solr.in.cmd` on Windows)
+4. If none are provided, the tools will default to `localhost:2181`
+
+This means if you have `ZK_HOST` configured in your environment, you do not
need to pass the `-z` option to every command.
+
=== Upload a Configuration Set
Use the `zk upconfig` command to upload one of the pre-configured
configuration sets or a customized configuration set to ZooKeeper.
@@ -1329,11 +1337,10 @@ An absolute path may be supplied instead.
+
[%autowidth,frame=none]
|===
-s|Required |Default: none
+|Optional |Default: `localhost:2181` if `ZK_HOST` env var not set
|===
+
The ZooKeeper connection string.
-Is not required if `ZK_HOST` is defined in `solr.in.sh` or `solr.in.cmd`.
+
*Example*: `-z 123.321.23.43:2181`
@@ -1401,11 +1408,10 @@ In either case, _pre-existing configurations at the
destination will be overwrit
+
[%autowidth,frame=none]
|===
-s|Required |Default: none
+|Optional |Default: `localhost:2181` if `ZK_HOST` env var not set
|===
+
The ZooKeeper connection string.
-Unnecessary if `ZK_HOST` is defined in `solr.in.sh` or `solr.in.cmd`.
+
*Example*: `-z 123.321.23.43:2181`
@@ -1485,11 +1491,10 @@ If `<dest>` ends in a slash character it names a
directory.
+
[%autowidth,frame=none]
|===
-s|Required |Default: none
+|Optional |Default: `localhost:2181` if `ZK_HOST` env var not set
|===
+
The ZooKeeper connection string.
-Optional if `ZK_HOST` is defined in `solr.in.sh` or `solr.in.cmd`.
+
*Example*: `-z 123.321.23.43:2181`
@@ -1568,11 +1573,10 @@ The path is assumed to be a ZooKeeper node, no `zk:`
prefix is necessary.
+
[%autowidth,frame=none]
|===
-s|Required |Default: none
+|Optional |Default: `localhost:2181` if `ZK_HOST` env var not set
|===
+
The ZooKeeper connection string.
-Optional if `ZK_HOST` is defined in `solr.in.sh` or `solr.in.cmd`.
+
*Example*: `-z 123.321.23.43:2181`
@@ -1626,11 +1630,10 @@ The `zk:` prefix is assumed.
+
[%autowidth,frame=none]
|===
-s|Required |Default: none
+|Optional |Default: `localhost:2181` if `ZK_HOST` env var not set
|===
+
The ZooKeeper connection string.
-Unnecessary if `ZK_HOST` is defined in `solr.in.sh` or `solr.in.cmd`.
+
*Example*: `-z 123.321.23.43:2181`
@@ -1681,11 +1684,10 @@ The path on ZooKeeper to list.
+
[%autowidth,frame=none]
|===
-s|Required |Default: none
+|Optional |Default: `localhost:2181` if `ZK_HOST` env var not set
|===
+
The ZooKeeper connection string.
-Optional if `ZK_HOST` is defined in `solr.in.sh` or `solr.in.cmd`.
+
*Example*: `-z 123.321.23.43:2181`
@@ -1730,11 +1732,10 @@ A leading slash is assumed if not present.
+
[%autowidth,frame=none]
|===
-s|Required |Default: none
+|Optional |Default: `localhost:2181` if `ZK_HOST` env var not set
|===
+
The ZooKeeper connection string.
-Optional if `ZK_HOST` is defined in `solr.in.sh` or `solr.in.cmd`.
+
*Example*: `-z 123.321.23.43:2181`
diff --git
a/solr/solr-ref-guide/modules/deployment-guide/pages/zookeeper-utilities.adoc
b/solr/solr-ref-guide/modules/deployment-guide/pages/zookeeper-utilities.adoc
index 6d70cb32c46..72872731e84 100644
---
a/solr/solr-ref-guide/modules/deployment-guide/pages/zookeeper-utilities.adoc
+++
b/solr/solr-ref-guide/modules/deployment-guide/pages/zookeeper-utilities.adoc
@@ -38,6 +38,17 @@ ZooKeeper's `zkCli.sh` provides a completely general,
application-agnostic shell
Use the `help` option to get a list of available ZooKeeper specific commands
from the script itself, as in `bin/solr zk -h`.
+=== ZooKeeper Connection String
+
+All `bin/solr zk` sub-commands require a ZooKeeper connection string to
function. You can provide this in three ways, in order of precedence:
+
+1. Explicitly pass the `-z` or `--zk-host` option: `bin/solr zk <command> -z
<zkHost>`
+2. Set the `ZK_HOST` environment variable: `export ZK_HOST=<zkHost>`
+3. Define `ZK_HOST` in `solr.in.sh` (or `solr.in.cmd` on Windows)
+4. If none are provided, the tools will default to `localhost:2181`
+
+This means if you have `ZK_HOST` configured in your environment, you do not
need to pass the `-z` option to every command.
+
== Solr CLI Examples
Below are some examples of using the `bin/solr` CLI which assume you have
already started the SolrCloud example (`bin/solr start -e cloud --no-prompt`)