forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
587 lines (520 loc) · 12.8 KB
/
task.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package kapacitor
import (
"bytes"
"errors"
"fmt"
"math/rand"
"sync"
"time"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/keyvalue"
"github.com/influxdata/kapacitor/pipeline"
)
type TaskDiagnostic interface {
WithNodeContext(node string) NodeDiagnostic
Error(msg string, err error, ctx ...keyvalue.T)
}
// The type of a task
type TaskType int
const (
StreamTask TaskType = iota
BatchTask
InvalidTask
)
func (t TaskType) String() string {
switch t {
case StreamTask:
return "stream"
case BatchTask:
return "batch"
default:
return "unknown"
}
}
func (t TaskType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
func (t *TaskType) UnmarshalText(text []byte) error {
switch string(text) {
case "stream":
*t = StreamTask
case "batch":
*t = BatchTask
default:
return fmt.Errorf("unknown task type %s", string(text))
}
return nil
}
type DBRP struct {
Database string `json:"db"`
RetentionPolicy string `json:"rp"`
}
func CreateDBRPMap(dbrps []DBRP) map[DBRP]bool {
dbMap := make(map[DBRP]bool, len(dbrps))
for _, dbrp := range dbrps {
dbMap[dbrp] = true
}
return dbMap
}
func (d DBRP) String() string {
return fmt.Sprintf("%q.%q", d.Database, d.RetentionPolicy)
}
// The complete definition of a task, its id, pipeline and type.
type Task struct {
ID string
Pipeline *pipeline.Pipeline
Type TaskType
DBRPs []DBRP
SnapshotInterval time.Duration
}
func (t *Task) Dot() []byte {
return t.Pipeline.Dot(t.ID)
}
// returns all the measurements from a FromNode
func (t *Task) Measurements() []string {
measurements := make([]string, 0)
_ = t.Pipeline.Walk(func(node pipeline.Node) error {
switch streamNode := node.(type) {
case *pipeline.FromNode:
measurements = append(measurements, streamNode.Measurement)
}
return nil
})
return measurements
}
// ----------------------------------
// ExecutingTask
// A task that is ready for execution.
type ExecutingTask struct {
tm *TaskMaster
Task *Task
source Node
outputs map[string]Output
// node lookup from pipeline.ID -> Node
lookup map[pipeline.ID]Node
nodes []Node
stopping chan struct{}
wg sync.WaitGroup
diag TaskDiagnostic
// Mutex for throughput var
tmu sync.RWMutex
throughput float64
}
// Create a new task from a defined kapacitor.
func NewExecutingTask(tm *TaskMaster, t *Task) (*ExecutingTask, error) {
d := tm.diag.WithTaskContext(t.ID)
et := &ExecutingTask{
tm: tm,
Task: t,
outputs: make(map[string]Output),
lookup: make(map[pipeline.ID]Node),
diag: d,
}
err := et.link()
if err != nil {
return nil, err
}
return et, nil
}
// walks the entire pipeline applying function f.
func (et *ExecutingTask) walk(f func(n Node) error) error {
for _, n := range et.nodes {
err := f(n)
if err != nil {
return err
}
}
return nil
}
// walks the entire pipeline in reverse order applying function f.
func (et *ExecutingTask) rwalk(f func(n Node) error) error {
for i := len(et.nodes) - 1; i >= 0; i-- {
err := f(et.nodes[i])
if err != nil {
return err
}
}
return nil
}
// Link all the nodes together based on the task pipeline.
func (et *ExecutingTask) link() error {
// Walk Pipeline and create equivalent executing nodes
err := et.Task.Pipeline.Walk(func(n pipeline.Node) error {
d := et.diag.WithNodeContext(n.Name())
en, err := et.createNode(n, d)
if err != nil {
return err
}
et.lookup[n.ID()] = en
// Save the walk order
et.nodes = append(et.nodes, en)
// Duplicate the Edges
for _, p := range n.Parents() {
ep := et.lookup[p.ID()]
err := ep.linkChild(en)
if err != nil {
return err
}
}
return err
})
if err != nil {
return err
}
// The first node is always the source node
et.source = et.nodes[0]
return nil
}
// Start the task.
func (et *ExecutingTask) start(ins []edge.StatsEdge, snapshot *TaskSnapshot) error {
for _, in := range ins {
et.source.addParentEdge(in)
}
validSnapshot := false
if snapshot != nil {
err := et.walk(func(n Node) error {
_, ok := snapshot.NodeSnapshots[n.Name()]
if !ok {
return fmt.Errorf("task pipeline changed not using snapshot")
}
return nil
})
validSnapshot = err == nil
}
err := et.walk(func(n Node) error {
if validSnapshot {
n.start(snapshot.NodeSnapshots[n.Name()])
} else {
n.start(nil)
}
return nil
})
if err != nil {
return err
}
et.stopping = make(chan struct{})
if et.Task.SnapshotInterval > 0 {
et.wg.Add(1)
go et.runSnapshotter()
}
// Start calcThroughput
et.wg.Add(1)
go et.calcThroughput()
return nil
}
func (et *ExecutingTask) stop() (err error) {
close(et.stopping)
_ = et.walk(func(n Node) error {
n.stop()
e := n.Wait()
if e != nil {
err = e
}
return nil
})
et.wg.Wait()
return
}
var ErrWrongTaskType = errors.New("wrong task type")
// Instruct source batch node to start querying and sending batches of data
func (et *ExecutingTask) StartBatching() error {
if et.Task.Type != BatchTask {
return ErrWrongTaskType
}
batcher := et.source.(*BatchNode)
err := et.checkDBRPs(batcher)
if err != nil {
batcher.Abort()
return err
}
batcher.Start()
return nil
}
func (et *ExecutingTask) BatchCount() (int, error) {
if et.Task.Type != BatchTask {
return 0, ErrWrongTaskType
}
batcher := et.source.(*BatchNode)
return batcher.Count(), nil
}
// Get the next `num` batch queries that the batcher will run starting at time `start`.
func (et *ExecutingTask) BatchQueries(start, stop time.Time) ([]BatchQueries, error) {
if et.Task.Type != BatchTask {
return nil, ErrWrongTaskType
}
batcher := et.source.(*BatchNode)
err := et.checkDBRPs(batcher)
if err != nil {
return nil, err
}
return batcher.Queries(start, stop)
}
// Check that the task allows access to DBRPs
func (et *ExecutingTask) checkDBRPs(batcher *BatchNode) error {
dbMap := CreateDBRPMap(et.Task.DBRPs)
dbrps, err := batcher.DBRPs()
if err != nil {
return err
}
for _, dbrp := range dbrps {
if !dbMap[dbrp] {
return fmt.Errorf("batch query is not allowed to request data from %v", dbrp)
}
}
return nil
}
// Stop all stats nodes
func (et *ExecutingTask) StopStats() {
_ = et.walk(func(n Node) error {
if s, ok := n.(*StatsNode); ok {
s.stopStats()
}
return nil
})
}
// Wait till the task finishes and return any error
func (et *ExecutingTask) Wait() error {
return et.rwalk(func(n Node) error {
return n.Wait()
})
}
// Get a named output.
func (et *ExecutingTask) GetOutput(name string) (Output, error) {
if o, ok := et.outputs[name]; ok {
return o, nil
} else {
return nil, fmt.Errorf("unknown output %s", name)
}
}
// Register a named output.
func (et *ExecutingTask) registerOutput(name string, o Output) {
et.outputs[name] = o
}
type ExecutionStats struct {
TaskStats map[string]interface{}
NodeStats map[string]map[string]interface{}
}
func (et *ExecutingTask) ExecutionStats() (ExecutionStats, error) {
executionStats := ExecutionStats{
TaskStats: make(map[string]interface{}),
NodeStats: make(map[string]map[string]interface{}),
}
// Fill the task stats
executionStats.TaskStats["throughput"] = et.getThroughput()
// Fill the nodes stats
err := et.walk(func(node Node) error {
nodeStats := node.stats()
// Add collected and emitted
nodeStats["collected"] = node.collectedCount()
nodeStats["emitted"] = node.emittedCount()
executionStats.NodeStats[node.Name()] = nodeStats
return nil
})
if err != nil {
return executionStats, err
}
return executionStats, nil
}
// Return a graphviz .dot formatted byte array.
// Label edges with relavant execution information.
func (et *ExecutingTask) EDot(labels bool) []byte {
var buf bytes.Buffer
buf.WriteString("digraph ")
buf.WriteString(et.Task.ID)
buf.WriteString(" {\n")
// Write graph attributes
unit := "points"
if et.Task.Type == BatchTask {
unit = "batches"
}
buf.WriteString("graph [")
if labels {
buf.WriteString(
fmt.Sprintf("label=\"Throughput: %0.2f %s/s\" forcelabels=true pad=\"0.8,0.5\"",
et.getThroughput(),
unit,
),
)
} else {
buf.WriteString(
fmt.Sprintf("throughput=\"%0.2f %s/s\"",
et.getThroughput(),
unit,
),
)
}
buf.WriteString("];\n")
_ = et.walk(func(n Node) error {
n.edot(&buf, labels)
return nil
})
buf.Write([]byte("}"))
return buf.Bytes()
}
// Return the current throughput value.
func (et *ExecutingTask) getThroughput() float64 {
et.tmu.RLock()
defer et.tmu.RUnlock()
return et.throughput
}
func (et *ExecutingTask) calcThroughput() {
defer et.wg.Done()
var previous int64
last := time.Now()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
current := et.source.collectedCount()
now := time.Now()
elapsed := float64(now.Sub(last)) / float64(time.Second)
et.tmu.Lock()
et.throughput = float64(current-previous) / elapsed
et.tmu.Unlock()
last = now
previous = current
case <-et.stopping:
return
}
}
}
// Create a node from a given pipeline node.
func (et *ExecutingTask) createNode(p pipeline.Node, d NodeDiagnostic) (n Node, err error) {
switch t := p.(type) {
case *pipeline.FromNode:
n, err = newFromNode(et, t, d)
case *pipeline.StreamNode:
n, err = newStreamNode(et, t, d)
case *pipeline.BatchNode:
n, err = newBatchNode(et, t, d)
case *pipeline.QueryNode:
n, err = newQueryNode(et, t, d)
case *pipeline.QueryFluxNode:
n, err = newQueryFluxNode(et, t, d)
case *pipeline.WindowNode:
n, err = newWindowNode(et, t, d)
case *pipeline.HTTPOutNode:
n, err = newHTTPOutNode(et, t, d)
case *pipeline.HTTPPostNode:
n, err = newHTTPPostNode(et, t, d)
case *pipeline.InfluxDBOutNode:
n, err = newInfluxDBOutNode(et, t, d)
case *pipeline.KapacitorLoopbackNode:
n, err = newKapacitorLoopbackNode(et, t, d)
case *pipeline.AlertNode:
n, err = newAlertNode(et, t, d)
case *pipeline.GroupByNode:
n, err = newGroupByNode(et, t, d)
case *pipeline.UnionNode:
n, err = newUnionNode(et, t, d)
case *pipeline.JoinNode:
n, err = newJoinNode(et, t, d)
case *pipeline.FlattenNode:
n, err = newFlattenNode(et, t, d)
case *pipeline.EvalNode:
n, err = newEvalNode(et, t, d)
case *pipeline.WhereNode:
n, err = newWhereNode(et, t, d)
case *pipeline.SampleNode:
n, err = newSampleNode(et, t, d)
case *pipeline.DerivativeNode:
n, err = newDerivativeNode(et, t, d)
case *pipeline.ChangeDetectNode:
n, err = newChangeDetectNode(et, t, d)
case *pipeline.UDFNode:
n, err = newUDFNode(et, t, d)
case *pipeline.StatsNode:
n, err = newStatsNode(et, t, d)
case *pipeline.ShiftNode:
n, err = newShiftNode(et, t, d)
case *pipeline.NoOpNode:
n, err = newNoOpNode(et, t, d)
case *pipeline.InfluxQLNode:
n, err = newInfluxQLNode(et, t, d)
case *pipeline.LogNode:
n, err = newLogNode(et, t, d)
case *pipeline.DefaultNode:
n, err = newDefaultNode(et, t, d)
case *pipeline.DeleteNode:
n, err = newDeleteNode(et, t, d)
case *pipeline.CombineNode:
n, err = newCombineNode(et, t, d)
case *pipeline.K8sAutoscaleNode:
n, err = newK8sAutoscaleNode(et, t, d)
case *pipeline.SwarmAutoscaleNode:
n, err = newSwarmAutoscaleNode(et, t, d)
case *pipeline.Ec2AutoscaleNode:
n, err = newEc2AutoscaleNode(et, t, d)
case *pipeline.StateDurationNode:
n, err = newStateDurationNode(et, t, d)
case *pipeline.StateCountNode:
n, err = newStateCountNode(et, t, d)
case *pipeline.SideloadNode:
n, err = newSideloadNode(et, t, d)
case *pipeline.TrickleNode:
n = newTrickleNode(et, t, d)
case *pipeline.BarrierNode:
n, err = newBarrierNode(et, t, d)
default:
return nil, fmt.Errorf("unknown pipeline node type %T", p)
}
if err == nil && n != nil {
n.init(p.IsQuiet())
}
return n, err
}
type TaskSnapshot struct {
NodeSnapshots map[string][]byte
}
func (et *ExecutingTask) Snapshot() (*TaskSnapshot, error) {
snapshot := &TaskSnapshot{
NodeSnapshots: make(map[string][]byte),
}
err := et.walk(func(n Node) error {
data, err := n.snapshot()
if err != nil {
return err
}
snapshot.NodeSnapshots[n.Name()] = data
return nil
})
if err != nil {
return nil, err
}
return snapshot, nil
}
func (et *ExecutingTask) runSnapshotter() {
defer et.wg.Done()
// Wait random duration to splay snapshot events across interval
select {
case <-time.After(time.Duration(rand.Float64() * float64(et.Task.SnapshotInterval))):
case <-et.stopping:
return
}
ticker := time.NewTicker(et.Task.SnapshotInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
snapshot, err := et.Snapshot()
if err != nil {
et.diag.Error("failed to snapshot task", err)
break
}
size := 0
for _, data := range snapshot.NodeSnapshots {
size += len(data)
}
// Only save the snapshot if it has content
if size > 0 {
err = et.tm.TaskStore.SaveSnapshot(et.Task.ID, snapshot)
if err != nil {
et.diag.Error("failed to save task snapshot", err)
}
}
case <-et.stopping:
return
}
}
}