From 269500841ea236eca88b0dea30a9cc1f26536fd5 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 21 Apr 2023 15:07:20 +0800 Subject: [PATCH] fix data race by replace clone (#6242) (#6350) close tikv/pd#6230, ref tikv/pd#6242 Signed-off-by: bufferflies <1045931706@qq.com> Co-authored-by: buffer <1045931706@qq.com> Co-authored-by: Ti Chi Robot --- pkg/core/storelimit/limit_test.go | 3 ++- pkg/core/storelimit/store_limit.go | 8 ++++++++ pkg/schedule/operator_controller.go | 9 +++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/core/storelimit/limit_test.go b/pkg/core/storelimit/limit_test.go index 60580fc795c..2b418952bb6 100644 --- a/pkg/core/storelimit/limit_test.go +++ b/pkg/core/storelimit/limit_test.go @@ -28,7 +28,8 @@ import ( func TestStoreLimit(t *testing.T) { re := require.New(t) rate := int64(15) - limit := NewStoreRateLimit(float64(rate)) + limit := NewStoreRateLimit(float64(rate)).(*StoreRateLimit) + re.Equal(limit.Rate(AddPeer), float64(15)) re.True(limit.Available(influence*rate, AddPeer, constant.Low)) re.True(limit.Take(influence*rate, AddPeer, constant.Low)) re.False(limit.Take(influence, AddPeer, constant.Low)) diff --git a/pkg/core/storelimit/store_limit.go b/pkg/core/storelimit/store_limit.go index 79dc07245a2..495040ab1a0 100644 --- a/pkg/core/storelimit/store_limit.go +++ b/pkg/core/storelimit/store_limit.go @@ -101,6 +101,14 @@ func (l *StoreRateLimit) Available(cost int64, typ Type, _ constant.PriorityLeve return l.limits[typ].Available(cost) } +// Rate returns the capacity of the store limit. +func (l *StoreRateLimit) Rate(typ Type) float64 { + if l.limits[typ] == nil { + return 0.0 + } + return l.limits[typ].ratePerSec +} + // Take takes count tokens from the bucket without blocking. // notice that the priority level is not used. func (l *StoreRateLimit) Take(cost int64, typ Type, _ constant.PriorityLevel) bool { diff --git a/pkg/schedule/operator_controller.go b/pkg/schedule/operator_controller.go index 70346e5671f..2a407c0a8ab 100644 --- a/pkg/schedule/operator_controller.go +++ b/pkg/schedule/operator_controller.go @@ -878,8 +878,9 @@ func (oc *OperatorController) getOrCreateStoreLimit(storeID uint64, limitType st log.Error("invalid store ID", zap.Uint64("store-id", storeID)) return nil } - - limit := s.GetStoreLimit() - limit.Reset(ratePerSec, limitType) - return limit + // The other limits do not need to update by config exclude StoreRateLimit. + if limit, ok := s.GetStoreLimit().(*storelimit.StoreRateLimit); ok && limit.Rate(limitType) != ratePerSec { + oc.cluster.GetBasicCluster().ResetStoreLimit(storeID, limitType, ratePerSec) + } + return s.GetStoreLimit() }