@@ -13,6 +13,7 @@ import (
1313 "time"
1414
1515 "github.com/cockroachdb/cockroach/pkg/util"
16+ "github.com/cockroachdb/cockroach/pkg/util/randutil"
1617 "github.com/cockroachdb/errors"
1718 "github.com/cockroachdb/pebble"
1819)
@@ -26,12 +27,14 @@ type Iterator = *assertionIter
2627// MaybeWrap returns the provided Pebble iterator, wrapped with double close
2728// detection.
2829func MaybeWrap (iter * pebble.Iterator ) Iterator {
29- return & assertionIter {Iterator : iter , closedCh : make (chan struct {})}
30+ rng , _ := randutil .NewPseudoRand ()
31+ return & assertionIter {Iterator : iter , rng : rng , closedCh : make (chan struct {})}
3032}
3133
3234// assertionIter wraps a *pebble.Iterator with assertion checking.
3335type assertionIter struct {
3436 * pebble.Iterator
37+ rng * rand.Rand
3538 closed bool
3639 closedCh chan struct {}
3740 // unsafeBufs hold buffers used for returning values with short lifetimes to
@@ -243,11 +246,11 @@ func (i *assertionIter) PrevWithLimit(limit []byte) pebble.IterValidityState {
243246// to the caller. This is used to ensure that the client respects the Pebble
244247// iterator interface and the lifetimes of buffers it returns.
245248func (i * assertionIter ) maybeMangleBufs () {
246- if rand .Intn (2 ) == 0 {
249+ if i . rng .Intn (2 ) == 0 {
247250 idx := i .unsafeBufs .idx
248251 zero (i .unsafeBufs .key [idx ])
249252 zero (i .unsafeBufs .val [idx ])
250- if rand .Intn (2 ) == 0 {
253+ if i . rng .Intn (2 ) == 0 {
251254 // Switch to a new buffer for the next iterator position.
252255 i .unsafeBufs .idx = (i .unsafeBufs .idx + 1 ) % 2
253256 }
@@ -273,7 +276,7 @@ func (i *assertionIter) maybeSaveAndMangleRangeKeyBufs() {
273276 // Randomly zero them to ensure we catch bugs where they're reused.
274277 idx := i .rangeKeyBufs .idx
275278 mangleBuf := & i .rangeKeyBufs .bufs [idx ]
276- if rand .Intn (2 ) == 0 {
279+ if i . rng .Intn (2 ) == 0 {
277280 mangleBuf .mangle ()
278281 }
279282 // If the new iterator position has range keys, copy them to our buffers.
@@ -283,7 +286,7 @@ func (i *assertionIter) maybeSaveAndMangleRangeKeyBufs() {
283286 if _ , hasRange := i .Iterator .HasPointAndRange (); ! hasRange {
284287 return
285288 }
286- switchBuffers := rand .Intn (2 ) == 0
289+ switchBuffers := i . rng .Intn (2 ) == 0
287290 if switchBuffers {
288291 // Switch to a new buffer for the new range key state.
289292 i .rangeKeyBufs .idx = (idx + 1 ) % 2
0 commit comments