forked from flyteorg/flytepropeller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
executable file
·79 lines (62 loc) · 2.09 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package compiler
import (
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flytepropeller/pkg/compiler/common"
"k8s.io/apimachinery/pkg/util/sets"
)
func toInterfaceProviderMap(tasks []common.InterfaceProvider) map[string]common.InterfaceProvider {
res := make(map[string]common.InterfaceProvider, len(tasks))
for _, task := range tasks {
res[task.GetID().String()] = task
}
return res
}
func toSlice(s sets.String) []string {
res := make([]string, 0, len(s))
for str := range s {
res = append(res, str)
}
return res
}
func toNodeIdsSet(nodes common.NodeIndex) sets.String {
res := sets.NewString()
for nodeID := range nodes {
res.Insert(nodeID)
}
return res
}
// Runs a depth-first coreWorkflow traversal to detect any cycles in the coreWorkflow. It produces the first cycle found, as well as
// all visited nodes and a boolean indicating whether or not it found a cycle.
func detectCycle(startNode string, neighbors func(nodeId string) sets.String) (cycle []common.NodeID, visited sets.String,
detected bool) {
// This is a set of nodes that were ever visited.
visited = sets.NewString()
// This is a set of in-progress visiting nodes.
visiting := sets.NewString()
var detector func(nodeId string) ([]common.NodeID, bool)
detector = func(nodeId string) ([]common.NodeID, bool) {
if visiting.Has(nodeId) {
return []common.NodeID{}, true
}
visiting.Insert(nodeId)
visited.Insert(nodeId)
for nextID := range neighbors(nodeId) {
if path, detected := detector(nextID); detected {
return append([]common.NodeID{nextID}, path...), true
}
}
visiting.Delete(nodeId)
return []common.NodeID{}, false
}
if path, detected := detector(startNode); detected {
return append([]common.NodeID{startNode}, path...), visiting, true
}
return []common.NodeID{}, visited, false
}
func toCompiledWorkflows(wfs ...*core.WorkflowTemplate) []*core.CompiledWorkflow {
compiledSubWfs := make([]*core.CompiledWorkflow, 0, len(wfs))
for _, wf := range wfs {
compiledSubWfs = append(compiledSubWfs, &core.CompiledWorkflow{Template: wf})
}
return compiledSubWfs
}