Skip to content

Commit 9ea3046

Browse files
craig[bot]yuzefovich
andcommitted
Merge #155787
155787: storage: don't use global rand in test configs r=yuzefovich a=yuzefovich I was looking into a test timeout under race and saw one spot where we use the global rand under race. We have a few spots like this in the storage folder, so this commit fixes them. Epic: None Release note: None Co-authored-by: Yahor Yuzefovich <yahor@cockroachlabs.com>
2 parents 56f3295 + 2477188 commit 9ea3046

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

pkg/storage/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ go_library(
8585
"//pkg/util/metamorphic",
8686
"//pkg/util/mon",
8787
"//pkg/util/protoutil",
88+
"//pkg/util/randutil",
8889
"//pkg/util/syncutil",
8990
"//pkg/util/sysutil",
9091
"//pkg/util/timeutil",

pkg/storage/intent_interleaving_iter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/cockroachdb/cockroach/pkg/roachpb"
1818
"github.com/cockroachdb/cockroach/pkg/util"
1919
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
20+
"github.com/cockroachdb/cockroach/pkg/util/randutil"
2021
"github.com/cockroachdb/errors"
2122
"github.com/cockroachdb/pebble"
2223
)
@@ -1477,6 +1478,7 @@ func (i *intentInterleavingIter) assertInvariants() error {
14771478
// changes to unsafe keys retrieved from MVCCIterators.
14781479
type unsafeMVCCIterator struct {
14791480
MVCCIterator
1481+
rng *rand.Rand
14801482
keyBuf []byte
14811483
rawKeyBuf []byte
14821484
rawMVCCKeyBuf []byte
@@ -1485,7 +1487,8 @@ type unsafeMVCCIterator struct {
14851487
// gcassert:inline
14861488
func maybeWrapInUnsafeIter(iter MVCCIterator) MVCCIterator {
14871489
if util.RaceEnabled {
1488-
return &unsafeMVCCIterator{MVCCIterator: iter}
1490+
rng, _ := randutil.NewPseudoRand()
1491+
return &unsafeMVCCIterator{MVCCIterator: iter, rng: rng}
14891492
}
14901493
return iter
14911494
}
@@ -1547,7 +1550,7 @@ func (i *unsafeMVCCIterator) UnsafeRawMVCCKey() []byte {
15471550
}
15481551

15491552
func (i *unsafeMVCCIterator) mangleBufs() {
1550-
if rand.Intn(2) == 0 {
1553+
if i.rng.Intn(2) == 0 {
15511554
for _, b := range [3][]byte{i.keyBuf, i.rawKeyBuf, i.rawMVCCKeyBuf} {
15521555
for i := range b {
15531556
b[i] = 0

pkg/storage/pebbleiter/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
visibility = ["//visibility:public"],
1515
deps = [
1616
"//pkg/util",
17+
"//pkg/util/randutil",
1718
"@com_github_cockroachdb_errors//:errors",
1819
"@com_github_cockroachdb_pebble//:pebble",
1920
],

pkg/storage/pebbleiter/crdb_test_on.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2829
func 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.
3335
type 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.
245248
func (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

Comments
 (0)