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


##########
solr/bin/solr.cmd:
##########
@@ -1292,6 +1304,12 @@ set eout=%eout:"=%
 echo !eout!
 GOTO :eof
 
+:delete_node_files
+  REM Remove the files recording the node on the given port
+  del /q "%SOLR_PID_DIR%\solr-%1.pid" >nul 2>nul

Review Comment:
   🤖 *AI text below* 🤖
   
   Could we exclude directories from PID enumeration and make this helper 
refuse directory targets for both marker paths? `dir /b` can return 
directories, and `stop --all` invokes cleanup even if the marker couldn’t be 
read. On Windows, [`del /q` given a directory deletes the files inside 
it](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/del).
 A directory with a PID-shaped name could therefore lose its contents even 
though no Solr process was stopped. The helper guard also protects the direct 
`stop -p` path.



##########
solr/bin/solr.cmd:
##########
@@ -1200,6 +1201,17 @@ IF "%FG%"=="1" (
       set "SCRIPT_ERROR=Solr did not start or was not reachable. Check the 
logs for errors."
       goto err
   )
+
+  REM START /B leaves us without a PID, so ask netstat who owns the port now 
that Solr is up

Review Comment:
   🤖 *AI text below* 🤖
   
   Could we tie PID publication to the JVM this invocation launched rather than 
to bare `status --max-wait-secs`? That command takes a snapshot of all local 
Solr processes: an empty snapshot can succeed before the new JVM appears, and 
an unrelated unhealthy node can make the command fail before the PID is 
written. Even a timeout for this node can leave a JVM that comes up later. The 
old `.port` record was written before the wait; the new `stop --all` ignores 
it. Please scope readiness to the target node and keep a recovery path for a 
verified child that outlives a failed or interrupted start.



##########
solr/bin/solr.cmd:
##########
@@ -875,7 +876,7 @@ IF "%SOLR_PORT_LISTEN%"=="" (
         @echo Stopping Solr process %%N running on port %SOLR_PORT_LISTEN%
         IF "%STOP_PORT%"=="" set /A STOP_PORT=%SOLR_PORT_LISTEN% - 1000
         "%JAVA%" %SOLR_SSL_OPTS% %SOLR_TOOL_OPTS% 
-Djetty.home="%SOLR_SERVER_DIR%" -jar "%SOLR_SERVER_DIR%\start.jar" 
%SOLR_JETTY_CONFIG% STOP.PORT=!STOP_PORT! STOP.KEY=%STOP_KEY% --stop
-        del "%SOLR_PID_DIR%"\solr-%SOLR_PORT_LISTEN%.port
+        CALL :delete_node_files %SOLR_PORT_LISTEN%

Review Comment:
   🤖 *AI text below* 🤖
   
   Could we move this cleanup after a confirmed exit and return a failure if 
the process is still alive? This removes the `.pid` immediately after sending 
the graceful stop request. If that request fails and the fallback cannot 
terminate the process (for example, if `jstack` fails, the `&&` chain never 
reaches `taskkill`), the script still reaches its normal success exit. This 
carries the old eager `.port` cleanup over to the new authoritative `.pid`: a 
subsequent `stop --all` has no record to recover with. Please keep the tracking 
record until the node is gone.



##########
solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:
##########
@@ -120,16 +119,8 @@ public Collection<SolrProcess> scanSolrPidFiles() throws 
IOException {
         Files.list(pidDir)
             .filter(p -> 
pidFilePattern.matcher(p.getFileName().toString()).matches())) {
       for (Path p : pidFiles.toList()) {
-        Optional<SolrProcess> process;
-        if (p.toString().endsWith(".port")) {
-          // On Windows, the file is a 'PORT' file containing the port number.
-          Integer port = Integer.valueOf(Files.readAllLines(p).getFirst());
-          process = processForPort(port);
-        } else {
-          // On Linux, the file is a 'PID' file containing the process ID.
-          Long pid = Long.valueOf(Files.readAllLines(p).getFirst());
-          process = getProcessForPid(pid);
-        }
+        Long pid = Long.valueOf(Files.readAllLines(p).getFirst().trim());

Review Comment:
   🤖 *AI text below* 🤖
   
   Could the Windows writer publish the PID file with a temporary file plus a 
rename, and could this scan skip empty or malformed records? The new `>` 
redirect creates or truncates the final `.pid` before writing its contents. A 
concurrent `solr status` can see an empty file, throw at `getFirst()`, and 
never list the other healthy nodes. A test with an empty `.pid` next to a valid 
one would cover the reader side.



##########
solr/bin/solr.cmd:
##########
@@ -830,35 +830,36 @@ IF "%SOLR_PORT_LISTEN%"=="" (
   IF "%STOP_ALL%"=="1" (
     REM Stop all running Solr instances
     set found_it=0
-    for /f "usebackq" %%i in (`dir /b "%SOLR_PID_DIR%" ^| findstr /i 
"^solr-.*\.port$"`) do (
-      set SOME_SOLR_PORT=
-      For /F "delims=" %%J In ('type "%SOLR_PID_DIR%\%%i"') do set 
SOME_SOLR_PORT=%%~J
-      if NOT "!SOME_SOLR_PORT!"=="" (
-        for /f "tokens=2,5" %%j in ('netstat -aon ^| find "TCP " ^| find ":0 " 
^| find ":!SOME_SOLR_PORT! "') do (
-          @REM j is the ip:port and k is the pid
-          IF NOT "%%k"=="0" (
-            IF "%%j"=="%SOLR_HOST_BIND%:!SOME_SOLR_PORT!" (
-              set found_it=1
-              @echo Stopping Solr process %%k running on port !SOME_SOLR_PORT!
-              IF "%STOP_PORT%"=="" (
-                set /A LOCAL_STOP_PORT=!SOME_SOLR_PORT! - 1000
-              ) else (
-                set LOCAL_STOP_PORT=%STOP_PORT%
-              )
-              "%JAVA%" %SOLR_SSL_OPTS% -Djetty.home="%SOLR_SERVER_DIR%" -jar 
"%SOLR_SERVER_DIR%\start.jar" STOP.PORT=!LOCAL_STOP_PORT! STOP.KEY=%STOP_KEY% 
--stop
-              del "%SOLR_PID_DIR%"\solr-!SOME_SOLR_PORT!.port
-              REM wait for the process to terminate
-              CALL :wait_for_process_exit %%k !SOLR_STOP_WAIT!
-              REM Kill it if it is still running after the graceful shutdown
-              IF EXIST "%JAVA_HOME%\bin\jstack.exe" (
-                qprocess "%%k" >nul 2>nul && "%JAVA_HOME%\bin\jstack.exe" %%k 
&& taskkill /f /PID %%k
-              ) else (
-                qprocess "%%k" >nul 2>nul && taskkill /f /PID %%k
-              )
-            )
-          )
+    for /f "usebackq" %%i in (`dir /b "%SOLR_PID_DIR%" ^| findstr /i 
"^solr-[0-9][0-9]*\.pid$"`) do (

Review Comment:
   🤖 *AI text below* 🤖
   
   Could we preserve `stop --all` for foreground Windows starts? The old 
foreground branch wrote a `.port` file that this loop could use. The new branch 
writes neither file, and this loop only looks for `.pid`, so an otherwise 
healthy foreground node started from another console is skipped. If this is an 
intentional behavior change, please document `stop -p` as the alternative and 
add a Windows lifecycle test.



##########
solr/solr-ref-guide/modules/upgrade-notes/pages/major-changes-in-solr-10.adoc:
##########
@@ -91,6 +91,11 @@ A request with no other parameters returns every collection.
 
 The v1 `CLUSTERSTATUS` command is unchanged, and `includeAll` still defaults 
to true.
 
+=== Windows PID Files
+
+On Windows, `bin\solr.cmd` now writes a true `solr-<port>.pid` file instead of 
relying on the `solr-<port>.port` file.
+The `solr-<port>.port` file is deprecated and will be removed in a future 
version.

Review Comment:
   🤖 *AI text below* 🤖
   
   Could the upgrade notes distinguish deprecating writes from dropping reads 
immediately? A Windows node started with the old script has only a `.port` 
file, and `stop --all` in the new script won’t find it. If there’s no migration 
fallback, please tell operators to stop those nodes with the old script before 
switching, or use an explicit `stop -p` and then start them with the new script 
to create the `.pid` file.



##########
solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java:
##########
@@ -120,16 +119,8 @@ public Collection<SolrProcess> scanSolrPidFiles() throws 
IOException {
         Files.list(pidDir)
             .filter(p -> 
pidFilePattern.matcher(p.getFileName().toString()).matches())) {
       for (Path p : pidFiles.toList()) {
-        Optional<SolrProcess> process;
-        if (p.toString().endsWith(".port")) {
-          // On Windows, the file is a 'PORT' file containing the port number.
-          Integer port = Integer.valueOf(Files.readAllLines(p).getFirst());
-          process = processForPort(port);
-        } else {
-          // On Linux, the file is a 'PID' file containing the process ID.
-          Long pid = Long.valueOf(Files.readAllLines(p).getFirst());
-          process = getProcessForPid(pid);
-        }
+        Long pid = Long.valueOf(Files.readAllLines(p).getFirst().trim());
+        Optional<SolrProcess> process = getProcessForPid(pid);

Review Comment:
   🤖 *AI text below* 🤖
   
   Could we revalidate liveness before deleting a file that isn’t in 
`pidProcessMap`? The map is built before `scanSolrPidFiles()` lists the 
directory. A concurrent Windows start can launch and atomically publish a 
correct PID file between those two steps. This scan then sees the new record, 
treats the snapshot miss as proof the process is gone, and deletes the file for 
a live node that `stop --all` now depends on. Atomic publication alone doesn’t 
address this; before deleting, also make sure the record hasn’t been replaced 
since it was read. A test that creates the manager before starting and 
registering a mock process would cover it.



##########
solr/bin/solr.cmd:
##########
@@ -830,35 +830,36 @@ IF "%SOLR_PORT_LISTEN%"=="" (
   IF "%STOP_ALL%"=="1" (
     REM Stop all running Solr instances
     set found_it=0
-    for /f "usebackq" %%i in (`dir /b "%SOLR_PID_DIR%" ^| findstr /i 
"^solr-.*\.port$"`) do (
-      set SOME_SOLR_PORT=
-      For /F "delims=" %%J In ('type "%SOLR_PID_DIR%\%%i"') do set 
SOME_SOLR_PORT=%%~J
-      if NOT "!SOME_SOLR_PORT!"=="" (
-        for /f "tokens=2,5" %%j in ('netstat -aon ^| find "TCP " ^| find ":0 " 
^| find ":!SOME_SOLR_PORT! "') do (
-          @REM j is the ip:port and k is the pid
-          IF NOT "%%k"=="0" (
-            IF "%%j"=="%SOLR_HOST_BIND%:!SOME_SOLR_PORT!" (
-              set found_it=1
-              @echo Stopping Solr process %%k running on port !SOME_SOLR_PORT!
-              IF "%STOP_PORT%"=="" (
-                set /A LOCAL_STOP_PORT=!SOME_SOLR_PORT! - 1000
-              ) else (
-                set LOCAL_STOP_PORT=%STOP_PORT%
-              )
-              "%JAVA%" %SOLR_SSL_OPTS% -Djetty.home="%SOLR_SERVER_DIR%" -jar 
"%SOLR_SERVER_DIR%\start.jar" STOP.PORT=!LOCAL_STOP_PORT! STOP.KEY=%STOP_KEY% 
--stop
-              del "%SOLR_PID_DIR%"\solr-!SOME_SOLR_PORT!.port
-              REM wait for the process to terminate
-              CALL :wait_for_process_exit %%k !SOLR_STOP_WAIT!
-              REM Kill it if it is still running after the graceful shutdown
-              IF EXIST "%JAVA_HOME%\bin\jstack.exe" (
-                qprocess "%%k" >nul 2>nul && "%JAVA_HOME%\bin\jstack.exe" %%k 
&& taskkill /f /PID %%k
-              ) else (
-                qprocess "%%k" >nul 2>nul && taskkill /f /PID %%k
-              )
-            )
-          )
+    for /f "usebackq" %%i in (`dir /b "%SOLR_PID_DIR%" ^| findstr /i 
"^solr-[0-9][0-9]*\.pid$"`) do (
+      set "SOME_SOLR_PORT=%%~ni"
+      set "SOME_SOLR_PORT=!SOME_SOLR_PORT:solr-=!"
+      set SOME_SOLR_PID=
+      For /F "delims=" %%J In ('type "%SOLR_PID_DIR%\%%i"') do set 
SOME_SOLR_PID=%%~J
+      REM Only stop the PID from the file if it still listens on the port the 
file is named after
+      set PID_OWNS_PORT=0
+      for /f "tokens=2,5" %%j in ('netstat -aon ^| find "TCP " ^| find ":0 " 
^| find ":!SOME_SOLR_PORT! "') do (
+        @REM j is the ip:port and k is the pid
+        IF "%%j"=="%SOLR_HOST_BIND%:!SOME_SOLR_PORT!" IF 
"%%k"=="!SOME_SOLR_PID!" set PID_OWNS_PORT=1
+      )
+      if "!PID_OWNS_PORT!"=="1" (
+        set found_it=1
+        @echo Stopping Solr process !SOME_SOLR_PID! running on port 
!SOME_SOLR_PORT!
+        IF "%STOP_PORT%"=="" (
+          set /A LOCAL_STOP_PORT=!SOME_SOLR_PORT! - 1000
+        ) else (
+          set LOCAL_STOP_PORT=%STOP_PORT%
+        )
+        "%JAVA%" %SOLR_SSL_OPTS% -Djetty.home="%SOLR_SERVER_DIR%" -jar 
"%SOLR_SERVER_DIR%\start.jar" STOP.PORT=!LOCAL_STOP_PORT! STOP.KEY=%STOP_KEY% 
--stop
+        REM wait for the process to terminate
+        CALL :wait_for_process_exit !SOME_SOLR_PID! !SOLR_STOP_WAIT!
+        REM Kill it if it is still running after the graceful shutdown
+        IF EXIST "%JAVA_HOME%\bin\jstack.exe" (
+          qprocess "!SOME_SOLR_PID!" >nul 2>nul && 
"%JAVA_HOME%\bin\jstack.exe" !SOME_SOLR_PID! && taskkill /f /PID !SOME_SOLR_PID!
+        ) else (
+          qprocess "!SOME_SOLR_PID!" >nul 2>nul && taskkill /f /PID 
!SOME_SOLR_PID!
         )
       )
+      CALL :delete_node_files !SOME_SOLR_PORT!

Review Comment:
   🤖 *AI text below* 🤖
   
   Could we remove the files only after confirming the recorded process stopped 
or the record is stale? This call also runs when `PID_OWNS_PORT` is `0`, which 
can simply mean that this invocation used a different `SOLR_HOST_BIND` from the 
one used at startup. The previous loop left the `.port` file alone on a bind 
mismatch; now a stop with the wrong include file removes the record a correctly 
configured retry needs. Also tie cleanup to the specific PID record inspected: 
if a replacement starts during the shutdown wait and publishes a new file on 
the same port, this unconditional delete removes the replacement’s record.



-- 
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