Skip to content

Commit 890da77

Browse files
Temahawkingrei
authored andcommitted
*: integrate circuitbreaker for get region calls to PD (pingcap#58737)
close pingcap#58780
1 parent 19bb67b commit 890da77

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

pkg/domain/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ go_library(
111111
"@com_github_tikv_pd_client//:client",
112112
"@com_github_tikv_pd_client//http",
113113
"@com_github_tikv_pd_client//opt",
114+
"@com_github_tikv_pd_client//pkg/circuitbreaker",
114115
"@com_github_tikv_pd_client//resource_group/controller",
115116
"@io_etcd_go_etcd_client_v3//:client",
116117
"@io_etcd_go_etcd_client_v3//concurrency",

pkg/domain/domain_sysvars.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323
"github.com/pingcap/tidb/pkg/meta"
2424
"github.com/pingcap/tidb/pkg/sessionctx/vardef"
2525
"github.com/pingcap/tidb/pkg/sessionctx/variable"
26+
"github.com/tikv/client-go/v2/tikv"
2627
pd "github.com/tikv/pd/client"
2728
"github.com/tikv/pd/client/opt"
29+
"github.com/tikv/pd/client/pkg/circuitbreaker"
2830
)
2931

3032
// initDomainSysVars() is called when a domain is initialized.
@@ -46,6 +48,8 @@ func (do *Domain) initDomainSysVars() {
4648
variable.SetLowResolutionTSOUpdateInterval = do.setLowResolutionTSOUpdateInterval
4749

4850
variable.ChangeSchemaCacheSize = do.changeSchemaCacheSize
51+
52+
variable.ChangePDMetadataCircuitBreakerErrorRateThresholdPct = changePDMetadataCircuitBreakerErrorRateThresholdPct
4953
}
5054

5155
// setStatsCacheCapacity sets statsCache cap
@@ -151,3 +155,9 @@ func (do *Domain) changeSchemaCacheSize(ctx context.Context, size uint64) error
151155
do.infoCache.Data.SetCacheCapacity(size)
152156
return nil
153157
}
158+
159+
func changePDMetadataCircuitBreakerErrorRateThresholdPct(errorRatePct uint32) {
160+
tikv.ChangePDRegionMetaCircuitBreakerSettings(func(config *circuitbreaker.Settings) {
161+
config.ErrorRateThresholdPct = errorRatePct
162+
})
163+
}

pkg/sessionctx/vardef/tidb_vars.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,9 @@ const (
12161216
// TiDBTSOClientRPCMode controls how the TSO client performs the TSO RPC requests. It internally controls the
12171217
// concurrency of the RPC. This variable provides an approach to tune the latency of getting timestamps from PD.
12181218
TiDBTSOClientRPCMode = "tidb_tso_client_rpc_mode"
1219+
// TiDBCircuitBreakerPDMetadataErrorRateThresholdPct variable is used to set percent of errors to trip the circuit breaker for get region calls to PD
1220+
// https://github.com/tikv/rfcs/blob/master/text/0115-circuit-breaker.md
1221+
TiDBCircuitBreakerPDMetadataErrorRateThresholdPct = "tidb_cb_pd_metadata_error_rate_threshold_pct"
12191222
)
12201223

12211224
// TiDB intentional limits, can be raised in the future.
@@ -1567,6 +1570,7 @@ const (
15671570
DefOptEnableProjectionPushDown = true
15681571
DefTiDBEnableSharedLockPromotion = false
15691572
DefTiDBTSOClientRPCMode = TSOClientRPCModeDefault
1573+
DefTiDBCircuitBreakerPDMetaErrorRatePct = 0
15701574
)
15711575

15721576
// Process global variables.

pkg/sessionctx/variable/sysvar.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3448,6 +3448,14 @@ var defaultSysVars = []*SysVar{
34483448
return (*SetPDClientDynamicOption.Load())(vardef.TiDBTSOClientRPCMode, val)
34493449
},
34503450
},
3451+
{Scope: vardef.ScopeGlobal, Name: vardef.TiDBCircuitBreakerPDMetadataErrorRateThresholdPct, Value: strconv.Itoa(vardef.DefTiDBCircuitBreakerPDMetaErrorRatePct), Type: vardef.TypeUnsigned, MinValue: 0, MaxValue: 100,
3452+
SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
3453+
if ChangePDMetadataCircuitBreakerErrorRateThresholdPct != nil {
3454+
ChangePDMetadataCircuitBreakerErrorRateThresholdPct(uint32(tidbOptPositiveInt32(val, vardef.DefTiDBCircuitBreakerPDMetaErrorRatePct)))
3455+
}
3456+
return nil
3457+
},
3458+
},
34513459
}
34523460

34533461
// GlobalSystemVariableInitialValue gets the default value for a system variable including ones that are dynamically set (e.g. based on the store)

pkg/sessionctx/variable/sysvar_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,30 @@ func TestTiDBSchemaCacheSize(t *testing.T) {
17431743
require.Error(t, err)
17441744
}
17451745

1746+
func TestTiDBCircuitBreakerPDMetadataErrorRateThresholdPct(t *testing.T) {
1747+
sv := GetSysVar(vardef.TiDBCircuitBreakerPDMetadataErrorRateThresholdPct)
1748+
vars := NewSessionVars(nil)
1749+
1750+
// Too low, will get raised to the min value
1751+
val, err := sv.Validate(vars, "-1", vardef.ScopeGlobal)
1752+
require.NoError(t, err)
1753+
require.Equal(t, strconv.FormatInt(GetSysVar(vardef.TiDBCircuitBreakerPDMetadataErrorRateThresholdPct).MinValue, 10), val)
1754+
warn := vars.StmtCtx.GetWarnings()[0].Err
1755+
require.Equal(t, "[variable:1292]Truncated incorrect tidb_cb_pd_metadata_error_rate_threshold_pct value: '-1'", warn.Error())
1756+
1757+
// Too high, will get lowered to the max value
1758+
val, err = sv.Validate(vars, "101", vardef.ScopeGlobal)
1759+
require.NoError(t, err)
1760+
require.Equal(t, strconv.FormatUint(GetSysVar(vardef.TiDBCircuitBreakerPDMetadataErrorRateThresholdPct).MaxValue, 10), val)
1761+
warn = vars.StmtCtx.GetWarnings()[1].Err
1762+
require.Equal(t, "[variable:1292]Truncated incorrect tidb_cb_pd_metadata_error_rate_threshold_pct value: '101'", warn.Error())
1763+
1764+
// valid
1765+
val, err = sv.Validate(vars, "10", vardef.ScopeGlobal)
1766+
require.NoError(t, err)
1767+
require.Equal(t, "10", val)
1768+
}
1769+
17461770
func TestEnableWindowFunction(t *testing.T) {
17471771
vars := NewSessionVars(nil)
17481772
require.Equal(t, vars.EnableWindowFunction, vardef.DefEnableWindowFunction)

pkg/sessionctx/variable/tidb_vars.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ var (
5454
EnableStatsOwner func() error = nil
5555
// DisableStatsOwner is the func registered by stats to disable running stats in this instance.
5656
DisableStatsOwner func() error = nil
57+
// ChangePDMetadataCircuitBreakerErrorRateThresholdPct changes the error rate threshold of the PD metadata circuit breaker.
58+
ChangePDMetadataCircuitBreakerErrorRateThresholdPct func(uint32) = nil
5759
)
5860

5961
// Hooks functions for Cluster Resource Control.

0 commit comments

Comments
 (0)