From 362910ba4602d8059f977d0b63bb824b2aeb6720 Mon Sep 17 00:00:00 2001 From: HunDunDM Date: Mon, 5 Jul 2021 19:21:55 +0800 Subject: [PATCH 1/3] scheduler: move part of the code of the hot-region-scheduler to the test package Signed-off-by: HunDunDM --- server/schedulers/hot_region.go | 25 ------------------- .../{hot_test.go => hot_region_test.go} | 20 +++++++++++++++ 2 files changed, 20 insertions(+), 25 deletions(-) rename server/schedulers/{hot_test.go => hot_region_test.go} (98%) diff --git a/server/schedulers/hot_region.go b/server/schedulers/hot_region.go index 3178e8fa8e8..be3676c1680 100644 --- a/server/schedulers/hot_region.go +++ b/server/schedulers/hot_region.go @@ -51,17 +51,6 @@ func init() { conf.storage = storage return newHotScheduler(opController, conf), nil }) - - // FIXME: remove this two schedule after the balance test move in schedulers package - { - schedule.RegisterScheduler(HotWriteRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { - return newHotWriteScheduler(opController, initHotRegionScheduleConfig()), nil - }) - schedule.RegisterScheduler(HotReadRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { - return newHotReadScheduler(opController, initHotRegionScheduleConfig()), nil - }) - - } } const ( @@ -124,20 +113,6 @@ func newHotScheduler(opController *schedule.OperatorController, conf *hotRegionS return ret } -func newHotReadScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { - ret := newHotScheduler(opController, conf) - ret.name = "" - ret.types = []rwType{read} - return ret -} - -func newHotWriteScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { - ret := newHotScheduler(opController, conf) - ret.name = "" - ret.types = []rwType{write} - return ret -} - func (h *hotScheduler) GetName() string { return h.name } diff --git a/server/schedulers/hot_test.go b/server/schedulers/hot_region_test.go similarity index 98% rename from server/schedulers/hot_test.go rename to server/schedulers/hot_region_test.go index b40478676d8..891ad998d45 100644 --- a/server/schedulers/hot_test.go +++ b/server/schedulers/hot_region_test.go @@ -34,6 +34,26 @@ import ( func init() { schedulePeerPr = 1.0 + schedule.RegisterScheduler(HotWriteRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { + return newHotWriteScheduler(opController, initHotRegionScheduleConfig()), nil + }) + schedule.RegisterScheduler(HotReadRegionType, func(opController *schedule.OperatorController, storage *core.Storage, decoder schedule.ConfigDecoder) (schedule.Scheduler, error) { + return newHotReadScheduler(opController, initHotRegionScheduleConfig()), nil + }) +} + +func newHotReadScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { + ret := newHotScheduler(opController, conf) + ret.name = "" + ret.types = []rwType{read} + return ret +} + +func newHotWriteScheduler(opController *schedule.OperatorController, conf *hotRegionSchedulerConfig) *hotScheduler { + ret := newHotScheduler(opController, conf) + ret.name = "" + ret.types = []rwType{write} + return ret } type testHotSchedulerSuite struct{} From eb372880b36dc5933e32e4afb939c0330b1a3eb8 Mon Sep 17 00:00:00 2001 From: HunDunDM Date: Mon, 5 Jul 2021 19:33:38 +0800 Subject: [PATCH 2/3] scheduler: remove the useless code in hot-region-scheduler Signed-off-by: HunDunDM --- server/schedulers/hot_region.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/server/schedulers/hot_region.go b/server/schedulers/hot_region.go index be3676c1680..ea1d424804d 100644 --- a/server/schedulers/hot_region.go +++ b/server/schedulers/hot_region.go @@ -74,10 +74,8 @@ type hotScheduler struct { name string *BaseScheduler sync.RWMutex - leaderLimit uint64 - peerLimit uint64 - types []rwType - r *rand.Rand + types []rwType + r *rand.Rand // states across multiple `Schedule` calls pendings map[*pendingInfluence]struct{} @@ -99,14 +97,12 @@ func newHotScheduler(opController *schedule.OperatorController, conf *hotRegionS ret := &hotScheduler{ name: HotRegionName, BaseScheduler: base, - leaderLimit: 1, - peerLimit: 1, types: []rwType{write, read}, r: rand.New(rand.NewSource(time.Now().UnixNano())), + pendings: map[*pendingInfluence]struct{}{}, regionPendings: make(map[uint64]*operator.Operator), conf: conf, } - ret.pendings = map[*pendingInfluence]struct{}{} for ty := resourceType(0); ty < resourceTypeLen; ty++ { ret.stLoadInfos[ty] = map[uint64]*storeLoadDetail{} } From f8b28b4a7ed2d6542f94ac9e1ae1763ffbcdaf9f Mon Sep 17 00:00:00 2001 From: HunDunDM Date: Mon, 5 Jul 2021 20:01:58 +0800 Subject: [PATCH 3/3] scheduler: fix nil pointer warning Signed-off-by: HunDunDM --- server/schedulers/hot_region.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/server/schedulers/hot_region.go b/server/schedulers/hot_region.go index ea1d424804d..e46f3018c72 100644 --- a/server/schedulers/hot_region.go +++ b/server/schedulers/hot_region.go @@ -512,9 +512,9 @@ func (bs *balanceSolver) solve() []*operator.Operator { } bs.cur = &solution{} var ( - best *solution - ops []*operator.Operator - infls []Influence + best *solution + op *operator.Operator + infl Influence ) for srcStoreID := range bs.filterSrcStores() { @@ -530,9 +530,9 @@ func (bs *balanceSolver) solve() []*operator.Operator { bs.cur.dstStoreID = dstStoreID bs.calcProgressiveRank() if bs.cur.progressiveRank < 0 && bs.betterThan(best) { - if newOps, newInfls := bs.buildOperators(); len(newOps) > 0 { - ops = newOps - infls = newInfls + if newOp, newInfl := bs.buildOperator(); newOp != nil { + op = newOp + infl = *newInfl clone := *bs.cur best = &clone } @@ -541,13 +541,11 @@ func (bs *balanceSolver) solve() []*operator.Operator { } } - for i := 0; i < len(ops); i++ { - // TODO: multiple operators need to be atomic. - if !bs.sche.addPendingInfluence(ops[i], best.srcStoreID, best.dstStoreID, infls[i]) { - return nil - } + if best == nil || !bs.sche.addPendingInfluence(op, best.srcStoreID, best.dstStoreID, infl) { + return nil } - return ops + + return []*operator.Operator{op} } // allowBalance check whether the operator count have exceed the hot region limit by type @@ -999,12 +997,11 @@ func (bs *balanceSolver) isReadyToBuild() bool { return true } -func (bs *balanceSolver) buildOperators() ([]*operator.Operator, []Influence) { +func (bs *balanceSolver) buildOperator() (op *operator.Operator, infl *Influence) { if !bs.isReadyToBuild() { return nil, nil } var ( - op *operator.Operator counters []prometheus.Counter err error ) @@ -1068,11 +1065,11 @@ func (bs *balanceSolver) buildOperators() ([]*operator.Operator, []Influence) { schedulerCounter.WithLabelValues(bs.sche.GetName(), "new-operator"), schedulerCounter.WithLabelValues(bs.sche.GetName(), bs.opTy.String())) - infl := Influence{ + infl = &Influence{ Loads: append(bs.cur.srcPeerStat.Loads[:0:0], bs.cur.srcPeerStat.Loads...), Count: 1, } - return []*operator.Operator{op}, []Influence{infl} + return op, infl } func (h *hotScheduler) GetHotStatus(typ string) *statistics.StoreHotPeersInfos {