Skip to content

Commit c8e6a23

Browse files
committed
spanset: assert that batches don't access store local and unreplicated RangeID local keys
This commit adds the following test-only assertions: 1) Generated batches don't touch store-local keys. 2) Generated batches don't touch unreplicated RangeID local keys. We disable the check in exactly 3 locations we know that we currently touch those keys.
1 parent ad834d9 commit c8e6a23

File tree

7 files changed

+215
-4
lines changed

7 files changed

+215
-4
lines changed

pkg/keys/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ var (
163163
//
164164
// LocalStorePrefix is the prefix identifying per-store data.
165165
LocalStorePrefix = makeKey(LocalPrefix, roachpb.Key("s"))
166+
LocalStoreMax = roachpb.Key(LocalStorePrefix).PrefixEnd()
166167
// localStoreClusterVersionSuffix stores the cluster-wide version
167168
// information for this store, updated any time the operator
168169
// updates the minimum cluster version.

pkg/keys/keys.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,21 @@ func DecodeRangeIDKey(
308308
return roachpb.RangeID(rangeInt), infix, suffix, b, nil
309309
}
310310

311+
// DecodeRangeIDPrefix parses a local range ID prefix into range ID.
312+
func DecodeRangeIDPrefix(key roachpb.Key) (roachpb.RangeID, error) {
313+
if !bytes.HasPrefix(key, LocalRangeIDPrefix) {
314+
return 0, errors.Errorf("key %s does not have %s prefix", key, LocalRangeIDPrefix)
315+
}
316+
// Cut the prefix, the Range ID, and the infix specifier.
317+
b := key[len(LocalRangeIDPrefix):]
318+
_, rangeInt, err := encoding.DecodeUvarintAscending(b)
319+
if err != nil {
320+
return 0, err
321+
}
322+
323+
return roachpb.RangeID(rangeInt), nil
324+
}
325+
311326
// AbortSpanKey returns a range-local key by Range ID for an
312327
// AbortSpan entry, with detail specified by encoding the
313328
// supplied transaction ID.

pkg/kv/kvserver/batcheval/cmd_end_transaction.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,8 +1340,10 @@ func splitTriggerHelper(
13401340
if err != nil {
13411341
return enginepb.MVCCStats{}, result.Result{}, errors.Wrap(err, "unable to fetch last replica GC timestamp")
13421342
}
1343+
13431344
if err := storage.MVCCPutProto(
1344-
ctx, batch, keys.RangeLastReplicaGCTimestampKey(split.RightDesc.RangeID), hlc.Timestamp{},
1345+
ctx, spanset.DisableForbiddenSpanAssertionsOnBatch(batch),
1346+
keys.RangeLastReplicaGCTimestampKey(split.RightDesc.RangeID), hlc.Timestamp{},
13451347
&replicaGCTS, storage.MVCCWriteOptions{Category: fs.BatchEvalReadCategory}); err != nil {
13461348
return enginepb.MVCCStats{}, result.Result{}, errors.Wrap(err, "unable to copy last replica GC timestamp")
13471349
}
@@ -1541,7 +1543,8 @@ func splitTriggerHelper(
15411543
// as all replicas will be responsible for writing it locally before
15421544
// applying the split.
15431545
if !rec.ClusterSettings().Version.IsActive(ctx, clusterversion.V25_4_WriteInitialTruncStateBeforeSplitApplication) {
1544-
if err := kvstorage.WriteInitialTruncState(ctx, batch, split.RightDesc.RangeID); err != nil {
1546+
if err := kvstorage.WriteInitialTruncState(ctx,
1547+
spanset.DisableForbiddenSpanAssertionsOnBatch(batch), split.RightDesc.RangeID); err != nil {
15451548
return enginepb.MVCCStats{}, result.Result{}, errors.Wrap(err, "unable to write initial Replica state")
15461549
}
15471550
}

pkg/kv/kvserver/batcheval/cmd_truncate_log.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ func TruncateLog(
118118
// are not tracked in the raft log delta. The delta will be adjusted below
119119
// raft.
120120
// We can pass zero as nowNanos because we're only interested in SysBytes.
121-
ms, err := storage.ComputeStats(ctx, readWriter, start, end, 0 /* nowNanos */)
121+
// TODO(#157895): Use the log engine here instead of the state machine engine.
122+
ms, err := storage.ComputeStats(ctx,
123+
spanset.DisableForbiddenSpanAssertionsOnBatch(readWriter), start, end, 0 /* nowNanos */)
122124
if err != nil {
123125
return result.Result{}, errors.Wrap(err, "while computing stats of Raft log freed by truncation")
124126
}

pkg/kv/kvserver/replica_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15197,3 +15197,123 @@ func TestLeaderlessWatcherInit(t *testing.T) {
1519715197
t.Fatalf("expected LeaderlessWatcher channel to be closed")
1519815198
}
1519915199
}
15200+
15201+
// TestOverlapsUnreplicatedRangeIDLocalKeys verifies that the function
15202+
// overlapsUnreplicatedRangeIDLocalKeys() successfully catches any overlap with
15203+
// unreplicated rangeID local keys.
15204+
func TestOverlapsUnreplicatedRangeIDLocalKeys(t *testing.T) {
15205+
defer leaktest.AfterTest(t)()
15206+
defer log.Scope(t).Close(t)
15207+
15208+
s := func(start, end roachpb.Key) roachpb.Span {
15209+
return roachpb.Span{Key: start, EndKey: end}
15210+
}
15211+
15212+
testCases := []struct {
15213+
span roachpb.Span
15214+
notOk bool
15215+
}{
15216+
// Full spans not overlapping with unreplicated local RangeID spans.
15217+
{span: s(roachpb.KeyMin, keys.LocalRangeIDPrefix.AsRawKey())},
15218+
{span: s(keys.RangeForceFlushKey(1), keys.RangeLeaseKey(1))},
15219+
{span: s(keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd(), roachpb.KeyMax)},
15220+
15221+
// Full spans overlapping with unreplicated local RangeID spans.
15222+
{span: s(roachpb.KeyMin, keys.RaftTruncatedStateKey(1)), notOk: true},
15223+
{span: s(keys.LocalRangeIDPrefix.AsRawKey(), keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd()),
15224+
notOk: true},
15225+
{span: s(keys.RaftTruncatedStateKey(1), keys.RaftTruncatedStateKey(2)), notOk: true},
15226+
{span: s(keys.RaftTruncatedStateKey(1), roachpb.KeyMax), notOk: true},
15227+
15228+
// Point spans not overlapping with unreplicated local RangeID spans.
15229+
{span: s(roachpb.KeyMin, nil)},
15230+
{span: s(keys.LocalRangeIDPrefix.AsRawKey().Prevish(1), nil)},
15231+
{span: s(keys.RangeForceFlushKey(1), nil)},
15232+
{span: s(keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd(), nil)},
15233+
{span: s(roachpb.KeyMax, nil)},
15234+
15235+
// Point spans overlapping with unreplicated local RangeID spans.
15236+
{span: s(keys.RangeTombstoneKey(1), nil), notOk: true},
15237+
{span: s(keys.RaftTruncatedStateKey(1), nil), notOk: true},
15238+
{span: s(keys.RaftTruncatedStateKey(2), nil), notOk: true},
15239+
15240+
// Tricky spans not overlapping with unreplicated local RangeID spans.
15241+
{span: s(nil, keys.LocalRangeIDPrefix.AsRawKey())},
15242+
{span: s(nil, keys.RangeForceFlushKey(1))},
15243+
{span: s(nil, keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd().Next())},
15244+
15245+
// Tricky spans overlapping with unreplicated local RangeID spans.
15246+
{span: s(nil, keys.RangeTombstoneKey(1).Next()), notOk: true},
15247+
{span: s(nil, keys.RaftTruncatedStateKey(1).Next()), notOk: true},
15248+
{span: s(nil, keys.RaftTruncatedStateKey(2).Next()), notOk: true},
15249+
}
15250+
15251+
for _, tc := range testCases {
15252+
t.Run("", func(t *testing.T) {
15253+
err := overlapsUnreplicatedRangeIDLocalKeys(spanset.TrickySpan(tc.span))
15254+
if tc.notOk {
15255+
require.Errorf(t, err, "expected error for span %s", tc.span)
15256+
} else {
15257+
require.NoErrorf(t, err, "expected no error for span %s", tc.span)
15258+
}
15259+
})
15260+
}
15261+
}
15262+
15263+
// TestOverlapsStoreLocalKeys verifies that the function
15264+
// overlapsStoreLocalKeys() successfully catches any overlap with
15265+
// store local keys.
15266+
func TestOverlapsStoreLocalKeys(t *testing.T) {
15267+
defer leaktest.AfterTest(t)()
15268+
defer log.Scope(t).Close(t)
15269+
15270+
s := func(start, end roachpb.Key) roachpb.Span {
15271+
return roachpb.Span{Key: start, EndKey: end}
15272+
}
15273+
15274+
testCases := []struct {
15275+
span roachpb.Span
15276+
notOK bool
15277+
}{
15278+
// Full spans not overlapping with Store-local span.
15279+
{span: s(roachpb.KeyMin, keys.LocalStorePrefix)},
15280+
{span: s(keys.LocalStoreMax, roachpb.KeyMax)},
15281+
15282+
// Full spans overlapping with Store-local span.
15283+
{span: s(roachpb.KeyMin, roachpb.Key(keys.LocalStorePrefix).Next()), notOK: true},
15284+
{span: s(keys.LocalStorePrefix, keys.LocalStoreMax), notOK: true},
15285+
{span: s(keys.StoreGossipKey(), keys.StoreIdentKey()), notOK: true},
15286+
{span: s(keys.LocalStoreMax.Prevish(1), roachpb.KeyMax), notOK: true},
15287+
15288+
// Point spans not overlapping with Store-local span.
15289+
{span: s(roachpb.KeyMin, nil)},
15290+
{span: s(roachpb.Key(keys.LocalStorePrefix).Prevish(1), nil)},
15291+
{span: s(keys.LocalStoreMax.Next(), nil)},
15292+
{span: s(roachpb.KeyMax, nil)},
15293+
15294+
// Point spans overlapping with Store-local span.
15295+
{span: s(keys.LocalStorePrefix, nil), notOK: true},
15296+
{span: s(keys.StoreGossipKey(), nil), notOK: true},
15297+
{span: s(keys.LocalStoreMax.Prevish(1), nil), notOK: true},
15298+
15299+
// Tricky spans with nil StartKey not overlapping with Store-local span.
15300+
{span: s(nil, keys.LocalStorePrefix)},
15301+
{span: s(nil, keys.LocalStoreMax.Next())},
15302+
15303+
// Tricky spans with nil StartKey overlapping with Store-local span.
15304+
{span: s(nil, roachpb.Key(keys.LocalStorePrefix).Next()), notOK: true},
15305+
{span: s(nil, keys.StoreGossipKey()), notOK: true},
15306+
{span: s(nil, keys.LocalStoreMax), notOK: true},
15307+
}
15308+
15309+
for _, tc := range testCases {
15310+
t.Run("", func(t *testing.T) {
15311+
err := overlapsStoreLocalKeys(spanset.TrickySpan(tc.span))
15312+
if tc.notOK {
15313+
require.Errorf(t, err, "expected error for span %s", tc.span)
15314+
} else {
15315+
require.NoErrorf(t, err, "expected no error for span %s", tc.span)
15316+
}
15317+
})
15318+
}
15319+
}

pkg/kv/kvserver/replica_write.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111
"time"
1212

13+
"github.com/cockroachdb/cockroach/pkg/keys"
1314
"github.com/cockroachdb/cockroach/pkg/kv"
1415
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
1516
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval"
@@ -814,8 +815,12 @@ func (r *Replica) newBatchedEngine(g *concurrency.Guard) (storage.Batch, *storag
814815
// safe as we're only ever writing at timestamps higher than the timestamp
815816
// any write latch would be declared at. But because of this, we don't
816817
// assert on access timestamps using spanset.NewBatchAt.
817-
batch = spanset.NewBatch(batch, g.LatchSpans())
818+
spans := g.LatchSpans()
819+
spans.AddForbiddenMatcher(overlapsUnreplicatedRangeIDLocalKeys)
820+
spans.AddForbiddenMatcher(overlapsStoreLocalKeys)
821+
batch = spanset.NewBatch(batch, spans)
818822
}
823+
819824
return batch, opLogger
820825
}
821826

@@ -884,3 +889,66 @@ func releaseMVCCStats(ms *enginepb.MVCCStats) {
884889
ms.Reset()
885890
mvccStatsPool.Put(ms)
886891
}
892+
893+
// overlapsUnreplicatedRangeIDLocalKeys checks if the provided span overlaps
894+
// with any unreplicated rangeID local keys.
895+
// Note that we could receive the span with a nil startKey, which has a special
896+
// meaning that the span represents: [endKey.Prev(), endKey).
897+
func overlapsUnreplicatedRangeIDLocalKeys(span spanset.TrickySpan) error {
898+
fullRangeIDLocalSpans := roachpb.Span{
899+
Key: keys.LocalRangeIDPrefix.AsRawKey(),
900+
EndKey: keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd(),
901+
}
902+
903+
// If the provided span is completely outside the rangeID local spans for any
904+
// rangeID, then there is no overlap with any rangeID local keys.
905+
if !spanset.Overlaps(fullRangeIDLocalSpans, span) {
906+
return nil
907+
}
908+
909+
// At this point, we know that we overlap with fullRangeIDLocalSpans. If we
910+
// are not completely within fullRangeIDLocalSpans, return an error as we
911+
// collide with at least one unreplicated RangeIDLocal key.
912+
if !spanset.Contains(fullRangeIDLocalSpans, span) {
913+
return errors.Errorf("overlapping an unreplicated rangeID key")
914+
}
915+
916+
// If the span in inside fullRangeIDLocalSpans, we expect that both start and
917+
// end keys should be in the same rangeID.
918+
rangeIDKey := span.Key
919+
if rangeIDKey == nil {
920+
rangeIDKey = span.EndKey
921+
}
922+
923+
rangeID, err := keys.DecodeRangeIDPrefix(rangeIDKey)
924+
if err != nil {
925+
return errors.NewAssertionErrorWithWrappedErrf(err,
926+
"could not decode range ID for span: %s", span)
927+
}
928+
929+
if spanset.Overlaps(roachpb.Span{
930+
Key: keys.MakeRangeIDUnreplicatedPrefix(rangeID),
931+
EndKey: keys.MakeRangeIDUnreplicatedPrefix(rangeID).PrefixEnd(),
932+
}, span) {
933+
return errors.Errorf("overlapping an unreplicated rangeID span")
934+
}
935+
936+
return nil
937+
}
938+
939+
// overlapsStoreLocalKeys returns an error if the provided span overlaps
940+
// with any store local keys.
941+
// Note that we could receive the span with a nil startKey, which has a special
942+
// meaning that the span represents: [endKey.Prev(), endKey).
943+
func overlapsStoreLocalKeys(span spanset.TrickySpan) error {
944+
localStoreSpan := roachpb.Span{
945+
Key: keys.LocalStorePrefix,
946+
EndKey: keys.LocalStoreMax,
947+
}
948+
949+
if spanset.Overlaps(localStoreSpan, span) {
950+
return errors.Errorf("overlaps with store local keys")
951+
}
952+
953+
return nil
954+
}

pkg/kv/kvserver/spanset/batch.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ func DisableLatchAssertions(rw storage.ReadWriter) storage.ReadWriter {
923923
// forbidden span assertions disabled. It does not modify the original batch.
924924
// The returned batch shares the same underlying storage.Batch but has its own
925925
// SpanSet wrapper with the forbidden span assertion disabled.
926+
// TODO(ibrahim): We eventually want to eliminate all the users of this
927+
// function.
926928
func DisableForbiddenSpanAssertionsOnBatch(rw storage.ReadWriter) storage.ReadWriter {
927929
switch v := rw.(type) {
928930
case *spanSetBatch:

0 commit comments

Comments
 (0)