-
Notifications
You must be signed in to change notification settings - Fork 12
/
manager_planner.go
1245 lines (1073 loc) · 38.4 KB
/
manager_planner.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 (
"fmt"
"hash/crc32"
"io"
"math"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/couchbase/blance"
log "github.com/couchbase/clog"
)
// Versioning the planner algorithm - updated each time the planner algo is changed.
// Refer to GetPlannerVersion() in defs.go to access it outside cbgt.
var plannerVersion string
func init() {
// custom score booster override for single partioned indexes
// to result in better balanced assignments.
blance.NodeScoreBooster = func(w int, s float64) float64 {
score := float64(-w)
if score < s {
score = s
}
return score
}
plannerVersion = "1"
}
// PlannerHooks allows advanced applications to register callbacks
// into the planning computation, in order to adjust the planning
// outcome. For example, an advanced application might adjust node
// weights more dynamically in order to achieve an improved balancing
// of pindexes across a cluster. It should be modified only during
// the init()'ialization phase of process startup. See CalcPlan()
// implementation for more information.
var PlannerHooks = map[string]PlannerHook{}
// A PlannerHook is an optional callback func supplied by the
// application via PlannerHooks and is invoked during planning.
type PlannerHook func(in PlannerHookInfo) (out PlannerHookInfo, skip bool, err error)
// A PlannerHookInfo is the in/out information provided to PlannerHook
// callbacks. If the PlannerHook wishes to modify any of these fields
// to affect the planning outcome, it must copy the field value
// (e.g., copy-on-write).
type PlannerHookInfo struct {
PlannerHookPhase string
Mode string
Version string
Server string
Options map[string]string
IndexDefs *IndexDefs
IndexDef *IndexDef
NodeDefs *NodeDefs
NodeUUIDsAll []string
NodeUUIDsToAdd []string
NodeUUIDsToRemove []string
NodeWeights map[string]int
NodeHierarchy map[string]string
PlannerFilter PlannerFilter
PlanPIndexesPrev *PlanPIndexes
PlanPIndexes *PlanPIndexes
PlanPIndexesForIndex map[string]*PlanPIndex
NumPlanPIndexes int
NodeTotalActives map[string]int
NodeSourceActives map[string]int
NodeSourceReplicas map[string]int
NodePartitionCount map[string]int
ExistingPlans *PlanPIndexes
}
// A NoopPlannerHook is a no-op planner hook that just returns its input.
func NoopPlannerHook(x PlannerHookInfo) (PlannerHookInfo, bool, error) {
return x, false, nil
}
// -------------------------------------------------------
// NOTE: You *must* update VERSION if the planning algorithm or config
// data schema changes, following semver rules.
// PlannerNOOP sends a synchronous NOOP request to the manager's planner, if any.
func (mgr *Manager) PlannerNOOP(msg string) {
atomic.AddUint64(&mgr.stats.TotPlannerNOOP, 1)
if mgr.tagsMap == nil || mgr.tagsMap["planner"] {
syncWorkReq(mgr.plannerCh, WORK_NOOP, msg, nil)
}
}
// PlannerKick synchronously kicks the manager's planner, if any.
func (mgr *Manager) PlannerKick(msg string) {
atomic.AddUint64(&mgr.stats.TotPlannerKick, 1)
if mgr.tagsMap == nil || mgr.tagsMap["planner"] {
syncWorkReq(mgr.plannerCh, WORK_KICK, msg, nil)
}
if mgr.peh != nil {
// Adding time to the event here indicating that this is the latest
// timestamp to be accounted for in the planner.
ev := &CfgEvent{Time: time.Now()}
if strings.Contains(msg, "CreateIndex") ||
strings.Contains(msg, "DeleteIndex") {
ev.Key = INDEX_DEFS_KEY
}
mgr.peh(ev)
}
}
// PlannerLoop is the main loop for the planner.
func (mgr *Manager) PlannerLoop() {
mgr.cfgObserver(componentPlanner, func(cfgEvent *CfgEvent) {
atomic.AddUint64(&mgr.stats.TotPlannerSubscriptionEvent, 1)
mgr.PlannerKick("cfg changed, key: " + cfgEvent.Key)
})
for {
select {
case <-mgr.stopCh:
atomic.AddUint64(&mgr.stats.TotPlannerStop, 1)
return
case m := <-mgr.plannerCh:
atomic.AddUint64(&mgr.stats.TotPlannerOpStart, 1)
log.Printf("planner: awakes, op: %v, msg: %s", m.op, m.msg)
var err error
if m.op == WORK_KICK {
atomic.AddUint64(&mgr.stats.TotPlannerKickStart, 1)
changed, err2 := mgr.PlannerOnce(m.msg)
if err2 != nil {
log.Warnf("planner: PlannerOnce, err: %v", err2)
atomic.AddUint64(&mgr.stats.TotPlannerKickErr, 1)
// Keep looping as perhaps it's a transient issue.
} else {
if changed {
atomic.AddUint64(&mgr.stats.TotPlannerKickChanged, 1)
mgr.JanitorKick("the plans have changed")
}
atomic.AddUint64(&mgr.stats.TotPlannerKickOk, 1)
}
} else if m.op == WORK_NOOP {
atomic.AddUint64(&mgr.stats.TotPlannerNOOPOk, 1)
} else {
err = fmt.Errorf("planner: unknown op: %s, m: %#v", m.op, m)
atomic.AddUint64(&mgr.stats.TotPlannerUnknownErr, 1)
}
atomic.AddUint64(&mgr.stats.TotPlannerOpRes, 1)
if m.resCh != nil {
if err != nil {
atomic.AddUint64(&mgr.stats.TotPlannerOpErr, 1)
m.resCh <- err
}
close(m.resCh)
}
atomic.AddUint64(&mgr.stats.TotPlannerOpDone, 1)
}
}
}
// PlannerOnce is the main body of a PlannerLoop.
func (mgr *Manager) PlannerOnce(reason string) (bool, error) {
log.Printf("planner: once, reason: %s", reason)
if mgr.cfg == nil { // Can occur during testing.
return false, fmt.Errorf("planner: skipped due to nil cfg")
}
return Plan(mgr.cfg, mgr.version, mgr.uuid, mgr.server,
mgr.Options(), nil)
}
// A PlannerFilter callback func should return true if the plans for
// an indexDef should be updated during CalcPlan(), and should return
// false if the plans for the indexDef should be remain untouched.
type PlannerFilter func(indexDef *IndexDef,
planPIndexesPrev, planPIndexes *PlanPIndexes) bool
var lastComputedPlanTime time.Time
var lcpM sync.RWMutex
func GetLastComputedPlanTime() time.Time {
lcpM.RLock()
defer lcpM.RUnlock()
return lastComputedPlanTime
}
func SetLastComputedPlanTime(plan time.Time) {
lcpM.Lock()
defer lcpM.Unlock()
lastComputedPlanTime = plan
}
// Plan runs the planner once.
func Plan(cfg Cfg, version, uuid, server string, options map[string]string,
plannerFilter PlannerFilter) (bool, error) {
// Setting the last computed plan time at the beginning of the plan process
// so that any index defs changes after this time will be considered for
// planning in the next iteration.
SetLastComputedPlanTime(time.Now())
indexDefs, nodeDefs, planPIndexesPrev, cas, err :=
PlannerGetPlan(cfg, version, uuid)
if err != nil {
return false, err
}
// Not setting the last computed plan time here since that leaves a window,
// however small, for the index defs to be changed in the time between
// fetching and setting the last computed plan time.
// use the effective version while calculating the new plan
eVersion := CfgGetVersion(cfg)
if eVersion != version {
log.Printf("planner: Plan, incoming version: %s, effective"+
"Cfg version used: %s", version, eVersion)
version = eVersion
}
planPIndexes, err := CalcPlan("", indexDefs, nodeDefs,
planPIndexesPrev, version, server, options, plannerFilter)
if err != nil {
return false, fmt.Errorf("planner: CalcPlan, err: %v", err)
}
if SamePlanPIndexes(planPIndexes, planPIndexesPrev) {
return false, nil
}
_, err = CfgSetPlanPIndexes(cfg, planPIndexes, cas)
if err != nil {
return false, fmt.Errorf("planner: could not save new plan,"+
" perhaps a concurrent planner won, cas: %d, err: %v",
cas, err)
}
return true, nil
}
// PlannerGetPlan retrieves plan related info from the Cfg.
func PlannerGetPlan(cfg Cfg, version string, uuid string) (
indexDefs *IndexDefs,
nodeDefs *NodeDefs,
planPIndexes *PlanPIndexes,
planPIndexesCAS uint64,
err error) {
// use the incoming version for a potential version bump
err = PlannerCheckVersion(cfg, version)
if err != nil {
return nil, nil, nil, 0, err
}
indexDefs, err = PlannerGetIndexDefs(cfg, version)
if err != nil {
return nil, nil, nil, 0, err
}
nodeDefs, err = PlannerGetNodeDefs(cfg, version, uuid)
if err != nil {
return nil, nil, nil, 0, err
}
planPIndexes, planPIndexesCAS, err = PlannerGetPlanPIndexes(cfg, version)
if err != nil {
return nil, nil, nil, 0, err
}
return indexDefs, nodeDefs, planPIndexes, planPIndexesCAS, nil
}
// PlannerCheckVersion errors if a version string is too low.
func PlannerCheckVersion(cfg Cfg, version string) error {
ok, err := CheckVersion(cfg, version)
if err != nil {
return fmt.Errorf("planner: CheckVersion err: %v", err)
}
if !ok {
return fmt.Errorf("planner: version too low: %v", version)
}
return nil
}
// PlannerGetIndexDefs retrieves index definitions from a Cfg.
func PlannerGetIndexDefs(cfg Cfg, version string) (*IndexDefs, error) {
indexDefs, _, err := CfgGetIndexDefs(cfg)
if err != nil {
return nil, fmt.Errorf("planner: CfgGetIndexDefs err: %v", err)
}
if indexDefs == nil {
indexDefs = NewIndexDefs(CfgGetVersion(cfg))
}
if VersionGTE(version, indexDefs.ImplVersion) == false {
return nil, fmt.Errorf("planner: indexDefs.ImplVersion: %s"+
" > version: %s", indexDefs.ImplVersion, version)
}
return indexDefs, nil
}
// PlannerGetNodeDefs retrieves node definitions from a Cfg.
func PlannerGetNodeDefs(cfg Cfg, version, uuid string) (
*NodeDefs, error) {
nodeDefs, _, err := CfgGetNodeDefs(cfg, NODE_DEFS_WANTED)
if err != nil {
return nil, fmt.Errorf("planner: CfgGetNodeDefs err: %v", err)
}
if nodeDefs == nil {
nodeDefs = NewNodeDefs(CfgGetVersion(cfg))
}
if VersionGTE(version, nodeDefs.ImplVersion) == false {
return nil, fmt.Errorf("planner: nodeDefs.ImplVersion: %s"+
" > version: %s", nodeDefs.ImplVersion, version)
}
if uuid == "" { // The caller may not be a node, so has empty uuid.
return nodeDefs, nil
}
nodeDef, exists := nodeDefs.NodeDefs[uuid]
if !exists || nodeDef == nil {
return nil, fmt.Errorf("planner: no NodeDef, uuid: %s", uuid)
}
if nodeDef.ImplVersion != version {
return nil, fmt.Errorf("planner: ended since NodeDef, uuid: %s,"+
" NodeDef.ImplVersion: %s != version: %s",
uuid, nodeDef.ImplVersion, version)
}
if nodeDef.UUID != uuid {
return nil, fmt.Errorf("planner: ended since NodeDef, uuid: %s,"+
" NodeDef.UUID: %s != uuid: %s",
uuid, nodeDef.UUID, uuid)
}
isPlanner := true
if nodeDef.Tags != nil && len(nodeDef.Tags) > 0 {
isPlanner = false
for _, tag := range nodeDef.Tags {
if tag == "planner" {
isPlanner = true
}
}
}
if !isPlanner {
return nil, fmt.Errorf("planner: ended since node, uuid: %s,"+
" is not a planner, tags: %#v", uuid, nodeDef.Tags)
}
return nodeDefs, nil
}
// PlannerGetPlanPIndexes retrieves the planned pindexes from a Cfg.
func PlannerGetPlanPIndexes(cfg Cfg, version string) (
*PlanPIndexes, uint64, error) {
planPIndexesPrev, cas, err := CfgGetPlanPIndexes(cfg)
if err != nil {
return nil, 0, fmt.Errorf("planner: CfgGetPlanPIndexes err: %v", err)
}
if planPIndexesPrev == nil {
planPIndexesPrev = NewPlanPIndexes(CfgGetVersion(cfg))
}
if VersionGTE(version, planPIndexesPrev.ImplVersion) == false {
return nil, 0, fmt.Errorf("planner: planPIndexesPrev.ImplVersion: %s"+
" > version: %s", planPIndexesPrev.ImplVersion, version)
}
return planPIndexesPrev, cas, nil
}
// Split logical indexes into PIndexes and assign PIndexes to nodes.
// As part of this, planner hook callbacks will be invoked to allow
// advanced applications to adjust the planning outcome.
func CalcPlan(mode string, indexDefs *IndexDefs, nodeDefs *NodeDefs,
planPIndexesPrev *PlanPIndexes, version, server string,
options map[string]string, plannerFilter PlannerFilter) (
*PlanPIndexes, error) {
plannerHook := PlannerHooks[options["plannerHookName"]]
if plannerHook == nil {
plannerHook = NoopPlannerHook
}
var nodeUUIDsAll []string
var nodeUUIDsToAdd []string
var nodeUUIDsToRemove []string
var nodeWeights map[string]int
var nodeHierarchy map[string]string
var planPIndexes *PlanPIndexes
numPlanPIndexes := len(planPIndexesPrev.PlanPIndexes)
nodeTotalActives := make(map[string]int)
nodeSourceActives := make(map[string]int)
nodeSourceReplicas := make(map[string]int)
nodePartitionCount := make(map[string]int)
existingPlans := new(PlanPIndexes)
existingPlans.PlanPIndexes = make(map[string]*PlanPIndex)
indexUUIDMap := make(map[string]struct{})
for _, index := range indexDefs.IndexDefs {
indexUUIDMap[index.UUID] = struct{}{}
}
for name, pindex := range planPIndexesPrev.PlanPIndexes {
// If the index UUID isn't present, the index is either deleted
// or updated - neither of which should influence the location of the new
// partitions
if _, exists := indexUUIDMap[pindex.IndexUUID]; !exists {
continue
}
for nodeUUID, planNode := range pindex.Nodes {
nodePartitionCount[nodeUUID]++
if planNode.Priority == 0 {
nodeTotalActives[nodeUUID]++
nodeSourceActives[nodeUUID+":"+pindex.SourceName]++
} else {
nodeSourceReplicas[nodeUUID+":"+pindex.SourceName]++
}
}
if pindex.PlannerVersion == plannerVersion {
// Only if the prev plans have been computed by a planner with the same
// planning algorithm, should they influence the new plans.
existingPlans.PlanPIndexes[name] = pindex
}
}
plannerHookCall := func(phase string, indexDef *IndexDef,
planPIndexesForIndex map[string]*PlanPIndex) (
PlannerHookInfo, bool, error) {
pho, skip, err := plannerHook(PlannerHookInfo{
PlannerHookPhase: phase,
Mode: mode,
Version: version,
Server: server,
Options: options,
IndexDefs: indexDefs,
IndexDef: indexDef,
NodeDefs: nodeDefs,
NodeUUIDsAll: nodeUUIDsAll,
NodeUUIDsToAdd: nodeUUIDsToAdd,
NodeUUIDsToRemove: nodeUUIDsToRemove,
NodeWeights: nodeWeights,
NodeHierarchy: nodeHierarchy,
PlannerFilter: plannerFilter,
PlanPIndexesPrev: planPIndexesPrev,
PlanPIndexes: planPIndexes,
PlanPIndexesForIndex: planPIndexesForIndex,
NumPlanPIndexes: numPlanPIndexes,
NodeTotalActives: nodeTotalActives,
NodeSourceActives: nodeSourceActives,
NodeSourceReplicas: nodeSourceReplicas,
NodePartitionCount: nodePartitionCount,
ExistingPlans: existingPlans,
})
mode = pho.Mode
version = pho.Version
server = pho.Server
options = pho.Options
indexDefs = pho.IndexDefs
nodeDefs = pho.NodeDefs
nodeUUIDsAll = pho.NodeUUIDsAll
nodeUUIDsToAdd = pho.NodeUUIDsToAdd
nodeUUIDsToRemove = pho.NodeUUIDsToRemove
nodeWeights = pho.NodeWeights
nodeHierarchy = pho.NodeHierarchy
plannerFilter = pho.PlannerFilter
planPIndexesPrev = pho.PlanPIndexesPrev
planPIndexes = pho.PlanPIndexes
numPlanPIndexes = pho.NumPlanPIndexes
nodeSourceActives = pho.NodeSourceActives
nodeSourceReplicas = pho.NodeSourceReplicas
nodeTotalActives = pho.NodeTotalActives
nodePartitionCount = pho.NodePartitionCount
existingPlans = pho.ExistingPlans
return pho, skip, err
}
if planPIndexes == nil {
planPIndexes = NewPlanPIndexes(version)
}
_, skip, err := plannerHookCall("begin", nil, nil)
if skip || err != nil {
return planPIndexes, err
}
// This simple planner assigns at most MaxPartitionsPerPIndex
// number of partitions onto a PIndex. And then uses blance to
// assign the PIndex to 1 or more nodes (based on NumReplicas).
if indexDefs == nil || nodeDefs == nil {
return nil, nil
}
nodeUUIDsAll, nodeUUIDsToAdd, nodeUUIDsToRemove, nodeWeights, nodeHierarchy =
CalcNodesLayout(indexDefs, nodeDefs, planPIndexesPrev)
_, skip, err = plannerHookCall("nodes", nil, nil)
if skip || err != nil {
return planPIndexes, err
}
// Examine every indexDef, ordered by name for stability...
var indexDefNames []string
for indexDefName := range indexDefs.IndexDefs {
indexDefNames = append(indexDefNames, indexDefName)
}
sort.Strings(indexDefNames)
for _, indexDefName := range indexDefNames {
indexDef := indexDefs.IndexDefs[indexDefName]
pho, skip2, err2 := plannerHookCall("indexDef.begin", indexDef, nil)
if skip2 {
continue
}
if err2 != nil {
return planPIndexes, err2
}
indexDef = pho.IndexDef
// If the plan is frozen, CasePlanFrozen clones the previous
// plan for this index.
if CasePlanFrozen(indexDef, planPIndexesPrev, planPIndexes) {
continue
}
// Skip if the plannerFilter returns false.
if plannerFilter != nil &&
!plannerFilter(indexDef, planPIndexesPrev, planPIndexes) {
continue
}
// Skip indexDef's with no instantiatable pindexImplType, such
// as index aliases.
pindexImplType, exists := PIndexImplTypes[indexDef.Type]
if !exists ||
pindexImplType == nil ||
pindexImplType.New == nil ||
pindexImplType.Open == nil {
continue
}
// Split each indexDef into 1 or more PlanPIndexes.
planPIndexesForIndex, err2 := SplitIndexDefIntoPlanPIndexes(
indexDef, server, options, planPIndexes)
if err2 != nil {
log.Warnf("planner: could not SplitIndexDefIntoPlanPIndexes,"+
" indexDef.Name: %s, server: %s, err: %v",
indexDef.Name, server, err2)
continue // Keep planning the other IndexDefs.
}
pho, skip, err = plannerHookCall("indexDef.split",
indexDef, planPIndexesForIndex)
if skip {
continue
}
if err != nil {
return planPIndexes, err
}
indexDef = pho.IndexDef
planPIndexesForIndex = pho.PlanPIndexesForIndex
adjustedWeights := nodeWeights
// override the node weights for single partitioned index to
// favour balanced partition assignments.
if len(planPIndexesForIndex) == 1 {
// ensure that the node stickiness option is not enabled.
if enabled, found := options["enablePartitionNodeStickiness"]; !found ||
enabled == "false" {
adjustedWeights = NormaliseNodeWeights(nodeWeights,
planPIndexesPrev, len(planPIndexesPrev.PlanPIndexes))
}
}
// Once we have a 1 or more PlanPIndexes for an IndexDef, use
// blance to assign the PlanPIndexes to nodes.
warnings := BlancePlanPIndexes(mode, indexDef,
planPIndexesForIndex, existingPlans,
nodeUUIDsAll, nodeUUIDsToAdd, nodeUUIDsToRemove,
adjustedWeights, nodeHierarchy, true)
planPIndexes.Warnings[indexDef.Name] = []string{}
for partitionName, partitionWarning := range warnings {
if _, exists := planPIndexes.PlanPIndexes[partitionName]; exists {
if planPIndexes.PlanPIndexes[partitionName].IndexName == indexDef.Name {
planPIndexes.Warnings[indexDef.Name] =
append(planPIndexes.Warnings[indexDef.Name], partitionWarning...)
}
}
}
for _, warning := range warnings {
log.Printf("planner: indexDef.Name: %s,"+
" PlanNextMap warning: %s", indexDef.Name, warning)
}
// existingPlans should account for previous plans from all prior
// iterations, not just from before the rebalance.
for planName, plan := range planPIndexesForIndex {
existingPlans.PlanPIndexes[planName] = plan
}
_, _, err = plannerHookCall("indexDef.balanced",
indexDef, planPIndexesForIndex)
if err != nil {
return planPIndexes, err
}
}
_, _, err = plannerHookCall("end", nil, nil)
return planPIndexes, err
}
// Return nodes' UUIDs, weights and hierarchy.
func GetNodeWeightsAndHierarchy(nodeDefs *NodeDefs) (nodeUUIDs []string,
nodeWeights map[string]int, nodeHierarchy map[string]string,
) {
// Retrieve nodeUUID's, weights, and hierarchy from the current nodeDefs.
nodeUUIDs = make([]string, 0)
nodeWeights = make(map[string]int)
nodeHierarchy = make(map[string]string)
for _, nodeDef := range nodeDefs.NodeDefs {
tags := StringsToMap(nodeDef.Tags)
// Consider only nodeDef's that can support pindexes.
if tags == nil || tags["pindex"] {
nodeUUIDs = append(nodeUUIDs, nodeDef.UUID)
if nodeDef.Weight > 0 {
nodeWeights[nodeDef.UUID] = nodeDef.Weight
}
child := nodeDef.UUID
ancestors := strings.Split(nodeDef.Container, "/")
for i := len(ancestors) - 1; i >= 0; i-- {
if child != "" && ancestors[i] != "" {
nodeHierarchy[child] = ancestors[i]
}
child = ancestors[i]
}
}
}
return nodeUUIDs, nodeWeights, nodeHierarchy
}
// CalcNodesLayout computes information about the nodes based on the
// index definitions, node definitions, and the current plan.
func CalcNodesLayout(indexDefs *IndexDefs, nodeDefs *NodeDefs,
planPIndexesPrev *PlanPIndexes) (
nodeUUIDsAll []string,
nodeUUIDsToAdd []string,
nodeUUIDsToRemove []string,
nodeWeights map[string]int,
nodeHierarchy map[string]string,
) {
// Retrieve nodeUUID's, weights, and hierarchy from the current nodeDefs.
nodeUUIDs, nodeWeights, nodeHierarchy := GetNodeWeightsAndHierarchy(nodeDefs)
// Retrieve nodeUUID's from the previous plan.
nodeUUIDsPrev := make([]string, 0)
if planPIndexesPrev != nil {
for _, planPIndexPrev := range planPIndexesPrev.PlanPIndexes {
for nodeUUIDPrev := range planPIndexPrev.Nodes {
nodeUUIDsPrev = append(nodeUUIDsPrev, nodeUUIDPrev)
}
}
}
// Dedupe.
nodeUUIDsPrev = StringsIntersectStrings(nodeUUIDsPrev, nodeUUIDsPrev)
// Calculate node deltas (nodes added & nodes removed).
nodeUUIDsAll, nodeUUIDsToAdd, nodeUUIDsToRemove =
CalcNodesToAddRemove(nodeUUIDs, nodeUUIDsPrev)
return nodeUUIDsAll, nodeUUIDsToAdd, nodeUUIDsToRemove,
nodeWeights, nodeHierarchy
}
// Calculate node deltas (nodes added & nodes removed).
func CalcNodesToAddRemove(currentNodeUUIDs, prevNodeUUIDs []string) (nodeUUIDsAll,
nodeUUIDsToAdd, nodeUUIDsToRemove []string) {
// Calculate node deltas (nodes added & nodes removed).
nodeUUIDsAll = make([]string, 0)
nodeUUIDsAll = append(nodeUUIDsAll, currentNodeUUIDs...)
nodeUUIDsAll = append(nodeUUIDsAll, prevNodeUUIDs...)
nodeUUIDsAll = StringsIntersectStrings(nodeUUIDsAll, nodeUUIDsAll) // Dedupe.
nodeUUIDsToAdd = StringsRemoveStrings(nodeUUIDsAll, prevNodeUUIDs)
nodeUUIDsToRemove = StringsRemoveStrings(nodeUUIDsAll, currentNodeUUIDs)
sort.Strings(nodeUUIDsAll)
sort.Strings(nodeUUIDsToAdd)
sort.Strings(nodeUUIDsToRemove)
return nodeUUIDsAll, nodeUUIDsToAdd, nodeUUIDsToRemove
}
// Split an IndexDef into 1 or more PlanPIndex'es, assigning data
// source partitions from the IndexDef to a PlanPIndex based on
// modulus of MaxPartitionsPerPIndex.
//
// NOTE: If MaxPartitionsPerPIndex isn't a clean divisor of the total
// number of data source partitions (like 1024 split into clumps of
// 10), then one PIndex assigned to the remainder will be smaller than
// the other PIndexes (such as having only a remainder of 4 partitions
// rather than the usual 10 partitions per PIndex).
func SplitIndexDefIntoPlanPIndexes(indexDef *IndexDef, server string,
options map[string]string, planPIndexesOut *PlanPIndexes) (
map[string]*PlanPIndex, error) {
var sourcePartitionsArr []string
var err error
// TODO Check the bucket state instead, if possible?
// If changing to a resuming index, don't attempt to get source partitions
// since bucket might not be ready.
if !strings.HasPrefix(indexDef.HibernationPath, UNHIBERNATE_TASK) {
sourcePartitionsArr, err = DataSourcePartitions(indexDef.SourceType,
indexDef.SourceName, indexDef.SourceUUID, indexDef.SourceParams,
server, options)
if err != nil {
return nil, fmt.Errorf("planner: could not get partitions,"+
" indexDef.Name: %s, server: %s, err: %v",
indexDef.Name, server, err)
}
} else {
for _, sourcePartition := range strings.Split(options["resumeSourcePartitions"], ",") {
sourcePartitionsArr = append(sourcePartitionsArr, sourcePartition)
}
}
planPIndexesForIndex := map[string]*PlanPIndex{}
addPlanPIndex := func(sourcePartitionsCurr []string) int {
sourcePartitions := strings.Join(sourcePartitionsCurr, ",")
planPIndex := &PlanPIndex{
Name: PlanPIndexName(indexDef, sourcePartitions),
UUID: NewUUID(),
IndexType: indexDef.Type,
IndexName: indexDef.Name,
IndexUUID: indexDef.UUID,
IndexParams: indexDef.Params,
SourceType: indexDef.SourceType,
SourceName: indexDef.SourceName,
SourceUUID: indexDef.SourceUUID,
SourceParams: indexDef.SourceParams,
SourcePartitions: sourcePartitions,
Nodes: make(map[string]*PlanPIndexNode),
HibernationPath: indexDef.HibernationPath,
PlannerVersion: plannerVersion,
}
if planPIndexesOut != nil {
planPIndexesOut.PlanPIndexes[planPIndex.Name] = planPIndex
}
planPIndexesForIndex[planPIndex.Name] = planPIndex
return len(planPIndexesForIndex)
}
groupSourcePartitionsIntoPlanPIndexes(
indexDef.PlanParams.IndexPartitions,
indexDef.PlanParams.MaxPartitionsPerPIndex,
sourcePartitionsArr,
addPlanPIndex,
)
return planPIndexesForIndex, nil
}
func groupSourcePartitionsIntoPlanPIndexes(indexPartitions, maxPartitionsPerPIndex int,
sourcePartitionsArr []string, callback func([]string) int) {
var currPlanPIndexes int
if indexPartitions > 0 {
// IndexPartitions has been defined/determined, which takes precedence;
// So make certain the plan pindexes count matches this value.
minPartitionsPerPIndex := int(math.Floor(float64(len(sourcePartitionsArr)) / float64(indexPartitions)))
sourcePartitionsGroups := make([]int, indexPartitions)
for i := 0; i < indexPartitions; i++ {
sourcePartitionsGroups[i] = minPartitionsPerPIndex
}
// deficit will always be positive or zero
deficit := len(sourcePartitionsArr) - (indexPartitions * minPartitionsPerPIndex)
cursor := 0
for deficit > 0 {
sourcePartitionsGroups[cursor]++
deficit--
cursor = (cursor + 1) % len(sourcePartitionsGroups)
}
sourcePartitionsCurr := []string{}
cursor = 0
for _, sourcePartition := range sourcePartitionsArr {
sourcePartitionsCurr = append(sourcePartitionsCurr, sourcePartition)
if len(sourcePartitionsCurr) == sourcePartitionsGroups[cursor] {
_ = callback(sourcePartitionsCurr)
sourcePartitionsCurr = []string{}
cursor++
}
}
} else {
sourcePartitionsCurr := []string{}
for _, sourcePartition := range sourcePartitionsArr {
sourcePartitionsCurr = append(sourcePartitionsCurr, sourcePartition)
if maxPartitionsPerPIndex > 0 &&
len(sourcePartitionsCurr) >= maxPartitionsPerPIndex {
currPlanPIndexes = callback(sourcePartitionsCurr)
sourcePartitionsCurr = []string{}
}
}
if len(sourcePartitionsCurr) > 0 || // Assign any leftover partitions.
currPlanPIndexes <= 0 { // Assign at least 1 PlanPIndex.
callback(sourcePartitionsCurr)
}
}
}
// --------------------------------------------------------
// BlancePlanPIndexes invokes the blance library's generic
// PlanNextMap() algorithm to create a new pindex layout plan.
func BlancePlanPIndexes(mode string,
indexDef *IndexDef,
planPIndexesForIndex map[string]*PlanPIndex,
planPIndexesPrev *PlanPIndexes,
nodeUUIDsAll []string,
nodeUUIDsToAdd []string,
nodeUUIDsToRemove []string,
nodeWeights map[string]int,
nodeHierarchy map[string]string,
skipExistingPartitions bool) map[string][]string {
model, modelConstraints := BlancePartitionModel(indexDef)
// First, reconstruct previous blance map from planPIndexesPrev.
blancePrevMap := BlanceMap(planPIndexesForIndex, planPIndexesPrev, skipExistingPartitions)
blanceMapToAssign := BlanceMap(planPIndexesForIndex, planPIndexesPrev, true)
partitionWeights := indexDef.PlanParams.PIndexWeights
stateStickiness := map[string]int(nil)
if mode == "failover" {
stateStickiness = map[string]int{"primary": 100000}
}
// Compute nodeUUIDsAllForIndex by rotating the nodeUUIDsAll based
// on a function of index name, so that multiple indexes will have
// layouts that favor different starting nodes, but whose
// computation is repeatable.
var nodeUUIDsAllForIndex []string
h := crc32.NewIEEE()
io.WriteString(h, indexDef.Name)
next := sort.SearchStrings(nodeUUIDsAll, fmt.Sprintf("%x", h.Sum32()))
for range nodeUUIDsAll {
if next >= len(nodeUUIDsAll) {
next = 0
}
nodeUUIDsAllForIndex =
append(nodeUUIDsAllForIndex, nodeUUIDsAll[next])
next++
}
// If there are server groups/racks defined and there are no explicit
// hierarchyRules available then assume a rule which assigns
// the replica partitions to different server groups/racks.
// Node hierarchy would look like datacenter/serverGroup/nodeUUID
// where nodeUUIDs are at level zero, serverGroups are at level one
// and datacenter is at level two.
// HierarchyRules specify which levels to include and which levels to
// exclude while considering the replica assignments.
// eg: ExcludeLevel: 1 means skip the same rack allocations.
if indexDef.PlanParams.HierarchyRules == nil &&
len(nodeHierarchy) > 0 {
indexDef.PlanParams.HierarchyRules = blance.HierarchyRules{
"replica": []*blance.HierarchyRule{{
IncludeLevel: 2,
ExcludeLevel: 1}}}
}
blanceNextMap, warnings := blance.PlanNextMap(blancePrevMap, blanceMapToAssign,
nodeUUIDsAllForIndex, nodeUUIDsToRemove, nodeUUIDsToAdd,
model, modelConstraints,
partitionWeights,
stateStickiness,
nodeWeights,
nodeHierarchy,
indexDef.PlanParams.HierarchyRules)
for planPIndexName, blancePartition := range blanceNextMap {
// Update settings for new plan pindexes ONLY
planPIndex, exists := planPIndexesForIndex[planPIndexName]
if !exists {
continue
}
planPIndex.Nodes = map[string]*PlanPIndexNode{}
for i, nodeUUID := range blancePartition.NodesByState["primary"] {
if i >= model["primary"].Constraints {
break
}
canRead, canWrite := DefaultIndexCanRead, DefaultIndexCanWrite
nodePlanParam :=
GetNodePlanParam(indexDef.PlanParams.NodePlanParams,
nodeUUID, indexDef.Name, planPIndexName)
if nodePlanParam != nil {
canRead = nodePlanParam.CanRead
canWrite = nodePlanParam.CanWrite
}
planPIndex.Nodes[nodeUUID] = &PlanPIndexNode{
CanRead: canRead,
CanWrite: canWrite,
Priority: 0,
}
}
for i, nodeUUID := range blancePartition.NodesByState["replica"] {
if i >= model["replica"].Constraints {
break
}
canRead, canWrite := DefaultIndexCanRead, DefaultIndexCanWrite
nodePlanParam :=
GetNodePlanParam(indexDef.PlanParams.NodePlanParams,
nodeUUID, indexDef.Name, planPIndexName)
if nodePlanParam != nil {
canRead = nodePlanParam.CanRead
canWrite = nodePlanParam.CanWrite
}
planPIndex.Nodes[nodeUUID] = &PlanPIndexNode{
CanRead: canRead,
CanWrite: canWrite,
Priority: i + 1,
}
}
}
return warnings
}
// NormaliseNodeWeights updates the node weights reflective of the
// existing partition count on those nodes based on the given plans
// and update factor.
func NormaliseNodeWeights(nodeWeights map[string]int,
planPIndexes *PlanPIndexes, factor int) map[string]int {
if planPIndexes == nil ||
len(planPIndexes.PlanPIndexes) == 0 {
return nodeWeights
}
rv := make(map[string]int)
for uuid, wt := range nodeWeights {
rv[uuid] = wt
}
nodePartitionCount := make(map[string]int)
for _, pindex := range planPIndexes.PlanPIndexes {
for nodeUUID := range pindex.Nodes {
nodePartitionCount[nodeUUID]++
}
}
for uuid, count := range nodePartitionCount {
if weight, ok := nodeWeights[uuid]; ok {
rv[uuid] = -factor * (weight + count)