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

resource_group: don't accumulate tokens when burstlimit less than 0 #7626

Merged
merged 2 commits into from
Jan 3, 2024
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
22 changes: 12 additions & 10 deletions pkg/mcs/resourcemanager/server/token_buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (gtb *GroupTokenBucket) init(now time.Time, clientID uint64) {
if gtb.Settings.FillRate == 0 {
gtb.Settings.FillRate = defaultRefillRate
}
if gtb.Tokens < defaultInitialTokens {
if gtb.Tokens < defaultInitialTokens && gtb.Settings.BurstLimit > 0 {
gtb.Tokens = defaultInitialTokens
}
// init slot
Expand All @@ -311,21 +311,23 @@ func (gtb *GroupTokenBucket) updateTokens(now time.Time, burstLimit int64, clien
var elapseTokens float64
if !gtb.Initialized {
gtb.init(now, clientUniqueID)
} else if delta := now.Sub(*gtb.LastUpdate); delta > 0 {
elapseTokens = float64(gtb.Settings.GetFillRate())*delta.Seconds() + gtb.lastBurstTokens
gtb.lastBurstTokens = 0
gtb.Tokens += elapseTokens
gtb.LastUpdate = &now
} else if burst := float64(burstLimit); burst > 0 {
if delta := now.Sub(*gtb.LastUpdate); delta > 0 {
elapseTokens = float64(gtb.Settings.GetFillRate())*delta.Seconds() + gtb.lastBurstTokens
gtb.lastBurstTokens = 0
gtb.Tokens += elapseTokens
}
if gtb.Tokens > burst {
elapseTokens -= gtb.Tokens - burst
gtb.Tokens = burst
}
}
gtb.LastUpdate = &now
// Reloan when setting changed
if gtb.settingChanged && gtb.Tokens <= 0 {
elapseTokens = 0
gtb.resetLoan()
}
if burst := float64(burstLimit); burst > 0 && gtb.Tokens > burst {
elapseTokens -= gtb.Tokens - burst
gtb.Tokens = burst
}
// Balance each slots.
gtb.balanceSlotTokens(clientUniqueID, gtb.Settings, requiredToken, elapseTokens)
}
Expand Down
31 changes: 30 additions & 1 deletion pkg/mcs/resourcemanager/server/token_buckets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,40 @@ func TestGroupTokenBucketUpdateAndPatch(t *testing.T) {
},
}
tb.patch(tbSetting)

time.Sleep(10 * time.Millisecond)
time2 := time.Now()
tb.request(time2, 0, 0, clientUniqueID)
re.LessOrEqual(math.Abs(100000-tb.Tokens), time2.Sub(time1).Seconds()*float64(tbSetting.Settings.FillRate)+1e7)
re.Equal(tbSetting.Settings.FillRate, tb.Settings.FillRate)

tbSetting = &rmpb.TokenBucket{
Tokens: 0,
Settings: &rmpb.TokenLimitSettings{
FillRate: 2000,
BurstLimit: -1,
},
}
tb = NewGroupTokenBucket(tbSetting)
tb.request(time2, 0, 0, clientUniqueID)
re.LessOrEqual(math.Abs(tbSetting.Tokens), 1e-7)
time3 := time.Now()
tb.request(time3, 0, 0, clientUniqueID)
re.LessOrEqual(math.Abs(tbSetting.Tokens), 1e-7)

tbSetting = &rmpb.TokenBucket{
Tokens: 200000,
Settings: &rmpb.TokenLimitSettings{
FillRate: 2000,
BurstLimit: -1,
},
}
tb = NewGroupTokenBucket(tbSetting)
tb.request(time3, 0, 0, clientUniqueID)
re.LessOrEqual(math.Abs(tbSetting.Tokens-200000), 1e-7)
time.Sleep(10 * time.Millisecond)
time4 := time.Now()
tb.request(time4, 0, 0, clientUniqueID)
re.LessOrEqual(math.Abs(tbSetting.Tokens-200000), 1e-7)
}

func TestGroupTokenBucketRequest(t *testing.T) {
Expand Down
Loading