det101 commented on code in PR #18385:
URL:
https://github.com/apache/dolphinscheduler/pull/18385#discussion_r3480389001
##########
dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/engine/graph/WorkflowGraph.java:
##########
@@ -64,6 +67,53 @@ public WorkflowGraph(List<WorkflowTaskRelation>
workflowTaskRelations, List<Task
addTaskEdge(workflowTaskRelations);
}
+ private void checkIfDAG(List<WorkflowTaskRelation> workflowTaskRelations,
List<TaskDefinition> taskDefinitions) {
+ Map<Long, List<Long>> preTaskCodeMap = workflowTaskRelations
+ .stream()
+ .filter(relation -> relation.getPreTaskCode() > 0 &&
relation.getPostTaskCode() > 0)
+
.collect(Collectors.groupingBy(WorkflowTaskRelation::getPostTaskCode,
+
Collectors.mapping(WorkflowTaskRelation::getPreTaskCode, Collectors.toList())));
+ Map<Long, List<Long>> postTaskCodeMap = workflowTaskRelations
+ .stream()
+ .filter(relation -> relation.getPreTaskCode() > 0 &&
relation.getPostTaskCode() > 0)
+
.collect(Collectors.groupingBy(WorkflowTaskRelation::getPreTaskCode,
+
Collectors.mapping(WorkflowTaskRelation::getPostTaskCode,
Collectors.toList())));
+
+ Map<Long, Integer> inDegreeCount = new HashMap<>();
+ for (TaskDefinition taskDefinition : taskDefinitions) {
+ List<Long> preTasks = preTaskCodeMap.get(taskDefinition.getCode());
+ inDegreeCount.put(taskDefinition.getCode(), preTasks == null ? 0 :
preTasks.size());
+ }
+
+ Deque<Long> queue = new ArrayDeque<>();
+ for (Map.Entry<Long, Integer> entry : inDegreeCount.entrySet()) {
+ if (entry.getValue() == 0) {
+ queue.addLast(entry.getKey());
+ }
+ }
+
+ Set<Long> sortedTaskCodes = new HashSet<>();
+ while (!queue.isEmpty()) {
+ Long taskCode = queue.pollFirst();
+ sortedTaskCodes.add(taskCode);
+
+ List<Long> postCodes = postTaskCodeMap.get(taskCode);
+ if (postCodes == null) {
+ continue;
+ }
+ for (Long postCode : postCodes) {
+ inDegreeCount.put(postCode, inDegreeCount.get(postCode) - 1);
+ if (inDegreeCount.get(postCode) == 0) {
+ queue.addLast(postCode);
+ }
+ }
+ }
+
+ if (sortedTaskCodes.size() < taskDefinitions.size()) {
+ throw new IllegalArgumentException("The workflow task relation is
not a DAG");
Review Comment:
Include details about the relevant tasks in the suggestion error to
facilitate quick identification.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]