This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
397 lines (355 loc) · 9.29 KB
/
buffer.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
/*
Copyright 2018-2019 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package backend
import (
"bytes"
"context"
"fmt"
"sort"
"sync"
"github.com/gravitational/teleport"
radix "github.com/armon/go-radix"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)
// CircularBuffer implements in-memory circular buffer
// of predefined size, that is capable of fan-out of the backend events.
type CircularBuffer struct {
sync.Mutex
*log.Entry
ctx context.Context
cancel context.CancelFunc
events []Event
start int
end int
size int
watchers *watcherTree
}
// NewCircularBuffer returns a new instance of circular buffer
func NewCircularBuffer(ctx context.Context, size int) (*CircularBuffer, error) {
if size <= 0 {
return nil, trace.BadParameter("circular buffer size should be > 0")
}
ctx, cancel := context.WithCancel(ctx)
buf := &CircularBuffer{
Entry: log.WithFields(log.Fields{
trace.Component: teleport.ComponentBuffer,
}),
ctx: ctx,
cancel: cancel,
events: make([]Event, size),
start: -1,
end: -1,
size: 0,
watchers: newWatcherTree(),
}
return buf, nil
}
// Reset resets all events from the queue
// and closes all active watchers
func (c *CircularBuffer) Reset() {
c.Lock()
defer c.Unlock()
// could close multiple times
c.watchers.walk(func(w *BufferWatcher) {
w.closeWatcher()
})
c.watchers = newWatcherTree()
c.start = -1
c.end = -1
c.size = 0
for i := 0; i < len(c.events); i++ {
c.events[i] = Event{}
}
}
// Close closes circular buffer and all watchers
func (c *CircularBuffer) Close() error {
c.cancel()
c.Reset()
return nil
}
// Size returns circular buffer size
func (c *CircularBuffer) Size() int {
return c.size
}
// Events returns a copy of records as arranged from start to end
func (c *CircularBuffer) Events() []Event {
c.Lock()
defer c.Unlock()
return c.eventsCopy()
}
// eventsCopy returns a copy of events as arranged from start to end
func (c *CircularBuffer) eventsCopy() []Event {
if c.size == 0 {
return nil
}
var out []Event
for i := 0; i < c.size; i++ {
index := (c.start + i) % len(c.events)
if out == nil {
out = make([]Event, 0, c.size)
}
out = append(out, c.events[index])
}
return out
}
// PushBatch pushes elements to the queue as a batch
func (c *CircularBuffer) PushBatch(events []Event) {
c.Lock()
defer c.Unlock()
for i := range events {
c.push(events[i])
}
}
// Push pushes elements to the queue
func (c *CircularBuffer) Push(r Event) {
c.Lock()
defer c.Unlock()
c.push(r)
}
func (c *CircularBuffer) push(r Event) {
if c.size == 0 {
c.start = 0
c.end = 0
c.size = 1
} else if c.size < len(c.events) {
c.end = (c.end + 1) % len(c.events)
c.events[c.end] = r
c.size++
} else {
c.end = c.start
c.start = (c.start + 1) % len(c.events)
}
c.events[c.end] = r
c.fanOutEvent(r)
}
func (c *CircularBuffer) fanOutEvent(r Event) {
var watchersToDelete []*BufferWatcher
c.watchers.walkPath(string(r.Item.Key), func(watcher *BufferWatcher) {
if watcher.MetricComponent != "" {
watcherQueues.WithLabelValues(watcher.MetricComponent).Set(float64(len(watcher.eventsC)))
}
select {
case watcher.eventsC <- r:
case <-c.ctx.Done():
return
default:
watchersToDelete = append(watchersToDelete, watcher)
}
})
for _, watcher := range watchersToDelete {
c.Warningf("Closing %v, buffer overflow at %v elements.", watcher, len(watcher.eventsC))
watcher.closeWatcher()
c.watchers.rm(watcher)
}
}
func removeRedundantPrefixes(prefixes [][]byte) [][]byte {
if len(prefixes) == 0 {
return prefixes
}
// group adjacent prefixes together
sort.Slice(prefixes, func(i, j int) bool {
return bytes.Compare(prefixes[i], prefixes[j]) == -1
})
// j increments only for values with non-redundant prefixes
j := 0
for i := 1; i < len(prefixes); i++ {
// skip keys that have first key as a prefix
if bytes.HasPrefix(prefixes[i], prefixes[j]) {
continue
}
j++
// assign the first non-matching key to the j
prefixes[j] = prefixes[i]
}
return prefixes[:j+1]
}
// NewWatcher adds a new watcher to the events buffer
func (c *CircularBuffer) NewWatcher(ctx context.Context, watch Watch) (Watcher, error) {
c.Lock()
defer c.Unlock()
select {
case <-c.ctx.Done():
return nil, trace.BadParameter("buffer is closed")
default:
}
if watch.QueueSize == 0 {
watch.QueueSize = len(c.events)
}
if len(watch.Prefixes) == 0 {
// if watcher has no prefixes, assume it will match anything
// starting from the separator (what includes all keys in backend invariant, see Keys function)
watch.Prefixes = append(watch.Prefixes, []byte{Separator})
} else {
// if watcher's prefixes are redundant, keep only shorter prefixes
// to avoid double fan out
watch.Prefixes = removeRedundantPrefixes(watch.Prefixes)
}
closeCtx, cancel := context.WithCancel(ctx)
w := &BufferWatcher{
buffer: c,
Watch: watch,
eventsC: make(chan Event, watch.QueueSize),
ctx: closeCtx,
cancel: cancel,
capacity: watch.QueueSize,
}
c.Debugf("Add %v.", w)
select {
case w.eventsC <- Event{Type: OpInit}:
case <-c.ctx.Done():
return nil, trace.BadParameter("buffer is closed")
default:
c.Warningf("Closing %v, buffer overflow.", w)
w.Close()
return nil, trace.BadParameter("buffer overflow")
}
c.watchers.add(w)
return w, nil
}
func (c *CircularBuffer) removeWatcherWithLock(watcher *BufferWatcher) {
c.Lock()
defer c.Unlock()
if watcher == nil {
c.Warningf("Internal logic error: %v.", trace.DebugReport(trace.BadParameter("empty watcher")))
return
}
c.Debugf("Removed watcher %p via external close.", watcher)
found := c.watchers.rm(watcher)
if !found {
c.Debugf("Could not find watcher %v.", watcher)
}
}
// BufferWatcher is a watcher connected to the
// buffer and receiving fan-out events from the watcher
type BufferWatcher struct {
buffer *CircularBuffer
Watch
eventsC chan Event
ctx context.Context
cancel context.CancelFunc
capacity int
}
// String returns user-friendly representation
// of the buffer watcher
func (w *BufferWatcher) String() string {
return fmt.Sprintf("Watcher(name=%v, prefixes=%v, capacity=%v, size=%v)", w.Name, string(bytes.Join(w.Prefixes, []byte(", "))), w.capacity, len(w.eventsC))
}
// Events returns events channel
func (w *BufferWatcher) Events() <-chan Event {
return w.eventsC
}
// Done channel is closed when watcher is closed
func (w *BufferWatcher) Done() <-chan struct{} {
return w.ctx.Done()
}
// Close closes the watcher, could
// be called multiple times, removes the watcher
// from the buffer queue
func (w *BufferWatcher) Close() error {
w.closeAndRemove(removeAsync)
return nil
}
// closeWatcher closes watcher
func (w *BufferWatcher) closeWatcher() {
w.cancel()
}
const (
removeSync = true
removeAsync = false
)
// closeAndRemove closes the watcher, could
// be called multiple times, removes the watcher
// from the buffer queue synchronously (used in tests)
// or asyncronously, used in prod, to avoid potential deadlocks
func (w *BufferWatcher) closeAndRemove(sync bool) {
w.closeWatcher()
if sync {
w.buffer.removeWatcherWithLock(w)
} else {
go w.buffer.removeWatcherWithLock(w)
}
}
func newWatcherTree() *watcherTree {
return &watcherTree{
Tree: radix.New(),
}
}
type watcherTree struct {
*radix.Tree
}
// add adds buffer watcher to the tree
func (t *watcherTree) add(w *BufferWatcher) {
for _, p := range w.Prefixes {
prefix := string(p)
val, ok := t.Tree.Get(prefix)
var watchers []*BufferWatcher
if ok {
watchers = val.([]*BufferWatcher)
}
watchers = append(watchers, w)
t.Tree.Insert(prefix, watchers)
}
}
// rm removes the buffer watcher from the prefix tree
func (t *watcherTree) rm(w *BufferWatcher) bool {
if w == nil {
return false
}
var found bool
for _, p := range w.Prefixes {
prefix := string(p)
val, ok := t.Tree.Get(prefix)
if !ok {
continue
}
buffers := val.([]*BufferWatcher)
prevLen := len(buffers)
for i := range buffers {
if buffers[i] == w {
buffers = append(buffers[:i], buffers[i+1:]...)
found = true
break
}
}
if len(buffers) == 0 {
t.Tree.Delete(prefix)
} else if len(buffers) != prevLen {
t.Tree.Insert(prefix, buffers)
}
}
return found
}
// walkFn is a callback executed for every matching watcher
type walkFn func(w *BufferWatcher)
// walkPath walks the tree above the longest matching prefix
// and calls fn callback for every buffer watcher
func (t *watcherTree) walkPath(key string, fn walkFn) {
t.Tree.WalkPath(key, func(prefix string, val interface{}) bool {
watchers := val.([]*BufferWatcher)
for _, w := range watchers {
fn(w)
}
return false
})
}
// walk calls fn for every matching leaf of the tree
func (t *watcherTree) walk(fn walkFn) {
t.Tree.Walk(func(prefix string, val interface{}) bool {
watchers := val.([]*BufferWatcher)
for _, w := range watchers {
fn(w)
}
return false
})
}