Skip to content

Commit

Permalink
*: switch to rand/v2
Browse files Browse the repository at this point in the history
Switch to using `math/rand/v2` instead of `math/rand` or
`golang.org/x/exp/rand`.

Note that with v2, `rand.Uint64()` is pretty much the same as
`runtime_fastrand` so we can remove the fastrand package, but that is
left for another change.
  • Loading branch information
RaduBerinde committed Oct 7, 2024
1 parent 3f7527f commit 10aec22
Show file tree
Hide file tree
Showing 109 changed files with 873 additions and 831 deletions.
10 changes: 5 additions & 5 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
"math/rand/v2"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -1611,9 +1611,9 @@ func TestBatchSpanCaching(t *testing.T) {
require.NoError(t, b.Set(k, k, nil))
}

seed := int64(time.Now().UnixNano())
seed := uint64(time.Now().UnixNano())
t.Logf("seed = %d", seed)
rng := rand.New(rand.NewSource(seed))
rng := rand.New(rand.NewPCG(seed, seed))
iters := make([][]*Iterator, ks.Count())
defer func() {
for _, keyIters := range iters {
Expand Down Expand Up @@ -1677,12 +1677,12 @@ func TestBatchSpanCaching(t *testing.T) {
default: /* 45 % */
// Create a new iterator through cloning a random existing iterator
// and check that it observes the right state.
readKey := rng.Int63n(nextWriteKey + 1)
readKey := rng.Int64N(nextWriteKey + 1)
itersForReadKey := iters[readKey]
if len(itersForReadKey) == 0 {
continue
}
iter, err := itersForReadKey[rng.Intn(len(itersForReadKey))].Clone(CloneOptions{})
iter, err := itersForReadKey[rng.IntN(len(itersForReadKey))].Clone(CloneOptions{})
require.NoError(t, err)
checkIter(iter, readKey)
iters[readKey] = append(iters[readKey], iter)
Expand Down
4 changes: 2 additions & 2 deletions checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"bytes"
"context"
"fmt"
"math/rand"
"math/rand/v2"
"runtime"
"sort"
"strings"
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestCheckpointManyFiles(t *testing.T) {
// We want to test the case where the appended record with the excluded files
// makes the manifest cross 32KB. This will happen for a range of values
// around 450.
n := 400 + rand.Intn(100)
n := 400 + rand.IntN(100)
for i := 0; i < n; i++ {
err := d.Set(mkKey(i), nil, nil)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pebble/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"fmt"
"log"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/cockroachdb/pebble/internal/crdbtest"
"github.com/cockroachdb/pebble/internal/randvar"
"github.com/spf13/cobra"
"golang.org/x/exp/rand"
)

var queueConfig struct {
Expand Down Expand Up @@ -43,7 +43,7 @@ func queueTest() (test, *atomic.Int64) {
init: func(d DB, wg *sync.WaitGroup) {
var (
value []byte
rng = rand.New(rand.NewSource(1449168817))
rng = rand.New(rand.NewPCG(0, 1449168817))
queue = make([][]byte, queueConfig.size)
)
for i := 0; i < queueConfig.size; i++ {
Expand Down
8 changes: 4 additions & 4 deletions cmd/pebble/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"math"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/cockroachdb/pebble/internal/crdbtest"
"github.com/cockroachdb/pebble/internal/randvar"
"github.com/spf13/cobra"
"golang.org/x/exp/rand"
)

var scanConfig struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func runScan(cmd *cobra.Command, args []string) {
const count = 100000
const batch = 1000

rng := rand.New(rand.NewSource(1449168817))
rng := rand.New(rand.NewPCG(0, 1449168817))
keys := make([][]byte, count)

for i := 0; i < count; {
Expand Down Expand Up @@ -95,7 +95,7 @@ func runScan(cmd *cobra.Command, args []string) {
go func(i int) {
defer wg.Done()

rng := rand.New(rand.NewSource(uint64(i)))
rng := rand.New(rand.NewPCG(0, uint64(i)))
startKeyBuf := append(make([]byte, 0, 64), []byte("key-")...)
endKeyBuf := append(make([]byte, 0, 64), []byte("key-")...)
minTS := encodeUint64Ascending(nil, math.MaxUint64)
Expand All @@ -104,7 +104,7 @@ func runScan(cmd *cobra.Command, args []string) {
wait(limiter)

rows := int(rowDist.Uint64(rng))
startIdx := rng.Int31n(int32(len(keys) - rows))
startIdx := rng.Int32N(int32(len(keys) - rows))
startKey := encodeUint32Ascending(startKeyBuf[:4], uint32(startIdx))
endKey := encodeUint32Ascending(endKeyBuf[:4], uint32(startIdx+int32(rows)))

Expand Down
4 changes: 2 additions & 2 deletions cmd/pebble/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"fmt"
"log"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/cockroachdb/pebble/internal/crdbtest"
"github.com/cockroachdb/pebble/internal/randvar"
"github.com/spf13/cobra"
"golang.org/x/exp/rand"
)

var syncConfig struct {
Expand Down Expand Up @@ -67,7 +67,7 @@ func runSync(cmd *cobra.Command, args []string) {
go func() {
defer wg.Done()

rand := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
rand := rand.New(rand.NewPCG(0, uint64(time.Now().UnixNano())))
var raw []byte
var buf []byte
var block []byte
Expand Down
2 changes: 1 addition & 1 deletion cmd/pebble/ycsb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"fmt"
"log"
"math/rand/v2"
"strconv"
"strings"
"sync"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/cockroachdb/pebble/internal/randvar"
"github.com/cockroachdb/pebble/internal/rate"
"github.com/spf13/cobra"
"golang.org/x/exp/rand"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/binary"
"fmt"
"io"
"math/rand/v2"
"sync"
"sync/atomic"
"testing"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/cockroachdb/pebble/vfs"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
)

type testCommitEnv struct {
Expand Down Expand Up @@ -401,7 +401,7 @@ func BenchmarkCommitPipeline(b *testing.B) {
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
rng := rand.New(rand.NewPCG(0, uint64(time.Now().UnixNano())))
buf := make([]byte, keySize)

for pb.Next() {
Expand Down
8 changes: 4 additions & 4 deletions compaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
crand "crypto/rand"
"fmt"
"math"
"math/rand"
"math/rand/v2"
"path/filepath"
"regexp"
"sort"
Expand Down Expand Up @@ -914,12 +914,12 @@ func TestManualCompaction(t *testing.T) {
}
}()

seed := time.Now().UnixNano()
rng := rand.New(rand.NewSource(seed))
seed := uint64(time.Now().UnixNano())
rng := rand.New(rand.NewPCG(0, seed))
t.Logf("seed: %d", seed)

randVersion := func(min, max FormatMajorVersion) FormatMajorVersion {
return FormatMajorVersion(int(min) + rng.Intn(int(max)-int(min)+1))
return FormatMajorVersion(int(min) + rng.IntN(int(max)-int(min)+1))
}

var compactionLog bytes.Buffer
Expand Down
16 changes: 8 additions & 8 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
"math/rand/v2"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -417,9 +417,9 @@ func parseValue(s string) []byte {
panic(err)
}
b := make([]byte, n)
rnd := rand.New(rand.NewSource(int64(n)))
if _, err := rnd.Read(b); err != nil {
panic(err)
rnd := rand.New(rand.NewPCG(0, uint64(n)))
for i := range b {
b[i] = byte(rnd.Uint32())
}
return b
}
Expand Down Expand Up @@ -569,7 +569,7 @@ func runBuildRemoteCmd(td *datadriven.TestData, d *DB, storage remote.Storage) e
}

writeOpts := d.opts.MakeWriterOptions(0 /* level */, tableFormat)
if blockSize == 0 && rand.Intn(4) == 0 {
if blockSize == 0 && rand.IntN(4) == 0 {
// Force two-level indexes if not already forced on or off.
blockSize = 5
}
Expand Down Expand Up @@ -1092,9 +1092,9 @@ func runDBDefineCmdReuseFS(td *datadriven.TestData, opts *Options) (*DB, error)
var randBytes int
if n, err := fmt.Sscanf(valueStr, "<rand-bytes=%d>", &randBytes); err == nil && n == 1 {
value = make([]byte, randBytes)
rnd := rand.New(rand.NewSource(int64(key.SeqNum())))
if _, err := rnd.Read(value[:]); err != nil {
return nil, err
rnd := rand.New(rand.NewPCG(0, uint64(key.SeqNum())))
for j := range value {
value[j] = byte(rnd.Uint32())
}
}
if err := mem.set(key, value); err != nil {
Expand Down
24 changes: 13 additions & 11 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"fmt"
"io"
"math/rand/v2"
"path/filepath"
"regexp"
"runtime"
Expand All @@ -33,7 +34,6 @@ import (
"github.com/cockroachdb/pebble/vfs/errorfs"
"github.com/cockroachdb/pebble/wal"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
)

// try repeatedly calls f, sleeping between calls with exponential back-off,
Expand Down Expand Up @@ -319,12 +319,12 @@ func TestRandomWrites(t *testing.T) {
}
xxx := bytes.Repeat([]byte("x"), 512)

rng := rand.New(rand.NewSource(123))
rng := rand.New(rand.NewPCG(0, 123))
const N = 1000
for i := 0; i < N; i++ {
k := rng.Intn(len(keys))
if rng.Intn(20) != 0 {
wants[k] = rng.Intn(len(xxx) + 1)
k := rng.IntN(len(keys))
if rng.IntN(20) != 0 {
wants[k] = rng.IntN(len(xxx) + 1)
if err := d.Set(keys[k], xxx[:wants[k]], nil); err != nil {
t.Fatalf("i=%d: Set: %v", i, err)
}
Expand All @@ -335,7 +335,7 @@ func TestRandomWrites(t *testing.T) {
}
}

if i != N-1 || rng.Intn(50) != 0 {
if i != N-1 || rng.IntN(50) != 0 {
continue
}
for k := range keys {
Expand Down Expand Up @@ -938,7 +938,7 @@ func TestFlushEmpty(t *testing.T) {
}

func TestRollManifest(t *testing.T) {
toPreserve := rand.Int31n(5) + 1
toPreserve := rand.Int32N(5) + 1
opts := &Options{
MaxManifestFileSize: 1,
L0CompactionThreshold: 10,
Expand Down Expand Up @@ -1863,7 +1863,7 @@ func TestMemtableIngestInversion(t *testing.T) {
}

func BenchmarkDelete(b *testing.B) {
rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
rng := rand.New(rand.NewPCG(0, uint64(time.Now().UnixNano())))
const keyCount = 10000
var keys [keyCount][]byte
for i := 0; i < keyCount; i++ {
Expand Down Expand Up @@ -2092,7 +2092,9 @@ func TestWALFailoverAvoidsWriteStall(t *testing.T) {
require.NoError(t, err)
defer d.Close()
value := make([]byte, 1<<20)
rand.Read(value)
for i := range value {
value[i] = byte(rand.Uint32())
}
// After ~8 writes, the default write stall threshold is exceeded, but the
// writes will not block indefinitely since failover has or will happen, and
// wal.Manager.ElevateWriteStallThresholdForFailover() will return true.
Expand Down Expand Up @@ -2155,7 +2157,7 @@ func TestDeterminism(t *testing.T) {
FormatMajorVersion: FormatNewest,
DisableAutomaticCompactions: true,
}
opts.Experimental.IngestSplit = func() bool { return rand.Intn(2) == 1 }
opts.Experimental.IngestSplit = func() bool { return rand.IntN(2) == 1 }
var err error
if d, err = runDBDefineCmdReuseFS(td, opts); err != nil {
return err.Error()
Expand Down Expand Up @@ -2442,7 +2444,7 @@ func TestLoadBlockSema(t *testing.T) {
defer wg.Done()
const numQueries = 100
for i := 0; i < numQueries; i++ {
val, closer, err := db.Get(key(rand.Intn(numRegions), rand.Intn(numKeys)))
val, closer, err := db.Get(key(rand.IntN(numRegions), rand.IntN(numKeys)))
require.NoError(t, err)
require.Equal(t, []byte("value"), val)
if closer != nil {
Expand Down
Loading

0 comments on commit 10aec22

Please sign in to comment.