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 { ...@@ -87,7 +87,7 @@ public class ProcessAspect {
String parentStep = MDC.get(PROCESS_STEP); String parentStep = MDC.get(PROCESS_STEP);
ProcessStepType processStepType = processStep.type(); ProcessStepType processStepType = processStep.type();
MDC.put(PROCESS_STEP, processStepType.getValue()); MDC.put(PROCESS_STEP, processStepType.getValue());
contextHolder.registerProcessStep(processStepType, MDC.get(PROCESS_NAME)); contextHolder.registerProcessStep(processStepType, MDC.get(PROCESS_NAME), parentStep);
try { try {
result = joinPoint.proceed(); result = joinPoint.proceed();
...@@ -120,7 +120,7 @@ public class ProcessAspect { ...@@ -120,7 +120,7 @@ public class ProcessAspect {
Annotation[] paramAnn = annotations[i]; Annotation[] paramAnn = annotations[i];
for (Annotation annotation : paramAnn) { for (Annotation annotation : paramAnn) {
if (annotation instanceof ProcessId) { if (annotation instanceof ProcessId) {
return param; return String.valueOf(param.hashCode());
} }
} }
} }
......
...@@ -19,14 +19,18 @@ ...@@ -19,14 +19,18 @@
package org.dinky.context; package org.dinky.context;
import org.dinky.aop.ProcessAspect;
import org.dinky.data.exception.BusException; import org.dinky.data.exception.BusException;
import org.dinky.process.enums.ProcessStatus; import org.dinky.process.enums.ProcessStatus;
import org.dinky.process.enums.ProcessStepType; import org.dinky.process.enums.ProcessStepType;
import org.dinky.process.enums.ProcessType; import org.dinky.process.enums.ProcessType;
import org.dinky.process.exception.DinkyException;
import org.dinky.process.model.ProcessEntity; import org.dinky.process.model.ProcessEntity;
import org.dinky.process.model.ProcessStep; import org.dinky.process.model.ProcessStep;
import org.dinky.utils.LogUtil; import org.dinky.utils.LogUtil;
import org.apache.http.util.TextUtils;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -35,6 +39,7 @@ import java.util.List; ...@@ -35,6 +39,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.MDC;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
...@@ -111,8 +116,8 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity> ...@@ -111,8 +116,8 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
throw new BusException(StrFormatter.format("process {} does not exist", processName)); throw new BusException(StrFormatter.format("process {} does not exist", processName));
} }
logPross.get(processName).appendLog(log); logPross.get(processName).appendLog(log);
Map<String, ProcessStep> parentStep = getParentNode(processStep, getStepsMap(processName)); ProcessStep stepNode = getStepNode(processStep.getValue(), getStepsMap(processName));
parentStep.get(processStep.getValue()).appendLog(log); stepNode.appendLog(log);
sendAsync(processName, logPross.get(processName)); sendAsync(processName, logPross.get(processName));
} }
...@@ -145,9 +150,11 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity> ...@@ -145,9 +150,11 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
* *
* @param type process step type * @param type process step type
* @param processName process name * @param processName process name
* @param parentStep parent step
* @throws RuntimeException Throws an exception if the process does not exist * @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)) { if (!logPross.containsKey(processName)) {
throw new BusException(StrFormatter.format("Process {} does not exist", processName)); throw new BusException(StrFormatter.format("Process {} does not exist", processName));
} }
...@@ -162,7 +169,14 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity> ...@@ -162,7 +169,14 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
.errLog(new StringBuilder()) .errLog(new StringBuilder())
.childStepsMap(new LinkedHashMap<>()) .childStepsMap(new LinkedHashMap<>())
.build(); .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> ...@@ -203,8 +217,7 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity>
if (!logPross.containsKey(processName)) { if (!logPross.containsKey(processName)) {
return; return;
} }
Map<String, ProcessStep> processStepNode = getParentNode(type, getStepsMap(processName)); ProcessStep processStep = getStepNode(type.getValue(), getStepsMap(processName));
ProcessStep processStep = processStepNode.get(type.getValue());
processStep.setStepStatus(status); processStep.setStepStatus(status);
processStep.setEndTime(LocalDateTime.now()); processStep.setEndTime(LocalDateTime.now());
processStep.setTime(processStep.getEndTime().compareTo(processStep.getStartTime())); processStep.setTime(processStep.getEndTime().compareTo(processStep.getStartTime()));
...@@ -213,19 +226,35 @@ public class ConsoleContextHolder extends BaseSseContext<String, ProcessEntity> ...@@ -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. private ProcessStep findStepNode(String stepType, Map<String, ProcessStep> stepsMap) {
* 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. for (Map.Entry<String, ProcessStep> entry : stepsMap.entrySet()) {
* Finally, the getParentNode method returns a mapping of child steps containing the parent node for use in other methods. if (entry.getKey().equals(stepType)) {
*/ return entry.getValue();
private Map<String, ProcessStep> getParentNode(ProcessStepType type, Map<String, ProcessStep> stepsMap) { } else {
if (type.getParentStep() != null) { ProcessStep stepNode = findStepNode(stepType, entry.getValue().getChildStepsMap());
Map<String, ProcessStep> map = getParentNode(type.getParentStep(), stepsMap); if (stepNode != null) {
return map.get(type.getParentStep().getValue()).getChildStepsMap(); return stepNode;
}
}
} }
return stepsMap; return null;
} }
private Map<String, ProcessStep> getStepsMap(String processName) { private Map<String, ProcessStep> getStepsMap(String processName) {
......
...@@ -26,22 +26,20 @@ import lombok.Getter; ...@@ -26,22 +26,20 @@ import lombok.Getter;
@Getter @Getter
public enum ProcessStepType { public enum ProcessStepType {
SUBMIT_TASK("SUBMIT_TASK", null, Status.PROCESS_SUBMIT_SUBMITTASK), SUBMIT_TASK("SUBMIT_TASK", Status.PROCESS_SUBMIT_SUBMITTASK),
SUBMIT_PRECHECK("SUBMIT_PRECHECK", SUBMIT_TASK, Status.PROCESS_SUBMIT_CHECKSQL), SUBMIT_PRECHECK("SUBMIT_PRECHECK", Status.PROCESS_SUBMIT_CHECKSQL),
SUBMIT_EXECUTE("SUBMIT_EXECUTE", SUBMIT_TASK, Status.PROCESS_SUBMIT_EXECUTE), SUBMIT_EXECUTE("SUBMIT_EXECUTE", Status.PROCESS_SUBMIT_EXECUTE),
SUBMIT_BUILD_CONFIG("SUBMIT_BUILD_CONFIG", SUBMIT_EXECUTE, Status.PROCESS_SUBMIT_BUILDCONFIG), SUBMIT_BUILD_CONFIG("SUBMIT_BUILD_CONFIG", Status.PROCESS_SUBMIT_BUILDCONFIG),
SUBMIT_EXECUTE_COMMON_SQL("SUBMIT_EXECUTE_COMMON_SQL", SUBMIT_BUILD_CONFIG, Status.PROCESS_SUBMIT_EXECUTECOMMSQL), SUBMIT_EXECUTE_COMMON_SQL("SUBMIT_EXECUTE_COMMON_SQL", Status.PROCESS_SUBMIT_EXECUTECOMMSQL),
SUBMIT_EXECUTE_FLINK_SQL("SUBMIT_EXECUTE_FLINK_SQL", SUBMIT_BUILD_CONFIG, Status.PROCESS_SUBMIT_EXECUTEFLINKSQL), SUBMIT_EXECUTE_FLINK_SQL("SUBMIT_EXECUTE_FLINK_SQL", Status.PROCESS_SUBMIT_EXECUTEFLINKSQL),
UNKNOWN("UNKNOWN", null, Status.UNKNOWN_ERROR), UNKNOWN("UNKNOWN", Status.UNKNOWN_ERROR),
; ;
private final String value; private final String value;
private final ProcessStepType parentStep;
private final Status desc; private final Status desc;
ProcessStepType(String type, ProcessStepType parentStep, Status desc) { ProcessStepType(String type, Status desc) {
this.value = type; this.value = type;
this.parentStep = parentStep;
this.desc = desc; 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