Skip to content

Commit

Permalink
vfs: move loggingFS to vfs
Browse files Browse the repository at this point in the history
This change moves the `pebble.loggingFS` code to vfs so it can be used
by other tests. We introduce a `base.InMemLogger` that can be used
with the new logging FS.

The existing implementation did not log `Remove` and `RemoveAll`
operations. This is fixed (hence the test output changes).
  • Loading branch information
RaduBerinde committed Feb 6, 2023
1 parent 4eba71e commit c54158b
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 212 deletions.
38 changes: 18 additions & 20 deletions checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func TestCheckpoint(t *testing.T) {
}
}()

var buf syncedBuffer
mem := vfs.NewMem()
var memLog base.InMemLogger
opts := &Options{
FS: loggingFS{mem, &buf},
FS: vfs.WithLogging(mem, memLog.Infof),
FormatMajorVersion: FormatNewest,
L0CompactionThreshold: 10,
}
Expand All @@ -40,7 +40,7 @@ func TestCheckpoint(t *testing.T) {
if len(td.CmdArgs) != 1 {
return "batch <db>"
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
b := d.NewBatch()
if err := runBatchDefineCmd(td, b); err != nil {
Expand All @@ -49,7 +49,7 @@ func TestCheckpoint(t *testing.T) {
if err := b.Commit(Sync); err != nil {
return err.Error()
}
return buf.String()
return memLog.String()

case "checkpoint":
if !(len(td.CmdArgs) == 2 || (len(td.CmdArgs) == 3 && td.CmdArgs[2].Key == "restrict")) {
Expand All @@ -70,34 +70,34 @@ func TestCheckpoint(t *testing.T) {
}
opts = append(opts, WithRestrictToSpans(spans))
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
if err := d.Checkpoint(td.CmdArgs[1].String(), opts...); err != nil {
return err.Error()
}
return buf.String()
return memLog.String()

case "compact":
if len(td.CmdArgs) != 1 {
return "compact <db>"
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
if err := d.Compact(nil, []byte("\xff"), false); err != nil {
return err.Error()
}
return buf.String()
return memLog.String()

case "flush":
if len(td.CmdArgs) != 1 {
return "flush <db>"
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
if err := d.Flush(); err != nil {
return err.Error()
}
return buf.String()
return memLog.String()

case "list":
if len(td.CmdArgs) != 1 {
Expand All @@ -108,9 +108,7 @@ func TestCheckpoint(t *testing.T) {
return err.Error()
}
sort.Strings(paths)
buf.Reset()
fmt.Fprintf(&buf, "%s\n", strings.Join(paths, "\n"))
return buf.String()
return fmt.Sprintf("%s\n", strings.Join(paths, "\n"))

case "open":
if len(td.CmdArgs) != 1 && len(td.CmdArgs) != 2 {
Expand All @@ -124,30 +122,30 @@ func TestCheckpoint(t *testing.T) {
opts.ReadOnly = true
}

buf.Reset()
memLog.Reset()
dir := td.CmdArgs[0].String()
d, err := Open(dir, opts)
if err != nil {
return err.Error()
}
dbs[dir] = d
return buf.String()
return memLog.String()

case "scan":
if len(td.CmdArgs) != 1 {
return "scan <db>"
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
iter := d.NewIter(nil)
for valid := iter.First(); valid; valid = iter.Next() {
fmt.Fprintf(&buf, "%s %s\n", iter.Key(), iter.Value())
memLog.Infof("%s %s", iter.Key(), iter.Value())
}
fmt.Fprintf(&buf, ".\n")
memLog.Infof(".")
if err := iter.Close(); err != nil {
fmt.Fprintf(&buf, "%v\n", err)
memLog.Infof("%v\n", err)
}
return buf.String()
return memLog.String()

default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
Expand Down
25 changes: 12 additions & 13 deletions cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
Expand All @@ -23,11 +24,11 @@ func TestArchiveCleaner(t *testing.T) {
}
}()

var buf syncedBuffer
mem := vfs.NewMem()
var memLog base.InMemLogger
opts := (&Options{
Cleaner: ArchiveCleaner{},
FS: loggingFS{mem, &buf},
FS: vfs.WithLogging(mem, memLog.Infof),
WALDir: "wal",
}).WithFSDefaults()

Expand All @@ -37,7 +38,7 @@ func TestArchiveCleaner(t *testing.T) {
if len(td.CmdArgs) != 1 {
return "batch <db>"
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
b := d.NewBatch()
if err := runBatchDefineCmd(td, b); err != nil {
Expand All @@ -46,29 +47,29 @@ func TestArchiveCleaner(t *testing.T) {
if err := b.Commit(Sync); err != nil {
return err.Error()
}
return buf.String()
return memLog.String()

case "compact":
if len(td.CmdArgs) != 1 {
return "compact <db>"
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
if err := d.Compact(nil, []byte("\xff"), false); err != nil {
return err.Error()
}
return buf.String()
return memLog.String()

case "flush":
if len(td.CmdArgs) != 1 {
return "flush <db>"
}
buf.Reset()
memLog.Reset()
d := dbs[td.CmdArgs[0].String()]
if err := d.Flush(); err != nil {
return err.Error()
}
return buf.String()
return memLog.String()

case "list":
if len(td.CmdArgs) != 1 {
Expand All @@ -79,9 +80,7 @@ func TestArchiveCleaner(t *testing.T) {
return err.Error()
}
sort.Strings(paths)
buf.Reset()
fmt.Fprintf(&buf, "%s\n", strings.Join(paths, "\n"))
return buf.String()
return fmt.Sprintf("%s\n", strings.Join(paths, "\n"))

case "open":
if len(td.CmdArgs) != 1 && len(td.CmdArgs) != 2 {
Expand All @@ -95,14 +94,14 @@ func TestArchiveCleaner(t *testing.T) {
opts.ReadOnly = true
}

buf.Reset()
memLog.Reset()
dir := td.CmdArgs[0].String()
d, err := Open(dir, opts)
if err != nil {
return err.Error()
}
dbs[dir] = d
return buf.String()
return memLog.String()

default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
Expand Down
Loading

0 comments on commit c54158b

Please sign in to comment.