Skip to content

Commit

Permalink
db: rework fileCache to use genericcache
Browse files Browse the repository at this point in the history
We no longer intermingle the cache infrastructure logic with the file
cache specifics. This will help when we extend the file cache to
support blob files.
  • Loading branch information
RaduBerinde committed Feb 14, 2025
1 parent 8cc384d commit 6edd850
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 745 deletions.
30 changes: 26 additions & 4 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,10 @@ func TestIterLeak(t *testing.T) {
t.Run(fmt.Sprintf("leak=%t", leak), func(t *testing.T) {
for _, flush := range []bool{true, false} {
t.Run(fmt.Sprintf("flush=%t", flush), func(t *testing.T) {
fc := NewFileCache(10, 100)
d, err := Open("", testingRandomized(t, &Options{
FS: vfs.NewMem(),
FS: vfs.NewMem(),
FileCache: fc,
}))
require.NoError(t, err)

Expand All @@ -710,15 +712,16 @@ func TestIterLeak(t *testing.T) {
require.NoError(t, iter.Close())
require.NoError(t, d.Close())
} else {
defer iter.Close()
if err := d.Close(); err == nil {
t.Fatalf("expected failure, but found success")
} else if !strings.HasPrefix(err.Error(), "leaked iterators:") {
t.Fatalf("expected leaked iterators, but found %+v", err)
} else {
t.Log(err.Error())
}
iter.Close()
}
fc.Unref()
})
}
})
Expand All @@ -732,13 +735,16 @@ func TestIterLeakSharedCache(t *testing.T) {
t.Run(fmt.Sprintf("leak=%t", leak), func(t *testing.T) {
for _, flush := range []bool{true, false} {
t.Run(fmt.Sprintf("flush=%t", flush), func(t *testing.T) {
fc := NewFileCache(10, 100)
d1, err := Open("", &Options{
FS: vfs.NewMem(),
FS: vfs.NewMem(),
FileCache: fc,
})
require.NoError(t, err)

d2, err := Open("", &Options{
FS: vfs.NewMem(),
FS: vfs.NewMem(),
FileCache: fc,
})
require.NoError(t, err)

Expand Down Expand Up @@ -789,6 +795,22 @@ func TestIterLeakSharedCache(t *testing.T) {
}
}

if !leak {
fc.Unref()
} else if flush {
require.Panics(t, func() {
fc.Unref()
})
} else {
// When we're not flushing and we leak an iterator, Unref might or
// might not panic, depending on whether there was a file involved.
func() {
defer func() {
recover()
}()
fc.Unref()
}()
}
})
}
})
Expand Down
Loading

0 comments on commit 6edd850

Please sign in to comment.