Skip to content

Commit

Permalink
fix data race by replace clone
Browse files Browse the repository at this point in the history
Signed-off-by: bufferflies <[email protected]>
  • Loading branch information
bufferflies committed Mar 29, 2023
1 parent fa18b03 commit c0deca6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/core/storelimit/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ type StoreLimit interface {
Take(count int64, typ Type, level constant.PriorityLevel) bool
// Reset resets the store limit
Reset(rate float64, typ Type)
// Cap returns the capacity of the store limit
Cap(typ Type) float64
}
5 changes: 5 additions & 0 deletions pkg/core/storelimit/sliding_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func NewSlidingWindows(cap float64) *SlidingWindows {
}
}

// Cap returns the capacity of the sliding windows.
func (s *SlidingWindows) Cap(typ Type) float64 {
return float64(s.windows[typ].capacity)
}

// Reset resets the capacity of the sliding windows.
// It doesn't clear all the used, only set the capacity.
func (s *SlidingWindows) Reset(cap float64, typ Type) {
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 @@ -90,6 +90,14 @@ func (l *StoreRateLimit) Available(cost int64, typ Type, _ constant.PriorityLeve
return l.limits[typ].Available(cost)
}

// Cap returns the capacity of the store limit.
func (l *StoreRateLimit) Cap(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
7 changes: 4 additions & 3 deletions pkg/schedule/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,8 @@ func (oc *OperatorController) getOrCreateStoreLimit(storeID uint64, limitType st
return nil
}

limit := s.GetStoreLimit()
limit.Reset(ratePerSec, limitType)
return limit
if limit := s.GetStoreLimit(); limit.Cap(limitType) != ratePerSec {
oc.cluster.GetBasicCluster().ResetStoreLimit(storeID, limitType)
}
return s.GetStoreLimit()
}

0 comments on commit c0deca6

Please sign in to comment.