-
Notifications
You must be signed in to change notification settings - Fork 493
/
barrier.go
323 lines (288 loc) · 7.1 KB
/
barrier.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package kapacitor
import (
"errors"
"time"
"sync"
"sync/atomic"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
type BarrierNode struct {
node
b *pipeline.BarrierNode
barrierStopper map[models.GroupID]func()
}
// Create a new BarrierNode, which emits a barrier if data traffic has been idle for the configured amount of time.
func newBarrierNode(et *ExecutingTask, n *pipeline.BarrierNode, d NodeDiagnostic) (*BarrierNode, error) {
if n.Idle == 0 && n.Period == 0 {
return nil, errors.New("barrier node must have either a non zero idle or a non zero period")
}
bn := &BarrierNode{
node: node{Node: n, et: et, diag: d},
b: n,
barrierStopper: map[models.GroupID]func(){},
}
bn.node.runF = bn.runBarrierEmitter
return bn, nil
}
func (n *BarrierNode) runBarrierEmitter([]byte) error {
defer n.stopBarrierEmitter()
consumer := edge.NewGroupedConsumer(n.ins[0], n)
n.statMap.Set(statCardinalityGauge, consumer.CardinalityVar())
return consumer.Consume()
}
func (n *BarrierNode) stopBarrierEmitter() {
for _, stopF := range n.barrierStopper {
stopF()
}
}
func (n *BarrierNode) NewGroup(group edge.GroupInfo, first edge.PointMeta) (edge.Receiver, error) {
r, stopF, err := n.newBarrier(group, first)
if err != nil {
return nil, err
}
n.barrierStopper[group.ID] = stopF
return edge.NewReceiverFromForwardReceiverWithStats(
n.outs,
edge.NewTimedForwardReceiver(n.timer, r),
), nil
}
func (n *BarrierNode) newBarrier(group edge.GroupInfo, first edge.PointMeta) (edge.ForwardReceiver, func(), error) {
switch {
case n.b.Idle != 0:
idleBarrier := newIdleBarrier(
first.Name(),
group,
n.ins[0],
n.b.Idle,
n.outs,
n.b.Delete,
)
return idleBarrier, idleBarrier.Stop, nil
case n.b.Period != 0:
periodicBarrier := newPeriodicBarrier(
first.Name(),
group,
n.ins[0],
n.b.Period,
n.outs,
n.b.Delete,
)
return periodicBarrier, periodicBarrier.Stop, nil
default:
return nil, nil, errors.New("unreachable code, barrier node should have non-zero idle or non-zero period")
}
}
type idleBarrier struct {
name string
group edge.GroupInfo
in edge.Edge
del bool
idle time.Duration
lastPointT atomic.Value
lastBarrierT atomic.Value
wg sync.WaitGroup
outs []edge.StatsEdge
stopC chan struct{}
resetTimerC chan struct{}
}
func newIdleBarrier(name string, group edge.GroupInfo, in edge.Edge, idle time.Duration, outs []edge.StatsEdge, del bool) *idleBarrier {
r := &idleBarrier{
name: name,
group: group,
in: in,
idle: idle,
lastPointT: atomic.Value{},
lastBarrierT: atomic.Value{},
wg: sync.WaitGroup{},
outs: outs,
stopC: make(chan struct{}),
resetTimerC: make(chan struct{}),
del: del,
}
r.Init()
return r
}
func (n *idleBarrier) Init() {
n.lastPointT.Store(time.Now().UTC())
n.lastBarrierT.Store(time.Time{})
n.wg.Add(1)
go n.idleHandler()
}
func (n *idleBarrier) Stop() {
select {
case <-n.stopC:
default:
close(n.stopC)
n.wg.Wait()
}
}
func (n *idleBarrier) BeginBatch(m edge.BeginBatchMessage) (edge.Message, error) {
return m, nil
}
func (n *idleBarrier) BatchPoint(m edge.BatchPointMessage) (edge.Message, error) {
if !m.Time().Before(n.lastBarrierT.Load().(time.Time)) {
n.resetTimer()
n.lastPointT.Store(m.Time())
return m, nil
}
return nil, nil
}
func (n *idleBarrier) EndBatch(m edge.EndBatchMessage) (edge.Message, error) {
return m, nil
}
func (n *idleBarrier) Barrier(m edge.BarrierMessage) (edge.Message, error) {
if !m.Time().Before(n.lastBarrierT.Load().(time.Time)) {
n.resetTimer()
n.lastPointT.Store(m.Time())
n.lastBarrierT.Store(m.Time())
return m, nil
}
return nil, nil
}
func (n *idleBarrier) DeleteGroup(m edge.DeleteGroupMessage) (edge.Message, error) {
if m.GroupID() == n.group.ID {
n.Stop()
}
return m, nil
}
func (n *idleBarrier) Done() {}
func (n *idleBarrier) Point(m edge.PointMessage) (edge.Message, error) {
if !m.Time().Before(n.lastBarrierT.Load().(time.Time)) {
n.resetTimer()
n.lastPointT.Store(m.Time())
return m, nil
}
return nil, nil
}
func (n *idleBarrier) resetTimer() {
n.resetTimerC <- struct{}{}
}
func (n *idleBarrier) emitBarrier() error {
newT := n.lastPointT.Load().(time.Time).Add(n.idle)
n.lastPointT.Store(newT)
n.lastBarrierT.Store(newT)
err := edge.Forward(n.outs, edge.NewBarrierMessage(n.group, newT))
if err != nil {
return err
}
if n.del {
return n.in.Collect(edge.NewDeleteGroupMessage(n.group.ID))
}
return nil
}
func (n *idleBarrier) idleHandler() {
defer n.wg.Done()
idleTimer := time.NewTimer(n.idle)
for {
select {
case <-n.resetTimerC:
if !idleTimer.Stop() {
<-idleTimer.C
}
idleTimer.Reset(n.idle)
case <-idleTimer.C:
n.emitBarrier()
idleTimer.Reset(n.idle)
case <-n.stopC:
idleTimer.Stop()
return
}
}
}
type periodicBarrier struct {
name string
group edge.GroupInfo
in edge.Edge
del bool
lastT atomic.Value
ticker *time.Ticker
wg sync.WaitGroup
outs []edge.StatsEdge
stopC chan struct{}
}
func newPeriodicBarrier(name string, group edge.GroupInfo, in edge.Edge, period time.Duration, outs []edge.StatsEdge, del bool) *periodicBarrier {
r := &periodicBarrier{
name: name,
group: group,
in: in,
lastT: atomic.Value{},
ticker: time.NewTicker(period),
wg: sync.WaitGroup{},
outs: outs,
stopC: make(chan struct{}),
del: del,
}
r.Init()
return r
}
func (n *periodicBarrier) Init() {
n.lastT.Store(time.Time{})
n.wg.Add(1)
go n.periodicEmitter()
}
func (n *periodicBarrier) Stop() {
select {
case <-n.stopC:
default:
close(n.stopC)
n.ticker.Stop()
n.wg.Wait()
}
}
func (n *periodicBarrier) BeginBatch(m edge.BeginBatchMessage) (edge.Message, error) {
return m, nil
}
func (n *periodicBarrier) BatchPoint(m edge.BatchPointMessage) (edge.Message, error) {
if !m.Time().Before(n.lastT.Load().(time.Time)) {
return m, nil
}
return nil, nil
}
func (n *periodicBarrier) EndBatch(m edge.EndBatchMessage) (edge.Message, error) {
return m, nil
}
func (n *periodicBarrier) Barrier(m edge.BarrierMessage) (edge.Message, error) {
if !m.Time().Before(n.lastT.Load().(time.Time)) {
return m, nil
}
return nil, nil
}
func (n *periodicBarrier) DeleteGroup(m edge.DeleteGroupMessage) (edge.Message, error) {
if m.GroupID() == n.group.ID {
n.Stop()
}
return m, nil
}
func (n *periodicBarrier) Done() {}
func (n *periodicBarrier) Point(m edge.PointMessage) (edge.Message, error) {
if !m.Time().Before(n.lastT.Load().(time.Time)) {
return m, nil
}
return nil, nil
}
func (n *periodicBarrier) emitBarrier() error {
nowT := time.Now().UTC()
n.lastT.Store(nowT)
err := edge.Forward(n.outs, edge.NewBarrierMessage(n.group, nowT))
if err != nil {
return err
}
if n.del {
// Send DeleteGroupMessage into self
return n.in.Collect(edge.NewDeleteGroupMessage(n.group.ID))
}
return nil
}
func (n *periodicBarrier) periodicEmitter() {
defer n.wg.Done()
for {
select {
case <-n.ticker.C:
n.emitBarrier()
case <-n.stopC:
return
}
}
}