forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
417 lines (387 loc) · 9.07 KB
/
join.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
package kapacitor
import (
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/timer"
"github.com/influxdb/influxdb/influxql"
)
type JoinNode struct {
node
j *pipeline.JoinNode
fill influxql.FillOption
fillValue interface{}
groups map[models.GroupID]*group
mu sync.RWMutex
runningGroups sync.WaitGroup
}
// Create a new JoinNode, which takes pairs from parent streams combines them into a single point.
func newJoinNode(et *ExecutingTask, n *pipeline.JoinNode, l *log.Logger) (*JoinNode, error) {
for _, name := range n.Names {
if len(name) == 0 {
return nil, fmt.Errorf("must provide a prefix name for the join node, see .as() property method")
}
if strings.ContainsRune(name, '.') {
return nil, fmt.Errorf("cannot use name %s as field prefix, it contains a '.' character", name)
}
}
names := make(map[string]bool, len(n.Names))
for _, name := range n.Names {
if names[name] {
return nil, fmt.Errorf("cannot use the same prefix name see .as() property method")
}
names[name] = true
}
jn := &JoinNode{
j: n,
node: node{Node: n, et: et, logger: l},
}
// Set fill
switch fill := n.Fill.(type) {
case string:
switch fill {
case "null":
jn.fill = influxql.NullFill
case "none":
jn.fill = influxql.NoFill
default:
return nil, fmt.Errorf("unexpected fill option %s", fill)
}
case int64, float64:
jn.fill = influxql.NumberFill
jn.fillValue = fill
default:
jn.fill = influxql.NoFill
}
jn.node.runF = jn.runJoin
return jn, nil
}
func (j *JoinNode) runJoin([]byte) error {
j.groups = make(map[models.GroupID]*group)
wg := sync.WaitGroup{}
for i := range j.ins {
// Start gorouting per parent so we do not deadlock.
// This way independent of the order that parents receive data
// we can handle it.
wg.Add(1)
go func(i int) {
defer wg.Done()
in := j.ins[i]
for p, ok := in.Next(); ok; p, ok = in.Next() {
// Get the group for this point or batch
g := j.getGroup(p)
g.points <- srcPoint{
src: i,
p: p,
}
}
}(i)
}
wg.Wait()
// No more points are comming signal all groups to finish up.
for _, group := range j.groups {
close(group.points)
}
j.runningGroups.Wait()
for _, group := range j.groups {
group.emitAll()
}
return nil
}
// safely get the group for the point or create one if it doesn't exist.
func (j *JoinNode) getGroup(p models.PointInterface) *group {
j.mu.Lock()
defer j.mu.Unlock()
group := j.groups[p.PointGroup()]
if group == nil {
group = newGroup(len(j.ins), j)
j.groups[p.PointGroup()] = group
j.runningGroups.Add(1)
go group.run()
}
return group
}
// represents an incoming data point and which parent it came from
type srcPoint struct {
src int
p models.PointInterface
}
// handles emitting joined sets once enough data has arrived from parents.
type group struct {
sets map[time.Time]*joinset
head []time.Time
oldestTime time.Time
j *JoinNode
points chan srcPoint
timer timer.Timer
}
func newGroup(i int, j *JoinNode) *group {
return &group{
sets: make(map[time.Time]*joinset),
head: make([]time.Time, i),
j: j,
points: make(chan srcPoint),
timer: j.et.tm.TimingService.NewTimer(j.avgExecVar),
}
}
// start consuming incoming points
func (g *group) run() {
defer g.j.runningGroups.Done()
for sp := range g.points {
g.timer.Start()
g.collect(sp.src, sp.p)
g.timer.Stop()
}
}
// collect a point from a given parent.
// emit the oldest set if we have collected enough data.
func (g *group) collect(i int, p models.PointInterface) {
t := p.PointTime().Round(g.j.j.Tolerance)
if t.Before(g.oldestTime) || g.oldestTime.IsZero() {
g.oldestTime = t
}
set := g.sets[t]
if set == nil {
set = newJoinset(g.j.j.StreamName, g.j.fill, g.j.fillValue, g.j.j.Names, g.j.j.Tolerance, t, g.j.logger)
g.sets[t] = set
}
set.Add(i, p)
// Update head
g.head[i] = t
// Check if all parents have been read past the oldestTime.
// If so we can emit the set.
emit := true
for _, t := range g.head {
if !t.After(g.oldestTime) {
// Still posible to get more data
// need to wait for more points
emit = false
break
}
}
if emit {
g.emit()
}
}
// emit a set and update the oldestTime.
func (g *group) emit() {
set := g.sets[g.oldestTime]
g.emitJoinedSet(set)
delete(g.sets, g.oldestTime)
g.oldestTime = time.Time{}
for t := range g.sets {
if g.oldestTime.IsZero() || t.Before(g.oldestTime) {
g.oldestTime = t
}
}
}
// emit sets until we have none left.
func (g *group) emitAll() {
for len(g.sets) > 0 {
g.emit()
}
}
// emit a single joined set
func (g *group) emitJoinedSet(set *joinset) error {
if set.name == "" {
set.name = set.First().PointName()
}
switch g.j.Wants() {
case pipeline.StreamEdge:
p, ok := set.JoinIntoPoint()
if ok {
g.timer.Pause()
for _, out := range g.j.outs {
err := out.CollectPoint(p)
if err != nil {
return err
}
}
g.timer.Resume()
}
case pipeline.BatchEdge:
b, ok := set.JoinIntoBatch()
if ok {
g.timer.Pause()
for _, out := range g.j.outs {
err := out.CollectBatch(b)
if err != nil {
return err
}
}
g.timer.Resume()
}
}
return nil
}
// represents a set of points or batches from the same joined time
type joinset struct {
name string
fill influxql.FillOption
fillValue interface{}
prefixes []string
time time.Time
tolerance time.Duration
values []models.PointInterface
expected int
size int
finished int
first int
logger *log.Logger
}
func newJoinset(
name string,
fill influxql.FillOption,
fillValue interface{},
prefixes []string,
tolerance time.Duration,
time time.Time,
l *log.Logger,
) *joinset {
expected := len(prefixes)
return &joinset{
name: name,
fill: fill,
fillValue: fillValue,
prefixes: prefixes,
expected: expected,
values: make([]models.PointInterface, expected),
first: expected,
time: time,
logger: l,
}
}
// add a point to the set from a given parent index.
func (js *joinset) Add(i int, v models.PointInterface) {
if i < js.first {
js.first = i
}
js.values[i] = v
js.size++
}
// a valid point in the set
func (js *joinset) First() models.PointInterface {
return js.values[js.first]
}
// join all points into a single point
func (js *joinset) JoinIntoPoint() (models.Point, bool) {
fields := make(models.Fields, js.size*len(js.First().PointFields()))
for i, p := range js.values {
if p == nil {
switch js.fill {
case influxql.NullFill:
for k := range js.First().PointFields() {
fields[js.prefixes[i]+"."+k] = nil
}
case influxql.NumberFill:
for k := range js.First().PointFields() {
fields[js.prefixes[i]+"."+k] = js.fillValue
}
default:
// inner join no valid point possible
return models.Point{}, false
}
} else {
for k, v := range p.PointFields() {
fields[js.prefixes[i]+"."+k] = v
}
}
}
p := models.Point{
Name: js.name,
Group: js.First().PointGroup(),
Tags: js.First().PointTags(),
Dimensions: js.First().PointDimensions(),
Time: js.time,
Fields: fields,
}
return p, true
}
// join all batches the set into a single batch
func (js *joinset) JoinIntoBatch() (models.Batch, bool) {
newBatch := models.Batch{
Name: js.name,
Group: js.First().PointGroup(),
Tags: js.First().PointTags(),
TMax: js.time,
}
empty := make([]bool, js.expected)
emptyCount := 0
indexes := make([]int, js.expected)
var fieldNames []string
for emptyCount < js.expected {
set := make([]*models.BatchPoint, js.expected)
setTime := time.Time{}
count := 0
for i, batch := range js.values {
if empty[i] {
continue
}
if batch == nil {
emptyCount++
empty[i] = true
continue
}
b := batch.(models.Batch)
if indexes[i] == len(b.Points) {
emptyCount++
empty[i] = true
continue
}
bp := b.Points[indexes[i]]
t := bp.Time.Round(js.tolerance)
if setTime.IsZero() {
setTime = t
}
if t.Equal(setTime) {
if fieldNames == nil {
for k := range bp.Fields {
fieldNames = append(fieldNames, k)
}
}
set[i] = &bp
indexes[i]++
count++
}
}
// we didn't get any points from any group we must be empty
// skip this set
if count == 0 {
continue
}
// Join all batch points in set
fields := make(models.Fields, js.expected*len(fieldNames))
for i, bp := range set {
if bp == nil {
switch js.fill {
case influxql.NullFill:
for _, k := range fieldNames {
fields[js.prefixes[i]+"."+k] = nil
}
case influxql.NumberFill:
for _, k := range fieldNames {
fields[js.prefixes[i]+"."+k] = js.fillValue
}
default:
// inner join no valid point possible
return models.Batch{}, false
}
} else {
for k, v := range bp.Fields {
fields[js.prefixes[i]+"."+k] = v
}
}
}
bp := models.BatchPoint{
Tags: newBatch.Tags,
Time: setTime,
Fields: fields,
}
newBatch.Points = append(newBatch.Points, bp)
}
return newBatch, true
}