forked from grafana/carbon-relay-ng
/
table.go
284 lines (251 loc) · 7.22 KB
/
table.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
package main
import (
"errors"
"fmt"
"github.com/Dieterbe/go-metrics"
"sync"
)
type Table struct {
sync.Mutex
Blacklist []*Matcher `json:"blacklist"`
Routes []*Route `json:"routes"`
spoolDir string
numBlacklist metrics.Counter
numUnroutable metrics.Counter
}
func NewTable(spoolDir string) *Table {
routes := make([]*Route, 0)
blacklist := make([]*Matcher, 0)
t := &Table{
sync.Mutex{},
blacklist,
routes,
spoolDir,
Counter("target_type=count.unit=Metric.direction=blacklist"),
Counter("target_type=count.unit=Metric.direction=unroutable"),
}
t.Run()
return t
}
// not thread safe, run this once only
func (table *Table) Run() error {
table.Lock()
defer table.Unlock()
for _, route := range table.Routes {
err := route.Run()
if err != nil {
return err
}
}
return nil
}
// buf is assumed to have no whitespace at the end
func (table *Table) Dispatch(buf []byte) {
table.Lock()
defer table.Unlock()
for _, matcher := range table.Blacklist {
if matcher.Match(buf) {
table.numBlacklist.Inc(1)
return
}
}
routed := false
for _, route := range table.Routes {
if route.Match(buf) {
routed = true
//fmt.Println("routing to " + dest.Key)
// routes should take this in as fast as they can
log.Info("table sending to route: %s", buf)
route.in <- buf
}
}
if !routed {
table.numUnroutable.Inc(1)
log.Notice("unrouteable: %s\n", buf)
}
}
// to view the state of the table/route at any point in time
// we might add more functions to view specific entries if the need for that appears
func (table *Table) Snapshot() *Table {
table.Lock()
defer table.Unlock()
blacklist := make([]*Matcher, len(table.Blacklist))
for i, p := range table.Blacklist {
blacklist[i] = p
}
routes := make([]*Route, len(table.Routes))
for i, r := range table.Routes {
routes[i] = r.Snapshot()
}
return &Table{sync.Mutex{}, blacklist, routes, table.spoolDir, nil, nil}
}
func (table *Table) GetRoute(key string) *Route {
table.Lock()
defer table.Unlock()
for _, r := range table.Routes {
if r.Key == key {
return r
}
}
return nil
}
func (table *Table) AddRoute(route *Route) {
table.Lock()
defer table.Unlock()
table.Routes = append(table.Routes, route)
}
func (table *Table) AddBlacklist(matcher *Matcher) {
table.Lock()
defer table.Unlock()
table.Blacklist = append(table.Blacklist, matcher)
}
func (table *Table) Flush() error {
table.Lock()
defer table.Unlock()
for _, route := range table.Routes {
err := route.Flush()
if err != nil {
return err
}
}
return nil
}
func (table *Table) Shutdown() error {
table.Lock()
defer table.Unlock()
for _, route := range table.Routes {
err := route.Shutdown()
if err != nil {
return err
}
}
table.Routes = make([]*Route, 0)
return nil
}
// idempotent semantics, not existing is fine
func (table *Table) DelRoute(key string) error {
table.Lock()
defer table.Unlock()
toDelete := -1
var i int
var route *Route
for i, route = range table.Routes {
if route.Key == key {
toDelete = i
break
}
}
if toDelete == -1 {
return nil
}
table.Routes = append(table.Routes[:toDelete], table.Routes[toDelete+1:]...)
err := route.Shutdown()
if err != nil {
// dest removed from routing table but still trying to connect
// it won't get new stuff on its input though
return err
}
return nil
}
func (table *Table) DelBlacklist(index int) error {
table.Lock()
defer table.Unlock()
if index >= len(table.Blacklist) {
return errors.New(fmt.Sprintf("Invalid index %d", index))
}
table.Blacklist = append(table.Blacklist[:index], table.Blacklist[index+1:]...)
return nil
}
func (table *Table) DelDestination(key string, index int) error {
route := table.GetRoute(key)
if route == nil {
return errors.New(fmt.Sprintf("Invalid route for %v", key))
}
return route.DelDestination(index)
}
func (table *Table) UpdateDestination(key string, index int, opts map[string]string) error {
route := table.GetRoute(key)
if route == nil {
return errors.New(fmt.Sprintf("Invalid route for %v", key))
}
return route.UpdateDestination(index, opts)
}
func (table *Table) UpdateRoute(key string, opts map[string]string) error {
route := table.GetRoute(key)
if route == nil {
return errors.New(fmt.Sprintf("Invalid route for %v", key))
}
return route.Update(opts)
}
func (table *Table) Print() (str string) {
// TODO also print route type, print blacklist
// we want to print things concisely (but no smaller than the defaults below)
// so we have to figure out the max lengths of everything first
// the default values can be arbitrary (bot not smaller than the column titles),
// i figured multiples of 4 should look good
// 'R' stands for Route, 'D' for dest, 'B' blacklist
maxBPrefix := 4
maxBSub := 4
maxBRegex := 4
maxRKey := 8
maxRPrefix := 4
maxRSub := 4
maxRRegex := 4
maxDPrefix := 4
maxDSub := 4
maxDRegex := 4
maxDAddr := 16
maxDSpoolDir := 16
t := table.Snapshot()
for _, black := range t.Blacklist {
maxBPrefix = max(maxBRegex, len(black.Prefix))
maxBSub = max(maxBSub, len(black.Sub))
maxBRegex = max(maxBRegex, len(black.Regex))
}
for _, route := range t.Routes {
maxRKey = max(maxRKey, len(route.Key))
maxRPrefix = max(maxRPrefix, len(route.Matcher.Prefix))
maxRSub = max(maxRSub, len(route.Matcher.Sub))
maxRRegex = max(maxRRegex, len(route.Matcher.Regex))
for _, dest := range route.Dests {
maxDPrefix = max(maxDPrefix, len(dest.Matcher.Prefix))
maxDSub = max(maxDSub, len(dest.Matcher.Sub))
maxDRegex = max(maxDRegex, len(dest.Matcher.Regex))
maxDAddr = max(maxDAddr, len(dest.Addr))
maxDSpoolDir = max(maxDSpoolDir, len(dest.spoolDir))
}
}
heaFmtB := fmt.Sprintf("Blacklist: %%%ds %%%ds %%%ds\n", maxBPrefix+1, maxBSub+1, maxBRegex+1)
rowFmtB := fmt.Sprintf(" %%%ds %%%ds %%%ds\n", maxBPrefix+1, maxBSub+1, maxBRegex+1)
heaFmtR := fmt.Sprintf("Routes: %%%ds %%%ds %%%ds %%%ds\n", maxRKey+1, maxRPrefix+1, maxRSub+1, maxRRegex+1)
rowFmtR := fmt.Sprintf("> %%%ds %%%ds %%%ds %%%ds\n", maxRKey+1, maxRPrefix+1, maxRSub+1, maxRRegex+1)
heaFmtD := fmt.Sprintf(" %%%ds %%%ds %%%ds %%%ds %%%ds %%6s %%6s %%6s\n", maxDPrefix+1, maxDSub+1, maxDRegex+1, maxDAddr+1, maxDSpoolDir+1)
rowFmtD := fmt.Sprintf(" %%%ds %%%ds %%%ds %%%ds %%%ds %%6t %%6t %%6t\n", maxDPrefix+1, maxDSub+1, maxDRegex+1, maxDAddr+1, maxDSpoolDir+1)
str += fmt.Sprintf(heaFmtB, "prefix", "substr", "regex")
for _, black := range t.Blacklist {
str += fmt.Sprintf(rowFmtB, black.Prefix, black.Sub, black.Regex)
}
str += "\n"
str += fmt.Sprintf(heaFmtR, "key", "prefix", "substr", "regex")
str += "==========="
for i := 1; i < maxRKey+maxRPrefix+maxRSub+maxRRegex+7; i++ {
str += "="
}
str += "\n"
for _, route := range t.Routes {
m := route.Matcher
str += fmt.Sprintf(rowFmtR, route.Key, m.Prefix, m.Sub, m.Regex)
str += fmt.Sprintf(heaFmtD, "prefix", "substr", "regex", "addr", "spoolDir", "spool", "pickle", "online")
str += " "
for i := 1; i < maxDPrefix+maxDSub+maxDRegex+maxDAddr+maxDSpoolDir+5+3*6+10; i++ {
str += "-"
}
str += "\n"
for _, dest := range route.Dests {
m := dest.Matcher
str += fmt.Sprintf(rowFmtD, m.Prefix, m.Sub, m.Regex, dest.Addr, dest.spoolDir, dest.Spool, dest.Pickle, dest.Online)
}
str += "\n"
}
return
}