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