forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
145 lines (129 loc) · 2.93 KB
/
pipeline.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
package pipeline
import (
"bytes"
"fmt"
"time"
"github.com/influxdata/kapacitor/tick"
)
// Information relavant to configuring a deadman's swith
type DeadmanService interface {
Interval() time.Duration
Threshold() float64
Id() string
Message() string
Global() bool
}
// A complete data processing pipeline. Starts with a single source.
// tick:ignore
type Pipeline struct {
sources []Node
id ID
sorted []Node
deadman DeadmanService
}
// Create a pipeline from a given script.
// tick:ignore
func CreatePipeline(script string, sourceEdge EdgeType, scope *tick.Scope, deadman DeadmanService) (*Pipeline, error) {
p := &Pipeline{
deadman: deadman,
}
var src Node
switch sourceEdge {
case StreamEdge:
src = newStreamNode()
scope.Set("stream", src)
case BatchEdge:
src = newBatchNode()
scope.Set("batch", src)
default:
return nil, fmt.Errorf("source edge type must be either Stream or Batch not %s", sourceEdge)
}
p.addSource(src)
err := tick.Evaluate(script, scope)
if err != nil {
return nil, err
}
if deadman.Global() {
switch s := src.(type) {
case *StreamNode:
s.Deadman(deadman.Threshold(), deadman.Interval())
case *BatchNode:
s.Deadman(deadman.Threshold(), deadman.Interval())
default:
return nil, fmt.Errorf("source edge type must be either Stream or Batch not %s", sourceEdge)
}
}
if err = p.Walk(
func(n Node) error {
return n.validate()
}); err != nil {
return nil, err
}
return p, nil
}
func (p *Pipeline) addSource(src Node) {
src.setPipeline(p)
p.assignID(src)
p.sources = append(p.sources, src)
}
func (p *Pipeline) assignID(n Node) error {
n.setID(p.id)
p.id++
return nil
}
// Walks the entire pipeline and calls func f on each node exactly once.
// f will be called on a node n only after all of its parents have already had f called.
// tick:ignore
func (p *Pipeline) Walk(f func(n Node) error) error {
if p.sorted == nil {
p.sort()
}
for _, n := range p.sorted {
err := f(n)
if err != nil {
return err
}
}
return nil
}
func (p *Pipeline) sort() {
// Iterate the sources in reverse order
for i := len(p.sources) - 1; i >= 0; i-- {
p.visit(p.sources[i])
}
//reverse p.sorted
s := p.sorted
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
// Depth first search topological sorting of a DAG.
// https://en.wikipedia.org/wiki/Topological_sorting#Algorithms
func (p *Pipeline) visit(n Node) {
if n.tMark() {
panic("pipeline contains a cycle")
}
if !n.pMark() {
n.setTMark(true)
for _, c := range n.Children() {
p.visit(c)
}
n.setPMark(true)
n.setTMark(false)
p.sorted = append(p.sorted, n)
}
}
// Return a graphviz .dot formatted byte array.
// tick:ignore
func (p *Pipeline) Dot(name string) []byte {
var buf bytes.Buffer
buf.Write([]byte("digraph "))
buf.Write([]byte(name))
buf.Write([]byte(" {\n"))
p.Walk(func(n Node) error {
n.dot(&buf)
return nil
})
buf.Write([]byte("}"))
return buf.Bytes()
}