nick-boss-tech commented on code in PR #5346:
URL: https://github.com/apache/solr/pull/5346#discussion_r4637414643


##########
solr/bin/solr:
##########
@@ -520,11 +521,14 @@ function stop_solr() {
 
   DIR="$1"
   SOLR_PORT_LISTEN="$2"
-  THIS_STOP_PORT="${STOP_PORT:-$((SOLR_PORT_LISTEN - 1000))}"
   STOP_KEY="$3"
   SOLR_PID="$4"
 
   if [ -n "$SOLR_PID"  ]; then
+    # Each node knows its own stop port, so read it back from the running 
process
+    THIS_STOP_PORT="$(run_tool status -p "$SOLR_PORT_LISTEN" --stop-port 
2>/dev/null | tail -1 | tr -cd '0-9')"

Review Comment:
   🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
   
   Could we check the helper’s exit status and accept only a complete, valid 
port, rather than stripping everything except digits? I checked the current 
filter in Bash: a diagnostic containing `8983` becomes `8983`, and `-1` becomes 
`1`. Jetty uses `-1` to disable its stop listener, so that setting should not 
lead to a connection attempt on port 1. Please handle an explicit disabled 
setting separately from a failed lookup.



##########
solr/core/src/java/org/apache/solr/cli/StatusTool.java:
##########
@@ -197,13 +220,29 @@ public void runImpl(CommandLine cli) throws Exception {
     solrUrl = CLIUtils.hasConnectionOption(cli) ? 
CLIUtils.normalizeSolrUrl(cli) : null;
     port = cli.hasOption(PORT_OPTION) ? cli.getParsedOptionValue(PORT_OPTION) 
: null;
     shortFormat = cli.hasOption(SHORT_OPTION);
+    printStopPort = cli.hasOption(STOP_PORT_OPTION);
     maxWaitSecs = cli.getParsedOptionValue(MAX_WAIT_SECS_OPTION, 0);
     credentials = cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION);
 
     runTool();
   }
 
   public int runTool() throws Exception {
+    // Answered from the process arguments alone, and deliberately before any 
HTTP call: a node
+    // that no longer serves requests is exactly the one that still has to be 
stopped.
+    if (printStopPort) {
+      Optional<SolrProcess> proc =
+          port == null ? Optional.empty() : processMgr.processForPort(port);

Review Comment:
   🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
   
   Could the internal lookup take the PID instead of the HTTP port? Both 
scripts already identify the process before calling the helper. [The manager 
currently builds a host-wide map keyed by 
port](https://github.com/janhoy/solr/blob/5c39ec647393566611bfe3a346a9c799e8c99342/solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java#L69-L84);
 two processes advertising the same port on different bind addresses make the 
map throw before it can answer, even though the scripts know which process to 
stop. A PID lookup should also bypass that port-map construction.



##########
solr/bin/solr.cmd:
##########
@@ -1292,6 +1289,32 @@ set eout=%eout:"=%
 echo !eout!
 GOTO :eof
 
+:resolve_stop_port
+  REM Each node knows its own stop port, so read it back from the running 
process.
+  REM The tool's output is captured through a temp file and not with a 'for 
/f' over its command
+  REM line: the quotes around -classpath are lost when the command is wrapped 
in backquotes, and
+  REM the class is then not found, which would leave every stop falling back 
to the derived port.
+  set RESOLVED_STOP_PORT=
+  set "STOP_PORT_ERR=nul"
+  IF "%verbose%"=="1" set "STOP_PORT_ERR=con"
+  set "STOP_PORT_OUT=%TEMP%\solr-stopport-%RANDOM%.txt"
+  "%JAVA%" %SOLR_TOOL_OPTS% -Dsolr.install.dir="%SOLR_TIP%" ^
+    
-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml"
 ^
+    -classpath 
"%SOLR_TIP%\lib\*;%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*"
 ^

Review Comment:
   🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
   
   Could the stop-port helper use SOLR_SERVER_DIR rather than 
DEFAULT_SERVER_DIR? The actual stop command uses the server tree selected by 
--server-dir, but this classpath still loads SolrCLI from the default tree. 
With a relocated server tree and no default copy, discovery fails and the 
script falls back to the wrong port for a custom-port node. The Unix run_tool 
call has the same dependency. A stop or restart test with a relocated server 
tree would cover it.



##########
solr/bin/solr.cmd:
##########
@@ -1292,6 +1289,32 @@ set eout=%eout:"=%
 echo !eout!
 GOTO :eof
 
+:resolve_stop_port
+  REM Each node knows its own stop port, so read it back from the running 
process.
+  REM The tool's output is captured through a temp file and not with a 'for 
/f' over its command
+  REM line: the quotes around -classpath are lost when the command is wrapped 
in backquotes, and
+  REM the class is then not found, which would leave every stop falling back 
to the derived port.
+  set RESOLVED_STOP_PORT=
+  set "STOP_PORT_ERR=nul"
+  IF "%verbose%"=="1" set "STOP_PORT_ERR=con"
+  set "STOP_PORT_OUT=%TEMP%\solr-stopport-%RANDOM%.txt"
+  "%JAVA%" %SOLR_TOOL_OPTS% -Dsolr.install.dir="%SOLR_TIP%" ^
+    
-Dlog4j.configurationFile="file:///%DEFAULT_SERVER_DIR%\resources\log4j2-console.xml"
 ^
+    -classpath 
"%SOLR_TIP%\lib\*;%DEFAULT_SERVER_DIR%\solr-webapp\webapp\WEB-INF\lib\*;%DEFAULT_SERVER_DIR%\lib\ext\*"
 ^
+    org.apache.solr.cli.SolrCLI status -p %1 --stop-port >"%STOP_PORT_OUT%" 
2>%STOP_PORT_ERR%

Review Comment:
   🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
   
   Could we put a timeout around this lookup? The Java helper runs 
synchronously before `wait_for_process_exit`, and JNA’s [no-argument WMI 
query](https://github.com/java-native-access/jna/blob/5.19.1/contrib/platform/src/com/sun/jna/platform/win32/COM/WbemcliUtil.java#L138-L166)
 waits indefinitely for results. If WMI stalls, neither stop nor restart 
reaches the fallback or the configured shutdown wait. Could a timed-out lookup 
fall back the same way as a lookup with no result?



##########
solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:
##########
@@ -154,6 +161,24 @@ private Optional<Integer> 
parsePortFromProcess(ProcessHandle ph) {
     return portStr.isPresent() ? portStr.map(Integer::parseInt) : 
Optional.empty();
   }
 
+  /**
+   * Finds the Jetty stop port of a running node from its {@code -DSTOP.PORT} 
argument, falling back
+   * to the port that {@code bin/solr} derives when nothing was configured.
+   */
+  private int parseStopPortFromProcess(ProcessHandle ph, int port) {
+    for (String arg : arguments(ph).reversed()) {
+      int idx = arg.indexOf(STOP_PORT_ARG);
+      if (idx >= 0) {
+        try {
+          return Integer.parseInt(arg.substring(idx + STOP_PORT_ARG.length()));
+        } catch (NumberFormatException e) {
+          log.warn("Ignoring unparseable {} of process {}", arg, ph.pid());
+        }
+      }
+    }
+    return port - DEFAULT_STOP_PORT_OFFSET;

Review Comment:
   🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
   
   Could discovery return "unknown" when it doesn’t find a parseable 
`-DSTOP.PORT`? A recognized process may have been launched outside `bin/solr` 
with its stop port configured elsewhere. Returning `port - 1000` here makes the 
script treat that guess as a discovery and bypass the `STOP_PORT` the operator 
supplied for shutdown. Let the script apply its existing `STOP_PORT`-or-derived 
fallback in that case.



##########
solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:
##########
@@ -154,6 +161,24 @@ private Optional<Integer> 
parsePortFromProcess(ProcessHandle ph) {
     return portStr.isPresent() ? portStr.map(Integer::parseInt) : 
Optional.empty();
   }
 
+  /**
+   * Finds the Jetty stop port of a running node from its {@code -DSTOP.PORT} 
argument, falling back
+   * to the port that {@code bin/solr} derives when nothing was configured.
+   */
+  private int parseStopPortFromProcess(ProcessHandle ph, int port) {
+    for (String arg : arguments(ph).reversed()) {
+      int idx = arg.indexOf(STOP_PORT_ARG);

Review Comment:
   🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
   
   Could this match the actual JVM option rather than searching inside every 
argument? On Unix, ProcessHandle already supplies individual arguments. A later 
unrelated option whose value contains `-DSTOP.PORT=` can currently take 
precedence over the real setting. Keep the reverse scan, but match the exact 
prefix after platform-appropriate argument parsing.



##########
solr/core/src/java/org/apache/solr/cli/StatusTool.java:
##########
@@ -197,13 +220,29 @@ public void runImpl(CommandLine cli) throws Exception {
     solrUrl = CLIUtils.hasConnectionOption(cli) ? 
CLIUtils.normalizeSolrUrl(cli) : null;
     port = cli.hasOption(PORT_OPTION) ? cli.getParsedOptionValue(PORT_OPTION) 
: null;
     shortFormat = cli.hasOption(SHORT_OPTION);
+    printStopPort = cli.hasOption(STOP_PORT_OPTION);
     maxWaitSecs = cli.getParsedOptionValue(MAX_WAIT_SECS_OPTION, 0);
     credentials = cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION);
 
     runTool();
   }
 
   public int runTool() throws Exception {
+    // Answered from the process arguments alone, and deliberately before any 
HTTP call: a node
+    // that no longer serves requests is exactly the one that still has to be 
stopped.
+    if (printStopPort) {
+      Optional<SolrProcess> proc =
+          port == null ? Optional.empty() : processMgr.processForPort(port);
+      if (proc.isEmpty()) {
+        CLIO.err("Could not find a running Solr on port " + port);
+        runtime.exit(1);
+        return 1;
+      }
+      CLIO.out(String.valueOf(proc.get().stopPort()));

Review Comment:
   🤖 *AI text below* 🤖 *(reviewed by Nick Shanin)*
   
   What should this print for `-DSTOP.PORT=0`? [Jetty assigns an available 
port](https://javadoc.jetty.org/jetty-12/org/eclipse/jetty/server/ShutdownMonitor.html)
 in that case, so `0` is not the port the stop command can connect to. Both 
scripts accept it as a successful lookup and skip the operator’s `STOP_PORT` 
override, which could contain the actual assigned port. Could the helper report 
that the port is unknown from argv in this case?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to