Skip to content

[Bug] [Task Api] The MACPATTERN in ProcessUtils does not cover all cases on MacOS and ProcessUtilsTest failed on MacOS #15480

@CommandCV

Description

@CommandCV

Search before asking

  • I had searched in the issues and found no similar issues.

What happened

org.apache.dolphinscheduler.plugin.task.api.utils.ProcessUtils

The is the MACPATTERN in ProcessUtils:

private static final Pattern MACPATTERN = Pattern.compile("-[+|-]-\\s(\\d+)");

and the getPidsStr method is:

public static String getPidsStr(int processId) throws Exception {
        StringBuilder sb = new StringBuilder();
        Matcher mat = null;
        // pstree pid get sub pids
        if (SystemUtils.IS_OS_MAC) {
            String pids = OSUtils.exeCmd(String.format("%s -sp %d", TaskConstants.PSTREE, processId));
            if (StringUtils.isNotEmpty(pids)) {
                mat = MACPATTERN.matcher(pids);
            }
        } else if (SystemUtils.IS_OS_LINUX) {
            String pids = OSUtils.exeCmd(String.format("%s -p %d", TaskConstants.PSTREE, processId));
            if (StringUtils.isNotEmpty(pids)) {
                mat = LINUXPATTERN.matcher(pids);
            }
        } else {
            String pids = OSUtils.exeCmd(String.format("%s -p %d", TaskConstants.PSTREE, processId));
            if (StringUtils.isNotEmpty(pids)) {
                mat = WINDOWSPATTERN.matcher(pids);
            }
        }

        if (null != mat) {
            while (mat.find()) {
                sb.append(mat.group(1)).append(" ");
            }
        }

        return sb.toString().trim();
    }

when I executed pstree -sp in MacOS, this was all the case:
pstree -sp 04619

-+= 04619 xxxxxx /Applications/IntelliJ IDEA.app/Contents/MacOS/idea
 |--- 04626 xxxxxx /Applications/IntelliJ IDEA.app/Contents/bin/fsnotifier
 |--- 04730 xxxxxx /Users/xxxxxx/Library/Java/JavaVirtualMachines/corretto-17.0.8.1/Contents/Home/bin/java -Djava.awt.headless=true -Dmaven.defaultProjectBuilder.disableGlobalModelCache=true -D
 |--- 04997 xxxxxx /Applications/IntelliJ IDEA.app/Contents/jbr/Contents/Home/bin/java -Xmx1024m -Didea.version=2023.2.5 -Dfile.encoding=UTF-8 -classpath /Applications/IntelliJ IDEA.app/Contents/lib
 \--= 04769 xxxxxx /bin/zsh --login -i

pstree -sp 04642

 -+= 04642 xxxxxx /Applications/Docker.app/Contents/MacOS/com.docker.backend
 |--- 04656 xxxxxx docker serve --address unix:///Users/xxxxxx/.docker/run/docker-cli-api.sock
 |--- 04658 xxxxxx com.docker.extensions -address /Users/xxxxxx/Library/Containers/com.docker.docker/Data/extension-manager.sock -watchdog
 |--- 04659 xxxxxx com.docker.dev-envs -watchdog
 |--- 04660 xxxxxx com.docker.build
 |--- 04661 xxxxxx /Applications/Docker.app/Contents/MacOS/com.docker.virtualization --kernel /Applications/Docker.app/Contents/Resources/linuxkit/kernel --cmdline init=/init loglevel=1 root=/dev/vd
 \-+- 04685 xxxxxx /Applications/Docker.app/Contents/MacOS/Docker Desktop.app/Contents/MacOS/Docker Desktop --name=dashboard
   |--- 04692 xxxxxx /Applications/Docker.app/Contents/MacOS/Docker Desktop.app/Contents/Frameworks/Docker Desktop Helper (GPU).app/Contents/MacOS/Docker Desktop Helper (GPU) --type=gpu-process --us
   |--- 04693 xxxxxx /Applications/Docker.app/Contents/MacOS/Docker Desktop.app/Contents/Frameworks/Docker Desktop Helper.app/Contents/MacOS/Docker Desktop Helper --type=utility --utility-sub-type=n
   |--- 04696 xxxxxx /Applications/Docker.app/Contents/MacOS/Docker Desktop.app/Contents/Frameworks/Docker Desktop Helper (Renderer).app/Contents/MacOS/Docker Desktop Helper (Renderer) --type=render
   |--- 04700 xxxxxx /Applications/Docker.app/Contents/MacOS/Docker Desktop.app/Contents/Frameworks/Docker Desktop Helper (Renderer).app/Contents/MacOS/Docker Desktop Helper (Renderer) --type=render
   |-+- 06650 xxxxxx /Applications/Docker.app/Contents/Resources/bin/docker stats --all --no-trunc --no-stream --format {{ json .}}
   | \--- 06651 xxxxxx /Applications/Docker.app/Contents/Resources/bin/com.docker.cli stats --all --no-trunc --no-stream --format {{ json .}}
   \-+- 06652 xxxxxx /Applications/Docker.app/Contents/Resources/bin/docker stats --all --no-trunc --no-stream --format {{ json .}}
     \--- 06653 xxxxxx /Applications/Docker.app/Contents/Resources/bin/com.docker.cli stats --all --no-trunc --no-stream --format {{ json .}}

The conclusion drawn from the above results is: there are a total of 4 types of output: '-+=', '--=', '-+-', and '---', but the pattern: -[+|-]-\\s(\\d+) only covers 2 types: '-+-' and '---'.

org.apache.dolphinscheduler.plugin.task.api.utils.ProcessUtilsTest

In ProcessUtilsTest, the testGetPidsStr only considers linux scenario:

String pids = "sudo(6279)---558_1497.sh(6282)---sleep(6354)";
int processId = 6279;
String exceptPidsStr = "6279 6282 6354";
String command;
MockedStatic<OSUtils> osUtilsMockedStatic = Mockito.mockStatic(OSUtils.class);
if (SystemUtils.IS_OS_MAC) {
    command = String.format("%s -sp %d", TaskConstants.PSTREE, processId);
} else if (SystemUtils.IS_OS_LINUX) {
    command = String.format("%s -p %d", TaskConstants.PSTREE, processId);
} else {
    command = String.format("%s -p %d", TaskConstants.PSTREE, processId);
}
osUtilsMockedStatic.when(() -> OSUtils.exeCmd(command)).thenReturn(pids);
String actualPidsStr = ProcessUtils.getPidsStr(processId);
Assertions.assertEquals(exceptPidsStr, actualPidsStr);

If you run this test on MacOS, you get this error:

org.opentest4j.AssertionFailedError: 
Expected :6279 6282 6354
Actual   :

What you expected to happen

The MACPATTERN in ProcessUtils should cover all MacOS cases and run unit tests successfully on MacOS.

How to reproduce

Run thegetPidsStr method in ProcessUtils on MacOS.

Anything else

No response

Version

3.2.x

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions