Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cache): missing chunk #4572

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions pkg/storer/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,18 @@ 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 {
return c.removeOldest(ctx, store, store.ChunkStore(), count, 1000)
}

func (c *Cache) removeOldest(ctx context.Context, store internal.Storage, chStore storage.ChunkStore, count uint64, batchCnt int) error {

if count <= 0 {
return nil
}

// we are okay to not lock here because RemoveOldest removes entries from the beginning of the list
// while all the functions above adds new entries.

evictItems := make([]*cacheEntry, 0, count)
err := store.IndexStore().Iterate(
storage.Query{
Expand All @@ -293,6 +296,9 @@ func (c *Cache) removeOldest(ctx context.Context, store internal.Storage, chStor
return fmt.Errorf("failed iterating over cache order index: %w", err)
}

c.glock.Lock()
defer c.glock.Unlock()

for i := 0; i < len(evictItems); i += batchCnt {
end := i + batchCnt
if end > len(evictItems) {
Expand All @@ -304,9 +310,7 @@ func (c *Cache) removeOldest(ctx context.Context, store internal.Storage, chStor
return fmt.Errorf("failed creating batch: %w", err)
}

items := evictItems[i:end]

for _, entry := range items {
for _, entry := range evictItems[i:end] {
err = batch.Delete(entry)
if err != nil {
return fmt.Errorf("failed deleting cache entry %s: %w", entry, err)
Expand All @@ -329,18 +333,12 @@ func (c *Cache) removeOldest(ctx context.Context, store internal.Storage, chStor
return err
}

c.size.Add(-int64(len(items)))
c.size.Add(-int64(end - i))
}

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
Loading