This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imperatives.go
354 lines (333 loc) · 9.59 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
package main
import (
"errors"
"fmt"
"github.com/graphite-ng/carbon-relay-ng/_third_party/github.com/taylorchu/toki"
"github.com/graphite-ng/carbon-relay-ng/aggregator"
"strconv"
"strings"
"time"
//"github.com/davecgh/go-spew/spew"
)
const (
addBlack toki.Token = iota
addAgg
addRouteSendAllMatch
addRouteSendFirstMatch
addRouteConsistentHashing
addDest
modDest
modRoute
opt
str
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 tokenDefGlobal = []toki.Def{
{Token: addBlack, Pattern: "addBlack .*"},
{Token: addAgg, Pattern: "addAgg .*"},
{Token: addRouteSendAllMatch, Pattern: "addRoute sendAllMatch [a-z-_]+"},
{Token: addRouteSendFirstMatch, Pattern: "addRoute sendFirstMatch [a-z-_]+"},
{Token: addRouteConsistentHashing, Pattern: "addRoute consistentHashing [a-z-_]+"},
{Token: addDest, Pattern: "addDest [a-z-_]+"},
{Token: modDest, Pattern: "modDest .*"},
{Token: modRoute, Pattern: "modRoute .*"},
{Token: opt, Pattern: "[a-z]+="},
{Token: str, Pattern: "\".*\""},
{Token: word, Pattern: "[^ ]+"},
}
var tokenDefDest = []toki.Def{
{Token: opt, Pattern: "[a-z]+="},
{Token: str, Pattern: "\".*\""},
{Token: word, Pattern: "[^ ]+"},
}
// 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(specs []string, table *Table, allowMatcher bool) (destinations []*Destination, err error) {
s := toki.NewScanner(tokenDefDest)
for _, spec := range specs {
//fmt.Println("spec" + spec)
var prefix, sub, regex, addr, spoolDir string
var spool, pickle bool
flush := 1000
reconn := 10000
spoolDir = table.spoolDir
s.SetInput(spec)
t := s.Next()
//fmt.Println("thisisit")
if len(t.Value) == 0 {
return destinations, errors.New("addr not set for endpoint")
}
addr = string(t.Value)
//fmt.Println(t.Token, word)
if t.Token != word {
//fmt.Println("wtf", t.Token, word)
return destinations, fmt.Errorf("expected destination endpoint spec, not '%s'", t.Value)
}
for {
t := s.Next()
if t.Token == opt {
val := string(t.Value)
//fmt.Println("yes got my opt with val", val)
switch val {
case "prefix=":
val := s.Next()
prefix = string(val.Value)
case "sub=":
val := s.Next()
sub = string(val.Value)
case "regex=":
val := s.Next()
regex = string(val.Value)
case "flush=":
val := s.Next()
i, err := strconv.Atoi(string(val.Value))
if err != nil {
return destinations, err
}
flush = i
case "reconn=":
val := s.Next()
i, err := strconv.Atoi(string(val.Value))
if err != nil {
return destinations, err
}
reconn = i
case "pickle=":
t := s.Next()
val := string(t.Value)
if val == "true" {
pickle = true
} else if val == "false" {
} else {
return destinations, fmt.Errorf("unrecognized pickle value '%s'", val)
}
case "spool=":
t := s.Next()
val := string(t.Value)
if val == "true" {
spool = true
} else if val != "false" {
return destinations, fmt.Errorf("unrecognized spool value '%s'", val)
}
default:
return destinations, fmt.Errorf("unrecognized option '%s'", val)
}
} else {
break
//return destinations, errors.New(fmt.Sprintf("expected endpoint option, not token type %v with value '%s'", t.Token, t.Value))
}
}
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
}
// note the two spaces between a route and endpoints
//"addBlack [prefix|sub|regex] filter-out-all-metrics-matching-this-substring",
//"addRoute sendAllMatch carbon-default 127.0.0.1:2005 spool=false pickle=false",
//"addRoute sendFirstMatch demo sub=foo prefix=foo re=foo 127.0.0.1:12345 spool=true"
//addBlack string-without-spaces
//addRoute <type> <key> <match options> <dests> # match options can't have spaces for now. sorry
//dests:
// <tcp addr> <options>
func applyCommand(table *Table, cmd string) error {
inputs := strings.Split(cmd, " ")
s := toki.NewScanner(tokenDefGlobal)
s.SetInput(inputs[0])
t := s.Next()
if t.Token == addBlack {
inputs = strings.Fields(cmd)
prefix_pat := ""
sub_pat := ""
regex_pat := ""
if len(inputs) == 2 {
// The fallback case to support the default substring method
sub_pat = inputs[1]
} else if len(inputs) == 3 {
// New case supporting prefix, sub and regex patterns
match_method := inputs[1]
pattern := inputs[2]
if match_method == "prefix" {
prefix_pat = pattern
} else if match_method == "sub" {
sub_pat = pattern
} else if match_method == "regex" {
regex_pat = pattern
} else {
return errors.New("addBlack [prefix|sub|regex] <pattern> (invalid match type)")
}
} else {
return errors.New("addBlack [prefix|sub|regex] <pattern>")
}
m, err := NewMatcher(prefix_pat, sub_pat, regex_pat)
if err != nil {
return err
}
table.AddBlacklist(m)
} else if t.Token == addAgg {
inputs = strings.Fields(cmd)
if len(inputs) != 6 {
return errors.New("addAgg <func> <match> <key> <interval> <wait>")
}
fun := inputs[1]
regex := inputs[2]
outFmt := inputs[3]
interval, err := strconv.Atoi(inputs[4])
if err != nil {
return err
}
wait, err := strconv.Atoi(inputs[5])
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)
} else if t.Token == addRouteSendAllMatch {
split := strings.Split(string(t.Value), " ")
key := split[2]
if len(inputs) < 2 {
return fmt.Errorf("must get at least 1 destination for route '%s'", key)
}
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
destinations, err := readDestinations(inputs[1:], table, true)
if err != nil {
return err
}
route, err := NewRouteSendAllMatch(key, prefix, sub, regex, destinations)
if err != nil {
return err
}
table.AddRoute(route)
} else if t.Token == addRouteSendFirstMatch {
split := strings.Split(string(t.Value), " ")
key := split[2]
if len(inputs) < 2 {
return fmt.Errorf("must get at least 1 destination for route '%s'", key)
}
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
destinations, err := readDestinations(inputs[1:], table, true)
if err != nil {
return err
}
route, err := NewRouteSendFirstMatch(key, prefix, sub, regex, destinations)
if err != nil {
return err
}
table.AddRoute(route)
} else if t.Token == addRouteConsistentHashing {
split := strings.Split(string(t.Value), " ")
key := split[2]
if len(inputs) < 3 {
return fmt.Errorf("must get at least 2 destinations for consistent hashing route '%s'", key)
}
prefix, sub, regex, err := readRouteOpts(s)
if err != nil {
return err
}
destinations, err := readDestinations(inputs[1:], table, false)
if err != nil {
return err
}
route, err := NewRouteConsistentHashing(key, prefix, sub, regex, destinations)
if err != nil {
return err
}
table.AddRoute(route)
} else if t.Token == addDest {
//split := strings.Split(string(t.Value), " ")
//key := split[2]
fmt.Println("val", t.Value)
fmt.Println("inputs", inputs[0])
} else if t.Token == modDest {
split := strings.Split(string(t.Value), " ")
if len(split) < 4 {
return errors.New("need a key, index and at least one option")
}
key := split[1]
index, err := strconv.Atoi(split[2])
if err != nil {
return err
}
opts := make(map[string]string)
for _, str := range split[3:] {
opt := strings.Split(str, "=")
if len(opt) != 2 {
return fmt.Errorf("bad option format at '%s'", str)
}
opts[opt[0]] = opt[1]
}
return table.UpdateDestination(key, index, opts)
} else if t.Token == modRoute {
split := strings.Split(string(t.Value), " ")
if len(split) < 3 {
return errors.New("need a key and at least one option")
}
key := split[1]
opts := make(map[string]string)
for _, str := range split[2:] {
opt := strings.Split(str, "=")
if len(opt) != 2 {
return fmt.Errorf("bad option format at '%s'", str)
}
opts[opt[0]] = opt[1]
}
return table.UpdateRoute(key, opts)
} else {
return fmt.Errorf("unrecognized command '%s'", t.Value)
}
if t = s.Next(); t.Token != toki.EOF {
//fmt.Println("h")
return fmt.Errorf("extraneous input '%s'", t.Value)
}
return nil
}
func readRouteOpts(s *toki.Scanner) (prefix, sub, regex string, err error) {
for {
t := s.Next()
//spew.Dump(t.Token)
//spew.Dump(t)
if t.Token == toki.EOF {
break
}
if t.Token == toki.Error {
return "", "", "", errors.New("read the error token instead of one i recognize")
}
if t.Token == opt {
//fmt.Println("yet")
switch string(t.Value) {
case "prefix=":
val := s.Next()
prefix = string(val.Value)
case "sub=":
val := s.Next()
sub = string(val.Value)
case "regex=":
val := s.Next()
regex = string(val.Value)
default:
return "", "", "", fmt.Errorf("unrecognized option '%s'", t.Value)
}
}
}
return
}