From 7793c676c4e9f22a85e157e3f4d5abf4df793893 Mon Sep 17 00:00:00 2001 From: JmPotato Date: Mon, 27 Oct 2025 14:44:10 +0800 Subject: [PATCH] feat(resourcecontrol): refine request bypass logic for internal analyze and system requests (#1772) ref tidbcloud/cloud-storage-engine#3703 Signed-off-by: JmPotato Co-authored-by: glorv --- internal/client/client_interceptor.go | 3 --- internal/resourcecontrol/resource_control.go | 28 +++++++++++++++----- util/request_source.go | 2 ++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/internal/client/client_interceptor.go b/internal/client/client_interceptor.go index 6ad7acd3..a6a67c9a 100644 --- a/internal/client/client_interceptor.go +++ b/internal/client/client_interceptor.go @@ -161,9 +161,6 @@ func getResourceControlInfo(ctx context.Context, req *tikvrpc.Request) ( if rcInterceptor == nil { return "", nil, nil } - // bypass some internal requests and it's may influence user experience. For example, the - // request of `alter user password`, totally bypasses the resource control. it's not cost - // many resources, but it's may influence the user experience. // If the resource group has background jobs, we should not record consumption and wait for it. // Background jobs will record and report in tikv side. resourceControlInterceptor := *rcInterceptor diff --git a/internal/resourcecontrol/resource_control.go b/internal/resourcecontrol/resource_control.go index a496cc99..dc66c679 100644 --- a/internal/resourcecontrol/resource_control.go +++ b/internal/resourcecontrol/resource_control.go @@ -20,6 +20,7 @@ import ( "github.com/pingcap/kvproto/pkg/coprocessor" "github.com/pingcap/kvproto/pkg/kvrpcpb" + "github.com/tikv/client-go/v2/config" "github.com/tikv/client-go/v2/kv" "github.com/tikv/client-go/v2/tikvrpc" "github.com/tikv/client-go/v2/util" @@ -53,15 +54,28 @@ func toPDAccessLocationType(accessType kv.AccessLocationType) controller.AccessL } } +// reqTypeAnalyze is the type of analyze coprocessor request. +// ref: https://github.com/pingcap/tidb/blob/ee4eac2ccb83e1ea653b8131d9a43495019cb5ac/pkg/kv/kv.go#L340 +const reqTypeAnalyze = 104 + +func shouldBypass(req *tikvrpc.Request) bool { + requestSource := req.Context.GetRequestSource() + // Check both coprocessor request type and the request source to ensure the request is an internal analyze request. + // Internal analyze request may consume a lot of resources, bypass it to avoid affecting the user experience. + // This bypass currently only works with NextGen. + if config.NextGen && (req.BatchCop().GetTp() == reqTypeAnalyze || req.Cop().GetTp() == reqTypeAnalyze) && + strings.Contains(requestSource, util.InternalTxnStats) { + return true + } + // Some internal requests should be bypassed, which may affect the user experience. + // For example, the `alter user password` request completely bypasses resource control. + // Although it does not consume many resources, it can still impact the user experience. + return strings.Contains(requestSource, util.InternalRequestPrefix+util.InternalTxnOthers) +} + // MakeRequestInfo extracts the relevant information from a BatchRequest. func MakeRequestInfo(req *tikvrpc.Request) *RequestInfo { - var bypass bool - requestSource := req.Context.GetRequestSource() - if len(requestSource) > 0 { - if strings.Contains(requestSource, util.InternalRequestPrefix+util.InternalTxnOthers) { - bypass = true - } - } + bypass := shouldBypass(req) storeID := req.Context.GetPeer().GetStoreId() if !req.IsTxnWriteRequest() && !req.IsRawWriteRequest() { return &RequestInfo{ diff --git a/util/request_source.go b/util/request_source.go index 833c30e0..405c235b 100644 --- a/util/request_source.go +++ b/util/request_source.go @@ -39,6 +39,8 @@ const ( InternalTxnGC = "gc" // InternalTxnMeta is the type of the miscellaneous meta usage. InternalTxnMeta = InternalTxnOthers + // InternalTxnStats is the type of statistics txn. + InternalTxnStats = "stats" ) // explicit source types.