Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db: fix iterator error propagation, NextPrefix violation #3593

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flushable.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ func (s *ingestedFlushable) constructRangeDelIter(
file *manifest.FileMetadata, _ keyspan.SpanIterOptions,
) (keyspan.FragmentIterator, error) {
iters, err := s.newIters(context.Background(), file, nil, internalIterOpts{}, iterRangeDeletions)
// Note that the keyspan level iter expects a non-nil iterator to be
// returned even if there is an error. So, we return iters.RangeDeletion()
// regardless of the value of err.
return iters.RangeDeletion(), err
if err != nil {
return nil, err
}
return iters.RangeDeletion(), nil
}

// newRangeDelIter is part of the flushable interface.
Expand Down
32 changes: 14 additions & 18 deletions internal/keyspan/keyspanimpl/level_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,40 @@ const (
)

func (l *LevelIter) loadFile(file *manifest.FileMetadata, dir int) loadFileReturnIndicator {
indicator := noFileLoaded
if l.iterFile == file {
if l.err != nil {
return noFileLoaded
}
if l.iter != nil {
// We are already at the file, but we would need to check for bounds.
// Set indicator accordingly.
indicator = fileAlreadyLoaded
// We are already at the file.
return fileAlreadyLoaded
}
// We were already at file, but don't have an iterator, probably because the file was
// beyond the iteration bounds. It may still be, but it is also possible that the bounds
// have changed. We handle that below.
}

// Note that LevelIter.Close() can be called multiple times.
if indicator != fileAlreadyLoaded {
if err := l.Close(); err != nil {
return noFileLoaded
}
if err := l.Close(); err != nil {
return noFileLoaded
}

l.iterFile = file
l.iter = nil
if file == nil {
return noFileLoaded
}
if indicator != fileAlreadyLoaded {
l.iter, l.err = l.newIter(file, l.tableOpts)
if l.wrapFn != nil {
l.iter = l.wrapFn(l.iter)
}
l.iter = keyspan.MaybeAssert(l.iter, l.cmp)
indicator = newFileLoaded
}
if l.err != nil {
iter, err := l.newIter(file, l.tableOpts)
if err != nil {
l.err = err
return noFileLoaded
}
return indicator
l.iter = iter
if l.wrapFn != nil {
l.iter = l.wrapFn(l.iter)
}
l.iter = keyspan.MaybeAssert(l.iter, l.cmp)
return newFileLoaded
}

// SeekGE implements keyspan.FragmentIterator.
Expand Down
64 changes: 49 additions & 15 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ func (i *Iterator) First() bool {
i.requiresReposition = false
i.stats.ForwardSeekCount[InterfaceCall]++

i.iterFirstWithinBounds()
i.err = i.iterFirstWithinBounds()
if i.err != nil {
i.iterValidityState = IterExhausted
return false
Expand Down Expand Up @@ -1667,8 +1667,7 @@ func (i *Iterator) Last() bool {
i.requiresReposition = false
i.stats.ReverseSeekCount[InterfaceCall]++

i.iterLastWithinBounds()
if i.err != nil {
if i.err = i.iterLastWithinBounds(); i.err != nil {
i.iterValidityState = IterExhausted
return false
}
Expand Down Expand Up @@ -1741,6 +1740,11 @@ func (i *Iterator) nextPrefix() IterValidityState {
// key.
i.rangeKey.updated = i.rangeKey.hasRangeKey && !i.Valid() && i.opts.rangeKeys()
}
// NextPrefix from an exhausted position is undefined. We keep the exhausted
// position, which provides determinism for the metamorphic tests.
if i.iterValidityState == IterExhausted {
return i.iterValidityState
}

// Although NextPrefix documents that behavior at IterAtLimit is undefined,
// this function handles these cases as a simple prefix-agnostic Next. This
Expand Down Expand Up @@ -1770,7 +1774,10 @@ func (i *Iterator) nextPrefix() IterValidityState {
}
// The Iterator is exhausted and i.iter is positioned before the first
// key. Reposition to point to the first internal key.
i.iterFirstWithinBounds()
if i.err = i.iterFirstWithinBounds(); i.err != nil {
i.iterValidityState = IterExhausted
return i.iterValidityState
}
case iterPosCurReversePaused:
// Positioned at a limit. Implement as a prefix-agnostic Next. See TODO
// up above.
Expand All @@ -1789,7 +1796,15 @@ func (i *Iterator) nextPrefix() IterValidityState {
if i.iterKV == nil {
// We're positioned before the first key. Need to reposition to point to
// the first key.
i.iterFirstWithinBounds()
i.err = i.iterFirstWithinBounds()
if i.iterKV == nil {
i.iterValidityState = IterExhausted
return i.iterValidityState
}
if invariants.Enabled && !i.equal(i.iterKV.K.UserKey, i.key) {
i.opts.getLogger().Fatalf("pebble: invariant violation: First internal iterator from iterPosPrev landed on %q, not %q",
i.iterKV.K.UserKey, i.key)
}
} else {
// Move the internal iterator back onto the user key stored in
// i.key. iterPosPrev guarantees that it's positioned at the last
Expand All @@ -1800,14 +1815,14 @@ func (i *Iterator) nextPrefix() IterValidityState {
// This should only be possible if i.iter.Next() encountered an
// error.
if i.iter.Error() == nil {
i.opts.logger.Fatalf("pebble: invariant violation: Nexting internal iterator from iterPosPrev found nothing")
i.opts.getLogger().Fatalf("pebble: invariant violation: Nexting internal iterator from iterPosPrev found nothing")
}
// NB: Iterator.Error() will return i.iter.Error().
i.iterValidityState = IterExhausted
return i.iterValidityState
}
if invariants.Enabled && !i.equal(i.iterKV.K.UserKey, i.key) {
i.opts.logger.Fatalf("pebble: invariant violation: Nexting internal iterator from iterPosPrev landed on %q, not %q",
i.opts.getLogger().Fatalf("pebble: invariant violation: Nexting internal iterator from iterPosPrev landed on %q, not %q",
i.iterKV.K.UserKey, i.key)
}
}
Expand Down Expand Up @@ -1850,6 +1865,10 @@ func (i *Iterator) internalNextPrefix(currKeyPrefixLen int) {
}
i.stats.ForwardStepCount[InternalIterCall]++
i.prefixOrFullSeekKey = i.comparer.ImmediateSuccessor(i.prefixOrFullSeekKey[:0], i.key[:currKeyPrefixLen])
if i.iterKV.K.IsExclusiveSentinel() {
panic(errors.AssertionFailedf("pebble: unexpected exclusive sentinel key: %q", i.iterKV.K))
}

i.iterKV = i.iter.NextPrefix(i.prefixOrFullSeekKey)
if invariants.Enabled && i.iterKV != nil {
if iterKeyPrefixLen := i.split(i.iterKV.K.UserKey); i.cmp(i.iterKV.K.UserKey[:iterKeyPrefixLen], i.prefixOrFullSeekKey) < 0 {
Expand Down Expand Up @@ -1913,7 +1932,10 @@ func (i *Iterator) nextWithLimit(limit []byte) IterValidityState {
}
// We're positioned before the first key. Need to reposition to point to
// the first key.
i.iterFirstWithinBounds()
if i.err = i.iterFirstWithinBounds(); i.err != nil {
i.iterValidityState = IterExhausted
return i.iterValidityState
}
case iterPosCurReversePaused:
// Switching directions.
// The iterator must not be exhausted since it paused.
Expand All @@ -1933,10 +1955,14 @@ func (i *Iterator) nextWithLimit(limit []byte) IterValidityState {
if i.iterKV == nil {
// We're positioned before the first key. Need to reposition to point to
// the first key.
i.iterFirstWithinBounds()
i.err = i.iterFirstWithinBounds()
} else {
i.nextUserKey()
}
if i.err != nil {
i.iterValidityState = IterExhausted
return i.iterValidityState
}
i.nextUserKey()
case iterPosNext:
// Already at the right place.
Expand Down Expand Up @@ -2031,12 +2057,12 @@ func (i *Iterator) PrevWithLimit(limit []byte) IterValidityState {
if i.iterKV == nil {
// We're positioned after the last key. Need to reposition to point to
// the last key.
i.iterLastWithinBounds()
i.err = i.iterLastWithinBounds()
} else {
i.prevUserKey()
if i.err != nil {
return i.iterValidityState
}
}
if i.err != nil {
return i.iterValidityState
}
if stepAgain {
i.prevUserKey()
Expand All @@ -2052,24 +2078,32 @@ func (i *Iterator) PrevWithLimit(limit []byte) IterValidityState {

// iterFirstWithinBounds moves the internal iterator to the first key,
// respecting bounds.
func (i *Iterator) iterFirstWithinBounds() {
func (i *Iterator) iterFirstWithinBounds() error {
i.stats.ForwardSeekCount[InternalIterCall]++
if lowerBound := i.opts.GetLowerBound(); lowerBound != nil {
i.iterKV = i.iter.SeekGE(lowerBound, base.SeekGEFlagsNone)
} else {
i.iterKV = i.iter.First()
}
if i.iterKV == nil {
return i.iter.Error()
}
return nil
}

// iterLastWithinBounds moves the internal iterator to the last key, respecting
// bounds.
func (i *Iterator) iterLastWithinBounds() {
func (i *Iterator) iterLastWithinBounds() error {
i.stats.ReverseSeekCount[InternalIterCall]++
if upperBound := i.opts.GetUpperBound(); upperBound != nil {
i.iterKV = i.iter.SeekLT(upperBound, base.SeekLTFlagsNone)
} else {
i.iterKV = i.iter.Last()
}
if i.iterKV == nil {
return i.iter.Error()
}
return nil
}

// RangeKeyData describes a range key's data, set through RangeKeySet. The key
Expand Down
Loading