forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
402 lines (357 loc) · 8.63 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
package kapacitor
import (
"bytes"
"errors"
"fmt"
"log"
"math/rand"
"sync"
"time"
"github.com/influxdata/kapacitor/pipeline"
)
// The type of a task
type TaskType int
const (
StreamTask TaskType = iota
BatchTask
)
func (t TaskType) String() string {
switch t {
case StreamTask:
return "stream"
case BatchTask:
return "batch"
default:
return "unknown"
}
}
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 name, pipeline and type.
type Task struct {
Name string
Pipeline *pipeline.Pipeline
Type TaskType
DBRPs []DBRP
SnapshotInterval time.Duration
}
func (t *Task) Dot() []byte {
return t.Pipeline.Dot(t.Name)
}
// ----------------------------------
// 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
stopSnapshotter chan struct{}
wg sync.WaitGroup
logger *log.Logger
}
// Create a new task from a defined kapacitor.
func NewExecutingTask(tm *TaskMaster, t *Task) (*ExecutingTask, error) {
l := tm.LogService.NewLogger(fmt.Sprintf("[task:%s] ", t.Name), log.LstdFlags)
et := &ExecutingTask{
tm: tm,
Task: t,
outputs: make(map[string]Output),
lookup: make(map[pipeline.ID]Node),
logger: l,
}
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 {
l := et.tm.LogService.NewLogger(
fmt.Sprintf("[%s:%s] ", et.Task.Name, n.Name()),
log.LstdFlags,
)
en, err := et.createNode(n, l)
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()]
ep.linkChild(en)
}
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, 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
}
if et.Task.SnapshotInterval > 0 {
et.wg.Add(1)
et.stopSnapshotter = make(chan struct{})
go et.runSnapshotter()
}
return nil
}
func (et *ExecutingTask) stop() (err error) {
if et.Task.SnapshotInterval > 0 {
close(et.stopSnapshotter)
}
et.walk(func(n Node) error {
n.stop()
e := n.Err()
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.(*SourceBatchNode)
err := et.checkDBRPs(batcher)
if err != nil {
return err
}
batcher.Start()
return nil
}
func (et *ExecutingTask) BatchCount() (int, error) {
if et.Task.Type != BatchTask {
return 0, ErrWrongTaskType
}
batcher := et.source.(*SourceBatchNode)
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) ([][]string, error) {
if et.Task.Type != BatchTask {
return nil, ErrWrongTaskType
}
batcher := et.source.(*SourceBatchNode)
err := et.checkDBRPs(batcher)
if err != nil {
return nil, err
}
return batcher.Queries(start, stop), nil
}
// Check that the task allows access to DBRPs
func (et *ExecutingTask) checkDBRPs(batcher *SourceBatchNode) 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
}
// Wait till the task finishes and return any error
func (et *ExecutingTask) Err() error {
return et.rwalk(func(n Node) error {
return n.Err()
})
}
// 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
}
// Return a graphviz .dot formatted byte array.
// Label edges with relavant execution information.
func (et *ExecutingTask) EDot() []byte {
var buf bytes.Buffer
buf.Write([]byte("digraph "))
buf.Write([]byte(et.Task.Name))
buf.Write([]byte(" {\n"))
et.walk(func(n Node) error {
n.edot(&buf)
return nil
})
buf.Write([]byte("}"))
return buf.Bytes()
}
// Create a node from a given pipeline node.
func (et *ExecutingTask) createNode(p pipeline.Node, l *log.Logger) (Node, error) {
switch t := p.(type) {
case *pipeline.StreamNode:
return newStreamNode(et, t, l)
case *pipeline.SourceBatchNode:
return newSourceBatchNode(et, t, l)
case *pipeline.BatchNode:
return newBatchNode(et, t, l)
case *pipeline.WindowNode:
return newWindowNode(et, t, l)
case *pipeline.HTTPOutNode:
return newHTTPOutNode(et, t, l)
case *pipeline.InfluxDBOutNode:
return newInfluxDBOutNode(et, t, l)
case *pipeline.MapNode:
return newMapNode(et, t, l)
case *pipeline.ReduceNode:
return newReduceNode(et, t, l)
case *pipeline.AlertNode:
return newAlertNode(et, t, l)
case *pipeline.GroupByNode:
return newGroupByNode(et, t, l)
case *pipeline.UnionNode:
return newUnionNode(et, t, l)
case *pipeline.JoinNode:
return newJoinNode(et, t, l)
case *pipeline.EvalNode:
return newEvalNode(et, t, l)
case *pipeline.WhereNode:
return newWhereNode(et, t, l)
case *pipeline.SampleNode:
return newSampleNode(et, t, l)
case *pipeline.DerivativeNode:
return newDerivativeNode(et, t, l)
case *pipeline.UDFNode:
return newUDFNode(et, t, l)
case *pipeline.StatsNode:
return newStatsNode(et, t, l)
default:
return nil, fmt.Errorf("unknown pipeline node type %T", p)
}
}
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.stopSnapshotter:
return
}
ticker := time.NewTicker(et.Task.SnapshotInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
snapshot, err := et.Snapshot()
if err != nil {
et.logger.Println("E! failed to snapshot task", et.Task.Name, 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.Name, snapshot)
if err != nil {
et.logger.Println("E! failed to save task snapshot", et.Task.Name, err)
}
}
case <-et.stopSnapshotter:
return
}
}
}