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

schedule: fix race for region scatter #3760

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 14 additions & 11 deletions server/schedule/region_scatterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,10 @@ func (s *selectedStores) GetGroupDistribution(group string) (map[uint64]uint64,
return s.getDistributionByGroupLocked(group)
}

// getDistributionByGroupLocked should be called with lock
func (s *selectedStores) getDistributionByGroupLocked(group string) (map[uint64]uint64, bool) {
if result, ok := s.groupDistribution.Get(group); ok {
return result.(map[uint64]uint64), true
}
return nil, false
}

func (s *selectedStores) totalCountByStore(storeID uint64) uint64 {
// TotalCountByStore counts the total count by store
func (s *selectedStores) TotalCountByStore(storeID uint64) uint64 {
s.mu.RLock()
defer s.mu.RUnlock()
groups := s.groupDistribution.GetAllID()
totalCount := uint64(0)
for _, group := range groups {
Expand All @@ -110,6 +105,14 @@ func (s *selectedStores) totalCountByStore(storeID uint64) uint64 {
return totalCount
}

// getDistributionByGroupLocked should be called with lock
func (s *selectedStores) getDistributionByGroupLocked(group string) (map[uint64]uint64, bool) {
if result, ok := s.groupDistribution.Get(group); ok {
return result.(map[uint64]uint64), true
}
return nil, false
}

// RegionScatterer scatters regions.
type RegionScatterer struct {
ctx context.Context
Expand Down Expand Up @@ -347,7 +350,7 @@ func (r *RegionScatterer) selectCandidates(region *core.RegionInfo, sourceStoreI
maxStoreTotalCount := uint64(0)
minStoreTotalCount := uint64(math.MaxUint64)
for _, store := range r.cluster.GetStores() {
count := context.selectedPeer.totalCountByStore(store.GetID())
count := context.selectedPeer.TotalCountByStore(store.GetID())
if count > maxStoreTotalCount {
maxStoreTotalCount = count
}
Expand All @@ -356,7 +359,7 @@ func (r *RegionScatterer) selectCandidates(region *core.RegionInfo, sourceStoreI
}
}
for _, store := range stores {
storeCount := context.selectedPeer.totalCountByStore(store.GetID())
storeCount := context.selectedPeer.TotalCountByStore(store.GetID())
// If storeCount is equal to the maxStoreTotalCount, we should skip this store as candidate.
// If the storeCount are all the same for the whole cluster(maxStoreTotalCount == minStoreTotalCount), any store
// could be selected as candidate.
Expand Down
2 changes: 1 addition & 1 deletion server/schedule/region_scatterer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func (s *testScatterRegionSuite) TestRegionFromDifferentGroups(c *C) {
max := uint64(0)
min := uint64(math.MaxUint64)
for i := uint64(1); i <= uint64(storeCount); i++ {
count := ss.totalCountByStore(i)
count := ss.TotalCountByStore(i)
if count > max {
max = count
}
Expand Down