Skip to content

Commit a53735e

Browse files
craig[bot]yuzefovich
andcommitted
Merge #144127
144127: workload/tpcc: temporarily add back golang.org/x/exp/rand r=yuzefovich a=yuzefovich This commit is partial revert of 84dd73a which removed the usages of golang.org/x/exp/rand from the code base. In particular, this commit brings back the usage into Order and OrderLine generation of TPCC fixtures since the mentioned commit broke the mixed-version import. It's not immediately clear what's wrong there, but the imported tables in the mixed-version 25.1-master state don't pass 3.3.2.4 check. I've been using COCKROACH_RANDOM_SEED=-1621471749504012229 with `import/mixed-versions` for (relatively) fast reproduction. Informs: #143870. Epic: None Release note: None Co-authored-by: Yahor Yuzefovich <[email protected]>
2 parents e4fc9d0 + 2c3a13d commit a53735e

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

pkg/ccl/workloadccl/allccl/all_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func TestDeterministicInitialData(t *testing.T) {
282282
`roachmart`: 0xda5e73423dbdb2d9,
283283
`sqlsmith`: 0xcbf29ce484222325,
284284
`startrek`: 0xa0249fbdf612734c,
285-
`tpcc`: 0x3f37ea71beae16fb,
285+
`tpcc`: 0xcccced25deea244e,
286286
`tpch`: 0xcd2abbd021ed895d,
287287
`ycsb`: 0x0e6012ee6491a0fb,
288288
}

pkg/testutils/lint/lint_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,8 @@ func TestLint(t *testing.T) {
20902090
stream.GrepNot(`pkg/cmd/mirror/go/mirror.go`),
20912091
// As above, the bazel build tag has an impact here.
20922092
stream.GrepNot(`pkg/testutils/docker/single_node_docker_test.go`),
2093+
// TODO(#143870): remove uses of this package.
2094+
stream.GrepNot(`"golang.org/x/exp/rand" is deprecated`),
20932095
}
20942096
for analyzerName, config := range nogoConfig {
20952097
if !staticcheckCheckNameRe.MatchString(analyzerName) {

pkg/workload/tpcc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ go_library(
5050
"@com_github_prometheus_client_golang//prometheus",
5151
"@com_github_prometheus_client_golang//prometheus/promauto",
5252
"@com_github_spf13_pflag//:pflag",
53+
"@org_golang_x_exp//rand",
5354
"@org_golang_x_sync//errgroup",
5455
],
5556
)

pkg/workload/tpcc/generate.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/cockroachdb/cockroach/pkg/util/bufalloc"
1515
"github.com/cockroachdb/cockroach/pkg/util/uuid"
1616
"github.com/cockroachdb/cockroach/pkg/workload"
17+
randold "golang.org/x/exp/rand"
1718
)
1819

1920
// These constants are all set by the spec - they're not knobs. Don't change
@@ -57,6 +58,7 @@ const (
5758

5859
type generateLocals struct {
5960
rng tpccRand
61+
rngOld tpccRandOld
6062
uuidAlloc uuid.UUID
6163
}
6264

@@ -480,8 +482,8 @@ func (w *tpcc) tpccOrderInitialRowBatch(rowIdx int, cb coldata.Batch, a *bufallo
480482

481483
// NB: numOrderLines is not allowed to use precomputed random data, make sure
482484
// it stays that way. See 4.3.2.1.
483-
l.rng.Rand = rand.New(rand.NewPCG(RandomSeed.Seed(), uint64(rowIdx)))
484-
numOrderLines := randInt(l.rng.Rand, minOrderLinesPerOrder, maxOrderLinesPerOrder)
485+
l.rngOld.Seed(RandomSeed.Seed() + uint64(rowIdx))
486+
numOrderLines := randIntOld(l.rngOld.Rand, minOrderLinesPerOrder, maxOrderLinesPerOrder)
485487

486488
oID := (rowIdx % numOrdersPerDistrict) + 1
487489
dID := ((rowIdx / numOrdersPerDistrict) % numDistrictsPerWarehouse) + 1
@@ -500,7 +502,7 @@ func (w *tpcc) tpccOrderInitialRowBatch(rowIdx int, cb coldata.Batch, a *bufallo
500502
// We need a random permutation of customers that stable for all orders in a
501503
// district, so use the district ID to seed the random permutation.
502504
w.randomCIDsCache.values[dID] = make([]int, numCustomersPerDistrict)
503-
for i, cID := range rand.New(rand.NewPCG(uint64(dID), 0)).Perm(numCustomersPerDistrict) {
505+
for i, cID := range randold.New(randold.NewSource(uint64(dID))).Perm(numCustomersPerDistrict) {
504506
w.randomCIDsCache.values[dID][i] = cID + 1
505507
}
506508
}
@@ -512,7 +514,7 @@ func (w *tpcc) tpccOrderInitialRowBatch(rowIdx int, cb coldata.Batch, a *bufallo
512514
var carrierID int64
513515
if oID < 2101 {
514516
carrierSet = true
515-
carrierID = randInt(l.rng.Rand, 1, 10)
517+
carrierID = randIntOld(l.rngOld.Rand, 1, 10)
516518
}
517519

518520
cb.Reset(orderTypes, 1, coldata.StandardColumnFactory)
@@ -601,15 +603,15 @@ func (w *tpcc) tpccOrderLineInitialRowBatch(
601603

602604
// NB: numOrderLines is not allowed to use precomputed random data, make sure
603605
// it stays that way. See 4.3.2.1.
604-
l.rng.Rand = rand.New(rand.NewPCG(RandomSeed.Seed(), uint64(orderRowIdx)))
605-
numOrderLines := int(randInt(l.rng.Rand, minOrderLinesPerOrder, maxOrderLinesPerOrder))
606+
l.rngOld.Seed(RandomSeed.Seed() + uint64(orderRowIdx))
607+
numOrderLines := int(randIntOld(l.rngOld.Rand, minOrderLinesPerOrder, maxOrderLinesPerOrder))
606608

607609
// NB: There is one batch of order_line rows per order
608610
oID := (orderRowIdx % numOrdersPerDistrict) + 1
609611
dID := ((orderRowIdx / numOrdersPerDistrict) % numDistrictsPerWarehouse) + 1
610612
wID := (orderRowIdx / numOrdersPerWarehouse)
611613

612-
ao := aCharsOffset(l.rng.IntN(len(aCharsAlphabet)))
614+
ao := aCharsOffset(l.rngOld.Intn(len(aCharsAlphabet)))
613615
cb.Reset(orderLineTypes, numOrderLines, coldata.StandardColumnFactory)
614616
olOIDCol := cb.ColVec(0).Int64()
615617
olDIDCol := cb.ColVec(1).Int64()
@@ -634,14 +636,14 @@ func (w *tpcc) tpccOrderLineInitialRowBatch(
634636
amount = 0
635637
deliveryDSet = true
636638
} else {
637-
amount = float64(randInt(l.rng.Rand, 1, 999999)) / 100.0
639+
amount = float64(randIntOld(l.rngOld.Rand, 1, 999999)) / 100.0
638640
}
639641

640642
olOIDCol[rowIdx] = int64(oID)
641643
olDIDCol[rowIdx] = int64(dID)
642644
olWIDCol[rowIdx] = int64(wID)
643645
olNumberCol[rowIdx] = int64(olNumber)
644-
olIIDCol[rowIdx] = randInt(l.rng.Rand, 1, 100000)
646+
olIIDCol[rowIdx] = randIntOld(l.rngOld.Rand, 1, 100000)
645647
olSupplyWIDCol[rowIdx] = int64(wID)
646648
if deliveryDSet {
647649
olDeliveryDCol.Set(rowIdx, w.nowTime)
@@ -650,7 +652,7 @@ func (w *tpcc) tpccOrderLineInitialRowBatch(
650652
}
651653
olQuantityCol[rowIdx] = 5
652654
olAmountCol[rowIdx] = amount
653-
olDistInfoCol.Set(rowIdx, randAStringInitialDataOnly(&l.rng, &ao, a, 24, 24))
655+
olDistInfoCol.Set(rowIdx, randAStringInitialDataOnlyOld(&l.rngOld, &ao, a, 24, 24))
654656
}
655657
}
656658

pkg/workload/tpcc/random.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cockroachdb/cockroach/pkg/util/bufalloc"
1212
"github.com/cockroachdb/cockroach/pkg/workload/workloadimpl"
13+
randold "golang.org/x/exp/rand"
1314
)
1415

1516
var cLastTokens = [...]string{
@@ -34,6 +35,13 @@ type tpccRand struct {
3435
aChars, letters, numbers workloadimpl.PrecomputedRand
3536
}
3637

38+
// TODO(#143870): remove this.
39+
type tpccRandOld struct {
40+
*randold.Rand
41+
42+
aChars, letters, numbers workloadimpl.PrecomputedRand
43+
}
44+
3745
type aCharsOffset int
3846
type lettersOffset int
3947
type numbersOffset int
@@ -59,6 +67,27 @@ func randStringFromAlphabet(
5967
return b
6068
}
6169

70+
func randStringFromAlphabetOld(
71+
rng *randold.Rand,
72+
a *bufalloc.ByteAllocator,
73+
minLen, maxLen int,
74+
pr workloadimpl.PrecomputedRand,
75+
prOffset *int,
76+
) []byte {
77+
size := maxLen
78+
if maxLen-minLen != 0 {
79+
size = int(randIntOld(rng, minLen, maxLen))
80+
}
81+
if size == 0 {
82+
return nil
83+
}
84+
85+
var b []byte
86+
*a, b = a.Alloc(size, 0 /* extraCap */)
87+
*prOffset = pr.FillBytes(*prOffset, b)
88+
return b
89+
}
90+
6291
// randAStringInitialDataOnly generates a random alphanumeric string of length
6392
// between min and max inclusive. It uses a set of pregenerated random data,
6493
// which the spec allows only for initial data. See 4.3.2.2.
@@ -71,6 +100,12 @@ func randAStringInitialDataOnly(
71100
return randStringFromAlphabet(rng.Rand, a, min, max, rng.aChars, (*int)(ao))
72101
}
73102

103+
func randAStringInitialDataOnlyOld(
104+
rng *tpccRandOld, ao *aCharsOffset, a *bufalloc.ByteAllocator, min, max int,
105+
) []byte {
106+
return randStringFromAlphabetOld(rng.Rand, a, min, max, rng.aChars, (*int)(ao))
107+
}
108+
74109
// randNStringInitialDataOnly generates a random numeric string of length
75110
// between min and max inclusive. See 4.3.2.2.
76111
//
@@ -138,6 +173,10 @@ func randInt(rng *rand.Rand, min, max int) int64 {
138173
return int64(rng.IntN(max-min+1) + min)
139174
}
140175

176+
func randIntOld(rng *randold.Rand, min, max int) int64 {
177+
return int64(rng.Intn(max-min+1) + min)
178+
}
179+
141180
// randCLastSyllables returns a customer last name string generated according to
142181
// the table in 4.3.2.3. Given a number between 0 and 999, each of the three
143182
// syllables is determined by the corresponding digit in the three digit

pkg/workload/tpcc/tpcc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/cockroachdb/errors"
3232
"github.com/jackc/pgx/v5"
3333
"github.com/spf13/pflag"
34+
randold "golang.org/x/exp/rand"
3435
"golang.org/x/sync/errgroup"
3536
)
3637

@@ -661,6 +662,9 @@ func (w *tpcc) Tables() []workload.Table {
661662
aCharsInit := workloadimpl.PrecomputedRandInit(rand.NewPCG(seed, 0), precomputedLength, aCharsAlphabet)
662663
lettersInit := workloadimpl.PrecomputedRandInit(rand.NewPCG(seed, 0), precomputedLength, lettersAlphabet)
663664
numbersInit := workloadimpl.PrecomputedRandInit(rand.NewPCG(seed, 0), precomputedLength, numbersAlphabet)
665+
aCharsInitOld := workloadimpl.PrecomputedRandInit(randold.New(randold.NewSource(seed)), precomputedLength, aCharsAlphabet)
666+
lettersInitOld := workloadimpl.PrecomputedRandInit(randold.New(randold.NewSource(seed)), precomputedLength, lettersAlphabet)
667+
numbersInitOld := workloadimpl.PrecomputedRandInit(randold.New(randold.NewSource(seed)), precomputedLength, numbersAlphabet)
664668
if w.localsPool == nil {
665669
w.localsPool = &sync.Pool{
666670
New: func() interface{} {
@@ -674,6 +678,15 @@ func (w *tpcc) Tables() []workload.Table {
674678
letters: lettersInit(),
675679
numbers: numbersInit(),
676680
},
681+
rngOld: tpccRandOld{
682+
Rand: randold.New(randold.NewSource(uint64(timeutil.Now().UnixNano()))),
683+
// Intentionally wait until here to initialize the precomputed rands
684+
// so a caller of Tables that only wants schema doesn't compute
685+
// them.
686+
aChars: aCharsInitOld(),
687+
letters: lettersInitOld(),
688+
numbers: numbersInitOld(),
689+
},
677690
}
678691
},
679692
}

0 commit comments

Comments
 (0)