|
19 | 19 |
|
20 | 20 | import static com.google.common.base.Preconditions.checkNotNull; |
21 | 21 |
|
| 22 | +import org.apache.dolphinscheduler.common.graph.DAG; |
22 | 23 | import org.apache.dolphinscheduler.dao.entity.TaskDefinition; |
23 | 24 | import org.apache.dolphinscheduler.dao.entity.WorkflowTaskRelation; |
24 | 25 |
|
25 | | -import java.util.ArrayDeque; |
26 | 26 | import java.util.ArrayList; |
27 | 27 | import java.util.HashMap; |
28 | 28 | import java.util.HashSet; |
29 | 29 | import java.util.List; |
30 | 30 | import java.util.Map; |
31 | | -import java.util.Queue; |
32 | 31 | import java.util.Set; |
33 | 32 | import java.util.function.Function; |
34 | 33 | import java.util.stream.Collectors; |
@@ -61,57 +60,18 @@ public WorkflowGraph(List<WorkflowTaskRelation> workflowTaskRelations, List<Task |
61 | 60 | } |
62 | 61 |
|
63 | 62 | private void checkIfDAG(List<WorkflowTaskRelation> workflowTaskRelations, List<TaskDefinition> taskDefinitions) { |
64 | | - // If topology-sort-result`s size less than taskDefinitions`s size, then not a DAG |
65 | | - Map<Long, List<Long>> preTaskCodeMap = workflowTaskRelations |
66 | | - .stream() |
67 | | - .collect(Collectors.groupingBy(WorkflowTaskRelation::getPostTaskCode, |
68 | | - Collectors.mapping(WorkflowTaskRelation::getPreTaskCode, Collectors.toList()))); |
69 | | - Map<Long, List<Long>> postTaskCodeMap = workflowTaskRelations |
70 | | - .stream() |
71 | | - .collect(Collectors.groupingBy(WorkflowTaskRelation::getPreTaskCode, |
72 | | - Collectors.mapping(WorkflowTaskRelation::getPostTaskCode, Collectors.toList()))); |
73 | | - |
74 | | - // build in-degree count |
75 | | - Map<Long, Integer> inDegreeCount = new HashMap<>(); |
| 63 | + DAG<Long, TaskDefinition, WorkflowTaskRelation> graph = new DAG<>(); |
| 64 | + // Fill the vertices |
76 | 65 | for (TaskDefinition taskDefinition : taskDefinitions) { |
77 | | - List<Long> preTasks = preTaskCodeMap.get(taskDefinition.getCode()); |
78 | | - if (preTasks == null) { |
79 | | - inDegreeCount.put(taskDefinition.getCode(), 0); |
80 | | - } else { |
81 | | - inDegreeCount.put(taskDefinition.getCode(), preTasks.size()); |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - // Adds the task with zero-in-degree to the queue |
86 | | - Set<Long> visitTable = new HashSet<>(); |
87 | | - Queue<Long> queue = new ArrayDeque<>(); |
88 | | - for (Map.Entry<Long, Integer> entry : inDegreeCount.entrySet()) { |
89 | | - if (entry.getValue() == 0 && visitTable.add(entry.getKey())) { |
90 | | - queue.offer(entry.getKey()); |
91 | | - } |
| 66 | + graph.addNode(taskDefinition.getCode(), taskDefinition); |
92 | 67 | } |
93 | | - |
94 | | - // topology sort |
95 | | - Set<Long> resultTable = new HashSet<>(); |
96 | | - while (!queue.isEmpty()) { |
97 | | - Long taskCode = queue.poll(); |
98 | | - resultTable.add(taskCode); |
99 | | - |
100 | | - List<Long> postCodes = postTaskCodeMap.get(taskCode); |
101 | | - if (postCodes == null) { |
102 | | - continue; |
| 68 | + // Fill edge relations |
| 69 | + for (WorkflowTaskRelation relation : workflowTaskRelations) { |
| 70 | + long preTaskCode = relation.getPreTaskCode(); |
| 71 | + // When exist a ring cycle, then not a DAG. |
| 72 | + if (preTaskCode != 0 && !graph.addEdge(preTaskCode, relation.getPostTaskCode())) { |
| 73 | + throw new IllegalArgumentException("The workflow graph is not a DAG"); |
103 | 74 | } |
104 | | - for (Long postCode : postCodes) { |
105 | | - inDegreeCount.put(postCode, inDegreeCount.get(postCode) - 1); |
106 | | - |
107 | | - if (inDegreeCount.get(postCode) == 0) { |
108 | | - queue.offer(postCode); |
109 | | - } |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - if (resultTable.size() < taskDefinitions.size()) { |
114 | | - throw new IllegalArgumentException("The workflow task relation is not a DAG"); |
115 | 75 | } |
116 | 76 | } |
117 | 77 |
|
|
0 commit comments