/
distributer.go
563 lines (465 loc) · 18.6 KB
/
distributer.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
* Tencent is pleased to support the open source community by making 蓝鲸 available.
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 distribution
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"configcenter/src/common"
"configcenter/src/common/backbone"
"configcenter/src/common/blog"
"configcenter/src/common/metadata"
"configcenter/src/common/util"
"configcenter/src/common/watch"
etypes "configcenter/src/scene_server/event_server/types"
ewatcher "configcenter/src/scene_server/event_server/watcher"
"configcenter/src/source_controller/coreservice/event"
"configcenter/src/storage/dal"
"configcenter/src/storage/reflector"
"configcenter/src/storage/stream/types"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/redis.v5"
)
var (
// defaultListWatchPageSize is default page size of list watcher.
defaultListWatchPageSize = 500
// defaultWatchEventStepSize is default step size of watch event.
defaultWatchEventStepSize = 100
// defaultWatchEventLoopInterval is default watch event loop interval.
defaultWatchEventLoopInterval = 500 * time.Millisecond
// defaultMasterCheckInterval is default master state check interval.
defaultMasterCheckInterval = 3 * time.Second
)
// Distributor is event subscription distributer.
type Distributor struct {
ctx context.Context
engine *backbone.Engine
// db is cc main database.
db dal.RDB
// cache is cc redis client.
cache *redis.Client
// subWatcher is subscription watcher.
subWatcher reflector.Interface
// subscriptions is local subscriptions records, update by listwatcher, subscriptionid -> subscription.
subscriptions map[int64]*metadata.Subscription
// subscriptionsMu is subscriptions mutex.
subscriptionsMu sync.RWMutex
// subscribers is event subscribers map, key(event_type) -> subscriberIDs.
// could find subids by event type in records.
subscribers map[string][]int64
// subscribersMu is subscribers mutex.
subscribersMu sync.RWMutex
// waitForHandleResourceCursors is channel to wait for handling resource cursors.
waitForHandleResourceCursors chan struct{}
// resourceCursors is cursors for resource, CursorType -> oldest cursor.
resourceCursors map[watch.CursorType]*watch.Cursor
// resourceCursorsMu is resourceCursors mutex.
resourceCursorsMu sync.RWMutex
// eventHandler is event handler that handles all event senders.
eventHandler *EventHandler
// metrics.
// watchAndDistributeTotal is event watch and distribute total stat.
watchAndDistributeTotal *prometheus.CounterVec
// watchAndDistributeDuration is watch and distribute cost duration stat.
watchAndDistributeDuration *prometheus.HistogramVec
}
// NewDistributer creates a new Distributor instance.
func NewDistributer(ctx context.Context, engine *backbone.Engine, db dal.RDB, cache *redis.Client, subWatcher reflector.Interface) *Distributor {
return &Distributor{
ctx: ctx,
engine: engine,
db: db,
cache: cache,
subWatcher: subWatcher,
subscriptions: make(map[int64]*metadata.Subscription),
subscribers: make(map[string][]int64),
resourceCursors: make(map[watch.CursorType]*watch.Cursor),
waitForHandleResourceCursors: make(chan struct{}),
}
}
// registerMetrics registers prometheus metrics.
func (d *Distributor) registerMetrics() {
d.watchAndDistributeTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: fmt.Sprintf("%s_watch_dist_total", etypes.MetricsNamespacePrefix),
Help: "total number stats of watched and distributed event.",
},
[]string{"status"},
)
d.engine.Metric().Registry().MustRegister(d.watchAndDistributeTotal)
d.engine.Metric().Registry().MustRegister(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_subscriptions", etypes.MetricsNamespacePrefix),
Help: "current number of subscriptions.",
},
func() float64 {
d.subscriptionsMu.RLock()
defer d.subscriptionsMu.RUnlock()
return float64(len(d.subscriptions))
},
))
d.watchAndDistributeDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: fmt.Sprintf("%s_watch_dist_duration", etypes.MetricsNamespacePrefix),
Help: "watch and distribute duration of events.",
},
[]string{"status"},
)
d.engine.Metric().Registry().MustRegister(d.watchAndDistributeDuration)
}
// LoadSubscriptions loads all subscriptions in cc.
func (d *Distributor) LoadSubscriptions() error {
// list and watch subscriptions.
opts := types.Options{
EventStruct: &metadata.Subscription{},
Collection: common.BKTableNameSubscription,
}
// set event handler callback funcs.
listWatchCap := &reflector.Capable{
OnChange: reflector.OnChangeEvent{
OnLister: d.onUpsertSubscriptions,
OnListerDone: d.onListSubscriptionsDone,
OnAdd: d.onUpsertSubscriptions,
OnUpdate: d.onUpsertSubscriptions,
OnDelete: d.onDeleteSubscriptions,
},
}
// set list watch options.
listOpts := &types.ListWatchOptions{
Options: opts,
PageSize: &defaultListWatchPageSize,
}
// run to list and keep watching subscriptions.
return d.subWatcher.ListWatcher(context.Background(), listOpts, listWatchCap)
}
// onUpsertSubscriptions handles event that target subscription inserted or updated.
// It add or update subscription metadata and subscriber in local chains.
func (d *Distributor) onUpsertSubscriptions(e *types.Event) {
d.subscriptionsMu.Lock()
defer d.subscriptionsMu.Unlock()
subscription := e.Document.(*metadata.Subscription)
blog.Info("on upsert subscription, %+v", subscription)
oldSubscription, isExist := d.subscriptions[subscription.SubscriptionID]
if !isExist {
// insert.
d.subscriptions[subscription.SubscriptionID] = subscription
eventTypes := strings.Split(subscription.SubscriptionForm, ",")
// adds.
for _, eventType := range eventTypes {
d.addSubscriber(eventType, subscription.SubscriptionID)
}
} else {
// update.
if subscription.GetCacheKey() != oldSubscription.GetCacheKey() {
d.subscriptions[subscription.SubscriptionID] = subscription
}
// update event types.
oldEventTypes := strings.Split(oldSubscription.SubscriptionForm, ",")
eventTypes := strings.Split(subscription.SubscriptionForm, ",")
subs, plugs := util.CalSliceDiff(oldEventTypes, eventTypes)
// removes.
for _, eventType := range subs {
d.remSubscriber(eventType, subscription.SubscriptionID)
}
// adds.
for _, eventType := range plugs {
d.addSubscriber(eventType, subscription.SubscriptionID)
}
}
}
// onDeleteSubscriptions handles event that target subscription deleted.
// It delete local subscription metadata and removes subscriber in local chains.
func (d *Distributor) onDeleteSubscriptions(e *types.Event) {
d.subscriptionsMu.Lock()
defer d.subscriptionsMu.Unlock()
subscription := e.Document.(*metadata.Subscription)
blog.Info("on delete subscription, %+v", subscription)
if _, isExist := d.subscriptions[subscription.SubscriptionID]; isExist {
delete(d.subscriptions, subscription.SubscriptionID)
}
// removes.
eventTypes := strings.Split(subscription.SubscriptionForm, ",")
for _, eventType := range eventTypes {
d.remSubscriber(eventType, subscription.SubscriptionID)
}
}
// onListSubscriptionsDone handles event that after LIST-DONE at distributer first setup.
func (d *Distributor) onListSubscriptionsDone() {
d.subscriptionsMu.RLock()
defer d.subscriptionsMu.RUnlock()
blog.Info("distributer listwatch subscriptions at first setup done, count[%d]", len(d.subscriptions))
d.waitForHandleResourceCursors <- struct{}{}
}
// getResourceCursor parses all subscribers and find oldest watcher cursor for distributer which
// would watchs from the oldest cursor and distribute events to all subscribers by event handler.
func (d *Distributor) getResourceCursor(cursorType watch.CursorType) (*watch.Cursor, error) {
d.subscribersMu.RLock()
defer d.subscribersMu.RUnlock()
for eventType, subids := range d.subscribers {
if metadata.ParseCursorTypeFromEventType(eventType) != cursorType {
blog.Info("get resource cursor type from event type[%+v], not %+v, ignore it", eventType, cursorType)
continue
}
// range subscribers on target resource event.
for _, subid := range subids {
// get all cursors of subscribers, and try to find oldest cursor for target resource.
cursorKey := etypes.EventCacheSubscriberCursorKey(eventType, subid)
// get target subscriber cursor from cache.
val, err := d.cache.Get(cursorKey).Result()
if err != nil || len(val) == 0 {
blog.Warnf("get resource[%+v] cursor failed, query target subscriber cursor[%s], %+v, %+v", cursorType, cursorKey, val, err)
continue
}
// decode target cursor from cache.
subCursor := &watch.Cursor{}
if err := subCursor.Decode(val); err != nil {
blog.Warnf("get resource[%+v] cursor failed, invalid cursor[%s], %+v, %+v", cursorType, cursorKey, val, err)
continue
}
// update local resource oldest cursors.
d.resourceCursorsMu.Lock()
if oldestCursor, isExist := d.resourceCursors[cursorType]; !isExist {
d.resourceCursors[cursorType] = subCursor
} else {
// compare to get oldest cursor.
if subCursor.ClusterTime.Sec < oldestCursor.ClusterTime.Sec {
d.resourceCursors[cursorType] = subCursor
}
}
d.resourceCursorsMu.Unlock()
}
}
// return final oldest cursor for this cursortype.
return d.resourceCursors[cursorType], nil
}
func (d *Distributor) watchAndDistribute(cursorType watch.CursorType) error {
// get inner resource key.
resourcekey, err := event.GetResourceKeyWithCursorType(cursorType)
if err != nil {
return err
}
go func() {
for {
if !d.engine.ServiceManageInterface.IsMaster() {
blog.Warnf("not master eventserver node, skip watch and distribute!")
time.Sleep(defaultMasterCheckInterval)
continue
}
opts := &watch.WatchEventOptions{Resource: cursorType, Cursor: watch.NoEventCursor}
// try get newest watch cursor every time.
cursor, err := d.getResourceCursor(cursorType)
if err != nil {
blog.Warnf("watch and distribute for resource[%+v] failed, can't get subscriber cursor, %+v", cursorType, err)
}
if cursor != nil {
cursorStr, err := cursor.Encode()
if err != nil {
blog.Warnf("watch and distribute for resource[%+v] failed, can't encode subscriber cursor, %+v", cursorType, err)
} else {
opts.Cursor = cursorStr
}
}
// watch resource with cursor.
if err := d.watchAndDistributeWithCursor(cursorType, resourcekey, opts); err != nil {
d.watchAndDistributeTotal.WithLabelValues("WatchDistributeFailed").Inc()
blog.Errorf("watch and distribute for resource[%+v] failed, retry now, %+v", cursorType, err)
time.Sleep(defaultWatchEventLoopInterval)
}
}
}()
return nil
}
// watchAndDistributeWithCursor watches with oldest cursor(NoEventCursor when find oldest cursor faild or no-exist) and
// distributes events to event handler which would send event messages to subscribers.
func (d *Distributor) watchAndDistributeWithCursor(cursorType watch.CursorType, key event.Key, opts *watch.WatchEventOptions) error {
blog.Info("start watching and distribute for resource[%+v] with opts[%+v]", cursorType, opts)
defer blog.Info("stop watching and distribute for resource[%+v] with opts[%+v]", cursorType, opts)
// build a resource watcher.
watcher := ewatcher.NewWatcher(d.ctx, d.cache)
// start from this cursor.
startCursor := opts.Cursor
// can't find target old cursor, try to watch from last event node.
if startCursor == watch.NoEventCursor {
node, _, err := watcher.GetLatestEventDetail(key)
if err != nil {
if err != ewatcher.TailNodeNotExistError && err != ewatcher.NoEventsError && err != ewatcher.TailNodeTargetNotExistError {
return err
}
// event chain list is empty, which means no event and not be initialized, just watch and wait from head node.
startCursor = key.HeadKey()
blog.Info("watching for resource[%+v] from lastest event node, but no events now, try to watch from head node", cursorType)
} else {
// watch from lastest event node.
startCursor = node.Cursor
blog.Info("watching for resource[%+v] from cursor[%+v]", cursorType, startCursor)
}
}
// keep watching.
for {
// check master state.
if !d.engine.ServiceManageInterface.IsMaster() {
blog.Warnf("master state changed, stop watching!")
return errors.New("master state changed")
}
// new watching round.
cost := time.Now()
blog.Info("watching for resource[%+v] from cursor[%+v] now!", cursorType, startCursor)
targetNodes, err := watcher.GetNodesFromCursor(defaultWatchEventStepSize, startCursor, key)
d.watchAndDistributeDuration.WithLabelValues("WatchWithCursor").Observe(time.Since(cost).Seconds())
if err != nil {
if err == ewatcher.HeadNodeNotExistError {
// no origin events.
time.Sleep(defaultWatchEventLoopInterval)
continue
}
if err == ewatcher.StartCursorNotExistError {
// target cursor not exist, re-watch from head event node.
startCursor = key.HeadKey()
time.Sleep(defaultWatchEventLoopInterval)
continue
}
return err
}
if len(targetNodes) == 0 {
// no events from origin.
time.Sleep(defaultWatchEventLoopInterval)
continue
}
lastNode := targetNodes[len(targetNodes)-1]
blog.Info("watching for resource[%+v], cursor[%+v], new nodes num[%d]", cursorType, startCursor, len(targetNodes))
// hit target event types.
hitNodes := watcher.GetHitNodeWithEventType(targetNodes, opts.EventTypes)
if len(hitNodes) == 0 {
// range some nodes but no any hits, so reset the cursor to current cursor,
// and try to get hit nodes in next round.
startCursor = lastNode.Cursor
time.Sleep(defaultWatchEventLoopInterval)
continue
}
blog.Info("watching for resource[%+v], cursor[%+v], new hit nodes num[%d]", cursorType, startCursor, len(hitNodes))
// get final hit event datas.
cost = time.Now()
eventDetailStrs, err := watcher.GetEventDetailsWithCursorNodes(hitNodes, key)
d.watchAndDistributeDuration.WithLabelValues("GetEventDetailsWithCursor").Observe(time.Since(cost).Seconds())
if err != nil {
d.watchAndDistributeTotal.WithLabelValues("GetEventDetailsWithCursorFailed").Inc()
blog.Errorf("watching for resource[%+v], get event details with cursor[%+v] failed, %+v", cursorType, startCursor, err)
// can't get final target event datas, and try again later.
time.Sleep(defaultWatchEventLoopInterval)
continue
}
// distribute to subscriber senders.
cost = time.Now()
err = d.eventHandler.Handle(hitNodes, eventDetailStrs, opts)
d.watchAndDistributeDuration.WithLabelValues("HandleEvents").Observe(time.Since(cost).Seconds())
if err != nil {
d.watchAndDistributeTotal.WithLabelValues("HandleEventsFailed").Inc()
blog.Errorf("distribute resource[%+v] %d events to handler failed, %+v", cursorType, len(eventDetailStrs), err)
// get hit nodes success, but can't distribute to event handler, do not reset cursor,
// try to re-distribute to handler in next round.
time.Sleep(defaultWatchEventLoopInterval)
continue
}
d.watchAndDistributeTotal.WithLabelValues("Success").Inc()
// distribute success, reset cursor and watch in next round.
blog.Info("watching and distribute for resource[%+v], handled %d nodes successfully", cursorType, len(eventDetailStrs))
startCursor = lastNode.Cursor
}
return nil
}
// addSubscriber adds new subscriber with target event type.
func (d *Distributor) addSubscriber(eventType string, subid int64) {
d.subscribersMu.Lock()
defer d.subscribersMu.Unlock()
blog.Info("add event subscriber, type[%s] subid[%d]", eventType, subid)
if _, isExist := d.subscribers[eventType]; !isExist {
d.subscribers[eventType] = []int64{}
}
subscribers := d.subscribers[eventType]
for _, id := range subscribers {
if subid == id {
// already exist.
return
}
}
d.subscribers[eventType] = append(d.subscribers[eventType], subid)
}
// remSubscriber removes subscriber with target event type.
func (d *Distributor) remSubscriber(eventType string, subid int64) {
d.subscribersMu.Lock()
defer d.subscribersMu.Unlock()
blog.Info("remove event subscriber, type[%s] subid[%d]", eventType, subid)
if _, isExist := d.subscribers[eventType]; !isExist {
return
}
subscribers := d.subscribers[eventType]
updated := []int64{}
for _, id := range subscribers {
if subid != id {
updated = append(updated, id)
}
}
d.subscribers[eventType] = updated
}
// FindSubscribers returns all subscribers on event type.
func (d *Distributor) FindSubscribers(eventType string) []int64 {
d.subscribersMu.RLock()
defer d.subscribersMu.RUnlock()
subscribers := d.subscribers[eventType]
if subscribers == nil {
return []int64{}
}
return subscribers
}
// FindSubscription return target subscription base on subid.
func (d *Distributor) FindSubscription(subid int64) *metadata.Subscription {
d.subscriptionsMu.RLock()
defer d.subscriptionsMu.RUnlock()
return d.subscriptions[subid]
}
// ListSubscriptionIDs return all subscription ids.
func (d *Distributor) ListSubscriptionIDs() []int64 {
d.subscriptionsMu.RLock()
defer d.subscriptionsMu.RUnlock()
subids := []int64{}
for subid := range d.subscriptions {
subids = append(subids, subid)
}
return subids
}
// Start starts the Distributor, it would load all subscriptions in listwatch mode, and handle runtime
// subscription update messages, push event to subscribers when tatget event happend.
func (d *Distributor) Start(eventHandler *EventHandler) error {
d.eventHandler = eventHandler
// register metrics.
d.registerMetrics()
// list and keep watching subscriptions.
if err := d.LoadSubscriptions(); err != nil {
return fmt.Errorf("load subscriptions at first setups failed, %+v", err)
}
// wait for LIST-DONE to handle resource cursors.
<-d.waitForHandleResourceCursors
// run event hander.
if err := d.eventHandler.Start(); err != nil {
return fmt.Errorf("start event handler failed, %+v", err)
}
// range all resource cursors and watch to distribute.
for _, cursorType := range watch.ListEventCallbackCursorTypes() {
if err := d.watchAndDistribute(cursorType); err != nil {
return fmt.Errorf("watch and distribute resource events failed, %+v", err)
}
}
return nil
}