janhoy commented on code in PR #5348:
URL: https://github.com/apache/solr/pull/5348#discussion_r4655458909


##########
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:
   Enumeration now uses `dir /b /a-d`, same as `find -type f` in bin/solr. I'll 
skip a guard in the helper; a directory named `solr-<port>.pid` isn't a 
realistic case.
   



##########
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:
   Done in 6957a405aa0, files are removed after the wait/kill in both paths.
   



##########
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:
   The process map and the directory scan both happen inside one short-lived 
`bin/solr stop`/`status` invocation, so the window is milliseconds. That isn't 
worth the extra machinery.
   



##########
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:
   Intentional: this matches bin/solr, where a foreground node has no PID file 
and is stopped with `stop -p`. The foreground `.port` write is back for 
consistency, though nothing reads 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:
   Dropped the bind-address condition: the PID listening on the port is enough, 
same as bin/solr. The unconditional cleanup then only removes stale records, 
which matches bin/solr's `rm -f "$PIDF"`. A replacement starting mid-shutdown 
isn't handled by bin/solr either.
   



##########
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:
   Good catch. Without a target, `status --max-wait-secs` could return 0 before 
the JVM was even listed (the `--solr-url` arg was lost in SOLR-17685). 
Readiness is now scoped with `-p <port>`, and the PID file is written before 
checking the result, like bin/solr writes it before waiting.
   



##########
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:
   bin/solr writes the PID file the same way (`echo $! >`). Guarding against a 
status run that lands inside that single write is more complexity than the case 
is worth.
   



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