From d072adbcd029386efbc6748f0a81a9f8e96ca74f Mon Sep 17 00:00:00 2001 From: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:35:13 +0200 Subject: [PATCH] Throttler: refactor global configuration setting as throttler member (#14853) Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> --- .../vttablet/tabletserver/throttle/config/config.go | 9 +++------ go/vt/vttablet/tabletserver/throttle/throttler.go | 11 ++++++----- .../tabletserver/throttle/throttler_test.go | 13 ++++++++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/go/vt/vttablet/tabletserver/throttle/config/config.go b/go/vt/vttablet/tabletserver/throttle/config/config.go index 5241d686d10..f6234955cc4 100644 --- a/go/vt/vttablet/tabletserver/throttle/config/config.go +++ b/go/vt/vttablet/tabletserver/throttle/config/config.go @@ -41,12 +41,9 @@ limitations under the License. package config -// Instance is the one configuration for the throttler -var Instance = &ConfigurationSettings{} - -// Settings returns the settings of the global instance of Configuration -func Settings() *ConfigurationSettings { - return Instance +// NewConfigurationSettings creates new throttler configuration settings. +func NewConfigurationSettings() *ConfigurationSettings { + return &ConfigurationSettings{} } // ConfigurationSettings models a set of configurable values, that can be diff --git a/go/vt/vttablet/tabletserver/throttle/throttler.go b/go/vt/vttablet/tabletserver/throttle/throttler.go index d7739b52703..9797c820610 100644 --- a/go/vt/vttablet/tabletserver/throttle/throttler.go +++ b/go/vt/vttablet/tabletserver/throttle/throttler.go @@ -159,6 +159,7 @@ type Throttler struct { mysqlAggregateInterval time.Duration throttledAppsSnapshotInterval time.Duration + configSettings *config.ConfigurationSettings env tabletenv.Env pool *connpool.Pool tabletTypeFunc func() topodatapb.TabletType @@ -308,7 +309,7 @@ func (throttler *Throttler) GetMetricsThreshold() float64 { func (throttler *Throttler) initConfig() { log.Infof("Throttler: initializing config") - config.Instance = &config.ConfigurationSettings{ + throttler.configSettings = &config.ConfigurationSettings{ Stores: config.StoresSettings{ MySQL: config.MySQLConfigurationSettings{ IgnoreDialTCPErrors: true, @@ -316,12 +317,12 @@ func (throttler *Throttler) initConfig() { }, }, } - config.Instance.Stores.MySQL.Clusters[selfStoreName] = &config.MySQLClusterConfigurationSettings{ + throttler.configSettings.Stores.MySQL.Clusters[selfStoreName] = &config.MySQLClusterConfigurationSettings{ MetricQuery: throttler.GetMetricsQuery(), ThrottleThreshold: &throttler.MetricsThreshold, IgnoreHostsCount: 0, } - config.Instance.Stores.MySQL.Clusters[shardStoreName] = &config.MySQLClusterConfigurationSettings{ + throttler.configSettings.Stores.MySQL.Clusters[shardStoreName] = &config.MySQLClusterConfigurationSettings{ MetricQuery: throttler.GetMetricsQuery(), ThrottleThreshold: &throttler.MetricsThreshold, IgnoreHostsCount: 0, @@ -857,7 +858,7 @@ func (throttler *Throttler) refreshMySQLInventory(ctx context.Context) error { } } - for clusterName, clusterSettings := range config.Settings().Stores.MySQL.Clusters { + for clusterName, clusterSettings := range throttler.configSettings.Stores.MySQL.Clusters { clusterName := clusterName clusterSettings.MetricQuery = metricsQuery clusterSettings.ThrottleThreshold.Store(metricsThreshold) @@ -931,7 +932,7 @@ func (throttler *Throttler) aggregateMySQLMetrics(ctx context.Context) error { metricName := fmt.Sprintf("mysql/%s", clusterName) ignoreHostsCount := throttler.mysqlInventory.IgnoreHostsCount[clusterName] ignoreHostsThreshold := throttler.mysqlInventory.IgnoreHostsThreshold[clusterName] - aggregatedMetric := aggregateMySQLProbes(ctx, probes, clusterName, throttler.mysqlInventory.TabletMetrics, ignoreHostsCount, config.Settings().Stores.MySQL.IgnoreDialTCPErrors, ignoreHostsThreshold) + aggregatedMetric := aggregateMySQLProbes(ctx, probes, clusterName, throttler.mysqlInventory.TabletMetrics, ignoreHostsCount, throttler.configSettings.Stores.MySQL.IgnoreDialTCPErrors, ignoreHostsThreshold) throttler.aggregatedMetrics.Set(metricName, aggregatedMetric, cache.DefaultExpiration) } return nil diff --git a/go/vt/vttablet/tabletserver/throttle/throttler_test.go b/go/vt/vttablet/tabletserver/throttle/throttler_test.go index f0d895e1413..9de15f18ca4 100644 --- a/go/vt/vttablet/tabletserver/throttle/throttler_test.go +++ b/go/vt/vttablet/tabletserver/throttle/throttler_test.go @@ -100,12 +100,12 @@ func (w FakeHeartbeatWriter) RequestHeartbeats() { func newTestThrottler() *Throttler { metricsQuery := "select 1" - config.Settings().Stores.MySQL.Clusters = map[string]*config.MySQLClusterConfigurationSettings{ + configSettings := config.NewConfigurationSettings() + configSettings.Stores.MySQL.Clusters = map[string]*config.MySQLClusterConfigurationSettings{ selfStoreName: {}, shardStoreName: {}, } - clusters := config.Settings().Stores.MySQL.Clusters - for _, s := range clusters { + for _, s := range configSettings.Stores.MySQL.Clusters { s.MetricQuery = metricsQuery s.ThrottleThreshold = &atomic.Uint64{} s.ThrottleThreshold.Store(1) @@ -121,6 +121,7 @@ func newTestThrottler() *Throttler { tabletTypeFunc: func() topodatapb.TabletType { return topodatapb.TabletType_PRIMARY }, overrideTmClient: &fakeTMClient{}, } + throttler.configSettings = configSettings throttler.mysqlThrottleMetricChan = make(chan *mysql.MySQLThrottleMetric) throttler.mysqlInventoryChan = make(chan *mysql.Inventory, 1) throttler.mysqlClusterProbesChan = make(chan *mysql.ClusterProbes) @@ -222,17 +223,18 @@ func TestIsAppExempted(t *testing.T) { // `PRIMARY` tablet, probes other tablets). On the leader, the list is expected to be non-empty. func TestRefreshMySQLInventory(t *testing.T) { metricsQuery := "select 1" - config.Settings().Stores.MySQL.Clusters = map[string]*config.MySQLClusterConfigurationSettings{ + configSettings := config.NewConfigurationSettings() + clusters := map[string]*config.MySQLClusterConfigurationSettings{ selfStoreName: {}, "ks1": {}, "ks2": {}, } - clusters := config.Settings().Stores.MySQL.Clusters for _, s := range clusters { s.MetricQuery = metricsQuery s.ThrottleThreshold = &atomic.Uint64{} s.ThrottleThreshold.Store(1) } + configSettings.Stores.MySQL.Clusters = clusters throttler := &Throttler{ mysqlClusterProbesChan: make(chan *mysql.ClusterProbes), @@ -240,6 +242,7 @@ func TestRefreshMySQLInventory(t *testing.T) { ts: &FakeTopoServer{}, mysqlInventory: mysql.NewInventory(), } + throttler.configSettings = configSettings throttler.metricsQuery.Store(metricsQuery) throttler.initThrottleTabletTypes()