/
base.go
354 lines (332 loc) · 7.96 KB
/
base.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
package source
import (
"context"
"reflect"
"sort"
"sync"
pkgerrs "github.com/pkg/errors"
"github.com/derry6/vade-go/pkg/log"
client "github.com/derry6/vade-go/source/client"
"github.com/derry6/vade-go/source/parser"
)
var (
// 确保BaseSource实现了Source接口
_ Source = (*BaseSource)(nil)
)
type BaseSource struct {
name string
withDeleted bool
stores []*pathStore
values map[string]*configValue // 根据优先级聚合后的配置
callback func([]*Event)
defaultParser parser.Parser
client client.Client
prefix string
priority int
mutex sync.RWMutex
}
func (bs *BaseSource) Close() error { return bs.client.Close() }
func (bs *BaseSource) Name() string { return bs.name }
func (bs *BaseSource) Client() client.Client { return bs.client }
func (bs *BaseSource) Priority() int { return bs.priority }
func (bs *BaseSource) parse(p parser.Parser, data []byte) (v map[string]interface{}, err error) {
if p == nil {
return bs.defaultParser.Parse(data, bs.prefix)
}
return p.Parse(data, bs.prefix)
}
// 设置回调
func (bs *BaseSource) OnEvents(cb func([]*Event)) {
bs.callback = cb
}
// 获取所有的key
func (bs *BaseSource) Keys() (keys []string) {
bs.mutex.RLock()
defer bs.mutex.RUnlock()
for key := range bs.values {
keys = append(keys, key)
}
return
}
// 获取所有的配置
func (bs *BaseSource) All() (values map[string]interface{}) {
bs.mutex.RLock()
defer bs.mutex.RUnlock()
values = make(map[string]interface{})
for key, value := range bs.values {
if value == nil {
values[key] = nil
continue
}
values[key] = value.value
}
return
}
// 获取单个配置
func (bs *BaseSource) Get(key string) (value interface{}, ok bool) {
bs.mutex.RLock()
defer bs.mutex.RUnlock()
v, ok := bs.values[key]
if ok {
if v == nil {
return nil, false
}
return v.value, true
}
return nil, false
}
// 设置配置
func (bs *BaseSource) Set(key string, value interface{}) {
bs.mutex.Lock()
defer bs.mutex.Unlock()
// 设置到每个namespace当中
for _, n := range bs.stores {
if _, ok := n.values[key]; ok {
n.values[key] = value
}
}
if n, ok := bs.values[key]; ok {
n.value = value
}
}
// 添加配置
func (bs *BaseSource) AddPath(path string, opts ...PathOption) error {
bs.mutex.RLock()
p := bs.findStore(path)
if p != nil {
// 已经添加过了
bs.mutex.RUnlock()
return nil
}
bs.mutex.RUnlock()
pOpts := newPathOptions(opts...)
return bs.addPath(path, pOpts)
}
func (bs *BaseSource) watchPath(path string, pOpts *pathOptions) error {
if pOpts.watchDisabled {
return nil
}
return bs.client.Watch(path, func(data []byte) {
e2 := bs.handlePathUpdated(path, data)
if e2 != nil {
log.Get().Errorf("Can't handle path config updated, source: %q, path: %q :%v", bs.Name(), path, e2)
}
})
}
func (bs *BaseSource) addPath(path string, pOpts *pathOptions) error {
var (
values map[string]interface{}
store = &pathStore{
path: path,
pri: pOpts.priority,
parser: pOpts.parser,
values: map[string]interface{}{},
}
)
data, err := bs.client.Pull(context.Background(), path)
if err != nil {
if pOpts.required {
return pkgerrs.Wrapf(err, "pull required path configs")
} else {
log.Get().Warnf("Can not pull path %q configs: %v", path, err)
}
} else {
values, err = bs.parse(pOpts.parser, data)
if err != nil {
if pOpts.required {
return pkgerrs.Wrapf(err, "parse required path configs")
} else {
log.Get().Warnf("Can not parse path %q configs: %v", path, err)
}
}
}
bs.mutex.Lock()
bs.stores = append(bs.stores, store)
sort.Sort(pathHighToLow(bs.stores))
events := bs.populateEvents(store, values)
bs.mutex.Unlock()
// todo: handle errors
_ = bs.watchPath(path, pOpts)
bs.dispatchEvents(events)
return nil
}
func (bs *BaseSource) findStore(path string) *pathStore {
for _, store := range bs.stores {
if store.path == path {
return store
}
}
return nil
}
func (bs *BaseSource) findAnotherStore(key string, s1 *pathStore) (v2 interface{}, s2 *pathStore) {
for _, store := range bs.stores {
if store == s1 {
continue
}
if v, ok := store.values[key]; ok {
return v, store
}
}
return
}
// 处理namespace内容变更事件
func (bs *BaseSource) handlePathUpdated(path string, data []byte) error {
bs.mutex.RLock()
p := bs.findStore(path)
bs.mutex.RUnlock()
if p == nil {
return pkgerrs.New("path not found")
}
values, err := bs.parse(p.parser, data)
if err != nil {
return err
}
bs.mutex.Lock()
events := bs.populateEvents(p, values)
bs.mutex.Unlock()
bs.dispatchEvents(events)
return nil
}
func (bs *BaseSource) handleCreated(store *pathStore, ev *Event) bool {
// 创建
lastv, ok := bs.values[ev.Key]
if !ok {
bs.values[ev.Key] = &configValue{store: store, value: ev.ValueTo}
return true
}
if lastv == nil {
ev.Action = Updated
ev.ValueFrom = nil
bs.values[ev.Key] = &configValue{store: store, value: ev.ValueTo}
return true
}
if lastv.store == nil {
lastv.store = store
}
if store.pri < lastv.store.pri {
return false
}
// 优先级相同或者高于旧的优先级
lastv.store = store
if !reflect.DeepEqual(lastv.value, ev.ValueTo) {
ev.ValueFrom = lastv.value
ev.Action = Updated
lastv.value = ev.ValueTo
return true
}
return false
}
func (bs *BaseSource) handleUpdated(store *pathStore, ev *Event) bool {
lastv, ok := bs.values[ev.Key]
if !ok || lastv == nil {
ev.Action = Created
ev.ValueFrom = nil
bs.values[ev.Key] = &configValue{store: store, value: ev.ValueTo}
return true
}
if lastv.store == nil {
lastv.store = store
}
if store.pri < lastv.store.pri {
return false
}
lastv.store = store
if !reflect.DeepEqual(lastv.value, ev.ValueTo) {
ev.Action = Updated
ev.ValueFrom = lastv.value
lastv.value = ev.ValueTo
return true
}
return false
}
func (bs *BaseSource) handleDeleted(store *pathStore, ev *Event) bool {
lastv, ok := bs.values[ev.Key]
if !ok {
return false
}
if lastv == nil || lastv.store == nil {
delete(bs.values, ev.Key)
ev.ValueFrom = nil
if lastv != nil {
ev.ValueFrom = lastv.value
}
return true
}
if lastv.store != store {
return false
}
isReal := false
// 如果是该set 维护, 找到其他set
newValue, newStore := bs.findAnotherStore(ev.Key, store)
if newStore != nil {
if !reflect.DeepEqual(newValue, lastv.value) {
ev.Action = Updated
ev.ValueFrom = lastv.value
ev.ValueTo = newValue
isReal = true
}
bs.values[ev.Key].store = newStore
bs.values[ev.Key].value = newValue
} else {
ev.ValueFrom = lastv.value
ev.ValueTo = nil
ev.Action = Deleted
delete(bs.values, ev.Key)
isReal = true
}
return isReal
}
func (bs *BaseSource) populateEvents(store *pathStore, values map[string]interface{}) (events []*Event) {
if store.values == nil {
store.values = map[string]interface{}{}
}
// 相对于本set的变化
pathEvents := store.Update(values, bs.withDeleted)
for _, ev := range pathEvents {
if ev.Key != "" {
ev.Path = store.path
switch ev.Action {
case Created:
if bs.handleCreated(store, ev) {
events = append(events, ev)
}
case Updated:
if bs.handleUpdated(store, ev) {
events = append(events, ev)
}
case Deleted:
if bs.handleDeleted(store, ev) {
events = append(events, ev)
}
}
}
}
store.values = values
return
}
func (bs *BaseSource) dispatchEvents(events []*Event) {
if cb := bs.callback; len(events) > 0 && cb != nil {
go func() {
defer func() {
if err := recover(); err != nil {
log.Get().Errorf("Can not handle events: %v", err)
}
}()
cb(events)
}()
}
}
func newBaseSource(name string, c client.Client, opts *options) *BaseSource {
return &BaseSource{
client: c,
name: name,
withDeleted: opts.withDeleted,
prefix: opts.prefix,
priority: opts.priority,
stores: make([]*pathStore, 0),
values: map[string]*configValue{},
callback: nil,
defaultParser: parser.NewDefault(),
mutex: sync.RWMutex{},
}
}