/
config.go
519 lines (470 loc) · 16.9 KB
/
config.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dynamicconfig
import (
"fmt"
"reflect"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/uber/cadence/common/log"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/types"
)
const (
errCountLogThreshold = 1000
)
// NewCollection creates a new collection
func NewCollection(
client Client,
logger log.Logger,
filterOptions ...FilterOption,
) *Collection {
return &Collection{
client: client,
logger: logger,
logKeys: &sync.Map{},
errCount: -1,
filterOptions: filterOptions,
}
}
// Collection wraps dynamic config client with a closure so that across the code, the config values
// can be directly accessed by calling the function without propagating the client everywhere in
// code
type Collection struct {
client Client
logger log.Logger
logKeys *sync.Map // map of config Keys for logging to capture changes
errCount int64
filterOptions []FilterOption
}
func (c *Collection) logError(
key Key,
filters map[Filter]interface{},
err error,
) {
errCount := atomic.AddInt64(&c.errCount, 1)
if errCount%errCountLogThreshold == 0 {
// log only every 'x' errors to reduce mem allocs and to avoid log noise
filteredKey := getFilteredKeyAsString(key, filters)
if _, ok := err.(*types.EntityNotExistsError); ok {
c.logger.Debug("dynamic config not set, use default value", tag.Key(filteredKey))
} else {
c.logger.Warn("Failed to fetch key from dynamic config", tag.Key(filteredKey), tag.Error(err))
}
}
}
func (c *Collection) logValue(
key Key,
filters map[Filter]interface{},
value, defaultValue interface{},
cmpValueEquals func(interface{}, interface{}) bool,
) {
filteredKey := getFilteredKeyAsString(key, filters)
loadedValue, loaded := c.logKeys.LoadOrStore(filteredKey, value)
if !loaded {
c.logger.Info("First loading dynamic config",
tag.Key(filteredKey), tag.Value(value), tag.DefaultValue(defaultValue))
} else {
// it's loaded before, check if the value has changed
if !cmpValueEquals(loadedValue, value) {
c.logger.Info("Dynamic config has changed",
tag.Key(filteredKey), tag.Value(value), tag.DefaultValue(loadedValue))
// update the logKeys so that we can capture the changes again
// (ignore the racing condition here because it's just for logging, we need a lock if really need to solve it)
c.logKeys.Store(filteredKey, value)
}
}
}
// PropertyFn is a wrapper to get property from dynamic config
type PropertyFn func() interface{}
// IntPropertyFn is a wrapper to get int property from dynamic config
type IntPropertyFn func(opts ...FilterOption) int
// IntPropertyFnWithDomainFilter is a wrapper to get int property from dynamic config with domain as filter
type IntPropertyFnWithDomainFilter func(domain string) int
// IntPropertyFnWithTaskListInfoFilters is a wrapper to get int property from dynamic config with three filters: domain, taskList, taskType
type IntPropertyFnWithTaskListInfoFilters func(domain string, taskList string, taskType int) int
// IntPropertyFnWithShardIDFilter is a wrapper to get int property from dynamic config with shardID as filter
type IntPropertyFnWithShardIDFilter func(shardID int) int
// FloatPropertyFn is a wrapper to get float property from dynamic config
type FloatPropertyFn func(opts ...FilterOption) float64
// FloatPropertyFnWithShardIDFilter is a wrapper to get float property from dynamic config with shardID as filter
type FloatPropertyFnWithShardIDFilter func(shardID int) float64
// DurationPropertyFn is a wrapper to get duration property from dynamic config
type DurationPropertyFn func(opts ...FilterOption) time.Duration
// DurationPropertyFnWithDomainFilter is a wrapper to get duration property from dynamic config with domain as filter
type DurationPropertyFnWithDomainFilter func(domain string) time.Duration
// DurationPropertyFnWithDomainIDFilter is a wrapper to get duration property from dynamic config with domainID as filter
type DurationPropertyFnWithDomainIDFilter func(domainID string) time.Duration
// DurationPropertyFnWithTaskListInfoFilters is a wrapper to get duration property from dynamic config with three filters: domain, taskList, taskType
type DurationPropertyFnWithTaskListInfoFilters func(domain string, taskList string, taskType int) time.Duration
// DurationPropertyFnWithShardIDFilter is a wrapper to get duration property from dynamic config with shardID as filter
type DurationPropertyFnWithShardIDFilter func(shardID int) time.Duration
// BoolPropertyFn is a wrapper to get bool property from dynamic config
type BoolPropertyFn func(opts ...FilterOption) bool
// StringPropertyFn is a wrapper to get string property from dynamic config
type StringPropertyFn func(opts ...FilterOption) string
// MapPropertyFn is a wrapper to get map property from dynamic config
type MapPropertyFn func(opts ...FilterOption) map[string]interface{}
// StringPropertyFnWithDomainFilter is a wrapper to get string property from dynamic config
type StringPropertyFnWithDomainFilter func(domain string) string
// BoolPropertyFnWithDomainFilter is a wrapper to get bool property from dynamic config with domain as filter
type BoolPropertyFnWithDomainFilter func(domain string) bool
// BoolPropertyFnWithDomainIDFilter is a wrapper to get bool property from dynamic config with domainID as filter
type BoolPropertyFnWithDomainIDFilter func(domainID string) bool
// BoolPropertyFnWithTaskListInfoFilters is a wrapper to get bool property from dynamic config with three filters: domain, taskList, taskType
type BoolPropertyFnWithTaskListInfoFilters func(domain string, taskList string, taskType int) bool
// GetProperty gets a interface property and returns defaultValue if property is not found
func (c *Collection) GetProperty(key Key, defaultValue interface{}) PropertyFn {
return func() interface{} {
val, err := c.client.GetValue(key, defaultValue)
if err != nil {
c.logError(key, nil, err)
}
c.logValue(key, nil, val, defaultValue, reflect.DeepEqual)
return val
}
}
// GetIntProperty gets property and asserts that it's an integer
func (c *Collection) GetIntProperty(key Key, defaultValue int) IntPropertyFn {
return func(opts ...FilterOption) int {
filters := c.toFilterMap(opts...)
val, err := c.client.GetIntValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, intCompareEquals)
return val
}
}
// GetIntPropertyFilteredByDomain gets property with domain filter and asserts that it's an integer
func (c *Collection) GetIntPropertyFilteredByDomain(key Key, defaultValue int) IntPropertyFnWithDomainFilter {
return func(domain string) int {
filters := c.toFilterMap(DomainFilter(domain))
val, err := c.client.GetIntValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, intCompareEquals)
return val
}
}
// GetIntPropertyFilteredByTaskListInfo gets property with taskListInfo as filters and asserts that it's an integer
func (c *Collection) GetIntPropertyFilteredByTaskListInfo(key Key, defaultValue int) IntPropertyFnWithTaskListInfoFilters {
return func(domain string, taskList string, taskType int) int {
filters := c.toFilterMap(
DomainFilter(domain),
TaskListFilter(taskList),
TaskTypeFilter(taskType),
)
val, err := c.client.GetIntValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, intCompareEquals)
return val
}
}
// GetIntPropertyFilteredByShardID gets property with shardID as filter and asserts that it's an integer
func (c *Collection) GetIntPropertyFilteredByShardID(key Key, defaultValue int) IntPropertyFnWithShardIDFilter {
return func(shardID int) int {
filters := c.toFilterMap(ShardIDFilter(shardID))
val, err := c.client.GetIntValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, intCompareEquals)
return val
}
}
// GetFloat64Property gets property and asserts that it's a float64
func (c *Collection) GetFloat64Property(key Key, defaultValue float64) FloatPropertyFn {
return func(opts ...FilterOption) float64 {
filters := c.toFilterMap(opts...)
val, err := c.client.GetFloatValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, float64CompareEquals)
return val
}
}
// GetFloat64PropertyFilteredByShardID gets property with shardID filter and asserts that it's a float64
func (c *Collection) GetFloat64PropertyFilteredByShardID(key Key, defaultValue float64) FloatPropertyFnWithShardIDFilter {
return func(shardID int) float64 {
filters := c.toFilterMap(ShardIDFilter(shardID))
val, err := c.client.GetFloatValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, float64CompareEquals)
return val
}
}
// GetDurationProperty gets property and asserts that it's a duration
func (c *Collection) GetDurationProperty(key Key, defaultValue time.Duration) DurationPropertyFn {
return func(opts ...FilterOption) time.Duration {
filters := c.toFilterMap(opts...)
val, err := c.client.GetDurationValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, durationCompareEquals)
return val
}
}
// GetDurationPropertyFilteredByDomain gets property with domain filter and asserts that it's a duration
func (c *Collection) GetDurationPropertyFilteredByDomain(key Key, defaultValue time.Duration) DurationPropertyFnWithDomainFilter {
return func(domain string) time.Duration {
filters := c.toFilterMap(DomainFilter(domain))
val, err := c.client.GetDurationValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, durationCompareEquals)
return val
}
}
// GetDurationPropertyFilteredByDomainID gets property with domainID filter and asserts that it's a duration
func (c *Collection) GetDurationPropertyFilteredByDomainID(key Key, defaultValue time.Duration) DurationPropertyFnWithDomainIDFilter {
return func(domainID string) time.Duration {
filters := c.toFilterMap(DomainIDFilter(domainID))
val, err := c.client.GetDurationValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, durationCompareEquals)
return val
}
}
// GetDurationPropertyFilteredByTaskListInfo gets property with taskListInfo as filters and asserts that it's a duration
func (c *Collection) GetDurationPropertyFilteredByTaskListInfo(key Key, defaultValue time.Duration) DurationPropertyFnWithTaskListInfoFilters {
return func(domain string, taskList string, taskType int) time.Duration {
filters := c.toFilterMap(
DomainFilter(domain),
TaskListFilter(taskList),
TaskTypeFilter(taskType),
)
val, err := c.client.GetDurationValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, durationCompareEquals)
return val
}
}
// GetDurationPropertyFilteredByShardID gets property with shardID id as filter and asserts that it's a duration
func (c *Collection) GetDurationPropertyFilteredByShardID(key Key, defaultValue time.Duration) DurationPropertyFnWithShardIDFilter {
return func(shardID int) time.Duration {
filters := c.toFilterMap(ShardIDFilter(shardID))
val, err := c.client.GetDurationValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, durationCompareEquals)
return val
}
}
// GetBoolProperty gets property and asserts that it's an bool
func (c *Collection) GetBoolProperty(key Key, defaultValue bool) BoolPropertyFn {
return func(opts ...FilterOption) bool {
filters := c.toFilterMap(opts...)
opts = append(opts, c.filterOptions...)
val, err := c.client.GetBoolValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, boolCompareEquals)
return val
}
}
// GetStringProperty gets property and asserts that it's an string
func (c *Collection) GetStringProperty(key Key, defaultValue string) StringPropertyFn {
return func(opts ...FilterOption) string {
filters := c.toFilterMap(opts...)
val, err := c.client.GetStringValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, stringCompareEquals)
return val
}
}
// GetMapProperty gets property and asserts that it's a map
func (c *Collection) GetMapProperty(key Key, defaultValue map[string]interface{}) MapPropertyFn {
return func(opts ...FilterOption) map[string]interface{} {
filters := c.toFilterMap(opts...)
val, err := c.client.GetMapValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, reflect.DeepEqual)
return val
}
}
// GetStringPropertyFilteredByDomain gets property with domain filter and asserts that it's a string
func (c *Collection) GetStringPropertyFilteredByDomain(key Key, defaultValue string) StringPropertyFnWithDomainFilter {
return func(domain string) string {
filters := c.toFilterMap(DomainFilter(domain))
val, err := c.client.GetStringValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, stringCompareEquals)
return val
}
}
// GetBoolPropertyFilteredByDomain gets property with domain filter and asserts that it's a bool
func (c *Collection) GetBoolPropertyFilteredByDomain(key Key, defaultValue bool) BoolPropertyFnWithDomainFilter {
return func(domain string) bool {
filters := c.toFilterMap(DomainFilter(domain))
val, err := c.client.GetBoolValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, boolCompareEquals)
return val
}
}
// GetBoolPropertyFilteredByDomainID gets property with domainID filter and asserts that it's a bool
func (c *Collection) GetBoolPropertyFilteredByDomainID(key Key, defaultValue bool) BoolPropertyFnWithDomainIDFilter {
return func(domainID string) bool {
filters := c.toFilterMap(DomainIDFilter(domainID))
val, err := c.client.GetBoolValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, boolCompareEquals)
return val
}
}
// GetBoolPropertyFilteredByTaskListInfo gets property with taskListInfo as filters and asserts that it's an bool
func (c *Collection) GetBoolPropertyFilteredByTaskListInfo(key Key, defaultValue bool) BoolPropertyFnWithTaskListInfoFilters {
return func(domain string, taskList string, taskType int) bool {
filters := c.toFilterMap(
DomainFilter(domain),
TaskListFilter(taskList),
TaskTypeFilter(taskType),
)
val, err := c.client.GetBoolValue(
key,
filters,
defaultValue,
)
if err != nil {
c.logError(key, filters, err)
}
c.logValue(key, filters, val, defaultValue, boolCompareEquals)
return val
}
}
func (c *Collection) toFilterMap(opts ...FilterOption) map[Filter]interface{} {
l := len(opts)
m := make(map[Filter]interface{}, l)
for _, opt := range opts {
opt(m)
}
for _, opt := range c.filterOptions {
opt(m)
}
return m
}
func getFilteredKeyAsString(
key Key,
filters map[Filter]interface{},
) string {
var sb strings.Builder
sb.WriteString(key.String())
for filter := unknownFilter + 1; filter < lastFilterTypeForTest; filter++ {
if value, ok := filters[filter]; ok {
sb.WriteString(fmt.Sprintf(",%v:%v", filter.String(), value))
}
}
return sb.String()
}