Skip to content

Commit

Permalink
schedule: fix race for region scatter (tikv#3760)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <disxiaofei@163.com>
  • Loading branch information
Yisaer authored and bufferflies committed Jul 9, 2021
1 parent a993629 commit c042cbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
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

0 comments on commit c042cbc

Please sign in to comment.