Skip to content

Commit 7793c67

Browse files
JmPotatoglorv
andauthored
feat(resourcecontrol): refine request bypass logic for internal analyze and system requests (#1772)
ref tidbcloud/cloud-storage-engine#3703 Signed-off-by: JmPotato <[email protected]> Co-authored-by: glorv <[email protected]>
1 parent f8d9beb commit 7793c67

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

internal/client/client_interceptor.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ func getResourceControlInfo(ctx context.Context, req *tikvrpc.Request) (
161161
if rcInterceptor == nil {
162162
return "", nil, nil
163163
}
164-
// bypass some internal requests and it's may influence user experience. For example, the
165-
// request of `alter user password`, totally bypasses the resource control. it's not cost
166-
// many resources, but it's may influence the user experience.
167164
// If the resource group has background jobs, we should not record consumption and wait for it.
168165
// Background jobs will record and report in tikv side.
169166
resourceControlInterceptor := *rcInterceptor

internal/resourcecontrol/resource_control.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/pingcap/kvproto/pkg/coprocessor"
2222
"github.com/pingcap/kvproto/pkg/kvrpcpb"
23+
"github.com/tikv/client-go/v2/config"
2324
"github.com/tikv/client-go/v2/kv"
2425
"github.com/tikv/client-go/v2/tikvrpc"
2526
"github.com/tikv/client-go/v2/util"
@@ -53,15 +54,28 @@ func toPDAccessLocationType(accessType kv.AccessLocationType) controller.AccessL
5354
}
5455
}
5556

56-
// MakeRequestInfo extracts the relevant information from a BatchRequest.
57-
func MakeRequestInfo(req *tikvrpc.Request) *RequestInfo {
58-
var bypass bool
57+
// reqTypeAnalyze is the type of analyze coprocessor request.
58+
// ref: https://github.com/pingcap/tidb/blob/ee4eac2ccb83e1ea653b8131d9a43495019cb5ac/pkg/kv/kv.go#L340
59+
const reqTypeAnalyze = 104
60+
61+
func shouldBypass(req *tikvrpc.Request) bool {
5962
requestSource := req.Context.GetRequestSource()
60-
if len(requestSource) > 0 {
61-
if strings.Contains(requestSource, util.InternalRequestPrefix+util.InternalTxnOthers) {
62-
bypass = true
63-
}
63+
// Check both coprocessor request type and the request source to ensure the request is an internal analyze request.
64+
// Internal analyze request may consume a lot of resources, bypass it to avoid affecting the user experience.
65+
// This bypass currently only works with NextGen.
66+
if config.NextGen && (req.BatchCop().GetTp() == reqTypeAnalyze || req.Cop().GetTp() == reqTypeAnalyze) &&
67+
strings.Contains(requestSource, util.InternalTxnStats) {
68+
return true
6469
}
70+
// Some internal requests should be bypassed, which may affect the user experience.
71+
// For example, the `alter user password` request completely bypasses resource control.
72+
// Although it does not consume many resources, it can still impact the user experience.
73+
return strings.Contains(requestSource, util.InternalRequestPrefix+util.InternalTxnOthers)
74+
}
75+
76+
// MakeRequestInfo extracts the relevant information from a BatchRequest.
77+
func MakeRequestInfo(req *tikvrpc.Request) *RequestInfo {
78+
bypass := shouldBypass(req)
6579
storeID := req.Context.GetPeer().GetStoreId()
6680
if !req.IsTxnWriteRequest() && !req.IsRawWriteRequest() {
6781
return &RequestInfo{

util/request_source.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const (
3939
InternalTxnGC = "gc"
4040
// InternalTxnMeta is the type of the miscellaneous meta usage.
4141
InternalTxnMeta = InternalTxnOthers
42+
// InternalTxnStats is the type of statistics txn.
43+
InternalTxnStats = "stats"
4244
)
4345

4446
// explicit source types.

0 commit comments

Comments
 (0)