forked from grafana/carbon-relay-ng
/
imperatives.go
558 lines (516 loc) · 14.1 KB
/
imperatives.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package main
import (
"errors"
"fmt"
"github.com/graphite-ng/carbon-relay-ng/aggregator"
"github.com/taylorchu/toki"
"strconv"
"strings"
"time"
)
const (
addBlack toki.Token = iota
addAgg
addRouteSendAllMatch
addRouteSendFirstMatch
addRouteConsistentHashing
addRouteGrafanaNet
addDest
modDest
modRoute
str
sep
sumFn
avgFn
num
optPrefix
optAddr
optSub
optRegex
optFlush
optReconn
optPickle
optSpool
optTrue
optFalse
optBufSize
optFlushMaxNum
optFlushMaxWait
word
)
// we should make sure we apply changes atomatically. e.g. when changing dest between address A and pickle=false and B with pickle=true,
// we should never half apply the change if one of them fails.
var tokens = []toki.Def{
{Token: addBlack, Pattern: "addBlack"},
{Token: addAgg, Pattern: "addAgg"},
{Token: addRouteSendAllMatch, Pattern: "addRoute sendAllMatch"},
{Token: addRouteSendFirstMatch, Pattern: "addRoute sendFirstMatch"},
{Token: addRouteConsistentHashing, Pattern: "addRoute consistentHashing"},
{Token: addRouteGrafanaNet, Pattern: "addRoute grafanaNet"},
{Token: addDest, Pattern: "addDest"},
{Token: modDest, Pattern: "modDest"},
{Token: modRoute, Pattern: "modRoute"},
{Token: optPrefix, Pattern: "prefix="},
{Token: optAddr, Pattern: "addr="},
{Token: optSub, Pattern: "sub="},
{Token: optRegex, Pattern: "regex="},
{Token: optFlush, Pattern: "flush="},
{Token: optReconn, Pattern: "reconn="},
{Token: optPickle, Pattern: "pickle="},
{Token: optSpool, Pattern: "spool="},
{Token: optTrue, Pattern: "true"},
{Token: optFalse, Pattern: "false"},
{Token: optBufSize, Pattern: "bufSize="},
{Token: optFlushMaxNum, Pattern: "flushMaxNum="},
{Token: optFlushMaxWait, Pattern: "flushMaxWait="},
{Token: str, Pattern: "\".*\""},
{Token: sep, Pattern: "##"},
{Token: sumFn, Pattern: "sum"},
{Token: avgFn, Pattern: "avg"},
{Token: num, Pattern: "[0-9]+( |$)"}, // unfortunately we need the 2nd piece cause otherwise it would match the first of ip addresses etc. this means we need to TrimSpace later
{Token: word, Pattern: "[^ ]+"},
}
// note the two spaces between a route and endpoints
// match options can't have spaces for now. sorry
var errFmtAddBlack = errors.New("addBlack <prefix|sub|regex> <pattern>")
var errFmtAddAgg = errors.New("addAgg <sum|avg> <regex> <fmt> <interval> <wait>")
var errFmtAddRoute = errors.New("addRoute <type> <key> [prefix/sub/regex=,..] <dest> [<dest>[...]] where <dest> is <addr> [prefix/sub,regex,flush,reconn,pickle,spool=...]") // note flush and reconn are ints, pickle and spool are true/false. other options are strings
var errFmtAddRouteGrafanaNet = errors.New("addRoute GrafanaNet key [prefix/sub/regex] addr apiKey schemasFile [spool=true/false bufSize=int flushMaxNum=int flushMaxWait=int]")
var errFmtAddDest = errors.New("addDest <routeKey> <dest>") // not implemented yet
var errFmtModDest = errors.New("modDest <routeKey> <dest> <addr/prefix/sub/regex=>") // one or more can be specified at once
var errFmtModRoute = errors.New("modRoute <routeKey> <prefix/sub/regex=>") // one or more can be specified at once
func applyCommand(table *Table, cmd string) error {
s := toki.NewScanner(tokens)
s.SetInput(strings.Replace(cmd, " ", " ## ", -1)) // token library skips whitespace but for us double space is significant
t := s.Next()
switch t.Token {
case addAgg:
return readAddAgg(s, table)
case addBlack:
return readAddBlack(s, table)
case addDest:
return errors.New("sorry, addDest is not implemented yet")
case addRouteSendAllMatch:
return readAddRoute(s, table, NewRouteSendAllMatch)
case addRouteSendFirstMatch:
return readAddRoute(s, table, NewRouteSendFirstMatch)
case addRouteConsistentHashing:
return readAddRouteConsistentHashing(s, table)
case addRouteGrafanaNet:
return readAddRouteGrafanaNet(s, table)
case modDest:
return readModDest(s, table)
case modRoute:
return readModRoute(s, table)
default:
return fmt.Errorf("unrecognized command %q", t.Value)
}
if t = s.Next(); t.Token != toki.EOF {
return fmt.Errorf("extraneous input %q", t.Value)
}
return nil
}
func readAddAgg(s *toki.Scanner, table *Table) error {
t := s.Next()
if t.Token != sumFn && t.Token != avgFn {
return errors.New("invalid function. need sum/avg")
}
fun := string(t.Value)
if t = s.Next(); t.Token != word {
return errors.New("need a regex string")
}
regex := string(t.Value)
if t = s.Next(); t.Token != word {
return errors.New("need a format string")
}
outFmt := string(t.Value)
if t = s.Next(); t.Token != num {
return errors.New("need an interval number")
}
interval, err := strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return err
}
if t = s.Next(); t.Token != num {
return errors.New("need a wait number")
}
wait, err := strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return err
}
agg, err := aggregator.New(fun, regex, outFmt, uint(interval), uint(wait), table.In)
if err != nil {
return err
}
table.AddAggregator(agg)
return nil
}
func readAddBlack(s *toki.Scanner, table *Table) error {
prefix_pat := ""
sub_pat := ""
regex_pat := ""
t := s.Next()
if t.Token != word {
return errFmtAddBlack
}
method := string(t.Value)
switch method {
case "prefix":
if t = s.Next(); t.Token != word {
return errFmtAddBlack
}
prefix_pat = string(t.Value)
case "sub":
if t = s.Next(); t.Token != word {
return errFmtAddBlack
}
sub_pat = string(t.Value)
case "regex":
if t = s.Next(); t.Token != word {
return errFmtAddBlack
}
regex_pat = string(t.Value)
default:
return errFmtAddBlack
}
m, err := NewMatcher(prefix_pat, sub_pat, regex_pat)
if err != nil {
return err
}
table.AddBlacklist(m)
return nil
}
func readAddRoute(s *toki.Scanner, table *Table, constructor func(key, prefix, sub, regex string, destinations []*Destination) (Route, error)) error {
t := s.Next()
if t.Token != word {
return errFmtAddRoute
}
key := string(t.Value)
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
destinations, err := readDestinations(s, table, true)
if err != nil {
return err
}
if len(destinations) == 0 {
return fmt.Errorf("must get at least 1 destination for route '%s'", key)
}
route, err := constructor(key, prefix, sub, regex, destinations)
if err != nil {
return err
}
table.AddRoute(route)
return nil
}
func readAddRouteConsistentHashing(s *toki.Scanner, table *Table) error {
t := s.Next()
if t.Token != word {
return errFmtAddRoute
}
key := string(t.Value)
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
destinations, err := readDestinations(s, table, false)
if err != nil {
return err
}
if len(destinations) < 2 {
return fmt.Errorf("must get at least 2 destination for route '%s'", key)
}
route, err := NewRouteConsistentHashing(key, prefix, sub, regex, destinations)
if err != nil {
return err
}
table.AddRoute(route)
return nil
}
func readAddRouteGrafanaNet(s *toki.Scanner, table *Table) error {
t := s.Next()
if t.Token != word {
return errFmtAddRouteGrafanaNet
}
key := string(t.Value)
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
t = s.Next()
if t.Token != word {
return errFmtAddRouteGrafanaNet
}
addr := string(t.Value)
t = s.Next()
if t.Token != word {
return errFmtAddRouteGrafanaNet
}
apiKey := string(t.Value)
t = s.Next()
if t.Token != word {
return errFmtAddRouteGrafanaNet
}
schemasFile := string(t.Value)
t = s.Next()
var spool bool
var bufSize = int(1e7) // since a message is typically around 100B this is 1GB
var flushMaxNum = 10000 // number of metrics
var flushMaxWait = 500 // in ms
for ; t.Token != toki.EOF; t = s.Next() {
switch t.Token {
case optSpool:
t = s.Next()
if t.Token == optTrue || t.Token == optFalse {
spool, err = strconv.ParseBool(string(t.Value))
if err != nil {
return err
}
} else {
return errFmtAddRouteGrafanaNet
}
case optBufSize:
t = s.Next()
if t.Token == num {
bufSize, err = strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return err
}
} else {
return errFmtAddRouteGrafanaNet
}
case optFlushMaxNum:
t = s.Next()
if t.Token == num {
flushMaxNum, err = strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return err
}
} else {
return errFmtAddRouteGrafanaNet
}
case optFlushMaxWait:
t = s.Next()
if t.Token == num {
flushMaxWait, err = strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return err
}
} else {
return errFmtAddRouteGrafanaNet
}
default:
return fmt.Errorf("unexpected token %s %q", t.Token, t.Value)
}
}
route, err := NewRouteGrafanaNet(key, prefix, sub, regex, addr, apiKey, schemasFile, spool, bufSize, flushMaxNum, flushMaxWait)
if err != nil {
return err
}
table.AddRoute(route)
return nil
}
func readModDest(s *toki.Scanner, table *Table) error {
t := s.Next()
if t.Token != word {
return errFmtAddRoute
}
key := string(t.Value)
t = s.Next()
if t.Token != num {
return errFmtAddRoute
}
index, err := strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return err
}
opts := make(map[string]string)
for {
t = s.Next()
switch t.Token {
case toki.EOF:
break
case optAddr:
if t = s.Next(); t.Token != word {
return errFmtModDest
}
opts["addr"] = string(t.Value)
case optPrefix:
if t = s.Next(); t.Token != word {
return errFmtModDest
}
opts["prefix"] = string(t.Value)
case optSub:
if t = s.Next(); t.Token != word {
return errFmtModDest
}
opts["sub"] = string(t.Value)
case optRegex:
if t = s.Next(); t.Token != word {
return errFmtModDest
}
opts["regex"] = string(t.Value)
default:
return errFmtModDest
}
}
if len(opts) == 0 {
return errors.New("modDest needs at least 1 option")
}
return table.UpdateDestination(key, index, opts)
}
func readModRoute(s *toki.Scanner, table *Table) error {
t := s.Next()
if t.Token != word {
return errFmtAddRoute
}
key := string(t.Value)
opts := make(map[string]string)
for {
t = s.Next()
switch t.Token {
case toki.EOF:
break
case optPrefix:
if t = s.Next(); t.Token != word {
return errFmtModDest
}
opts["prefix"] = string(t.Value)
case optSub:
if t = s.Next(); t.Token != word {
return errFmtModDest
}
opts["sub"] = string(t.Value)
case optRegex:
if t = s.Next(); t.Token != word {
return errFmtModDest
}
opts["regex"] = string(t.Value)
default:
return errFmtModDest
}
}
if len(opts) == 0 {
return errors.New("modRoute needs at least 1 option")
}
return table.UpdateRoute(key, opts)
}
// we should read and apply all destinations at once,
// or at least make sure we apply them to the global datastruct at once,
// otherwise we can destabilize things / send wrong traffic, etc
func readDestinations(s *toki.Scanner, table *Table, allowMatcher bool) (destinations []*Destination, err error) {
for t := s.Next(); t.Token != toki.EOF; {
if t.Token == sep {
t = s.Next()
}
var prefix, sub, regex, addr, spoolDir string
var spool, pickle bool
flush := 1000
reconn := 10000
spoolDir = table.spoolDir
if t.Token != word {
return destinations, errors.New("addr not set for endpoint")
}
addr = string(t.Value)
for t.Token != toki.EOF && t.Token != sep {
t = s.Next()
switch t.Token {
case optPrefix:
if t = s.Next(); t.Token != word {
return destinations, errFmtAddRoute
}
prefix = string(t.Value)
case optSub:
if t = s.Next(); t.Token != word {
return destinations, errFmtAddRoute
}
sub = string(t.Value)
case optRegex:
if t = s.Next(); t.Token != word {
return destinations, errFmtAddRoute
}
regex = string(t.Value)
case optFlush:
if t = s.Next(); t.Token != num {
return destinations, errFmtAddRoute
}
flush, err = strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return destinations, err
}
case optReconn:
if t = s.Next(); t.Token != num {
return destinations, errFmtAddRoute
}
reconn, err = strconv.Atoi(strings.TrimSpace(string(t.Value)))
if err != nil {
return destinations, err
}
case optPickle:
if t = s.Next(); t.Token != optTrue && t.Token != optFalse {
return destinations, errFmtAddRoute
}
pickle, err = strconv.ParseBool(string(t.Value))
if err != nil {
return destinations, fmt.Errorf("unrecognized pickle value '%s'", t)
}
case optSpool:
if t = s.Next(); t.Token != optTrue && t.Token != optFalse {
return destinations, errFmtAddRoute
}
spool, err = strconv.ParseBool(string(t.Value))
if err != nil {
return destinations, fmt.Errorf("unrecognized spool value '%s'", t)
}
case toki.EOF:
case sep:
break
default:
return destinations, fmt.Errorf("unrecognized option '%s'", t)
}
}
periodFlush := time.Duration(flush) * time.Millisecond
periodReConn := time.Duration(reconn) * time.Millisecond
if !allowMatcher && (prefix != "" || sub != "" || regex != "") {
return destinations, fmt.Errorf("matching options (prefix, sub, and regex) not allowed for this route type")
}
dest, err := NewDestination(prefix, sub, regex, addr, spoolDir, spool, pickle, periodFlush, periodReConn)
if err != nil {
return destinations, err
}
destinations = append(destinations, dest)
}
return destinations, nil
}
func readRouteOpts(s *toki.Scanner) (prefix, sub, regex string, err error) {
for {
t := s.Next()
switch t.Token {
case toki.EOF:
break
case toki.Error:
return "", "", "", errors.New("read the error token instead of one i recognize")
case optPrefix:
if t = s.Next(); t.Token != word {
return "", "", "", errors.New("bad prefix option")
}
prefix = string(t.Value)
case optSub:
if t = s.Next(); t.Token != word {
return "", "", "", errors.New("bad sub option")
}
sub = string(t.Value)
case optRegex:
if t = s.Next(); t.Token != word {
return "", "", "", errors.New("bad regex option")
}
regex = string(t.Value)
case sep:
return
default:
return "", "", "", fmt.Errorf("unrecognized option '%s'", t.Value)
}
}
return
}