Skip to content

Commit

Permalink
add exp
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed May 18, 2022
1 parent fefd67e commit 2706744
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
48 changes: 30 additions & 18 deletions server/schedulers/hot_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (

minHotScheduleInterval = time.Second
maxHotScheduleInterval = 20 * time.Second
stddevThreshold = 0.1
)

var (
Expand Down Expand Up @@ -384,6 +385,8 @@ type balanceSolver struct {
minorDecRatio float64
maxPeerNum int
minHotDegree int

pick func(s interface{}, p func(int) bool) bool
}

func (bs *balanceSolver) init() {
Expand Down Expand Up @@ -423,6 +426,11 @@ func (bs *balanceSolver) init() {
bs.greatDecRatio, bs.minorDecRatio = bs.sche.conf.GetGreatDecRatio(), bs.sche.conf.GetMinorDecRatio()
bs.maxPeerNum = bs.sche.conf.GetMaxPeerNumber()
bs.minHotDegree = bs.GetOpts().GetHotRegionCacheHitsThreshold()

bs.pick = slice.AnyOf
if bs.sche.conf.IsStrictPickingStoreEnabled() {
bs.pick = slice.AllOf
}
}

func (bs *balanceSolver) isSelectedDim(dim int) bool {
Expand Down Expand Up @@ -473,6 +481,12 @@ func (bs *balanceSolver) solve() []*operator.Operator {
}
bs.cur = &solution{}
tryUpdateBestSolution := func() {
if bs.cur.progressiveRank == -1 && bs.isUniformFirstPriority(bs.cur.srcStore) {
// Because region is available for src and dst, so stddev is the same for both, only need to calcurate one.
// If first priority dim is enough uniform, -1 is unnecessary and maybe lead to worse balance for second priority dim
hotSchedulerResultCounter.WithLabelValues("skip-for-exp", strconv.FormatUint(bs.cur.dstStore.GetID(), 10)).Inc()
return
}
if bs.cur.progressiveRank < 0 && bs.betterThan(bs.best) {
if newOps, newInfl := bs.buildOperators(); len(newOps) > 0 {
bs.ops = newOps
Expand Down Expand Up @@ -561,15 +575,12 @@ func (bs *balanceSolver) filterSrcStores() map[uint64]*statistics.StoreLoadDetai
}

func (bs *balanceSolver) checkSrcByDimPriorityAndTolerance(minLoad, expectLoad *statistics.StoreLoad, toleranceRatio float64) bool {
if bs.sche.conf.IsStrictPickingStoreEnabled() {
return slice.AllOf(minLoad.Loads, func(i int) bool {
if bs.isSelectedDim(i) {
return minLoad.Loads[i] > toleranceRatio*expectLoad.Loads[i]
}
return true
})
}
return minLoad.Loads[bs.firstPriority] > toleranceRatio*expectLoad.Loads[bs.firstPriority]
return bs.pick(minLoad.Loads, func(i int) bool {
if bs.isSelectedDim(i) {
return minLoad.Loads[i] > toleranceRatio*expectLoad.Loads[i]
}
return true
})
}

// filterHotPeers filtered hot peers from statistics.HotPeerStat and deleted the peer if its region is in pending status.
Expand Down Expand Up @@ -767,15 +778,16 @@ func (bs *balanceSolver) pickDstStores(filters []filter.Filter, candidates []*st
}

func (bs *balanceSolver) checkDstByPriorityAndTolerance(maxLoad, expect *statistics.StoreLoad, toleranceRatio float64) bool {
if bs.sche.conf.IsStrictPickingStoreEnabled() {
return slice.AllOf(maxLoad.Loads, func(i int) bool {
if bs.isSelectedDim(i) {
return maxLoad.Loads[i]*toleranceRatio < expect.Loads[i]
}
return true
})
}
return maxLoad.Loads[bs.firstPriority]*toleranceRatio < expect.Loads[bs.firstPriority]
return bs.pick(maxLoad.Loads, func(i int) bool {
if bs.isSelectedDim(i) {
return maxLoad.Loads[i]*toleranceRatio < expect.Loads[i]
}
return true
})
}

func (bs *balanceSolver) isUniformFirstPriority(store *statistics.StoreLoadDetail) bool {
return store.IsUniform(bs.firstPriority, stddevThreshold)
}

// calcProgressiveRank calculates `bs.cur.progressiveRank`.
Expand Down
27 changes: 26 additions & 1 deletion server/statistics/store_hot_peers_infos.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package statistics

import (
"fmt"
"math"

"github.com/tikv/pd/server/core"
)
Expand Down Expand Up @@ -186,6 +187,19 @@ func summaryStoresLoadByEngine(
for i := range expectLoads {
expectLoads[i] = allStoreLoadSum[i] / float64(allStoreCount)
}

stddevLoads := make([]float64, len(allStoreLoadSum))
if allHotPeersCount != 0 {
for _, detail := range loadDetail {
for i := range expectLoads {
stddevLoads[i] += math.Pow(detail.LoadPred.Current.Loads[i]-expectLoads[i], 2)
}
}
for i := range stddevLoads {
stddevLoads[i] = math.Sqrt(stddevLoads[i]/float64(allStoreCount)) / expectLoads[i]
}
}

{
// Metric for debug.
engine := collector.Engine()
Expand All @@ -197,13 +211,24 @@ func summaryStoresLoadByEngine(
hotPeerSummary.WithLabelValues(ty, engine).Set(expectLoads[QueryDim])
ty = "exp-count-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, engine).Set(expectCount)
ty = "stddev-byte-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, engine).Set(stddevLoads[ByteDim])
ty = "stddev-key-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, engine).Set(stddevLoads[KeyDim])
ty = "stddev-query-rate-" + rwTy.String() + "-" + kind.String()
hotPeerSummary.WithLabelValues(ty, engine).Set(stddevLoads[QueryDim])
}
expect := StoreLoad{
Loads: expectLoads,
Count: float64(allHotPeersCount) / float64(allStoreCount),
Count: expectCount,
}
stddev := StoreLoad{
Loads: stddevLoads,
Count: expectCount,
}
for _, detail := range loadDetail {
detail.LoadPred.Expect = expect
detail.LoadPred.Stddev = stddev
}
return loadDetail
}
Expand Down
6 changes: 6 additions & 0 deletions server/statistics/store_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func (li *StoreLoadDetail) ToHotPeersStat() *HotPeersStat {
}
}

// IsUniform returns true if the stores are uniform.
func (li *StoreLoadDetail) IsUniform(dim int, threshold float64) bool {
return li.LoadPred.Stddev.Loads[dim] < threshold
}

func toHotPeerStatShow(p *HotPeerStat, kind RWType) HotPeerStatShow {
b, k, q := GetRegionStatKind(kind, ByteDim), GetRegionStatKind(kind, KeyDim), GetRegionStatKind(kind, QueryDim)
byteRate := p.Loads[b]
Expand Down Expand Up @@ -206,6 +211,7 @@ type StoreLoadPred struct {
Current StoreLoad
Future StoreLoad
Expect StoreLoad
Stddev StoreLoad
}

// Min returns the min load between current and future.
Expand Down

0 comments on commit 2706744

Please sign in to comment.