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

cluster: avoid unexpected statistic modify in range cluster (#3599) #3602

Merged
merged 3 commits into from
Apr 16, 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
11 changes: 11 additions & 0 deletions server/core/store_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ func SetStoreStats(stats *pdpb.StoreStats) StoreCreateOption {
}
}

// SetNewStoreStats sets the raw statistics information for the store.
func SetNewStoreStats(stats *pdpb.StoreStats) StoreCreateOption {
return func(store *StoreInfo) {
// There is no clone in default store stats, we create new one to avoid to modify others.
// And range cluster cannot use HMA because the last value is not cached
store.storeStats = &storeStats{
rawStats: stats,
}
}
}

// AttachAvailableFunc attaches a customize function for the store. The function f returns true if the store limit is not exceeded.
func AttachAvailableFunc(limitType storelimit.Type, f func() bool) StoreCreateOption {
return func(store *StoreInfo) {
Expand Down
10 changes: 10 additions & 0 deletions server/core/store_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (ss *storeStats) updateRawStats(rawStats *pdpb.StoreStats) {
defer ss.mu.Unlock()
ss.rawStats = rawStats

if ss.avgAvailable == nil {
return
}

ss.avgAvailable.Add(float64(rawStats.GetAvailable()))
deviation := math.Abs(float64(rawStats.GetAvailable()) - ss.avgAvailable.Get())
ss.maxAvailableDeviation.Add(deviation)
Expand Down Expand Up @@ -143,13 +147,19 @@ func (ss *storeStats) GetApplyingSnapCount() uint32 {
func (ss *storeStats) GetAvgAvailable() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
if ss.avgAvailable == nil {
return ss.rawStats.Available
}
return climp0(ss.avgAvailable.Get())
}

// GetAvailableDeviation returns approximate magnitude of available in the recent period.
func (ss *storeStats) GetAvailableDeviation() uint64 {
ss.mu.RLock()
defer ss.mu.RUnlock()
if ss.avgMaxAvailableDeviation == nil {
return 0
}
return climp0(ss.avgMaxAvailableDeviation.Get())
}

Expand Down
2 changes: 1 addition & 1 deletion server/schedule/range_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *RangeCluster) updateStoreInfo(s *core.StoreInfo) *core.StoreInfo {
newStats.UsedSize = uint64(float64(regionSize)/amplification) * (1 << 20)
newStats.Available = s.GetCapacity() - newStats.UsedSize
newStore := s.Clone(
core.SetStoreStats(newStats),
core.SetNewStoreStats(newStats), // it means to use instant value directly
core.SetLeaderCount(leaderCount),
core.SetRegionCount(regionCount),
core.SetPendingPeerCount(pendingPeerCount),
Expand Down