|
|
|
@ -18,8 +18,11 @@
|
|
|
|
|
package org.apache.dolphinscheduler.server.utils; |
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.enums.ExecutionStatus; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.CommonUtils; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.FileUtils; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.HadoopUtils; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.LoggerUtils; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.OSUtils; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.StringUtils; |
|
|
|
@ -54,23 +57,19 @@ public class ProcessUtils {
|
|
|
|
|
|
|
|
|
|
private static final Pattern WINDOWSATTERN = Pattern.compile("(\\d+)"); |
|
|
|
|
|
|
|
|
|
private static final String LOCAL_PROCESS_EXEC = "jdk.lang.Process.allowAmbiguousCommands"; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* build command line characters. |
|
|
|
|
* |
|
|
|
|
* @param commandList command list |
|
|
|
|
* @return command |
|
|
|
|
*/ |
|
|
|
|
public static String buildCommandStr(List<String> commandList) { |
|
|
|
|
String cmdstr; |
|
|
|
|
String[] cmd = commandList.toArray(new String[commandList.size()]); |
|
|
|
|
String[] cmd = commandList.toArray(new String[0]); |
|
|
|
|
SecurityManager security = System.getSecurityManager(); |
|
|
|
|
boolean allowAmbiguousCommands = false; |
|
|
|
|
if (security == null) { |
|
|
|
|
allowAmbiguousCommands = true; |
|
|
|
|
String value = System.getProperty("jdk.lang.Process.allowAmbiguousCommands"); |
|
|
|
|
if (value != null) { |
|
|
|
|
allowAmbiguousCommands = !"false".equalsIgnoreCase(value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
boolean allowAmbiguousCommands = isAllowAmbiguousCommands(security); |
|
|
|
|
if (allowAmbiguousCommands) { |
|
|
|
|
|
|
|
|
|
String executablePath = new File(cmd[0]).getPath(); |
|
|
|
@ -108,6 +107,24 @@ public class ProcessUtils {
|
|
|
|
|
return cmdstr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* check is allow ambiguous commands |
|
|
|
|
* |
|
|
|
|
* @param security security manager |
|
|
|
|
* @return allow ambiguous command flag |
|
|
|
|
*/ |
|
|
|
|
private static boolean isAllowAmbiguousCommands(SecurityManager security) { |
|
|
|
|
boolean allowAmbiguousCommands = false; |
|
|
|
|
if (security == null) { |
|
|
|
|
allowAmbiguousCommands = true; |
|
|
|
|
String value = System.getProperty(LOCAL_PROCESS_EXEC); |
|
|
|
|
if (value != null) { |
|
|
|
|
allowAmbiguousCommands = !Constants.STRING_FALSE.equalsIgnoreCase(value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return allowAmbiguousCommands; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get executable path. |
|
|
|
|
* |
|
|
|
@ -139,8 +156,7 @@ public class ProcessUtils {
|
|
|
|
|
* @return format arg |
|
|
|
|
*/ |
|
|
|
|
private static String quoteString(String arg) { |
|
|
|
|
StringBuilder argbuf = new StringBuilder(arg.length() + 2); |
|
|
|
|
return argbuf.append('"').append(arg).append('"').toString(); |
|
|
|
|
return '"' + arg + '"'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -155,15 +171,17 @@ public class ProcessUtils {
|
|
|
|
|
while (regexMatcher.find()) { |
|
|
|
|
matchList.add(regexMatcher.group()); |
|
|
|
|
} |
|
|
|
|
return matchList.toArray(new String[matchList.size()]); |
|
|
|
|
return matchList.toArray(new String[0]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Lazy Pattern. |
|
|
|
|
*/ |
|
|
|
|
private static class LazyPattern { |
|
|
|
|
// Escape-support version:
|
|
|
|
|
// "(\")((?:\\\\\\1|.)+?)\\1|([^\\s\"]+)";
|
|
|
|
|
/** |
|
|
|
|
* Escape-support version: |
|
|
|
|
* "(\")((?:\\\\\\1|.)+?)\\1|([^\\s\"]+)"; |
|
|
|
|
*/ |
|
|
|
|
private static final Pattern PATTERN = Pattern.compile("[^\\s\"]+|\"[^\"]*\""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -191,6 +209,7 @@ public class ProcessUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* create command line. |
|
|
|
|
* |
|
|
|
|
* @param verificationType verification type |
|
|
|
|
* @param executablePath executable path |
|
|
|
|
* @param cmd cmd |
|
|
|
@ -220,29 +239,26 @@ public class ProcessUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* whether is quoted. |
|
|
|
|
* @param noQuotesInside |
|
|
|
|
* @param arg |
|
|
|
|
* @param errorMessage |
|
|
|
|
* |
|
|
|
|
* @param noQuotesInside no quotes inside |
|
|
|
|
* @param arg arg |
|
|
|
|
* @param errorMessage error message |
|
|
|
|
* @return boolean |
|
|
|
|
*/ |
|
|
|
|
private static boolean isQuoted(boolean noQuotesInside, String arg, String errorMessage) { |
|
|
|
|
int lastPos = arg.length() - 1; |
|
|
|
|
if (lastPos >= 1 && arg.charAt(0) == '"' && arg.charAt(lastPos) == '"') { |
|
|
|
|
// The argument has already been quoted.
|
|
|
|
|
if (noQuotesInside) { |
|
|
|
|
if (arg.indexOf('"', 1) != lastPos) { |
|
|
|
|
if (noQuotesInside && arg.indexOf('"', 1) != lastPos) { |
|
|
|
|
// There is ["] inside.
|
|
|
|
|
throw new IllegalArgumentException(errorMessage); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
if (noQuotesInside) { |
|
|
|
|
if (arg.indexOf('"') >= 0) { |
|
|
|
|
if (noQuotesInside && arg.indexOf('"') >= 0) { |
|
|
|
|
// There is ["] inside.
|
|
|
|
|
throw new IllegalArgumentException(errorMessage); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -259,8 +275,8 @@ public class ProcessUtils {
|
|
|
|
|
|
|
|
|
|
if (!argIsQuoted) { |
|
|
|
|
char[] testEscape = ESCAPE_VERIFICATION[verificationType]; |
|
|
|
|
for (int i = 0; i < testEscape.length; ++i) { |
|
|
|
|
if (arg.indexOf(testEscape[i]) >= 0) { |
|
|
|
|
for (char c : testEscape) { |
|
|
|
|
if (arg.indexOf(c) >= 0) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -277,18 +293,42 @@ public class ProcessUtils {
|
|
|
|
|
* @param executePath execute path |
|
|
|
|
*/ |
|
|
|
|
public static void cancelApplication(List<String> appIds, Logger logger, String tenantCode, String executePath) { |
|
|
|
|
if (appIds.size() > 0) { |
|
|
|
|
String appid = appIds.get(appIds.size() - 1); |
|
|
|
|
if (CollectionUtils.isNotEmpty(appIds)) { |
|
|
|
|
|
|
|
|
|
for (String appId : appIds) { |
|
|
|
|
try { |
|
|
|
|
ExecutionStatus applicationStatus = HadoopUtils.getInstance().getApplicationStatus(appId); |
|
|
|
|
|
|
|
|
|
if (!applicationStatus.typeIsFinished()) { |
|
|
|
|
String commandFile = String |
|
|
|
|
.format("%s/%s.kill", executePath, appid); |
|
|
|
|
String cmd = "yarn application -kill " + appid; |
|
|
|
|
.format("%s/%s.kill", executePath, appId); |
|
|
|
|
String cmd = "yarn application -kill " + appId; |
|
|
|
|
execYarnKillCommand(logger, tenantCode, appId, commandFile, cmd); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error(String.format("Get yarn application app id [%s] status failed: [%s]", appId, e.getMessage())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* build kill command for yarn application |
|
|
|
|
* |
|
|
|
|
* @param logger logger |
|
|
|
|
* @param tenantCode tenant code |
|
|
|
|
* @param appId app id |
|
|
|
|
* @param commandFile command file |
|
|
|
|
* @param cmd cmd |
|
|
|
|
*/ |
|
|
|
|
private static void execYarnKillCommand(Logger logger, String tenantCode, String appId, String commandFile, String cmd) { |
|
|
|
|
try { |
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
sb.append("#!/bin/sh\n"); |
|
|
|
|
sb.append("BASEDIR=$(cd `dirname $0`; pwd)\n"); |
|
|
|
|
sb.append("cd $BASEDIR\n"); |
|
|
|
|
if (CommonUtils.getSystemEnvPath() != null) { |
|
|
|
|
sb.append("source " + CommonUtils.getSystemEnvPath() + "\n"); |
|
|
|
|
sb.append("source ").append(CommonUtils.getSystemEnvPath()).append("\n"); |
|
|
|
|
} |
|
|
|
|
sb.append("\n\n"); |
|
|
|
|
sb.append(cmd); |
|
|
|
@ -299,17 +339,15 @@ 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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
logger.info("kill cmd:{}", runCmd); |
|
|
|
|
|
|
|
|
|
Runtime.getRuntime().exec(runCmd); |
|
|
|
|
OSUtils.exeCmd(runCmd); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("kill application error", e); |
|
|
|
|
} |
|
|
|
|
logger.error(String.format("Kill yarn application app id [%s] failed: [%s]", appId, e.getMessage())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -345,24 +383,29 @@ public class ProcessUtils {
|
|
|
|
|
* get pids str. |
|
|
|
|
* |
|
|
|
|
* @param processId process id |
|
|
|
|
* @return pids |
|
|
|
|
* @return pids pid String |
|
|
|
|
* @throws Exception exception |
|
|
|
|
*/ |
|
|
|
|
public static String getPidsStr(int processId) throws Exception { |
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
Matcher mat; |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (null != mat) { |
|
|
|
|
while (mat.find()) { |
|
|
|
|
sb.append(mat.group(1)).append(" "); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return sb.toString().trim(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -375,7 +418,7 @@ public class ProcessUtils {
|
|
|
|
|
try { |
|
|
|
|
Thread.sleep(Constants.SLEEP_TIME_MILLIS); |
|
|
|
|
LogClientService logClient = null; |
|
|
|
|
String log = null; |
|
|
|
|
String log; |
|
|
|
|
try { |
|
|
|
|
logClient = new LogClientService(); |
|
|
|
|
log = logClient.viewLog(Host.of(taskExecutionContext.getHost()).getIp(), |
|
|
|
@ -393,13 +436,13 @@ public class ProcessUtils {
|
|
|
|
|
logger.error("task instance work dir is empty"); |
|
|
|
|
throw new RuntimeException("task instance work dir is empty"); |
|
|
|
|
} |
|
|
|
|
if (appIds.size() > 0) { |
|
|
|
|
if (CollectionUtils.isNotEmpty(appIds)) { |
|
|
|
|
cancelApplication(appIds, logger, taskExecutionContext.getTenantCode(), taskExecutionContext.getExecutePath()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
logger.error("kill yarn job failure",e); |
|
|
|
|
logger.error("kill yarn job failure", e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|