@@ -18,6 +18,7 @@ import (
18
18
"context"
19
19
"encoding/json"
20
20
"math"
21
+ "strings"
21
22
"sync"
22
23
"sync/atomic"
23
24
"time"
@@ -32,6 +33,7 @@ import (
32
33
pd "github.com/tikv/pd/client"
33
34
"github.com/tikv/pd/client/errs"
34
35
"go.uber.org/zap"
36
+ "golang.org/x/exp/slices"
35
37
)
36
38
37
39
const (
@@ -56,8 +58,10 @@ const (
56
58
type ResourceGroupKVInterceptor interface {
57
59
// OnRequestWait is used to check whether resource group has enough tokens. It maybe needs to wait some time.
58
60
OnRequestWait (ctx context.Context , resourceGroupName string , info RequestInfo ) (* rmpb.Consumption , * rmpb.Consumption , error )
59
- // OnResponse is used to consume tokens after receiving response
61
+ // OnResponse is used to consume tokens after receiving response.
60
62
OnResponse (resourceGroupName string , req RequestInfo , resp ResponseInfo ) (* rmpb.Consumption , error )
63
+ // IsBackgroundRequest If the resource group has background jobs, we should not record consumption and wait for it.
64
+ IsBackgroundRequest (ctx context.Context , resourceGroupName , requestResource string ) bool
61
65
}
62
66
63
67
// ResourceGroupProvider provides some api to interact with resource manager server.
@@ -454,7 +458,6 @@ func (c *ResourceGroupsController) OnRequestWait(
454
458
) (* rmpb.Consumption , * rmpb.Consumption , error ) {
455
459
gc , err := c .tryGetResourceGroup (ctx , resourceGroupName )
456
460
if err != nil {
457
- failedRequestCounter .WithLabelValues (resourceGroupName ).Inc ()
458
461
return nil , nil , err
459
462
}
460
463
return gc .onRequestWait (ctx , info )
@@ -472,6 +475,28 @@ func (c *ResourceGroupsController) OnResponse(
472
475
return tmp .(* groupCostController ).onResponse (req , resp )
473
476
}
474
477
478
+ // IsBackgroundRequest If the resource group has background jobs, we should not record consumption and wait for it.
479
+ func (c * ResourceGroupsController ) IsBackgroundRequest (ctx context.Context ,
480
+ resourceGroupName , requestResource string ) bool {
481
+ gc , err := c .tryGetResourceGroup (ctx , resourceGroupName )
482
+ if err != nil {
483
+ failedRequestCounter .WithLabelValues (resourceGroupName ).Inc ()
484
+ return false
485
+ }
486
+
487
+ gc .metaLock .RLock ()
488
+ defer gc .metaLock .RUnlock ()
489
+ if bg := gc .meta .BackgroundSettings ; bg != nil {
490
+ if len (requestResource ) == 0 || len (bg .JobTypes ) == 0 {
491
+ return false
492
+ }
493
+ if idx := strings .LastIndex (requestResource , "_" ); idx != - 1 {
494
+ return slices .Contains (bg .JobTypes , requestResource [idx + 1 :])
495
+ }
496
+ }
497
+ return false
498
+ }
499
+
475
500
// GetResourceGroup returns the meta setting of the given resource group name.
476
501
func (c * ResourceGroupsController ) GetResourceGroup (resourceGroupName string ) (* rmpb.ResourceGroup , error ) {
477
502
gc , err := c .tryGetResourceGroup (c .loopCtx , resourceGroupName )
@@ -518,7 +543,7 @@ type groupCostController struct {
518
543
lastRequestTime time.Time
519
544
520
545
// requestInProgress is set true when sending token bucket request.
521
- // And it is set false when reciving token bucket response.
546
+ // And it is set false when receiving token bucket response.
522
547
// This triggers a retry attempt on the next tick.
523
548
requestInProgress bool
524
549
0 commit comments