forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
influxql.go
301 lines (265 loc) · 7.46 KB
/
influxql.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
package kapacitor
import (
"fmt"
"reflect"
"time"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/pkg/errors"
)
// tmpl -- go get github.com/benbjohnson/tmpl
//go:generate tmpl [email protected] influxql.gen.go.tmpl
type createReduceContextFunc func(c baseReduceContext) reduceContext
type InfluxQLNode struct {
node
n *pipeline.InfluxQLNode
createFn createReduceContextFunc
isStreamTransformation bool
currentKind reflect.Kind
}
func newInfluxQLNode(et *ExecutingTask, n *pipeline.InfluxQLNode, d NodeDiagnostic) (*InfluxQLNode, error) {
m := &InfluxQLNode{
node: node{Node: n, et: et, diag: d},
n: n,
isStreamTransformation: n.ReduceCreater.IsStreamTransformation,
}
m.node.runF = m.runInfluxQL
return m, nil
}
type reduceContext interface {
AggregatePoint(name string, p edge.FieldsTagsTimeGetter) error
EmitPoint() (edge.PointMessage, error)
EmitBatch() edge.BufferedBatchMessage
}
type baseReduceContext struct {
as string
field string
name string
groupInfo edge.GroupInfo
time time.Time
pointTimes bool
topBottomInfo *pipeline.TopBottomCallInfo
}
func (n *InfluxQLNode) runInfluxQL([]byte) error {
consumer := edge.NewGroupedConsumer(
n.ins[0],
n,
)
n.statMap.Set(statCardinalityGauge, consumer.CardinalityVar())
return consumer.Consume()
}
func (n *InfluxQLNode) NewGroup(group edge.GroupInfo, first edge.PointMeta) (edge.Receiver, error) {
return edge.NewReceiverFromForwardReceiverWithStats(
n.outs,
edge.NewTimedForwardReceiver(n.timer, n.newGroup(first)),
), nil
}
func (n *InfluxQLNode) newGroup(first edge.PointMeta) edge.ForwardReceiver {
bc := baseReduceContext{
as: n.n.As,
field: n.n.Field,
name: first.Name(),
groupInfo: first.GroupInfo(),
time: first.Time(),
pointTimes: n.n.PointTimes || n.isStreamTransformation,
}
g := influxqlGroup{
n: n,
bc: bc,
}
if n.isStreamTransformation {
return &influxqlStreamingTransformGroup{
influxqlGroup: g,
}
}
return &g
}
type influxqlGroup struct {
n *InfluxQLNode
bc baseReduceContext
rc reduceContext
batchSize int
name string
begin edge.BeginBatchMessage
}
func (g *influxqlGroup) BeginBatch(begin edge.BeginBatchMessage) (edge.Message, error) {
g.begin = begin
g.batchSize = 0
g.bc.time = begin.Time()
g.rc = nil
return nil, nil
}
func (g *influxqlGroup) BatchPoint(bp edge.BatchPointMessage) (edge.Message, error) {
if g.rc == nil {
if err := g.realizeReduceContextFromFields(bp.Fields()); err != nil {
g.n.diag.Error("failed to realize reduce context from fields", err)
return nil, nil
}
}
g.batchSize++
if err := g.rc.AggregatePoint(g.begin.Name(), bp); err != nil {
g.n.diag.Error("failed to aggregate point in batch", err)
}
return nil, nil
}
func (g *influxqlGroup) EndBatch(end edge.EndBatchMessage) (edge.Message, error) {
if g.batchSize == 0 && !g.n.n.ReduceCreater.IsEmptyOK {
// Do not call Emit on the reducer since it can't handle empty batches.
return nil, nil
}
if g.rc == nil {
// Assume float64 type since we do not have any data.
if err := g.realizeReduceContext(reflect.Float64); err != nil {
return nil, err
}
}
m, err := g.n.emit(g.rc)
if err != nil {
g.n.diag.Error("failed to emit batch", err)
return nil, nil
}
return m, nil
}
func (g *influxqlGroup) Point(p edge.PointMessage) (edge.Message, error) {
if p.Time().Equal(g.bc.time) {
g.aggregatePoint(p)
} else {
// Time has elapsed, emit current context
var msg edge.Message
if g.rc != nil {
m, err := g.n.emit(g.rc)
if err != nil {
g.n.diag.Error("failed to emit stream", err)
}
msg = m
}
// Reset context
g.bc.name = p.Name()
g.bc.time = p.Time()
g.rc = nil
// Aggregate the current point
g.aggregatePoint(p)
return msg, nil
}
return nil, nil
}
func (g *influxqlGroup) aggregatePoint(p edge.PointMessage) {
if g.rc == nil {
if err := g.realizeReduceContextFromFields(p.Fields()); err != nil {
g.n.diag.Error("failed to realize reduce context from fields", err)
return
}
}
err := g.rc.AggregatePoint(p.Name(), p)
if err != nil {
g.n.diag.Error("failed to aggregate point", err)
}
}
func (g *influxqlGroup) getFieldKind(fields models.Fields) (reflect.Kind, error) {
f, exists := fields[g.bc.field]
if !exists {
return reflect.Invalid, fmt.Errorf("field %q missing from point", g.bc.field)
}
return reflect.TypeOf(f).Kind(), nil
}
func (g *influxqlGroup) realizeReduceContextFromFields(fields models.Fields) error {
k, err := g.getFieldKind(fields)
if err != nil {
return err
}
return g.realizeReduceContext(k)
}
func (g *influxqlGroup) realizeReduceContext(kind reflect.Kind) error {
createFn, err := g.n.getCreateFn(kind)
if err != nil {
return err
}
g.rc = createFn(g.bc)
return nil
}
func (g *influxqlGroup) Barrier(b edge.BarrierMessage) (edge.Message, error) {
return b, nil
}
func (g *influxqlGroup) DeleteGroup(d edge.DeleteGroupMessage) (edge.Message, error) {
return d, nil
}
func (g *influxqlGroup) Done() {}
type influxqlStreamingTransformGroup struct {
influxqlGroup
}
func (g *influxqlStreamingTransformGroup) BeginBatch(begin edge.BeginBatchMessage) (edge.Message, error) {
g.begin = begin.ShallowCopy()
g.begin.SetSizeHint(0)
g.bc.time = begin.Time()
g.rc = nil
return begin, nil
}
func (g *influxqlStreamingTransformGroup) BatchPoint(bp edge.BatchPointMessage) (edge.Message, error) {
if g.rc == nil {
if err := g.realizeReduceContextFromFields(bp.Fields()); err != nil {
g.n.diag.Error("failed to realize reduce context from fields", err)
return nil, nil
}
}
if err := g.rc.AggregatePoint(g.begin.Name(), bp); err != nil {
g.n.diag.Error("failed to aggregate batch point", err)
}
if ep, err := g.rc.EmitPoint(); err != nil {
g.n.diag.Error("failed to emit batch point", err)
} else if ep != nil {
return edge.NewBatchPointMessage(
ep.Fields(),
ep.Tags(),
ep.Time(),
), nil
}
return nil, nil
}
func (g *influxqlStreamingTransformGroup) EndBatch(end edge.EndBatchMessage) (edge.Message, error) {
return end, nil
}
func (g *influxqlStreamingTransformGroup) Point(p edge.PointMessage) (edge.Message, error) {
if g.rc == nil {
if err := g.realizeReduceContextFromFields(p.Fields()); err != nil {
g.n.diag.Error("failed to realize reduce context from fields", err)
// Skip point
return nil, nil
}
}
err := g.rc.AggregatePoint(p.Name(), p)
if err != nil {
g.n.diag.Error("failed to aggregate point", err)
}
m, err := g.n.emit(g.rc)
if err != nil {
g.n.diag.Error("failed to emit stream", err)
return nil, nil
}
return m, nil
}
func (g *influxqlStreamingTransformGroup) Barrier(b edge.BarrierMessage) (edge.Message, error) {
return b, nil
}
func (n *InfluxQLNode) getCreateFn(kind reflect.Kind) (createReduceContextFunc, error) {
changed := n.currentKind != kind
if !changed && n.createFn != nil {
return n.createFn, nil
}
n.currentKind = kind
createFn, err := determineReduceContextCreateFn(n.n.Method, kind, n.n.ReduceCreater)
if err != nil {
return nil, errors.Wrapf(err, "invalid influxql func %s with field %s", n.n.Method, n.n.Field)
}
n.createFn = createFn
return n.createFn, nil
}
func (n *InfluxQLNode) emit(context reduceContext) (edge.Message, error) {
switch n.Provides() {
case pipeline.StreamEdge:
return context.EmitPoint()
case pipeline.BatchEdge:
return context.EmitBatch(), nil
}
return nil, nil
}