Skip to content

Commit

Permalink
address the comment
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Jan 18, 2021
1 parent 7bc720d commit 718b0a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
13 changes: 13 additions & 0 deletions server/schedule/opt/healthy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ package opt

import "github.com/tikv/pd/server/core"

// BalanceEmptyRegionThreshold is a threshold which allow balance the empty region if the region number is less than this threshold.
var balanceEmptyRegionThreshold = 50

// IsRegionHealthy checks if a region is healthy for scheduling. It requires the
// region does not have any down or pending peers. And when placement rules
// feature is disabled, it requires the region does not have any learner peer.
Expand All @@ -31,6 +34,11 @@ func IsHealthyAllowPending(cluster Cluster, region *core.RegionInfo) bool {
return len(region.GetDownPeers()) == 0
}

// IsEmptyRegionAllowBalance checks if a region is an empty region and can be balanced.
func IsEmptyRegionAllowBalance(cluster Cluster, region *core.RegionInfo) bool {
return region.GetApproximateSize() <= core.EmptyRegionApproximateSize && cluster.GetRegionCount() > balanceEmptyRegionThreshold
}

// HealthRegion returns a function that checks if a region is healthy for
// scheduling. It requires the region does not have any down or pending peers,
// and does not have any learner peers when placement rules is disabled.
Expand All @@ -45,6 +53,11 @@ func HealthAllowPending(cluster Cluster) func(*core.RegionInfo) bool {
return func(region *core.RegionInfo) bool { return IsHealthyAllowPending(cluster, region) }
}

// AllowBalanceEmptyRegion returns a function that checks if a region is an empty region and can be balanced.
func AllowBalanceEmptyRegion(cluster Cluster) func(*core.RegionInfo) bool {
return func(region *core.RegionInfo) bool { return IsEmptyRegionAllowBalance(cluster, region) }
}

// IsRegionReplicated checks if a region is fully replicated. When placement
// rules is enabled, its peers should fit corresponding rules. When placement
// rules is disabled, it should have enough replicas and no any learner peer.
Expand Down
18 changes: 4 additions & 14 deletions server/schedulers/balance_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func init() {
const (
// balanceRegionRetryLimit is the limit to retry schedule for selected store.
balanceRegionRetryLimit = 10
// BalanceEmptyRegionThreshold is a threshold which allow balance the empty region if the region number is less than this threshold.
balanceEmptyRegionThreshold = 50
// BalanceRegionName is balance region scheduler name.
BalanceRegionName = "balance-region-scheduler"
// BalanceRegionType is balance region scheduler type.
Expand Down Expand Up @@ -148,39 +146,31 @@ func (s *balanceRegionScheduler) Schedule(cluster opt.Cluster) []*operator.Opera
return stores[i].RegionScore(opts.GetRegionScoreFormulaVersion(), opts.GetHighSpaceRatio(), opts.GetLowSpaceRatio(), iOp, -1) >
stores[j].RegionScore(opts.GetRegionScoreFormulaVersion(), opts.GetHighSpaceRatio(), opts.GetLowSpaceRatio(), jOp, -1)
})
regionCount := cluster.GetRegionCount()
for _, source := range stores {
sourceID := source.GetID()

for i := 0; i < balanceRegionRetryLimit; i++ {
// Priority pick the region that has a pending peer.
// Pending region may means the disk is overload, remove the pending region firstly.
region := cluster.RandPendingRegion(sourceID, s.conf.Ranges, opt.HealthAllowPending(cluster), opt.ReplicatedRegion(cluster))
region := cluster.RandPendingRegion(sourceID, s.conf.Ranges, opt.HealthAllowPending(cluster), opt.ReplicatedRegion(cluster), opt.AllowBalanceEmptyRegion(cluster))
if region == nil {
// Then pick the region that has a follower in the source store.
region = cluster.RandFollowerRegion(sourceID, s.conf.Ranges, opt.HealthRegion(cluster), opt.ReplicatedRegion(cluster))
region = cluster.RandFollowerRegion(sourceID, s.conf.Ranges, opt.HealthRegion(cluster), opt.ReplicatedRegion(cluster), opt.AllowBalanceEmptyRegion(cluster))
}
if region == nil {
// Then pick the region has the leader in the source store.
region = cluster.RandLeaderRegion(sourceID, s.conf.Ranges, opt.HealthRegion(cluster), opt.ReplicatedRegion(cluster))
region = cluster.RandLeaderRegion(sourceID, s.conf.Ranges, opt.HealthRegion(cluster), opt.ReplicatedRegion(cluster), opt.AllowBalanceEmptyRegion(cluster))
}
if region == nil {
// Finally pick learner.
region = cluster.RandLearnerRegion(sourceID, s.conf.Ranges, opt.HealthRegion(cluster), opt.ReplicatedRegion(cluster))
region = cluster.RandLearnerRegion(sourceID, s.conf.Ranges, opt.HealthRegion(cluster), opt.ReplicatedRegion(cluster), opt.AllowBalanceEmptyRegion(cluster))
}
if region == nil {
schedulerCounter.WithLabelValues(s.GetName(), "no-region").Inc()
continue
}
log.Debug("select region", zap.String("scheduler", s.GetName()), zap.Uint64("region-id", region.GetID()))

// Skip the empty region
if region.GetApproximateSize() <= core.EmptyRegionApproximateSize && regionCount > balanceEmptyRegionThreshold {
log.Debug("region is empty", zap.String("scheduler", s.GetName()), zap.Uint64("region-id", region.GetID()))
schedulerCounter.WithLabelValues(s.GetName(), "empty-region").Inc()
continue
}

// Skip hot regions.
if cluster.IsRegionHot(region) {
log.Debug("region is hot", zap.String("scheduler", s.GetName()), zap.Uint64("region-id", region.GetID()))
Expand Down

0 comments on commit 718b0a3

Please sign in to comment.