Skip to content

Commit

Permalink
batch get region size
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Oct 25, 2023
1 parent 35b2719 commit 0052176
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
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 {
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

Check warning on line 1620 in pkg/core/region.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/region.go#L1619-L1620

Added lines #L1619 - L1620 were not covered by tests
}
if limit > 0 && cnt >= limit {
startKey = region.GetEndKey()
return false

Check warning on line 1624 in pkg/core/region.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/region.go#L1623-L1624

Added lines #L1623 - L1624 were not covered by tests
}
cnt++
size += region.GetApproximateSize()
return true
})
r.t.RUnlock()
if cnt == 0 {
break

Check warning on line 1632 in pkg/core/region.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/region.go#L1632

Added line #L1632 was not covered by tests
}
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

0 comments on commit 0052176

Please sign in to comment.