-
Notifications
You must be signed in to change notification settings - Fork 453
/
util.go
322 lines (267 loc) · 7.87 KB
/
util.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
// 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 util
import (
"errors"
"fmt"
"time"
"github.com/m3db/m3/src/cluster/generated/proto/commonpb"
"github.com/m3db/m3/src/cluster/kv"
"go.uber.org/zap"
)
var (
noopLogger = zap.NewNop()
errNilStore = errors.New("kv store is nil")
)
// BoolFromValue get a bool from kv.Value. If the value is nil, the default value
// is returned.
func BoolFromValue(v kv.Value, key string, defaultValue bool, opts Options) (bool, error) {
if opts == nil {
opts = NewOptions()
}
var res bool
updateFn := func(i interface{}) { res = i.(bool) }
if err := updateWithKV(
getBool, updateFn, opts.ValidateFn(), key, v, defaultValue, opts.Logger(),
); err != nil {
return false, err
}
return res, nil
}
// Float64FromValue gets a float64 from kv.Value. If the value is nil, the default
// value is returned.
func Float64FromValue(v kv.Value, key string, defaultValue float64, opts Options) (float64, error) {
if opts == nil {
opts = NewOptions()
}
var res float64
updateFn := func(i interface{}) { res = i.(float64) }
if err := updateWithKV(
getFloat64, updateFn, opts.ValidateFn(), key, v, defaultValue, opts.Logger(),
); err != nil {
return 0, err
}
return res, nil
}
// Int64FromValue gets an int64 from kv.Value. If the value is nil, the default
// value is returned.
func Int64FromValue(v kv.Value, key string, defaultValue int64, opts Options) (int64, error) {
if opts == nil {
opts = NewOptions()
}
var res int64
updateFn := func(i interface{}) { res = i.(int64) }
if err := updateWithKV(
getInt64, updateFn, opts.ValidateFn(), key, v, defaultValue, opts.Logger(),
); err != nil {
return 0, err
}
return res, nil
}
// StringFromValue gets a string from kv.Value. If the value is nil, the default
// value is returned.
func StringFromValue(v kv.Value, key string, defaultValue string, opts Options) (string, error) {
if opts == nil {
opts = NewOptions()
}
var res string
updateFn := func(i interface{}) { res = i.(string) }
if err := updateWithKV(
getString, updateFn, opts.ValidateFn(), key, v, defaultValue, opts.Logger(),
); err != nil {
return "", err
}
return res, nil
}
// StringArrayFromValue gets a string array from kv.Value. If the value is nil,
// the default value is returned.
func StringArrayFromValue(
v kv.Value, key string, defaultValue []string, opts Options,
) ([]string, error) {
if opts == nil {
opts = NewOptions()
}
var res []string
updateFn := func(i interface{}) { res = i.([]string) }
if err := updateWithKV(
getStringArray, updateFn, opts.ValidateFn(), key, v, defaultValue, opts.Logger(),
); err != nil {
return nil, err
}
return res, nil
}
// TimeFromValue gets a time.Time from kv.Value. If the value is nil, the
// default value is returned.
func TimeFromValue(
v kv.Value, key string, defaultValue time.Time, opts Options,
) (time.Time, error) {
if opts == nil {
opts = NewOptions()
}
var res time.Time
updateFn := func(i interface{}) { res = i.(time.Time) }
if err := updateWithKV(
getTime, updateFn, opts.ValidateFn(), key, v, defaultValue, opts.Logger(),
); err != nil {
return time.Time{}, err
}
return res, nil
}
func getBool(v kv.Value) (interface{}, error) {
var boolProto commonpb.BoolProto
err := v.Unmarshal(&boolProto)
return boolProto.Value, err
}
func getFloat64(v kv.Value) (interface{}, error) {
var float64proto commonpb.Float64Proto
err := v.Unmarshal(&float64proto)
return float64proto.Value, err
}
func getInt64(v kv.Value) (interface{}, error) {
var int64Proto commonpb.Int64Proto
if err := v.Unmarshal(&int64Proto); err != nil {
return 0, err
}
return int64Proto.Value, nil
}
func getString(v kv.Value) (interface{}, error) {
var stringProto commonpb.StringProto
if err := v.Unmarshal(&stringProto); err != nil {
return 0, err
}
return stringProto.Value, nil
}
func getStringArray(v kv.Value) (interface{}, error) {
var stringArrProto commonpb.StringArrayProto
if err := v.Unmarshal(&stringArrProto); err != nil {
return nil, err
}
return stringArrProto.Values, nil
}
func getStringArrayPointer(v kv.Value) (interface{}, error) {
var stringArrProto commonpb.StringArrayProto
if err := v.Unmarshal(&stringArrProto); err != nil {
return nil, err
}
return &stringArrProto.Values, nil
}
func getTime(v kv.Value) (interface{}, error) {
var int64Proto commonpb.Int64Proto
if err := v.Unmarshal(&int64Proto); err != nil {
return nil, err
}
return time.Unix(int64Proto.Value, 0), nil
}
func watchAndUpdate(
store kv.Store,
key string,
getValue getValueFn,
update updateFn,
validate ValidateFn,
defaultValue interface{},
logger *zap.Logger,
) (kv.ValueWatch, error) {
if store == nil {
return nil, errNilStore
}
var (
watch kv.ValueWatch
err error
)
watch, err = store.Watch(key)
if err != nil {
return nil, fmt.Errorf("could not establish initial watch: %v", err)
}
go func() {
for range watch.C() {
updateWithKV(getValue, update, validate, key, watch.Get(), defaultValue, logger)
}
// The channel for a ValueWatch should never close.
getLogger(logger).
With(zap.String("key", key)).
Error("watch unexpectedly closed")
}()
return watch, nil
}
func updateWithKV(
getValue getValueFn,
update updateFn,
validate ValidateFn,
key string,
v kv.Value,
defaultValue interface{},
logger *zap.Logger,
) error {
if v == nil {
// The key is deleted from kv, use the default value.
update(defaultValue)
logNilUpdate(logger, key, defaultValue)
return nil
}
newValue, err := getValue(v)
if err != nil {
logMalformedUpdate(logger, key, v.Version(), newValue, err)
return err
}
if validate != nil {
if err := validate(newValue); err != nil {
logInvalidUpdate(logger, key, v.Version(), newValue, err)
return err
}
}
update(newValue)
logUpdateSuccess(logger, key, v.Version(), newValue)
return nil
}
func logNilUpdate(logger *zap.Logger, k string, v interface{}) {
getLogger(logger).Warn("nil value from kv store, applying default value",
zap.String("key", k),
zap.Any("defaultValue", v),
)
}
func logMalformedUpdate(logger *zap.Logger, k string, ver int, v interface{}, err error) {
getLogger(logger).Warn("malformed value from kv store, not applying update",
zap.String("key", k),
zap.Any("malformedValue", v),
zap.Int("version", ver),
zap.Error(err),
)
}
func logInvalidUpdate(logger *zap.Logger, k string, ver int, v interface{}, err error) {
getLogger(logger).Warn("invalid value from kv store, not applying update",
zap.String("key", k),
zap.Any("invalidValue", v),
zap.Int("version", ver),
zap.Error(err),
)
}
func logUpdateSuccess(logger *zap.Logger, k string, ver int, v interface{}) {
getLogger(logger).Info("value update success",
zap.String("key", k),
zap.Any("value", v),
zap.Int("version", ver),
)
}
func getLogger(logger *zap.Logger) *zap.Logger {
if logger == nil {
return noopLogger
}
return logger
}