Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: stop using math/rand.Seed #48653

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions br/pkg/lightning/mydump/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,17 +1130,17 @@ func TestSampleParquetDataSize(t *testing.T) {
pwriter.PageSize = 8 * 1024 //8K
pwriter.CompressionType = parquet.CompressionCodec_SNAPPY
seed := time.Now().Unix()
t.Logf("seed: %d", seed)
szpnygo marked this conversation as resolved.
Show resolved Hide resolved
rand.Seed(seed)
t.Logf("seed: %d. To reproduce the random behaviour, manually set `rand.New(rand.NewSource(seed))`", seed)
rnd := rand.New(rand.NewSource(seed))
totalRowSize := 0
for i := 0; i < 1000; i++ {
kl := rand.Intn(20) + 1
kl := rnd.Intn(20) + 1
key := make([]byte, kl)
kl, err = rand.Read(key)
kl, err = rnd.Read(key)
require.NoError(t, err)
vl := rand.Intn(20) + 1
vl := rnd.Intn(20) + 1
value := make([]byte, vl)
vl, err = rand.Read(value)
vl, err = rnd.Read(value)
require.NoError(t, err)

totalRowSize += kl + vl + 8
Expand Down
1 change: 0 additions & 1 deletion cmd/ddltest/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,5 @@ func addEnvPath(newPath string) {
}

func init() {
rand.Seed(time.Now().UnixNano())
_ = store.Register("tikv", tidbdriver.TiKVDriver{})
}
4 changes: 0 additions & 4 deletions cmd/importer/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ const (
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func randInt(min int, max int) int {
return min + rand.Intn(max-min+1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/parser/mysql"
Expand Down Expand Up @@ -109,7 +108,6 @@ func genTestChunk4VecGroupChecker(chkRows []int, sameNum int) (expr []expression
numGroups = numRows/sameNum + 1
}

rand.Seed(time.Now().Unix())
nullPos := rand.Intn(numGroups)
cnt := 0
val := rand.Int63()
Expand Down
1 change: 0 additions & 1 deletion pkg/planner/core/tests/prepare/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func TestRandomFlushPlanCache(t *testing.T) {
execStmts = append(execStmts, execStmt)
}

rand.Seed(time.Now().Unix())
for i := 0; i < 10; i++ {
// Warm up to make sure all the plans are in the cache.
for _, execStmt := range execStmts {
Expand Down
1 change: 0 additions & 1 deletion pkg/statistics/handle/updatetest/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,6 @@ func TestMergeTopN(t *testing.T) {

topNs := make([]*statistics.TopN, 0, topnNum)
res := make(map[int]uint64)
rand.Seed(time.Now().Unix())
for i := 0; i < topnNum; i++ {
topN := statistics.NewTopN(n)
occur := make(map[int]bool)
Expand Down
2 changes: 0 additions & 2 deletions pkg/store/driver/tikv_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"crypto/tls"
"fmt"
"math/rand"
"net/url"
"strings"
"sync"
Expand Down Expand Up @@ -57,7 +56,6 @@ var mc storeCache

func init() {
mc.cache = make(map[string]*tikvStore)
rand.Seed(time.Now().UnixNano())

// Setup the Hooks to dynamic control global resource controller.
variable.EnableGlobalResourceControlFunc = tikv.EnableResourceControl
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/chunk/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func BenchmarkListGetRow(b *testing.B) {
for _, chk := range chks {
l.Add(chk)
}
rand.Seed(0)
rnd := rand.New(rand.NewSource(0))
ptrs := make([]RowPtr, 0, b.N)
for i := 0; i < min(b.N, 10000); i++ {
ptrs = append(ptrs, RowPtr{
ChkIdx: rand.Uint32() % uint32(numChk),
RowIdx: rand.Uint32() % uint32(numRow),
ChkIdx: rnd.Uint32() % uint32(numChk),
RowIdx: rnd.Uint32() % uint32(numRow),
})
}
for i := 10000; i < cap(ptrs); i++ {
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/chunk/row_in_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ func BenchmarkDataInDiskByRowsGetRow(b *testing.B) {
b.Fatal(err)
}
}
rand.Seed(0)
rnd := rand.New(rand.NewSource(0))
ptrs := make([]RowPtr, 0, b.N)
for i := 0; i < min(b.N, 10000); i++ {
ptrs = append(ptrs, RowPtr{
ChkIdx: rand.Uint32() % uint32(numChk),
RowIdx: rand.Uint32() % uint32(numRow),
ChkIdx: rnd.Uint32() % uint32(numChk),
RowIdx: rnd.Uint32() % uint32(numRow),
})
}
for i := 10000; i < cap(ptrs); i++ {
Expand Down
4 changes: 0 additions & 4 deletions pkg/util/importer/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ const (
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func randInt(min int, max int) int {
return min + rand.Intn(max-min+1) // nolint:gosec
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/util/selection/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"math/rand"
"sort"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -84,7 +83,6 @@ func TestSelectionWithSerialCase(t *testing.T) {

func randomTestCase(size int) testSlice {
data := make(testSlice, 0, size)
rand.Seed(time.Now().Unix())
for i := 0; i < size; i++ {
data = append(data, rand.Int()%100)
}
Expand Down
2 changes: 0 additions & 2 deletions tools/check/ut.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ func main() {

// Get the correct count of CPU if it's in docker.
p = runtime.GOMAXPROCS(0)
rand.Seed(time.Now().Unix())
var err error
workDir, err = os.Getwd()
if err != nil {
Expand Down Expand Up @@ -953,7 +952,6 @@ func filter(input []string, f func(string) bool) []string {
}

func shuffle(tasks []task) {
rand.Seed(time.Now().UnixNano())
for i := 0; i < len(tasks); i++ {
pos := rand.Intn(len(tasks))
tasks[i], tasks[pos] = tasks[pos], tasks[i]
Expand Down
Loading