Skip to content

Commit

Permalink
cluster: avoid unexpected statistic modify in range cluster (#3599)
Browse files Browse the repository at this point in the history
* fix

Signed-off-by: lhy1024 <[email protected]>

* fix

Signed-off-by: lhy1024 <[email protected]>

* add comment

Signed-off-by: lhy1024 <[email protected]>

* add comment

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: Ti Chi Robot <[email protected]>
  • Loading branch information
lhy1024 and ti-chi-bot authored Apr 16, 2021
1 parent b8c3d14 commit 9097e68
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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

0 comments on commit 9097e68

Please sign in to comment.