-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch.go
More file actions
269 lines (236 loc) · 7.5 KB
/
dispatch.go
File metadata and controls
269 lines (236 loc) · 7.5 KB
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
package informer
import (
"fmt"
"sync"
"sync/atomic"
"time"
"k8s.io/client-go/tools/cache"
mcstorage "github.com/kplane-dev/storage"
)
// MultiClusterEventHandler receives events with cluster identity intact.
// Objects are *storage.ObjectWithClusterIdentity envelopes.
type MultiClusterEventHandler interface {
OnAdd(obj *mcstorage.ObjectWithClusterIdentity, isInInitialList bool)
OnUpdate(oldObj, newObj *mcstorage.ObjectWithClusterIdentity)
OnDelete(obj *mcstorage.ObjectWithClusterIdentity)
}
// MultiClusterEventHandlerFuncs is a convenience adapter for MultiClusterEventHandler.
type MultiClusterEventHandlerFuncs struct {
AddFunc func(obj *mcstorage.ObjectWithClusterIdentity, isInInitialList bool)
UpdateFunc func(oldObj, newObj *mcstorage.ObjectWithClusterIdentity)
DeleteFunc func(obj *mcstorage.ObjectWithClusterIdentity)
}
func (f MultiClusterEventHandlerFuncs) OnAdd(obj *mcstorage.ObjectWithClusterIdentity, isInInitialList bool) {
if f.AddFunc != nil {
f.AddFunc(obj, isInInitialList)
}
}
func (f MultiClusterEventHandlerFuncs) OnUpdate(oldObj, newObj *mcstorage.ObjectWithClusterIdentity) {
if f.UpdateFunc != nil {
f.UpdateFunc(oldObj, newObj)
}
}
func (f MultiClusterEventHandlerFuncs) OnDelete(obj *mcstorage.ObjectWithClusterIdentity) {
if f.DeleteFunc != nil {
f.DeleteFunc(obj)
}
}
// HandlerRegistration is returned when a handler is registered. It can be used
// to check sync status or remove the handler.
type HandlerRegistration interface {
// HasSynced returns true when the parent informer has completed its
// initial sync and this handler has been marked as synced. Handlers
// registered before initial sync completes are marked when the
// initial-events-end bookmark arrives. Handlers registered after sync
// are marked immediately upon registration.
HasSynced() bool
// Remove removes this handler registration.
Remove() error
}
type handlerRegistry struct {
mu sync.RWMutex
nextID int
allHandlers map[int]*registeredHandler
clusterHandlers map[string]map[int]*registeredHandler
parentSynced func() bool
}
type registeredHandler struct {
id int
clusterID string // empty = all-clusters handler
handler interface{}
synced atomic.Bool
}
func newHandlerRegistry(parentSynced func() bool) *handlerRegistry {
return &handlerRegistry{
allHandlers: make(map[int]*registeredHandler),
clusterHandlers: make(map[string]map[int]*registeredHandler),
parentSynced: parentSynced,
}
}
// addAllClusters registers a MultiClusterEventHandler.
// If the informer has already completed initial sync, the handler is marked
// synced immediately — there is no "initial list" to replay for late registrations.
func (r *handlerRegistry) addAllClusters(handler MultiClusterEventHandler) *handlerRegistration {
r.mu.Lock()
defer r.mu.Unlock()
id := r.nextID
r.nextID++
h := ®isteredHandler{id: id, handler: handler}
if r.parentSynced() {
h.synced.Store(true)
}
r.allHandlers[id] = h
return &handlerRegistration{registry: r, handler: h}
}
// addForCluster registers a cache.ResourceEventHandler for a specific cluster.
// If the informer has already completed initial sync, the handler is marked
// synced immediately — there is no "initial list" to replay for late registrations.
func (r *handlerRegistry) addForCluster(clusterID string, handler cache.ResourceEventHandler) *handlerRegistration {
r.mu.Lock()
defer r.mu.Unlock()
id := r.nextID
r.nextID++
h := ®isteredHandler{id: id, clusterID: clusterID, handler: handler}
if r.parentSynced() {
h.synced.Store(true)
}
if r.clusterHandlers[clusterID] == nil {
r.clusterHandlers[clusterID] = make(map[int]*registeredHandler)
}
r.clusterHandlers[clusterID][id] = h
return &handlerRegistration{registry: r, handler: h}
}
func (r *handlerRegistry) remove(h *registeredHandler) {
r.mu.Lock()
defer r.mu.Unlock()
if h.clusterID == "" {
delete(r.allHandlers, h.id)
} else {
if m, ok := r.clusterHandlers[h.clusterID]; ok {
delete(m, h.id)
if len(m) == 0 {
delete(r.clusterHandlers, h.clusterID)
}
}
}
}
func (r *handlerRegistry) dispatchAdd(env *mcstorage.ObjectWithClusterIdentity, isInitialList bool) {
r.mu.RLock()
defer r.mu.RUnlock()
for _, h := range r.allHandlers {
if mch, ok := h.handler.(MultiClusterEventHandler); ok {
mch.OnAdd(env, isInitialList)
}
}
if handlers, ok := r.clusterHandlers[env.ClusterID]; ok {
obj := unwrapCacheableObject(env.Object)
for _, h := range handlers {
if reh, ok := h.handler.(cache.ResourceEventHandler); ok {
reh.OnAdd(obj, isInitialList)
}
}
}
}
func (r *handlerRegistry) dispatchUpdate(oldEnv, newEnv *mcstorage.ObjectWithClusterIdentity) {
r.mu.RLock()
defer r.mu.RUnlock()
for _, h := range r.allHandlers {
if mch, ok := h.handler.(MultiClusterEventHandler); ok {
mch.OnUpdate(oldEnv, newEnv)
}
}
oldCluster := oldEnv.ClusterID
newCluster := newEnv.ClusterID
if oldCluster == newCluster {
// Same cluster: dispatch update to per-cluster handlers
if handlers, ok := r.clusterHandlers[newCluster]; ok {
oldObj := unwrapCacheableObject(oldEnv.Object)
newObj := unwrapCacheableObject(newEnv.Object)
for _, h := range handlers {
if reh, ok := h.handler.(cache.ResourceEventHandler); ok {
reh.OnUpdate(oldObj, newObj)
}
}
}
} else {
// Cluster changed (rare but defensive): old cluster sees delete, new sees add
if handlers, ok := r.clusterHandlers[oldCluster]; ok {
oldObj := unwrapCacheableObject(oldEnv.Object)
for _, h := range handlers {
if reh, ok := h.handler.(cache.ResourceEventHandler); ok {
reh.OnDelete(oldObj)
}
}
}
if handlers, ok := r.clusterHandlers[newCluster]; ok {
newObj := unwrapCacheableObject(newEnv.Object)
for _, h := range handlers {
if reh, ok := h.handler.(cache.ResourceEventHandler); ok {
reh.OnAdd(newObj, false)
}
}
}
}
}
func (r *handlerRegistry) dispatchDelete(env *mcstorage.ObjectWithClusterIdentity) {
r.mu.RLock()
defer r.mu.RUnlock()
for _, h := range r.allHandlers {
if mch, ok := h.handler.(MultiClusterEventHandler); ok {
mch.OnDelete(env)
}
}
if handlers, ok := r.clusterHandlers[env.ClusterID]; ok {
obj := unwrapCacheableObject(env.Object)
for _, h := range handlers {
if reh, ok := h.handler.(cache.ResourceEventHandler); ok {
reh.OnDelete(obj)
}
}
}
}
// markAllSynced marks all currently registered handlers as synced.
func (r *handlerRegistry) markAllSynced() {
r.mu.RLock()
defer r.mu.RUnlock()
for _, h := range r.allHandlers {
h.synced.Store(true)
}
for _, handlers := range r.clusterHandlers {
for _, h := range handlers {
h.synced.Store(true)
}
}
}
// handlerRegistration implements HandlerRegistration.
type handlerRegistration struct {
registry *handlerRegistry
handler *registeredHandler
}
func (r *handlerRegistration) HasSynced() bool {
return r.registry.parentSynced() && r.handler.synced.Load()
}
func (r *handlerRegistration) HasSyncedChecker() cache.DoneChecker {
return ®istrationDoneChecker{reg: r}
}
func (r *handlerRegistration) Remove() error {
r.registry.remove(r.handler)
return nil
}
// registrationDoneChecker implements cache.DoneChecker for a handler registration.
type registrationDoneChecker struct {
reg *handlerRegistration
}
func (d *registrationDoneChecker) Name() string {
return fmt.Sprintf("handler-%d", d.reg.handler.id)
}
func (d *registrationDoneChecker) Done() <-chan struct{} {
ch := make(chan struct{})
go func() {
for !d.reg.HasSynced() {
time.Sleep(10 * time.Millisecond)
}
close(ch)
}()
return ch
}