Skip to content

Commit

Permalink
manifest: Added Virtual SSTable Metadata Bounds Checking
Browse files Browse the repository at this point in the history
Added a check inside of `FileMetadata.ValidateVirtual()` to ensure that
the keys returned from a virtual sstable are within the bounds of the
Smallest and Largest metadata keys.

Informs: #2593
  • Loading branch information
raggar committed Jul 10, 2023
1 parent 168079a commit 2244422
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
28 changes: 28 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2489,3 +2489,31 @@ func (d *DB) SetCreatorID(creatorID uint64) error {
func (d *DB) ObjProvider() objstorage.Provider {
return d.objProvider
}

func (d *DB) checkVirtualBounds(m *fileMetadata, opts *IterOptions, iterOpts internalIterOpts) {
if !invariants.Enabled {
return
}

it, _, err := d.newIters(context.TODO(), m, opts, iterOpts)
if err != nil {
panic("pebble: error retrieving virtual iterator")
}

// check that virtual sstable bounds are tight from both ends.
first, _ := it.First()
if d.cmp(first.UserKey, m.Smallest.UserKey) != 0 {
panic("pebble: virtual sstable lower bound is not tight")
}
last, _ := it.Last()
if d.cmp(last.UserKey, m.Largest.UserKey) != 0 {
panic("pebble: virtual sstable upper bound is not tight")
}

// check that iterator keys are within bounds.
for key, _ := it.First(); key != nil; key, _ = it.Next() {
if d.cmp(key.UserKey, m.Smallest.UserKey) < 0 || d.cmp(key.UserKey, m.Largest.UserKey) > 0 {
panic("pebble: virtual sstable key is not within bounds")
}
}
}
2 changes: 2 additions & 0 deletions table_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ func TestVirtualReadsWiring(t *testing.T) {
v2.SmallestPointKey = v2.Smallest

v1.ValidateVirtual(parentFile)
d.checkVirtualBounds(v1, nil, internalIterOpts{})
v2.ValidateVirtual(parentFile)
d.checkVirtualBounds(v2, nil, internalIterOpts{})

// Write the version edit.
fileMetrics := func(ve *versionEdit) map[int]*LevelMetrics {
Expand Down
4 changes: 4 additions & 0 deletions version_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func TestLatestRefCounting(t *testing.T) {
m2.SmallestPointKey = m2.Smallest

m1.ValidateVirtual(f)
d.checkVirtualBounds(m1, nil, internalIterOpts{})
m2.ValidateVirtual(f)
d.checkVirtualBounds(m2, nil, internalIterOpts{})

fileMetrics := func(ve *versionEdit) map[int]*LevelMetrics {
metrics := newFileMetrics(ve.NewFiles)
Expand Down Expand Up @@ -282,7 +284,9 @@ func TestVirtualSSTableManifestReplay(t *testing.T) {
m2.Stats.NumEntries = 1

m1.ValidateVirtual(f)
d.checkVirtualBounds(m1, nil, internalIterOpts{})
m2.ValidateVirtual(f)
d.checkVirtualBounds(m2, nil, internalIterOpts{})

fileMetrics := func(ve *versionEdit) map[int]*LevelMetrics {
metrics := newFileMetrics(ve.NewFiles)
Expand Down

0 comments on commit 2244422

Please sign in to comment.