-
Notifications
You must be signed in to change notification settings - Fork 727
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
client, resource_manager: resource manager client #5810
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ca83b1d
impl token request
CabinfeverB 497c67d
impl resouce manager client
CabinfeverB 321c2ad
impl token request and fix update bug
CabinfeverB 0fc2ca5
add test
CabinfeverB ef25725
address comment
CabinfeverB 6a13cab
address comment
CabinfeverB 6f18863
address comment
CabinfeverB 2d2fc55
merge master
CabinfeverB 887d221
merge master
CabinfeverB 1b769a6
Merge branch 'master' into resource-manager/client
CabinfeverB 356f118
Merge branch 'resource-manager/server_part3' into resource-manager/cl…
CabinfeverB 7bc3bc9
address comment
CabinfeverB f73f782
address comment
CabinfeverB d5aa646
address comment
CabinfeverB 72079f2
Merge branch 'resource-manager/server_part3' into resource-manager/cl…
CabinfeverB 29f44e1
address comment
CabinfeverB eefe996
address comment
CabinfeverB 69a3ac0
merge master
CabinfeverB 6115bc6
merge master
CabinfeverB d776fca
add test
CabinfeverB 00d13b6
Merge branch 'master' into resource-manager/client
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
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,278 @@ | ||
// Copyright 2023 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, | ||
// 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 pd | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pingcap/errors" | ||
rmpb "github.com/pingcap/kvproto/pkg/resource_manager" | ||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type actionType int | ||
|
||
const ( | ||
add actionType = 0 | ||
modify actionType = 1 | ||
) | ||
|
||
// ResourceManagerClient manages resource group info and token request. | ||
type ResourceManagerClient interface { | ||
ListResourceGroups(ctx context.Context) ([]*rmpb.ResourceGroup, error) | ||
GetResourceGroup(ctx context.Context, resourceGroupName string) (*rmpb.ResourceGroup, error) | ||
AddResourceGroup(ctx context.Context, metaGroup *rmpb.ResourceGroup) (string, error) | ||
ModifyResourceGroup(ctx context.Context, metaGroup *rmpb.ResourceGroup) (string, error) | ||
DeleteResourceGroup(ctx context.Context, resourceGroupName string) (string, error) | ||
AcquireTokenBuckets(ctx context.Context, request *rmpb.TokenBucketsRequest) ([]*rmpb.TokenBucketResponse, error) | ||
} | ||
|
||
// resourceManagerClient gets the ResourceManager client of current PD leader. | ||
func (c *client) resourceManagerClient() rmpb.ResourceManagerClient { | ||
if cc, ok := c.clientConns.Load(c.GetLeaderAddr()); ok { | ||
return rmpb.NewResourceManagerClient(cc.(*grpc.ClientConn)) | ||
} | ||
return nil | ||
} | ||
|
||
// ListResourceGroups loads and returns all metadata of resource groups. | ||
func (c *client) ListResourceGroups(ctx context.Context) ([]*rmpb.ResourceGroup, error) { | ||
req := &rmpb.ListResourceGroupsRequest{} | ||
resp, err := c.resourceManagerClient().ListResourceGroups(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resErr := resp.GetError() | ||
if resErr != nil { | ||
return nil, errors.Errorf("[resource_manager]" + resErr.Message) | ||
} | ||
return resp.GetGroups(), nil | ||
} | ||
|
||
func (c *client) GetResourceGroup(ctx context.Context, resourceGroupName string) (*rmpb.ResourceGroup, error) { | ||
req := &rmpb.GetResourceGroupRequest{ | ||
ResourceGroupName: resourceGroupName, | ||
} | ||
resp, err := c.resourceManagerClient().GetResourceGroup(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resErr := resp.GetError() | ||
if resErr != nil { | ||
return nil, errors.Errorf("[resource_manager]" + resErr.Message) | ||
} | ||
return resp.GetGroup(), nil | ||
} | ||
|
||
func (c *client) AddResourceGroup(ctx context.Context, metaGroup *rmpb.ResourceGroup) (string, error) { | ||
return c.putResourceGroup(ctx, metaGroup, add) | ||
} | ||
|
||
func (c *client) ModifyResourceGroup(ctx context.Context, metaGroup *rmpb.ResourceGroup) (string, error) { | ||
return c.putResourceGroup(ctx, metaGroup, modify) | ||
} | ||
|
||
func (c *client) putResourceGroup(ctx context.Context, metaGroup *rmpb.ResourceGroup, typ actionType) (str string, err error) { | ||
req := &rmpb.PutResourceGroupRequest{ | ||
Group: metaGroup, | ||
} | ||
var resp *rmpb.PutResourceGroupResponse | ||
switch typ { | ||
case add: | ||
resp, err = c.resourceManagerClient().AddResourceGroup(ctx, req) | ||
case modify: | ||
resp, err = c.resourceManagerClient().ModifyResourceGroup(ctx, req) | ||
} | ||
if err != nil { | ||
return str, err | ||
} | ||
resErr := resp.GetError() | ||
if resErr != nil { | ||
return str, errors.Errorf("[resource_manager]" + resErr.Message) | ||
} | ||
str = resp.GetBody() | ||
return | ||
} | ||
|
||
func (c *client) DeleteResourceGroup(ctx context.Context, resourceGroupName string) (string, error) { | ||
req := &rmpb.DeleteResourceGroupRequest{ | ||
ResourceGroupName: resourceGroupName, | ||
} | ||
resp, err := c.resourceManagerClient().DeleteResourceGroup(ctx, req) | ||
if err != nil { | ||
return "", err | ||
} | ||
resErr := resp.GetError() | ||
if resErr != nil { | ||
return "", errors.Errorf("[resource_manager]" + resErr.Message) | ||
} | ||
return resp.GetBody(), nil | ||
} | ||
|
||
func (c *client) AcquireTokenBuckets(ctx context.Context, request *rmpb.TokenBucketsRequest) ([]*rmpb.TokenBucketResponse, error) { | ||
req := &tokenRequest{ | ||
done: make(chan error, 1), | ||
requestCtx: ctx, | ||
clientCtx: c.ctx, | ||
Requeset: request, | ||
} | ||
c.tokenDispatcher.tokenBatchController.tokenRequestCh <- req | ||
grantedTokens, err := req.Wait() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return grantedTokens, err | ||
} | ||
|
||
type tokenRequest struct { | ||
clientCtx context.Context | ||
requestCtx context.Context | ||
done chan error | ||
Requeset *rmpb.TokenBucketsRequest | ||
TokenBuckets []*rmpb.TokenBucketResponse | ||
} | ||
|
||
func (req *tokenRequest) Wait() (tokenBuckets []*rmpb.TokenBucketResponse, err error) { | ||
select { | ||
case err = <-req.done: | ||
err = errors.WithStack(err) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tokenBuckets = req.TokenBuckets | ||
return | ||
case <-req.requestCtx.Done(): | ||
return nil, errors.WithStack(req.requestCtx.Err()) | ||
case <-req.clientCtx.Done(): | ||
return nil, errors.WithStack(req.clientCtx.Err()) | ||
} | ||
} | ||
|
||
type tokenBatchController struct { | ||
tokenRequestCh chan *tokenRequest | ||
} | ||
|
||
func newTokenBatchController(tokenRequestCh chan *tokenRequest) *tokenBatchController { | ||
return &tokenBatchController{ | ||
tokenRequestCh: tokenRequestCh, | ||
} | ||
} | ||
|
||
type tokenDispatcher struct { | ||
dispatcherCancel context.CancelFunc | ||
tokenBatchController *tokenBatchController | ||
} | ||
|
||
type resourceManagerConnectionContext struct { | ||
stream rmpb.ResourceManager_AcquireTokenBucketsClient | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
} | ||
|
||
func (c *client) createTokenispatcher() { | ||
dispatcherCtx, dispatcherCancel := context.WithCancel(c.ctx) | ||
dispatcher := &tokenDispatcher{ | ||
dispatcherCancel: dispatcherCancel, | ||
tokenBatchController: newTokenBatchController( | ||
make(chan *tokenRequest, 1)), | ||
} | ||
go c.handleResourceTokenDispatcher(dispatcherCtx, dispatcher.tokenBatchController) | ||
c.tokenDispatcher = dispatcher | ||
} | ||
|
||
func (c *client) handleResourceTokenDispatcher(dispatcherCtx context.Context, tbc *tokenBatchController) { | ||
var connection resourceManagerConnectionContext | ||
if err := c.tryResourceManagerConnect(dispatcherCtx, &connection); err != nil { | ||
log.Warn("get stream error", zap.Error(err)) | ||
} | ||
|
||
for { | ||
var firstRequest *tokenRequest | ||
select { | ||
case <-dispatcherCtx.Done(): | ||
return | ||
case firstRequest = <-tbc.tokenRequestCh: | ||
} | ||
stream, streamCtx, cancel := connection.stream, connection.ctx, connection.cancel | ||
if stream == nil { | ||
c.tryResourceManagerConnect(dispatcherCtx, &connection) | ||
firstRequest.done <- errors.Errorf("no stream") | ||
continue | ||
} | ||
select { | ||
case <-streamCtx.Done(): | ||
log.Info("[resource_manager] resource manager stream is canceled") | ||
cancel() | ||
connection.stream = nil | ||
continue | ||
default: | ||
} | ||
err := c.processTokenRequests(stream, firstRequest) | ||
if err != nil { | ||
log.Info("processTokenRequests error", zap.Error(err)) | ||
cancel() | ||
connection.stream = nil | ||
} | ||
} | ||
} | ||
|
||
func (c *client) processTokenRequests(stream rmpb.ResourceManager_AcquireTokenBucketsClient, t *tokenRequest) error { | ||
req := t.Requeset | ||
if err := stream.Send(req); err != nil { | ||
err = errors.WithStack(err) | ||
t.done <- err | ||
return err | ||
} | ||
resp, err := stream.Recv() | ||
if err != nil { | ||
err = errors.WithStack(err) | ||
t.done <- err | ||
return err | ||
} | ||
if resp.GetError() != nil { | ||
return errors.Errorf("[resource_manager]" + resp.GetError().Message) | ||
} | ||
tokenBuckets := resp.GetResponses() | ||
t.TokenBuckets = tokenBuckets | ||
t.done <- nil | ||
return nil | ||
} | ||
|
||
func (c *client) tryResourceManagerConnect(ctx context.Context, connection *resourceManagerConnectionContext) error { | ||
var ( | ||
err error | ||
stream rmpb.ResourceManager_AcquireTokenBucketsClient | ||
) | ||
for i := 0; i < maxRetryTimes; i++ { | ||
cctx, cancel := context.WithCancel(ctx) | ||
stream, err = c.resourceManagerClient().AcquireTokenBuckets(cctx) | ||
if err == nil && stream != nil { | ||
connection.cancel = cancel | ||
connection.ctx = cctx | ||
connection.stream = stream | ||
return nil | ||
} | ||
cancel() | ||
select { | ||
case <-ctx.Done(): | ||
return err | ||
case <-time.After(retryInterval): | ||
} | ||
} | ||
return err | ||
} |
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
Oops, something went wrong.
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.
you can replace the client in
tests/msc/resource_manager_test.go
to add the tests.