Skip to content

Commit

Permalink
fix: cache size fix
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Feb 6, 2024
1 parent ee249af commit 3f37fff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
24 changes: 12 additions & 12 deletions pkg/storer/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions pkg/storer/internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions pkg/storer/internal/cache/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()
Expand Down

0 comments on commit 3f37fff

Please sign in to comment.