forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_reduce.go
218 lines (199 loc) · 4.41 KB
/
map_reduce.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
package kapacitor
import (
"fmt"
"log"
"time"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdb/influxdb/tsdb"
)
type MapResult struct {
Name string
Group models.GroupID
Dims []string
Tags map[string]string
TMax time.Time
Outs []interface{}
}
type MapFunc func(in *tsdb.MapInput) interface{}
type ReduceFunc func(in []interface{}, tmax time.Time, useTMax bool, as string) interface{}
type MapInfo struct {
Field string
Func MapFunc
}
type MapNode struct {
node
mr *pipeline.MapNode
f MapFunc
field string
parallel int
}
func newMapNode(et *ExecutingTask, n *pipeline.MapNode, l *log.Logger) (*MapNode, error) {
f, ok := n.Map.(MapInfo)
if !ok {
return nil, fmt.Errorf("invalid map given to map node %T", n.Map)
}
m := &MapNode{
node: node{Node: n, et: et, logger: l},
mr: n,
f: f.Func,
field: f.Field,
parallel: 2,
}
m.node.runF = m.runMaps
return m, nil
}
func (m *MapNode) runMaps([]byte) error {
switch m.mr.Wants() {
case pipeline.StreamEdge:
return m.runStreamMap()
case pipeline.BatchEdge:
return m.runBatchMap()
default:
return fmt.Errorf("cannot map %v edge", m.mr.Wants())
}
}
func (m *MapNode) runStreamMap() error {
batches := make(map[models.GroupID]*models.Batch)
for p, ok := m.ins[0].NextPoint(); ok; {
b := batches[p.Group]
if b == nil {
// Create new batch
tags := make(map[string]string, len(p.Dimensions))
for _, dim := range p.Dimensions {
tags[dim] = p.Tags[dim]
}
b = &models.Batch{
Name: p.Name,
Group: p.Group,
Tags: tags,
TMax: p.Time,
}
batches[p.Group] = b
}
if p.Time.Equal(b.TMax) {
b.Points = append(b.Points, models.BatchPointFromPoint(p))
// advance to next point
p, ok = m.ins[0].NextPoint()
} else {
err := m.mapBatch(*b)
if err != nil {
return err
}
batches[b.Group] = nil
}
}
return nil
}
func (m *MapNode) runBatchMap() error {
for b, ok := m.ins[0].NextBatch(); ok; b, ok = m.ins[0].NextBatch() {
err := m.mapBatch(b)
if err != nil {
return err
}
}
return nil
}
func (m *MapNode) mapBatch(b models.Batch) error {
if len(b.Points) == 0 {
return nil
}
done := make(chan bool, m.parallel)
mr := &MapResult{
Outs: make([]interface{}, m.parallel),
}
inputs := make([]tsdb.MapInput, m.parallel)
j := 0
for _, p := range b.Points {
value, ok := p.Fields[m.field]
if !ok {
fields := make([]string, 0, len(p.Fields))
for field := range p.Fields {
fields = append(fields, field)
}
return fmt.Errorf("unknown field %s, available fields %v", m.field, fields)
}
item := tsdb.MapItem{
Timestamp: p.Time.Unix(),
Value: value,
Fields: p.Fields,
Tags: p.Tags,
}
inputs[j].Items = append(inputs[j].Items, item)
inputs[j].TMin = -1
j = (j + 1) % m.parallel
}
mr.Name = b.Name
mr.Group = b.Group
mr.TMax = b.TMax
mr.Tags = b.Tags
mr.Dims = models.SortedKeys(b.Tags)
for i := 0; i < m.parallel; i++ {
go func(i int) {
mr.Outs[i] = m.f(&inputs[i])
done <- true
}(i)
}
finished := 0
for finished != m.parallel {
<-done
finished++
}
for _, child := range m.outs {
err := child.CollectMaps(mr)
if err != nil {
return err
}
}
return nil
}
type ReduceNode struct {
node
r *pipeline.ReduceNode
f ReduceFunc
}
func newReduceNode(et *ExecutingTask, n *pipeline.ReduceNode, l *log.Logger) (*ReduceNode, error) {
f, ok := n.Reduce.(ReduceFunc)
if !ok {
return nil, fmt.Errorf("invalid func given to batch reduce node %T", n.Reduce)
}
b := &ReduceNode{
node: node{Node: n, et: et, logger: l},
r: n,
f: f,
}
b.node.runF = b.runReduce
return b, nil
}
func (r *ReduceNode) runReduce([]byte) error {
for m, ok := r.ins[0].NextMaps(); ok; m, ok = r.ins[0].NextMaps() {
rr := r.f(m.Outs, m.TMax, r.r.PointTimes, r.r.As)
switch result := rr.(type) {
case models.Point:
result.Name = m.Name
result.Group = m.Group
result.Dimensions = m.Dims
result.Tags = m.Tags
for _, c := range r.outs {
err := c.CollectPoint(result)
if err != nil {
return err
}
}
case models.Batch:
result.Name = m.Name
result.Group = m.Group
result.Tags = m.Tags
result.TMax = m.TMax
for _, c := range r.outs {
err := c.CollectBatch(result)
if err != nil {
return err
}
}
default:
return fmt.Errorf("unexpected result from reduce function %T", rr)
}
}
return nil
}