Unverified Commit 80529cf8 authored by gaoyan's avatar gaoyan Committed by GitHub

[Optimize ]optimize The Process (#2409)

* optimize The Process * fix processID use DTO * formate code
parent 227b6e9f
......@@ -87,7 +87,7 @@ public class ProcessAspect {
String parentStep = MDC.get(PROCESS_STEP);
ProcessStepType processStepType = processStep.type();
MDC.put(PROCESS_STEP, processStepType.getValue());
contextHolder.registerProcessStep(processStepType, MDC.get(PROCESS_NAME));
contextHolder.registerProcessStep(processStepType, MDC.get(PROCESS_NAME), parentStep);
try {
result = joinPoint.proceed();
......@@ -120,7 +120,7 @@ public class ProcessAspect {
Annotation[] paramAnn = annotations[i];
for (Annotation annotation : paramAnn) {
if (annotation instanceof ProcessId) {
return param;
return String.valueOf(param.hashCode());
}
}
}
......
......@@ -19,14 +19,18 @@
package org.dinky.context;
import org.dinky.aop.ProcessAspect;
import org.dinky.data.exception.BusException;
import org.dinky.process.enums.ProcessStatus;
import org.dinky.process.enums.ProcessStepType;
import org.dinky.process.enums.ProcessType;
import org.dinky.process.exception.DinkyException;
import org.dinky.process.model.ProcessEntity;
import org.dinky.process.model.ProcessStep;
import org.dinky.utils.LogUtil;
import org.apache.http.util.TextUtils;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.ArrayList;
......@@ -35,6 +39,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.MDC;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import com.alibaba.fastjson2.JSONObject;
......@@ -111,8 +116,8 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
throw new BusException(StrFormatter.format("process {} does not exist", processName));
}
logPross.get(processName).appendLog(log);
Map<String, ProcessStep> parentStep = getParentNode(processStep, getStepsMap(processName));
parentStep.get(processStep.getValue()).appendLog(log);
ProcessStep stepNode = getStepNode(processStep.getValue(), getStepsMap(processName));
stepNode.appendLog(log);
sendAsync(processName, logPross.get(processName));
}
......@@ -145,9 +150,11 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
*
* @param type process step type
* @param processName process name
* @param parentStep parent step
* @throws RuntimeException Throws an exception if the process does not exist
*/
public void registerProcessStep(ProcessStepType type, String processName) throws RuntimeException {
public void registerProcessStep(ProcessStepType type, String processName, String parentStep)
throws RuntimeException {
if (!logPross.containsKey(processName)) {
throw new BusException(StrFormatter.format("Process {} does not exist", processName));
}
......@@ -162,7 +169,14 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
.errLog(new StringBuilder())
.childStepsMap(new LinkedHashMap<>())
.build();
getParentNode(type, process.getStepsMap()).put(type.getValue(), processStep);
if (TextUtils.isEmpty(parentStep)) {
// parentStep为空表示为顶级节点
process.getStepsMap().put(type.getValue(), processStep);
} else {
ProcessStep stepNode = getStepNode(parentStep, process.getStepsMap());
stepNode.getChildStepsMap().put(type.getValue(), processStep);
}
}
/**
......@@ -203,8 +217,7 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
if (!logPross.containsKey(processName)) {
return;
}
Map<String, ProcessStep> processStepNode = getParentNode(type, getStepsMap(processName));
ProcessStep processStep = processStepNode.get(type.getValue());
ProcessStep processStep = getStepNode(type.getValue(), getStepsMap(processName));
processStep.setStepStatus(status);
processStep.setEndTime(LocalDateTime.now());
processStep.setTime(processStep.getEndTime().compareTo(processStep.getStartTime()));
......@@ -213,19 +226,35 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
}
}
private ProcessStep getStepNode(String stepType, Map<String, ProcessStep> stepsMap) {
ProcessStep stepNode = findStepNode(stepType, stepsMap);
if (stepNode != null) {
return stepNode;
}
String errorStr = StrFormatter.format(
"Get Parent Node Failed, This is most likely a Dinky bug, "
+ "please report the following information back to the community:\nProcess:{},\nstep:{},\nprocessNam:{}",
JSONObject.toJSONString(logPross),
stepType,
MDC.get(ProcessAspect.PROCESS_NAME));
throw new DinkyException(errorStr);
}
/**
* The * method first checks if a given type has a parent step (i.e. type.getParentStep()) is null).
* If there is a parent step, recursively call the getParentNode method, passing the parent step type and stepsMap
* to get the child step mapping of the parent node.
* If a given type does not have a parent step, it returns steps directly to indicate that the step of that type is a top-level step.
* Finally, the getParentNode method returns a mapping of child steps containing the parent node for use in other methods.
*/
private Map<String, ProcessStep> getParentNode(ProcessStepType type, Map<String, ProcessStep> stepsMap) {
if (type.getParentStep() != null) {
Map<String, ProcessStep> map = getParentNode(type.getParentStep(), stepsMap);
return map.get(type.getParentStep().getValue()).getChildStepsMap();
* 递归查找节点
* */
private ProcessStep findStepNode(String stepType, Map<String, ProcessStep> stepsMap) {
for (Map.Entry<String, ProcessStep> entry : stepsMap.entrySet()) {
if (entry.getKey().equals(stepType)) {
return entry.getValue();
} else {
ProcessStep stepNode = findStepNode(stepType, entry.getValue().getChildStepsMap());
if (stepNode != null) {
return stepNode;
}
}
}
return stepsMap;
return null;
}
private Map<String, ProcessStep> getStepsMap(String processName) {
......
......@@ -26,22 +26,20 @@ import lombok.Getter;
@Getter
public enum ProcessStepType {
SUBMIT_TASK("SUBMIT_TASK", null, Status.PROCESS_SUBMIT_SUBMITTASK),
SUBMIT_PRECHECK("SUBMIT_PRECHECK", SUBMIT_TASK, Status.PROCESS_SUBMIT_CHECKSQL),
SUBMIT_EXECUTE("SUBMIT_EXECUTE", SUBMIT_TASK, Status.PROCESS_SUBMIT_EXECUTE),
SUBMIT_BUILD_CONFIG("SUBMIT_BUILD_CONFIG", SUBMIT_EXECUTE, Status.PROCESS_SUBMIT_BUILDCONFIG),
SUBMIT_EXECUTE_COMMON_SQL("SUBMIT_EXECUTE_COMMON_SQL", SUBMIT_BUILD_CONFIG, Status.PROCESS_SUBMIT_EXECUTECOMMSQL),
SUBMIT_EXECUTE_FLINK_SQL("SUBMIT_EXECUTE_FLINK_SQL", SUBMIT_BUILD_CONFIG, Status.PROCESS_SUBMIT_EXECUTEFLINKSQL),
UNKNOWN("UNKNOWN", null, Status.UNKNOWN_ERROR),
SUBMIT_TASK("SUBMIT_TASK", Status.PROCESS_SUBMIT_SUBMITTASK),
SUBMIT_PRECHECK("SUBMIT_PRECHECK", Status.PROCESS_SUBMIT_CHECKSQL),
SUBMIT_EXECUTE("SUBMIT_EXECUTE", Status.PROCESS_SUBMIT_EXECUTE),
SUBMIT_BUILD_CONFIG("SUBMIT_BUILD_CONFIG", Status.PROCESS_SUBMIT_BUILDCONFIG),
SUBMIT_EXECUTE_COMMON_SQL("SUBMIT_EXECUTE_COMMON_SQL", Status.PROCESS_SUBMIT_EXECUTECOMMSQL),
SUBMIT_EXECUTE_FLINK_SQL("SUBMIT_EXECUTE_FLINK_SQL", Status.PROCESS_SUBMIT_EXECUTEFLINKSQL),
UNKNOWN("UNKNOWN", Status.UNKNOWN_ERROR),
;
private final String value;
private final ProcessStepType parentStep;
private final Status desc;
ProcessStepType(String type, ProcessStepType parentStep, Status desc) {
ProcessStepType(String type, Status desc) {
this.value = type;
this.parentStep = parentStep;
this.desc = desc;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment