Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: batch get region size #7252

Merged
merged 6 commits into from
Nov 8, 2023
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 @@
}

// 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
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

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
nolouch marked this conversation as resolved.
Show resolved Hide resolved
)

// 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
Loading