/
event.go
505 lines (411 loc) · 15.2 KB
/
event.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
/*
* 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"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"configcenter/src/common"
"configcenter/src/common/backbone"
"configcenter/src/common/blog"
ccjson "configcenter/src/common/json"
"configcenter/src/common/metadata"
"configcenter/src/common/watch"
etypes "configcenter/src/scene_server/event_server/types"
"configcenter/src/storage/stream/types"
"github.com/prometheus/client_golang/prometheus"
"github.com/tidwall/gjson"
"gopkg.in/redis.v5"
)
const (
// defaultTransTimeout is default timeout for trans event data from base queue to identifier
// duplicate queue.
defaultTransTimeout = 60 * time.Second
// defaultHandleTimeout is default timeout for event handle.
defaultHandleTimeout = time.Second
// defaultFusingEventExpireSec is default expire second num for event.
defaultFusingEventExpireSec = 3 * 60 * 60
// defaultCleanCheckInterval is default interval for cleaning check.
defaultCleanCheckInterval = time.Minute
// defaultPushersCheckInterval is default interval for pushers check.
defaultPushersCheckInterval = 10 * time.Second
// defaultCleanTrimThreshold is default threshold for cleaning trim.
defaultCleanTrimThreshold = 2 * 10000
// defaultCleanDelThreshold is default threshold for cleaning del.
defaultCleanDelThreshold = 5 * 10000
// defaultCleanUnit is default clean unit count for cleaning.
defaultCleanUnit = 10000
)
// EventHandler manages all event pushers, and update pushers in dynamic mode,
// when there are events need to be sent, the pusher would check master state and send
// message to subscriber in callback or not.
type EventHandler struct {
ctx context.Context
engine *backbone.Engine
// cache is cc redis client.
cache *redis.Client
// pushers is local event pushers, update in dynamic mode, subid -> EventPushers.
pushers map[int64]*EventPusher
// pushersMu make pushers ops safe.
pushersMu sync.RWMutex
// distributer handles all events distribution.
distributer *Distributor
// metrics.
// eventHandleTotal is event handle total stat.
eventHandleTotal *prometheus.CounterVec
// eventHandleDuration is event handle cost duration stat.
eventHandleDuration *prometheus.HistogramVec
// pusherHandleTotal is event pusher handle total stat.
pusherHandleTotal *prometheus.CounterVec
// pusherHandleDuration is event pusher cost duration stat.
pusherHandleDuration *prometheus.HistogramVec
}
// NewEventHandler creates new EventHandler object.
func NewEventHandler(ctx context.Context, engine *backbone.Engine, cache *redis.Client) *EventHandler {
return &EventHandler{
ctx: ctx,
engine: engine,
cache: cache,
pushers: make(map[int64]*EventPusher),
}
}
// registerMetrics registers prometheus metrics.
func (h *EventHandler) registerMetrics() {
h.eventHandleTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: fmt.Sprintf("%s_event_handle_total", etypes.MetricsNamespacePrefix),
Help: "total number stats of handle event.",
},
[]string{"status"},
)
h.engine.Metric().Registry().MustRegister(h.eventHandleTotal)
h.engine.Metric().Registry().MustRegister(prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: fmt.Sprintf("%s_pushers", etypes.MetricsNamespacePrefix),
Help: "current number of pushers.",
},
func() float64 {
h.pushersMu.RLock()
defer h.pushersMu.RUnlock()
return float64(len(h.pushers))
},
))
h.eventHandleDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: fmt.Sprintf("%s_event_handle_duration", etypes.MetricsNamespacePrefix),
Help: "handle duration of events.",
},
[]string{"status"},
)
h.engine.Metric().Registry().MustRegister(h.eventHandleDuration)
h.pusherHandleTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: fmt.Sprintf("%s_pusher_handle_total", etypes.MetricsNamespacePrefix),
Help: "total number stats of event pusher.",
},
[]string{"status"},
)
h.engine.Metric().Registry().MustRegister(h.pusherHandleTotal)
h.pusherHandleDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: fmt.Sprintf("%s_pusher_handle_duration", etypes.MetricsNamespacePrefix),
Help: "pusher duration of events.",
},
[]string{"status"},
)
h.engine.Metric().Registry().MustRegister(h.pusherHandleDuration)
}
// SetDistributer setups distributer to event handler.
func (h *EventHandler) SetDistributer(distributer *Distributor) {
h.distributer = distributer
}
// Handle handles events distributed by distributer, add events to real handle queue to
// handle host identifier infos. Handler would find all relate subscribers and send event message
// to target subscribers by callback.
func (h *EventHandler) Handle(nodes []*watch.ChainNode, eventDetailStrs []string, opts *watch.WatchEventOptions) error {
blog.Info("handle new events now, count[%d]", len(eventDetailStrs))
defer blog.Info("handle new events done, count[%d]", len(eventDetailStrs))
for idx, eventDetailStr := range eventDetailStrs {
updateFields := []string{}
for updateField, _ := range gjson.Get(eventDetailStr, "update_fields").Map() {
updateFields = append(updateFields, updateField)
}
deletedFields := []string{}
for _, deletedField := range gjson.Get(eventDetailStr, "deleted_fields").Array() {
deletedFields = append(deletedFields, deletedField.String())
}
jsonDetailStr := types.GetEventDetail(eventDetailStr)
cut := ccjson.CutJsonDataWithFields(&jsonDetailStr, opts.Fields)
event := &watch.WatchEventDetail{
Cursor: nodes[idx].Cursor,
Resource: opts.Resource,
EventType: nodes[idx].EventType,
Detail: watch.JsonString(*cut),
}
eventInst := &metadata.EventInst{
ID: h.cache.Incr(etypes.EventCacheEventIDKey).Val(),
Cursor: event.Cursor,
UpdateFields: updateFields,
DeletedFields: deletedFields,
}
cursor := &watch.Cursor{}
if err := cursor.Decode(eventInst.Cursor); err == nil {
timeNow := metadata.ParseTimeInUnixTS(int64(cursor.ClusterTime.Sec), int64(cursor.ClusterTime.Nano))
eventInst.ActionTime = timeNow
}
switch event.Resource {
case watch.Host:
eventInst.EventType = metadata.EventTypeInstData
eventInst.ObjType = common.BKInnerObjIDHost
case watch.ModuleHostRelation:
eventInst.EventType = metadata.EventTypeRelation
eventInst.ObjType = metadata.EventObjTypeModuleTransfer
case watch.Biz:
eventInst.EventType = metadata.EventTypeInstData
eventInst.ObjType = common.BKInnerObjIDApp
case watch.Set:
eventInst.EventType = metadata.EventTypeInstData
eventInst.ObjType = common.BKInnerObjIDSet
case watch.Module:
eventInst.EventType = metadata.EventTypeInstData
eventInst.ObjType = common.BKInnerObjIDModule
case watch.Process:
eventInst.EventType = metadata.EventTypeInstData
eventInst.ObjType = common.BKInnerObjIDProc
case watch.ProcessInstanceRelation:
eventInst.EventType = metadata.EventTypeRelation
eventInst.ObjType = metadata.EventObjTypeProcModule
case watch.ObjectBase:
eventInst.EventType = metadata.EventTypeInstData
jsonStr := event.Detail.(watch.JsonString)
detailBytes, _ := jsonStr.MarshalJSON()
eventInst.ObjType = gjson.Get(string(detailBytes), "bk_obj_id").String()
default:
continue
}
switch event.EventType {
case watch.Create:
eventInst.Action = metadata.EventActionCreate
eventInst.Data = []metadata.EventData{metadata.EventData{CurData: event.Detail}}
case watch.Update:
eventInst.Action = metadata.EventActionUpdate
eventInst.Data = []metadata.EventData{metadata.EventData{CurData: event.Detail}}
case watch.Delete:
eventInst.Action = metadata.EventActionDelete
eventInst.Data = []metadata.EventData{metadata.EventData{PreData: event.Detail}}
default:
continue
}
eventData, err := json.Marshal(eventInst)
if err != nil {
blog.Errorf("marshal event data failed, %+v, %+v", eventInst, err)
continue
}
blog.Info("handle new event %+v", eventInst)
// push event data to main event queue.
cost := time.Now()
err = h.cache.LPush(etypes.EventCacheEventQueueKey, eventData).Err()
h.eventHandleDuration.WithLabelValues("PushEventInst").Observe(time.Since(cost).Seconds())
if err != nil {
h.eventHandleTotal.WithLabelValues("PushEventInstFailed").Inc()
blog.Errorf("push new event data to event queue failed, %+v, %+v", eventInst, err)
continue
}
}
return nil
}
// popEvent keeps poping event from main event queue and add event to duplicated queue. identifier would
// handle host events and add back to main event queue with another level EVENT.
func (h *EventHandler) popEvent() (*metadata.EventInst, error) {
var eventStr string
// pop from main event queue and re-add to duplicated queue for identifier.
cost := time.Now()
err := h.cache.BRPopLPush(etypes.EventCacheEventQueueKey,
etypes.EventCacheEventQueueDuplicateKey, defaultTransTimeout).Scan(&eventStr)
h.eventHandleDuration.WithLabelValues("PopNewEvent").Observe(time.Since(cost).Seconds())
if err != nil {
h.eventHandleTotal.WithLabelValues("PopNewEventFailed").Inc()
return nil, err
}
// ignore empty event.
if len(eventStr) == 0 || eventStr == etypes.NilStr {
return nil, nil
}
eventData := []byte(eventStr)
// marshal to EventInst.
eventInst := &metadata.EventInst{}
if err := json.Unmarshal(eventData, eventInst); err != nil {
return nil, err
}
// check expire event.
if time.Now().Unix()-eventInst.ActionTime.Unix() > defaultFusingEventExpireSec {
// old event, expire it.
h.eventHandleTotal.WithLabelValues("ExpireEventNum").Inc()
return nil, nil
}
blog.Info("pop new event, %s", eventStr)
return eventInst, nil
}
// nextDistID returns new event dist id for target subscriber.
func (h *EventHandler) nextDistID(subid int64) (int64, error) {
return h.cache.Incr(etypes.EventCacheDistIDPrefix + fmt.Sprint(subid)).Result()
}
// sendToPushers sends new event instance to pusher of target subscriber.
func (h *EventHandler) sendToPusher(subid int64, dist *metadata.DistInst) error {
h.pushersMu.Lock()
if _, isExist := h.pushers[subid]; !isExist {
// create new pusher for the subscriber.
newPusher := NewEventPusher(h.ctx, h.engine, subid, h.cache, h.distributer,
h.pusherHandleTotal, h.pusherHandleDuration)
// run new pusher.
newPusher.Run()
h.pushers[subid] = newPusher
}
// pusher of subscriber.
pusher := h.pushers[subid]
h.pushersMu.Unlock()
dstbID, err := h.nextDistID(subid)
if err != nil {
return err
}
dist.DstbID = dstbID
dist.SubscriptionID = subid
// add to subscriber pusher.
return pusher.Handle(dist)
}
// handleEvent handles target event.
func (h *EventHandler) handleEvent(event *metadata.EventInst) error {
blog.Infof("handle event inst, %+v", event)
defer blog.Infof("handle event inst done, %+v", event)
// find all subscribers on this event type.
subscribers := h.distributer.FindSubscribers(event.GetType())
// distribute to subscribers.
for _, subscriber := range subscribers {
// push to subscriber pusher.
cost := time.Now()
err := h.sendToPusher(subscriber, &metadata.DistInst{EventInst: *event})
h.eventHandleDuration.WithLabelValues("PushEventToPusher").Observe(time.Since(cost).Seconds())
if err != nil {
blog.Errorf("push to pusher for subid[%d] failed, %+v", subscriber, err)
h.eventHandleTotal.WithLabelValues("PushEventToPusherFailed").Inc()
continue
}
h.eventHandleTotal.WithLabelValues("Success").Inc()
}
return nil
}
func (h *EventHandler) handlePushers() {
ticker := time.NewTicker(defaultPushersCheckInterval)
defer ticker.Stop()
for {
<-ticker.C
blog.Info("handle pushers, check base on subscriptions now")
subids := h.distributer.ListSubscriptionIDs()
h.pushersMu.Lock()
for _, subid := range subids {
if _, isExist := h.pushers[subid]; !isExist {
// create new pusher for the subscriber.
newPusher := NewEventPusher(h.ctx, h.engine, subid, h.cache, h.distributer,
h.pusherHandleTotal, h.pusherHandleDuration)
// run new pusher.
newPusher.Run()
h.pushers[subid] = newPusher
}
}
pushersCount := len(h.pushers)
h.pushersMu.Unlock()
blog.Info("handle pushers, current subscription count[%d], pusher count[%d]", len(subids), pushersCount)
}
}
// cleaning keeps cleaning expire or redundancy events in event cache queue.
func (h *EventHandler) cleaning() {
ticker := time.NewTicker(defaultCleanCheckInterval)
defer ticker.Stop()
for {
if !h.engine.ServiceManageInterface.IsMaster() {
blog.Warnf("not master eventserver node, skip cleaning events!")
time.Sleep(defaultMasterCheckInterval)
continue
}
<-ticker.C
blog.Info("cleaning expire and redundancy events now")
// clean.
eventCount, err := h.cache.LLen(etypes.EventCacheEventQueueKey).Result()
if err != nil {
blog.Errorf("fetch expire and redundancy events failed, %+v", err)
continue
}
blog.Info("cleaning events, current events count[%d], trim threshold[%d], delete threshold[%d], clean unit count[%d]",
eventCount, defaultCleanTrimThreshold, defaultCleanDelThreshold, defaultCleanUnit)
if eventCount < defaultCleanTrimThreshold {
continue
}
if eventCount < defaultCleanDelThreshold {
if err := h.cache.LTrim(etypes.EventCacheEventQueueKey, 0, eventCount-defaultCleanUnit).Err(); err != nil {
blog.Errorf("trim expire and redundancy events failed, %+v", err)
continue
}
}
// too many events.
if err := h.cache.Del(etypes.EventCacheEventQueueKey).Err(); err != nil {
blog.Errorf("delete expire and redundancy queue failed, %+v", err)
continue
}
blog.Info("cleaning expire and redundancy events done")
}
}
// start starts the poping and handling logic really.
func (h *EventHandler) start() {
// keep cleaning.
go h.cleaning()
// keep check and handle pushers.
go h.handlePushers()
for {
if !h.engine.ServiceManageInterface.IsMaster() {
blog.Warnf("not master eventserver node, skip distribute events!")
time.Sleep(defaultMasterCheckInterval)
continue
}
// pop.
event, err := h.popEvent()
if err != nil {
blog.Errorf("pop new event failed, %+v", err)
time.Sleep(defaultHandleTimeout)
continue
}
// ignore empty event.
if event == nil {
continue
}
// handle.
if err := h.handleEvent(event); err != nil {
blog.Errorf("handle new event failed, %+v, %+v", event, err)
time.Sleep(defaultHandleTimeout)
continue
}
}
}
// Start starts event handler and keep processing event from distributer.
func (h *EventHandler) Start() error {
if h.distributer == nil {
return errors.New("distributer not inited")
}
blog.Info("event handler starting now!")
// register metrics.
h.registerMetrics()
// keep poping events and handle distribution.
go h.start()
return nil
}