Skip to content

Commit

Permalink
fix: metric setter
Browse files Browse the repository at this point in the history
  • Loading branch information
alok committed Jul 28, 2023
1 parent e05128e commit efc66ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/storer/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,8 @@ func (c *Cache) Getter(store internal.Storage) storage.Getter {
}

// MoveFromReserve moves the chunks from the reserve to the cache. This is
// called when the reserve is full and we need to move chunks from the reserve
// to the cache. It avoids the need to delete the chunk and re-add it to the
// cache.
// called when the reserve is full and we need to perform eviction.
// It avoids the need to delete the chunk and re-add it to the cache.
func (c *Cache) MoveFromReserve(
ctx context.Context,
store internal.Storage,
Expand All @@ -481,11 +480,16 @@ func (c *Cache) MoveFromReserve(
newHead := &cacheEntry{Address: state.Head}
newTail := &cacheEntry{Address: state.Tail}

// if newTail is not found, this is a new cache and we need to set the
// tail and head
err = store.IndexStore().Get(newTail)
if err != nil && !errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("failed getting tail entry %s: %w", newTail, err)
}

// this is a corner case where cache capacity is less than the no of items
// being added. In this case we need to only add the last c.capacity items
// from the addrs slice. This would generally happen in tests.
if len(addrs) > int(c.capacity) {
addrs = addrs[len(addrs)-int(c.capacity):]
}
Expand All @@ -505,6 +509,8 @@ func (c *Cache) MoveFromReserve(
return fmt.Errorf("failed updating new tail entry %s: %w", newTail, err)
}
} else {
// if we are here, it means its a fresh cache and we need to set
// the head and tail
newHead = newEntry
}
newTail = newEntry
Expand All @@ -515,6 +521,7 @@ func (c *Cache) MoveFromReserve(
return nil
}

// last entry after the loop is the new tail
err = batch.Put(newTail)
if err != nil {
return fmt.Errorf("failed updating new tail entry %s: %w", newTail, err)
Expand All @@ -541,6 +548,8 @@ func (c *Cache) MoveFromReserve(
}
newHead = &cacheEntry{Address: newHead.Next}
}
// this is again a corner case where the cache capacity no. of items
// are being added, so the newHead is the first item in the slice.
if newHead.Address.IsZero() {
newHead = &cacheEntry{Address: addrs[0]}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/storer/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ func New(ctx context.Context, dirPath string, opts *Options) (*DB, error) {
opts.RadiusSetter,
logger,
func(ctx context.Context, store internal.Storage, addrs ...swarm.Address) error {
defer func() { db.metrics.CacheSize.Set(float64(db.cacheObj.Size())) }()

db.lock.Lock(cacheAccessLockKey)
defer db.lock.Unlock(cacheAccessLockKey)

Expand Down

0 comments on commit efc66ed

Please sign in to comment.