Skip to content

Commit a7f95a7

Browse files
committed
kvserver: exclude stateEngine keys from overlapsUnreplicatedRangeIDLocalKeys
We previously made a simplified assumption in overlapsUnreplicatedRangeIDLocalKeys where we treat RangeTombstoneKey and RaftReplicaIDKey as LogEngine keys (even though they are technically stateEngine keys). This commit excludes RangeTombstoneKey and RaftReplicaIDKey from that check. This will be useful when we add Engine assertions (not only eval batch assertions).
1 parent 7662384 commit a7f95a7

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

pkg/kv/kvserver/replica_raftstorage.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,11 @@ func (r *Replica) destroyInfoRaftMuLocked() kvstorage.DestroyReplicaInfo {
880880
}
881881
}
882882

883-
// overlapsUnreplicatedRangeIDLocalKeys checks if the provided span overlaps
884-
// with any unreplicated rangeID local keys.
883+
// overlapsLocalRaftKeys returns an error if the provided span overlaps with any
884+
// unreplicated rangeID local raft keys.
885885
// Note that we could receive the span with a nil startKey, which has a special
886886
// meaning that the span represents: [endKey.Prev(), endKey).
887-
func overlapsUnreplicatedRangeIDLocalKeys(span spanset.TrickySpan) error {
887+
func overlapsLocalRaftKeys(span spanset.TrickySpan) error {
888888
fullRangeIDLocalSpans := roachpb.Span{
889889
Key: keys.LocalRangeIDPrefix.AsRawKey(),
890890
EndKey: keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd(),
@@ -916,14 +916,31 @@ func overlapsUnreplicatedRangeIDLocalKeys(span spanset.TrickySpan) error {
916916
return errors.NewAssertionErrorWithWrappedErrf(err,
917917
"could not decode range ID for span: %s", span)
918918
}
919-
if spanset.Overlaps(roachpb.Span{
919+
920+
// If the span is inside RangeIDLocalSpans but outside RangeIDUnreplicated,
921+
// it cannot overlap local raft keys.
922+
if !spanset.Overlaps(roachpb.Span{
920923
Key: keys.MakeRangeIDUnreplicatedPrefix(rangeID),
921924
EndKey: keys.MakeRangeIDUnreplicatedPrefix(rangeID).PrefixEnd(),
922925
}, span) {
923-
return errors.Errorf("overlapping an unreplicated rangeID span")
926+
return nil
924927
}
925928

926-
return nil
929+
// RangeTombstoneKey and RaftReplicaIDKey are considered part of the
930+
// StateEngine keys. We need to exclude them from the check below.
931+
if span.Key != nil && roachpb.Span(span).Equal(roachpb.Span{
932+
Key: keys.RangeTombstoneKey(rangeID),
933+
}) {
934+
return nil
935+
}
936+
937+
if span.Key != nil && roachpb.Span(span).Equal(roachpb.Span{
938+
Key: keys.RaftReplicaIDKey(rangeID),
939+
}) {
940+
return nil
941+
}
942+
943+
return errors.Errorf("overlapping an unreplicated rangeID span")
927944
}
928945

929946
// overlapsStoreLocalKeys returns an error if the provided span overlaps

pkg/kv/kvserver/replica_read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (r *Replica) executeReadOnlyBatch(
9494
}
9595
if util.RaceEnabled {
9696
spans := g.LatchSpans()
97-
spans.AddForbiddenMatcher(overlapsUnreplicatedRangeIDLocalKeys)
97+
spans.AddForbiddenMatcher(overlapsLocalRaftKeys)
9898
spans.AddForbiddenMatcher(overlapsStoreLocalKeys)
9999
rw = spanset.NewReadWriterAt(rw, spans, ba.Timestamp)
100100
}

pkg/kv/kvserver/replica_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15202,10 +15202,9 @@ func TestLeaderlessWatcherInit(t *testing.T) {
1520215202
}
1520315203
}
1520415204

15205-
// TestOverlapsUnreplicatedRangeIDLocalKeys verifies that the function
15206-
// overlapsUnreplicatedRangeIDLocalKeys() successfully catches any overlap with
15207-
// unreplicated rangeID local keys.
15208-
func TestOverlapsUnreplicatedRangeIDLocalKeys(t *testing.T) {
15205+
// TestOverlapsLocalRaftKeys verifies that the function overlapsLocalRaftKeys()
15206+
// successfully catches any overlap with unreplicated rangeID local raft keys.
15207+
func TestOverlapsLocalRaftKeys(t *testing.T) {
1520915208
defer leaktest.AfterTest(t)()
1521015209
defer log.Scope(t).Close(t)
1521115210

@@ -15230,20 +15229,23 @@ func TestOverlapsUnreplicatedRangeIDLocalKeys(t *testing.T) {
1523015229
{span: s(keys.MakeRangeIDUnreplicatedPrefix(1).PrefixEnd(), roachpb.KeyMax), notOk: true}, // partial overlap
1523115230
{span: s(keys.MakeRangeIDUnreplicatedPrefix(1),
1523215231
keys.MakeRangeIDUnreplicatedPrefix(1).PrefixEnd()), notOk: true},
15232+
{span: s(keys.RangeTombstoneKey(1), keys.RaftTruncatedStateKey(1)), notOk: true},
15233+
{span: s(keys.RaftReplicaIDKey(1), keys.RaftTruncatedStateKey(1)), notOk: true},
1523315234
{span: s(keys.RaftTruncatedStateKey(1), roachpb.KeyMax), notOk: true},
1523415235

1523515236
// Point spans not overlapping with unreplicated local RangeID spans.
1523615237
{span: s(roachpb.KeyMin, nil)},
1523715238
{span: s(keys.LocalRangeIDPrefix.AsRawKey().Prevish(1), nil)},
15238-
{span: s(keys.MakeRangeIDUnreplicatedPrefix(1).Prevish(1), nil)},
1523915239
{span: s(keys.RangeForceFlushKey(1), nil)},
15240+
{span: s(keys.MakeRangeIDUnreplicatedPrefix(1).Prevish(1), nil)},
15241+
{span: s(keys.RangeTombstoneKey(1), nil)},
15242+
{span: s(keys.RaftReplicaIDKey(1), nil)},
1524015243
{span: s(keys.MakeRangeIDUnreplicatedPrefix(1).PrefixEnd(), nil)},
1524115244
{span: s(keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd(), nil)},
1524215245
{span: s(roachpb.KeyMax, nil)},
1524315246

1524415247
// Point spans overlapping with unreplicated local RangeID spans.
1524515248
{span: s(keys.MakeRangeIDUnreplicatedPrefix(1), nil), notOk: true},
15246-
{span: s(keys.RangeTombstoneKey(1), nil), notOk: true},
1524715249
{span: s(keys.RaftTruncatedStateKey(1), nil), notOk: true},
1524815250
{span: s(keys.RaftTruncatedStateKey(2), nil), notOk: true},
1524915251
{span: s(keys.MakeRangeIDUnreplicatedPrefix(1).PrefixEnd().Prevish(1), nil), notOk: true},
@@ -15252,12 +15254,13 @@ func TestOverlapsUnreplicatedRangeIDLocalKeys(t *testing.T) {
1525215254
{span: s(nil, keys.LocalRangeIDPrefix.AsRawKey())},
1525315255
{span: s(nil, keys.MakeRangeIDUnreplicatedPrefix(1))},
1525415256
{span: s(nil, keys.RangeForceFlushKey(1))},
15257+
{span: s(nil, keys.RangeTombstoneKey(1).Next())},
15258+
{span: s(nil, keys.RaftReplicaIDKey(1).Next())},
1525515259
{span: s(nil, keys.MakeRangeIDUnreplicatedPrefix(1).PrefixEnd().Next())},
1525615260
{span: s(nil, keys.LocalRangeIDPrefix.AsRawKey().PrefixEnd().Next())},
1525715261

1525815262
// Tricky spans overlapping with unreplicated local RangeID spans.
1525915263
{span: s(nil, keys.MakeRangeIDUnreplicatedPrefix(1).Next()), notOk: true},
15260-
{span: s(nil, keys.RangeTombstoneKey(1).Next()), notOk: true},
1526115264
{span: s(nil, keys.RaftTruncatedStateKey(1).Next()), notOk: true},
1526215265
{span: s(nil, keys.RaftTruncatedStateKey(2).Next()), notOk: true},
1526315266
{span: s(nil, keys.MakeRangeIDUnreplicatedPrefix(1).PrefixEnd()), notOk: true},
@@ -15266,7 +15269,7 @@ func TestOverlapsUnreplicatedRangeIDLocalKeys(t *testing.T) {
1526615269

1526715270
for _, tc := range testCases {
1526815271
t.Run("", func(t *testing.T) {
15269-
err := overlapsUnreplicatedRangeIDLocalKeys(spanset.TrickySpan(tc.span))
15272+
err := overlapsLocalRaftKeys(spanset.TrickySpan(tc.span))
1527015273
require.Equal(t, tc.notOk, err != nil, tc.span)
1527115274
})
1527215275
}

pkg/kv/kvserver/replica_write.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ func (r *Replica) newBatchedEngine(g *concurrency.Guard) (storage.Batch, *storag
815815
// any write latch would be declared at. But because of this, we don't
816816
// assert on access timestamps using spanset.NewBatchAt.
817817
spans := g.LatchSpans()
818-
spans.AddForbiddenMatcher(overlapsUnreplicatedRangeIDLocalKeys)
818+
spans.AddForbiddenMatcher(overlapsLocalRaftKeys)
819819
spans.AddForbiddenMatcher(overlapsStoreLocalKeys)
820820
batch = spanset.NewBatch(batch, spans)
821821
}

0 commit comments

Comments
 (0)