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

Introduce the resource control client #648

Merged
merged 13 commits into from
Jan 20, 2023
Prev Previous commit
Simplify the code
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Jan 20, 2023
commit 313152f32c389e3c7109b164eb670ccdea62fc0b
33 changes: 10 additions & 23 deletions internal/client/client_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
)

func init() {
resourceControlSwitch.Store(false)
resourceControlInterceptor = nil
ResourceControlSwitch.Store(false)
ResourceControlInterceptor = nil
}

var _ Client = interceptedClient{}
Expand Down Expand Up @@ -62,33 +62,20 @@ func (r interceptedClient) SendRequest(ctx context.Context, addr string, req *ti
}

var (
resourceControlSwitch atomic.Value
resourceControlInterceptor resourceControlClient.ResourceGroupKVInterceptor
// ResourceControlSwitch is used to control whether to enable the resource control.
ResourceControlSwitch atomic.Value
// ResourceControlInterceptor is used to build the resource control interceptor.
ResourceControlInterceptor resourceControlClient.ResourceGroupKVInterceptor
)

// EnableResourceControl enables the resource control.
func EnableResourceControl() {
resourceControlSwitch.Store(true)
}

// DisableResourceControl disables the resource control.
func DisableResourceControl() {
resourceControlSwitch.Store(false)
}

// SetResourceControlInterceptor sets the interceptor for resource control.
func SetResourceControlInterceptor(interceptor resourceControlClient.ResourceGroupKVInterceptor) {
resourceControlInterceptor = interceptor
}

// buildResourceControlInterceptor builds a resource control interceptor with
// the given resource group name.
func buildResourceControlInterceptor(
ctx context.Context,
req *tikvrpc.Request,
resourceGroupName string,
) interceptor.RPCInterceptor {
if !resourceControlSwitch.Load().(bool) {
if !ResourceControlSwitch.Load().(bool) {
return nil
}
// When the group name is empty or "default", we don't need to
Expand All @@ -97,22 +84,22 @@ func buildResourceControlInterceptor(
return nil
}
// No resource group interceptor is set.
if resourceControlInterceptor == nil {
if ResourceControlInterceptor == nil {
return nil
}
// Make the request info.
reqInfo := resourcecontrol.MakeRequestInfo(req)
// Build the interceptor.
return func(next interceptor.RPCInterceptorFunc) interceptor.RPCInterceptorFunc {
return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
err := resourceControlInterceptor.OnRequestWait(ctx, resourceGroupName, reqInfo)
err := ResourceControlInterceptor.OnRequestWait(ctx, resourceGroupName, reqInfo)
if err != nil {
return nil, err
}
resp, err := next(target, req)
if resp != nil {
respInfo := resourcecontrol.MakeResponseInfo(resp)
resourceControlInterceptor.OnResponse(ctx, resourceGroupName, reqInfo, respInfo)
ResourceControlInterceptor.OnResponse(ctx, resourceGroupName, reqInfo, respInfo)
}
return resp, err
}
Expand Down
8 changes: 4 additions & 4 deletions tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,22 @@ func (s *KVStore) updateSafeTS(ctx context.Context) {

// EnableResourceControl enables the resource control.
func EnableResourceControl() {
client.EnableResourceControl()
client.ResourceControlSwitch.Store(true)
}

// DisableResourceControl disables the resource control.
func DisableResourceControl() {
client.DisableResourceControl()
client.ResourceControlSwitch.Store(false)
}

// SetResourceControlInterceptor sets the interceptor for resource control.
func SetResourceControlInterceptor(interceptor resourceControlClient.ResourceGroupKVInterceptor) {
client.SetResourceControlInterceptor(interceptor)
client.ResourceControlInterceptor = interceptor
}

// UnsetResourceControlInterceptor un-sets the interceptor for resource control.
func UnsetResourceControlInterceptor() {
client.SetResourceControlInterceptor(nil)
client.ResourceControlInterceptor = nil
}

// Variables defines the variables used by TiKV storage.
Expand Down