Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resourcemanager: change param name #7293

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/mcs/resourcemanager/server/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func FromProtoResourceGroup(group *rmpb.ResourceGroup) *ResourceGroup {
// RequestRU requests the RU of the resource group.
func (rg *ResourceGroup) RequestRU(
now time.Time,
neededTokens float64,
requiredToken float64,
targetPeriodMs, clientUniqueID uint64,
) *rmpb.GrantedRUTokenBucket {
rg.Lock()
Expand All @@ -147,7 +147,7 @@ func (rg *ResourceGroup) RequestRU(
if rg.RUSettings == nil || rg.RUSettings.RU.Settings == nil {
return nil
}
tb, trickleTimeMs := rg.RUSettings.RU.request(now, neededTokens, targetPeriodMs, clientUniqueID)
tb, trickleTimeMs := rg.RUSettings.RU.request(now, requiredToken, targetPeriodMs, clientUniqueID)
return &rmpb.GrantedRUTokenBucket{GrantedTokens: tb, TrickleTimeMs: trickleTimeMs}
}

Expand Down
42 changes: 21 additions & 21 deletions pkg/mcs/resourcemanager/server/token_buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (gtb *GroupTokenBucket) init(now time.Time, clientID uint64) {
}

// updateTokens updates the tokens and settings.
func (gtb *GroupTokenBucket) updateTokens(now time.Time, burstLimit int64, clientUniqueID uint64, consumptionToken float64) {
func (gtb *GroupTokenBucket) updateTokens(now time.Time, burstLimit int64, clientUniqueID uint64, requiredToken float64) {
var elapseTokens float64
if !gtb.Initialized {
gtb.init(now, clientUniqueID)
Expand All @@ -288,46 +288,46 @@ func (gtb *GroupTokenBucket) updateTokens(now time.Time, burstLimit int64, clien
gtb.Tokens = burst
}
// Balance each slots.
gtb.balanceSlotTokens(clientUniqueID, gtb.Settings, consumptionToken, elapseTokens)
gtb.balanceSlotTokens(clientUniqueID, gtb.Settings, requiredToken, elapseTokens)
}

// request requests tokens from the corresponding slot.
func (gtb *GroupTokenBucket) request(now time.Time,
neededTokens float64,
requiredToken float64,
targetPeriodMs, clientUniqueID uint64,
) (*rmpb.TokenBucket, int64) {
burstLimit := gtb.Settings.GetBurstLimit()
gtb.updateTokens(now, burstLimit, clientUniqueID, neededTokens)
gtb.updateTokens(now, burstLimit, clientUniqueID, requiredToken)
slot, ok := gtb.tokenSlots[clientUniqueID]
if !ok {
return &rmpb.TokenBucket{Settings: &rmpb.TokenLimitSettings{BurstLimit: burstLimit}}, 0
}
res, trickleDuration := slot.assignSlotTokens(neededTokens, targetPeriodMs)
res, trickleDuration := slot.assignSlotTokens(requiredToken, targetPeriodMs)
// Update bucket to record all tokens.
gtb.Tokens -= slot.lastTokenCapacity - slot.tokenCapacity
slot.lastTokenCapacity = slot.tokenCapacity

return res, trickleDuration
}

func (ts *TokenSlot) assignSlotTokens(neededTokens float64, targetPeriodMs uint64) (*rmpb.TokenBucket, int64) {
func (ts *TokenSlot) assignSlotTokens(requiredToken float64, targetPeriodMs uint64) (*rmpb.TokenBucket, int64) {
var res rmpb.TokenBucket
burstLimit := ts.settings.GetBurstLimit()
res.Settings = &rmpb.TokenLimitSettings{BurstLimit: burstLimit}
// If BurstLimit < 0, just return.
if burstLimit < 0 {
res.Tokens = neededTokens
res.Tokens = requiredToken
return &res, 0
}
// FillRate is used for the token server unavailable in abnormal situation.
if neededTokens <= 0 {
if requiredToken <= 0 {
return &res, 0
}
// If the current tokens can directly meet the requirement, returns the need token.
if ts.tokenCapacity >= neededTokens {
ts.tokenCapacity -= neededTokens
if ts.tokenCapacity >= requiredToken {
ts.tokenCapacity -= requiredToken
// granted the total request tokens
res.Tokens = neededTokens
res.Tokens = requiredToken
return &res, 0
}

Expand All @@ -336,7 +336,7 @@ func (ts *TokenSlot) assignSlotTokens(neededTokens float64, targetPeriodMs uint6
hasRemaining := false
if ts.tokenCapacity > 0 {
grantedTokens = ts.tokenCapacity
neededTokens -= grantedTokens
requiredToken -= grantedTokens
ts.tokenCapacity = 0
hasRemaining = true
}
Expand Down Expand Up @@ -373,36 +373,36 @@ func (ts *TokenSlot) assignSlotTokens(neededTokens float64, targetPeriodMs uint6
for i := 1; i < loanCoefficient; i++ {
p[i] = float64(loanCoefficient-i)*float64(fillRate)*targetPeriodTimeSec + p[i-1]
}
for i := 0; i < loanCoefficient && neededTokens > 0 && trickleTime < targetPeriodTimeSec; i++ {
for i := 0; i < loanCoefficient && requiredToken > 0 && trickleTime < targetPeriodTimeSec; i++ {
loan := -ts.tokenCapacity
if loan >= p[i] {
continue
}
roundReserveTokens := p[i] - loan
fillRate := float64(loanCoefficient-i) * float64(fillRate)
if roundReserveTokens > neededTokens {
ts.tokenCapacity -= neededTokens
grantedTokens += neededTokens
if roundReserveTokens > requiredToken {
ts.tokenCapacity -= requiredToken
grantedTokens += requiredToken
trickleTime += grantedTokens / fillRate
neededTokens = 0
requiredToken = 0
} else {
roundReserveTime := roundReserveTokens / fillRate
if roundReserveTime+trickleTime >= targetPeriodTimeSec {
roundTokens := (targetPeriodTimeSec - trickleTime) * fillRate
neededTokens -= roundTokens
requiredToken -= roundTokens
ts.tokenCapacity -= roundTokens
grantedTokens += roundTokens
trickleTime = targetPeriodTimeSec
} else {
grantedTokens += roundReserveTokens
neededTokens -= roundReserveTokens
requiredToken -= roundReserveTokens
ts.tokenCapacity -= roundReserveTokens
trickleTime += roundReserveTime
}
}
}
if neededTokens > 0 && grantedTokens < defaultReserveRatio*float64(fillRate)*targetPeriodTimeSec {
reservedTokens := math.Min(neededTokens+grantedTokens, defaultReserveRatio*float64(fillRate)*targetPeriodTimeSec)
if requiredToken > 0 && grantedTokens < defaultReserveRatio*float64(fillRate)*targetPeriodTimeSec {
reservedTokens := math.Min(requiredToken+grantedTokens, defaultReserveRatio*float64(fillRate)*targetPeriodTimeSec)
ts.tokenCapacity -= reservedTokens - grantedTokens
grantedTokens = reservedTokens
}
Expand Down
Loading