Skip to content

Commit bb567bd

Browse files
fix(cache): make the validity timeout configurable
1 parent 80c52e0 commit bb567bd

File tree

8 files changed

+55
-46
lines changed

8 files changed

+55
-46
lines changed

dist/etc/scylla-manager.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ http: 127.0.0.1:5080
3434
# Debug server that allows to run pporf profiling on demand on a live system.
3535
#debug: 127.0.0.1:5112
3636

37+
# Set the validity timeout for Scylla Manager Agent API clients cached by Scylla Manager.
38+
# Use 0 to disable the cache.
39+
#client_cache_timeout: 15m
40+
3741
# Logging configuration.
3842
#logger:
3943
# Where to print logs, stderr or stdout.

pkg/cmd/scylla-manager/server.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ func (s *server) makeServices() error {
7171
drawerStore := store.NewTableStore(s.session, table.Drawer)
7272
secretsStore := store.NewTableStore(s.session, table.Secrets)
7373

74-
s.clusterSvc, err = cluster.NewService(s.session, metrics.NewClusterMetrics().MustRegister(), secretsStore, s.config.TimeoutConfig, s.logger.Named("cluster"))
74+
s.clusterSvc, err = cluster.NewService(s.session, metrics.NewClusterMetrics().MustRegister(), secretsStore, s.config.TimeoutConfig,
75+
s.config.ClientCacheTimeout, s.logger.Named("cluster"))
7576
if err != nil {
7677
return errors.Wrapf(err, "cluster service")
7778
}

pkg/config/server/server.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,23 @@ type SSLConfig struct {
4646

4747
// Config contains configuration structure for scylla manager.
4848
type Config struct {
49-
HTTP string `yaml:"http"`
50-
HTTPS string `yaml:"https"`
51-
TLSVersion config.TLSVersion `yaml:"tls_version"`
52-
TLSCertFile string `yaml:"tls_cert_file"`
53-
TLSKeyFile string `yaml:"tls_key_file"`
54-
TLSCAFile string `yaml:"tls_ca_file"`
55-
Prometheus string `yaml:"prometheus"`
56-
Debug string `yaml:"debug"`
57-
Logger config.LogConfig `yaml:"logger"`
58-
Database DBConfig `yaml:"database"`
59-
SSL SSLConfig `yaml:"ssl"`
60-
Healthcheck healthcheck.Config `yaml:"healthcheck"`
61-
Backup backup.Config `yaml:"backup"`
62-
Restore restore.Config `yaml:"restore"`
63-
Repair repair.Config `yaml:"repair"`
64-
TimeoutConfig scyllaclient.TimeoutConfig `yaml:"agent_client"`
49+
HTTP string `yaml:"http"`
50+
HTTPS string `yaml:"https"`
51+
TLSVersion config.TLSVersion `yaml:"tls_version"`
52+
TLSCertFile string `yaml:"tls_cert_file"`
53+
TLSKeyFile string `yaml:"tls_key_file"`
54+
TLSCAFile string `yaml:"tls_ca_file"`
55+
Prometheus string `yaml:"prometheus"`
56+
Debug string `yaml:"debug"`
57+
ClientCacheTimeout time.Duration `yaml:"client_cache_timeout"`
58+
Logger config.LogConfig `yaml:"logger"`
59+
Database DBConfig `yaml:"database"`
60+
SSL SSLConfig `yaml:"ssl"`
61+
Healthcheck healthcheck.Config `yaml:"healthcheck"`
62+
Backup backup.Config `yaml:"backup"`
63+
Restore restore.Config `yaml:"restore"`
64+
Repair repair.Config `yaml:"repair"`
65+
TimeoutConfig scyllaclient.TimeoutConfig `yaml:"agent_client"`
6566
}
6667

6768
func DefaultConfig() Config {
@@ -83,11 +84,12 @@ func DefaultConfig() Config {
8384
SSL: SSLConfig{
8485
Validate: true,
8586
},
86-
Healthcheck: healthcheck.DefaultConfig(),
87-
Backup: backup.DefaultConfig(),
88-
Restore: restore.DefaultConfig(),
89-
Repair: repair.DefaultConfig(),
90-
TimeoutConfig: scyllaclient.DefaultTimeoutConfig(),
87+
Healthcheck: healthcheck.DefaultConfig(),
88+
ClientCacheTimeout: 15 * time.Minute,
89+
Backup: backup.DefaultConfig(),
90+
Restore: restore.DefaultConfig(),
91+
Repair: repair.DefaultConfig(),
92+
TimeoutConfig: scyllaclient.DefaultTimeoutConfig(),
9193
}
9294
}
9395

pkg/config/server/server_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ func TestConfigModification(t *testing.T) {
3737
}
3838

3939
golden := server.Config{
40-
HTTP: "127.0.0.1:80",
41-
HTTPS: "127.0.0.1:443",
42-
TLSVersion: "TLSv1.3",
43-
TLSCertFile: "tls.cert",
44-
TLSKeyFile: "tls.key",
45-
TLSCAFile: "ca.cert",
46-
Prometheus: "127.0.0.1:9090",
47-
Debug: "127.0.0.1:112",
40+
HTTP: "127.0.0.1:80",
41+
HTTPS: "127.0.0.1:443",
42+
TLSVersion: "TLSv1.3",
43+
TLSCertFile: "tls.cert",
44+
TLSKeyFile: "tls.key",
45+
TLSCAFile: "ca.cert",
46+
Prometheus: "127.0.0.1:9090",
47+
Debug: "127.0.0.1:112",
48+
ClientCacheTimeout: 15 * time.Minute,
4849
Logger: config.LogConfig{
4950
Config: log.Config{
5051
Mode: log.StderrMode,

pkg/scyllaclient/provider.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import (
1515
// ProviderFunc is a function that returns a Client for a given cluster.
1616
type ProviderFunc func(ctx context.Context, clusterID uuid.UUID) (*Client, error)
1717

18-
// hostCheckValidity specifies how often to check if hosts changed.
19-
const hostCheckValidity = 15 * time.Minute
20-
2118
type clientTTL struct {
2219
client *Client
2320
ttl time.Time
@@ -32,10 +29,10 @@ type CachedProvider struct {
3229
logger log.Logger
3330
}
3431

35-
func NewCachedProvider(f ProviderFunc, logger log.Logger) *CachedProvider {
32+
func NewCachedProvider(f ProviderFunc, cacheInvalidationTimeout time.Duration, logger log.Logger) *CachedProvider {
3633
return &CachedProvider{
3734
inner: f,
38-
validity: hostCheckValidity,
35+
validity: cacheInvalidationTimeout,
3936
clients: make(map[uuid.UUID]clientTTL),
4037
logger: logger.Named("cache-provider"),
4138
}
@@ -49,16 +46,12 @@ func (p *CachedProvider) Client(ctx context.Context, clusterID uuid.UUID) (*Clie
4946

5047
// Cache hit
5148
if ok {
52-
if c.ttl.After(timeutc.Now()) {
53-
return c.client, nil
54-
}
55-
5649
// Check if hosts did not change before returning
5750
changed, err := c.client.CheckHostsChanged(ctx)
5851
if err != nil {
5952
p.logger.Error(ctx, "Cannot check if hosts changed", "error", err)
6053
}
61-
if !changed && err == nil {
54+
if c.ttl.After(timeutc.Now()) && !changed && err == nil {
6255
return c.client, nil
6356
}
6457
}

pkg/service/cluster/service.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"sort"
1111
"strconv"
12+
"time"
1213

1314
"github.com/gocql/gocql"
1415
"github.com/pkg/errors"
@@ -56,7 +57,9 @@ type Service struct {
5657
onChangeListener func(ctx context.Context, c Change) error
5758
}
5859

59-
func NewService(session gocqlx.Session, metrics metrics.ClusterMetrics, secretsStore store.Store, timeoutConfig scyllaclient.TimeoutConfig, l log.Logger) (*Service, error) {
60+
func NewService(session gocqlx.Session, metrics metrics.ClusterMetrics, secretsStore store.Store, timeoutConfig scyllaclient.TimeoutConfig,
61+
cacheInvalidationTimeout time.Duration, l log.Logger,
62+
) (*Service, error) {
6063
if session.Session == nil || session.Closed() {
6164
return nil, errors.New("invalid session")
6265
}
@@ -68,7 +71,7 @@ func NewService(session gocqlx.Session, metrics metrics.ClusterMetrics, secretsS
6871
logger: l,
6972
timeoutConfig: timeoutConfig,
7073
}
71-
s.clientCache = scyllaclient.NewCachedProvider(s.createClient)
74+
s.clientCache = scyllaclient.NewCachedProvider(s.createClient, cacheInvalidationTimeout, l)
7275

7376
return s, nil
7477
}

pkg/service/cluster/service_integration_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/google/go-cmp/cmp/cmpopts"
1717
"github.com/pkg/errors"
1818
"github.com/scylladb/go-log"
19+
"github.com/scylladb/scylla-manager/v3/pkg/config/server"
1920
. "github.com/scylladb/scylla-manager/v3/pkg/testutils/testconfig"
2021

2122
"github.com/scylladb/scylla-manager/v3/pkg/metrics"
@@ -35,7 +36,8 @@ func TestClientIntegration(t *testing.T) {
3536

3637
session := CreateScyllaManagerDBSession(t)
3738
secretsStore := store.NewTableStore(session, table.Secrets)
38-
s, err := cluster.NewService(session, metrics.NewClusterMetrics(), secretsStore, scyllaclient.DefaultTimeoutConfig(), log.NewDevelopment())
39+
s, err := cluster.NewService(session, metrics.NewClusterMetrics(), secretsStore, scyllaclient.DefaultTimeoutConfig(),
40+
server.DefaultConfig().ClientCacheTimeout, log.NewDevelopment())
3941
if err != nil {
4042
t.Fatal(err)
4143
}
@@ -103,7 +105,8 @@ func TestServiceStorageIntegration(t *testing.T) {
103105

104106
secretsStore := store.NewTableStore(session, table.Secrets)
105107

106-
s, err := cluster.NewService(session, metrics.NewClusterMetrics(), secretsStore, scyllaclient.DefaultTimeoutConfig(), log.NewDevelopment())
108+
s, err := cluster.NewService(session, metrics.NewClusterMetrics(), secretsStore, scyllaclient.DefaultTimeoutConfig(),
109+
server.DefaultConfig().ClientCacheTimeout, log.NewDevelopment())
107110
if err != nil {
108111
t.Fatal(err)
109112
}

pkg/service/healthcheck/service_integration_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func TestStatusIntegration(t *testing.T) {
4242
defer session.Close()
4343

4444
s := store.NewTableStore(session, table.Secrets)
45-
clusterSvc, err := cluster.NewService(session, metrics.NewClusterMetrics(), s, scyllaclient.DefaultTimeoutConfig(), log.NewDevelopment())
45+
clusterSvc, err := cluster.NewService(session, metrics.NewClusterMetrics(), s, scyllaclient.DefaultTimeoutConfig(),
46+
15*time.Minute, log.NewDevelopment())
4647
if err != nil {
4748
t.Fatal(err)
4849
}
@@ -69,7 +70,8 @@ func TestStatusWithCQLCredentialsIntegration(t *testing.T) {
6970
defer session.Close()
7071

7172
s := store.NewTableStore(session, table.Secrets)
72-
clusterSvc, err := cluster.NewService(session, metrics.NewClusterMetrics(), s, scyllaclient.DefaultTimeoutConfig(), log.NewDevelopment())
73+
clusterSvc, err := cluster.NewService(session, metrics.NewClusterMetrics(), s, scyllaclient.DefaultTimeoutConfig(),
74+
15*time.Minute, log.NewDevelopment())
7375
if err != nil {
7476
t.Fatal(err)
7577
}

0 commit comments

Comments
 (0)