-
Notifications
You must be signed in to change notification settings - Fork 493
/
flatten.go
254 lines (227 loc) · 5.27 KB
/
flatten.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
package kapacitor
import (
"bytes"
"fmt"
"sync"
"time"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
type FlattenNode struct {
node
f *pipeline.FlattenNode
bufPool sync.Pool
}
// Create a new FlattenNode, which takes pairs from parent streams combines them into a single point.
func newFlattenNode(et *ExecutingTask, n *pipeline.FlattenNode, d NodeDiagnostic) (*FlattenNode, error) {
fn := &FlattenNode{
f: n,
node: node{Node: n, et: et, diag: d},
bufPool: sync.Pool{
New: func() interface{} { return &bytes.Buffer{} },
},
}
fn.node.runF = fn.runFlatten
return fn, nil
}
func (n *FlattenNode) runFlatten([]byte) error {
consumer := edge.NewGroupedConsumer(
n.ins[0],
n,
)
n.statMap.Set(statCardinalityGauge, consumer.CardinalityVar())
return consumer.Consume()
}
func (n *FlattenNode) NewGroup(group edge.GroupInfo, first edge.PointMeta) (edge.Receiver, error) {
t := first.Time().Round(n.f.Tolerance)
return &flattenBuffer{
n: n,
time: t,
name: first.Name(),
groupInfo: group,
}, nil
}
type flattenBuffer struct {
n *FlattenNode
time time.Time
name string
groupInfo edge.GroupInfo
points []edge.FieldsTagsTimeGetter
}
func (b *flattenBuffer) BeginBatch(begin edge.BeginBatchMessage) error {
b.n.timer.Start()
defer b.n.timer.Stop()
b.name = begin.Name()
b.time = time.Time{}
if s := begin.SizeHint(); s > cap(b.points) {
b.points = make([]edge.FieldsTagsTimeGetter, 0, s)
}
begin = begin.ShallowCopy()
begin.SetSizeHint(0)
b.n.timer.Pause()
err := edge.Forward(b.n.outs, begin)
b.n.timer.Resume()
return err
}
func (b *flattenBuffer) BatchPoint(bp edge.BatchPointMessage) error {
b.n.timer.Start()
defer b.n.timer.Stop()
t := bp.Time().Round(b.n.f.Tolerance)
bp = bp.ShallowCopy()
bp.SetTime(t)
t, fields, err := b.addPoint(bp)
if err != nil {
return err
}
if len(fields) == 0 {
return nil
}
return b.emitBatchPoint(t, fields)
}
func (b *flattenBuffer) emitBatchPoint(t time.Time, fields models.Fields) error {
// Emit batch point
flatP := edge.NewBatchPointMessage(
fields,
b.groupInfo.Tags,
t,
)
b.n.timer.Pause()
err := edge.Forward(b.n.outs, flatP)
b.n.timer.Resume()
return err
}
func (b *flattenBuffer) EndBatch(end edge.EndBatchMessage) error {
b.n.timer.Start()
defer b.n.timer.Stop()
if len(b.points) > 0 {
fields, err := b.n.flatten(b.points)
if err != nil {
return err
}
if err := b.emitBatchPoint(b.time, fields); err != nil {
return err
}
b.points = b.points[0:0]
}
b.n.timer.Pause()
err := edge.Forward(b.n.outs, end)
b.n.timer.Resume()
return err
}
func (b *flattenBuffer) Point(p edge.PointMessage) error {
b.n.timer.Start()
defer b.n.timer.Stop()
t := p.Time().Round(b.n.f.Tolerance)
p = p.ShallowCopy()
p.SetTime(t)
t, fields, err := b.addPoint(p)
if err != nil {
return err
}
if len(fields) == 0 {
return nil
}
// Emit point
flatP := edge.NewPointMessage(
b.name, "", "",
b.groupInfo.Dimensions,
fields,
b.groupInfo.Tags,
t,
)
// update the time
if t.After(b.time) {
b.time = t
}
b.n.timer.Pause()
err = edge.Forward(b.n.outs, flatP)
b.n.timer.Resume()
return err
}
func (b *flattenBuffer) addPoint(p edge.FieldsTagsTimeGetter) (next time.Time, fields models.Fields, err error) {
t := p.Time()
if !t.Equal(b.time) {
if len(b.points) > 0 {
fields, err = b.n.flatten(b.points)
if err != nil {
return
}
next = b.time
b.points = b.points[0:0]
}
// Update buffer with new time
b.time = t
}
b.points = append(b.points, p)
return
}
func (b *flattenBuffer) Barrier(barrier edge.BarrierMessage) error {
b.n.timer.Start()
defer b.n.timer.Stop()
if barrier.Time().After(b.time) && len(b.points) > 0 {
fields, err := b.n.flatten(b.points)
if err != nil {
return err
}
msg := edge.NewPointMessage(
b.name, "", "",
b.groupInfo.Dimensions,
fields,
b.groupInfo.Tags,
b.time,
)
b.n.timer.Pause()
err = edge.Forward(b.n.outs, msg)
b.n.timer.Resume()
if err != nil {
return err
}
b.points = b.points[0:0]
}
b.n.timer.Pause()
err := edge.Forward(b.n.outs, barrier)
b.n.timer.Resume()
return err
}
func (b *flattenBuffer) DeleteGroup(d edge.DeleteGroupMessage) error {
b.points = b.points[0:0]
return edge.Forward(b.n.outs, d)
}
func (b *flattenBuffer) Done() {}
func (n *FlattenNode) flatten(points []edge.FieldsTagsTimeGetter) (models.Fields, error) {
fields := make(models.Fields)
if len(points) == 0 {
return fields, nil
}
fieldPrefix := n.bufPool.Get().(*bytes.Buffer)
defer n.bufPool.Put(fieldPrefix)
POINTS:
for _, p := range points {
tags := p.Tags()
for i, tag := range n.f.Dimensions {
if v, ok := tags[tag]; ok {
if i > 0 {
fieldPrefix.WriteString(n.f.Delimiter)
}
fieldPrefix.WriteString(v)
} else {
n.diag.Error("point missing tag for flatten operation", fmt.Errorf("tag %s is missing from point", tag))
continue POINTS
}
}
l := fieldPrefix.Len()
for fname, value := range p.Fields() {
if !n.f.DropOriginalFieldNameFlag {
if l > 0 {
fieldPrefix.WriteString(n.f.Delimiter)
}
fieldPrefix.WriteString(fname)
}
fields[fieldPrefix.String()] = value
fieldPrefix.Truncate(l)
}
fieldPrefix.Reset()
}
return fields, nil
}