Skip to content

Commit

Permalink
fix race
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <disxiaofei@163.com>
  • Loading branch information
Yisaer authored and ti-chi-bot committed Jun 10, 2021
1 parent 2b5ada0 commit 3521301
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 @@ -86,15 +86,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 @@ -111,6 +106,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 @@ -348,7 +351,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 @@ -357,7 +360,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 @@ -464,7 +464,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 3521301

Please sign in to comment.