diff --git a/pkg/storer/cachestore.go b/pkg/storer/cachestore.go index 90a1c704551..7be738b5d38 100644 --- a/pkg/storer/cachestore.go +++ b/pkg/storer/cachestore.go @@ -41,10 +41,10 @@ func (db *DB) cacheWorker(ctx context.Context) { continue } - newSize := size - uint64((float64(capc) * 0.90)) // evict until cache size is 90% of capacity + evict := size - uint64((float64(capc) * 0.90)) // evict until cache size is 90% of capacity err := db.Execute(ctx, func(s internal.Storage) error { - return db.cacheObj.RemoveOldest(ctx, s, s.ChunkStore(), newSize) + return db.cacheObj.RemoveOldest(ctx, s, s.ChunkStore(), evict) }) if err != nil { db.logger.Warning("cache eviction failure", "error", err) diff --git a/pkg/storer/cachestore_test.go b/pkg/storer/cachestore_test.go index b479d944ca6..dc2264491e7 100644 --- a/pkg/storer/cachestore_test.go +++ b/pkg/storer/cachestore_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/ethersphere/bee/pkg/spinlock" storage "github.com/ethersphere/bee/pkg/storage" "github.com/ethersphere/bee/pkg/storage/storagetest" chunktesting "github.com/ethersphere/bee/pkg/storage/testing" @@ -99,6 +100,7 @@ func testCacheStore(t *testing.T, newStorer func() (*storer.DB, error)) { lstore.SetRepoStorePutHook(nil) // add chunks beyond capacity and verify the correct chunks are removed // from the cache based on last access order + newChunks := chunktesting.GenerateTestRandomChunks(5) putter := lstore.Cache() for _, ch := range newChunks { @@ -113,8 +115,23 @@ func testCacheStore(t *testing.T, newStorer func() (*storer.DB, error)) { t.Fatal(err) } - if info.Cache.Size != 10 { - t.Fatalf("unexpected cache size: want 10 have %d", info.Cache.Size) + info, err = lstore.DebugInfo(context.Background()) + if err != nil { + t.Fatal(err) + } + + err = spinlock.WaitWithInterval(time.Second*5, time.Second, func() bool { + info, err = lstore.DebugInfo(context.Background()) + if err != nil { + t.Fatal(err) + } + if info.Cache.Size == 10 { + return true + } + return false + }) + if err != nil { + t.Fatal(err) } }) } diff --git a/pkg/storer/internal/cache/cache.go b/pkg/storer/internal/cache/cache.go index b524ab8b617..edb06420924 100644 --- a/pkg/storer/internal/cache/cache.go +++ b/pkg/storer/internal/cache/cache.go @@ -311,7 +311,14 @@ func (c *Cache) RemoveOldest( } } - return batch.Commit() + err = batch.Commit() + if err != nil { + return err + } + + c.size.Add(-int64(len(evictItems))) + + return nil } type cacheEntry struct {