Skip to content

Commit

Permalink
add a threshold for balance empty region
Browse files Browse the repository at this point in the history
Signed-off-by: rleungx <rleungx@gmail.com>
  • Loading branch information
rleungx committed Jan 13, 2021
1 parent 2438346 commit 0481cc7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pkg/mock/mockcluster/mockcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package mockcluster

import (
"fmt"
"strconv"
"time"

"github.com/gogo/protobuf/proto"
Expand Down Expand Up @@ -583,7 +584,11 @@ func (mc *Cluster) CheckLabelProperty(typ string, labels []*metapb.StoreLabel) b

// PutRegionStores mocks method.
func (mc *Cluster) PutRegionStores(id uint64, stores ...uint64) {
meta := &metapb.Region{Id: id}
meta := &metapb.Region{
Id: id,
StartKey: []byte(strconv.FormatUint(id, 10)),
EndKey: []byte(strconv.FormatUint(id+1, 10)),
}
for _, s := range stores {
meta.Peers = append(meta.Peers, &metapb.Peer{StoreId: s})
}
Expand Down
5 changes: 4 additions & 1 deletion server/schedulers/balance_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ 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 @@ -142,6 +144,7 @@ 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()

Expand All @@ -168,7 +171,7 @@ func (s *balanceRegionScheduler) Schedule(cluster opt.Cluster) []*operator.Opera
log.Debug("select region", zap.String("scheduler", s.GetName()), zap.Uint64("region-id", region.GetID()))

// Skip the empty region
if region.GetApproximateSize() <= core.EmptyRegionApproximateSize {
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
Expand Down
6 changes: 6 additions & 0 deletions server/schedulers/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,12 @@ func (s *testBalanceRegionSchedulerSuite) TestEmptyRegion(c *C) {
)
tc.PutRegion(region)
operators := sb.Schedule(tc)
c.Assert(operators, NotNil)

for i := uint64(10); i < 60; i++ {
tc.PutRegionStores(i, 1, 3, 4)
}
operators = sb.Schedule(tc)
c.Assert(operators, IsNil)
}

Expand Down

0 comments on commit 0481cc7

Please sign in to comment.