未验证 提交 efee53be 编写于 作者: Z zhuangchong 提交者: GitHub

[Improvement][server] server model ProcessUtils.java code cleaning. (#3382)

* server model ProcessUtils.java code cleaning.

* add checkstyle,server model ProcessUtils.java code cleaning.

* ProcessUtils.java checkstyle.

* update.

* update.
Co-authored-by: Nunknown <zhuangchong-phq@sinosig.com>
上级 2ba529a9
...@@ -14,53 +14,52 @@ ...@@ -14,53 +14,52 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.server.utils; package org.apache.dolphinscheduler.server.utils;
import java.nio.charset.StandardCharsets;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.CommonUtils; import org.apache.dolphinscheduler.common.utils.CommonUtils;
import org.apache.dolphinscheduler.common.utils.FileUtils;
import org.apache.dolphinscheduler.common.utils.LoggerUtils; import org.apache.dolphinscheduler.common.utils.LoggerUtils;
import org.apache.dolphinscheduler.common.utils.OSUtils; import org.apache.dolphinscheduler.common.utils.OSUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.commons.io.FileUtils;
import org.apache.dolphinscheduler.remote.utils.Host; import org.apache.dolphinscheduler.remote.utils.Host;
import org.apache.dolphinscheduler.server.entity.TaskExecutionContext; import org.apache.dolphinscheduler.server.entity.TaskExecutionContext;
import org.apache.dolphinscheduler.service.log.LogClientService; import org.apache.dolphinscheduler.service.log.LogClientService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
* mainly used to get the start command line of a process * mainly used to get the start command line of a process.
*/ */
public class ProcessUtils { public class ProcessUtils {
/** /**
* logger * logger.
*/ */
private final static Logger logger = LoggerFactory.getLogger(ProcessUtils.class); private static final Logger logger = LoggerFactory.getLogger(ProcessUtils.class);
/** /**
* Initialization regularization, solve the problem of pre-compilation performance, * Initialization regularization, solve the problem of pre-compilation performance,
* avoid the thread safety problem of multi-thread operation * avoid the thread safety problem of multi-thread operation.
*/ */
private static final Pattern MACPATTERN = Pattern.compile("-[+|-]-\\s(\\d+)"); private static final Pattern MACPATTERN = Pattern.compile("-[+|-]-\\s(\\d+)");
private static final Pattern WINDOWSATTERN = Pattern.compile("(\\d+)"); private static final Pattern WINDOWSATTERN = Pattern.compile("(\\d+)");
/** /**
* build command line characters * build command line characters.
* @param commandList command list * @param commandList command list
* @return command * @return command
* @throws IOException io exception
*/ */
public static String buildCommandStr(List<String> commandList) throws IOException { public static String buildCommandStr(List<String> commandList) {
String cmdstr; String cmdstr;
String[] cmd = commandList.toArray(new String[commandList.size()]); String[] cmd = commandList.toArray(new String[commandList.size()]);
SecurityManager security = System.getSecurityManager(); SecurityManager security = System.getSecurityManager();
...@@ -102,7 +101,6 @@ public class ProcessUtils { ...@@ -102,7 +101,6 @@ public class ProcessUtils {
} }
} }
cmdstr = createCommandLine( cmdstr = createCommandLine(
isShellFile(executablePath) ? VERIFICATION_CMD_BAT : VERIFICATION_WIN32, quoteString(executablePath), cmd); isShellFile(executablePath) ? VERIFICATION_CMD_BAT : VERIFICATION_WIN32, quoteString(executablePath), cmd);
...@@ -111,13 +109,12 @@ public class ProcessUtils { ...@@ -111,13 +109,12 @@ public class ProcessUtils {
} }
/** /**
* get executable path * get executable path.
* *
* @param path path * @param path path
* @return executable path * @return executable path
* @throws IOException io exception
*/ */
private static String getExecutablePath(String path) throws IOException { private static String getExecutablePath(String path) {
boolean pathIsQuoted = isQuoted(true, path, "Executable name has embedded quote, split the arguments"); boolean pathIsQuoted = isQuoted(true, path, "Executable name has embedded quote, split the arguments");
File fileToRun = new File(pathIsQuoted ? path.substring(1, path.length() - 1) : path); File fileToRun = new File(pathIsQuoted ? path.substring(1, path.length() - 1) : path);
...@@ -125,7 +122,7 @@ public class ProcessUtils { ...@@ -125,7 +122,7 @@ public class ProcessUtils {
} }
/** /**
* whether is shell file * whether is shell file.
* *
* @param executablePath executable path * @param executablePath executable path
* @return true if endsWith .CMD or .BAT * @return true if endsWith .CMD or .BAT
...@@ -136,7 +133,7 @@ public class ProcessUtils { ...@@ -136,7 +133,7 @@ public class ProcessUtils {
} }
/** /**
* quote string * quote string.
* *
* @param arg argument * @param arg argument
* @return format arg * @return format arg
...@@ -147,7 +144,7 @@ public class ProcessUtils { ...@@ -147,7 +144,7 @@ public class ProcessUtils {
} }
/** /**
* get tokens from command * get tokens from command.
* *
* @param command command * @param command command
* @return token string array * @return token string array
...@@ -162,7 +159,7 @@ public class ProcessUtils { ...@@ -162,7 +159,7 @@ public class ProcessUtils {
} }
/** /**
* Lazy Pattern * Lazy Pattern.
*/ */
private static class LazyPattern { private static class LazyPattern {
// Escape-support version: // Escape-support version:
...@@ -171,34 +168,29 @@ public class ProcessUtils { ...@@ -171,34 +168,29 @@ public class ProcessUtils {
} }
/** /**
* verification cmd bat * verification cmd bat.
*/ */
private static final int VERIFICATION_CMD_BAT = 0; private static final int VERIFICATION_CMD_BAT = 0;
/** /**
* verification win32 * verification win32.
*/ */
private static final int VERIFICATION_WIN32 = 1; private static final int VERIFICATION_WIN32 = 1;
/** /**
* verification legacy * verification legacy.
*/ */
private static final int VERIFICATION_LEGACY = 2; private static final int VERIFICATION_LEGACY = 2;
/** /**
* escape verification * escape verification.
*/ */
private static final char[][] ESCAPE_VERIFICATION = {{' ', '\t', '<', '>', '&', '|', '^'}, private static final char[][] ESCAPE_VERIFICATION = {{' ', '\t', '<', '>', '&', '|', '^'},
{' ', '\t', '<', '>'}, {' ', '\t'}}; {' ', '\t', '<', '>'}, {' ', '\t'}};
/**
* matcher
*/
private static Matcher matcher;
/** /**
* create command line * create command line.
* @param verificationType verification type * @param verificationType verification type
* @param executablePath executable path * @param executablePath executable path
* @param cmd cmd * @param cmd cmd
...@@ -227,7 +219,7 @@ public class ProcessUtils { ...@@ -227,7 +219,7 @@ public class ProcessUtils {
} }
/** /**
* whether is quoted * whether is quoted.
* @param noQuotesInside * @param noQuotesInside
* @param arg * @param arg
* @param errorMessage * @param errorMessage
...@@ -255,7 +247,7 @@ public class ProcessUtils { ...@@ -255,7 +247,7 @@ public class ProcessUtils {
} }
/** /**
* whether needs escaping * whether needs escaping.
* *
* @param verificationType verification type * @param verificationType verification type
* @param arg arg * @param arg arg
...@@ -277,16 +269,14 @@ public class ProcessUtils { ...@@ -277,16 +269,14 @@ public class ProcessUtils {
} }
/** /**
* kill yarn application * kill yarn application.
* *
* @param appIds app id list * @param appIds app id list
* @param logger logger * @param logger logger
* @param tenantCode tenant code * @param tenantCode tenant code
* @param executePath execute path * @param executePath execute path
* @throws IOException io exception
*/ */
public static void cancelApplication(List<String> appIds, Logger logger, String tenantCode,String executePath) public static void cancelApplication(List<String> appIds, Logger logger, String tenantCode, String executePath) {
throws IOException {
if (appIds.size() > 0) { if (appIds.size() > 0) {
String appid = appIds.get(appIds.size() - 1); String appid = appIds.get(appIds.size() - 1);
String commandFile = String String commandFile = String
...@@ -324,17 +314,17 @@ public class ProcessUtils { ...@@ -324,17 +314,17 @@ public class ProcessUtils {
} }
/** /**
* kill tasks according to different task types * kill tasks according to different task types.
* *
* @param taskExecutionContext taskExecutionContext * @param taskExecutionContext taskExecutionContext
*/ */
public static void kill(TaskExecutionContext taskExecutionContext) { public static void kill(TaskExecutionContext taskExecutionContext) {
try { try {
int processId = taskExecutionContext.getProcessId(); int processId = taskExecutionContext.getProcessId();
if(processId == 0 ){ if (processId == 0) {
logger.error("process kill failed, process id :{}, task id:{}", logger.error("process kill failed, process id :{}, task id:{}",
processId, taskExecutionContext.getTaskInstanceId()); processId, taskExecutionContext.getTaskInstanceId());
return ; return;
} }
String cmd = String.format("sudo kill -9 %s", getPidsStr(processId)); String cmd = String.format("sudo kill -9 %s", getPidsStr(processId));
...@@ -352,13 +342,13 @@ public class ProcessUtils { ...@@ -352,13 +342,13 @@ public class ProcessUtils {
} }
/** /**
* get pids str * get pids str.
* *
* @param processId process id * @param processId process id
* @return pids * @return pids
* @throws Exception exception * @throws Exception exception
*/ */
public static String getPidsStr(int processId)throws Exception{ public static String getPidsStr(int processId) throws Exception {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Matcher mat; Matcher mat;
// pstree pid get sub pids // pstree pid get sub pids
...@@ -370,14 +360,14 @@ public class ProcessUtils { ...@@ -370,14 +360,14 @@ public class ProcessUtils {
mat = WINDOWSATTERN.matcher(pids); mat = WINDOWSATTERN.matcher(pids);
} }
while (mat.find()){ while (mat.find()) {
sb.append(mat.group(1)).append(" "); sb.append(mat.group(1)).append(" ");
} }
return sb.toString().trim(); return sb.toString().trim();
} }
/** /**
* find logs and kill yarn tasks * find logs and kill yarn tasks.
* *
* @param taskExecutionContext taskExecutionContext * @param taskExecutionContext taskExecutionContext
*/ */
...@@ -392,7 +382,7 @@ public class ProcessUtils { ...@@ -392,7 +382,7 @@ public class ProcessUtils {
Constants.RPC_PORT, Constants.RPC_PORT,
taskExecutionContext.getLogPath()); taskExecutionContext.getLogPath());
} finally { } finally {
if(logClient != null){ if (logClient != null) {
logClient.close(); logClient.close();
} }
} }
......
...@@ -312,14 +312,8 @@ public abstract class AbstractCommandExecutor { ...@@ -312,14 +312,8 @@ public abstract class AbstractCommandExecutor {
* @param commands process builder * @param commands process builder
*/ */
private void printCommand(List<String> commands) { private void printCommand(List<String> commands) {
String cmdStr; String cmdStr = ProcessUtils.buildCommandStr(commands);
logger.info("task run command:\n{}", cmdStr);
try {
cmdStr = ProcessUtils.buildCommandStr(commands);
logger.info("task run command:\n{}", cmdStr);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
} }
/** /**
......
...@@ -40,11 +40,8 @@ public class ProcessUtilsTest { ...@@ -40,11 +40,8 @@ public class ProcessUtilsTest {
public void testBuildCommandStr() { public void testBuildCommandStr() {
List<String> commands = new ArrayList<>(); List<String> commands = new ArrayList<>();
commands.add("sudo"); commands.add("sudo");
try { Assert.assertEquals(ProcessUtils.buildCommandStr(commands), "sudo");
Assert.assertEquals(ProcessUtils.buildCommandStr(commands), "sudo");
} catch (IOException e) {
Assert.fail(e.getMessage());
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册