forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(argo-server): fix global variable validation error with reversed …
…dag.tasks (argoproj#4369) Signed-off-by: chenyu.zheng <[email protected]>
- Loading branch information
Showing
4 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package sorting | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type TopologicalSortingNode struct { | ||
NodeName string | ||
Dependencies []string | ||
} | ||
|
||
func TopologicalSorting(graph []*TopologicalSortingNode) ([]*TopologicalSortingNode, error) { | ||
priorNodeCountMap := make(map[string]int, len(graph)) // nodeName -> priorNodeCount | ||
nextNodeMap := make(map[string][]string, len(graph)) // nodeName -> nextNodeList | ||
nodeNameMap := make(map[string]*TopologicalSortingNode, len(graph)) // nodeName -> node | ||
for _, node := range graph { | ||
if _, ok := nodeNameMap[node.NodeName]; ok { | ||
return nil, fmt.Errorf("duplicated nodeName %s", node.NodeName) | ||
} | ||
nodeNameMap[node.NodeName] = node | ||
priorNodeCountMap[node.NodeName] = len(node.Dependencies) | ||
} | ||
for _, node := range graph { | ||
for _, dependency := range node.Dependencies { | ||
if _, ok := nodeNameMap[dependency]; !ok { | ||
return nil, fmt.Errorf("invalid dependency %s", dependency) | ||
} | ||
nextNodeMap[dependency] = append(nextNodeMap[dependency], node.NodeName) | ||
} | ||
} | ||
|
||
queue := make([]*TopologicalSortingNode, len(graph)) | ||
head, tail := 0, 0 | ||
for nodeName, priorNodeCount := range priorNodeCountMap { | ||
if priorNodeCount == 0 { | ||
queue[tail] = nodeNameMap[nodeName] | ||
tail += 1 | ||
} | ||
} | ||
|
||
for head < len(queue) { | ||
curr := queue[head] | ||
if curr == nil { | ||
return nil, fmt.Errorf("graph with cycle") | ||
} | ||
for _, next := range nextNodeMap[curr.NodeName] { | ||
if priorNodeCountMap[next] > 0 { | ||
if priorNodeCountMap[next] == 1 { | ||
queue[tail] = nodeNameMap[next] | ||
tail += 1 | ||
} | ||
priorNodeCountMap[next] -= 1 | ||
} | ||
} | ||
head += 1 | ||
} | ||
|
||
return queue, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package sorting | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func graphToString(graph []*TopologicalSortingNode) string { | ||
var nodeNames []string | ||
for _, node := range graph { | ||
nodeNames = append(nodeNames, node.NodeName) | ||
} | ||
return strings.Join(nodeNames, ",") | ||
} | ||
|
||
func TestTopologicalSorting_EmptyInput(t *testing.T) { | ||
result, err := TopologicalSorting([]*TopologicalSortingNode{}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if len(result) != 0 { | ||
t.Error("return value not empty", result) | ||
} | ||
} | ||
|
||
func TestTopologicalSorting_DuplicatedNode(t *testing.T) { | ||
graph := []*TopologicalSortingNode{ | ||
{ | ||
NodeName: "a", | ||
}, | ||
{ | ||
NodeName: "b", | ||
Dependencies: []string{ | ||
"a", | ||
}, | ||
}, | ||
{ | ||
NodeName: "a", | ||
Dependencies: []string{ | ||
"b", | ||
}, | ||
}, | ||
} | ||
_, err := TopologicalSorting(graph) | ||
if err == nil { | ||
t.Error("error missing") | ||
} | ||
} | ||
|
||
func TestTopologicalSorting_InvalidDependency(t *testing.T) { | ||
graph := []*TopologicalSortingNode{ | ||
{ | ||
NodeName: "a", | ||
}, | ||
{ | ||
NodeName: "b", | ||
Dependencies: []string{ | ||
"a", | ||
}, | ||
}, | ||
{ | ||
NodeName: "c", | ||
Dependencies: []string{ | ||
"a", | ||
"d", | ||
}, | ||
}, | ||
} | ||
_, err := TopologicalSorting(graph) | ||
if err == nil { | ||
t.Error("error missing") | ||
} | ||
} | ||
|
||
func TestTopologicalSorting_GraphWithCycle(t *testing.T) { | ||
graph := []*TopologicalSortingNode{ | ||
{ | ||
NodeName: "a", | ||
Dependencies: []string{ | ||
"b", | ||
}, | ||
}, | ||
{ | ||
NodeName: "b", | ||
Dependencies: []string{ | ||
"a", | ||
}, | ||
}, | ||
} | ||
_, err := TopologicalSorting(graph) | ||
if err == nil { | ||
t.Error("error missing") | ||
} | ||
} | ||
|
||
func TestTopologicalSorting_GraphWithCycle2(t *testing.T) { | ||
graph := []*TopologicalSortingNode{ | ||
{ | ||
NodeName: "a", | ||
}, | ||
{ | ||
NodeName: "b", | ||
Dependencies: []string{ | ||
"a", | ||
"c", | ||
}, | ||
}, | ||
{ | ||
NodeName: "c", | ||
Dependencies: []string{ | ||
"a", | ||
"b", | ||
}, | ||
}, | ||
} | ||
_, err := TopologicalSorting(graph) | ||
if err == nil { | ||
t.Error("error missing") | ||
} | ||
} | ||
|
||
func TestTopologicalSorting_ValidInput(t *testing.T) { | ||
graph := []*TopologicalSortingNode{ | ||
{ | ||
NodeName: "a", | ||
}, | ||
{ | ||
NodeName: "b", | ||
Dependencies: []string{ | ||
"a", | ||
}, | ||
}, | ||
{ | ||
NodeName: "c", | ||
Dependencies: []string{ | ||
"b", | ||
}, | ||
}, | ||
} | ||
result, err := TopologicalSorting(graph) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
resultStr := graphToString(result) | ||
if resultStr != "a,b,c" { | ||
t.Error("wrong output", resultStr) | ||
} | ||
} | ||
|
||
func TestTopologicalSorting_ValidInput2(t *testing.T) { | ||
graph := []*TopologicalSortingNode{ | ||
{ | ||
NodeName: "a", | ||
}, | ||
{ | ||
NodeName: "b", | ||
Dependencies: []string{ | ||
"a", | ||
}, | ||
}, | ||
{ | ||
NodeName: "c", | ||
Dependencies: []string{ | ||
"a", | ||
}, | ||
}, | ||
{ | ||
NodeName: "d", | ||
Dependencies: []string{ | ||
"b", | ||
"c", | ||
}, | ||
}, | ||
} | ||
result, err := TopologicalSorting(graph) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
resultStr := graphToString(result) | ||
if resultStr != "a,b,c,d" && resultStr != "a,c,b,d" { | ||
t.Error("wrong output", resultStr) | ||
} | ||
} | ||
|
||
func TestTopologicalSorting_ValidInput3(t *testing.T) { | ||
graph := []*TopologicalSortingNode{ | ||
{ | ||
NodeName: "a", | ||
}, | ||
{ | ||
NodeName: "b", | ||
}, | ||
} | ||
result, err := TopologicalSorting(graph) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
resultStr := graphToString(result) | ||
if resultStr != "a,b" && resultStr != "b,a" { | ||
t.Error("wrong output", resultStr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters