-
Notifications
You must be signed in to change notification settings - Fork 493
/
eval.go
221 lines (209 loc) · 5.67 KB
/
eval.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
package kapacitor
import (
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick/ast"
"github.com/influxdata/kapacitor/tick/stateful"
)
type EvalNode struct {
node
e *pipeline.EvalNode
expressions []stateful.Expression
expressionsByGroup map[models.GroupID][]stateful.Expression
refVarList [][]string
scopePool stateful.ScopePool
tags map[string]bool
expressionsByGroupMu sync.RWMutex
evalErrors *expvar.Int
}
// Create a new EvalNode which applies a transformation func to each point in a stream and returns a single point.
func newEvalNode(et *ExecutingTask, n *pipeline.EvalNode, l *log.Logger) (*EvalNode, error) {
if len(n.AsList) != len(n.Lambdas) {
return nil, errors.New("must provide one name per expression via the 'As' property")
}
en := &EvalNode{
node: node{Node: n, et: et, logger: l},
e: n,
expressionsByGroup: make(map[models.GroupID][]stateful.Expression),
}
// Create stateful expressions
en.expressions = make([]stateful.Expression, len(n.Lambdas))
en.refVarList = make([][]string, len(n.Lambdas))
expressions := make([]ast.Node, len(n.Lambdas))
for i, lambda := range n.Lambdas {
expressions[i] = lambda.Expression
statefulExpr, err := stateful.NewExpression(lambda.Expression)
if err != nil {
return nil, fmt.Errorf("Failed to compile %v expression: %v", i, err)
}
en.expressions[i] = statefulExpr
refVars := ast.FindReferenceVariables(lambda.Expression)
en.refVarList[i] = refVars
}
// Create a single pool for the combination of all expressions
en.scopePool = stateful.NewScopePool(ast.FindReferenceVariables(expressions...))
// Create map of tags
if l := len(n.TagsList); l > 0 {
en.tags = make(map[string]bool, l)
for _, tag := range n.TagsList {
en.tags[tag] = true
}
}
en.node.runF = en.runEval
return en, nil
}
func (e *EvalNode) runEval(snapshot []byte) error {
valueF := func() int64 {
e.expressionsByGroupMu.RLock()
l := len(e.expressionsByGroup)
e.expressionsByGroupMu.RUnlock()
return int64(l)
}
e.statMap.Set(statCardinalityGauge, expvar.NewIntFuncGauge(valueF))
switch e.Provides() {
case pipeline.StreamEdge:
var err error
for p, ok := e.ins[0].NextPoint(); ok; p, ok = e.ins[0].NextPoint() {
e.timer.Start()
p.Fields, p.Tags, err = e.eval(p.Time, p.Group, p.Fields, p.Tags)
if err != nil {
e.incrementErrorCount()
if !e.e.QuietFlag {
e.logger.Println("E!", err)
}
e.timer.Stop()
// Skip bad point
continue
}
e.timer.Stop()
for _, child := range e.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
}
case pipeline.BatchEdge:
var err error
for b, ok := e.ins[0].NextBatch(); ok; b, ok = e.ins[0].NextBatch() {
e.timer.Start()
for i := 0; i < len(b.Points); {
p := b.Points[i]
b.Points[i].Fields, b.Points[i].Tags, err = e.eval(p.Time, b.Group, p.Fields, p.Tags)
if err != nil {
e.incrementErrorCount()
if !e.e.QuietFlag {
e.logger.Println("E!", err)
}
// Remove bad point
b.Points = append(b.Points[:i], b.Points[i+1:]...)
} else {
i++
}
}
e.timer.Stop()
for _, child := range e.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
return nil
}
func (e *EvalNode) eval(now time.Time, group models.GroupID, fields models.Fields, tags models.Tags) (models.Fields, models.Tags, error) {
vars := e.scopePool.Get()
defer e.scopePool.Put(vars)
e.expressionsByGroupMu.RLock()
expressions, ok := e.expressionsByGroup[group]
e.expressionsByGroupMu.RUnlock()
if !ok {
expressions = make([]stateful.Expression, len(e.expressions))
for i, exp := range e.expressions {
expressions[i] = exp.CopyReset()
}
e.expressionsByGroupMu.Lock()
e.expressionsByGroup[group] = expressions
e.expressionsByGroupMu.Unlock()
}
for i, expr := range expressions {
err := fillScope(vars, e.refVarList[i], now, fields, tags)
if err != nil {
return nil, nil, err
}
v, err := expr.Eval(vars)
if err != nil {
return nil, nil, err
}
name := e.e.AsList[i]
vars.Set(name, v)
}
newTags := tags
if len(e.tags) > 0 {
newTags = newTags.Copy()
for tag := range e.tags {
v, err := vars.Get(tag)
if err != nil {
return nil, nil, err
}
if s, ok := v.(string); !ok {
return nil, nil, fmt.Errorf("result of a tag expression must be of type string, got %T", v)
} else {
newTags[tag] = s
}
}
}
var newFields models.Fields
if e.e.KeepFlag {
if l := len(e.e.KeepList); l != 0 {
newFields = make(models.Fields, l)
for _, f := range e.e.KeepList {
// Try the vars scope first
if vars.Has(f) {
v, err := vars.Get(f)
if err != nil {
return nil, nil, err
}
newFields[f] = v
} else if v, ok := fields[f]; ok {
// Try the raw fields next, since it may not have been a referenced var.
newFields[f] = v
} else {
return nil, nil, fmt.Errorf("cannot keep field %q, field does not exist", f)
}
}
} else {
newFields = make(models.Fields, len(fields)+len(e.e.AsList))
for f, v := range fields {
newFields[f] = v
}
for _, f := range e.e.AsList {
v, err := vars.Get(f)
if err != nil {
return nil, nil, err
}
newFields[f] = v
}
}
} else {
newFields = make(models.Fields, len(e.e.AsList)-len(e.tags))
for _, f := range e.e.AsList {
if e.tags[f] {
continue
}
v, err := vars.Get(f)
if err != nil {
return nil, nil, err
}
newFields[f] = v
}
}
return newFields, newTags, nil
}