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

storage: randomly mangle range key buffers in test #97222

Merged
merged 1 commit into from
Feb 27, 2023
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: 0 additions & 8 deletions pkg/storage/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,14 +1606,6 @@ func assertSimpleMVCCIteratorInvariants(iter SimpleMVCCIterator) error {
}
}

} else {
// Bounds and range keys must be empty.
if bounds := iter.RangeBounds(); !bounds.Equal(roachpb.Span{}) {
return errors.AssertionFailedf("hasRange=false but RangeBounds=%s", bounds)
}
if r := iter.RangeKeys(); !r.IsEmpty() || !r.Bounds.Equal(roachpb.Span{}) {
return errors.AssertionFailedf("hasRange=false but RangeKeys=%s", r)
}
}
if hasPoint {
value, err := iter.UnsafeValue()
Expand Down
10 changes: 5 additions & 5 deletions pkg/storage/mvcc_history_metamorphic_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (m *metamorphicIterator) moveAround() {
}

hasPoint, _ := m.it.HasPointAndRange()
rangeKeys := m.it.RangeKeys().Clone()
rangeKeys := rangeKeysIfExist(m.it).Clone()
var rangeKeysIgnoringTime storage.MVCCRangeKeyStack
if iit != nil {
rangeKeysIgnoringTime = iit.RangeKeysIgnoringTime()
Expand Down Expand Up @@ -225,7 +225,7 @@ func (m *metamorphicIterator) moveAround() {
if m.it.UnsafeKey().Equal(cur) {
break // made it
}
printfln("step: %s %s [changed=%t]", m.it.UnsafeKey(), m.it.RangeKeys(), m.it.RangeKeyChanged())
printfln("step: %s %s [changed=%t]", m.it.UnsafeKey(), rangeKeysIfExist(m.it), m.it.RangeKeyChanged())
if iit != nil {
// If we're an incremental iterator with time bounds, and `cur` is not within bounds,
// would miss it if we used Next. So call NextIgnoringTime unconditionally.
Expand All @@ -248,7 +248,7 @@ func (m *metamorphicIterator) moveAround() {
valid, err := m.it.Valid()
require.Nil(m.t, err)
require.True(m.t, valid, "unable to recover original position following SeekLT")
printfln("rev-step: %s %s [changed=%t]", m.it.UnsafeKey(), m.it.RangeKeys(), m.it.RangeKeyChanged())
printfln("rev-step: %s %s [changed=%t]", m.it.UnsafeKey(), rangeKeysIfExist(m.it), m.it.RangeKeyChanged())
if m.it.UnsafeKey().Equal(cur) {
printfln("done")
break // made it
Expand All @@ -269,13 +269,13 @@ func (m *metamorphicIterator) moveAround() {
rangeKeysIgnoringTime2 = iit.RangeKeysIgnoringTime()
}
printfln("recovered position: %s hasPoint=%t, rangeKeys=%s, rangeKeysIgnoringTime=%s",
m.it.UnsafeKey(), hasPoint2, m.it.RangeKeys(), rangeKeysIgnoringTime2)
m.it.UnsafeKey(), hasPoint2, rangeKeysIfExist(m.it), rangeKeysIgnoringTime2)
}
// Back where we started and hopefully in an indistinguishable state.
// When the stack is empty, sometimes it's a nil slice and sometimes zero
// slice. A similar problem exists with MVCCRangeKeyVersion.Value. Sidestep
// them by comparing strings.
require.Equal(m.t, fmt.Sprint(rangeKeys), fmt.Sprint(m.it.RangeKeys()))
require.Equal(m.t, fmt.Sprint(rangeKeys), fmt.Sprint(rangeKeysIfExist(m.it)))
if iit != nil {
require.Equal(m.t, fmt.Sprint(rangeKeysIgnoringTime), fmt.Sprint(iit.RangeKeysIgnoringTime()))
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/storage/mvcc_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1982,9 +1982,18 @@ func printIter(e *evalCtx) {
}
}

func rangeKeysIfExist(it storage.SimpleMVCCIterator) storage.MVCCRangeKeyStack {
if valid, err := it.Valid(); !valid || err != nil {
return storage.MVCCRangeKeyStack{}
} else if _, hasRange := it.HasPointAndRange(); !hasRange {
return storage.MVCCRangeKeyStack{}
}
return it.RangeKeys()
}

func checkAndUpdateRangeKeyChanged(e *evalCtx) bool {
rangeKeyChanged := e.iter.RangeKeyChanged()
rangeKeys := e.iter.RangeKeys()
rangeKeys := rangeKeysIfExist(e.iter)

if incrIter := e.tryMVCCIncrementalIter(); incrIter != nil {
// For MVCCIncrementalIterator, make sure RangeKeyChangedIgnoringTime() fires
Expand Down
7 changes: 7 additions & 0 deletions pkg/storage/mvcc_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ type MVCCRangeKeyVersion struct {
Value []byte
}

// CloneInto copies the version into the provided destination
// MVCCRangeKeyVersion, reusing and overwriting its value slice.
func (v MVCCRangeKeyVersion) CloneInto(dst *MVCCRangeKeyVersion) {
dst.Timestamp = v.Timestamp
dst.Value = append(dst.Value[:0], v.Value...)
}

// AsRangeKey returns an MVCCRangeKey for the given version. Byte slices
// are shared with the stack.
func (s MVCCRangeKeyStack) AsRangeKey(v MVCCRangeKeyVersion) MVCCRangeKey {
Expand Down
68 changes: 53 additions & 15 deletions pkg/storage/pebble_mvcc_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
Expand Down Expand Up @@ -441,14 +442,15 @@ type pebbleMVCCScanner struct {
// cur* variables store the "current" record we're pointing to. Updated in
// updateCurrent. Note that the timestamp can be clobbered in the case of
// adding an intent from the intent history but is otherwise meaningful.
curUnsafeKey MVCCKey
curRawKey []byte
curUnsafeValue MVCCValue
curRawValue pebble.LazyValue
curRangeKeys MVCCRangeKeyStack
savedRangeKeys MVCCRangeKeyStack
results results
intents pebble.Batch
curUnsafeKey MVCCKey
curRawKey []byte
curUnsafeValue MVCCValue
curRawValue pebble.LazyValue
curRangeKeys MVCCRangeKeyStack
savedRangeKeys MVCCRangeKeyStack
savedRangeKeyVers MVCCRangeKeyVersion
results results
intents pebble.Batch
// mostRecentTS stores the largest timestamp observed that is equal to or
// above the scan timestamp. Only applicable if failOnMoreRecent is true. If
// set and no other error is hit, a WriteToOld error will be returned from
Expand Down Expand Up @@ -546,20 +548,40 @@ func (p *pebbleMVCCScanner) get(ctx context.Context) {
if !p.iterValid() {
return
}

// Unlike scans, if tombstones are enabled, we synthesize point tombstones
// for MVCC range tombstones even if there is no existing point key below
// it. These are often needed for e.g. conflict checks. However, both
// processRangeKeys and getOne may need to advance the iterator,
// moving away from range key we originally landed on. If we're in tombstone
// mode and there's a range key, save the most recent visible value so that
// we can use it to synthesize a tombstone if we fail to find a KV.
var hadMVCCRangeTombstone bool
if p.tombstones {
if _, hasRange := p.parent.HasPointAndRange(); hasRange {
rangeKeys := p.parent.RangeKeys()
if rkv, ok := rangeKeys.FirstAtOrBelow(p.ts); ok {
hadMVCCRangeTombstone = true
rkv.CloneInto(&p.savedRangeKeyVers)
}
}
}

var added bool
if p.processRangeKeys(true /* seeked */, false /* reverse */) {
if p.updateCurrent() {
_, added = p.getOne(ctx)
}
}
p.maybeFailOnMoreRecent()
// Unlike scans, if tombstones are enabled, we synthesize point tombstones for
// MVCC range tombstones even if there is no existing point key below it.
// These are often needed for e.g. conflict checks.
if p.tombstones && !added && p.err == nil {
if rkv, ok := p.coveredByRangeKey(hlc.MinTimestamp); ok {
p.addSynthetic(ctx, p.curRangeKeys.Bounds.Key, rkv)
}

// In tombstone mode, if there was no existing point key we may need to
// synthesize a point tombstone if we saved a range key before
// Unlike scans, if tombstones are enabled, we synthesize point tombstones
// for MVCC range tombstones even if there is no existing point key below
// it. These are often needed for e.g. conflict checks.
if p.tombstones && hadMVCCRangeTombstone && !added && p.err == nil {
p.addSynthetic(ctx, p.start, p.savedRangeKeyVers)
}
}

Expand Down Expand Up @@ -1658,6 +1680,9 @@ func (p *pebbleMVCCScanner) iterNext() bool {
p.parent.Next()
// We don't need to process range key changes here, because curRangeKeys
// already contains the range keys at this position from before the peek.
if buildutil.CrdbTestBuild {
p.assertOwnedRangeKeys()
}
if !p.iterValid() {
return false
}
Expand Down Expand Up @@ -1834,3 +1859,16 @@ func (p *pebbleMVCCScanner) intentsRepr() []byte {
}
return p.intents.Repr()
}

// assertOwnedRangeKeys asserts that p.curRangeKeys is empty, or backed by
// p.savedRangeKeys's buffers.
func (p *pebbleMVCCScanner) assertOwnedRangeKeys() {
if p.curRangeKeys.IsEmpty() {
return
}
// NB: We compare on the EndKey in case the start key is /Min, the empty
// key.
if &p.curRangeKeys.Bounds.EndKey[0] != &p.savedRangeKeys.Bounds.EndKey[0] {
panic(errors.AssertionFailedf("current range keys are not scanner-owned"))
}
}
1 change: 1 addition & 0 deletions pkg/storage/pebbleiter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/storage/pebbleiter",
visibility = ["//visibility:public"],
deps = [
"//pkg/util",
"@com_github_cockroachdb_errors//:errors",
"@com_github_cockroachdb_pebble//:pebble",
],
Expand Down
Loading