forked from couchbase/cbgt
/
cfg_metakv.go
597 lines (475 loc) · 14.9 KB
/
cfg_metakv.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// 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 cbgt
import (
"encoding/json"
"fmt"
"hash/crc32"
"reflect"
"sort"
"strings"
"sync"
log "github.com/couchbase/clog"
"github.com/couchbase/cbauth/metakv"
)
// The values stored in a Cfg are normally atomic, but CfgMetaKv has
// an exception for values related to NodeDefs. In CfgMetaKv,
// NodeDefs are handled in "composite" fashion, where CfgMetaKv will
// split a NodeDefs value into zero or more child NodeDef values, each
// stored into metakv using a shared path prefix (so, similar to
// having a file-per-NodeDef in a shared directory).
//
// This allows concurrent, racing nodes to register their own entries
// into a composite NodeDefs "directory". This can happen when
// multiple nodes are launched at nearly the same time, faster than
// metakv can replicate in eventually consistent fashion.
//
// Eventually, metakv replication will occur, and a reader will see a
// union of NodeDef values that CfgMetaKv reconstructs on the fly for
// the entire "directory".
// Prefix of paths stored in metakv, and should be immutable after
// process init()'ialization.
var CfgMetaKvPrefix = "/cbgt/cfg/"
type cfgMetaKvAdvancedHandler interface {
get(c *CfgMetaKv, key string, cas uint64) ([]byte, uint64, error)
set(c *CfgMetaKv, key string, val []byte, cas uint64) (uint64, error)
del(c *CfgMetaKv, key string, cas uint64) error
}
var cfgMetaKvAdvancedKeys map[string]cfgMetaKvAdvancedHandler = map[string]cfgMetaKvAdvancedHandler{
CfgNodeDefsKey(NODE_DEFS_WANTED): &cfgMetaKvNodeDefsSplitHandler{},
CfgNodeDefsKey(NODE_DEFS_KNOWN): &cfgMetaKvNodeDefsSplitHandler{},
PLAN_PINDEXES_KEY: &cfgMetaKvPlanPIndexesHandler{},
}
type CfgMetaKv struct {
prefix string // Prefix for paths stores in metakv.
nodeUUID string // The uuid for this node.
m sync.Mutex // Protects the fields that follow.
cfgMem *CfgMem
cancelCh chan struct{}
lastSplitCAS uint64
splitEntries map[string]CfgMetaKvEntry
}
type CfgMetaKvEntry struct {
cas uint64
data []byte
}
// NewCfgMetaKv returns a CfgMetaKv that reads and stores its single
// configuration file in the metakv.
func NewCfgMetaKv(nodeUUID string) (*CfgMetaKv, error) {
cfg := &CfgMetaKv{
prefix: CfgMetaKvPrefix,
nodeUUID: nodeUUID,
cfgMem: NewCfgMem(),
cancelCh: make(chan struct{}),
splitEntries: map[string]CfgMetaKvEntry{},
}
backoffStartSleepMS := 200
backoffFactor := float32(1.5)
backoffMaxSleepMS := 5000
go ExponentialBackoffLoop("cfg_metakv.RunObserveChildren",
func() int {
err := metakv.RunObserveChildren(cfg.prefix, cfg.metaKVCallback,
cfg.cancelCh)
if err == nil {
return -1 // Success, so stop the loop.
}
log.Printf("cfg_metakv: RunObserveChildren, err: %v", err)
return 0 // No progress, so exponential backoff.
},
backoffStartSleepMS, backoffFactor, backoffMaxSleepMS)
return cfg, nil
}
func (c *CfgMetaKv) Get(key string, cas uint64) ([]byte, uint64, error) {
c.m.Lock()
data, cas, err := c.getLOCKED(key, cas)
c.m.Unlock()
return data, cas, err
}
func (c *CfgMetaKv) getLOCKED(key string, cas uint64) ([]byte, uint64, error) {
handler := cfgMetaKvAdvancedKeys[key]
if handler != nil {
return handler.get(c, key, cas)
}
return c.getRawLOCKED(key, cas)
}
func (c *CfgMetaKv) getRawLOCKED(key string, cas uint64) ([]byte, uint64, error) {
path := c.keyToPath(key)
v, _, err := metakv.Get(path) // TODO: Handle rev.
if err != nil {
return nil, 0, err
}
return v, 1, nil
}
func (c *CfgMetaKv) Set(key string, val []byte, cas uint64) (
uint64, error) {
log.Printf("cfg_metakv: Set, key: %v, cas: %x, split: %t, nodeUUID: %s",
key, cas, cfgMetaKvAdvancedKeys[key] != nil, c.nodeUUID)
c.m.Lock()
casResult, err := c.setLOCKED(key, val, cas)
c.m.Unlock()
return casResult, err
}
func (c *CfgMetaKv) setLOCKED(key string, val []byte, cas uint64) (
uint64, error) {
handler := cfgMetaKvAdvancedKeys[key]
if handler != nil {
return handler.set(c, key, val, cas)
}
return c.setRawLOCKED(key, val, cas)
}
func (c *CfgMetaKv) setRawLOCKED(key string, val []byte, cas uint64) (
uint64, error) {
path := c.keyToPath(key)
log.Printf("cfg_metakv: Set path: %v", path)
err := metakv.Set(path, val, nil) // TODO: Handle rev better.
if err != nil {
return 0, err
}
return 1, nil
}
func (c *CfgMetaKv) Del(key string, cas uint64) error {
c.m.Lock()
err := c.delLOCKED(key, cas)
c.m.Unlock()
return err
}
func (c *CfgMetaKv) delLOCKED(key string, cas uint64) error {
handler := cfgMetaKvAdvancedKeys[key]
if handler != nil {
return handler.del(c, key, cas)
}
return c.delRawLOCKED(key, cas)
}
func (c *CfgMetaKv) delRawLOCKED(key string, cas uint64) error {
path := c.keyToPath(key)
return metakv.Delete(path, nil) // TODO: Handle rev better.
}
func (c *CfgMetaKv) Load() error {
metakv.IterateChildren(c.prefix, c.metaKVCallback)
return nil
}
func (c *CfgMetaKv) metaKVCallback(path string,
value []byte, rev interface{}) error {
key := c.pathToKey(path)
log.Printf("cfg_metakv: metaKVCallback, path: %v, key: %v,"+
" deletion: %t", path, key, value == nil)
for splitKeyPrefix := range cfgMetaKvAdvancedKeys {
// Handle the case when the key from metakv looks like
// "nodeDefs-known/63f8a79660", but the subscription was for a
// key prefix like "nodeDefs-known".
if strings.HasPrefix(key, splitKeyPrefix) {
key = splitKeyPrefix
}
}
c.m.Lock()
c.cfgMem.FireEvent(key, 1, nil)
c.m.Unlock()
return nil
}
func (c *CfgMetaKv) Subscribe(key string, ch chan CfgEvent) error {
c.m.Lock()
err := c.cfgMem.Subscribe(key, ch)
c.m.Unlock()
return err
}
func (c *CfgMetaKv) Refresh() error {
return c.Load()
}
// RemoveAllKeys removes all cfg entries from metakv, where the caller
// should no longer use this CfgMetaKv instance, but instead create a
// new instance.
func (c *CfgMetaKv) RemoveAllKeys() {
metakv.RecursiveDelete(c.prefix)
}
func (c *CfgMetaKv) keyToPath(key string) string {
return c.prefix + key
}
func (c *CfgMetaKv) pathToKey(k string) string {
return k[len(c.prefix):]
}
// for testing
func (c *CfgMetaKv) listChildPaths(key string) ([]string, error) {
g := []string{}
if cfgMetaKvAdvancedKeys[key] != nil {
m, err := metakv.ListAllChildren(c.keyToPath(key) + "/")
if err != nil {
return nil, err
}
for _, v := range m {
g = append(g, v.Path)
}
}
return g, nil
}
func checkSumUUIDs(uuids []string) string {
sort.Strings(uuids)
d, _ := json.Marshal(uuids)
return fmt.Sprint(crc32.ChecksumIEEE(d))
}
// ----------------------------------------------------------------
type cfgMetaKvNodeDefsSplitHandler struct{}
// get() retrieves multiple child entries from the metakv and weaves
// the results back into a composite nodeDefs. get() must be invoked
// with c.m.Lock()'ed.
func (a *cfgMetaKvNodeDefsSplitHandler) get(
c *CfgMetaKv, key string, cas uint64) ([]byte, uint64, error) {
m, err := metakv.ListAllChildren(c.keyToPath(key) + "/")
if err != nil {
return nil, 0, err
}
rv := &NodeDefs{NodeDefs: make(map[string]*NodeDef)}
uuids := []string{}
for _, v := range m {
var childNodeDefs NodeDefs
err = json.Unmarshal(v.Value, &childNodeDefs)
if err != nil {
return nil, 0, err
}
for k1, v1 := range childNodeDefs.NodeDefs {
rv.NodeDefs[k1] = v1
}
if rv.ImplVersion == "" ||
VersionGTE(childNodeDefs.ImplVersion, rv.ImplVersion) {
rv.ImplVersion = childNodeDefs.ImplVersion
}
uuids = append(uuids, childNodeDefs.UUID)
}
rv.UUID = checkSumUUIDs(uuids)
if rv.ImplVersion == "" {
rv.ImplVersion = VERSION
}
data, err := json.Marshal(rv)
if err != nil {
return nil, 0, err
}
casResult := c.lastSplitCAS + 1
c.lastSplitCAS = casResult
c.splitEntries[key] = CfgMetaKvEntry{
cas: casResult,
data: data,
}
return data, casResult, nil
}
// set() splits a nodeDefs into multiple child metakv entries and must
// be invoked with c.m.Lock()'ed.
func (a *cfgMetaKvNodeDefsSplitHandler) set(
c *CfgMetaKv, key string, val []byte, cas uint64) (uint64, error) {
path := c.keyToPath(key)
curEntry := c.splitEntries[key]
if cas != 0 && cas != curEntry.cas {
log.Warnf("cfg_metakv: Set split, key: %v, cas mismatch: %x != %x",
key, cas, curEntry.cas)
return 0, &CfgCASError{}
}
var curNodeDefs NodeDefs
if curEntry.data != nil && len(curEntry.data) > 0 {
err := json.Unmarshal(curEntry.data, &curNodeDefs)
if err != nil {
return 0, err
}
}
var nd NodeDefs
err := json.Unmarshal(val, &nd)
if err != nil {
return 0, err
}
// Analyze which children were added, removed, updated.
//
added := map[string]bool{}
removed := map[string]bool{}
updated := map[string]bool{}
for k, v := range nd.NodeDefs {
if curNodeDefs.NodeDefs == nil ||
curNodeDefs.NodeDefs[k] == nil {
added[k] = true
} else {
if !reflect.DeepEqual(curNodeDefs.NodeDefs[k], v) {
updated[k] = true
}
}
}
if curNodeDefs.NodeDefs != nil {
for k := range curNodeDefs.NodeDefs {
if nd.NodeDefs[k] == nil {
removed[k] = true
}
}
}
log.Printf("cfg_metakv: Set split, key: %v,"+
" added: %v, removed: %v, updated: %v",
key, added, removed, updated)
LOOP:
for k, v := range nd.NodeDefs {
if c.nodeUUID != "" && c.nodeUUID != v.UUID {
// If we have a nodeUUID, only add/update our
// nodeDef, where other nodes will each add/update
// only their own nodeDef's.
log.Printf("cfg_metakv: Set split, key: %v,"+
" skipping other node UUID: %v, self nodeUUID: %s",
key, v.UUID, c.nodeUUID)
continue LOOP
}
childNodeDefs := NodeDefs{
UUID: nd.UUID,
NodeDefs: map[string]*NodeDef{},
ImplVersion: nd.ImplVersion,
}
childNodeDefs.NodeDefs[k] = v
val, err = json.Marshal(childNodeDefs)
if err != nil {
return 0, err
}
childPath := path + "/" + k
log.Printf("cfg_metakv: Set split, key: %v, childPath: %v",
key, childPath)
err = metakv.Set(childPath, val, nil)
if err != nil {
return 0, err
}
break LOOP
}
// Remove composite children entries from metakv only if
// caller was attempting removals only. This should work as
// the caller usually has read-compute-write logic that only
// removes node defs and does not add/update node defs in the
// same read-compute-write code path.
//
if len(added) <= 0 && len(updated) <= 0 && len(removed) > 0 {
for nodeDefUUID := range removed {
childPath := path + "/" + nodeDefUUID
log.Printf("cfg_metakv: Set delete, childPath: %v", childPath)
err = metakv.Delete(childPath, nil)
if err != nil {
return 0, err
}
}
}
casResult := c.lastSplitCAS + 1
c.lastSplitCAS = casResult
return casResult, err
}
func (a *cfgMetaKvNodeDefsSplitHandler) del(
c *CfgMetaKv, key string, cas uint64) error {
delete(c.splitEntries, key)
path := c.keyToPath(key)
return metakv.RecursiveDelete(path + "/")
}
// ----------------------------------------------------------------
// cfgMetaKvPlanPIndexesHandler rewrites the get/set of a planPIndex
// document by deduplicating repeated index definitions and source
// definitions.
type cfgMetaKvPlanPIndexesHandler struct{}
// PlanPIndexesShared represents a PlanPIndexes that has been
// deduplicated into shared parts.
type PlanPIndexesShared struct {
PlanPIndexes
// Key is "indexType/indexName/indexUUID".
SharedIndexDefs map[string]*PlanPIndexIndexDef `json:"sharedIndexDefs"`
// Key is "sourceType/sourceName/sourceUUID".
SharedSourceDefs map[string]*PlanPIndexSourceDef `json:"sharedSourceDefs"`
}
// PlanPIndexIndexDef represents the shared, repeated index definition
// part of a PlanPIndex.
type PlanPIndexIndexDef struct {
IndexParams string `json:"indexParams"`
}
// PlanPIndexSourceDef represents the shared, repeated source
// definition part of a PlanPIndex.
type PlanPIndexSourceDef struct {
SourceParams string `json:"sourceParams"`
}
func (a *cfgMetaKvPlanPIndexesHandler) get(c *CfgMetaKv,
key string, cas uint64) ([]byte, uint64, error) {
buf, casResult, err := c.getRawLOCKED(key, cas)
if err != nil || len(buf) <= 0 {
return buf, casResult, err
}
var shared PlanPIndexesShared
err = json.Unmarshal(buf, &shared)
if err != nil {
return nil, 0, err
}
planPIndexes := &shared.PlanPIndexes
// Expand/reconstitute the planPIndex's name.
for ppiName, ppi := range planPIndexes.PlanPIndexes {
if ppi.Name == "" {
ppi.Name = ppiName
}
}
// Expand/reconstitute the index def part of each planPIndex.
if shared.SharedIndexDefs != nil {
for _, ppi := range planPIndexes.PlanPIndexes {
if ppi.IndexParams == "" {
k := ppi.IndexType + "/" + ppi.IndexName + "/" + ppi.IndexUUID
sharedPart, ok := shared.SharedIndexDefs[k]
if ok && sharedPart != nil {
ppi.IndexParams = sharedPart.IndexParams
}
}
}
}
// Expand/reconstitute the source def part of each planPIndex.
if shared.SharedSourceDefs != nil {
for _, ppi := range planPIndexes.PlanPIndexes {
if ppi.SourceParams == "" {
k := ppi.SourceType + "/" + ppi.SourceName + "/" + ppi.SourceUUID +
"/" + ppi.IndexName + "/" + ppi.IndexUUID
sharedPart, ok := shared.SharedSourceDefs[k]
if ok && sharedPart != nil {
ppi.SourceParams = sharedPart.SourceParams
}
}
}
}
bufResult, err := json.Marshal(planPIndexes)
if err != nil {
return nil, 0, err
}
return bufResult, casResult, nil
}
func (a *cfgMetaKvPlanPIndexesHandler) set(c *CfgMetaKv,
key string, val []byte, cas uint64) (uint64, error) {
var shared PlanPIndexesShared
err := json.Unmarshal(val, &shared.PlanPIndexes)
if err != nil {
return 0, err
}
shared.SharedIndexDefs = map[string]*PlanPIndexIndexDef{}
shared.SharedSourceDefs = map[string]*PlanPIndexSourceDef{}
// Reduce the planPIndexes by not repeating the shared parts.
for _, ppi := range shared.PlanPIndexes.PlanPIndexes {
ppi.Name = ""
if ppi.IndexParams != "" {
k := ppi.IndexType + "/" + ppi.IndexName + "/" + ppi.IndexUUID
shared.SharedIndexDefs[k] = &PlanPIndexIndexDef{
IndexParams: ppi.IndexParams,
}
ppi.IndexParams = ""
}
if ppi.SourceParams != "" {
k := ppi.SourceType + "/" + ppi.SourceName + "/" + ppi.SourceUUID +
"/" + ppi.IndexName + "/" + ppi.IndexUUID
shared.SharedSourceDefs[k] = &PlanPIndexSourceDef{
SourceParams: ppi.SourceParams,
}
ppi.SourceParams = ""
}
}
valShared, err := json.Marshal(&shared)
if err != nil {
return 0, err
}
return c.setRawLOCKED(key, valShared, cas)
}
func (a *cfgMetaKvPlanPIndexesHandler) del(c *CfgMetaKv,
key string, cas uint64) error {
return c.delRawLOCKED(key, cas)
}