Skip to content

Commit 72499dd

Browse files
authored
SOLR-7962: Passing additional arguments to solr.cmd using "--jvm-opts" (formerly "-a") does not work with "-e" on Windows (#3347)
* --jvm-opts (formerly '-a') option doesn't kick in on Windows * fixing regression in --jvm-opts on linux * add bats test for --jvm-opts * platform dependent logging for start script
1 parent 99f4ece commit 72499dd

5 files changed

Lines changed: 81 additions & 22 deletions

File tree

solr/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ Bug Fixes
293293
* SOLR-17758: The NumFieldLimitingUpdateRequestProcessor's "warnOnly" mode has been fixed, and now processes documents even
294294
when the limit has been exceeded. (Jason Gerlowski, Rahul Goswami)
295295

296+
* SOLR-7962: Passing additional arguments to solr.cmd using "--jvm-opts" (formerly "-a") in conjunction with "-e" (examples like 'techproducts')
297+
wouldn't reflect on Windows (Rahul Goswami via Eric Pugh)
298+
296299
Dependency Upgrades
297300
---------------------
298301
* SOLR-17471: Upgrade Lucene to 9.12.1. (Pierre Salagnac, Christine Poerschke)

solr/bin/solr.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ IF NOT "%EXAMPLE%"=="" (
752752
-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml" ^
753753
-Dsolr.install.symDir="%SOLR_TIP%" ^
754754
-classpath "%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*" ^
755-
org.apache.solr.cli.SolrCLI run_example --script "%SDIR%\solr.cmd" -e %EXAMPLE% --server-dir "%SOLR_SERVER_DIR%" ^
755+
org.apache.solr.cli.SolrCLI run_example --script "%SDIR%\solr.cmd" -e %EXAMPLE% --server-dir "%SOLR_SERVER_DIR%" --jvm-opts "%SOLR_ADDL_ARGS%" ^
756756
--url-scheme !SOLR_URL_SCHEME! !PASS_TO_RUN_EXAMPLE!
757757

758758
REM End of run_example

solr/core/src/java/org/apache/solr/cli/RunExampleTool.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,15 @@ public void runImpl(CommandLine cli) throws Exception {
230230
throw new IllegalArgumentException(
231231
"Value of --script option is invalid! " + script + " not found");
232232
} else {
233-
Path scriptFile = serverDir.getParent().resolve("bin").resolve("solr");
233+
Path scriptFile =
234+
serverDir.getParent().resolve("bin").resolve(CLIUtils.isWindows() ? "solr.cmd" : "solr");
234235
if (Files.isRegularFile(scriptFile)) {
235236
script = scriptFile.toAbsolutePath().toString();
236237
} else {
237-
scriptFile = serverDir.getParent().resolve("bin").resolve("solr.cmd");
238-
if (Files.isRegularFile(scriptFile)) {
239-
script = scriptFile.toAbsolutePath().toString();
240-
} else {
241-
throw new IllegalArgumentException(
242-
"Cannot locate the bin/solr script! Please pass --script to this application.");
243-
}
238+
throw new IllegalArgumentException(
239+
"Cannot locate the bin/"
240+
+ scriptFile.getFileName().toString()
241+
+ " script! Please pass --script to this application.");
244242
}
245243
}
246244

@@ -688,7 +686,8 @@ protected Map<String, Object> startSolr(
688686
String verboseArg = isVerbose() ? "--verbose" : "";
689687

690688
String jvmOpts = cli.getOptionValue(JVM_OPTS_OPTION);
691-
String jvmOptsArg = (jvmOpts != null) ? " --jvm-opts \"" + jvmOpts + "\"" : "";
689+
String jvmOptsArg =
690+
(jvmOpts != null && !jvmOpts.isEmpty()) ? " --jvm-opts \"" + jvmOpts + "\"" : "";
692691

693692
Path cwd = Path.of(System.getProperty("user.dir"));
694693
Path binDir = Path.of(script).getParent();
@@ -708,10 +707,10 @@ protected Map<String, Object> startSolr(
708707
? "-Dsolr.modules=clustering,extraction,langid,ltr,scripting -Dsolr.ltr.enabled=true -Dsolr.clustering.enabled=true"
709708
: "";
710709

711-
String startCmd =
710+
String startCmdStr =
712711
String.format(
713712
Locale.ROOT,
714-
"\"%s\" start %s -p %d --solr-home \"%s\" --server-dir \"%s\" %s %s %s %s %s %s %s %s",
713+
"\"%s\" start %s -p %d --solr-home \"%s\" --server-dir \"%s\" %s %s %s %s %s %s %s",
715714
callScript,
716715
cloudModeArg,
717716
port,
@@ -723,12 +722,11 @@ protected Map<String, Object> startSolr(
723722
forceArg,
724723
verboseArg,
725724
extraArgs,
726-
jvmOptsArg,
727725
syspropArg);
728-
startCmd = startCmd.replaceAll("\\s+", " ").trim(); // for pretty printing
726+
startCmdStr = startCmdStr.replaceAll("\\s+", " ").trim(); // for pretty printing
729727

730728
echo("\nStarting up Solr on port " + port + " using command:");
731-
echo(startCmd + "\n");
729+
echo(startCmdStr + jvmOptsArg + "\n");
732730

733731
String solrUrl =
734732
String.format(
@@ -757,8 +755,21 @@ protected Map<String, Object> startSolr(
757755
}
758756
}
759757
DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
760-
executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd), startEnv, handler);
761-
758+
org.apache.commons.exec.CommandLine startCmd =
759+
org.apache.commons.exec.CommandLine.parse(startCmdStr);
760+
761+
if (!jvmOptsArg.isEmpty()) {
762+
startCmd.addArgument("--jvm-opts");
763+
764+
/* CommandLine.parse() tends to strip off the quotes by default before sending to cmd.exe.
765+
This may cause cmd to break up the argument value on certain characters in unintended ways
766+
thereby passing incorrect value to start.cmd
767+
(eg: for "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:18983", it
768+
breaks apart the value at "-agentlib:jdwp" passing incorrect args to start.cmd ).
769+
The 'false' here tells Exec: “don’t touch my quotes—this is one atomic argument” */
770+
startCmd.addArgument("\"" + jvmOpts + "\"", false);
771+
}
772+
executor.execute(startCmd, startEnv, handler);
762773
// wait for execution.
763774
try {
764775
handler.waitFor(3000);
@@ -767,21 +778,25 @@ protected Map<String, Object> startSolr(
767778
Thread.interrupted();
768779
}
769780
if (handler.hasResult() && handler.getExitValue() != 0) {
781+
startCmdStr += jvmOptsArg;
770782
throw new Exception(
771783
"Failed to start Solr using command: "
772-
+ startCmd
784+
+ startCmdStr
773785
+ " Exception : "
774786
+ handler.getException());
775787
}
776788
} else {
789+
// Unlike Windows, special handling of jvmOpts is not required on linux. We can simply
790+
// concatenate to form the complete command
791+
startCmdStr += jvmOptsArg;
777792
try {
778-
code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd));
793+
code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmdStr));
779794
} catch (ExecuteException e) {
780795
throw new Exception(
781-
"Failed to start Solr using command: " + startCmd + " Exception : " + e);
796+
"Failed to start Solr using command: " + startCmdStr + " Exception : " + e);
782797
}
798+
if (code != 0) throw new Exception("Failed to start Solr using command: " + startCmdStr);
783799
}
784-
if (code != 0) throw new Exception("Failed to start Solr using command: " + startCmd);
785800

786801
return getNodeStatus(
787802
solrUrl, cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION), maxWaitSecs);

solr/core/src/java/org/apache/solr/cli/SolrCLI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public static CommandLine parseCmdLine(Tool tool, String[] args) {
124124
List<String> dashDList = new ArrayList<>();
125125
for (int a = 1; a < args.length; a++) {
126126
String arg = args[a];
127-
if (arg.startsWith("-D")) {
127+
if (!args[a - 1].equals("--jvm-opts") && arg.startsWith("-D")) {
128128
dashDList.add(arg);
129129
} else {
130130
toolArgList.add(arg);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bats
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one or more
4+
# contributor license agreements. See the NOTICE file distributed with
5+
# this work for additional information regarding copyright ownership.
6+
# The ASF licenses this file to You under the Apache License, Version 2.0
7+
# (the "License"); you may not use this file except in compliance with
8+
# the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
load bats_helper
19+
20+
setup() {
21+
common_clean_setup
22+
}
23+
24+
teardown() {
25+
# save a snapshot of SOLR_HOME for failed tests
26+
save_home_on_failure
27+
28+
solr stop --all >/dev/null 2>&1
29+
}
30+
31+
@test "start -e cloud works with --jvm-opts" {
32+
solr start -e cloud --no-prompt --jvm-opts "-Dcustom.prop=helloworld"
33+
solr assert --started http://localhost:${SOLR_PORT} --cloud http://localhost:${SOLR_PORT} --timeout 60000
34+
solr assert --started http://localhost:${SOLR2_PORT} --cloud http://localhost:${SOLR2_PORT} --timeout 60000
35+
36+
run curl "http://localhost:${SOLR_PORT}/solr/admin/info/properties"
37+
assert_output --partial 'helloworld'
38+
39+
run curl "http://localhost:${SOLR2_PORT}/solr/admin/info/properties"
40+
assert_output --partial 'helloworld'
41+
}

0 commit comments

Comments
 (0)