Skip to content

Commit a5c4b61

Browse files
nolouchti-chi-bot
authored andcommitted
This is an automated cherry-pick of tikv#8352
close tikv#8349 Signed-off-by: ti-chi-bot <[email protected]>
1 parent 233109c commit a5c4b61

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

client/resource_group/controller/config.go

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ const (
5252
defaultTargetPeriod = 5 * time.Second
5353
// defaultMaxWaitDuration is the max duration to wait for the token before throwing error.
5454
defaultMaxWaitDuration = 30 * time.Second
55+
<<<<<<< HEAD
56+
=======
57+
// defaultLTBTokenRPCMaxDelay is the upper bound of backoff delay for local token bucket RPC.
58+
defaultLTBTokenRPCMaxDelay = 1 * time.Second
59+
// defaultWaitRetryTimes is the times to retry when waiting for the token.
60+
defaultWaitRetryTimes = 20
61+
// defaultWaitRetryInterval is the interval to retry when waiting for the token.
62+
defaultWaitRetryInterval = 50 * time.Millisecond
63+
>>>>>>> 6b25787af (resource_control: allow configuration of the maximum retry time for the local bucket (#8352))
5564
)
5665

5766
const (
@@ -73,18 +82,39 @@ const (
7382

7483
// Because the resource manager has not been deployed in microservice mode,
7584
// do not enable this function.
76-
defaultDegradedModeWaitDuration = 0
85+
defaultDegradedModeWaitDuration = time.Duration(0)
7786
defaultAvgBatchProportion = 0.7
7887
)
7988

80-
// Config is the configuration of the resource manager controller which includes some option for client needed.
81-
type Config struct {
89+
// TokenRPCParams is the parameters for local bucket RPC.
90+
type TokenRPCParams struct {
91+
// WaitRetryInterval is the interval to retry when waiting for the token.
92+
WaitRetryInterval Duration `toml:"wait-retry-interval" json:"wait-retry-interval"`
93+
94+
// WaitRetryTimes is the times to retry when waiting for the token.
95+
WaitRetryTimes int `toml:"wait-retry-times" json:"wait-retry-times"`
96+
}
97+
98+
// LocalBucketConfig is the configuration for local bucket. not export to server side.
99+
type LocalBucketConfig struct {
100+
TokenRPCParams `toml:"token-rpc-params" json:"token-rpc-params"`
101+
}
102+
103+
// BaseConfig is the configuration of the resource manager controller which includes some option for client needed.
104+
// TODO: unified the configuration for client and server, server side in pkg/mcs/resourcemanger/config.go.
105+
type BaseConfig struct {
82106
// EnableDegradedMode is to control whether resource control client enable degraded mode when server is disconnect.
83107
DegradedModeWaitDuration Duration `toml:"degraded-mode-wait-duration" json:"degraded-mode-wait-duration"`
84108

85109
// LTBMaxWaitDuration is the max wait time duration for local token bucket.
86110
LTBMaxWaitDuration Duration `toml:"ltb-max-wait-duration" json:"ltb-max-wait-duration"`
87111

112+
<<<<<<< HEAD
113+
=======
114+
// LTBTokenRPCMaxDelay is the upper bound of backoff delay for local token bucket RPC.
115+
LTBTokenRPCMaxDelay Duration `toml:"ltb-token-rpc-max-delay" json:"ltb-token-rpc-max-delay"`
116+
117+
>>>>>>> 6b25787af (resource_control: allow configuration of the maximum retry time for the local bucket (#8352))
88118
// RequestUnit is the configuration determines the coefficients of the RRU and WRU cost.
89119
// This configuration should be modified carefully.
90120
RequestUnit RequestUnitConfig `toml:"request-unit" json:"request-unit"`
@@ -93,13 +123,50 @@ type Config struct {
93123
EnableControllerTraceLog bool `toml:"enable-controller-trace-log" json:"enable-controller-trace-log,string"`
94124
}
95125

126+
// Config is the configuration of the resource manager controller.
127+
type Config struct {
128+
BaseConfig
129+
LocalBucketConfig
130+
}
131+
132+
// Adjust adjusts the configuration.
133+
func (c *Config) Adjust() {
134+
// valid the configuration, TODO: separately add the valid function.
135+
if c.BaseConfig.LTBMaxWaitDuration.Duration == 0 {
136+
c.BaseConfig.LTBMaxWaitDuration = NewDuration(defaultMaxWaitDuration)
137+
}
138+
if c.LocalBucketConfig.WaitRetryInterval.Duration == 0 {
139+
c.LocalBucketConfig.WaitRetryInterval = NewDuration(defaultWaitRetryInterval)
140+
}
141+
// adjust the client settings. calculate the retry times.
142+
if int(c.BaseConfig.LTBTokenRPCMaxDelay.Duration) != int(c.LocalBucketConfig.WaitRetryInterval.Duration)*c.LocalBucketConfig.WaitRetryTimes {
143+
c.LocalBucketConfig.WaitRetryTimes = int(c.BaseConfig.LTBTokenRPCMaxDelay.Duration / c.LocalBucketConfig.WaitRetryInterval.Duration)
144+
}
145+
}
146+
96147
// DefaultConfig returns the default resource manager controller configuration.
97148
func DefaultConfig() *Config {
98149
return &Config{
150+
<<<<<<< HEAD
99151
DegradedModeWaitDuration: NewDuration(defaultDegradedModeWaitDuration),
100152
LTBMaxWaitDuration: NewDuration(defaultMaxWaitDuration),
101153
RequestUnit: DefaultRequestUnitConfig(),
102154
EnableControllerTraceLog: false,
155+
=======
156+
BaseConfig: BaseConfig{
157+
DegradedModeWaitDuration: NewDuration(defaultDegradedModeWaitDuration),
158+
RequestUnit: DefaultRequestUnitConfig(),
159+
EnableControllerTraceLog: false,
160+
LTBMaxWaitDuration: NewDuration(defaultMaxWaitDuration),
161+
LTBTokenRPCMaxDelay: NewDuration(defaultLTBTokenRPCMaxDelay),
162+
},
163+
LocalBucketConfig: LocalBucketConfig{
164+
TokenRPCParams: TokenRPCParams{
165+
WaitRetryInterval: NewDuration(defaultWaitRetryInterval),
166+
WaitRetryTimes: defaultWaitRetryTimes,
167+
},
168+
},
169+
>>>>>>> 6b25787af (resource_control: allow configuration of the maximum retry time for the local bucket (#8352))
103170
}
104171
}
105172

client/resource_group/controller/controller.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func NewResourceGroupController(
172172
log.Info("load resource controller config", zap.Reflect("config", config), zap.Reflect("ru-config", controller.ruConfig))
173173
controller.calculators = []ResourceCalculator{newKVCalculator(controller.ruConfig), newSQLCalculator(controller.ruConfig)}
174174
controller.safeRuConfig.Store(controller.ruConfig)
175+
enableControllerTraceLog.Store(config.EnableControllerTraceLog)
175176
return controller, nil
176177
}
177178

@@ -180,12 +181,17 @@ func loadServerConfig(ctx context.Context, provider ResourceGroupProvider) (*Con
180181
if err != nil {
181182
return nil, err
182183
}
184+
config := DefaultConfig()
185+
defer config.Adjust()
183186
kvs := resp.GetKvs()
184187
if len(kvs) == 0 {
185188
log.Warn("[resource group controller] server does not save config, load config failed")
186-
return DefaultConfig(), nil
189+
return config, nil
187190
}
191+
<<<<<<< HEAD
188192
config := &Config{}
193+
=======
194+
>>>>>>> 6b25787af (resource_control: allow configuration of the maximum retry time for the local bucket (#8352))
189195
err = json.Unmarshal(kvs[0].GetValue(), config)
190196
if err != nil {
191197
return nil, err
@@ -288,7 +294,6 @@ func (c *ResourceGroupsController) Start(ctx context.Context) {
288294
watchRetryTimer.Reset(watchRetryInterval)
289295
}
290296
}
291-
292297
case <-emergencyTokenAcquisitionTicker.C:
293298
c.executeOnAllGroups((*groupCostController).resetEmergencyTokenAcquisition)
294299
/* channels */
@@ -370,6 +375,7 @@ func (c *ResourceGroupsController) Start(ctx context.Context) {
370375
if err := json.Unmarshal(item.Kv.Value, config); err != nil {
371376
continue
372377
}
378+
config.Adjust()
373379
c.ruConfig = GenerateRUConfig(config)
374380

375381
// Stay compatible with serverless
@@ -383,7 +389,6 @@ func (c *ResourceGroupsController) Start(ctx context.Context) {
383389
}
384390
log.Info("load resource controller config after config changed", zap.Reflect("config", config), zap.Reflect("ruConfig", c.ruConfig))
385391
}
386-
387392
case gc := <-c.tokenBucketUpdateChan:
388393
now := gc.run.now
389394
go gc.handleTokenBucketUpdateEvent(c.loopCtx, now)

pkg/mcs/resourcemanager/server/config.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const (
5959
defaultDegradedModeWaitDuration = time.Second * 0
6060
// defaultMaxWaitDuration is the max duration to wait for the token before throwing error.
6161
defaultMaxWaitDuration = 30 * time.Second
62+
// defaultLTBTokenRPCMaxDelay is the upper bound of backoff delay for local token bucket RPC.
63+
defaultLTBTokenRPCMaxDelay = 1 * time.Second
6264
)
6365

6466
// Config is the configuration for the resource manager.
@@ -99,6 +101,9 @@ type ControllerConfig struct {
99101
// LTBMaxWaitDuration is the max wait time duration for local token bucket.
100102
LTBMaxWaitDuration typeutil.Duration `toml:"ltb-max-wait-duration" json:"ltb-max-wait-duration"`
101103

104+
// LTBTokenRPCMaxDelay is the upper bound of backoff delay for local token bucket RPC.
105+
LTBTokenRPCMaxDelay typeutil.Duration `toml:"ltb-token-rpc-max-delay" json:"ltb-token-rpc-max-delay"`
106+
102107
// RequestUnit is the configuration determines the coefficients of the RRU and WRU cost.
103108
// This configuration should be modified carefully.
104109
RequestUnit RequestUnitConfig `toml:"request-unit" json:"request-unit"`
@@ -112,10 +117,23 @@ func (rmc *ControllerConfig) Adjust(meta *configutil.ConfigMetaData) {
112117
if rmc == nil {
113118
return
114119
}
120+
<<<<<<< HEAD
115121
rmc.RequestUnit.Adjust()
116122

117123
configutil.AdjustDuration(&rmc.DegradedModeWaitDuration, defaultDegradedModeWaitDuration)
118124
configutil.AdjustDuration(&rmc.LTBMaxWaitDuration, defaultMaxWaitDuration)
125+
=======
126+
rmc.RequestUnit.Adjust(meta.Child("request-unit"))
127+
if !meta.IsDefined("degraded-mode-wait-duration") {
128+
configutil.AdjustDuration(&rmc.DegradedModeWaitDuration, defaultDegradedModeWaitDuration)
129+
}
130+
if !meta.IsDefined("ltb-max-wait-duration") {
131+
configutil.AdjustDuration(&rmc.LTBMaxWaitDuration, defaultMaxWaitDuration)
132+
}
133+
if !meta.IsDefined("ltb-token-rpc-max-delay") {
134+
configutil.AdjustDuration(&rmc.LTBTokenRPCMaxDelay, defaultLTBTokenRPCMaxDelay)
135+
}
136+
>>>>>>> 6b25787af (resource_control: allow configuration of the maximum retry time for the local bucket (#8352))
119137
failpoint.Inject("enableDegradedMode", func() {
120138
configutil.AdjustDuration(&rmc.DegradedModeWaitDuration, time.Second)
121139
})

pkg/mcs/resourcemanager/server/config_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestControllerConfig(t *testing.T) {
2828
cfgData := `
2929
[controller]
3030
ltb-max-wait-duration = "60s"
31+
ltb-token-rpc-max-delay = "500ms"
3132
degraded-mode-wait-duration = "2s"
3233
[controller.request-unit]
3334
read-base-cost = 1.0
@@ -42,8 +43,14 @@ read-cpu-ms-cost = 5.0
4243
err = cfg.Adjust(&meta, false)
4344
re.NoError(err)
4445

46+
<<<<<<< HEAD
4547
re.Equal(cfg.Controller.DegradedModeWaitDuration.Duration, time.Second*2)
4648
re.Equal(cfg.Controller.LTBMaxWaitDuration.Duration, time.Second*60)
49+
=======
50+
re.Equal(2*time.Second, cfg.Controller.DegradedModeWaitDuration.Duration)
51+
re.Equal(60*time.Second, cfg.Controller.LTBMaxWaitDuration.Duration)
52+
re.Equal(500*time.Millisecond, cfg.Controller.LTBTokenRPCMaxDelay.Duration)
53+
>>>>>>> 6b25787af (resource_control: allow configuration of the maximum retry time for the local bucket (#8352))
4754
re.LessOrEqual(math.Abs(cfg.Controller.RequestUnit.CPUMsCost-5), 1e-7)
4855
re.LessOrEqual(math.Abs(cfg.Controller.RequestUnit.WriteCostPerByte-4), 1e-7)
4956
re.LessOrEqual(math.Abs(cfg.Controller.RequestUnit.WriteBaseCost-3), 1e-7)

tests/integrations/mcs/resourcemanager/resource_manager_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,10 +1362,22 @@ func (suite *resourceManagerClientTestSuite) TestResourceGroupControllerConfigCh
13621362

13631363
configURL := "/resource-manager/api/v1/config/controller"
13641364
waitDuration := 10 * time.Second
1365+
tokenRPCMaxDelay := 2 * time.Second
13651366
readBaseCost := 1.5
13661367
defaultCfg := controller.DefaultConfig()
1368+
<<<<<<< HEAD
13671369
// failpoint enableDegradedMode will setup and set it be 1s.
13681370
defaultCfg.DegradedModeWaitDuration.Duration = time.Second
1371+
=======
1372+
expectCfg := server.ControllerConfig{
1373+
// failpoint enableDegradedMode will setup and set it be 1s.
1374+
DegradedModeWaitDuration: typeutil.NewDuration(time.Second),
1375+
LTBMaxWaitDuration: typeutil.Duration(defaultCfg.LTBMaxWaitDuration),
1376+
LTBTokenRPCMaxDelay: typeutil.Duration(defaultCfg.LTBTokenRPCMaxDelay),
1377+
RequestUnit: server.RequestUnitConfig(defaultCfg.RequestUnit),
1378+
EnableControllerTraceLog: defaultCfg.EnableControllerTraceLog,
1379+
}
1380+
>>>>>>> 6b25787af (resource_control: allow configuration of the maximum retry time for the local bucket (#8352))
13691381
expectRUCfg := controller.GenerateRUConfig(defaultCfg)
13701382
// initial config verification
13711383
respString := sendRequest("GET", getAddr()+configURL, nil)
@@ -1384,6 +1396,13 @@ func (suite *resourceManagerClientTestSuite) TestResourceGroupControllerConfigCh
13841396
value: waitDuration,
13851397
expected: func(ruConfig *controller.RUConfig) { ruConfig.DegradedModeWaitDuration = waitDuration },
13861398
},
1399+
{
1400+
configJSON: fmt.Sprintf(`{"ltb-token-rpc-max-delay": "%v"}`, tokenRPCMaxDelay),
1401+
value: waitDuration,
1402+
expected: func(ruConfig *controller.RUConfig) {
1403+
ruConfig.WaitRetryTimes = int(tokenRPCMaxDelay / ruConfig.WaitRetryInterval)
1404+
},
1405+
},
13871406
{
13881407
configJSON: fmt.Sprintf(`{"ltb-max-wait-duration": "%v"}`, waitDuration),
13891408
value: waitDuration,

0 commit comments

Comments
 (0)