forked from gravitational/teleport
/
fanout.go
480 lines (435 loc) · 12.8 KB
/
fanout.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
/*
Copyright 2020 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 services
import (
"context"
"sync"
"sync/atomic"
"github.com/gravitational/trace"
"github.com/zmb3/teleport/api/types"
)
const defaultQueueSize = 64
type fanoutEntry struct {
kind types.WatchKind
watcher *fanoutWatcher
}
// Fanout is a helper which allows a stream of events to be fanned-out to many
// watchers. Used by the cache layer to forward events.
type Fanout struct {
mu sync.Mutex
init, closed bool
watchers map[string][]fanoutEntry
// eventsCh is used in tests
eventsCh chan FanoutEvent
}
// NewFanout creates a new Fanout instance in an uninitialized
// state. Until initialized, watchers will be queued but no
// events will be sent.
func NewFanout(eventsCh ...chan FanoutEvent) *Fanout {
f := &Fanout{
watchers: make(map[string][]fanoutEntry),
}
if len(eventsCh) != 0 {
f.eventsCh = eventsCh[0]
}
return f
}
const (
// EventWatcherRemoved is emitted when event watcher has been removed
EventWatcherRemoved = iota
)
// FanoutEvent is used in tests
type FanoutEvent struct {
// Kind is event kind
Kind int
}
// NewWatcher attaches a new watcher to this fanout instance.
func (f *Fanout) NewWatcher(ctx context.Context, watch types.Watch) (types.Watcher, error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.closed {
return nil, trace.Errorf("cannot register watcher, fanout system closed")
}
w, err := newFanoutWatcher(ctx, f, watch)
if err != nil {
return nil, trace.Wrap(err)
}
if f.init {
// fanout is already initialized; emit init event immediately.
if !w.init() {
w.cancel()
return nil, trace.BadParameter("failed to send init event")
}
}
f.addWatcher(w)
return w, nil
}
// SetInit sets Fanout into an initialized state, sending OpInit events
// to any watchers which were added prior to initialization.
func (f *Fanout) SetInit() {
f.mu.Lock()
defer f.mu.Unlock()
if f.init {
return
}
for _, entries := range f.watchers {
var remove []*fanoutWatcher
for _, entry := range entries {
if !entry.watcher.init() {
remove = append(remove, entry.watcher)
}
}
for _, w := range remove {
f.removeWatcher(w)
w.cancel()
}
}
f.init = true
}
func filterEventSecrets(event types.Event) types.Event {
r, ok := event.Resource.(types.ResourceWithSecrets)
if !ok {
return event
}
event.Resource = r.WithoutSecrets()
return event
}
// Len returns a total count of watchers
func (f *Fanout) Len() int {
f.mu.Lock()
defer f.mu.Unlock()
var count int
for key := range f.watchers {
count += len(f.watchers[key])
}
return count
}
func (f *Fanout) trySendEvent(e FanoutEvent) {
if f.eventsCh == nil {
return
}
select {
case f.eventsCh <- e:
default:
}
}
// Emit broadcasts events to all matching watchers that have been attached
// to this fanout instance.
func (f *Fanout) Emit(events ...types.Event) {
f.mu.Lock()
defer f.mu.Unlock()
if !f.init {
panic("Emit called on uninitialized fanout instance")
}
for _, fullEvent := range events {
// by default, we operate on a version of the event which
// has had secrets filtered out.
event := filterEventSecrets(fullEvent)
var remove []*fanoutWatcher
// If the event has no associated resource, emit it to all watchers.
if event.Resource == nil {
for _, entries := range f.watchers {
for _, entry := range entries {
if err := entry.watcher.emit(event); err != nil {
entry.watcher.setError(err)
remove = append(remove, entry.watcher)
}
}
}
} else {
for _, entry := range f.watchers[event.Resource.GetKind()] {
match, err := entry.kind.Matches(event)
if err != nil {
entry.watcher.setError(err)
remove = append(remove, entry.watcher)
continue
}
if !match {
continue
}
emitEvent := event
// if this entry loads secrets, emit the
// full unfiltered event.
if entry.kind.LoadSecrets {
emitEvent = fullEvent
}
if err := entry.watcher.emit(emitEvent); err != nil {
entry.watcher.setError(err)
remove = append(remove, entry.watcher)
}
}
}
for _, w := range remove {
f.removeWatcher(w)
w.cancel()
}
}
}
// Reset closes all attached watchers and places the fanout instance
// into an uninitialized state. Reset may be called on an uninitialized
// fanout instance to remove "queued" watchers.
func (f *Fanout) Reset() {
watchers := f.takeAndReset()
// goroutines run with a "happens after" releationship to the
// expressions that create them. since we move ownership of the
// old watcher mapping prior to spawning this goroutine, we are
// "safe" to modify it without worrying about locking. because
// we don't continue to hold the lock in the foreground goroutine,
// this fanout instance may permit new events/registrations/inits/resets
// while the old watchers are still being closed. this is fine, since
// the aformentioned move guarantees that these old watchers aren't
// going to observe any of the new state transitions.
go closeWatchers(watchers)
}
func (f *Fanout) takeAndReset() map[string][]fanoutEntry {
f.mu.Lock()
defer f.mu.Unlock()
watchersToClose := f.watchers
f.watchers = make(map[string][]fanoutEntry)
f.init = false
return watchersToClose
}
// Close permanently closes the fanout. Existing watchers will be
// closed and no new watchers will be added.
func (f *Fanout) Close() {
watchers := f.takeAndClose()
// goroutines run with a "happens after" releationship to the
// expressions that create them. since we move ownership of the
// old watcher mapping prior to spawning this goroutine, we are
// "safe" to modify it without worrying about locking. because
// we don't continue to hold the lock in the foreground goroutine,
// this fanout instance may permit new events/registrations/inits/resets
// while the old watchers are still being closed. this is fine, since
// the aformentioned move guarantees that these old watchers aren't
// going to observe any of the new state transitions.
go closeWatchers(watchers)
}
func (f *Fanout) takeAndClose() map[string][]fanoutEntry {
f.mu.Lock()
defer f.mu.Unlock()
watchersToClose := f.watchers
f.watchers = make(map[string][]fanoutEntry)
f.closed = true
return watchersToClose
}
func closeWatchers(watchersToClose map[string][]fanoutEntry) {
for _, entries := range watchersToClose {
for _, entry := range entries {
entry.watcher.cancel()
}
}
}
func (f *Fanout) addWatcher(w *fanoutWatcher) {
for _, kind := range w.watch.Kinds {
entries := f.watchers[kind.Kind]
entries = append(entries, fanoutEntry{
kind: kind,
watcher: w,
})
f.watchers[kind.Kind] = entries
}
}
func (f *Fanout) removeWatcherWithLock(w *fanoutWatcher) {
if w == nil {
return
}
f.mu.Lock()
defer f.mu.Unlock()
f.removeWatcher(w)
}
func (f *Fanout) removeWatcher(w *fanoutWatcher) {
for _, kind := range w.watch.Kinds {
entries := f.watchers[kind.Kind]
Inner:
for i, entry := range entries {
if entry.watcher == w {
entries = append(entries[:i], entries[i+1:]...)
f.trySendEvent(FanoutEvent{Kind: EventWatcherRemoved})
break Inner
}
}
switch len(entries) {
case 0:
delete(f.watchers, kind.Kind)
default:
f.watchers[kind.Kind] = entries
}
}
}
func newFanoutWatcher(ctx context.Context, f *Fanout, watch types.Watch) (*fanoutWatcher, error) {
if len(watch.Kinds) < 1 {
return nil, trace.BadParameter("must specify at least one resource kind to watch")
}
ctx, cancel := context.WithCancel(ctx)
if watch.QueueSize < 1 {
watch.QueueSize = defaultQueueSize
}
return &fanoutWatcher{
fanout: f,
watch: watch,
eventC: make(chan types.Event, watch.QueueSize),
cancel: cancel,
ctx: ctx,
}, nil
}
type fanoutWatcher struct {
emux sync.Mutex
fanout *Fanout
err error
watch types.Watch
eventC chan types.Event
cancel context.CancelFunc
ctx context.Context
initOnce sync.Once
initOk bool
}
// init transmits the OpInit event. safe to double-call.
func (w *fanoutWatcher) init() (ok bool) {
w.initOnce.Do(func() {
select {
case w.eventC <- types.Event{Type: types.OpInit}:
w.initOk = true
default:
w.initOk = false
}
})
return w.initOk
}
func (w *fanoutWatcher) emit(event types.Event) error {
select {
case <-w.ctx.Done():
return trace.Wrap(w.ctx.Err(), "watcher closed")
case w.eventC <- event:
return nil
default:
return trace.BadParameter("buffer overflow")
}
}
func (w *fanoutWatcher) Events() <-chan types.Event {
return w.eventC
}
func (w *fanoutWatcher) Done() <-chan struct{} {
return w.ctx.Done()
}
func (w *fanoutWatcher) Close() error {
w.cancel()
// goroutine is to prevent accidental
// deadlock, if watcher.Close is called
// under Fanout mutex
go w.fanout.removeWatcherWithLock(w)
return nil
}
func (w *fanoutWatcher) setError(err error) {
w.emux.Lock()
defer w.emux.Unlock()
w.err = err
}
func (w *fanoutWatcher) Error() error {
w.emux.Lock()
defer w.emux.Unlock()
if w.err != nil {
return w.err
}
select {
case <-w.Done():
return trace.Errorf("watcher closed")
default:
return nil
}
}
// fanoutSetSize is the number of members in a fanout set. selected based on some experimentation with
// the FanoutSetRegistration benchmark. This value keeps 100K concurrent registrations well under 1s.
const fanoutSetSize = 128
// FanoutSet is a collection of separate Fanout instances. It exposes an identical API, and "load balances"
// watcher registration across the enclosed instances. In very large clusters it is possible for tens of
// thousands of nodes to simultaneously request watchers. This can cause serious contention issues. FanoutSet is
// a simple but effective solution to that problem.
type FanoutSet struct {
// rw mutex is used to ensure that Close and Reset operations are exclusive,
// since these operations close watchers. Enforcing this property isn't strictly
// necessary, but it prevents a scenario where watchers might observe a reset/close,
// attempt re-registration, and observe the *same* reset/close again. This isn't
// necessarily a problem, but it might confuse attempts to debug other event-system
// issues, so we choose to avoid it.
rw sync.RWMutex
counter atomic.Uint64
members []*Fanout
}
// NewFanoutSet creates a new FanoutSet instance in an uninitialized
// state. Until initialized, watchers will be queued but no
// events will be sent.
func NewFanoutSet() *FanoutSet {
members := make([]*Fanout, 0, fanoutSetSize)
for i := 0; i < fanoutSetSize; i++ {
members = append(members, NewFanout())
}
return &FanoutSet{
members: members,
}
}
// NewWatcher attaches a new watcher to a fanout instance.
func (s *FanoutSet) NewWatcher(ctx context.Context, watch types.Watch) (types.Watcher, error) {
s.rw.RLock() // see field-level docks for locking model
defer s.rw.RUnlock()
fi := int(s.counter.Add(1) % uint64(len(s.members)))
return s.members[fi].NewWatcher(ctx, watch)
}
// SetInit sets the Fanout instances into an initialized state, sending OpInit
// events to any watchers which were added prior to initialization.
func (s *FanoutSet) SetInit() {
s.rw.RLock() // see field-level docks for locking model
defer s.rw.RUnlock()
for _, f := range s.members {
f.SetInit()
}
}
// Emit broadcasts events to all matching watchers that have been attached
// to this fanout set.
func (s *FanoutSet) Emit(events ...types.Event) {
s.rw.RLock() // see field-level docks for locking model
defer s.rw.RUnlock()
for _, f := range s.members {
f.Emit(events...)
}
}
// Reset closes all attached watchers and places the fanout instances
// into an uninitialized state. Reset may be called on an uninitialized
// fanout set to remove "queued" watchers.
func (s *FanoutSet) Reset() {
s.rw.Lock() // see field-level docks for locking model
defer s.rw.Unlock()
var watcherMappings []map[string][]fanoutEntry
for _, f := range s.members {
watcherMappings = append(watcherMappings, f.takeAndReset())
}
go func() {
for _, watchers := range watcherMappings {
closeWatchers(watchers)
}
}()
}
// Close permanently closes the fanout. Existing watchers will be
// closed and no new watchers will be added.
func (s *FanoutSet) Close() {
s.rw.Lock() // see field-level docks for locking model
defer s.rw.Unlock()
var watcherMappings []map[string][]fanoutEntry
for _, f := range s.members {
watcherMappings = append(watcherMappings, f.takeAndClose())
}
go func() {
for _, watchers := range watcherMappings {
closeWatchers(watchers)
}
}()
}