-
Notifications
You must be signed in to change notification settings - Fork 12
/
manager.go
1529 lines (1318 loc) · 44 KB
/
manager.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbgt
import (
"container/list"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
log "github.com/couchbase/clog"
"github.com/couchbase/tools-common/objstore/objcli"
)
// A Manager represents a runtime node in a cluster.
//
// Although often used like a singleton, multiple Manager instances
// can be instantiated in a process to simulate a cluster of nodes.
//
// A Manager has two related child, actor-like goroutines:
// - planner
// - janitor
//
// A planner splits index definitions into index partitions (pindexes)
// and assigns those pindexes to nodes. A planner wakes up and runs
// whenever the index definitions change or the set of nodes changes
// (which are both read from the Cfg system). A planner stores the
// latest plans into the Cfg system.
//
// A janitor running on each node maintains runtime PIndex and Feed
// instances, creating, deleting & hooking them up as necessary to try
// to match to latest plans from the planner. A janitor wakes up and
// runs whenever it sees that latest plans in the Cfg have changed.
//
// As part of server: multiple urls permitted with ';' delimiter.
type Manager struct {
startTime time.Time
version string // See VERSION.
cfg Cfg
uuid string // Unique to every Manager instance.
tags []string // The tags at Manager start.
tagsMap map[string]bool // The tags at Manager start, performance opt.
container string // '/' separated containment path (optional).
weight int
extras string
bindHttp string
dataDir string
server string // The default datasource that will be indexed.
stopCh chan struct{}
plannerCh chan *workReq // Kicks planner that there's more work.
janitorCh chan *workReq // Kicks janitor that there's more work.
meh ManagerEventHandlers
stats ManagerStats
m sync.RWMutex // Protects the fields that follow.
pindexes map[string]*PIndex // Key is PIndex.Name().
bootingPIndexes map[string]bool // booting flag
lastNodeDefs map[string]*NodeDefs
lastIndexDefs *IndexDefs
lastIndexDefsByName map[string]*IndexDef
lastPlanPIndexes *PlanPIndexes
lastPlanPIndexesByName map[string][]*PlanPIndex
coveringCache map[CoveringPIndexesSpec]*CoveringPIndexes
feedsMutex sync.RWMutex
feeds map[string]Feed // Key is Feed.Name().
optionsMutex sync.RWMutex
options map[string]string
eventsMutex sync.RWMutex
events *list.List
peh PlannerEventHandlerCallback
stablePlanPIndexesMutex sync.RWMutex // Protects the local stable plan access.
// The below fields are related to hibernationa and optional.
objStoreClient objcli.Client
hibernationCtx context.Context
hibernationCancel context.CancelFunc
bucketInHibernationMutex sync.RWMutex
bucketInHibernation string
}
func (mgr *Manager) GetHibernationContext() (context.Context, context.CancelFunc) {
return mgr.hibernationCtx, mgr.hibernationCancel
}
func (mgr *Manager) setHibernationContext(rateLimit uint64) {
mgr.hibernationCtx = context.Background()
mgr.hibernationCtx, mgr.hibernationCancel = context.WithCancel(mgr.hibernationCtx)
mgr.hibernationCtx = context.WithValue(mgr.hibernationCtx, "rateLimit", rateLimit)
}
func (mgr *Manager) GetObjStoreClient() objcli.Client {
return mgr.objStoreClient
}
func (mgr *Manager) setObjStoreClient(client objcli.Client) {
mgr.objStoreClient = client
}
// This is the key for the manager option to track which bucket
// is currently being tracked for hibernation.
// If set to "$", it indicates that currently, no bucket is
// being hibernated/tracked for hibernation.
const bucketInHibernationKey = "bucketInHibernation"
const NoBucketInHibernation = "$"
func (mgr *Manager) MarkBucketForHibernation(bucketTaskKey string) error {
if mgr.Options()[bucketInHibernationKey] == bucketTaskKey {
return nil
}
return mgr.SetOption(bucketInHibernationKey, bucketTaskKey, true)
}
func (mgr *Manager) ResetBucketTrackedForHibernation() error {
if mgr.Options()[bucketInHibernationKey] == NoBucketInHibernation {
return nil
}
return mgr.SetOption(bucketInHibernationKey, NoBucketInHibernation, true)
}
func (mgr *Manager) IsBucketBeingHibernated(bucket string) bool {
if bucket == "" {
return false
}
mgr.bucketInHibernationMutex.RLock()
bucketInHibernation := mgr.bucketInHibernation
mgr.bucketInHibernationMutex.RUnlock()
return bucketInHibernation == bucket
}
func (mgr *Manager) RegisterHibernationBucketTracker(bucket string) {
mgr.bucketInHibernationMutex.Lock()
defer mgr.bucketInHibernationMutex.Unlock()
if mgr.bucketInHibernation == bucket {
return
}
mgr.bucketInHibernation = bucket
atomic.AddUint64(&mgr.stats.TotRegisterHibernationBucketTracker, 1)
}
func (mgr *Manager) UnregisterBucketTracker() {
mgr.bucketInHibernationMutex.Lock()
defer mgr.bucketInHibernationMutex.Unlock()
if mgr.bucketInHibernation == NoBucketInHibernation {
return
}
mgr.bucketInHibernation = NoBucketInHibernation
atomic.AddUint64(&mgr.stats.TotUnregisterHibernationBucketTracker, 1)
}
// Sets options in manager and optionally persists them as cluster options
// if cfgSet is true
func (mgr *Manager) SetOption(key, value string, cfgSet bool) error {
mgr.optionsMutex.Lock()
defer mgr.optionsMutex.Unlock()
mgr.options[key] = value
if !cfgSet {
return nil
}
mo := ClusterOptions{}
oval := reflect.ValueOf(&mo)
for k, v := range mgr.options {
fName := strings.ToUpper(string(k[0])) + k[1:]
f := oval.Elem().FieldByName(fName)
if f.IsValid() {
f.SetString(v)
}
}
_, err := CfgSetClusterOptions(mgr.cfg, &mo, 0)
if err != nil {
return err
}
atomic.AddUint64(&mgr.stats.TotSetOptions, 1)
return nil
}
// PlannerEventHandlerCallback is an optional event
// callback for an external planner that wish to receive
// direct notifications of custom cfg events (bypassing
// the metakv/cfg event subscription model) directly
// from the local manager instance.
// Currently the manager only provides index definition
// related events alone in this callbacks.
type PlannerEventHandlerCallback func(*CfgEvent)
// RegisterPlannerEventHandlerCallback lets an external
// planner register for any custom index definition change
// notifications from the manager instance.
func (mgr *Manager) RegisterPlannerEventHandlerCallback(
ev PlannerEventHandlerCallback) {
mgr.peh = ev
}
// ManagerStats represents the stats/metrics tracked by a Manager
// instance.
type ManagerStats struct {
TotKick uint64
TotSetOptions uint64
TotRegisterFeed uint64
TotUnregisterFeed uint64
TotRegisterPIndex uint64
TotUnregisterPIndex uint64
TotLoadDataDir uint64
TotSaveNodeDef uint64
TotSaveNodeDefNil uint64
TotSaveNodeDefGetErr uint64
TotSaveNodeDefSetErr uint64
TotSaveNodeDefRetry uint64
TotSaveNodeDefSame uint64
TotSaveNodeDefOk uint64
TotCreateIndex uint64
TotCreateIndexOk uint64
TotDeleteIndex uint64
TotDeleteIndexOk uint64
TotIndexControl uint64
TotIndexControlOk uint64
TotDeleteIndexBySource uint64
TotDeleteIndexBySourceErr uint64
TotDeleteIndexBySourceOk uint64
TotPlannerOpStart uint64
TotPlannerOpRes uint64
TotPlannerOpErr uint64
TotPlannerOpDone uint64
TotPlannerNOOP uint64
TotPlannerNOOPOk uint64
TotPlannerKick uint64
TotPlannerKickStart uint64
TotPlannerKickChanged uint64
TotPlannerKickErr uint64
TotPlannerKickOk uint64
TotPlannerUnknownErr uint64
TotPlannerSubscriptionEvent uint64
TotPlannerStop uint64
TotJanitorOpStart uint64
TotJanitorOpRes uint64
TotJanitorOpErr uint64
TotJanitorOpDone uint64
TotJanitorNOOP uint64
TotJanitorNOOPOk uint64
TotJanitorKick uint64
TotJanitorKickStart uint64
TotJanitorKickErr uint64
TotJanitorKickOk uint64
TotJanitorClosePIndex uint64
TotJanitorRemovePIndex uint64
TotJanitorRestartPIndex uint64
TotJanitorUnknownErr uint64
TotJanitorSubscriptionEvent uint64
TotJanitorStop uint64
TotRefreshLastNodeDefs uint64
TotRefreshLastIndexDefs uint64
TotRefreshLastPlanPIndexes uint64
TotSlowConfigAccess uint64
TotRegisterHibernationBucketTracker uint64
TotUnregisterHibernationBucketTracker uint64
}
// ClusterOptions stores the configurable cluster-level
// manager options.
// Follow strict naming guideline for any option additions.
// Every field in ClusterOptions should have the same exact
// name as is in the original manager options cache map with
// the exception of being exported field names.
type ClusterOptions struct {
BleveMaxResultWindow string `json:"bleveMaxResultWindow"`
BleveMaxClauseCount string `json:"bleveMaxClauseCount"`
FeedAllotment string `json:"feedAllotment"`
FtsMemoryQuota string `json:"ftsMemoryQuota"`
MaxReplicasAllowed string `json:"maxReplicasAllowed"`
SlowQueryLogTimeout string `json:"slowQueryLogTimeout"`
EnableVerboseLogging string `json:"enableVerboseLogging"`
MaxFeedsPerDCPAgent string `json:"maxFeedsPerDCPAgent"`
KVConnectionBufferSize string `json:"kvConnectionBufferSize"`
MaxConcurrentPartitionMovesPerNode string `json:"maxConcurrentPartitionMovesPerNode"`
UseOSOBackfill string `json:"useOSOBackfill"`
SeqChecksTimeoutInSec string `json:"seqChecksTimeoutInSec"`
DisableFileTransferRebalance string `json:"disableFileTransferRebalance"`
EnablePartitionNodeStickiness string `json:"enablePartitionNodeStickiness"`
DisableGeoPointSpatialPlugin string `json:"disableGeoPointSpatialPlugin"`
MaxIndexCountPerSource string `json:"maxIndexCountPerSource"`
MinBackoffTimeForBatchLimitingMS string `json:"minBackoffTimeForBatchLimitingMS"`
MaxBackoffTimeForBatchLimitingMS string `json:"maxBackoffTimeForBatchLimitingMS"`
DisableRegulatorControl string `json:"disableRegulatorControl"`
ResourceUtilizationHighWaterMark string `json:"resourceUtilizationHighWaterMark"`
ResourceUtilizationLowWaterMark string `json:"resourceUtilizationLowWaterMark"`
ResourceUnderUtilizationWaterMark string `json:"resourceUnderUtilizationWaterMark"`
BucketInHibernation string `json:"bucketInHibernation"`
HibernationSourcePartitions string `json:"hibernationSourcePartitions"`
}
var ErrNoIndexDefs = errors.New("no index definitions found")
// MANAGER_MAX_EVENTS limits the number of events tracked by a Manager
// for diagnosis/debugging.
const MANAGER_MAX_EVENTS = 10
const MANAGER_CLUSTER_OPTIONS_KEY = "manager_cluster_options_key"
// ManagerEventHandlers represents the callback interface where an
// application can receive important event callbacks from a Manager.
type ManagerEventHandlers interface {
OnRegisterPIndex(pindex *PIndex)
OnUnregisterPIndex(pindex *PIndex)
OnFeedError(srcType string, r Feed, err error)
OnRefreshManagerOptions(options map[string]string)
}
// NewManager returns a new, ready-to-be-started Manager instance.
func NewManager(version string, cfg Cfg, uuid string, tags []string,
container string, weight int, extras, bindHttp, dataDir, server string,
meh ManagerEventHandlers) *Manager {
return NewManagerEx(version, cfg, uuid, tags, container, weight, extras,
bindHttp, dataDir, server, meh, nil)
}
// NewManagerEx returns a new, ready-to-be-started Manager instance,
// with additional options.
func NewManagerEx(version string, cfg Cfg, uuid string, tags []string,
container string, weight int, extras, bindHttp, dataDir, server string,
meh ManagerEventHandlers, options map[string]string) *Manager {
if options == nil {
options = map[string]string{}
}
return &Manager{
startTime: time.Now(),
version: version,
cfg: cfg,
uuid: uuid,
tags: tags,
tagsMap: StringsToMap(tags),
container: container,
weight: weight,
extras: extras,
bindHttp: bindHttp, // TODO: Need FQDN:port instead of ":8095".
dataDir: dataDir,
server: server,
stopCh: make(chan struct{}),
options: options,
feeds: make(map[string]Feed),
pindexes: make(map[string]*PIndex),
bootingPIndexes: make(map[string]bool),
plannerCh: make(chan *workReq),
janitorCh: make(chan *workReq),
meh: meh,
events: list.New(),
lastNodeDefs: make(map[string]*NodeDefs),
}
}
func (mgr *Manager) Stop() {
close(mgr.stopCh)
}
// Start will start and register a Manager instance with its
// configured Cfg system, based on the register parameter. See
// Manager.Register().
func (mgr *Manager) Start(register string) error {
err := mgr.Register(register)
if err != nil {
return err
}
if mgr.tagsMap == nil || mgr.tagsMap["pindex"] {
mldd := mgr.options["managerLoadDataDir"]
if mldd == "" || mldd == "true" {
err := mgr.LoadDataDir()
if err != nil {
return err
}
}
}
if mgr.tagsMap == nil || mgr.tagsMap["planner"] {
go mgr.PlannerLoop()
go mgr.PlannerKick("start")
}
if mgr.tagsMap == nil ||
(mgr.tagsMap["pindex"] && mgr.tagsMap["janitor"]) {
go mgr.JanitorLoop()
go mgr.JanitorKick("start")
}
return mgr.StartCfg()
}
// StartCfg will start Cfg subscriptions.
func (mgr *Manager) StartCfg() error {
if mgr.cfg != nil { // TODO: Need err handling for Cfg subscriptions.
// refresh the cluster options.
mgr.RefreshOptions()
go func() {
ei := make(chan CfgEvent)
mgr.cfg.Subscribe(INDEX_DEFS_KEY, ei)
mgr.cfg.Subscribe(MANAGER_CLUSTER_OPTIONS_KEY, ei)
for {
select {
case <-mgr.stopCh:
return
case e := <-ei:
if e.Key == INDEX_DEFS_KEY {
mgr.GetIndexDefs(true)
continue
}
mgr.RefreshOptions()
}
}
}()
go func() {
ep := make(chan CfgEvent)
mgr.cfg.Subscribe(PLAN_PINDEXES_DIRECTORY_STAMP, ep)
for {
select {
case <-mgr.stopCh:
return
case <-ep:
mgr.GetPlanPIndexes(true)
}
}
}()
kinds := []string{NODE_DEFS_KNOWN, NODE_DEFS_WANTED}
for _, kind := range kinds {
go func(kind string) {
ep := make(chan CfgEvent)
mgr.cfg.Subscribe(CfgNodeDefsKey(kind), ep)
for {
select {
case <-mgr.stopCh:
return
case <-ep:
mgr.GetNodeDefs(kind, true)
}
}
}(kind)
}
}
return nil
}
// StartRegister is deprecated and has been renamed to Register().
func (mgr *Manager) StartRegister(register string) error {
return mgr.Register(register)
}
// Register will register or unregister a Manager with its configured
// Cfg system, based on the register parameter, which can have these
// values:
// * wanted - register this node as wanted
// * wantedForce - same as wanted, but force a Cfg update
// * known - register this node as known
// * knownForce - same as unknown, but force a Cfg update
// * unwanted - unregister this node no longer wanted
// * unknown - unregister this node no longer wanted and no longer known
// * unchanged - don't change any Cfg registrations for this node
func (mgr *Manager) Register(register string) error {
if register == "unchanged" {
return nil
}
if register == "unwanted" || register == "unknown" {
err := mgr.RemoveNodeDef(NODE_DEFS_WANTED)
if err != nil {
return err
}
if register == "unknown" {
err := mgr.RemoveNodeDef(NODE_DEFS_KNOWN)
if err != nil {
return err
}
}
}
log.Printf("manager: container: %s", mgr.container)
if register == "known" || register == "knownForce" ||
register == "wanted" || register == "wantedForce" {
// Save our nodeDef (with our UUID) into the Cfg as a known node.
err := mgr.SaveNodeDef(NODE_DEFS_KNOWN, register == "knownForce")
if err != nil {
return err
}
if register == "wanted" || register == "wantedForce" {
// Save our nodeDef (with our UUID) into the Cfg as a wanted node.
err := mgr.SaveNodeDef(NODE_DEFS_WANTED, register == "wantedForce")
if err != nil {
return err
}
}
}
return nil
}
// ---------------------------------------------------------------
// SaveNodeDef updates the NodeDef registrations in the Cfg system for
// this Manager node instance.
func (mgr *Manager) SaveNodeDef(kind string, force bool) error {
atomic.AddUint64(&mgr.stats.TotSaveNodeDef, 1)
if mgr.cfg == nil {
atomic.AddUint64(&mgr.stats.TotSaveNodeDefNil, 1)
return nil // Occurs during testing.
}
nodeDef := &NodeDef{
HostPort: mgr.bindHttp,
UUID: mgr.uuid,
ImplVersion: mgr.version,
Tags: mgr.tags,
Container: mgr.container,
Weight: mgr.weight,
Extras: mgr.extras,
}
for {
nodeDefs, cas, err := CfgGetNodeDefs(mgr.cfg, kind)
if err != nil {
atomic.AddUint64(&mgr.stats.TotSaveNodeDefGetErr, 1)
return err
}
if nodeDefs == nil {
nodeDefs = NewNodeDefs(mgr.version)
}
nodeDefPrev, exists := nodeDefs.NodeDefs[mgr.uuid]
if exists && !force {
if reflect.DeepEqual(nodeDefPrev, nodeDef) {
atomic.AddUint64(&mgr.stats.TotSaveNodeDefSame, 1)
atomic.AddUint64(&mgr.stats.TotSaveNodeDefOk, 1)
return nil // No changes, so leave the existing nodeDef.
}
}
nodeDefs.UUID = NewUUID()
nodeDefs.NodeDefs[mgr.uuid] = nodeDef
nodeDefs.ImplVersion = CfgGetVersion(mgr.cfg)
log.Printf("manager: setting the nodeDefs implVersion "+
"to %s", nodeDefs.ImplVersion)
_, err = CfgSetNodeDefs(mgr.cfg, kind, nodeDefs, cas)
if err != nil {
if _, ok := err.(*CfgCASError); ok {
// Retry if it was a CAS mismatch, as perhaps
// multiple nodes are all racing to register themselves,
// such as in a full datacenter power restart.
atomic.AddUint64(&mgr.stats.TotSaveNodeDefRetry, 1)
continue
}
atomic.AddUint64(&mgr.stats.TotSaveNodeDefSetErr, 1)
return err
}
break
}
atomic.AddUint64(&mgr.stats.TotSaveNodeDefOk, 1)
return nil
}
// ---------------------------------------------------------------
// RemoveNodeDef removes the NodeDef registrations in the Cfg system for
// this Manager node instance.
func (mgr *Manager) RemoveNodeDef(kind string) error {
if mgr.cfg == nil {
return nil // Occurs during testing.
}
for {
err := CfgRemoveNodeDef(mgr.cfg, kind, mgr.uuid, CfgGetVersion(mgr.cfg))
if err != nil {
if _, ok := err.(*CfgCASError); ok {
// Retry if it was a CAS mismatch, as perhaps multiple
// nodes are racing to register/unregister themselves,
// such as in a full cluster power restart.
continue
}
return err
}
break
}
return nil
}
// bootingPIndexes maintains the loading status of pindexes
// during the loadDataDir operation. An entry in bootingPIndexes
// indicates that the pindex is booting.
// bootingPIndex returns true if the pindex loading is in progress
func (mgr *Manager) bootingPIndex(pindex string) bool {
mgr.m.RLock()
rv := mgr.bootingPIndexes[pindex]
mgr.m.RUnlock()
return rv
}
// update the booting status and returns whether the update was success or not
func (mgr *Manager) updateBootingStatus(pindex string, status bool) bool {
if pindex != "" {
mgr.m.Lock()
defer mgr.m.Unlock()
if !status {
// booting completed
delete(mgr.bootingPIndexes, pindex)
return true
}
if _, exists := mgr.pindexes[pindex]; exists {
// already booted by Janitor, no status updates
return false
}
if mgr.bootingPIndexes[pindex] {
// if booting already in progress
return false
}
mgr.bootingPIndexes[pindex] = true
}
return true
}
type pindexLoadReq struct {
path, pindexName string
}
// ---------------------------------------------------------------
// TempPathPrefix indicates the prefix string applied to
// name a temp directory.
var TempPathPrefix = "temp$$"
// Walk the data dir and register pindexes for a Manager instance.
func (mgr *Manager) LoadDataDir() error {
log.Printf("manager: loading dataDir...")
dirEntries, err := os.ReadDir(mgr.dataDir)
if err != nil {
return fmt.Errorf("manager: could not read dataDir: %s, err: %v",
mgr.dataDir, err)
}
// clean up any left over temp download directories.
for i := len(dirEntries) - 1; i >= 0; i-- {
path := filepath.Join(mgr.dataDir, dirEntries[i].Name())
if strings.HasPrefix(dirEntries[i].Name(), TempPathPrefix) {
log.Printf("manager: purging temp directory: %s", path)
os.RemoveAll(path)
dirEntries = append(dirEntries[:i], dirEntries[i+1:]...)
}
}
size := len(dirEntries)
openReqs := make(chan *pindexLoadReq, size)
nWorkers := getWorkerCount(size)
var wg sync.WaitGroup
// spawn the openPIndex workers
for i := 0; i < nWorkers; i++ {
wg.Add(1)
go func() {
for req := range openReqs {
// check whether the path still exists and if not then skip.
if _, err := os.Stat(req.path); os.IsNotExist(err) {
continue
}
// check whether pindex already loaded by the Janitor
// its possible after the first kick from a worker.
// if not loaded yet, then mark the pindex booting inprogress status
if !mgr.updateBootingStatus(req.pindexName, true) {
// 'p' already loaded
continue
}
// we have already validated the pindex paths, hence feeding directly
pindex, err := OpenPIndex(mgr, req.path)
if err != nil {
if strings.Contains(err.Error(), panicCallStack) {
log.Printf("manager: OpenPIndex error,"+
" cleaning up and trying NewPIndex,"+
" path: %s, err: %v", req.path, err)
os.RemoveAll(req.path)
} else {
log.Errorf("manager: could not open pindex path: %s, err: %v",
req.path, err)
}
} else {
mgr.registerPIndex(pindex)
// kick the janitor only in case of successful pindex load
// to complete the boot up ceremony like feed hook ups.
// but for a failure, we would like to depend on the
// usual healing power of JanitorOnce loop.
// Note: The moment first work kick happens, then its the Janitor
// who handles the further loading of pindexes.
mgr.janitorCh <- &workReq{op: WORK_KICK}
}
// mark the pindex booting complete status
mgr.updateBootingStatus(req.pindexName, false)
}
wg.Done()
}()
}
// feed the openPIndex workers with pindex paths
for _, dirInfo := range dirEntries {
path := mgr.dataDir + string(os.PathSeparator) + dirInfo.Name()
// validate the pindex path here, if valid then
// send to workers for further processing
name, ok := mgr.ParsePIndexPath(path)
if !ok {
// Skip the entry that doesn't match the naming pattern.
continue
}
openReqs <- &pindexLoadReq{path: path, pindexName: name}
}
close(openReqs)
// log this message only after all workers have completed
go func() {
wg.Wait()
atomic.AddUint64(&mgr.stats.TotLoadDataDir, 1)
log.Printf("manager: loading dataDir... done")
}()
// leave the pindex loading task to the async workers and return here
return nil
}
// ---------------------------------------------------------------
// Schedule kicks of the planner and janitor of a Manager.
func (mgr *Manager) Kick(msg string) {
atomic.AddUint64(&mgr.stats.TotKick, 1)
mgr.PlannerKick(msg)
mgr.JanitorKick(msg)
}
// ---------------------------------------------------------------
// ClosePIndex synchronously has the janitor close a pindex.
func (mgr *Manager) ClosePIndex(pindex *PIndex) error {
return syncWorkReq(mgr.janitorCh, JANITOR_CLOSE_PINDEX,
"api-ClosePIndex:"+pindex.Name, pindex)
}
// RemovePIndex synchronously has the janitor remove a pindex.
func (mgr *Manager) RemovePIndex(pindex *PIndex) error {
return syncWorkReq(mgr.janitorCh, JANITOR_REMOVE_PINDEX,
"api-RemovePIndex:"+pindex.Name, pindex)
}
// GetPIndex retrieves a named pindex instance.
func (mgr *Manager) GetPIndex(pindexName string) *PIndex {
mgr.m.RLock()
rv := mgr.pindexes[pindexName]
mgr.m.RUnlock()
return rv
}
func (mgr *Manager) registerPIndex(pindex *PIndex) error {
mgr.m.Lock()
defer mgr.m.Unlock()
if _, exists := mgr.pindexes[pindex.Name]; exists {
return fmt.Errorf("manager: registered pindex exists, name: %s",
pindex.Name)
}
pindexes := mgr.copyPIndexesLOCKED()
pindexes[pindex.Name] = pindex
mgr.pindexes = pindexes
atomic.AddUint64(&mgr.stats.TotRegisterPIndex, 1)
mgr.coveringCache = nil
if mgr.meh != nil {
mgr.meh.OnRegisterPIndex(pindex)
}
if RegisteredPIndexCallbacks.OnCreate != nil {
RegisteredPIndexCallbacks.OnCreate(pindex.Name)
}
return nil
}
// unregisterPIndex takes an optional pindexToMatch, which the caller
// can use for an exact pindex unregistration.
func (mgr *Manager) unregisterPIndex(name string, pindexToMatch *PIndex) *PIndex {
mgr.m.Lock()
defer mgr.m.Unlock()
pindex, ok := mgr.pindexes[name]
if ok {
if pindexToMatch != nil &&
pindexToMatch != pindex {
return nil
}
pindexes := mgr.copyPIndexesLOCKED()
delete(pindexes, name)
mgr.pindexes = pindexes
atomic.AddUint64(&mgr.stats.TotUnregisterPIndex, 1)
mgr.coveringCache = nil
if mgr.meh != nil {
mgr.meh.OnUnregisterPIndex(pindex)
}
if RegisteredPIndexCallbacks.OnDelete != nil {
RegisteredPIndexCallbacks.OnDelete(pindex.Name)
}
}
return pindex
}
// ---------------------------------------------------------------
func (mgr *Manager) registerFeed(feed Feed) error {
mgr.feedsMutex.Lock()
defer mgr.feedsMutex.Unlock()
if _, exists := mgr.feeds[feed.Name()]; exists {
return fmt.Errorf("manager: registered feed already exists, name: %s",
feed.Name())
}
feeds := mgr.copyFeedsLOCKED()
feeds[feed.Name()] = feed
mgr.feeds = feeds
atomic.AddUint64(&mgr.stats.TotRegisterFeed, 1)
return nil
}
func (mgr *Manager) unregisterFeed(name string) Feed {
mgr.feedsMutex.Lock()
defer mgr.feedsMutex.Unlock()
rv, ok := mgr.feeds[name]
if ok {
feeds := mgr.copyFeedsLOCKED()
delete(feeds, name)
mgr.feeds = feeds
atomic.AddUint64(&mgr.stats.TotUnregisterFeed, 1)
}
return rv
}
// ---------------------------------------------------------------
// Returns a snapshot copy of the current feeds and pindexes.
func (mgr *Manager) CurrentMaps() (map[string]Feed, map[string]*PIndex) {
mgr.feedsMutex.RLock()
feeds := mgr.feeds
mgr.feedsMutex.RUnlock()
mgr.m.RLock()
pindexes := mgr.pindexes
mgr.m.RUnlock()
return feeds, pindexes
}
// ---------------------------------------------------------------
func (mgr *Manager) copyFeedsLOCKED() map[string]Feed {
feeds := make(map[string]Feed)
for k, v := range mgr.feeds {
feeds[k] = v
}
return feeds
}
func (mgr *Manager) copyPIndexesLOCKED() map[string]*PIndex {
pindexes := make(map[string]*PIndex)
for k, v := range mgr.pindexes {
pindexes[k] = v
}
return pindexes
}
// ---------------------------------------------------------------
// Returns read-only snapshot of NodeDefs of a given kind (i.e.,
// NODE_DEFS_WANTED). Use refresh of true to force a read from Cfg.
func (mgr *Manager) GetNodeDefs(kind string, refresh bool) (
nodeDefs *NodeDefs, err error) {
mgr.m.RLock()
nodeDefs = mgr.lastNodeDefs[kind]
mgr.m.RUnlock()
if nodeDefs == nil || refresh {
mgr.m.Lock()
defer mgr.m.Unlock()
nodeDefs, _, err = CfgGetNodeDefs(mgr.Cfg(), kind)
if err != nil {
return nil, err
}
mgr.lastNodeDefs[kind] = nodeDefs
atomic.AddUint64(&mgr.stats.TotRefreshLastNodeDefs, 1)
mgr.coveringCache = nil
// update the container cache if required.
if nodeDef, ok := nodeDefs.NodeDefs[mgr.uuid]; ok {
if nodeDef.Container != mgr.container &&
nodeDef.Container != "" {
mgr.container = nodeDef.Container
log.Printf("manager: refreshed container: %s", mgr.container)
}
}
if RegisteredPIndexCallbacks.OnRefresh != nil {
RegisteredPIndexCallbacks.OnRefresh()
}
}
return nodeDefs, nil
}
// Returns read-only snapshot of the IndexDefs, also with IndexDef's
// organized by name. Use refresh of true to force a read from Cfg.
func (mgr *Manager) GetIndexDefs(refresh bool) (lastIndexDefs *IndexDefs,
lastIndexDefsByName map[string]*IndexDef, err error) {
if !refresh {
mgr.m.RLock()
lastIndexDefs = mgr.lastIndexDefs
lastIndexDefsByName = mgr.lastIndexDefsByName
mgr.m.RUnlock()
}
if lastIndexDefs == nil {
mgr.m.Lock()
lastIndexDefs, _, err = CfgGetIndexDefs(mgr.cfg)
if err != nil {
mgr.m.Unlock()
return nil, nil, err
}
mgr.lastIndexDefs = lastIndexDefs
atomic.AddUint64(&mgr.stats.TotRefreshLastIndexDefs, 1)
lastIndexDefsByName = make(map[string]*IndexDef)
if lastIndexDefs != nil {
for _, indexDef := range lastIndexDefs.IndexDefs {
lastIndexDefsByName[indexDef.Name] = indexDef
}
}
mgr.lastIndexDefsByName = lastIndexDefsByName
mgr.coveringCache = nil
mgr.m.Unlock()
if RegisteredPIndexCallbacks.OnRefresh != nil {
RegisteredPIndexCallbacks.OnRefresh()
}
}