From 9097e68c4db5eddbc177fc009b42801a421ff0a0 Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Fri, 16 Apr 2021 13:30:34 +0800 Subject: [PATCH] cluster: avoid unexpected statistic modify in range cluster (#3599) * fix Signed-off-by: lhy1024 * fix Signed-off-by: lhy1024 * add comment Signed-off-by: lhy1024 * add comment Signed-off-by: lhy1024 Co-authored-by: Ti Chi Robot <71242396+ti-chi-bot@users.noreply.github.com> --- server/core/store_option.go | 11 +++++++++++ server/core/store_stats.go | 10 ++++++++++ server/schedule/range_cluster.go | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/server/core/store_option.go b/server/core/store_option.go index 8c7ad72bdfd..6b39c6fb501 100644 --- a/server/core/store_option.go +++ b/server/core/store_option.go @@ -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) { diff --git a/server/core/store_stats.go b/server/core/store_stats.go index 6d4e642bcba..fe413b697cf 100644 --- a/server/core/store_stats.go +++ b/server/core/store_stats.go @@ -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) @@ -143,6 +147,9 @@ 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()) } @@ -150,6 +157,9 @@ func (ss *storeStats) GetAvgAvailable() uint64 { func (ss *storeStats) GetAvailableDeviation() uint64 { ss.mu.RLock() defer ss.mu.RUnlock() + if ss.avgMaxAvailableDeviation == nil { + return 0 + } return climp0(ss.avgMaxAvailableDeviation.Get()) } diff --git a/server/schedule/range_cluster.go b/server/schedule/range_cluster.go index 97d1048c104..061359b6eb5 100644 --- a/server/schedule/range_cluster.go +++ b/server/schedule/range_cluster.go @@ -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),