Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4207,8 +4207,8 @@ def go_deps():
build_tags = ["nextgen", "intest"],
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/pd/client",
sum = "h1:OoBvgoeWmdNEXtS+eOlhysz/OvhA4GS0OdPVhTXteGA=",
version = "v0.0.0-20260708075407-4e05b9d2c2d3",
sum = "h1:BiPn4oLXVqB9ptPNmyo3rh/2IH1dTouXrrnneH86Z3M=",
version = "v0.0.0-20260720043438-0b37df9a48ed",
)
go_repository(
name = "com_github_timakin_bodyclose",
Expand Down
19 changes: 19 additions & 0 deletions cmd/tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ func overrideConfig(cfg *config.Config, fset *flag.FlagSet) {
fset.Visit(func(f *flag.Flag) {
actualFlags[f.Name] = true
})
if actualFlags[nmStarterParams] && cfg.DeployMode == deploymode.Starter {
terror.MustNil(applyStarterAdditionalParams(cfg, getStarterAdditionalParams()))
}

// Base
if actualFlags[nmHost] {
Expand Down Expand Up @@ -1352,6 +1355,7 @@ type starterParams struct {
podName string
podIP string
podNamespace string
enableRGFallback bool
}

func parseStarterAdditionalParams(raw string) (starterParams, error) {
Expand Down Expand Up @@ -1394,13 +1398,28 @@ func parseStarterAdditionalParams(raw string) (starterParams, error) {
params.podIP = value
case "pod-namespace":
params.podNamespace = value
case "enable-rg-fallback":
enable, err := strconv.ParseBool(value)
if err != nil {
return params, fmt.Errorf("starter additional param %q must be a bool: %w", key, err)
}
params.enableRGFallback = enable
default:
return params, fmt.Errorf("unknown starter additional param %q", key)
}
}
return params, nil
}

func applyStarterAdditionalParams(cfg *config.Config, raw string) error {
params, err := parseStarterAdditionalParams(raw)
if err != nil {
return err
}
cfg.StarterParams.EnableRGFallback = params.enableRGFallback
return nil
}

func getStarterAdditionalParams() string {
if starterAdditionalParams == nil {
return ""
Expand Down
11 changes: 9 additions & 2 deletions cmd/tidb-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ func TestOverrideConfigKeyspaceActivateMode(t *testing.T) {
fset := initFlagSet()
require.NoError(t, fset.Parse([]string{
"--keyspace-activate=true",
"--starter-additional-params=pod-name=pod-1,pod-ip=10.0.0.1,pod-namespace=ns-1",
"--starter-additional-params=pod-name=pod-1,pod-ip=10.0.0.1,pod-namespace=ns-1,enable-rg-fallback=true",
}))

cfg := config.NewConfig()
cfg.DeployMode = deploymode.Starter
overrideConfig(cfg, fset)
require.True(t, cfg.KeyspaceActivateMode)
require.Equal(t, "pod-name=pod-1,pod-ip=10.0.0.1,pod-namespace=ns-1", *starterAdditionalParams)
require.True(t, cfg.StarterParams.EnableRGFallback)
require.Equal(t, "pod-name=pod-1,pod-ip=10.0.0.1,pod-namespace=ns-1,enable-rg-fallback=true", *starterAdditionalParams)
}

func TestSetGlobalVars(t *testing.T) {
Expand Down Expand Up @@ -217,6 +219,11 @@ func TestCreateMgrClientRequiresPodIdentityInStarter(t *testing.T) {
_, err = createMgrClientForStarter()
require.ErrorContains(t, err, `unknown starter additional param "unknown"`)

invalidBoolParam := "enable-rg-fallback=definitely"
starterAdditionalParams = &invalidBoolParam
_, err = createMgrClientForStarter()
require.ErrorContains(t, err, `starter additional param "enable-rg-fallback" must be a bool`)

config.UpdateGlobal(func(conf *config.Config) {
conf.StarterParams.ManagerAddr = ""
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ require (
github.com/stretchr/testify v1.11.1
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.8-0.20260708122311-01bd8f99f4da
github.com/tikv/pd/client v0.0.0-20260708075407-4e05b9d2c2d3
github.com/tikv/pd/client v0.0.0-20260720043438-0b37df9a48ed
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67
github.com/twmb/murmur3 v1.1.6
github.com/uber/jaeger-client-go v2.22.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,8 @@ github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/tikv/client-go/v2 v2.0.8-0.20260708122311-01bd8f99f4da h1:Ju9uUKu3M5gPl7Z90e9dcGSe8LtXxGqvSetbPcr6tyc=
github.com/tikv/client-go/v2 v2.0.8-0.20260708122311-01bd8f99f4da/go.mod h1:MRhIujZMMkYcI49Euif4+A3+SHSGwTpyUUadNz48R4g=
github.com/tikv/pd/client v0.0.0-20260708075407-4e05b9d2c2d3 h1:OoBvgoeWmdNEXtS+eOlhysz/OvhA4GS0OdPVhTXteGA=
github.com/tikv/pd/client v0.0.0-20260708075407-4e05b9d2c2d3/go.mod h1:3/Bu91CJONgkDA+Y0v/cnbROSJnu5tQ09vv7JGybUBA=
github.com/tikv/pd/client v0.0.0-20260720043438-0b37df9a48ed h1:BiPn4oLXVqB9ptPNmyo3rh/2IH1dTouXrrnneH86Z3M=
github.com/tikv/pd/client v0.0.0-20260720043438-0b37df9a48ed/go.mod h1:3/Bu91CJONgkDA+Y0v/cnbROSJnu5tQ09vv7JGybUBA=
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67 h1:9LPGD+jzxMlnk5r6+hJnar67cgpDIz/iyD+rfl5r2Vk=
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67/go.mod h1:mkjARE7Yr8qU23YcGMSALbIxTQ9r9QBVahQOBRfU460=
github.com/tjfoc/gmsm v1.3.2/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w=
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,9 @@ type StarterParams struct {
// ManagerAddr is the TiDB manager address used by the shutdown notifier.
// When empty and EnableManagerNotifier is true, the Starter path derives the service address from starter additional params.
ManagerAddr string `toml:"manager-addr" json:"manager-addr,omitempty"`
// EnableRGFallback enables resource group lookup fallback for resource control.
// It is populated from --starter-additional-params and is not file-backed config.
EnableRGFallback bool `toml:"-" json:"-"`
// MaxImportDataSize is the maximum total real source data size allowed for IMPORT INTO.
// Zero means unlimited.
MaxImportDataSize configtypes.ByteSize `toml:"max-import-data-size" json:"max-import-data-size,omitempty"`
Expand Down
14 changes: 13 additions & 1 deletion pkg/domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"optimize_trace.go",
"plan_replayer.go",
"plan_replayer_dump.go",
"resource_group_controller_options.go",
"ru_stats.go",
"runaway.go",
"schema_checker.go",
Expand Down Expand Up @@ -116,6 +117,7 @@ go_library(
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_kvproto//pkg/metapb",
"@com_github_pingcap_kvproto//pkg/pdpb",
"@com_github_pingcap_kvproto//pkg/resource_manager",
"@com_github_pingcap_log//:log",
"@com_github_pingcap_metering_sdk//config",
"@com_github_stretchr_testify//require",
Expand Down Expand Up @@ -149,14 +151,17 @@ go_test(
"plan_replayer_slow_log_test.go",
"plan_replayer_test.go",
"ru_stats_test.go",
"runaway_test.go",
"schema_checker_test.go",
"topn_slow_query_test.go",
],
embed = [":domain"],
flaky = True,
shard_count = 30,
shard_count = 33,
deps = [
"//pkg/config",
"//pkg/config/deploymode",
"//pkg/config/kerneltype",
"//pkg/ddl",
"//pkg/domain/infosync",
"//pkg/domain/serverinfo",
Expand All @@ -174,6 +179,7 @@ go_test(
"//pkg/parser/mysql",
"//pkg/parser/terror",
"//pkg/planner/extstore",
"//pkg/resourcegroup/runaway",
"//pkg/server",
"//pkg/session",
"//pkg/sessionctx/vardef",
Expand All @@ -191,15 +197,21 @@ go_test(
"@com_github_ngaut_pools//:pools",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_kvproto//pkg/kvrpcpb",
"@com_github_pingcap_kvproto//pkg/meta_storagepb",
"@com_github_pingcap_kvproto//pkg/metapb",
"@com_github_pingcap_kvproto//pkg/resource_manager",
"@com_github_prometheus_client_model//go",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//tikvrpc",
"@com_github_tikv_client_go_v2//txnkv/transaction",
"@com_github_tikv_pd_client//:client",
"@com_github_tikv_pd_client//errs",
"@com_github_tikv_pd_client//opt",
"@com_github_tikv_pd_client//resource_group/controller",
"@io_etcd_go_etcd_tests_v3//integration",
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//status",
"@org_uber_go_goleak//:goleak",
],
)
8 changes: 8 additions & 0 deletions pkg/domain/infosync/resource_manager_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ func (*mockResourceManagerClient) LoadResourceGroups(context.Context) ([]*rmpb.R
return nil, 0, nil
}

func (*mockResourceManagerClient) Get(context.Context, []byte, ...opt.MetaStorageOption) (*meta_storagepb.GetResponse, error) {
return &meta_storagepb.GetResponse{Header: &meta_storagepb.ResponseHeader{}}, nil
}

func (*mockResourceManagerClient) Put(context.Context, []byte, []byte, ...opt.MetaStorageOption) (*meta_storagepb.PutResponse, error) {
return &meta_storagepb.PutResponse{Header: &meta_storagepb.ResponseHeader{}}, nil
}

func (m *mockResourceManagerClient) Watch(_ context.Context, key []byte, _ ...opt.MetaStorageOption) (chan *metastorage.WatchResponse, error) {
if bytes.Equal(pd.GroupSettingsPathPrefixBytes(m.keyspaceID), key) {
return m.eventCh, nil
Expand Down
66 changes: 66 additions & 0 deletions pkg/domain/resource_group_controller_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2026 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package domain

import (
"time"

rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/config/deploymode"
"github.com/pingcap/tidb/pkg/resourcegroup/runaway"
rmclient "github.com/tikv/pd/client/resource_group/controller"
)

const (
defaultDegradedRUFillRate = 2_000_000
defaultDegradedRUBurstLimit = 50_000_000_000
defaultDegradedModeWaitTimeout = 3 * time.Second / 2
tokenWaitRetryInterval = 100 * time.Millisecond
tokenWaitRetryTimes = 20
)

func newDefaultDegradedRUSettings() *rmpb.GroupRequestUnitSettings {
return &rmpb.GroupRequestUnitSettings{
RU: &rmpb.TokenBucket{
Settings: &rmpb.TokenLimitSettings{
FillRate: defaultDegradedRUFillRate,
BurstLimit: defaultDegradedRUBurstLimit,
},
},
}
}

func newResourceGroupsControllerOptions() []rmclient.ResourceControlCreateOption {
opts := []rmclient.ResourceControlCreateOption{
rmclient.WithMaxWaitDuration(runaway.MaxWaitDuration),
}
if deploymode.IsStarter() && config.GetGlobalConfig().StarterParams.EnableRGFallback {
opts = append(opts,
// This Starter-only fallback path is a best-effort UX fallback for
// temporary GetResourceGroup failures. It provides a permissive
// group so user requests do not fail immediately; it is not intended
// to define a precise cross-RPC RU limit or response-side accounting
// contract while resource manager is unavailable. Keep synthesis
// inside the controller so degraded groups are not inserted into the
// normal metadata cache.
rmclient.WithDegradedRUSettings(newDefaultDegradedRUSettings()),
Comment thread
ystaticy marked this conversation as resolved.
rmclient.WithDegradedModeWaitDuration(defaultDegradedModeWaitTimeout),
rmclient.WithWaitRetryInterval(tokenWaitRetryInterval),
rmclient.WithWaitRetryTimes(tokenWaitRetryTimes),
)
}
return opts
}
2 changes: 1 addition & 1 deletion pkg/domain/runaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (do *Domain) initResourceGroupsController(ctx context.Context, pdClient pd.
if codec := do.Store().GetCodec(); codec != nil {
keyspaceID = uint32(codec.GetKeyspaceID())
}
control, err := rmclient.NewResourceGroupController(ctx, uniqueID, pdClient, nil, keyspaceID, rmclient.WithMaxWaitDuration(runaway.MaxWaitDuration))
control, err := rmclient.NewResourceGroupController(ctx, uniqueID, pdClient, nil, keyspaceID, newResourceGroupsControllerOptions()...)
if err != nil {
return err
}
Expand Down
Loading
Loading