/
aggregator.go
311 lines (290 loc) · 8.3 KB
/
aggregator.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
302
303
304
305
306
307
308
309
310
311
package aggregator
import (
"bytes"
"fmt"
"regexp"
"sync"
"time"
"github.com/graphite-ng/carbon-relay-ng/clock"
)
type Aggregator struct {
Fun string `json:"fun"`
procConstr func(val float64, ts uint32) Processor
in chan msg `json:"-"` // incoming metrics, already split in 3 fields
out chan []byte // outgoing metrics
Regex string `json:"regex,omitempty"`
Prefix string `json:"prefix,omitempty"`
Sub string `json:"substring,omitempty"`
regex *regexp.Regexp // compiled version of Regex
prefix []byte // automatically generated based on Prefix or regex, for fast preMatch
substring []byte // based on Sub, for fast preMatch
OutFmt string
outFmt []byte
Cache bool
reCache map[string]CacheEntry
Interval uint // expected interval between values in seconds, we will quantize to make sure alginment to interval-spaced timestamps
Wait uint // seconds to wait after quantized time value before flushing final outcome and ignoring future values that are sent too late.
aggregations map[aggkey]Processor // aggregations in process: one for each quantized timestamp and output key, i.e. for each output metric.
snapReq chan bool // chan to issue snapshot requests on
snapResp chan *Aggregator // chan on which snapshot response gets sent
shutdown chan struct{} // chan used internally to shut down
wg sync.WaitGroup // tracks worker running state
now func() time.Time // returns current time. wraps time.Now except in some unit tests
tick <-chan time.Time // controls when to flush
}
type msg struct {
buf [][]byte
val float64
ts uint32
}
// regexToPrefix inspects the regex and returns the longest static prefix part of the regex
// all inputs for which the regex match, must have this prefix
func regexToPrefix(regex string) []byte {
substr := ""
for i := 0; i < len(regex); i++ {
ch := regex[i]
if i == 0 {
if ch == '^' {
continue // good we need this
} else {
break // can't deduce any substring here
}
}
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' {
substr += string(ch)
// "\." means a dot character
} else if ch == 92 && i+1 < len(regex) && regex[i+1] == '.' {
substr += "."
i += 1
} else {
//fmt.Println("don't know what to do with", string(ch))
// anything more advanced should be regex syntax that is more permissive and hence not a static substring.
break
}
}
return []byte(substr)
}
// New creates an aggregator
func New(fun, regex, prefix, sub, outFmt string, cache bool, interval, wait uint, out chan []byte) (*Aggregator, error) {
return NewMocked(fun, regex, prefix, sub, outFmt, cache, interval, wait, out, 2000, time.Now, clock.AlignedTick(time.Duration(interval)*time.Second))
}
func NewMocked(fun, regex, prefix, sub, outFmt string, cache bool, interval, wait uint, out chan []byte, inBuf int, now func() time.Time, tick <-chan time.Time) (*Aggregator, error) {
regexObj, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
procConstr, err := GetProcessorConstructor(fun)
if err != nil {
return nil, err
}
var reCache map[string]CacheEntry
if cache {
reCache = make(map[string]CacheEntry)
}
var prefixBytes []byte
if prefix != "" {
prefixBytes = []byte(prefix)
} else {
prefixBytes = regexToPrefix(regex)
}
a := &Aggregator{
fun,
procConstr,
make(chan msg, inBuf),
out,
regex,
string(prefixBytes),
sub,
regexObj,
prefixBytes,
[]byte(sub),
outFmt,
[]byte(outFmt),
cache,
reCache,
interval,
wait,
make(map[aggkey]Processor),
make(chan bool),
make(chan *Aggregator),
make(chan struct{}),
sync.WaitGroup{},
now,
tick,
}
a.wg.Add(1)
go a.run()
return a, nil
}
type aggkey struct {
key string
ts uint
}
func (a *Aggregator) AddOrCreate(key string, ts uint32, quantized uint, value float64) {
k := aggkey{
key,
quantized,
}
proc, ok := a.aggregations[k]
if ok {
proc.Add(value, ts)
} else if quantized > uint(a.now().Unix())-a.Wait {
proc = a.procConstr(value, ts)
a.aggregations[k] = proc
}
}
// Flush finalizes and removes aggregations that are due
func (a *Aggregator) Flush(ts uint) {
for k, proc := range a.aggregations {
if k.ts < ts {
result, ok := proc.Flush()
if ok {
metric := fmt.Sprintf("%s %f %d", string(k.key), result, k.ts)
// log.Debug("aggregator %s-%v-%v values %v -> result %q", a.Fun, a.Regex, a.OutFmt, agg, metric)
a.out <- []byte(metric)
}
delete(a.aggregations, k)
}
}
//fmt.Println("flush done for ", a.now().Unix(), ". agg size now", len(a.aggregations), a.now())
}
func (a *Aggregator) Shutdown() {
close(a.shutdown)
a.wg.Wait()
}
func (a *Aggregator) AddMaybe(buf [][]byte, val float64, ts uint32) {
if a.PreMatch(buf[0]) {
a.in <- msg{
buf,
val,
ts,
}
}
}
//PreMatch checks if the specified metric matches the specified prefix and/or substring
//If prefix isn't explicitly specified it will be derived from the regex where possible.
//If this returns false the metric will not be passed through to the main regex matching stage.
func (a *Aggregator) PreMatch(buf []byte) bool {
if len(a.prefix) > 0 && !bytes.HasPrefix(buf, a.prefix) {
return false
}
if len(a.substring) > 0 && !bytes.Contains(buf, a.substring) {
return false
}
return true
}
type CacheEntry struct {
match bool
key string
seen uint32
}
//
func (a *Aggregator) match(m msg) (string, bool) {
key := m.buf[0]
var dst []byte
matches := a.regex.FindSubmatchIndex(key)
if matches == nil {
return "", false
}
return string(a.regex.Expand(dst, a.outFmt, key, matches)), true
}
func (a *Aggregator) run() {
for {
select {
case msg := <-a.in:
// note, we rely here on the fact that the packet has already been validated
key := msg.buf[0]
var outKey string
var ok bool
if a.reCache != nil {
entry, ok := a.reCache[string(key)]
if ok {
entry.seen = uint32(a.now().Unix())
a.reCache[string(key)] = entry
if !entry.match {
continue
}
outKey = entry.key
} else {
outKey, ok = a.match(msg)
a.reCache[string(key)] = CacheEntry{
ok,
outKey,
uint32(a.now().Unix()),
}
if !ok {
continue
}
}
} else {
outKey, ok = a.match(msg)
if !ok {
continue
}
}
ts := uint(msg.ts)
quantized := ts - (ts % a.Interval)
a.AddOrCreate(outKey, msg.ts, quantized, msg.val)
case now := <-a.tick:
thresh := now.Add(-time.Duration(a.Wait) * time.Second)
a.Flush(uint(thresh.Unix()))
// if cache is enabled, clean it out of stale entries
// it's not ideal to block our channel while flushing AND cleaning up the cache
// ideally, these operations are interleaved in time, but we can optimize that later
// this is a simple heuristic but should make the cache always converge on only active data (without memory leaks)
// even though some cruft may temporarily linger a bit longer.
// WARNING: this relies on Go's map implementation detail which randomizes iteration order, in order for us to reach
// the entire keyspace. This may stop working properly with future go releases. Will need to come up with smth better.
cutoff := uint32(now.Add(-100 * time.Duration(a.Wait) * time.Second).Unix())
if a.reCache != nil {
for k, v := range a.reCache {
if v.seen < cutoff {
delete(a.reCache, k)
}
break // stop looking when we don't see old entries. we'll look again soon enough.
}
}
case <-a.snapReq:
aggs := make(map[aggkey]Processor)
for k := range a.aggregations {
aggs[k] = nil
}
s := &Aggregator{
a.Fun,
a.procConstr,
nil,
nil,
a.Regex,
a.Prefix,
a.Sub,
nil,
a.prefix,
a.substring,
a.OutFmt,
nil,
a.Cache,
nil,
a.Interval,
a.Wait,
aggs,
nil,
nil,
nil,
sync.WaitGroup{},
time.Now,
nil,
}
a.snapResp <- s
case <-a.shutdown:
thresh := a.now().Add(-time.Duration(a.Wait) * time.Second)
a.Flush(uint(thresh.Unix()))
a.wg.Done()
return
}
}
}
// to view the state of the aggregator at any point in time
func (a *Aggregator) Snapshot() *Aggregator {
a.snapReq <- true
return <-a.snapResp
}