Skip to content

Commit a37bd6e

Browse files
committed
spanset: recursively disable spanset assertions
This commit changes the way we disable spanset assertionsby doing it recursively. This is because the SpansetBatch follows the decorator design pattern, and allows for multiple wrappers on top of each other. Note that the same recursive disabling was already happening in: DisableReadWriterAssertions and DisableReaderAssertions functions. Also, this commit adds DisableWriterAssertions so that we have the functions for reader, writer, and readwriter.
1 parent dd8a813 commit a37bd6e

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

pkg/kv/kvserver/spanset/batch.go

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,9 @@ func NewBatchAt(b storage.Batch, spans *SpanSet, ts hlc.Timestamp) storage.Batch
964964

965965
// DisableReaderAssertions unwraps any storage.Reader implementations that may
966966
// assert access against a given SpanSet.
967+
// TODO(ibrahim): Eventually we want to eliminate all the users of these generic
968+
// disable functions, and use the specific disable functions
969+
// (DisableUndeclaredSpanAssertions or DisableForbiddenSpanAssertions).
967970
func DisableReaderAssertions(reader storage.Reader) storage.Reader {
968971
switch v := reader.(type) {
969972
case ReadWriter:
@@ -975,10 +978,32 @@ func DisableReaderAssertions(reader storage.Reader) storage.Reader {
975978
}
976979
}
977980

981+
// DisableWriterAssertions unwraps any storage.Writer implementations that may
982+
// assert access against a given SpanSet.
983+
// TODO(ibrahim): Eventually we want to eliminate all the users of these generic
984+
// disable functions, and use the specific disable functions
985+
// (DisableUndeclaredSpanAssertions or DisableForbiddenSpanAssertions).
986+
func DisableWriterAssertions(writer storage.Writer) storage.Writer {
987+
switch v := writer.(type) {
988+
case spanSetWriter:
989+
return DisableWriterAssertions(v.w)
990+
case *spanSetWriteBatch:
991+
return DisableWriterAssertions(v.spanSetWriter.w)
992+
case *spanSetBatch:
993+
return DisableWriterAssertions(v.spanSetWriter.w)
994+
default:
995+
return writer
996+
}
997+
}
998+
978999
// DisableReadWriterAssertions unwraps any storage.ReadWriter implementations
9791000
// that may assert access against a given SpanSet.
1001+
// TODO(ibrahim): Eventually we want to eliminate all the users of these generic
1002+
// disable functions, and use the specific disable functions
1003+
// (DisableUndeclaredSpanAssertions or DisableForbiddenSpanAssertions).
9801004
func DisableReadWriterAssertions(rw storage.ReadWriter) storage.ReadWriter {
9811005
switch v := rw.(type) {
1006+
// TODO(ibrahim): fix the case where one case is a pointer and the other is not.
9821007
case ReadWriter:
9831008
return DisableReadWriterAssertions(v.spanSetWriter.w.(storage.ReadWriter))
9841009
case *spanSetBatch:
@@ -995,9 +1020,18 @@ func DisableReadWriterAssertions(rw storage.ReadWriter) storage.ReadWriter {
9951020
func DisableUndeclaredSpanAssertions(rw storage.ReadWriter) storage.ReadWriter {
9961021
switch v := rw.(type) {
9971022
case *spanSetBatch:
998-
newSnapSetBatch := v.shallowCopy()
999-
newSnapSetBatch.spans.DisableUndeclaredAccessAssertions()
1000-
return newSnapSetBatch
1023+
newSpanSetBatch := v.shallowCopy()
1024+
newSpanSetBatch.spans.DisableUndeclaredAccessAssertions()
1025+
1026+
// Recursively disable on the underlying batch in case there are
1027+
// nested spanSetBatches.
1028+
newSpanSetBatch.b = DisableUndeclaredSpanAssertions(v.b).(storage.Batch)
1029+
// Update the reader and writer to point to the recursively processed batch.
1030+
newSpanSetBatch.spanSetReader.r = newSpanSetBatch.b
1031+
newSpanSetBatch.spanSetWriteBatch.spanSetWriter.w = newSpanSetBatch.b
1032+
newSpanSetBatch.spanSetWriteBatch.wb = newSpanSetBatch.b
1033+
return newSpanSetBatch
1034+
10011035
default:
10021036
return rw
10031037
}
@@ -1011,10 +1045,27 @@ func DisableUndeclaredSpanAssertions(rw storage.ReadWriter) storage.ReadWriter {
10111045
// function.
10121046
func DisableForbiddenSpanAssertions(rw storage.ReadWriter) storage.ReadWriter {
10131047
switch v := rw.(type) {
1048+
// TODO(ibrahim): We eventually want to remove OpLoggerBatch from this switch
1049+
// case.
1050+
case *storage.OpLoggerBatch:
1051+
// OpLoggerBatch embeds a storage.Batch. Recursively process the inner
1052+
// batch and wrap it back in an OpLoggerBatch to preserve the chain.
1053+
innerBatch := DisableForbiddenSpanAssertions(v.Batch).(storage.Batch)
1054+
return v.ShallowCopyWithBatch(innerBatch)
1055+
10141056
case *spanSetBatch:
1015-
newSnapSetBatch := v.shallowCopy()
1016-
newSnapSetBatch.spans.DisableForbiddenSpansAssertions()
1017-
return newSnapSetBatch
1057+
newSpanSetBatch := v.shallowCopy()
1058+
newSpanSetBatch.spans.DisableForbiddenSpansAssertions()
1059+
1060+
// Recursively disable on the underlying batch in case there are
1061+
// nested spanSetBatches.
1062+
newSpanSetBatch.b = DisableForbiddenSpanAssertions(v.b).(storage.Batch)
1063+
// Update the reader and writer to point to the recursively processed batch.
1064+
newSpanSetBatch.spanSetReader.r = newSpanSetBatch.b
1065+
newSpanSetBatch.spanSetWriteBatch.spanSetWriter.w = newSpanSetBatch.b
1066+
newSpanSetBatch.spanSetWriteBatch.wb = newSpanSetBatch.b
1067+
return newSpanSetBatch
1068+
10181069
default:
10191070
return rw
10201071
}

pkg/storage/mvcc_logical_ops.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ func (ol *OpLoggerBatch) LogicalOps() []enginepb.MVCCLogicalOp {
170170
return ol.ops
171171
}
172172

173+
// ShallowCopyWithBatch returns a shallow copy of the OpLoggerBatch with a
174+
// different underlying batch. The ops slice is shared with the original.
175+
func (ol *OpLoggerBatch) ShallowCopyWithBatch(b Batch) *OpLoggerBatch {
176+
cpy := *ol
177+
cpy.Batch = b
178+
return &cpy
179+
}
180+
173181
// DisableOpLogger disables op logging for the given read/writer.
174182
func DisableOpLogger(rw ReadWriter) ReadWriter {
175183
return &noOpLogger{ReadWriter: rw}

0 commit comments

Comments
 (0)