-
Notifications
You must be signed in to change notification settings - Fork 7
/
topology.go
262 lines (211 loc) · 5.11 KB
/
topology.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
package streams
// Node represents a topology node.
type Node interface {
// Name gets the node name.
Name() string
// AddChild adds a child node to the node.
AddChild(n Node)
// Children gets the nodes children.
Children() []Node
// Processor gets the nodes processor.
Processor() Processor
}
var _ = (Node)(&SourceNode{})
// SourceNode represents a node between the source
// and the rest of the node tree.
type SourceNode struct {
name string
children []Node
}
// NewSourceNode create a new SourceNode.
func NewSourceNode(name string) *SourceNode {
return &SourceNode{
name: name,
}
}
// Name gets the node name.
func (n *SourceNode) Name() string {
return n.name
}
// AddChild adds a child node to the node.
func (n *SourceNode) AddChild(node Node) {
n.children = append(n.children, node)
}
// Children gets the nodes children.
func (n *SourceNode) Children() []Node {
return n.children
}
// Processor gets the nodes processor.
func (n *SourceNode) Processor() Processor {
return nil
}
var _ = (Node)(&ProcessorNode{})
// ProcessorNode represents the topology node for a processor.
type ProcessorNode struct {
name string
processor Processor
children []Node
}
// NewProcessorNode creates a new ProcessorNode.
func NewProcessorNode(name string, p Processor) *ProcessorNode {
return &ProcessorNode{
name: name,
processor: p,
}
}
// Name gets the node name.
func (n *ProcessorNode) Name() string {
return n.name
}
// AddChild adds a child node to the node.
func (n *ProcessorNode) AddChild(node Node) {
n.children = append(n.children, node)
}
// Children gets the nodes children.
func (n *ProcessorNode) Children() []Node {
return n.children
}
// Processor gets the nodes processor.
func (n *ProcessorNode) Processor() Processor {
return n.processor
}
// Topology represents the streams topology.
type Topology struct {
sources map[Source]Node
processors []Node
}
// Sources get the topology Sources.
func (t Topology) Sources() map[Source]Node {
return t.sources
}
// Processors gets the topology Processors.
func (t Topology) Processors() []Node {
return t.processors
}
// TopologyBuilder represents a topology builder.
type TopologyBuilder struct {
inspections []inspection
sources map[Source]Node
processors []Node
}
// NewTopologyBuilder creates a new TopologyBuilder.
func NewTopologyBuilder() *TopologyBuilder {
inspections := []inspection{
sourcesConnected,
committersConnected,
committerIsLeafNode,
}
return &TopologyBuilder{
inspections: inspections,
sources: map[Source]Node{},
processors: []Node{},
}
}
// AddSource adds a Source to the builder, returning the created Node.
func (tb *TopologyBuilder) AddSource(name string, source Source) Node {
n := NewSourceNode(name)
tb.sources[source] = n
return n
}
// AddProcessor adds a Processor to the builder, returning the created Node.
func (tb *TopologyBuilder) AddProcessor(name string, processor Processor, parents []Node) Node {
n := NewProcessorNode(name, processor)
for _, parent := range parents {
parent.AddChild(n)
}
tb.processors = append(tb.processors, n)
return n
}
// Build creates an immutable Topology.
func (tb *TopologyBuilder) Build() (*Topology, []error) {
var errs []error
for _, inspection := range tb.inspections {
if err := inspection(tb.sources, tb.processors); err != nil {
errs = append(errs, err)
}
}
return &Topology{
sources: tb.sources,
processors: tb.processors,
}, errs
}
func nodesConnected(roots []Node) bool {
if len(roots) <= 1 {
return true
}
var nodes []Node
var visit []Node
connections := 0
visit = append(visit, roots...)
for len(visit) > 0 {
var n Node
n, visit = visit[0], visit[1:]
nodes = append(nodes, n)
for _, c := range n.Children() {
if contains(c, visit) || contains(c, nodes) {
connections++
continue
}
visit = append(visit, c)
}
}
return connections == len(roots)-1
}
func flattenNodeTree(roots map[Source]Node) []Node {
var nodes []Node
var visit []Node
for _, node := range roots {
visit = append(visit, node)
}
for len(visit) > 0 {
var n Node
n, visit = visit[0], visit[1:]
if n.Processor() != nil {
nodes = append(nodes, n)
}
for _, c := range n.Children() {
if contains(c, visit) || contains(c, nodes) {
continue
}
visit = append(visit, c)
}
}
// In asymmetric trees, our dependencies can be out of order,
// which will cause errors. In order to ratify this, we check
// that not dependency appears higher in the list than us.
for i := 0; i < len(nodes); i++ {
node := nodes[i]
for _, child := range node.Children() {
pos := indexOf(child, nodes)
if pos < i {
temp := nodes[pos]
nodes[pos] = nodes[i]
nodes[i] = temp
i = pos
}
}
}
return nodes
}
func reverseNodes(nodes []Node) {
for i := len(nodes)/2 - 1; i >= 0; i-- {
opp := len(nodes) - 1 - i
nodes[i], nodes[opp] = nodes[opp], nodes[i]
}
}
func contains(n Node, nodes []Node) bool {
for _, node := range nodes {
if node == n {
return true
}
}
return false
}
func indexOf(n Node, nodes []Node) int {
for i, node := range nodes {
if node == n {
return i
}
}
return -1
}