Skip to content

Commit b868ee0

Browse files
committed
storage: use AssertionFailedf for panics
Update a few panics that threw a simple string to throw an error constructed through AssertionFailedf. Where applicable, this error is propagated through a return value instead. This is motivated by #150058, an instance where the panic string gets redacted, requiring a bit more work to identify the origin of the panic (through mapping the line number on the appropriate commit SHA). Epic: none Release note: none
1 parent 54eb1df commit b868ee0

12 files changed

+124
-87
lines changed

pkg/storage/backup_compaction_iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (f *BackupCompactionIterator) ValueLen() int {
116116
func (f *BackupCompactionIterator) HasPointAndRange() (bool, bool) {
117117
hasPoint, hasRange := f.iter.HasPointAndRange()
118118
if hasRange {
119-
panic("unexpected range tombstone")
119+
panic(errors.AssertionFailedf("unexpected range tombstone"))
120120
}
121121
return hasPoint, hasRange
122122
}

pkg/storage/batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (r *BatchReader) EngineKey() (EngineKey, error) {
128128
func (r *BatchReader) Value() []byte {
129129
switch r.kind {
130130
case pebble.InternalKeyKindDelete, pebble.InternalKeyKindSingleDelete:
131-
panic("cannot call Value on a deletion entry")
131+
panic(errors.AssertionFailedf("cannot call Value on a deletion entry"))
132132
default:
133133
return r.value
134134
}

pkg/storage/batch_test.go

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,55 @@ func TestBatchBasics(t *testing.T) {
147147
}
148148

149149
func shouldPanic(t *testing.T, f func(), funcName string, expectedPanicStr string) {
150+
t.Helper()
150151
defer func() {
152+
t.Helper()
151153
if r := recover(); r == nil {
152154
t.Fatalf("%v: test did not panic", funcName)
153-
} else if r != expectedPanicStr {
154-
t.Fatalf("%v: unexpected panic: %v", funcName, r)
155+
} else if err, ok := r.(error); ok {
156+
if errMsg := err.Error(); errMsg != expectedPanicStr {
157+
t.Fatalf("%v: unexpected error panic: %q ≠ %q", funcName, errMsg, expectedPanicStr)
158+
}
159+
} else if errMsg, ok := r.(string); ok && errMsg != expectedPanicStr {
160+
t.Fatalf("%v: unexpected panic: %q ≠ %q", funcName, errMsg, expectedPanicStr)
155161
}
156162
}()
157163
f()
158164
}
159165

160-
func shouldNotPanic(t *testing.T, f func(), funcName string) {
166+
func shouldPanicOrErr(t *testing.T, f func() error, funcName string, expectedPanicStr string) {
167+
t.Helper()
168+
var err error
169+
defer func() {
170+
t.Helper()
171+
if r := recover(); r != nil {
172+
if e, ok := r.(error); ok {
173+
err = e
174+
} else {
175+
panic(r)
176+
}
177+
}
178+
}()
179+
err = f()
180+
if err == nil {
181+
t.Fatalf("%v: test did not panic", funcName)
182+
} else if err.Error() != expectedPanicStr {
183+
t.Fatalf("%v: unexpected panic: %q ≠ %q", funcName, err, expectedPanicStr)
184+
}
185+
}
186+
187+
func shouldNotPanicOrErr(t *testing.T, f func() error, funcName string) {
188+
t.Helper()
161189
defer func() {
190+
t.Helper()
162191
if r := recover(); r != nil {
163192
t.Fatalf("%v: unexpected panic: %v", funcName, r)
164193
}
165194
}()
166-
f()
195+
err := f()
196+
if err != nil {
197+
t.Fatalf("%v: unexpected error: %v", funcName, err)
198+
}
167199
}
168200

169201
// TestReadOnlyBasics verifies that for a read-only ReadWriter (obtained via
@@ -181,23 +213,28 @@ func TestReadOnlyBasics(t *testing.T) {
181213
t.Fatal("read-only is expectedly found to be closed")
182214
}
183215
a := mvccKey("a")
184-
successTestCases := []func(){
185-
func() {
186-
_ = ro.MVCCIterate(context.Background(), a.Key, a.Key, MVCCKeyIterKind, IterKeyTypePointsOnly,
216+
successTestCases := []func() error{
217+
func() error {
218+
return ro.MVCCIterate(context.Background(), a.Key, a.Key, MVCCKeyIterKind, IterKeyTypePointsOnly,
187219
fs.UnknownReadCategory,
188220
func(MVCCKeyValue, MVCCRangeKeyStack) error { return iterutil.StopIteration() })
189221
},
190-
func() {
191-
iter, _ := ro.NewMVCCIterator(context.Background(), MVCCKeyIterKind, IterOptions{UpperBound: roachpb.KeyMax})
222+
func() error {
223+
iter, err := ro.NewMVCCIterator(context.Background(), MVCCKeyIterKind, IterOptions{UpperBound: roachpb.KeyMax})
192224
iter.Close()
225+
return err
193226
},
194-
func() {
195-
iter, _ := ro.NewMVCCIterator(context.Background(), MVCCKeyIterKind, IterOptions{
227+
func() error {
228+
iter, err := ro.NewMVCCIterator(context.Background(), MVCCKeyIterKind, IterOptions{
196229
MinTimestamp: hlc.MinTimestamp,
197230
MaxTimestamp: hlc.MaxTimestamp,
198231
UpperBound: roachpb.KeyMax,
199232
})
233+
if err != nil {
234+
return err
235+
}
200236
iter.Close()
237+
return nil
201238
},
202239
}
203240
defer func() {
@@ -207,25 +244,25 @@ func TestReadOnlyBasics(t *testing.T) {
207244
}
208245
shouldPanic(t, func() { ro.Close() }, "Close", "closing an already-closed pebbleReadOnly")
209246
for i, f := range successTestCases {
210-
shouldPanic(t, f, strconv.Itoa(i), "using a closed pebbleReadOnly")
247+
shouldPanicOrErr(t, f, strconv.Itoa(i), "using a closed pebbleReadOnly")
211248
}
212249
}()
213250

214251
for i, f := range successTestCases {
215-
shouldNotPanic(t, f, strconv.Itoa(i))
252+
shouldNotPanicOrErr(t, f, strconv.Itoa(i))
216253
}
217254

218255
// For a read-only ReadWriter, all Writer methods should panic.
219-
failureTestCases := []func(){
220-
func() { _ = ro.ApplyBatchRepr(nil, false) },
221-
func() { _ = ro.ClearUnversioned(a.Key, ClearOptions{}) },
222-
func() { _ = ro.SingleClearEngineKey(EngineKey{Key: a.Key}) },
223-
func() { _ = ro.ClearRawRange(a.Key, a.Key, true, true) },
224-
func() { _ = ro.Merge(a, nil) },
225-
func() { _ = ro.PutUnversioned(a.Key, nil) },
256+
failureTestCases := []func() error{
257+
func() error { return ro.ApplyBatchRepr(nil, false) },
258+
func() error { return ro.ClearUnversioned(a.Key, ClearOptions{}) },
259+
func() error { return ro.SingleClearEngineKey(EngineKey{Key: a.Key}) },
260+
func() error { return ro.ClearRawRange(a.Key, a.Key, true, true) },
261+
func() error { return ro.Merge(a, nil) },
262+
func() error { return ro.PutUnversioned(a.Key, nil) },
226263
}
227264
for i, f := range failureTestCases {
228-
shouldPanic(t, f, strconv.Itoa(i), "not implemented")
265+
shouldPanicOrErr(t, f, strconv.Itoa(i), "not implemented")
229266
}
230267

231268
if err := e.PutUnversioned(mvccKey("a").Key, []byte("value")); err != nil {

pkg/storage/fs/file_registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (r *FileRegistry) maybeElideEntries(ctx context.Context) error {
316316
for _, filename := range filenames {
317317
entry, ok := r.writeMu.mu.entries[filename]
318318
if !ok {
319-
panic("entry disappeared from map")
319+
panic(errors.AssertionFailedf("entry disappeared from map"))
320320
}
321321

322322
// Some entries may be elided. This is used within

pkg/storage/intent_interleaving_iter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (imr *intentInterleavingReader) NewMVCCIterator(
6767
) (MVCCIterator, error) {
6868
if (!opts.MinTimestamp.IsEmpty() || !opts.MaxTimestamp.IsEmpty()) &&
6969
iterKind == MVCCKeyAndIntentsIterKind {
70-
panic("cannot ask for interleaved intents when specifying timestamp hints")
70+
return nil, errors.AssertionFailedf("cannot ask for interleaved intents when specifying timestamp hints")
7171
}
7272
if iterKind == MVCCKeyIterKind || opts.KeyTypes == IterKeyTypeRangesOnly {
7373
return imr.wrappableReader.NewMVCCIterator(ctx, MVCCKeyIterKind, opts)
@@ -232,7 +232,7 @@ func newIntentInterleavingIterator(
232232
ctx context.Context, reader Reader, opts IterOptions,
233233
) (MVCCIterator, error) {
234234
if !opts.MinTimestamp.IsEmpty() || !opts.MaxTimestamp.IsEmpty() {
235-
panic("intentInterleavingIter must not be used with timestamp hints")
235+
return nil, errors.AssertionFailedf("intentInterleavingIter must not be used with timestamp hints")
236236
}
237237
var lowerIsLocal, upperIsLocal bool
238238
var constraint intentInterleavingIterConstraint
@@ -260,7 +260,7 @@ func newIntentInterleavingIterator(
260260
if !opts.Prefix {
261261
if opts.LowerBound == nil && opts.UpperBound == nil {
262262
// This is the same requirement as pebbleIterator.
263-
panic("iterator must set prefix or upper bound or lower bound")
263+
return nil, errors.AssertionFailedf("iterator must set prefix or upper bound or lower bound")
264264
}
265265
// At least one bound is specified, so constraint != notConstrained. But
266266
// may need to manufacture a bound for the currently unbounded side.

pkg/storage/lock_table_iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (i *LockTableIterator) advanceToMatchingLock(
301301
// zero UUID if we are in this branch, with the iterator positioned
302302
// after the matchTxnID. Assert for good measure.
303303
if i.matchTxnID == uuid.Nil {
304-
panic("matchTxnID is unexpectedly the zero UUID")
304+
panic(errors.AssertionFailedf("matchTxnID is unexpectedly the zero UUID"))
305305
}
306306
ltKey.TxnUUID = uuid.FromUint128(i.matchTxnID.ToUint128().Sub(1))
307307
seekKey, *seekKeyBuf = ltKey.ToEngineKey(*seekKeyBuf)

pkg/storage/mvcc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ func updateStatsOnClear(
10051005
// restoredNanos to orig.Timestamp (rule 1).
10061006
if restored != nil {
10071007
if restored.Txn != nil {
1008-
panic("restored version should never be an intent")
1008+
panic(errors.AssertionFailedf("restored version should never be an intent"))
10091009
}
10101010

10111011
ms.AgeTo(restoredNanos)

pkg/storage/open.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func WALFailover(
354354
return nil
355355
}
356356
default:
357-
panic("unreachable")
357+
panic(errors.AssertionFailedf("unreachable"))
358358
}
359359
}
360360

@@ -382,7 +382,7 @@ func WALFailover(
382382
case storageconfig.WALFailoverAmongStores:
383383
// Fallthrough
384384
default:
385-
panic("unreachable")
385+
panic(errors.AssertionFailedf("unreachable"))
386386
}
387387

388388
// Either

0 commit comments

Comments
 (0)