-
Notifications
You must be signed in to change notification settings - Fork 1
/
hot_resource.go
1166 lines (1043 loc) · 35.3 KB
/
hot_resource.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
package schedulers
import (
"fmt"
"math"
"math/rand"
"sort"
"strconv"
"sync"
"time"
"github.com/deepfabric/prophet/core"
"github.com/deepfabric/prophet/pb/metapb"
"github.com/deepfabric/prophet/schedule"
"github.com/deepfabric/prophet/schedule/filter"
"github.com/deepfabric/prophet/schedule/operator"
"github.com/deepfabric/prophet/schedule/opt"
"github.com/deepfabric/prophet/statistics"
"github.com/deepfabric/prophet/storage"
"github.com/deepfabric/prophet/util"
"github.com/prometheus/client_golang/prometheus"
)
func init() {
schedule.RegisterSliceDecoderBuilder(HotResourceType, func(args []string) schedule.ConfigDecoder {
return func(v interface{}) error {
return nil
}
})
schedule.RegisterScheduler(HotResourceType, func(opController *schedule.OperatorController, storage storage.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) {
conf := initHotResourceScheduleConfig()
if err := decoder(conf); err != nil {
return nil, err
}
conf.storage = storage
return newHotScheduler(opController, conf), nil
})
// FIXME: remove this two schedule after the balance test move in schedulers package
{
schedule.RegisterScheduler(HotWriteResourceType, func(opController *schedule.OperatorController, storage storage.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) {
return newHotWriteScheduler(opController, initHotResourceScheduleConfig()), nil
})
schedule.RegisterScheduler(HotReadResourceType, func(opController *schedule.OperatorController, storage storage.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) {
return newHotReadScheduler(opController, initHotResourceScheduleConfig()), nil
})
}
}
const (
// HotResourceName is balance hot resource scheduler name.
HotResourceName = "balance-hot-resource-scheduler"
// HotResourceType is balance hot resource scheduler type.
HotResourceType = "hot-resource"
// HotReadResourceType is hot read resource scheduler type.
HotReadResourceType = "hot-read-resource"
// HotWriteResourceType is hot write resource scheduler type.
HotWriteResourceType = "hot-write-resource"
minHotScheduleInterval = time.Second
maxHotScheduleInterval = 20 * time.Second
)
// schedulePeerPr the probability of schedule the hot peer.
var schedulePeerPr = 0.66
type hotScheduler struct {
name string
*BaseScheduler
sync.RWMutex
leaderLimit uint64
peerLimit uint64
types []rwType
r *rand.Rand
// states across multiple `Schedule` calls
pendings [resourceTypeLen]map[*pendingInfluence]struct{}
// resourcePendings containers resourceID -> [opType]Operator
// this records resourceID which have pending Operator by operation type. During filterHotPeers, the hot peers won't
// be selected if its owner resource is tracked in this attribute.
resourcePendings map[uint64][2]*operator.Operator
// temporary states but exported to API or metrics
stLoadInfos [resourceTypeLen]map[uint64]*containerLoadDetail
// pendingSums indicates the [resourceType] containerID -> pending Influence
// This containers the pending Influence for each container by resource type.
pendingSums [resourceTypeLen]map[uint64]Influence
// config of hot scheduler
conf *hotResourceSchedulerConfig
}
func newHotScheduler(opController *schedule.OperatorController, conf *hotResourceSchedulerConfig) *hotScheduler {
base := NewBaseScheduler(opController)
ret := &hotScheduler{
name: HotResourceName,
BaseScheduler: base,
leaderLimit: 1,
peerLimit: 1,
types: []rwType{write, read},
r: rand.New(rand.NewSource(time.Now().UnixNano())),
resourcePendings: make(map[uint64][2]*operator.Operator),
conf: conf,
}
for ty := resourceType(0); ty < resourceTypeLen; ty++ {
ret.pendings[ty] = map[*pendingInfluence]struct{}{}
ret.stLoadInfos[ty] = map[uint64]*containerLoadDetail{}
}
return ret
}
func newHotReadScheduler(opController *schedule.OperatorController, conf *hotResourceSchedulerConfig) *hotScheduler {
ret := newHotScheduler(opController, conf)
ret.name = ""
ret.types = []rwType{read}
return ret
}
func newHotWriteScheduler(opController *schedule.OperatorController, conf *hotResourceSchedulerConfig) *hotScheduler {
ret := newHotScheduler(opController, conf)
ret.name = ""
ret.types = []rwType{write}
return ret
}
func (h *hotScheduler) GetName() string {
return h.name
}
func (h *hotScheduler) GetType() string {
return HotResourceType
}
func (h *hotScheduler) GetMinInterval() time.Duration {
return minHotScheduleInterval
}
func (h *hotScheduler) GetNextInterval(interval time.Duration) time.Duration {
return intervalGrow(h.GetMinInterval(), maxHotScheduleInterval, exponentialGrowth)
}
func (h *hotScheduler) IsScheduleAllowed(cluster opt.Cluster) bool {
return h.allowBalanceLeader(cluster) || h.allowBalanceResource(cluster)
}
func (h *hotScheduler) allowBalanceLeader(cluster opt.Cluster) bool {
return h.OpController.OperatorCount(operator.OpHotResource) < cluster.GetOpts().GetHotResourceScheduleLimit() &&
h.OpController.OperatorCount(operator.OpLeader) < cluster.GetOpts().GetLeaderScheduleLimit()
}
func (h *hotScheduler) allowBalanceResource(cluster opt.Cluster) bool {
return h.OpController.OperatorCount(operator.OpHotResource) < cluster.GetOpts().GetHotResourceScheduleLimit()
}
func (h *hotScheduler) Schedule(cluster opt.Cluster) []*operator.Operator {
schedulerCounter.WithLabelValues(h.GetName(), "schedule").Inc()
return h.dispatch(h.types[h.r.Int()%len(h.types)], cluster)
}
func (h *hotScheduler) dispatch(typ rwType, cluster opt.Cluster) []*operator.Operator {
h.Lock()
defer h.Unlock()
h.prepareForBalance(cluster)
switch typ {
case read:
return h.balanceHotReadResources(cluster)
case write:
return h.balanceHotWriteResources(cluster)
}
return nil
}
// prepareForBalance calculate the summary of pending Influence for each container and prepare the load detail for
// each container
func (h *hotScheduler) prepareForBalance(cluster opt.Cluster) {
h.summaryPendingInfluence()
containersStat := cluster.GetContainersStats()
minHotDegree := cluster.GetOpts().GetHotResourceCacheHitsThreshold()
{ // update read statistics
resourceRead := cluster.ResourceReadStats()
containerByte := containersStat.GetContainersBytesReadStat()
containerKey := containersStat.GetContainersKeysReadStat()
h.stLoadInfos[readLeader] = summaryContainersLoad(
containerByte,
containerKey,
h.pendingSums[readLeader],
resourceRead,
minHotDegree,
read, metapb.ResourceKind_LeaderKind)
}
{ // update write statistics
resourceWrite := cluster.ResourceWriteStats()
containerByte := containersStat.GetContainersBytesWriteStat()
containerKey := containersStat.GetContainersKeysWriteStat()
h.stLoadInfos[writeLeader] = summaryContainersLoad(
containerByte,
containerKey,
h.pendingSums[writeLeader],
resourceWrite,
minHotDegree,
write, metapb.ResourceKind_LeaderKind)
h.stLoadInfos[writePeer] = summaryContainersLoad(
containerByte,
containerKey,
h.pendingSums[writePeer],
resourceWrite,
minHotDegree,
write, metapb.ResourceKind_ReplicaKind)
}
}
// summaryPendingInfluence calculate the summary of pending Influence for each container
// and clean the resource from resourceInfluence if they have ended operator.
func (h *hotScheduler) summaryPendingInfluence() {
for ty := resourceType(0); ty < resourceTypeLen; ty++ {
h.pendingSums[ty] = summaryPendingInfluence(h.pendings[ty], h.calcPendingWeight)
}
h.gcResourcePendings()
}
// gcResourcePendings check the resource whether it need to be deleted from resourcePendings depended on whether it have
// ended operator
func (h *hotScheduler) gcResourcePendings() {
for resID, pendings := range h.resourcePendings {
empty := true
for ty, op := range pendings {
if op != nil && op.IsEnd() {
if time.Now().After(op.GetCreateTime().Add(h.conf.GetMaxZombieDuration())) {
schedulerStatus.WithLabelValues(h.GetName(), "pending_op_infos").Dec()
pendings[ty] = nil
}
}
if pendings[ty] != nil {
empty = false
}
}
if empty {
delete(h.resourcePendings, resID)
} else {
h.resourcePendings[resID] = pendings
}
}
}
// summaryContainersLoad Load information of all available containers.
// it will filtered the hot peer and calculate the current and future stat(byte/key rate,count) for each container
func summaryContainersLoad(
containerByteRate map[uint64]float64,
containerKeyRate map[uint64]float64,
containerPendings map[uint64]Influence,
containerHotPeers map[uint64][]*statistics.HotPeerStat,
minHotDegree int,
rwTy rwType,
kind metapb.ResourceKind,
) map[uint64]*containerLoadDetail {
// loadDetail containers the containerID -> hotPeers stat and its current and future stat(key/byte rate,count)
loadDetail := make(map[uint64]*containerLoadDetail, len(containerByteRate))
allByteSum := 0.0
allKeySum := 0.0
allCount := 0.0
// Containers without byte rate statistics is not available to schedule.
for id, byteRate := range containerByteRate {
keyRate := containerKeyRate[id]
// Find all hot peers first
var hotPeers []*statistics.HotPeerStat
{
byteSum := 0.0
keySum := 0.0
for _, peer := range filterHotPeers(kind, minHotDegree, containerHotPeers[id]) {
byteSum += peer.GetByteRate()
keySum += peer.GetKeyRate()
hotPeers = append(hotPeers, peer.Clone())
}
// Use sum of hot peers to estimate leader-only byte rate.
// For write requests, Write{Bytes, Keys} is applied to all Peers at the same time, while the Leader and Follower are under different loads (usually the Leader consumes more CPU).
// But none of the current dimension reflect this difference, so we create a new dimension to reflect it.
if kind == metapb.ResourceKind_LeaderKind && rwTy == write {
byteRate = byteSum
keyRate = keySum
}
// Metric for debug.
{
ty := "byte-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(byteSum)
}
{
ty := "key-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(keySum)
}
}
allByteSum += byteRate
allKeySum += keyRate
allCount += float64(len(hotPeers))
// Build container load prediction from current load and pending influence.
stLoadPred := (&containerLoad{
ByteRate: byteRate,
KeyRate: keyRate,
Count: float64(len(hotPeers)),
}).ToLoadPred(containerPendings[id])
// Construct container load info.
loadDetail[id] = &containerLoadDetail{
LoadPred: stLoadPred,
HotPeers: hotPeers,
}
}
containerLen := float64(len(containerByteRate))
// container expectation byte/key rate and count for each container-load detail.
for id, detail := range loadDetail {
byteExp := allByteSum / containerLen
keyExp := allKeySum / containerLen
countExp := allCount / containerLen
detail.LoadPred.Expect.ByteRate = byteExp
detail.LoadPred.Expect.KeyRate = keyExp
detail.LoadPred.Expect.Count = countExp
// Debug
{
ty := "exp-byte-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(byteExp)
}
{
ty := "exp-key-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(keyExp)
}
{
ty := "exp-count-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(countExp)
}
}
return loadDetail
}
// filterHotPeers filter the peer whose hot degree is less than minHotDegress
func filterHotPeers(
kind metapb.ResourceKind,
minHotDegree int,
peers []*statistics.HotPeerStat,
) []*statistics.HotPeerStat {
var ret []*statistics.HotPeerStat
for _, peer := range peers {
if (kind == metapb.ResourceKind_LeaderKind && !peer.IsLeader()) ||
peer.HotDegree < minHotDegree {
continue
}
ret = append(ret, peer)
}
return ret
}
func (h *hotScheduler) addPendingInfluence(op *operator.Operator, srcContainer, dstContainer uint64, infl Influence, rwTy rwType, opTy opType) bool {
resID := op.ResourceID()
_, ok := h.resourcePendings[resID]
if ok {
schedulerStatus.WithLabelValues(h.GetName(), "pending_op_fails").Inc()
return false
}
influence := newPendingInfluence(op, srcContainer, dstContainer, infl)
rcTy := toResourceType(rwTy, opTy)
h.pendings[rcTy][influence] = struct{}{}
h.resourcePendings[resID] = [2]*operator.Operator{nil, nil}
{ // h.pendingOpInfos[resourceID][ty] = influence
tmp := h.resourcePendings[resID]
tmp[opTy] = op
h.resourcePendings[resID] = tmp
}
schedulerStatus.WithLabelValues(h.GetName(), "pending_op_create").Inc()
return true
}
func (h *hotScheduler) balanceHotReadResources(cluster opt.Cluster) []*operator.Operator {
// prefer to balance by leader
leaderSolver := newBalanceSolver(h, cluster, read, transferLeader)
ops := leaderSolver.solve()
if len(ops) > 0 {
return ops
}
peerSolver := newBalanceSolver(h, cluster, read, movePeer)
ops = peerSolver.solve()
if len(ops) > 0 {
return ops
}
schedulerCounter.WithLabelValues(h.GetName(), "skip").Inc()
return nil
}
func (h *hotScheduler) balanceHotWriteResources(cluster opt.Cluster) []*operator.Operator {
// prefer to balance by peer
s := h.r.Intn(100)
switch {
case s < int(schedulePeerPr*100):
peerSolver := newBalanceSolver(h, cluster, write, movePeer)
ops := peerSolver.solve()
if len(ops) > 0 {
return ops
}
default:
}
leaderSolver := newBalanceSolver(h, cluster, write, transferLeader)
ops := leaderSolver.solve()
if len(ops) > 0 {
return ops
}
schedulerCounter.WithLabelValues(h.GetName(), "skip").Inc()
return nil
}
type balanceSolver struct {
sche *hotScheduler
cluster opt.Cluster
stLoadDetail map[uint64]*containerLoadDetail
rwTy rwType
opTy opType
cur *solution
maxSrc *containerLoad
minDst *containerLoad
rankStep *containerLoad
}
type solution struct {
srcContainerID uint64
srcPeerStat *statistics.HotPeerStat
resource *core.CachedResource
dstContainerID uint64
// progressiveRank measures the contribution for balance.
// The smaller the rank, the better this solution is.
// If rank < 0, this solution makes thing better.
progressiveRank int64
}
func (bs *balanceSolver) init() {
switch toResourceType(bs.rwTy, bs.opTy) {
case writePeer:
bs.stLoadDetail = bs.sche.stLoadInfos[writePeer]
case writeLeader:
bs.stLoadDetail = bs.sche.stLoadInfos[writeLeader]
case readLeader:
bs.stLoadDetail = bs.sche.stLoadInfos[readLeader]
}
// And it will be unnecessary to filter unhealthy container, because it has been solved in process heartbeat
bs.maxSrc = &containerLoad{}
bs.minDst = &containerLoad{
ByteRate: math.MaxFloat64,
KeyRate: math.MaxFloat64,
Count: math.MaxFloat64,
}
maxCur := &containerLoad{}
for _, detail := range bs.stLoadDetail {
bs.maxSrc = maxLoad(bs.maxSrc, detail.LoadPred.min())
bs.minDst = minLoad(bs.minDst, detail.LoadPred.max())
maxCur = maxLoad(maxCur, &detail.LoadPred.Current)
}
bs.rankStep = &containerLoad{
ByteRate: maxCur.ByteRate * bs.sche.conf.GetByteRankStepRatio(),
KeyRate: maxCur.KeyRate * bs.sche.conf.GetKeyRankStepRatio(),
Count: maxCur.Count * bs.sche.conf.GetCountRankStepRatio(),
}
}
func newBalanceSolver(sche *hotScheduler, cluster opt.Cluster, rwTy rwType, opTy opType) *balanceSolver {
solver := &balanceSolver{
sche: sche,
cluster: cluster,
rwTy: rwTy,
opTy: opTy,
}
solver.init()
return solver
}
func (bs *balanceSolver) isValid() bool {
if bs.cluster == nil || bs.sche == nil || bs.stLoadDetail == nil {
return false
}
switch bs.rwTy {
case write, read:
default:
return false
}
switch bs.opTy {
case movePeer, transferLeader:
default:
return false
}
return true
}
// solve travels all the src containers, hot peers, dst containers and select each one of them to make a best scheduling solution.
// The comparing between solutions is based on calcProgressiveRank.
func (bs *balanceSolver) solve() []*operator.Operator {
if !bs.isValid() || !bs.allowBalance() {
return nil
}
bs.cur = &solution{}
var (
best *solution
ops []*operator.Operator
infls []Influence
)
for srcContainerID := range bs.filterSrcContainers() {
bs.cur.srcContainerID = srcContainerID
for _, srcPeerStat := range bs.filterHotPeers() {
bs.cur.srcPeerStat = srcPeerStat
bs.cur.resource = bs.getResource()
if bs.cur.resource == nil {
continue
}
for dstContainerID := range bs.filterDstContainers() {
bs.cur.dstContainerID = dstContainerID
bs.calcProgressiveRank()
if bs.cur.progressiveRank < 0 && bs.betterThan(best) {
if newOps, newInfls := bs.buildOperators(); len(newOps) > 0 {
ops = newOps
infls = newInfls
clone := *bs.cur
best = &clone
}
}
}
}
}
for i := 0; i < len(ops); i++ {
// TODO: multiple operators need to be atomic.
if !bs.sche.addPendingInfluence(ops[i], best.srcContainerID, best.dstContainerID, infls[i], bs.rwTy, bs.opTy) {
return nil
}
}
return ops
}
// allowBalance check whether the operator count have exceed the hot resource limit by type
func (bs *balanceSolver) allowBalance() bool {
switch bs.opTy {
case movePeer:
return bs.sche.allowBalanceResource(bs.cluster)
case transferLeader:
return bs.sche.allowBalanceLeader(bs.cluster)
default:
return false
}
}
// filterSrcContainers compare the min rate and the ratio * expectation rate, if both key and byte rate is greater than
// its expectation * ratio, the container would be selected as hot source container
func (bs *balanceSolver) filterSrcContainers() map[uint64]*containerLoadDetail {
ret := make(map[uint64]*containerLoadDetail)
for id, detail := range bs.stLoadDetail {
if bs.cluster.GetContainer(id) == nil {
util.GetLogger().Errorf("get the source container %d failed with not found",
id)
continue
}
if len(detail.HotPeers) == 0 {
continue
}
if detail.LoadPred.min().ByteRate > bs.sche.conf.GetSrcToleranceRatio()*detail.LoadPred.Expect.ByteRate &&
detail.LoadPred.min().KeyRate > bs.sche.conf.GetSrcToleranceRatio()*detail.LoadPred.Expect.KeyRate {
ret[id] = detail
hotSchedulerResultCounter.WithLabelValues("src-container-succ", strconv.FormatUint(id, 10)).Inc()
}
hotSchedulerResultCounter.WithLabelValues("src-container-failed", strconv.FormatUint(id, 10)).Inc()
}
return ret
}
// filterHotPeers filtered hot peers from statistics.HotPeerStat and deleted the peer if its resource is in pending status.
// The returned hotPeer count in controlled by `max-peer-number`.
func (bs *balanceSolver) filterHotPeers() []*statistics.HotPeerStat {
ret := bs.stLoadDetail[bs.cur.srcContainerID].HotPeers
// Return at most MaxPeerNum peers, to prevent balanceSolver.solve() too slow.
maxPeerNum := bs.sche.conf.GetMaxPeerNumber()
// filter pending resource
appendItem := func(items []*statistics.HotPeerStat, item *statistics.HotPeerStat) []*statistics.HotPeerStat {
if _, ok := bs.sche.resourcePendings[item.ID()]; !ok {
items = append(items, item)
}
return items
}
if len(ret) <= maxPeerNum {
nret := make([]*statistics.HotPeerStat, 0, len(ret))
for _, peer := range ret {
nret = appendItem(nret, peer)
}
return nret
}
byteSort := make([]*statistics.HotPeerStat, len(ret))
copy(byteSort, ret)
sort.Slice(byteSort, func(i, j int) bool {
return byteSort[i].GetByteRate() > byteSort[j].GetByteRate()
})
keySort := make([]*statistics.HotPeerStat, len(ret))
copy(keySort, ret)
sort.Slice(keySort, func(i, j int) bool {
return keySort[i].GetKeyRate() > keySort[j].GetKeyRate()
})
union := make(map[*statistics.HotPeerStat]struct{}, maxPeerNum)
for len(union) < maxPeerNum {
for len(byteSort) > 0 {
peer := byteSort[0]
byteSort = byteSort[1:]
if _, ok := union[peer]; !ok {
union[peer] = struct{}{}
break
}
}
for len(keySort) > 0 {
peer := keySort[0]
keySort = keySort[1:]
if _, ok := union[peer]; !ok {
union[peer] = struct{}{}
break
}
}
}
ret = make([]*statistics.HotPeerStat, 0, len(union))
for peer := range union {
ret = appendItem(ret, peer)
}
return ret
}
// isResourceAvailable checks whether the given resource is not available to schedule.
func (bs *balanceSolver) isResourceAvailable(res *core.CachedResource) bool {
if res == nil {
schedulerCounter.WithLabelValues(bs.sche.GetName(), "no-resource").Inc()
return false
}
if pendings, ok := bs.sche.resourcePendings[res.Meta.ID()]; ok {
if bs.opTy == transferLeader {
return false
}
if pendings[movePeer] != nil ||
(pendings[transferLeader] != nil && !pendings[transferLeader].IsEnd()) {
return false
}
}
if !opt.IsHealthyAllowPending(bs.cluster, res) {
schedulerCounter.WithLabelValues(bs.sche.GetName(), "unhealthy-replica").Inc()
return false
}
if !opt.IsResourceReplicated(bs.cluster, res) {
util.GetLogger().Debugf("resource %d has abnormal replica count, scheduler %s",
res.Meta.ID(),
res.Meta.ID())
schedulerCounter.WithLabelValues(bs.sche.GetName(), "abnormal-replica").Inc()
return false
}
return true
}
func (bs *balanceSolver) getResource() *core.CachedResource {
res := bs.cluster.GetResource(bs.cur.srcPeerStat.ID())
if !bs.isResourceAvailable(res) {
return nil
}
switch bs.opTy {
case movePeer:
_, ok := res.GetContainerPeer(bs.cur.srcContainerID)
if !ok {
util.GetLogger().Debugf("resource %d does not have a peer on source container, maybe stat out of date",
bs.cur.srcPeerStat.ID())
return nil
}
case transferLeader:
if res.GetLeader().GetContainerID() != bs.cur.srcContainerID {
util.GetLogger().Debugf("resource %d leader is not on source container, maybe stat out of date",
bs.cur.srcPeerStat.ID())
return nil
}
default:
return nil
}
return res
}
// filterDstContainers select the candidate container by filters
func (bs *balanceSolver) filterDstContainers() map[uint64]*containerLoadDetail {
var (
filters []filter.Filter
candidates []*core.CachedContainer
)
srcContainer := bs.cluster.GetContainer(bs.cur.srcContainerID)
if srcContainer == nil {
return nil
}
switch bs.opTy {
case movePeer:
filters = []filter.Filter{
&filter.ContainerStateFilter{ActionScope: bs.sche.GetName(), MoveResource: true},
filter.NewExcludedFilter(bs.sche.GetName(), bs.cur.resource.GetContainerIDs(), bs.cur.resource.GetContainerIDs()),
filter.NewSpecialUseFilter(bs.sche.GetName(), filter.SpecialUseHotResource),
filter.NewPlacementSafeguard(bs.sche.GetName(), bs.cluster, bs.cur.resource, srcContainer, bs.cluster.GetResourceFactory()),
}
candidates = bs.cluster.GetContainers()
case transferLeader:
filters = []filter.Filter{
&filter.ContainerStateFilter{ActionScope: bs.sche.GetName(), TransferLeader: true},
filter.NewSpecialUseFilter(bs.sche.GetName(), filter.SpecialUseHotResource),
}
if leaderFilter := filter.NewPlacementLeaderSafeguard(bs.sche.GetName(), bs.cluster, bs.cur.resource, srcContainer, bs.cluster.GetResourceFactory()); leaderFilter != nil {
filters = append(filters, leaderFilter)
}
candidates = bs.cluster.GetFollowerContainers(bs.cur.resource)
default:
return nil
}
return bs.pickDstContainers(filters, candidates)
}
func (bs *balanceSolver) pickDstContainers(filters []filter.Filter, candidates []*core.CachedContainer) map[uint64]*containerLoadDetail {
ret := make(map[uint64]*containerLoadDetail, len(candidates))
dstToleranceRatio := bs.sche.conf.GetDstToleranceRatio()
for _, container := range candidates {
if filter.Target(bs.cluster.GetOpts(), container, filters) {
detail := bs.stLoadDetail[container.Meta.ID()]
if detail.LoadPred.max().ByteRate*dstToleranceRatio < detail.LoadPred.Expect.ByteRate &&
detail.LoadPred.max().KeyRate*dstToleranceRatio < detail.LoadPred.Expect.KeyRate {
ret[container.Meta.ID()] = bs.stLoadDetail[container.Meta.ID()]
hotSchedulerResultCounter.WithLabelValues("dst-container-succ", strconv.FormatUint(container.Meta.ID(), 10)).Inc()
}
hotSchedulerResultCounter.WithLabelValues("dst-container-fail", strconv.FormatUint(container.Meta.ID(), 10)).Inc()
}
}
return ret
}
// calcProgressiveRank calculates `bs.cur.progressiveRank`.
// See the comments of `solution.progressiveRank` for more about progressive rank.
func (bs *balanceSolver) calcProgressiveRank() {
srcLd := bs.stLoadDetail[bs.cur.srcContainerID].LoadPred.min()
dstLd := bs.stLoadDetail[bs.cur.dstContainerID].LoadPred.max()
peer := bs.cur.srcPeerStat
rank := int64(0)
if bs.rwTy == write && bs.opTy == transferLeader {
// In this condition, CPU usage is the matter.
// Only consider about key rate.
if srcLd.KeyRate >= dstLd.KeyRate+peer.GetKeyRate() {
rank = -1
}
} else {
getSrcDecRate := func(a, b float64) float64 {
if a-b <= 0 {
return 1
}
return a - b
}
// we use DecRatio(Decline Ratio) to expect that the dst container's (key/byte) rate should still be less
// than the src container's (key/byte) rate after scheduling one peer.
keyDecRatio := (dstLd.KeyRate + peer.GetKeyRate()) / getSrcDecRate(srcLd.KeyRate, peer.GetKeyRate())
keyHot := peer.GetKeyRate() >= bs.sche.conf.GetMinHotKeyRate()
byteDecRatio := (dstLd.ByteRate + peer.GetByteRate()) / getSrcDecRate(srcLd.ByteRate, peer.GetByteRate())
byteHot := peer.GetByteRate() > bs.sche.conf.GetMinHotByteRate()
greatDecRatio, minorDecRatio := bs.sche.conf.GetGreatDecRatio(), bs.sche.conf.GetMinorGreatDecRatio()
switch {
case byteHot && byteDecRatio <= greatDecRatio && keyHot && keyDecRatio <= greatDecRatio:
// Both byte rate and key rate are balanced, the best choice.
rank = -3
case byteDecRatio <= minorDecRatio && keyHot && keyDecRatio <= greatDecRatio:
// Byte rate is not worsened, key rate is balanced.
rank = -2
case byteHot && byteDecRatio <= greatDecRatio:
// Byte rate is balanced, ignore the key rate.
rank = -1
}
}
bs.cur.progressiveRank = rank
}
// betterThan checks if `bs.cur` is a better solution than `old`.
func (bs *balanceSolver) betterThan(old *solution) bool {
if old == nil {
return true
}
switch {
case bs.cur.progressiveRank < old.progressiveRank:
return true
case bs.cur.progressiveRank > old.progressiveRank:
return false
}
if r := bs.compareSrcContainer(bs.cur.srcContainerID, old.srcContainerID); r < 0 {
return true
} else if r > 0 {
return false
}
if r := bs.compareDstContainer(bs.cur.dstContainerID, old.dstContainerID); r < 0 {
return true
} else if r > 0 {
return false
}
if bs.cur.srcPeerStat != old.srcPeerStat {
// compare resource
if bs.rwTy == write && bs.opTy == transferLeader {
switch {
case bs.cur.srcPeerStat.GetKeyRate() > old.srcPeerStat.GetKeyRate():
return true
case bs.cur.srcPeerStat.GetKeyRate() < old.srcPeerStat.GetKeyRate():
return false
}
} else {
byteRkCmp := rankCmp(bs.cur.srcPeerStat.GetByteRate(), old.srcPeerStat.GetByteRate(), stepRank(0, 100))
keyRkCmp := rankCmp(bs.cur.srcPeerStat.GetKeyRate(), old.srcPeerStat.GetKeyRate(), stepRank(0, 10))
switch bs.cur.progressiveRank {
case -2: // greatDecRatio < byteDecRatio <= minorDecRatio && keyDecRatio <= greatDecRatio
if keyRkCmp != 0 {
return keyRkCmp > 0
}
if byteRkCmp != 0 {
// prefer smaller byte rate, to reduce oscillation
return byteRkCmp < 0
}
case -3: // byteDecRatio <= greatDecRatio && keyDecRatio <= greatDecRatio
if keyRkCmp != 0 {
return keyRkCmp > 0
}
fallthrough
case -1: // byteDecRatio <= greatDecRatio
if byteRkCmp != 0 {
// prefer resource with larger byte rate, to converge faster
return byteRkCmp > 0
}
}
}
}
return false
}
// smaller is better
func (bs *balanceSolver) compareSrcContainer(st1, st2 uint64) int {
if st1 != st2 {
// compare source container
var lpCmp containerLPCmp
if bs.rwTy == write && bs.opTy == transferLeader {
lpCmp = sliceLPCmp(
minLPCmp(negLoadCmp(sliceLoadCmp(
stLdRankCmp(stLdKeyRate, stepRank(bs.maxSrc.KeyRate, bs.rankStep.KeyRate)),
stLdRankCmp(stLdByteRate, stepRank(bs.maxSrc.ByteRate, bs.rankStep.ByteRate)),
))),
diffCmp(sliceLoadCmp(
stLdRankCmp(stLdCount, stepRank(0, bs.rankStep.Count)),
stLdRankCmp(stLdKeyRate, stepRank(0, bs.rankStep.KeyRate)),
stLdRankCmp(stLdByteRate, stepRank(0, bs.rankStep.ByteRate)),
)),
)
} else {
lpCmp = sliceLPCmp(
minLPCmp(negLoadCmp(sliceLoadCmp(
stLdRankCmp(stLdByteRate, stepRank(bs.maxSrc.ByteRate, bs.rankStep.ByteRate)),
stLdRankCmp(stLdKeyRate, stepRank(bs.maxSrc.KeyRate, bs.rankStep.KeyRate)),
))),
diffCmp(
stLdRankCmp(stLdByteRate, stepRank(0, bs.rankStep.ByteRate)),
),
)
}
lp1 := bs.stLoadDetail[st1].LoadPred
lp2 := bs.stLoadDetail[st2].LoadPred
return lpCmp(lp1, lp2)
}
return 0
}
// smaller is better
func (bs *balanceSolver) compareDstContainer(st1, st2 uint64) int {
if st1 != st2 {
// compare destination container
var lpCmp containerLPCmp
if bs.rwTy == write && bs.opTy == transferLeader {
lpCmp = sliceLPCmp(
maxLPCmp(sliceLoadCmp(
stLdRankCmp(stLdKeyRate, stepRank(bs.minDst.KeyRate, bs.rankStep.KeyRate)),
stLdRankCmp(stLdByteRate, stepRank(bs.minDst.ByteRate, bs.rankStep.ByteRate)),
)),
diffCmp(sliceLoadCmp(
stLdRankCmp(stLdCount, stepRank(0, bs.rankStep.Count)),
stLdRankCmp(stLdKeyRate, stepRank(0, bs.rankStep.KeyRate)),
stLdRankCmp(stLdByteRate, stepRank(0, bs.rankStep.ByteRate)),
)))
} else {
lpCmp = sliceLPCmp(
maxLPCmp(sliceLoadCmp(
stLdRankCmp(stLdByteRate, stepRank(bs.minDst.ByteRate, bs.rankStep.ByteRate)),
stLdRankCmp(stLdKeyRate, stepRank(bs.minDst.KeyRate, bs.rankStep.KeyRate)),
)),
diffCmp(
stLdRankCmp(stLdByteRate, stepRank(0, bs.rankStep.ByteRate)),
),
)
}
lp1 := bs.stLoadDetail[st1].LoadPred
lp2 := bs.stLoadDetail[st2].LoadPred
return lpCmp(lp1, lp2)
}
return 0
}
func stepRank(rk0 float64, step float64) func(float64) int64 {
return func(rate float64) int64 {
return int64((rate - rk0) / step)
}
}
func (bs *balanceSolver) isReadyToBuild() bool {
if bs.cur.srcContainerID == 0 || bs.cur.dstContainerID == 0 ||
bs.cur.srcPeerStat == nil || bs.cur.resource == nil {
return false
}
if bs.cur.srcContainerID != bs.cur.srcPeerStat.ContainerID ||
bs.cur.resource.Meta.ID() != bs.cur.srcPeerStat.ID() {
return false
}
return true
}
func (bs *balanceSolver) buildOperators() ([]*operator.Operator, []Influence) {
if !bs.isReadyToBuild() {
return nil, nil
}
var (
op *operator.Operator
counters []prometheus.Counter
err error
)
switch bs.opTy {
case movePeer:
srcPeer, _ := bs.cur.resource.GetContainerPeer(bs.cur.srcContainerID) // checked in getResourceAndSrcPeer
dstPeer := metapb.Peer{ContainerID: bs.cur.dstContainerID, Role: srcPeer.Role}
desc := "move-hot-" + bs.rwTy.String() + "-peer"
op, err = operator.CreateMovePeerOperator(
desc,
bs.cluster,
bs.cur.resource,
operator.OpHotResource,
bs.cur.srcContainerID,
dstPeer)
counters = append(counters,
hotDirectionCounter.WithLabelValues("move-peer", bs.rwTy.String(), strconv.FormatUint(bs.cur.srcContainerID, 10), "out"),
hotDirectionCounter.WithLabelValues("move-peer", bs.rwTy.String(), strconv.FormatUint(dstPeer.GetContainerID(), 10), "in"))
case transferLeader:
if _, ok := bs.cur.resource.GetContainerVoter(bs.cur.dstContainerID); !ok {
return nil, nil
}
desc := "transfer-hot-" + bs.rwTy.String() + "-leader"
op, err = operator.CreateTransferLeaderOperator(
desc,
bs.cluster,
bs.cur.resource,
bs.cur.srcContainerID,
bs.cur.dstContainerID,
operator.OpHotResource)
counters = append(counters,