Skip to content

Commit

Permalink
fix data race by replace clone (tikv#6242)
Browse files Browse the repository at this point in the history
close tikv#6230

Signed-off-by: bufferflies <1045931706@qq.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
bufferflies and ti-chi-bot committed Apr 21, 2023
1 parent 5f99e0c commit 8d8172e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/core/storelimit/limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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))
Expand Down
8 changes: 8 additions & 0 deletions pkg/core/storelimit/store_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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 {
Expand Down
9 changes: 5 additions & 4 deletions pkg/schedule/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 8d8172e

Please sign in to comment.