Skip to content

Commit

Permalink
Fix rare missed sizecache lookup when first block is present (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbonfort authored Apr 6, 2022
1 parent 6d4e277 commit c78f4ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ func (a *Adapter) Size(key string) (int64, error) {
_, err = a.ReadAt(key, []byte{0}, 0) //ignore errors as we just want to populate the size cache
si, ok = a.sizeCache.Get(key)
}
if err == nil && !ok {
//first block may be in the block cache, but the size was evicted from the size cache, so we force
//a direct read to the source to repopulate the size cache. This should happen extremely
//unfrequently.
_, err = a.srcReadAt(key, []byte{0}, 0)
si, ok = a.sizeCache.Get(key)
}

if ok {
size := si.(int64)
if size == -1 {
Expand Down
9 changes: 9 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,12 @@ func TestLogging(t *testing.T) {
_, _ = r.Read(buf)
assert.Contains(t, lbuf.String(), "GET thekey off=0 len=131072")
}

func TestSizeCacheEviction(t *testing.T) {
bc, _ := NewAdapter(rr, SizeCache(1))
_, err := bc.Size("thekey")
assert.NoError(t, err)
_, _ = bc.Size("enoent")
_, err = bc.Size("thekey")
assert.NoError(t, err)
}

0 comments on commit c78f4ce

Please sign in to comment.