Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions pkg/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,17 +1609,34 @@ func (r *RegionsInfo) ScanRegionWithIterator(startKey []byte, iterator func(regi
}

// GetRegionSizeByRange scans regions intersecting [start key, end key), returns the total region size of this range.
func (r *RegionsInfo) GetRegionSizeByRange(startKey, endKey []byte) int64 {
r.t.RLock()
defer r.t.RUnlock()
func (r *RegionsInfo) GetRegionSizeByRange(startKey, endKey []byte, limit int) int64 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add bench for it?

var size int64
r.tree.scanRange(startKey, func(region *RegionInfo) bool {
if len(endKey) > 0 && bytes.Compare(region.GetStartKey(), endKey) >= 0 {
return false
for {
r.t.RLock()
var cnt int
r.tree.scanRange(startKey, func(region *RegionInfo) bool {
if len(endKey) > 0 && bytes.Compare(region.GetStartKey(), endKey) >= 0 {
startKey = region.GetEndKey()
return false
}
if limit > 0 && cnt >= limit {
startKey = region.GetEndKey()
return false
}
cnt++
size += region.GetApproximateSize()
return true
})
r.t.RUnlock()
if cnt == 0 {
break
}
size += region.GetApproximateSize()
return true
})

if len(startKey) == 0 {
break
}
}

return size
}

Expand Down
5 changes: 3 additions & 2 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const (
// minSnapshotDurationSec is the minimum duration that a store can tolerate.
// It should enlarge the limiter if the snapshot's duration is less than this value.
minSnapshotDurationSec = 5
scanRegionLimit = 1000
)

// Server is the interface for cluster.
Expand Down Expand Up @@ -1908,7 +1909,7 @@ func (c *RaftCluster) checkStores() {
func (c *RaftCluster) getThreshold(stores []*core.StoreInfo, store *core.StoreInfo) float64 {
start := time.Now()
if !c.opt.IsPlacementRulesEnabled() {
regionSize := c.core.GetRegionSizeByRange([]byte(""), []byte("")) * int64(c.opt.GetMaxReplicas())
regionSize := c.core.GetRegionSizeByRange([]byte(""), []byte(""), scanRegionLimit) * int64(c.opt.GetMaxReplicas())
weight := getStoreTopoWeight(store, stores, c.opt.GetLocationLabels(), c.opt.GetMaxReplicas())
return float64(regionSize) * weight * 0.9
}
Expand Down Expand Up @@ -1948,7 +1949,7 @@ func (c *RaftCluster) calculateRange(stores []*core.StoreInfo, store *core.Store
matchStores = append(matchStores, s)
}
}
regionSize := c.core.GetRegionSizeByRange(startKey, endKey) * int64(rule.Count)
regionSize := c.core.GetRegionSizeByRange(startKey, endKey, scanRegionLimit) * int64(rule.Count)
weight := getStoreTopoWeight(store, matchStores, rule.LocationLabels, rule.Count)
storeSize += float64(regionSize) * weight
log.Debug("calculate range result",
Expand Down