From a0dcc5163da546ac34867d3ae05b8ead9bab5ff3 Mon Sep 17 00:00:00 2001 From: Karol Kokoszka Date: Thu, 2 May 2024 14:16:01 +0200 Subject: [PATCH] feat(config): make scylla configs cache update freq configurable --- pkg/cmd/scylla-manager/server.go | 3 ++- pkg/config/server/server.go | 3 +++ pkg/config/server/server_test.go | 4 ++++ pkg/service/configcache/config.go | 17 +++++++++++++++++ pkg/service/configcache/service.go | 19 +++++-------------- pkg/service/configcache/service_test.go | 2 ++ .../healthcheck/service_integration_test.go | 5 +++-- 7 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 pkg/service/configcache/config.go diff --git a/pkg/cmd/scylla-manager/server.go b/pkg/cmd/scylla-manager/server.go index f2589d638d..8c9a40c228 100644 --- a/pkg/cmd/scylla-manager/server.go +++ b/pkg/cmd/scylla-manager/server.go @@ -80,7 +80,8 @@ func (s *server) makeServices(ctx context.Context) error { } s.clusterSvc.SetOnChangeListener(s.onClusterChange) - s.configCacheSvc = configcache.NewService(s.clusterSvc, s.clusterSvc.CreateClientNoCache, secretsStore, s.logger) + s.configCacheSvc = configcache.NewService(s.config.ConfigCache, s.clusterSvc, s.clusterSvc.CreateClientNoCache, + secretsStore, s.logger) s.initConfigCacheSvc(ctx) s.healthSvc, err = healthcheck.NewService( diff --git a/pkg/config/server/server.go b/pkg/config/server/server.go index 0f39b3bf34..72d4a4358a 100644 --- a/pkg/config/server/server.go +++ b/pkg/config/server/server.go @@ -7,6 +7,7 @@ import ( "time" "github.com/pkg/errors" + "github.com/scylladb/scylla-manager/v3/pkg/service/configcache" "github.com/scylladb/scylla-manager/v3/pkg/service/restore" "github.com/scylladb/scylla-manager/v3/pkg/config" @@ -59,6 +60,7 @@ type Config struct { Database DBConfig `yaml:"database"` SSL SSLConfig `yaml:"ssl"` Healthcheck healthcheck.Config `yaml:"healthcheck"` + ConfigCache configcache.Config `yaml:"config_cache"` Backup backup.Config `yaml:"backup"` Restore restore.Config `yaml:"restore"` Repair repair.Config `yaml:"repair"` @@ -90,6 +92,7 @@ func DefaultConfig() Config { Restore: restore.DefaultConfig(), Repair: repair.DefaultConfig(), TimeoutConfig: scyllaclient.DefaultTimeoutConfig(), + ConfigCache: configcache.DefaultConfig(), } } diff --git a/pkg/config/server/server_test.go b/pkg/config/server/server_test.go index 889d27264d..8f0ae43e66 100644 --- a/pkg/config/server/server_test.go +++ b/pkg/config/server/server_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/scylladb/go-log" + "github.com/scylladb/scylla-manager/v3/pkg/service/configcache" "github.com/scylladb/scylla-manager/v3/pkg/service/restore" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -110,6 +111,9 @@ func TestConfigModification(t *testing.T) { }, PoolDecayDuration: time.Hour, }, + ConfigCache: configcache.Config{ + UpdateFrequency: 5 * time.Minute, + }, } if diff := cmp.Diff(c, golden, configCmpOpts); diff != "" { diff --git a/pkg/service/configcache/config.go b/pkg/service/configcache/config.go new file mode 100644 index 0000000000..0594008def --- /dev/null +++ b/pkg/service/configcache/config.go @@ -0,0 +1,17 @@ +// Copyright (C) 2024 ScyllaDB + +package configcache + +import "time" + +// Config specifies the cache service configuration. +type Config struct { + // UpdateFrequency specifies how often to call Scylla for its configuration. + UpdateFrequency time.Duration `yaml:"update_frequency"` +} + +func DefaultConfig() Config { + return Config{ + UpdateFrequency: 5 * time.Minute, + } +} diff --git a/pkg/service/configcache/service.go b/pkg/service/configcache/service.go index 1de6888342..a58e420041 100644 --- a/pkg/service/configcache/service.go +++ b/pkg/service/configcache/service.go @@ -15,18 +15,6 @@ import ( "github.com/scylladb/scylla-manager/v3/pkg/util/uuid" ) -// ConnectionType defines an enum for different types of configuration. -type ConnectionType int - -const ( - // CQL defines cql connection type. - CQL ConnectionType = iota - // Alternator defines alternator connection type. - Alternator - - updateFrequency = 5 * time.Minute -) - // ConfigCacher is the interface defining the cache behavior. type ConfigCacher interface { // Read returns either the host configuration that is currently stored in the cache, @@ -51,6 +39,8 @@ type ConfigCacher interface { // Use svc.Read(clusterID, host) to read the configuration of particular host in given cluster. // Use svc.Run() to let the cache update itself periodically with the current configuration. type Service struct { + svcConfig Config + clusterSvc cluster.Servicer scyllaClient scyllaclient.ProviderFunc secretsStore store.Store @@ -60,8 +50,9 @@ type Service struct { } // NewService is the constructor for the cluster config cache service. -func NewService(clusterSvc cluster.Servicer, client scyllaclient.ProviderFunc, secretsStore store.Store, logger log.Logger) ConfigCacher { +func NewService(config Config, clusterSvc cluster.Servicer, client scyllaclient.ProviderFunc, secretsStore store.Store, logger log.Logger) ConfigCacher { return &Service{ + svcConfig: config, clusterSvc: clusterSvc, scyllaClient: client, secretsStore: secretsStore, @@ -96,7 +87,7 @@ func (svc *Service) Read(clusterID uuid.UUID, host string) (NodeConfig, error) { // Run starts the infinity loop responsible for updating the clusters configuration periodically. func (svc *Service) Run(ctx context.Context) { - freq := time.NewTicker(updateFrequency) + freq := time.NewTicker(svc.svcConfig.UpdateFrequency) for { // make sure to shut down when the context is cancelled diff --git a/pkg/service/configcache/service_test.go b/pkg/service/configcache/service_test.go index eb416f6d55..ee7cf858f2 100644 --- a/pkg/service/configcache/service_test.go +++ b/pkg/service/configcache/service_test.go @@ -82,6 +82,7 @@ func TestService_Read(t *testing.T) { t.Run(tc.name, func(t *testing.T) { // Given svc := Service{ + svcConfig: DefaultConfig(), clusterSvc: &mockClusterServicer{}, scyllaClient: mockProviderFunc, secretsStore: &mockStore{}, @@ -109,6 +110,7 @@ func TestService_Read(t *testing.T) { func TestService_Run(t *testing.T) { t.Run("validate context cancellation handling", func(t *testing.T) { svc := Service{ + svcConfig: DefaultConfig(), clusterSvc: &mockClusterServicer{}, scyllaClient: mockProviderFunc, secretsStore: &mockStore{}, diff --git a/pkg/service/healthcheck/service_integration_test.go b/pkg/service/healthcheck/service_integration_test.go index 77ee3a08d7..7dbe0b4376 100644 --- a/pkg/service/healthcheck/service_integration_test.go +++ b/pkg/service/healthcheck/service_integration_test.go @@ -82,7 +82,7 @@ func TestStatus_Ping_Independent_From_REST_Integration(t *testing.T) { t.Fatal(err) } - configCacheSvc := configcache.NewService(clusterSvc, scyllaClientProvider, s, logger.Named("config-cache")) + configCacheSvc := configcache.NewService(configcache.DefaultConfig(), clusterSvc, scyllaClientProvider, s, logger.Named("config-cache")) configCacheSvc.Init(context.Background()) defaultConfigForHealthcheck := DefaultConfig() @@ -223,7 +223,8 @@ func testStatusIntegration(t *testing.T, clusterID uuid.UUID, clusterSvc cluster sc.Transport = hrt return scyllaclient.NewClient(sc, logger.Named("scylla")) } - configCacheSvc := configcache.NewService(clusterSvc, scyllaClientProvider, secretsStore, logger.Named("config-cache")) + configCacheSvc := configcache.NewService(configcache.DefaultConfig(), clusterSvc, scyllaClientProvider, + secretsStore, logger.Named("config-cache")) configCacheSvc.Init(context.Background()) s, err := NewService(