From c24dbc448d9f8fc00878b287e421708c19a00fdc Mon Sep 17 00:00:00 2001 From: Eights-LI Date: Tue, 17 Nov 2020 22:25:25 +0800 Subject: [PATCH] deal with magic value --- .../dolphinscheduler/common/Constants.java | 33 ++++++++++++------- .../server/utils/ProcessUtils.java | 6 ++-- .../server/utils/ProcessUtilsTest.java | 7 ++-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java index 85066cc55a..6da58f7c9a 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java @@ -523,10 +523,9 @@ public final class Constants { public static final int HEARTBEAT_FOR_ZOOKEEPER_INFO_LENGTH = 10; + /** - * hadoop params constant - */ - /** + * hadoop params * jar */ public static final String JAR = "jar"; @@ -833,15 +832,15 @@ public final class Constants { public static final String FLINK_MAIN_CLASS = "-c"; - public static final int[] NOT_TERMINATED_STATES = new int[]{ - ExecutionStatus.SUBMITTED_SUCCESS.ordinal(), - ExecutionStatus.RUNNING_EXECUTION.ordinal(), - ExecutionStatus.DELAY_EXECUTION.ordinal(), - ExecutionStatus.READY_PAUSE.ordinal(), - ExecutionStatus.READY_STOP.ordinal(), - ExecutionStatus.NEED_FAULT_TOLERANCE.ordinal(), - ExecutionStatus.WAITTING_THREAD.ordinal(), - ExecutionStatus.WAITTING_DEPEND.ordinal() + public static final int[] NOT_TERMINATED_STATES = new int[] { + ExecutionStatus.SUBMITTED_SUCCESS.ordinal(), + ExecutionStatus.RUNNING_EXECUTION.ordinal(), + ExecutionStatus.DELAY_EXECUTION.ordinal(), + ExecutionStatus.READY_PAUSE.ordinal(), + ExecutionStatus.READY_STOP.ordinal(), + ExecutionStatus.NEED_FAULT_TOLERANCE.ordinal(), + ExecutionStatus.WAITTING_THREAD.ordinal(), + ExecutionStatus.WAITTING_DEPEND.ordinal() }; /** @@ -1009,4 +1008,14 @@ public final class Constants { * Network IP gets priority, default inner outer */ public static final String NETWORK_PRIORITY_STRATEGY = "dolphin.scheduler.network.priority.strategy"; + + /** + * exec shell scripts + */ + public static final String SH = "sh"; + + /** + * pstree, get pud and sub pid + */ + public static final String PSTREE = "pstree"; } diff --git a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java index 672ea7c70b..4cfc33d5a9 100644 --- a/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java +++ b/dolphinscheduler-server/src/main/java/org/apache/dolphinscheduler/server/utils/ProcessUtils.java @@ -339,7 +339,7 @@ public class ProcessUtils { FileUtils.writeStringToFile(new File(commandFile), sb.toString(), StandardCharsets.UTF_8); } - String runCmd = "sh " + commandFile; + String runCmd = String.format("%s %s", Constants.SH, commandFile); if (StringUtils.isNotEmpty(tenantCode)) { runCmd = "sudo -u " + tenantCode + " " + runCmd; } @@ -391,12 +391,12 @@ public class ProcessUtils { Matcher mat = null; // pstree pid get sub pids if (OSUtils.isMacOS()) { - String pids = OSUtils.exeCmd("pstree -sp " + processId); + String pids = OSUtils.exeCmd(String.format("%s -sp %d", Constants.PSTREE, processId)); if (null != pids) { mat = MACPATTERN.matcher(pids); } } else { - String pids = OSUtils.exeCmd("pstree -p " + processId); + String pids = OSUtils.exeCmd(String.format("%s -p %d", Constants.PSTREE, processId)); mat = WINDOWSATTERN.matcher(pids); } diff --git a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java index e2d514a2a5..4d1ed27619 100644 --- a/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java +++ b/dolphinscheduler-server/src/test/java/org/apache/dolphinscheduler/server/utils/ProcessUtilsTest.java @@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.server.utils; import static org.powermock.api.mockito.PowerMockito.when; +import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.ExecutionStatus; import org.apache.dolphinscheduler.common.utils.HadoopUtils; import org.apache.dolphinscheduler.common.utils.OSUtils; @@ -57,7 +58,7 @@ public class ProcessUtilsTest { PowerMockito.mockStatic(OSUtils.class); when(OSUtils.isMacOS()).thenReturn(true); - when(OSUtils.exeCmd("pstree -sp " + processId)).thenReturn(null); + when(OSUtils.exeCmd(String.format("%s -p %d", Constants.PSTREE, processId))).thenReturn(null); String pidListMac = ProcessUtils.getPidsStr(processId); Assert.assertEquals("", pidListMac); } @@ -96,8 +97,8 @@ public class ProcessUtilsTest { taskExecutionContext.setProcessId(1); PowerMockito.mockStatic(OSUtils.class); try { - when(OSUtils.exeCmd("pstree -sp " + 1)).thenReturn("1111"); - when(OSUtils.exeCmd("pstree -p " + 1)).thenReturn("1111"); + when(OSUtils.exeCmd(String.format("%s -sp %d", Constants.PSTREE, 1))).thenReturn("1111"); + when(OSUtils.exeCmd(String.format("%s -p %d", Constants.PSTREE, 1))).thenReturn("1111"); when(OSUtils.exeCmd("sudo kill -9")).thenReturn("1111"); } catch (Exception e) { e.printStackTrace();