diff --git a/client/resource_group/controller/config.go b/client/resource_group/controller/config.go index cbf0be54c8d..2095bc60601 100644 --- a/client/resource_group/controller/config.go +++ b/client/resource_group/controller/config.go @@ -77,8 +77,8 @@ const ( defaultAvgBatchProportion = 0.7 ) -// ControllerConfig is the configuration of the resource manager controller which includes some option for client needed. -type ControllerConfig struct { +// Config is the configuration of the resource manager controller which includes some option for client needed. +type Config struct { // EnableDegradedMode is to control whether resource control client enable degraded mode when server is disconnect. DegradedModeWaitDuration string `toml:"degraded-mode-wait-duration" json:"degraded-mode-wait-duration"` @@ -87,9 +87,9 @@ type ControllerConfig struct { RequestUnit RequestUnitConfig `toml:"request-unit" json:"request-unit"` } -// DefaultControllerConfig returns the default resource manager controller configuration. -func DefaultControllerConfig() *ControllerConfig { - return &ControllerConfig{ +// DefaultConfig returns the default resource manager controller configuration. +func DefaultConfig() *Config { + return &Config{ DegradedModeWaitDuration: defaultDegradedModeWaitDuration, RequestUnit: DefaultRequestUnitConfig(), } @@ -130,10 +130,10 @@ func DefaultRequestUnitConfig() RequestUnitConfig { } } -// Config is the configuration of the resource units, which gives the read/write request +// RUConfig is the configuration of the resource units, which gives the read/write request // units or request resource cost standards. It should be calculated by a given `RequestUnitConfig` // or `RequestResourceConfig`. -type Config struct { +type RUConfig struct { // RU model config ReadBaseCost RequestUnit ReadPerBatchBaseCost RequestUnit @@ -148,16 +148,16 @@ type Config struct { DegradedModeWaitDuration time.Duration } -// DefaultConfig returns the default configuration. -func DefaultConfig() *Config { - return GenerateConfig( - DefaultControllerConfig(), +// DefaultRUConfig returns the default configuration. +func DefaultRUConfig() *RUConfig { + return GenerateRUConfig( + DefaultConfig(), ) } -// GenerateConfig generates the configuration by the given request unit configuration. -func GenerateConfig(config *ControllerConfig) *Config { - cfg := &Config{ +// GenerateRUConfig generates the configuration by the given request unit configuration. +func GenerateRUConfig(config *Config) *RUConfig { + cfg := &RUConfig{ ReadBaseCost: RequestUnit(config.RequestUnit.ReadBaseCost), ReadPerBatchBaseCost: RequestUnit(config.RequestUnit.ReadPerBatchBaseCost), ReadBytesCost: RequestUnit(config.RequestUnit.ReadCostPerByte), diff --git a/client/resource_group/controller/controller.go b/client/resource_group/controller/controller.go index 460a148e510..7eb985af1ba 100755 --- a/client/resource_group/controller/controller.go +++ b/client/resource_group/controller/controller.go @@ -78,25 +78,25 @@ type ResourceControlCreateOption func(controller *ResourceGroupsController) // EnableSingleGroupByKeyspace is the option to enable single group by keyspace feature. func EnableSingleGroupByKeyspace() ResourceControlCreateOption { return func(controller *ResourceGroupsController) { - controller.config.isSingleGroupByKeyspace = true + controller.ruConfig.isSingleGroupByKeyspace = true } } // WithMaxWaitDuration is the option to set the max wait duration for acquiring token buckets. func WithMaxWaitDuration(d time.Duration) ResourceControlCreateOption { return func(controller *ResourceGroupsController) { - controller.config.maxWaitDuration = d + controller.ruConfig.maxWaitDuration = d } } var _ ResourceGroupKVInterceptor = (*ResourceGroupsController)(nil) -// ResourceGroupsController impls ResourceGroupKVInterceptor. +// ResourceGroupsController implements ResourceGroupKVInterceptor. type ResourceGroupsController struct { clientUniqueID uint64 provider ResourceGroupProvider groupsController sync.Map - config *Config + ruConfig *RUConfig loopCtx context.Context loopCancel func() @@ -128,19 +128,19 @@ func NewResourceGroupController( requestUnitConfig *RequestUnitConfig, opts ...ResourceControlCreateOption, ) (*ResourceGroupsController, error) { - controllerConfig, err := loadServerConfig(ctx, provider) + config, err := loadServerConfig(ctx, provider) if err != nil { return nil, err } if requestUnitConfig != nil { - controllerConfig.RequestUnit = *requestUnitConfig + config.RequestUnit = *requestUnitConfig } - log.Info("load resource controller config", zap.Reflect("config", controllerConfig)) - config := GenerateConfig(controllerConfig) + log.Info("load resource controller config", zap.Reflect("config", config)) + ruConfig := GenerateRUConfig(config) controller := &ResourceGroupsController{ clientUniqueID: clientUniqueID, provider: provider, - config: config, + ruConfig: ruConfig, lowTokenNotifyChan: make(chan struct{}, 1), tokenResponseChan: make(chan []*rmpb.TokenBucketResponse, 1), tokenBucketUpdateChan: make(chan *groupCostController, maxNotificationChanLen), @@ -148,30 +148,30 @@ func NewResourceGroupController( for _, opt := range opts { opt(controller) } - controller.calculators = []ResourceCalculator{newKVCalculator(controller.config), newSQLCalculator(controller.config)} + controller.calculators = []ResourceCalculator{newKVCalculator(controller.ruConfig), newSQLCalculator(controller.ruConfig)} return controller, nil } -func loadServerConfig(ctx context.Context, provider ResourceGroupProvider) (*ControllerConfig, error) { +func loadServerConfig(ctx context.Context, provider ResourceGroupProvider) (*Config, error) { items, _, err := provider.LoadGlobalConfig(ctx, nil, controllerConfigPath) if err != nil { return nil, err } if len(items) == 0 { log.Warn("[resource group controller] server does not save config, load config failed") - return DefaultControllerConfig(), nil + return DefaultConfig(), nil } - controllerConfig := &ControllerConfig{} - err = json.Unmarshal(items[0].PayLoad, controllerConfig) + config := &Config{} + err = json.Unmarshal(items[0].PayLoad, config) if err != nil { return nil, err } - return controllerConfig, nil + return config, nil } // GetConfig returns the config of controller. It's only used for test. -func (c *ResourceGroupsController) GetConfig() *Config { - return c.config +func (c *ResourceGroupsController) GetConfig() *RUConfig { + return c.ruConfig } // Source List @@ -184,8 +184,8 @@ const ( func (c *ResourceGroupsController) Start(ctx context.Context) { c.loopCtx, c.loopCancel = context.WithCancel(ctx) go func() { - if c.config.DegradedModeWaitDuration > 0 { - c.run.responseDeadline = time.NewTimer(c.config.DegradedModeWaitDuration) + if c.ruConfig.DegradedModeWaitDuration > 0 { + c.run.responseDeadline = time.NewTimer(c.ruConfig.DegradedModeWaitDuration) c.run.responseDeadline.Stop() defer c.run.responseDeadline.Stop() } @@ -214,11 +214,11 @@ func (c *ResourceGroupsController) Start(ctx context.Context) { log.Warn("load resource group revision failed", zap.Error(err)) } var watchChannel chan []*meta_storagepb.Event - if !c.config.isSingleGroupByKeyspace { + if !c.ruConfig.isSingleGroupByKeyspace { watchChannel, err = c.provider.Watch(ctx, pd.GroupSettingsPathPrefixBytes, pd.WithRev(revision), pd.WithPrefix()) } watchRetryTimer := time.NewTimer(watchRetryInterval) - if err == nil || c.config.isSingleGroupByKeyspace { + if err == nil || c.ruConfig.isSingleGroupByKeyspace { watchRetryTimer.Stop() } defer watchRetryTimer.Stop() @@ -259,7 +259,7 @@ func (c *ResourceGroupsController) Start(ctx context.Context) { c.executeOnAllGroups((*groupCostController).resetEmergencyTokenAcquisition) case resp, ok := <-watchChannel: failpoint.Inject("disableWatch", func() { - if c.config.isSingleGroupByKeyspace { + if c.ruConfig.isSingleGroupByKeyspace { panic("disableWatch") } }) @@ -335,7 +335,7 @@ func (c *ResourceGroupsController) tryGetResourceGroup(ctx context.Context, name return gc, nil } // Initialize the resource group controller. - gc, err := newGroupCostController(group, c.config, c.lowTokenNotifyChan, c.tokenBucketUpdateChan) + gc, err := newGroupCostController(group, c.ruConfig, c.lowTokenNotifyChan, c.tokenBucketUpdateChan) if err != nil { return nil, err } @@ -425,8 +425,8 @@ func (c *ResourceGroupsController) sendTokenBucketRequests(ctx context.Context, TargetRequestPeriodMs: uint64(defaultTargetPeriod / time.Millisecond), ClientUniqueId: c.clientUniqueID, } - if c.config.DegradedModeWaitDuration > 0 && c.responseDeadlineCh == nil { - c.run.responseDeadline.Reset(c.config.DegradedModeWaitDuration) + if c.ruConfig.DegradedModeWaitDuration > 0 && c.responseDeadlineCh == nil { + c.run.responseDeadline.Reset(c.ruConfig.DegradedModeWaitDuration) c.responseDeadlineCh = c.run.responseDeadline.C } go func() { @@ -485,7 +485,7 @@ type groupCostController struct { // invariant attributes name string mode rmpb.GroupMode - mainCfg *Config + mainCfg *RUConfig // meta info meta *rmpb.ResourceGroup metaLock sync.RWMutex @@ -574,7 +574,7 @@ type tokenCounter struct { func newGroupCostController( group *rmpb.ResourceGroup, - mainCfg *Config, + mainCfg *RUConfig, lowRUNotifyChan chan struct{}, tokenBucketUpdateChan chan *groupCostController, ) (*groupCostController, error) { diff --git a/client/resource_group/controller/controller_test.go b/client/resource_group/controller/controller_test.go index d3b2a29c211..165d501ddb1 100644 --- a/client/resource_group/controller/controller_test.go +++ b/client/resource_group/controller/controller_test.go @@ -42,7 +42,7 @@ func createTestGroupCostController(re *require.Assertions) *groupCostController } ch1 := make(chan struct{}) ch2 := make(chan *groupCostController) - gc, err := newGroupCostController(group, DefaultConfig(), ch1, ch2) + gc, err := newGroupCostController(group, DefaultRUConfig(), ch1, ch2) re.NoError(err) return gc } diff --git a/client/resource_group/controller/model.go b/client/resource_group/controller/model.go index 55a6f9ec939..81f7e52716e 100644 --- a/client/resource_group/controller/model.go +++ b/client/resource_group/controller/model.go @@ -65,13 +65,13 @@ type ResourceCalculator interface { // KVCalculator is used to calculate the KV-side consumption. type KVCalculator struct { - *Config + *RUConfig } var _ ResourceCalculator = (*KVCalculator)(nil) -func newKVCalculator(cfg *Config) *KVCalculator { - return &KVCalculator{Config: cfg} +func newKVCalculator(cfg *RUConfig) *KVCalculator { + return &KVCalculator{RUConfig: cfg} } // Trickle ... @@ -146,13 +146,13 @@ func (kc *KVCalculator) payBackWriteCost(consumption *rmpb.Consumption, req Requ // SQLCalculator is used to calculate the SQL-side consumption. type SQLCalculator struct { - *Config + *RUConfig } var _ ResourceCalculator = (*SQLCalculator)(nil) -func newSQLCalculator(cfg *Config) *SQLCalculator { - return &SQLCalculator{Config: cfg} +func newSQLCalculator(cfg *RUConfig) *SQLCalculator { + return &SQLCalculator{RUConfig: cfg} } // Trickle update sql layer CPU consumption. diff --git a/tests/integrations/mcs/resourcemanager/resource_manager_test.go b/tests/integrations/mcs/resourcemanager/resource_manager_test.go index 4bdefefbb81..d393dfe03ad 100644 --- a/tests/integrations/mcs/resourcemanager/resource_manager_test.go +++ b/tests/integrations/mcs/resourcemanager/resource_manager_test.go @@ -1081,7 +1081,7 @@ func (suite *resourceManagerClientTestSuite) TestLoadRequestUnitConfig() { re.NoError(err) config := ctr.GetConfig() re.NotNil(config) - expectedConfig := controller.DefaultConfig() + expectedConfig := controller.DefaultRUConfig() re.Equal(expectedConfig.ReadBaseCost, config.ReadBaseCost) re.Equal(expectedConfig.ReadBytesCost, config.ReadBytesCost) re.Equal(expectedConfig.WriteBaseCost, config.WriteBaseCost) @@ -1099,9 +1099,9 @@ func (suite *resourceManagerClientTestSuite) TestLoadRequestUnitConfig() { re.NoError(err) config = ctr.GetConfig() re.NotNil(config) - controllerConfig := controller.DefaultControllerConfig() + controllerConfig := controller.DefaultConfig() controllerConfig.RequestUnit = *ruConfig - expectedConfig = controller.GenerateConfig(controllerConfig) + expectedConfig = controller.GenerateRUConfig(controllerConfig) re.Equal(expectedConfig.ReadBaseCost, config.ReadBaseCost) re.Equal(expectedConfig.ReadBytesCost, config.ReadBytesCost) re.Equal(expectedConfig.WriteBaseCost, config.WriteBaseCost)