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: prevent cache size underflow #4986

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pkg/storer/cachestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
"time"

storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ func (db *DB) cacheWorker(ctx context.Context) {
continue
}

evict := size - capc
evict := uint64(size - capc)
if evict < db.reserveOptions.cacheMinEvictCount { // evict at least a min count
evict = db.reserveOptions.cacheMinEvictCount
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/storer/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"sync/atomic"
"time"

storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
"github.com/ethersphere/bee/v2/pkg/swarm"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -61,12 +61,14 @@ func New(ctx context.Context, store storage.Reader, capacity uint64) (*Cache, er
}

// Size returns the current size of the cache.
func (c *Cache) Size() uint64 {
return uint64(c.size.Load())
func (c *Cache) Size() int64 {
return c.size.Load()
}

// Capacity returns the capacity of the cache.
func (c *Cache) Capacity() uint64 { return uint64(c.capacity) }
func (c *Cache) Capacity() int64 {
return int64(c.capacity)
}

// Putter returns a Storage.Putter instance which adds the chunk to the underlying
// chunkstore and also adds a Cache entry for the chunk.
Expand Down
4 changes: 2 additions & 2 deletions pkg/storer/internal/cache/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"time"

storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
Expand Down Expand Up @@ -43,7 +43,7 @@ func (c *Cache) RemoveOldestMaxBatch(ctx context.Context, st transaction.Storage

func (c *Cache) State(store storage.Reader) CacheState {
state := CacheState{}
state.Size = c.Size()
state.Size = uint64(c.Size())
runner := swarm.ZeroAddress

err := store.Iterate(
Expand Down
Loading