-
Notifications
You must be signed in to change notification settings - Fork 124
/
dependency_graph.go
284 lines (242 loc) · 8.54 KB
/
dependency_graph.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package task
import (
"fmt"
"strings"
"github.com/pkg/errors"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/multi"
"gonum.org/v1/gonum/graph/topo"
"gonum.org/v1/gonum/graph/traverse"
)
// DependencyGraph models task dependency relationships as a directed graph.
// Use NewDependencyGraph to initialize a new DependencyGraph.
type DependencyGraph struct {
transposed bool
graph *multi.DirectedGraph
tasksToNodes map[TaskNode]graph.Node
nodesToTasks map[graph.Node]TaskNode
edgesToDependencies map[edgeKey]DependencyEdge
}
// NewDependencyGraph returns an initialized DependencyGraph.
// transposed determines the direction of edges in the graph.
// If transposed is false, edges point from dependent tasks to the tasks they depend on.
// If transposed is true, edges point from depended on tasks to the tasks that depend on them.
func NewDependencyGraph(transposed bool) DependencyGraph {
return DependencyGraph{
transposed: transposed,
graph: multi.NewDirectedGraph(),
tasksToNodes: make(map[TaskNode]graph.Node),
nodesToTasks: make(map[graph.Node]TaskNode),
edgesToDependencies: make(map[edgeKey]DependencyEdge),
}
}
type edgeKey struct {
from TaskNode
to TaskNode
}
// DependencyEdge is a representation of a dependency in the graph.
type DependencyEdge struct {
// Status is the status specified by the dependency, if any.
Status string
// From is the node the edge begins from.
From TaskNode
// To is the node the edge points to.
To TaskNode
}
// TaskNode is the representation of a task in the graph.
type TaskNode struct {
// Name is the display name of the task.
Name string
// Variant is the build variant of the task.
Variant string
// ID is the task's ID.
ID string
}
// String represents TaskNode as a string.
func (t TaskNode) String() string {
if t.ID != "" {
return t.ID
}
return fmt.Sprintf("%s/%s", t.Variant, t.Name)
}
// VersionDependencyGraph finds all the tasks from the version given by versionID and constructs a DependencyGraph from them.
func VersionDependencyGraph(versionID string, transposed bool) (DependencyGraph, error) {
tasks, err := FindWithFields(ByVersion(versionID), DependsOnKey, BuildVariantKey, DisplayNameKey)
if err != nil {
return DependencyGraph{}, errors.Wrapf(err, "getting tasks for version '%s'", versionID)
}
return taskDependencyGraph(tasks, transposed), nil
}
func taskDependencyGraph(tasks []Task, transposed bool) DependencyGraph {
g := NewDependencyGraph(transposed)
g.buildFromTasks(tasks)
return g
}
func (g *DependencyGraph) buildFromTasks(tasks []Task) {
taskIDToNode := make(map[string]TaskNode)
for _, task := range tasks {
tNode := task.ToTaskNode()
g.AddTaskNode(tNode)
taskIDToNode[task.Id] = tNode
}
for _, task := range tasks {
dependentTaskNode := task.ToTaskNode()
for _, dep := range task.DependsOn {
dependedOnTaskNode := taskIDToNode[dep.TaskId]
g.AddEdge(dependentTaskNode, dependedOnTaskNode, dep.Status)
}
}
}
// Nodes returns a slice of all the task nodes in the graph.
func (g *DependencyGraph) Nodes() []TaskNode {
tNodes := make([]TaskNode, 0, len(g.tasksToNodes))
for tNode := range g.tasksToNodes {
tNodes = append(tNodes, tNode)
}
return tNodes
}
// AddTaskNode adds a node to the graph.
func (g *DependencyGraph) AddTaskNode(tNode TaskNode) {
if _, ok := g.tasksToNodes[tNode]; ok {
return
}
node := g.graph.NewNode()
g.graph.AddNode(node)
g.tasksToNodes[tNode] = node
g.nodesToTasks[node] = tNode
}
// AddEdge adds an edge between tasks in the graph.
// The edge direction is determined by whether the DependencyGraph is transposed.
// Noop if one of the nodes doesn't exist in the graph.
func (g *DependencyGraph) AddEdge(dependentTask, dependedOnTask TaskNode, status string) {
if g.transposed {
g.addEdgeToGraph(DependencyEdge{From: dependedOnTask, To: dependentTask, Status: status})
} else {
g.addEdgeToGraph(DependencyEdge{From: dependentTask, To: dependedOnTask, Status: status})
}
}
func (g *DependencyGraph) addEdgeToGraph(edge DependencyEdge) {
fromNode, fromExists := g.tasksToNodes[edge.From]
toNode, toExists := g.tasksToNodes[edge.To]
if !(fromExists && toExists) {
return
}
line := g.graph.NewLine(fromNode, toNode)
g.graph.SetLine(line)
g.edgesToDependencies[edgeKey{from: edge.From, to: edge.To}] = edge
}
// EdgesIntoTask returns all the edges that point to t.
// For a regular graph these edges are tasks that directly depend on t.
// If the graph is transposed these edges are tasks t directly depends on.
func (g *DependencyGraph) EdgesIntoTask(t TaskNode) []DependencyEdge {
node := g.tasksToNodes[t]
if node == nil {
return nil
}
var edges []DependencyEdge
nodes := g.graph.To(node.ID())
for nodes.Next() {
edges = append(edges, g.edgesToDependencies[edgeKey{from: g.nodesToTasks[nodes.Node()], to: t}])
}
return edges
}
// GetDependencyEdge returns a pointer to the edge from fromNode to toNode.
// If the edge doesn't exist it returns nil.
func (g *DependencyGraph) GetDependencyEdge(fromTask, toTask TaskNode) *DependencyEdge {
depEdge, ok := g.edgesToDependencies[edgeKey{from: fromTask, to: toTask}]
if !ok {
return nil
}
return &depEdge
}
// DependencyCycles is a jagged array of node cycles.
type DependencyCycles [][]TaskNode
// String represents DependencyCycles as a string.
func (dc DependencyCycles) String() string {
cycles := make([]string, 0, len(dc))
for _, cycle := range dc {
cycleStrings := make([]string, 0, len(cycle))
for _, node := range cycle {
cycleStrings = append(cycleStrings, node.String())
}
cycles = append(cycles, fmt.Sprintf("[%s]", strings.Join(cycleStrings, ", ")))
}
return strings.Join(cycles, ", ")
}
// Cycles returns cycles in the graph, if any.
// Self-loops are also considered cycles.
func (g *DependencyGraph) Cycles() DependencyCycles {
var cycles DependencyCycles
stronglyConnectedComponents := topo.TarjanSCC(g.graph)
for _, scc := range stronglyConnectedComponents {
if len(scc) == 1 {
if g.graph.HasEdgeBetween(scc[0].ID(), scc[0].ID()) {
cycles = append(cycles, []TaskNode{g.nodesToTasks[scc[0]], g.nodesToTasks[scc[0]]})
}
} else {
var cycle []TaskNode
for _, node := range scc {
taskInCycle := g.nodesToTasks[node]
cycle = append(cycle, taskInCycle)
}
cycles = append(cycles, cycle)
}
}
return cycles
}
// DepthFirstSearch begins a DFS from start and returns whether target is reachable.
// If traverseEdge is not nil an edge is only traversed if traverseEdge returns true on that edge.
func (g *DependencyGraph) DepthFirstSearch(start, target TaskNode, traverseEdge func(edge DependencyEdge) bool) bool {
_, startExists := g.tasksToNodes[start]
_, targetExists := g.tasksToNodes[target]
if !(startExists && targetExists) {
return false
}
traversal := traverse.DepthFirst{
Traverse: func(e graph.Edge) bool {
if traverseEdge == nil {
return true
}
from := g.nodesToTasks[e.From()]
to := g.nodesToTasks[e.To()]
edge := g.edgesToDependencies[edgeKey{from: from, to: to}]
return traverseEdge(edge)
},
}
return traversal.Walk(g.graph, g.tasksToNodes[start], func(n graph.Node) bool { return g.nodesToTasks[n] == target }) != nil
}
// TopologicalStableSort sorts the nodes in the graph topologically. It is stable in the sense that when a topological ordering
// is ambiguous the order the tasks were added to the graph prevails.
// To sort with all dependent tasks before the tasks they depend on use the default graph.
// To sort with all depended on tasks before the tasks that depend on them use a transposed graph.
func (g *DependencyGraph) TopologicalStableSort() ([]TaskNode, error) {
sortedNodes, err := topo.SortStabilized(g.graph, nil)
if err != nil {
_, ok := err.(topo.Unorderable)
if !ok {
return nil, errors.Wrap(err, "sorting the graph")
}
}
sortedTasks := make([]TaskNode, 0, len(sortedNodes))
for _, node := range sortedNodes {
if node != nil {
sortedTasks = append(sortedTasks, g.nodesToTasks[node])
}
}
return sortedTasks, nil
}
// reachableFromNode returns all the dependencies recursively depended on by start.
// In the case of a transposed graph it returns all the dependencies recursively depending on start.
// The start node is not included in the result.
func (g *DependencyGraph) reachableFromNode(start TaskNode) []TaskNode {
var reachable []TaskNode
traversal := traverse.DepthFirst{
Visit: func(node graph.Node) {
if g.nodesToTasks[node] != start {
reachable = append(reachable, g.nodesToTasks[node])
}
},
}
_ = traversal.Walk(g.graph, g.tasksToNodes[start], nil)
return reachable
}