-
Notifications
You must be signed in to change notification settings - Fork 491
/
group_by.go
206 lines (178 loc) · 4.9 KB
/
group_by.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
package kapacitor
import (
"sort"
"sync"
"time"
"github.com/influxdata/kapacitor/edge"
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick/ast"
)
type GroupByNode struct {
node
g *pipeline.GroupByNode
byName bool
tagNames []string
begin edge.BeginBatchMessage
dimensions models.Dimensions
allDimensions bool
mu sync.RWMutex
lastTime time.Time
groups map[models.GroupID]edge.BufferedBatchMessage
}
// Create a new GroupByNode which splits the stream dynamically based on the specified dimensions.
func newGroupByNode(et *ExecutingTask, n *pipeline.GroupByNode, d NodeDiagnostic) (*GroupByNode, error) {
gn := &GroupByNode{
node: node{Node: n, et: et, diag: d},
g: n,
groups: make(map[models.GroupID]edge.BufferedBatchMessage),
}
gn.node.runF = gn.runGroupBy
gn.allDimensions, gn.tagNames = determineTagNames(n.Dimensions, n.ExcludedDimensions)
gn.byName = n.ByMeasurementFlag
return gn, nil
}
func (n *GroupByNode) runGroupBy([]byte) error {
valueF := func() int64 {
n.mu.RLock()
l := len(n.groups)
n.mu.RUnlock()
return int64(l)
}
n.statMap.Set(statCardinalityGauge, expvar.NewIntFuncGauge(valueF))
consumer := edge.NewConsumerWithReceiver(
n.ins[0],
n,
)
return consumer.Consume()
}
func (n *GroupByNode) Point(p edge.PointMessage) error {
p = p.ShallowCopy()
n.timer.Start()
dims := p.Dimensions()
dims.ByName = dims.ByName || n.byName
dims.TagNames = computeTagNames(p.Tags(), n.allDimensions, n.tagNames, n.g.ExcludedDimensions)
p.SetDimensions(dims)
n.timer.Stop()
if err := edge.Forward(n.outs, p); err != nil {
return err
}
return nil
}
func (n *GroupByNode) BeginBatch(begin edge.BeginBatchMessage) error {
n.timer.Start()
defer n.timer.Stop()
if err := n.emit(begin.Time()); err != nil {
return err
}
n.begin = begin
n.dimensions = begin.Dimensions()
n.dimensions.ByName = n.dimensions.ByName || n.byName
return nil
}
func (n *GroupByNode) BatchPoint(bp edge.BatchPointMessage) error {
n.timer.Start()
defer n.timer.Stop()
n.dimensions.TagNames = computeTagNames(bp.Tags(), n.allDimensions, n.tagNames, n.g.ExcludedDimensions)
groupID := models.ToGroupID(n.begin.Name(), bp.Tags(), n.dimensions)
group, ok := n.groups[groupID]
if !ok {
// Create new begin message
newBegin := n.begin.ShallowCopy()
newBegin.SetTagsAndDimensions(bp.Tags(), n.dimensions)
// Create buffer for group batch
group = edge.NewBufferedBatchMessage(
newBegin,
make([]edge.BatchPointMessage, 0, newBegin.SizeHint()),
edge.NewEndBatchMessage(),
)
n.mu.Lock()
n.groups[groupID] = group
n.mu.Unlock()
}
group.SetPoints(append(group.Points(), bp))
return nil
}
func (n *GroupByNode) EndBatch(end edge.EndBatchMessage) error {
return nil
}
func (n *GroupByNode) Barrier(b edge.BarrierMessage) error {
n.timer.Start()
err := n.emit(b.Time())
n.timer.Stop()
if err != nil {
return err
}
return edge.Forward(n.outs, b)
}
func (n *GroupByNode) DeleteGroup(d edge.DeleteGroupMessage) error {
n.timer.Start()
n.mu.Lock()
delete(n.groups, d.GroupID())
n.mu.Unlock()
n.timer.Stop()
return edge.Forward(n.outs, d)
}
func (n *GroupByNode) Done() {}
// emit sends all groups before time t to children nodes.
// The node timer must be started when calling this method.
func (n *GroupByNode) emit(t time.Time) error {
// TODO: ensure this time comparison works with barrier messages
if !t.Equal(n.lastTime) {
n.lastTime = t
// Emit all groups
for id, group := range n.groups {
// Update SizeHint since we know the final point count
group.Begin().SetSizeHint(len(group.Points()))
// Sort points since we didn't guarantee insertion order was sorted
sort.Sort(edge.BatchPointMessages(group.Points()))
// Send group batch to all children
n.timer.Pause()
if err := edge.Forward(n.outs, group); err != nil {
return err
}
n.timer.Resume()
n.mu.Lock()
// Remove from group
delete(n.groups, id)
n.mu.Unlock()
}
}
return nil
}
func determineTagNames(dimensions []interface{}, excluded []string) (allDimensions bool, realDimensions []string) {
for _, dim := range dimensions {
switch d := dim.(type) {
case string:
realDimensions = append(realDimensions, d)
case *ast.StarNode:
allDimensions = true
}
}
sort.Strings(realDimensions)
realDimensions = filterExcludedTagNames(realDimensions, excluded)
return
}
func filterExcludedTagNames(tagNames, excluded []string) []string {
filtered := tagNames[0:0]
for _, t := range tagNames {
found := false
for _, x := range excluded {
if x == t {
found = true
break
}
}
if !found {
filtered = append(filtered, t)
}
}
return filtered
}
func computeTagNames(tags models.Tags, allDimensions bool, tagNames, excluded []string) []string {
if allDimensions {
return filterExcludedTagNames(models.SortedKeys(tags), excluded)
}
return tagNames
}