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_manager: implement token assignment in server #5809

Merged
merged 12 commits into from
Jan 10, 2023

Conversation

CabinfeverB
Copy link
Member

@CabinfeverB CabinfeverB commented Dec 29, 2022

Signed-off-by: Cabinfever_B cabinfeveroier@gmail.com

What problem does this PR solve?

Issue Number: Close #5822

What is changed and how does it work?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Code changes

Side effects

  • Possible performance regression
  • Increased code complexity
  • Breaking backward compatibility

Related changes

Release note

Please add a release note.

Please refer to [Release Notes Language Style Guide](https://pingcap.github.io/tidb-dev-guide/contribute-to-tidb/release-notes-style-guide.html) to write a quality release note.

If you don't think this PR needs a release note then fill it with None.

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Dec 29, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • JmPotato
  • nolouch

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added do-not-merge/needs-linked-issue release-note The PR should write the release note. labels Dec 29, 2022
Copy link
Contributor

@lhy1024 lhy1024 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

roken? token?

@codecov
Copy link

codecov bot commented Dec 29, 2022

Codecov Report

Base: 75.40% // Head: 75.45% // Increases project coverage by +0.04% 🎉

Coverage data is based on head (3865ab3) compared to base (1b778f2).
Patch coverage: 49.12% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5809      +/-   ##
==========================================
+ Coverage   75.40%   75.45%   +0.04%     
==========================================
  Files         338      338              
  Lines       33774    33859      +85     
==========================================
+ Hits        25468    25548      +80     
- Misses       6113     6120       +7     
+ Partials     2193     2191       -2     
Flag Coverage Δ
unittests 75.45% <49.12%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/mcs/resource_manager/server/grpc_service.go 40.57% <0.00%> (-35.10%) ⬇️
pkg/mcs/resource_manager/server/resource_group.go 59.81% <0.00%> (-1.14%) ⬇️
pkg/mcs/resource_manager/server/token_bukets.go 89.18% <87.50%> (+50.05%) ⬆️
pkg/utils/metricutil/metricutil.go 82.75% <0.00%> (-10.35%) ⬇️
pkg/utils/tempurl/tempurl.go 60.00% <0.00%> (-10.00%) ⬇️
...erver/config/service_middleware_persist_options.go 91.66% <0.00%> (-8.34%) ⬇️
server/storage/storage.go 67.30% <0.00%> (-3.85%) ⬇️
server/schedulers/random_merge.go 62.50% <0.00%> (-3.13%) ⬇️
pkg/election/leadership.go 77.31% <0.00%> (-3.10%) ⬇️
server/server.go 74.61% <0.00%> (-1.03%) ⬇️
... and 22 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@CabinfeverB CabinfeverB changed the title resource_manager: implement about roken request resource_manager: implement about token request Dec 29, 2022
@nolouch nolouch changed the title resource_manager: implement about token request resource_manager: implement token assignment in server Dec 30, 2022
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"`
LoanExpireTime *time.Time `json:"loan_time,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments about the loan.

Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz link the issue.

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>

impl token request and fix update bug

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
for _, req := range request.Requests {
rg := s.manager.GetResourceGroup(req.ResourceGroupName)
if rg == nil {
return errors.New("resource group not found")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A logical error should not exit the main loop. Should set a error message in the response instead.

}
}
case rmpb.GroupMode_NativeMode:
return errors.New("not supports the resource type")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

var res rmpb.TokenBucket
res.Settings = &rmpb.TokenLimitSettings{}
// TODO: consider the shares for dispatch the fill rate
res.Settings.Fillrate = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's used for the token server unavailable in abnormal situation.

} else {
et := t.LastUpdate.Add(t.LoanMaxPeriod)
t.LoanExpireTime = &et
periodFilled = float64(t.Settings.Fillrate) * (1 - loanReserveRatio) * t.LoanMaxPeriod.Seconds()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to ensure LoadMaxPeriod period greater than targetPeriodMs ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and why not periodFilled = float64(t.Settings.Fillrate) * (1 - loanReserveRatio) * trickleTime

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

periodFilled is the number of tokens that can be allocated in whole loan period, so I think we should use LoanMaxPeriod

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will make the tokens not be graceful consumed with Fillrate in the loan period. Client will consume more tokens in target period(loan expire period> target period)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that you want the consumption curve to be smoother. To fit this situation, we can use shorter LoanMaxPeriod. We can use long LoanMaxPeriod for high credit or important resource group, so they can respond more promptly to qps peaks.

periodFilled = float64(t.Settings.Fillrate) * (1 - loanReserveRatio) * t.LoanMaxPeriod.Seconds()
}
periodFilled += t.Tokens
if periodFilled <= float64(t.Settings.Fillrate)*loanReserveRatio {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need * trickleTime?

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@nolouch
Copy link
Contributor

nolouch commented Jan 6, 2023

@CabinfeverB Has some conflicts.

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 6, 2023
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jan 6, 2023
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
// Request requests tokens from the token bucket.
func (t *GroupTokenBucket) Request(
// request requests tokens from the token bucket.
func (t *GroupTokenBucket) request(
Copy link
Contributor

@nolouch nolouch Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think request and update can be one function, also reduce the code functions.

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
Comment on lines 160 to 167
switch re.Type {
case rmpb.RequestUnitType_RRU:
tokens := rg.RequestRRU(now, re.Value, targetPeriodMs)
resp.GrantedRUTokens = append(resp.GrantedRUTokens, tokens)
case rmpb.RequestUnitType_WRU:
tokens := rg.RequestWRU(now, re.Value, targetPeriodMs)
resp.GrantedRUTokens = append(resp.GrantedRUTokens, tokens)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
switch re.Type {
case rmpb.RequestUnitType_RRU:
tokens := rg.RequestRRU(now, re.Value, targetPeriodMs)
resp.GrantedRUTokens = append(resp.GrantedRUTokens, tokens)
case rmpb.RequestUnitType_WRU:
tokens := rg.RequestWRU(now, re.Value, targetPeriodMs)
resp.GrantedRUTokens = append(resp.GrantedRUTokens, tokens)
}
switch re.Type {
case rmpb.RequestUnitType_RRU:
tokens := rg.RequestRRU(now, re.Value, targetPeriodMs)
case rmpb.RequestUnitType_WRU:
tokens := rg.RequestWRU(now, re.Value, targetPeriodMs)
}
resp.GrantedRUTokens = append(resp.GrantedRUTokens, tokens)


// GroupTokenBucket is a token bucket for a resource group.
// TODO: statistics Consumption
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: statistics Consumption
// TODO: statistics consumption @JmPotato

Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rest LGTM

// MaxTokens limits the number of tokens that can be accumulated
MaxTokens float64 `json:"max_tokens,omitempty"`

Consumption *rmpb.TokenBucketsRequest `json:"consumption,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Consumption *rmpb.TokenBucketsRequest `json:"consumption,omitempty"`
Consumption *rmpb.Consumption `json:"consumption,omitempty"`

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jan 10, 2023
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jan 10, 2023
@JmPotato
Copy link
Member

/merge

@ti-chi-bot
Copy link
Member

@JmPotato: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 3896b95

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jan 10, 2023
@ti-chi-bot
Copy link
Member

@CabinfeverB: Your PR was out of date, I have automatically updated it for you.

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot ti-chi-bot merged commit 4363701 into tikv:master Jan 10, 2023
@JmPotato JmPotato deleted the resource-manager/server_part3 branch January 11, 2023 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note The PR should write the release note. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Resource Manager] Token Bucket manage
6 participants