Skip to content

Commit

Permalink
cache: Update cache targetSize to always return a positive int
Browse files Browse the repository at this point in the history
Noticed when ingesting SSTables as part of a cockroach snapshot apply.
reservedSize would exceed maxSize, leading to an infinite loop in
evict().
  • Loading branch information
itsbilal committed Oct 21, 2019
1 parent d3404bf commit c96d043
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 8 additions & 1 deletion internal/cache/clockpro.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,14 @@ func (c *shard) Size() int64 {
}

func (c *shard) targetSize() int64 {
return c.maxSize - c.reservedSize
target := c.maxSize - c.reservedSize
// Always return a positive integer for targetSize. This is so that we don't
// end up in an infinite loop in evict(), in cases where reservedSize is
// greater than or equal to maxSize.
if target < 1 {
return 1
}
return target
}

// Add the entry to the cache, returning true if the entry was added and false
Expand Down
10 changes: 6 additions & 4 deletions internal/cache/clockpro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,22 @@ func TestZeroSize(t *testing.T) {
}

func TestReserve(t *testing.T) {
cache := newShards(2, 2)
cache := newShards(4, 2)
cache.Set(1, 0, 0, []byte("a"))
cache.Set(2, 0, 0, []byte("a"))
require.EqualValues(t, 2, cache.Size())
r := cache.Reserve(1)
require.EqualValues(t, 0, cache.Size())
cache.Set(1, 0, 0, []byte("a"))
cache.Set(2, 0, 0, []byte("a"))
require.EqualValues(t, 0, cache.Size())
cache.Set(3, 0, 0, []byte("a"))
cache.Set(4, 0, 0, []byte("a"))
require.EqualValues(t, 2, cache.Size())
r()
require.EqualValues(t, 0, cache.Size())
require.EqualValues(t, 2, cache.Size())
cache.Set(1, 0, 0, []byte("a"))
cache.Set(2, 0, 0, []byte("a"))
require.EqualValues(t, 2, cache.Size())
require.EqualValues(t, 4, cache.Size())
}

func TestReserveDoubleRelease(t *testing.T) {
Expand Down

0 comments on commit c96d043

Please sign in to comment.