From 3f37fff22ddbe971310c835c48c78f07e1219f22 Mon Sep 17 00:00:00 2001 From: istae <14264581+istae@users.noreply.github.com> Date: Tue, 6 Feb 2024 16:05:59 +0300 Subject: [PATCH] fix: cache size fix --- pkg/storer/internal/cache/cache.go | 24 +++++++++--------- pkg/storer/internal/cache/cache_test.go | 31 ++++++++++++++++++++++++ pkg/storer/internal/cache/export_test.go | 6 +++++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/pkg/storer/internal/cache/cache.go b/pkg/storer/internal/cache/cache.go index 6a79ad38521..a6644fe4d39 100644 --- a/pkg/storer/internal/cache/cache.go +++ b/pkg/storer/internal/cache/cache.go @@ -260,14 +260,8 @@ func (c *Cache) ShallowCopy( return nil } -// RemoveOldest removes the oldest cache entries from the store. The count -// specifies the number of entries to remove. -func (c *Cache) RemoveOldest( - ctx context.Context, - store internal.Storage, - chStore storage.ChunkStore, - count uint64, -) error { +func (c *Cache) removeOldest(ctx context.Context, store internal.Storage, chStore storage.ChunkStore, count uint64, batchCnt int) error { + if count <= 0 { return nil } @@ -299,8 +293,6 @@ func (c *Cache) RemoveOldest( return fmt.Errorf("failed iterating over cache order index: %w", err) } - batchCnt := 1_000 - for i := 0; i < len(evictItems); i += batchCnt { end := i + batchCnt if end > len(evictItems) { @@ -312,7 +304,9 @@ func (c *Cache) RemoveOldest( return fmt.Errorf("failed creating batch: %w", err) } - for _, entry := range evictItems[i:end] { + items := evictItems[i:end] + + for _, entry := range items { err = batch.Delete(entry) if err != nil { return fmt.Errorf("failed deleting cache entry %s: %w", entry, err) @@ -335,12 +329,18 @@ func (c *Cache) RemoveOldest( return err } - c.size.Add(-int64(len(evictItems))) + c.size.Add(-int64(len(items))) } return nil } +// RemoveOldest removes the oldest cache entries from the store. The count +// specifies the number of entries to remove. +func (c *Cache) RemoveOldest(ctx context.Context, store internal.Storage, chStore storage.ChunkStore, count uint64) error { + return c.removeOldest(ctx, store, store.ChunkStore(), count, 1000) +} + type cacheEntry struct { Address swarm.Address AccessTimestamp int64 diff --git a/pkg/storer/internal/cache/cache_test.go b/pkg/storer/internal/cache/cache_test.go index 8a3da624c75..c331853d352 100644 --- a/pkg/storer/internal/cache/cache_test.go +++ b/pkg/storer/internal/cache/cache_test.go @@ -347,6 +347,37 @@ func TestCache(t *testing.T) { }) } +func TestRemoveOldest(t *testing.T) { + t.Parallel() + + st := newTestStorage(t) + c, err := cache.New(context.Background(), st, 10) + if err != nil { + t.Fatal(err) + } + + chunks := chunktest.GenerateTestRandomChunks(30) + + for _, ch := range chunks { + err = c.Putter(st).Put(context.Background(), ch) + if err != nil { + t.Fatal(err) + } + } + + verifyCacheState(t, st.IndexStore(), c, chunks[0].Address(), chunks[29].Address(), 30) + verifyCacheOrder(t, c, st.IndexStore(), chunks...) + + err = c.RemoveOldestMaxBatch(context.Background(), st, st.ChunkStore(), 30, 5) + if err != nil { + t.Fatal(err) + } + + verifyCacheState(t, st.IndexStore(), c, swarm.ZeroAddress, swarm.ZeroAddress, 0) + + verifyChunksDeleted(t, st.ChunkStore(), chunks...) +} + func TestShallowCopy(t *testing.T) { t.Parallel() diff --git a/pkg/storer/internal/cache/export_test.go b/pkg/storer/internal/cache/export_test.go index 770bdd64690..9a8eda3afa3 100644 --- a/pkg/storer/internal/cache/export_test.go +++ b/pkg/storer/internal/cache/export_test.go @@ -5,10 +5,12 @@ package cache import ( + "context" "fmt" "time" storage "github.com/ethersphere/bee/pkg/storage" + "github.com/ethersphere/bee/pkg/storer/internal" "github.com/ethersphere/bee/pkg/swarm" ) @@ -35,6 +37,10 @@ type CacheState struct { Size uint64 } +func (c *Cache) RemoveOldestMaxBatch(ctx context.Context, store internal.Storage, chStore storage.ChunkStore, count uint64, batchCnt int) error { + return c.removeOldest(ctx, store, store.ChunkStore(), count, batchCnt) +} + func (c *Cache) State(store storage.Store) CacheState { state := CacheState{} state.Size = c.Size()