Skip to content

Commit

Permalink
crl-release-22.2: db: wrap error when creating Reader with backing fi…
Browse files Browse the repository at this point in the history
…lenum

Backport of cockroachdb#2633

Informs cockroachlabs/support#2643
  • Loading branch information
sumeerbhola authored and RaduBerinde committed Oct 6, 2023
1 parent a270b67 commit 7d3c5ff
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions table_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,9 @@ func (v *tableCacheValue) load(meta *fileMetadata, c *tableCacheShard, dbOpts *t
reopenOpt := sstable.FileReopenOpt{FS: dbOpts.fs, Filename: v.filename}
v.reader, v.err = sstable.NewReader(f, dbOpts.opts, cacheOpts, dbOpts.filterMetrics, reopenOpt)
}
if v.err != nil {
v.err = errors.Wrapf(v.err, "pebble: file %s error", errors.Safe(meta.FileNum))
}
if v.err == nil {
if meta.SmallestSeqNum == meta.LargestSeqNum {
v.reader.Properties.GlobalSeqNum = meta.LargestSeqNum
Expand Down
53 changes: 52 additions & 1 deletion table_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,13 @@ func TestTableCacheRetryAfterFailure(t *testing.T) {
require.NoError(t, err)

fs.setOpenError(true /* enabled */)
if _, _, err := c.newIters(
if _, _, err = c.newIters(
&fileMetadata{FileNum: 0},
nil, /* iter options */
internalIterOpts{}); err == nil {
t.Fatalf("expected failure, but found success")
}
require.Equal(t, "pebble: file 000000 error: injected error", err.Error())
fs.setOpenError(false /* enabled */)
var iter internalIterator
iter, _, err = c.newIters(
Expand All @@ -825,6 +826,56 @@ func TestTableCacheRetryAfterFailure(t *testing.T) {
fs.validate(t, c, nil)
}

// memFile is a file-like struct that buffers all data written to it in memory.
// Implements the sstable.writeCloseSyncer interface.
type memFile struct {
*bytes.Buffer
}

// Sync is part of the writeCloseSyncer interface.
func (*memFile) Sync() error {
return nil
}

// Close is part of the writeCloseSynce interface.
func (*memFile) Close() error {
return nil
}

func TestTableCacheErrorBadMagicNumber(t *testing.T) {
file := memFile{Buffer: &bytes.Buffer{}}
tw := sstable.NewWriter(&file, sstable.WriterOptions{TableFormat: sstable.TableFormatPebblev2})
tw.Set([]byte("a"), nil)
require.NoError(t, tw.Close())
buf := file.Bytes()
// Bad magic number.
buf[len(buf)-1] = 0
fs := &tableCacheTestFS{
FS: vfs.NewMem(),
}
const testFileNum = 3
f, err := fs.Create(base.MakeFilepath(fs, "", fileTypeTable, base.FileNum(testFileNum)))
require.NoError(t, err)
_, err = f.Write(buf)
require.NoError(t, err)
require.NoError(t, f.Close())
opts := &Options{}
opts.EnsureDefaults()
opts.Cache = NewCache(8 << 20) // 8 MB
defer opts.Cache.Unref()
c := newTableCacheContainer(nil, opts.Cache.NewID(), "", fs, opts, tableCacheTestCacheSize)
require.NoError(t, err)
defer c.close()

m := &fileMetadata{FileNum: testFileNum}
if _, _, err = c.newIters(m, nil, internalIterOpts{}); err == nil {
t.Fatalf("expected failure, but found success")
}
require.Equal(t,
"pebble: file 000003 error: pebble/table: invalid table (bad magic number: 0xf09faab3f09faa00)",
err.Error())
}

func TestTableCacheEvictClose(t *testing.T) {
errs := make(chan error, 10)
db, err := Open("test",
Expand Down

0 comments on commit 7d3c5ff

Please sign in to comment.