Skip to content

Commit

Permalink
Throttler: refactor global configuration setting as throttler member (#…
Browse files Browse the repository at this point in the history
…14853)

Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach authored Dec 26, 2023
1 parent d807985 commit d072adb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
9 changes: 3 additions & 6 deletions go/vt/vttablet/tabletserver/throttle/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions go/vt/vttablet/tabletserver/throttle/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -308,20 +309,20 @@ 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,
Clusters: map[string](*config.MySQLClusterConfigurationSettings){},
},
},
}
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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions go/vt/vttablet/tabletserver/throttle/throttler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -222,24 +223,26 @@ 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),
mysqlClusterThresholds: cache.New(cache.NoExpiration, 0),
ts: &FakeTopoServer{},
mysqlInventory: mysql.NewInventory(),
}
throttler.configSettings = configSettings
throttler.metricsQuery.Store(metricsQuery)
throttler.initThrottleTabletTypes()

Expand Down

0 comments on commit d072adb

Please sign in to comment.