From 4edd543f5096d6b698115606f37a90e177d28026 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Fri, 13 Dec 2024 15:50:37 +0100 Subject: [PATCH] expire worker cache faster during testing --- cmd/renterd/config.go | 1 + config/config.go | 1 + internal/test/e2e/cluster.go | 1 + internal/worker/cache.go | 18 +++++++++--------- worker/worker.go | 5 ++++- worker/worker_test.go | 1 + 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cmd/renterd/config.go b/cmd/renterd/config.go index ee9252096..07c8b4951 100644 --- a/cmd/renterd/config.go +++ b/cmd/renterd/config.go @@ -97,6 +97,7 @@ func defaultConfig() config.Config { ID: "", AccountsRefillInterval: defaultAccountRefillInterval, BusFlushInterval: 5 * time.Second, + CacheExpiry: 5 * time.Minute, DownloadMaxOverdrive: 5, DownloadOverdriveTimeout: 3 * time.Second, diff --git a/config/config.go b/config/config.go index 085503b49..a022776c4 100644 --- a/config/config.go +++ b/config/config.go @@ -130,6 +130,7 @@ type ( UploadMaxMemory uint64 `yaml:"uploadMaxMemory,omitempty"` UploadMaxOverdrive uint64 `yaml:"uploadMaxOverdrive,omitempty"` AllowUnauthenticatedDownloads bool `yaml:"allowUnauthenticatedDownloads,omitempty"` + CacheExpiry time.Duration `yaml:"cacheExpiry,omitempty"` } // Autopilot contains the configuration for an autopilot. diff --git a/internal/test/e2e/cluster.go b/internal/test/e2e/cluster.go index bee8ce48e..5b3c9ba3d 100644 --- a/internal/test/e2e/cluster.go +++ b/internal/test/e2e/cluster.go @@ -994,6 +994,7 @@ func testDBCfg() dbConfig { func testWorkerCfg() config.Worker { return config.Worker{ AccountsRefillInterval: 10 * time.Millisecond, + CacheExpiry: 100 * time.Millisecond, ID: "worker", BusFlushInterval: testBusFlushInterval, DownloadOverdriveTimeout: 500 * time.Millisecond, diff --git a/internal/worker/cache.go b/internal/worker/cache.go index 3fc69184a..f0fedded8 100644 --- a/internal/worker/cache.go +++ b/internal/worker/cache.go @@ -12,14 +12,13 @@ import ( ) const ( - cacheEntryExpiry = 5 * time.Minute - cacheKeyUsableHosts = "usablehosts" ) type memoryCache struct { - items map[string]*cacheEntry - mu sync.RWMutex + cacheEntryExpiry time.Duration + items map[string]*cacheEntry + mu sync.RWMutex } type cacheEntry struct { @@ -27,9 +26,10 @@ type cacheEntry struct { expiry time.Time } -func newMemoryCache() *memoryCache { +func newMemoryCache(expiry time.Duration) *memoryCache { return &memoryCache{ - items: make(map[string]*cacheEntry), + cacheEntryExpiry: expiry, + items: make(map[string]*cacheEntry), } } @@ -59,7 +59,7 @@ func (c *memoryCache) Set(key string, value interface{}) { defer c.mu.Unlock() c.items[key] = &cacheEntry{ value: value, - expiry: time.Now().Add(cacheEntryExpiry), + expiry: time.Now().Add(c.cacheEntryExpiry), } } @@ -79,12 +79,12 @@ type cache struct { logger *zap.SugaredLogger } -func NewCache(b Bus, logger *zap.Logger) WorkerCache { +func NewCache(b Bus, expiry time.Duration, logger *zap.Logger) WorkerCache { logger = logger.Named("workercache") return &cache{ b: b, - cache: newMemoryCache(), + cache: newMemoryCache(expiry), logger: logger.Sugar(), } } diff --git a/worker/worker.go b/worker/worker.go index f73ed59cf..1e4c63c8c 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -669,6 +669,9 @@ func New(cfg config.Worker, masterKey [32]byte, b Bus, l *zap.Logger) (*Worker, if cfg.UploadMaxMemory == 0 { return nil, errors.New("uploadMaxMemory cannot be 0") } + if cfg.CacheExpiry == 0 { + return nil, errors.New("cache expiry cannot be 0") + } a := alerts.WithOrigin(b, fmt.Sprintf("worker.%s", cfg.ID)) shutdownCtx, shutdownCancel := context.WithCancel(context.Background()) @@ -676,7 +679,7 @@ func New(cfg config.Worker, masterKey [32]byte, b Bus, l *zap.Logger) (*Worker, dialer := rhp.NewFallbackDialer(b, net.Dialer{}, l) w := &Worker{ alerts: a, - cache: iworker.NewCache(b, l), + cache: iworker.NewCache(b, cfg.CacheExpiry, l), dialer: dialer, id: cfg.ID, bus: b, diff --git a/worker/worker_test.go b/worker/worker_test.go index 0c731766b..c32e51e1e 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -156,6 +156,7 @@ func (w *testWorker) UsableHosts() []api.HostInfo { func newTestWorkerCfg() config.Worker { return config.Worker{ AccountsRefillInterval: time.Second, + CacheExpiry: 100 * time.Millisecond, ID: "test", BusFlushInterval: time.Second, DownloadOverdriveTimeout: time.Second,