-
Notifications
You must be signed in to change notification settings - Fork 728
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_manager: implement token assignment in server #5809
Merged
ti-chi-bot
merged 12 commits into
tikv:master
from
CabinfeverB:resource-manager/server_part3
Jan 10, 2023
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ca83b1d
impl token request
CabinfeverB 321c2ad
impl token request and fix update bug
CabinfeverB 0fc2ca5
add test
CabinfeverB ef25725
address comment
CabinfeverB 6a13cab
address comment
CabinfeverB 2d2fc55
merge master
CabinfeverB 887d221
merge master
CabinfeverB d5aa646
address comment
CabinfeverB 5fb09c9
address comment
CabinfeverB fc04c44
address comment
CabinfeverB 3896b95
address comment
CabinfeverB 3865ab3
Merge branch 'master' into resource-manager/server_part3
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2022 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS,g | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package server | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
rmpb "github.com/pingcap/kvproto/pkg/resource_manager" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGroupTokenBucketUpdateAndPatch(t *testing.T) { | ||
re := require.New(t) | ||
tbSetting := &rmpb.TokenBucket{ | ||
Tokens: 200000, | ||
Settings: &rmpb.TokenLimitSettings{ | ||
FillRate: 2000, | ||
BurstLimit: 20000000, | ||
}, | ||
} | ||
|
||
tb := NewGroupTokenBucket(tbSetting) | ||
time1 := time.Now() | ||
tb.update(time1) | ||
re.LessOrEqual(math.Abs(tbSetting.Tokens-tb.Tokens), 1e-7) | ||
re.Equal(tbSetting.Settings.FillRate, tb.Settings.FillRate) | ||
|
||
tbSetting = &rmpb.TokenBucket{ | ||
Tokens: -100000, | ||
Settings: &rmpb.TokenLimitSettings{ | ||
FillRate: 1000, | ||
BurstLimit: 10000000, | ||
}, | ||
} | ||
tb.patch(tbSetting) | ||
|
||
time2 := time.Now() | ||
tb.update(time2) | ||
re.LessOrEqual(math.Abs(100000-tb.Tokens), time2.Sub(time1).Seconds()*float64(tbSetting.Settings.FillRate)+1e7) | ||
re.Equal(tbSetting.Settings.FillRate, tb.Settings.FillRate) | ||
} | ||
|
||
func TestGroupTokenBucketRequest(t *testing.T) { | ||
re := require.New(t) | ||
tbSetting := &rmpb.TokenBucket{ | ||
Tokens: 200000, | ||
Settings: &rmpb.TokenLimitSettings{ | ||
FillRate: 2000, | ||
BurstLimit: 20000000, | ||
}, | ||
} | ||
|
||
gtb := NewGroupTokenBucket(tbSetting) | ||
time1 := time.Now() | ||
gtb.update(time1) | ||
tb, trickle := gtb.request(100000, uint64(time.Second)*10/uint64(time.Millisecond)) | ||
re.LessOrEqual(math.Abs(tb.Tokens-100000), 1e-7) | ||
re.Equal(trickle, int64(0)) | ||
tb, trickle = gtb.request(101000, uint64(time.Second)*10/uint64(time.Millisecond)) | ||
re.LessOrEqual(math.Abs(tb.Tokens-101000), 1e-7) | ||
re.Equal(trickle, int64(time.Second)*10/int64(time.Millisecond)) | ||
tb, trickle = gtb.request(17000, uint64(time.Second)*10/uint64(time.Millisecond)) | ||
re.LessOrEqual(math.Abs(tb.Tokens-17000), 1e-7) | ||
re.Equal(trickle, int64(time.Second)*10/int64(time.Millisecond)) | ||
tb, trickle = gtb.request(4000, uint64(time.Second)*10/uint64(time.Millisecond)) | ||
re.LessOrEqual(math.Abs(tb.Tokens-4000), 1e-7) | ||
re.Equal(trickle, int64(time.Second)*10/int64(time.Millisecond)) | ||
tb, trickle = gtb.request(19000, uint64(time.Second)*10/uint64(time.Millisecond)) | ||
re.LessOrEqual(math.Abs(tb.Tokens-18000), 1e-7) | ||
re.Equal(trickle, int64(time.Second)*10/int64(time.Millisecond)) | ||
time2 := time1.Add(10 * time.Second) | ||
gtb.update(time2) | ||
tb, trickle = gtb.request(20000, uint64(time.Second)*10/uint64(time.Millisecond)) | ||
re.LessOrEqual(math.Abs(tb.Tokens-20000), 1e-7) | ||
re.Equal(trickle, int64(time.Second)*10/int64(time.Millisecond)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,6 +15,7 @@ | |||||
package server | ||||||
|
||||||
import ( | ||||||
"math" | ||||||
"time" | ||||||
|
||||||
"github.com/gogo/protobuf/proto" | ||||||
|
@@ -23,14 +24,31 @@ import ( | |||||
|
||||||
const defaultRefillRate = 10000 | ||||||
|
||||||
const defaultInitialTokens = 10 * 10000 | ||||||
const ( | ||||||
defaultInitialTokens = 10 * 10000 | ||||||
defaultMaxTokens = 1e7 | ||||||
) | ||||||
|
||||||
var reserveRatio float64 = 0.05 | ||||||
|
||||||
// GroupTokenBucket is a token bucket for a resource group. | ||||||
// TODO: statistics Consumption | ||||||
type GroupTokenBucket struct { | ||||||
*rmpb.TokenBucket `json:"token_bucket,omitempty"` | ||||||
Consumption *rmpb.TokenBucketsRequest `json:"consumption,omitempty"` | ||||||
LastUpdate *time.Time `json:"last_update,omitempty"` | ||||||
Initialized bool `json:"initialized"` | ||||||
// MaxTokens limits the number of tokens that can be accumulated | ||||||
MaxTokens float64 `json:"max_tokens,omitempty"` | ||||||
|
||||||
Consumption *rmpb.TokenBucketsRequest `json:"consumption,omitempty"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
LastUpdate *time.Time `json:"last_update,omitempty"` | ||||||
Initialized bool `json:"initialized"` | ||||||
} | ||||||
|
||||||
// NewGroupTokenBucket returns a new GroupTokenBucket | ||||||
func NewGroupTokenBucket(tokenBucket *rmpb.TokenBucket) GroupTokenBucket { | ||||||
return GroupTokenBucket{ | ||||||
TokenBucket: tokenBucket, | ||||||
MaxTokens: defaultMaxTokens, | ||||||
} | ||||||
} | ||||||
|
||||||
// patch patches the token bucket settings. | ||||||
|
@@ -51,15 +69,19 @@ func (t *GroupTokenBucket) patch(settings *rmpb.TokenBucket) { | |||||
t.TokenBucket = tb | ||||||
} | ||||||
|
||||||
// Update updates the token bucket. | ||||||
func (t *GroupTokenBucket) Update(now time.Time) { | ||||||
// update updates the token bucket. | ||||||
func (t *GroupTokenBucket) update(now time.Time) { | ||||||
if !t.Initialized { | ||||||
if t.Settings.FillRate == 0 { | ||||||
t.Settings.FillRate = defaultRefillRate | ||||||
} | ||||||
if t.Tokens < defaultInitialTokens { | ||||||
t.Tokens = defaultInitialTokens | ||||||
} | ||||||
// TODO: If we support init or modify MaxTokens in the future, we can move following code. | ||||||
if t.Tokens > t.MaxTokens { | ||||||
t.MaxTokens = t.Tokens | ||||||
} | ||||||
t.LastUpdate = &now | ||||||
t.Initialized = true | ||||||
return | ||||||
|
@@ -70,12 +92,68 @@ func (t *GroupTokenBucket) Update(now time.Time) { | |||||
t.Tokens += float64(t.Settings.FillRate) * delta.Seconds() | ||||||
t.LastUpdate = &now | ||||||
} | ||||||
if t.Tokens > t.MaxTokens { | ||||||
t.Tokens = t.MaxTokens | ||||||
} | ||||||
} | ||||||
|
||||||
// Request requests tokens from the token bucket. | ||||||
func (t *GroupTokenBucket) Request( | ||||||
// request requests tokens from the token bucket. | ||||||
func (t *GroupTokenBucket) request( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||||||
neededTokens float64, targetPeriodMs uint64, | ||||||
) *rmpb.TokenBucket { | ||||||
// TODO: Implement the token bucket algorithm. | ||||||
return nil | ||||||
) (*rmpb.TokenBucket, int64) { | ||||||
var res rmpb.TokenBucket | ||||||
res.Settings = &rmpb.TokenLimitSettings{} | ||||||
// FillRate is used for the token server unavailable in abnormal situation. | ||||||
if neededTokens <= 0 { | ||||||
return &res, 0 | ||||||
} | ||||||
|
||||||
// If the current tokens can directly meet the requirement, returns the need token | ||||||
if t.Tokens >= neededTokens { | ||||||
t.Tokens -= neededTokens | ||||||
// granted the total request tokens | ||||||
res.Tokens = neededTokens | ||||||
return &res, 0 | ||||||
} | ||||||
|
||||||
// Firstly allocate the remaining tokens | ||||||
var grantedTokens float64 | ||||||
if t.Tokens > 0 { | ||||||
grantedTokens = t.Tokens | ||||||
neededTokens -= grantedTokens | ||||||
} | ||||||
|
||||||
var trickleTime = time.Duration(targetPeriodMs) * time.Millisecond | ||||||
availableRate := float64(t.Settings.FillRate) | ||||||
// When there are debt, the allotment will match the fill rate. | ||||||
// We will have a threshold, beyond which the token allocation will be a minimum. | ||||||
// the current threshold is fill rate * target period * 2. | ||||||
// | | ||||||
// fill rate |· · · · · · · · · | ||||||
// | · | ||||||
// | · | ||||||
// | · | ||||||
// | · | ||||||
// reserve rate | · · · · | ||||||
// | | ||||||
// rate 0 ----------------------------------------------- | ||||||
// debt period token 2*period token | ||||||
if debt := -t.Tokens; debt > 0 { | ||||||
debt -= float64(t.Settings.FillRate) * trickleTime.Seconds() | ||||||
if debt > 0 { | ||||||
debtRate := debt / float64(targetPeriodMs/1000) | ||||||
availableRate -= debtRate | ||||||
availableRate = math.Max(availableRate, reserveRatio*float64(t.Settings.FillRate)) | ||||||
} | ||||||
} | ||||||
|
||||||
consumptionDuration := time.Duration(float64(time.Second) * (neededTokens / availableRate)) | ||||||
if consumptionDuration <= trickleTime { | ||||||
grantedTokens += neededTokens | ||||||
} else { | ||||||
grantedTokens += availableRate * trickleTime.Seconds() | ||||||
} | ||||||
t.Tokens -= grantedTokens | ||||||
res.Tokens = grantedTokens | ||||||
return &res, trickleTime.Milliseconds() | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.