-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplan_graph.go
More file actions
171 lines (131 loc) · 3.17 KB
/
Copy pathplan_graph.go
File metadata and controls
171 lines (131 loc) · 3.17 KB
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
package running
import (
"fmt"
"strings"
"sync"
)
type _DAG struct {
NodeRefs map[string]*_NodeRef
Vertexes map[string]*_Vertex
Warning []string
sync.Mutex
}
type _Vertex struct {
Prev int
Traversed bool
Next []*_Vertex
RefRoot *_NodeRef
}
func newDAG() *_DAG {
return &_DAG{
NodeRefs: map[string]*_NodeRef{},
Vertexes: map[string]*_Vertex{},
}
}
// Verify detect if there is a circular dependency.
// include external circular dependency of the graph and internal circular dependency of the cluster.
func (graph *_DAG) Verify() error {
color := make(map[string]int8)
steps, left := graph.Steps()
if len(left) != 0 {
return fmt.Errorf("found cycle between nodes: %v", left)
}
for _, nodeNames := range steps {
for _, nodeName := range nodeNames {
if graph.NodeRefs[nodeName].HasCycle(color) {
return fmt.Errorf("found cycle in node: %s", nodeName)
}
}
}
return nil
}
// Steps list graph steps and left nodes.
// steps example: [[A, B], C], which means that A and B should be processed before C.
// left node can never be processed, implies a circular dependency.
func (graph *_DAG) Steps() ([][]string, []string) {
steps := make([][]string, 0)
graph.Lock()
for {
var names []string
for name, vertex := range graph.Vertexes {
// vertex is never traversed and all prev vertex are done, so it can be processed
if !vertex.Traversed && vertex.Prev == 0 {
names = append(names, name)
}
}
if len(names) == 0 {
break
}
// set vertex status to traversed
for _, name := range names {
graph.Vertexes[name].Traversed = true
for _, vertex := range graph.Vertexes[name].Next {
vertex.Prev--
}
}
steps = append(steps, names)
}
// found vertexes which are never traversed
left := make([]string, 0)
for _, vertex := range graph.Vertexes {
if !vertex.Traversed {
left = append(left, vertex.RefRoot.NodeName)
}
}
// reset vertex status to traversed
graph.reset()
graph.Unlock()
return steps, left
}
func (graph *_DAG) reset() {
for _, vertex := range graph.Vertexes {
vertex.Traversed = false
for _, nextVertex := range vertex.Next {
nextVertex.Prev++
}
}
}
type _NodeRef struct {
NodeName string
NodeType string
SubRefs []*_NodeRef
Wrappers []string
ReUse bool
Virtual bool
Labels map[string]struct{}
}
const (
_WHITE = 0
_GRAY = 1
_BLACK = 2
)
// HasCycle detect if there is a circular dependency in a cluster.
func (root _NodeRef) HasCycle(color map[string]int8) bool {
// first visit
color[root.NodeName] = _GRAY
for _, ref := range root.SubRefs {
// visit the node twice, there is a circular dependency
if color[ref.NodeName] == _GRAY {
return true
}
// visit sub-node and check cycle
if color[ref.NodeName] == _WHITE && ref.HasCycle(color) {
return true
}
}
// all sub-nodes are visited
color[root.NodeName] = _BLACK
return false
}
// Print format: NodeA:[SubNodeB, SubNodeC ...]
func (root _NodeRef) Print() string {
var subs []string
for _, ref := range root.SubRefs {
if len(ref.SubRefs) == 0 {
subs = append(subs, ref.NodeName)
} else {
subs = append(subs, ref.Print())
}
}
return fmt.Sprintf("%s:[%s]", root.NodeName, strings.Join(subs, ","))
}