forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
343 lines (295 loc) · 7.2 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
package kapacitor
import (
"bytes"
"errors"
"fmt"
"log"
"time"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick"
)
// 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
}
func NewTask(name, script string, tt TaskType, dbrps []DBRP) (*Task, error) {
t := &Task{
Name: name,
Type: tt,
DBRPs: dbrps,
}
var srcEdge pipeline.EdgeType
switch tt {
case StreamTask:
srcEdge = pipeline.StreamEdge
case BatchTask:
srcEdge = pipeline.BatchEdge
}
scope := tick.NewScope()
scope.Set("influxql", newInfluxQL())
scope.Set("time", func(d time.Duration) time.Duration { return d })
p, err := pipeline.CreatePipeline(script, srcEdge, scope)
if err != nil {
return nil, err
}
t.Pipeline = p
return t, nil
}
func (t *Task) Dot() []byte {
return t.Pipeline.Dot(t.Name)
}
// Create a new streamer task from a script.
func NewStreamer(name, script string, dbrps []DBRP) (*Task, error) {
return NewTask(name, script, StreamTask, dbrps)
}
// Create a new batcher task from a script.
func NewBatcher(name, script string, dbrps []DBRP) (*Task, error) {
return NewTask(name, script, BatchTask, dbrps)
}
// ----------------------------------
// 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
}
// Create a new task from a defined kapacitor.
func NewExecutingTask(tm *TaskMaster, t *Task) (*ExecutingTask, error) {
et := &ExecutingTask{
tm: tm,
Task: t,
outputs: make(map[string]Output),
lookup: make(map[pipeline.ID]Node),
}
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 {
en, err := et.createNode(n)
if err != nil {
return err
}
en.setLogger(et.tm.LogService.NewLogger(
fmt.Sprintf("[%s:%s] ", et.Task.Name, en.Name()),
log.LstdFlags,
))
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) error {
for _, in := range ins {
et.source.addParentEdge(in)
}
return et.walk(func(n Node) error {
n.start()
return nil
})
}
func (et *ExecutingTask) stop() (err error) {
et.walk(func(n Node) error {
n.stop()
e := n.Err()
if e != nil {
err = e
}
return nil
})
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) (Node, error) {
switch t := p.(type) {
case *pipeline.StreamNode:
return newStreamNode(et, t)
case *pipeline.SourceBatchNode:
return newSourceBatchNode(et, t)
case *pipeline.BatchNode:
return newBatchNode(et, t)
case *pipeline.WindowNode:
return newWindowNode(et, t)
case *pipeline.HTTPOutNode:
return newHTTPOutNode(et, t)
case *pipeline.InfluxDBOutNode:
return newInfluxDBOutNode(et, t)
case *pipeline.MapNode:
return newMapNode(et, t)
case *pipeline.ReduceNode:
return newReduceNode(et, t)
case *pipeline.AlertNode:
return newAlertNode(et, t)
case *pipeline.GroupByNode:
return newGroupByNode(et, t)
case *pipeline.UnionNode:
return newUnionNode(et, t)
case *pipeline.JoinNode:
return newJoinNode(et, t)
case *pipeline.EvalNode:
return newApplyNode(et, t)
case *pipeline.WhereNode:
return newWhereNode(et, t)
case *pipeline.SampleNode:
return newSampleNode(et, t)
case *pipeline.DerivativeNode:
return newDerivativeNode(et, t)
default:
return nil, fmt.Errorf("unknown pipeline node type %T", p)
}
}