This is an automated email from the ASF dual-hosted git repository.

jnioche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/storm.git


The following commit(s) were added to refs/heads/master by this push:
     new 0c64be68a STORM-4017 Fix for isAnyWindowsProcessAlive with multiple 
pids
     new 389d4c26f Merge pull request #3615 from 
Scomocouk/STORM4017-FixisAnyWindowsProcessAlive
0c64be68a is described below

commit 0c64be68a11ccfa522b9c6e4ef7a22d19d1f1a65
Author: Scott Moore <scott.mo...@viavisolutions.com>
AuthorDate: Mon Jan 8 17:45:47 2024 +0000

    STORM-4017 Fix for isAnyWindowsProcessAlive with multiple pids
---
 .../java/org/apache/storm/utils/ServerUtils.java   | 69 +++++++++++-----------
 .../org/apache/storm/utils/ServerUtilsTest.java    | 13 +++-
 2 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/storm-server/src/main/java/org/apache/storm/utils/ServerUtils.java 
b/storm-server/src/main/java/org/apache/storm/utils/ServerUtils.java
index cc0cc240b..6928eb100 100644
--- a/storm-server/src/main/java/org/apache/storm/utils/ServerUtils.java
+++ b/storm-server/src/main/java/org/apache/storm/utils/ServerUtils.java
@@ -911,46 +911,47 @@ public class ServerUtils {
      * @throws IOException on I/O exception
      */
     private static boolean isAnyWindowsProcessAlive(Collection<Long> pids, 
String user) throws IOException {
-        List<String> cmdArgs = new ArrayList<>();
-        cmdArgs.add("tasklist");
-        cmdArgs.add("/fo");
-        cmdArgs.add("list");
-        pids.forEach(pid -> {
+        List<String> unexpectedUsers = new ArrayList<>();
+        for (Long pid: pids) {
+            List<String> cmdArgs = new ArrayList<>();
+            cmdArgs.add("tasklist");
+            cmdArgs.add("/fo");
+            cmdArgs.add("list");
             cmdArgs.add("/fi");
             cmdArgs.add("pid eq " + pid);
-        });
-        cmdArgs.add("/v");
-        LOG.debug("CMD: {}", String.join(" ", cmdArgs));
-        ProcessBuilder pb = new ProcessBuilder(cmdArgs);
-        pb.redirectError(ProcessBuilder.Redirect.INHERIT);
-        List<String> unexpectedUsers = new ArrayList<>();
-        try (BufferedReader in = new BufferedReader(new 
InputStreamReader(pb.start().getInputStream(), StandardCharsets.UTF_8))) {
-            int lineNo = 0;
-            String line;
-            while ((line = in.readLine()) != null) {
-                lineNo++;
-                LOG.debug("CMD-LINE#{}: {}", lineNo, line);
-                if (line.contains("User Name:")) { //Check for : in case 
someone called their user "User Name"
-                    //This line contains the user name for the pid we're 
looking up
-                    //Example line: "User Name:    exampleDomain\exampleUser"
-                    List<String> userNameLineSplitOnWhitespace = 
Arrays.asList(line.split(":"));
-                    if (userNameLineSplitOnWhitespace.size() == 2) {
-                        List<String> userAndMaybeDomain = 
Arrays.asList(userNameLineSplitOnWhitespace.get(1).trim().split("\\\\"));
-                        String processUser = userAndMaybeDomain.size() == 2 ? 
userAndMaybeDomain.get(1) : userAndMaybeDomain.get(0);
-                        processUser = processUser.trim();
-                        if (user.equals(processUser)) {
-                            return true;
+            cmdArgs.add("/v");
+            LOG.debug("CMD: {}", String.join(" ", cmdArgs));
+            ProcessBuilder pb = new ProcessBuilder(cmdArgs);
+            pb.redirectError(ProcessBuilder.Redirect.INHERIT);
+            try (BufferedReader in = new BufferedReader(new 
InputStreamReader(pb.start().getInputStream(), StandardCharsets.UTF_8))) {
+                int lineNo = 0;
+                String line;
+                while ((line = in.readLine()) != null) {
+                    lineNo++;
+                    LOG.debug("CMD-LINE#{}: {}", lineNo, line);
+                    if (line.contains("User Name:")) { //Check for : in case 
someone called their user "User Name"
+                        //This line contains the user name for the pid we're 
looking up
+                        //Example line: "User Name:    
exampleDomain\exampleUser"
+                        List<String> userNameLineSplitOnWhitespace = 
Arrays.asList(line.split(":"));
+                        if (userNameLineSplitOnWhitespace.size() == 2) {
+                            List<String> userAndMaybeDomain = 
Arrays.asList(userNameLineSplitOnWhitespace.get(1).trim().split("\\\\"));
+                            String processUser = userAndMaybeDomain.size() == 
2 ? userAndMaybeDomain.get(1) : userAndMaybeDomain.get(0);
+                            processUser = processUser.trim();
+                            if (user.equals(processUser)) {
+                                return true;
+                            }
+                            unexpectedUsers.add(processUser);
+                        } else {
+                            LOG.error("Received unexpected output from 
tasklist command. Expected one colon in user name line. Line was {}",
+                                    line);
                         }
-                        unexpectedUsers.add(processUser);
-                    } else {
-                        LOG.error("Received unexpected output from tasklist 
command. Expected one colon in user name line. Line was {}",
-                            line);
+                        break;
                     }
                 }
+            } catch (IOException ex) {
+                String err = String.format("Cannot read output of command 
\"%s\"", String.join(" ", cmdArgs));
+                throw new IOException(err, ex);
             }
-        } catch (IOException ex) {
-            String err = String.format("Cannot read output of command \"%s\"", 
String.join(" ", cmdArgs));
-            throw new IOException(err, ex);
         }
         String pidsAsStr = StringUtils.join(pids, ",");
         if (unexpectedUsers.isEmpty()) {
diff --git 
a/storm-server/src/test/java/org/apache/storm/utils/ServerUtilsTest.java 
b/storm-server/src/test/java/org/apache/storm/utils/ServerUtilsTest.java
index f21421e5f..a5908a6a2 100644
--- a/storm-server/src/test/java/org/apache/storm/utils/ServerUtilsTest.java
+++ b/storm-server/src/test/java/org/apache/storm/utils/ServerUtilsTest.java
@@ -106,17 +106,24 @@ public class ServerUtilsTest {
     private Collection<Long> getRunningProcessIds(String user) throws 
IOException {
         // get list of few running processes
         Collection<Long> pids = new ArrayList<>();
-        String cmd = ServerUtils.IS_ON_WINDOWS ? "tasklist" : (user == null) ? 
"ps -e" : "ps -U " + user ;
+        int pidIndex = 0;
+        String cmd;
+        if (ServerUtils.IS_ON_WINDOWS) {
+            cmd = "tasklist";
+            pidIndex = 1;
+        } else {
+            cmd = (user == null) ? "ps -e" : "ps -U " + user;
+        }
         Process p = Runtime.getRuntime().exec(cmd);
         try (BufferedReader input = new BufferedReader(new 
InputStreamReader(p.getInputStream()))) {
             String line;
             while ((line = input.readLine()) != null) {
                 line = line.trim();
-                if (line.isEmpty() || line.startsWith("PID")) {
+                if (line.isEmpty()) {
                     continue;
                 }
                 try {
-                    String pidStr = line.split("\\s")[0];
+                    String pidStr = line.split("\\s+")[pidIndex];
                     if (pidStr.equalsIgnoreCase("pid")) {
                         continue; // header line
                     }

Reply via email to