forked from polarismesh/polaris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
387 lines (339 loc) · 11.6 KB
/
watcher.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
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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 config
import (
"context"
"sync"
"time"
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
"go.uber.org/zap"
cachetypes "github.com/polarismesh/polaris/cache/api"
api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/eventhub"
"github.com/polarismesh/polaris/common/model"
"github.com/polarismesh/polaris/common/utils"
)
const (
defaultLongPollingTimeout = 30000 * time.Millisecond
QueueSize = 10240
)
var (
notModifiedResponse = &apiconfig.ConfigClientResponse{
Code: utils.NewUInt32Value(uint32(apimodel.Code_DataNoChange)),
ConfigFile: nil,
}
)
type (
BetaReleaseMatcher func(clientLabels map[string]string, event *model.SimpleConfigFileRelease) bool
FileReleaseCallback func(clientId string, rsp *apiconfig.ConfigClientResponse) bool
WatchContextFactory func(clientId string, matcher BetaReleaseMatcher) WatchContext
WatchContext interface {
// ClientID 客户端发起的
ClientID() string
// ClientLabels 客户端的标识,用于灰度发布要做标签的匹配判断
ClientLabels() map[string]string
// AppendInterest 客户端增加订阅列表
AppendInterest(item *apiconfig.ClientConfigFileInfo)
// RemoveInterest 客户端删除订阅列表
RemoveInterest(item *apiconfig.ClientConfigFileInfo)
// ShouldNotify 判断是不是需要通知客户端某个配置变动了
ShouldNotify(event *model.SimpleConfigFileRelease) bool
// Reply 真正的通知逻辑
Reply(rsp *apiconfig.ConfigClientResponse)
// Close .
Close() error
// ShouldExpire 是不是存在有效时间
ShouldExpire(now time.Time) bool
// ListWatchFiles 列举出当前订阅的所有配置文件
ListWatchFiles() []*apiconfig.ClientConfigFileInfo
// IsOnce 是不是只能被通知一次
IsOnce() bool
}
)
type LongPollWatchContext struct {
clientId string
labels map[string]string
once sync.Once
finishTime time.Time
finishChan chan *apiconfig.ConfigClientResponse
watchConfigFiles map[string]*apiconfig.ClientConfigFileInfo
betaMatcher BetaReleaseMatcher
}
func (c *LongPollWatchContext) ClientLabels() map[string]string {
return c.labels
}
// IsOnce
func (c *LongPollWatchContext) IsOnce() bool {
return true
}
func (c *LongPollWatchContext) GetNotifieResult() *apiconfig.ConfigClientResponse {
return <-c.finishChan
}
func (c *LongPollWatchContext) GetNotifieResultWithTime(timeout time.Duration) (*apiconfig.ConfigClientResponse, error) {
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case ret := <-c.finishChan:
return ret, nil
case <-timer.C:
return nil, context.DeadlineExceeded
}
}
func (c *LongPollWatchContext) ShouldExpire(now time.Time) bool {
return now.After(c.finishTime)
}
// ClientID .
func (c *LongPollWatchContext) ClientID() string {
return c.clientId
}
func (c *LongPollWatchContext) ShouldNotify(event *model.SimpleConfigFileRelease) bool {
if event.ReleaseType == model.ReleaseTypeGray && !c.betaMatcher(c.ClientLabels(), event) {
return false
}
key := event.FileKey()
watchFile, ok := c.watchConfigFiles[key]
if !ok {
return false
}
return watchFile.GetVersion().GetValue() < event.Version
}
func (c *LongPollWatchContext) ListWatchFiles() []*apiconfig.ClientConfigFileInfo {
ret := make([]*apiconfig.ClientConfigFileInfo, 0, len(c.watchConfigFiles))
for _, v := range c.watchConfigFiles {
ret = append(ret, v)
}
return ret
}
// AppendInterest .
func (c *LongPollWatchContext) AppendInterest(item *apiconfig.ClientConfigFileInfo) {
key := model.BuildKeyForClientConfigFileInfo(item)
c.watchConfigFiles[key] = item
}
// RemoveInterest .
func (c *LongPollWatchContext) RemoveInterest(item *apiconfig.ClientConfigFileInfo) {
key := model.BuildKeyForClientConfigFileInfo(item)
delete(c.watchConfigFiles, key)
}
// Close .
func (c *LongPollWatchContext) Close() error {
return nil
}
func (c *LongPollWatchContext) Reply(rsp *apiconfig.ConfigClientResponse) {
c.once.Do(func() {
c.finishChan <- rsp
close(c.finishChan)
})
}
// watchCenter 处理客户端订阅配置请求,监听配置文件发布事件通知客户端
type watchCenter struct {
subCtx *eventhub.SubscribtionContext
lock sync.Mutex
// clientId -> watchContext
clients *utils.SyncMap[string, WatchContext]
// fileId -> []clientId
watchers *utils.SyncMap[string, *utils.SyncSet[string]]
// fileCache
fileCache cachetypes.ConfigFileCache
cacheMgr cachetypes.CacheManager
cancel context.CancelFunc
}
// NewWatchCenter 创建一个客户端监听配置发布的处理中心
func NewWatchCenter(cacheMgr cachetypes.CacheManager) (*watchCenter, error) {
ctx, cancel := context.WithCancel(context.Background())
wc := &watchCenter{
clients: utils.NewSyncMap[string, WatchContext](),
watchers: utils.NewSyncMap[string, *utils.SyncSet[string]](),
fileCache: cacheMgr.ConfigFile(),
cacheMgr: cacheMgr,
cancel: cancel,
}
var err error
wc.subCtx, err = eventhub.Subscribe(eventhub.ConfigFilePublishTopic, wc, eventhub.WithQueueSize(QueueSize))
if err != nil {
return nil, err
}
go wc.startHandleTimeoutRequestWorker(ctx)
return wc, nil
}
// PreProcess do preprocess logic for event
func (wc *watchCenter) PreProcess(_ context.Context, e any) any {
return e
}
// OnEvent event process logic
func (wc *watchCenter) OnEvent(ctx context.Context, arg any) error {
event, ok := arg.(*eventhub.PublishConfigFileEvent)
if !ok {
log.Warn("[Config][Watcher] receive invalid event type")
return nil
}
wc.notifyToWatchers(event.Message)
return nil
}
func (wc *watchCenter) CheckQuickResponseClient(watchCtx WatchContext) *apiconfig.ConfigClientResponse {
buildRet := func(release *model.ConfigFileRelease) *apiconfig.ConfigClientResponse {
ret := &apiconfig.ClientConfigFileInfo{
Namespace: utils.NewStringValue(release.Namespace),
Group: utils.NewStringValue(release.Group),
FileName: utils.NewStringValue(release.FileName),
Version: utils.NewUInt64Value(release.Version),
Md5: utils.NewStringValue(release.Md5),
Name: utils.NewStringValue(release.Name),
}
return api.NewConfigClientResponse(apimodel.Code_ExecuteSuccess, ret)
}
for _, configFile := range watchCtx.ListWatchFiles() {
namespace := configFile.GetNamespace().GetValue()
group := configFile.GetGroup().GetValue()
fileName := configFile.GetFileName().GetValue()
// 从缓存中获取灰度文件
if len(watchCtx.ClientLabels()) > 0 {
if release := wc.fileCache.GetActiveGrayRelease(namespace, group, fileName); release != nil {
if watchCtx.ShouldNotify(release.SimpleConfigFileRelease) {
return buildRet(release)
}
}
}
release := wc.fileCache.GetActiveRelease(namespace, group, fileName)
// 从缓存中获取最新的配置文件信息
if release != nil && watchCtx.ShouldNotify(release.SimpleConfigFileRelease) {
return buildRet(release)
}
}
return nil
}
// GetWatchContext .
func (wc *watchCenter) GetWatchContext(clientId string) (WatchContext, bool) {
return wc.clients.Load(clientId)
}
// DelWatchContext .
func (wc *watchCenter) DelWatchContext(clientId string) (WatchContext, bool) {
return wc.clients.Delete(clientId)
}
// AddWatcher 新增订阅者
func (wc *watchCenter) AddWatcher(clientId string,
watchFiles []*apiconfig.ClientConfigFileInfo, factory WatchContextFactory) WatchContext {
watchCtx, _ := wc.clients.ComputeIfAbsent(clientId, func(k string) WatchContext {
return factory(clientId, wc.MatchBetaReleaseFile)
})
for _, file := range watchFiles {
fileKey := utils.GenFileId(file.GetNamespace().GetValue(), file.GetGroup().GetValue(), file.GetFileName().GetValue())
watchCtx.AppendInterest(file)
clientIds, _ := wc.watchers.ComputeIfAbsent(fileKey, func(k string) *utils.SyncSet[string] {
return utils.NewSyncSet[string]()
})
clientIds.Add(clientId)
}
return watchCtx
}
// RemoveAllWatcher 删除订阅者
func (wc *watchCenter) RemoveAllWatcher(clientId string) {
oldVal, exist := wc.clients.Delete(clientId)
if !exist {
return
}
_ = oldVal.Close()
for _, file := range oldVal.ListWatchFiles() {
watchFileId := utils.GenFileId(file.Namespace.GetValue(), file.Group.GetValue(), file.FileName.GetValue())
watchers, ok := wc.watchers.Load(watchFileId)
if !ok {
continue
}
watchers.Remove(clientId)
}
wc.clients.Delete(clientId)
}
// RemoveWatcher 删除订阅者
func (wc *watchCenter) RemoveWatcher(clientId string, watchConfigFiles []*apiconfig.ClientConfigFileInfo) {
oldVal, exist := wc.clients.Delete(clientId)
if exist {
_ = oldVal.Close()
}
if len(watchConfigFiles) == 0 {
return
}
for _, file := range watchConfigFiles {
watchFileId := utils.GenFileId(file.Namespace.GetValue(), file.Group.GetValue(), file.FileName.GetValue())
watchers, ok := wc.watchers.Load(watchFileId)
if !ok {
continue
}
watchers.Remove(clientId)
}
}
func (wc *watchCenter) notifyToWatchers(publishConfigFile *model.SimpleConfigFileRelease) {
watchFileId := utils.GenFileId(publishConfigFile.Namespace, publishConfigFile.Group, publishConfigFile.FileName)
clientIds, ok := wc.watchers.Load(watchFileId)
if !ok {
return
}
changeNotifyRequest := publishConfigFile.ToSpecNotifyClientRequest()
response := api.NewConfigClientResponse(apimodel.Code_ExecuteSuccess, changeNotifyRequest)
notifyCnt := 0
clientIds.Range(func(clientId string) {
watchCtx, ok := wc.clients.Load(clientId)
if !ok {
log.Warn("[Config][Watcher] not found client when do notify.", zap.String("clientId", clientId),
zap.String("file", watchFileId))
return
}
if watchCtx.ShouldNotify(publishConfigFile) {
watchCtx.Reply(response)
notifyCnt++
// 只能用一次,通知完就要立马清理掉这个 WatchContext
if watchCtx.IsOnce() {
wc.RemoveAllWatcher(watchCtx.ClientID())
}
}
})
log.Info("[Config][Watcher] received config file release event.", zap.String("file", watchFileId),
zap.Uint64("version", publishConfigFile.Version), zap.Int("clients", clientIds.Len()),
zap.Int("notify", notifyCnt))
}
func (wc *watchCenter) MatchBetaReleaseFile(clientLabels map[string]string, event *model.SimpleConfigFileRelease) bool {
return wc.cacheMgr.Gray().HitGrayRule(model.GetGrayConfigRealseKey(event), clientLabels)
}
func (wc *watchCenter) Close() {
wc.cancel()
wc.subCtx.Cancel()
}
func (wc *watchCenter) startHandleTimeoutRequestWorker(ctx context.Context) {
t := time.NewTicker(time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
tNow := time.Now()
waitRemove := make([]WatchContext, 0, 32)
wc.clients.Range(func(client string, watchCtx WatchContext) {
if watchCtx.ShouldExpire(tNow) {
waitRemove = append(waitRemove, watchCtx)
}
})
if len(waitRemove) > 0 {
log.Info("remove expire watch context", zap.Any("client-ids", waitRemove))
}
for i := range waitRemove {
watchCtx := waitRemove[i]
watchCtx.Reply(notModifiedResponse)
wc.RemoveAllWatcher(watchCtx.ClientID())
}
}
}
}