forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.go
158 lines (134 loc) · 3.46 KB
/
union.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
package kapacitor
import (
"log"
"time"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/pipeline"
)
type UnionNode struct {
node
u *pipeline.UnionNode
// Buffer of points/batches from each source.
sources [][]timeMessage
// the low water marks for each source.
lowMarks []time.Time
rename string
}
type timeMessage interface {
edge.Message
edge.TimeGetter
}
// Create a new UnionNode which combines all parent data streams into a single stream.
// No transformation of any kind is performed.
func newUnionNode(et *ExecutingTask, n *pipeline.UnionNode, l *log.Logger) (*UnionNode, error) {
un := &UnionNode{
u: n,
node: node{Node: n, et: et, logger: l},
rename: n.Rename,
}
un.node.runF = un.runUnion
return un, nil
}
func (n *UnionNode) runUnion([]byte) error {
// Keep buffer of values from parents so they can be ordered.
n.sources = make([][]timeMessage, len(n.ins))
n.lowMarks = make([]time.Time, len(n.ins))
consumer := edge.NewMultiConsumerWithStats(n.ins, n)
return consumer.Consume()
}
func (n *UnionNode) BufferedBatch(src int, batch edge.BufferedBatchMessage) error {
n.timer.Start()
defer n.timer.Stop()
if n.rename != "" {
batch = batch.ShallowCopy()
batch.SetBegin(batch.Begin().ShallowCopy())
batch.Begin().SetName(n.rename)
}
// Add newest point to buffer
n.sources[src] = append(n.sources[src], batch)
// Emit the next values
return n.emitReady(false)
}
func (n *UnionNode) Point(src int, p edge.PointMessage) error {
n.timer.Start()
defer n.timer.Stop()
if n.rename != "" {
p = p.ShallowCopy()
p.SetName(n.rename)
}
// Add newest point to buffer
n.sources[src] = append(n.sources[src], p)
// Emit the next values
return n.emitReady(false)
}
func (n *UnionNode) Barrier(src int, b edge.BarrierMessage) error {
n.timer.Start()
defer n.timer.Stop()
// Add newest point to buffer
n.sources[src] = append(n.sources[src], b)
// Emit the next values
return n.emitReady(false)
}
func (n *UnionNode) Finish() error {
// We are done, emit all buffered
return n.emitReady(true)
}
func (n *UnionNode) emitReady(drain bool) error {
emitted := true
// Emit all points until nothing changes
for emitted {
emitted = false
// Find low water mark
var mark time.Time
validSources := 0
for i, values := range n.sources {
sourceMark := n.lowMarks[i]
if len(values) > 0 {
t := values[0].Time()
if mark.IsZero() || t.Before(mark) {
mark = t
}
sourceMark = t
}
n.lowMarks[i] = sourceMark
if !sourceMark.IsZero() {
validSources++
// Only consider the sourceMark if we are not draining
if !drain && (mark.IsZero() || sourceMark.Before(mark)) {
mark = sourceMark
}
}
}
if !drain && validSources != len(n.sources) {
// We can't continue processing until we have
// at least one value from each parent.
// Unless we are draining the buffer than we can continue.
return nil
}
// Emit all values that are at or below the mark.
for i, values := range n.sources {
var j int
l := len(values)
for j = 0; j < l; j++ {
if !values[j].Time().After(mark) {
err := n.emit(values[j])
if err != nil {
return err
}
// Note that we emitted something
emitted = true
} else {
break
}
}
// Drop values that were emitted
n.sources[i] = values[j:]
}
}
return nil
}
func (n *UnionNode) emit(m edge.Message) error {
n.timer.Pause()
defer n.timer.Resume()
return edge.Forward(n.outs, m)
}