From cdf824e92f6e7cbb7b92f74bdd70673b0f486da1 Mon Sep 17 00:00:00 2001 From: neosu Date: Fri, 17 Nov 2023 11:04:59 +0800 Subject: [PATCH 1/3] stop using math/rand.Seed --- br/pkg/lightning/mydump/loader_test.go | 4 ---- cmd/ddltest/ddl_test.go | 1 - cmd/importer/rand.go | 4 ---- .../internal/vecgroupchecker/vec_group_checker_test.go | 2 -- pkg/planner/core/tests/prepare/prepare_test.go | 1 - pkg/statistics/handle/updatetest/update_test.go | 1 - pkg/store/driver/tikv_driver.go | 2 -- pkg/util/chunk/list_test.go | 6 +++--- pkg/util/chunk/row_in_disk_test.go | 6 +++--- pkg/util/importer/rand.go | 4 ---- pkg/util/selection/selection_test.go | 2 -- tools/check/ut.go | 2 -- 12 files changed, 6 insertions(+), 29 deletions(-) diff --git a/br/pkg/lightning/mydump/loader_test.go b/br/pkg/lightning/mydump/loader_test.go index 2789eb838a64c..78c44b17a8e57 100644 --- a/br/pkg/lightning/mydump/loader_test.go +++ b/br/pkg/lightning/mydump/loader_test.go @@ -23,7 +23,6 @@ import ( "os" "path/filepath" "testing" - "time" "github.com/pingcap/tidb/br/pkg/lightning/common" "github.com/pingcap/tidb/br/pkg/lightning/config" @@ -1129,9 +1128,6 @@ func TestSampleParquetDataSize(t *testing.T) { pwriter.RowGroupSize = 128 * 1024 * 1024 //128M pwriter.PageSize = 8 * 1024 //8K pwriter.CompressionType = parquet.CompressionCodec_SNAPPY - seed := time.Now().Unix() - t.Logf("seed: %d", seed) - rand.Seed(seed) totalRowSize := 0 for i := 0; i < 1000; i++ { kl := rand.Intn(20) + 1 diff --git a/cmd/ddltest/ddl_test.go b/cmd/ddltest/ddl_test.go index f3792ee17f257..6711cb73fd25a 100644 --- a/cmd/ddltest/ddl_test.go +++ b/cmd/ddltest/ddl_test.go @@ -1156,6 +1156,5 @@ func addEnvPath(newPath string) { } func init() { - rand.Seed(time.Now().UnixNano()) _ = store.Register("tikv", tidbdriver.TiKVDriver{}) } diff --git a/cmd/importer/rand.go b/cmd/importer/rand.go index d0470afc94be5..1adab0f0dbce6 100644 --- a/cmd/importer/rand.go +++ b/cmd/importer/rand.go @@ -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) } diff --git a/pkg/executor/internal/vecgroupchecker/vec_group_checker_test.go b/pkg/executor/internal/vecgroupchecker/vec_group_checker_test.go index 9dda0d27b6acc..b1a26fb7776fa 100644 --- a/pkg/executor/internal/vecgroupchecker/vec_group_checker_test.go +++ b/pkg/executor/internal/vecgroupchecker/vec_group_checker_test.go @@ -18,7 +18,6 @@ import ( "fmt" "math/rand" "testing" - "time" "github.com/pingcap/tidb/pkg/expression" "github.com/pingcap/tidb/pkg/parser/mysql" @@ -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() diff --git a/pkg/planner/core/tests/prepare/prepare_test.go b/pkg/planner/core/tests/prepare/prepare_test.go index a681bdddc347f..3da9d010e8974 100644 --- a/pkg/planner/core/tests/prepare/prepare_test.go +++ b/pkg/planner/core/tests/prepare/prepare_test.go @@ -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 { diff --git a/pkg/statistics/handle/updatetest/update_test.go b/pkg/statistics/handle/updatetest/update_test.go index 01ef3342f6aea..e2e2f3967519a 100644 --- a/pkg/statistics/handle/updatetest/update_test.go +++ b/pkg/statistics/handle/updatetest/update_test.go @@ -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) diff --git a/pkg/store/driver/tikv_driver.go b/pkg/store/driver/tikv_driver.go index facdf3e8cf1dc..199661d56f95d 100644 --- a/pkg/store/driver/tikv_driver.go +++ b/pkg/store/driver/tikv_driver.go @@ -18,7 +18,6 @@ import ( "context" "crypto/tls" "fmt" - "math/rand" "net/url" "strings" "sync" @@ -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 diff --git a/pkg/util/chunk/list_test.go b/pkg/util/chunk/list_test.go index 106b9ece6f438..4ec1b777e41b2 100644 --- a/pkg/util/chunk/list_test.go +++ b/pkg/util/chunk/list_test.go @@ -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++ { diff --git a/pkg/util/chunk/row_in_disk_test.go b/pkg/util/chunk/row_in_disk_test.go index 1c27456a94ea3..453b32be292f8 100644 --- a/pkg/util/chunk/row_in_disk_test.go +++ b/pkg/util/chunk/row_in_disk_test.go @@ -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++ { diff --git a/pkg/util/importer/rand.go b/pkg/util/importer/rand.go index 60cfd26db3f4c..b039dde424e27 100644 --- a/pkg/util/importer/rand.go +++ b/pkg/util/importer/rand.go @@ -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 } diff --git a/pkg/util/selection/selection_test.go b/pkg/util/selection/selection_test.go index 310502f8886e9..779c8b05677f6 100644 --- a/pkg/util/selection/selection_test.go +++ b/pkg/util/selection/selection_test.go @@ -18,7 +18,6 @@ import ( "math/rand" "sort" "testing" - "time" "github.com/stretchr/testify/require" ) @@ -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) } diff --git a/tools/check/ut.go b/tools/check/ut.go index f6bc45ab17014..6fb662bed1edb 100644 --- a/tools/check/ut.go +++ b/tools/check/ut.go @@ -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 { @@ -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] From e14f130b53eb89cea71543b3e15e9754a7fd9cc1 Mon Sep 17 00:00:00 2001 From: neosu Date: Fri, 17 Nov 2023 12:04:36 +0800 Subject: [PATCH 2/3] Record random seed for error reproduction --- br/pkg/lightning/mydump/loader_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/br/pkg/lightning/mydump/loader_test.go b/br/pkg/lightning/mydump/loader_test.go index 78c44b17a8e57..2b3a0be1f1bf3 100644 --- a/br/pkg/lightning/mydump/loader_test.go +++ b/br/pkg/lightning/mydump/loader_test.go @@ -23,6 +23,7 @@ import ( "os" "path/filepath" "testing" + "time" "github.com/pingcap/tidb/br/pkg/lightning/common" "github.com/pingcap/tidb/br/pkg/lightning/config" @@ -1128,15 +1129,18 @@ func TestSampleParquetDataSize(t *testing.T) { pwriter.RowGroupSize = 128 * 1024 * 1024 //128M pwriter.PageSize = 8 * 1024 //8K pwriter.CompressionType = parquet.CompressionCodec_SNAPPY + seed := time.Now().Unix() + t.Logf("seed: %d. To reproduce the random behaviour, manually set `rand.Seed(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 From e2dc1077c4009ff3499af4331bfa894772c36378 Mon Sep 17 00:00:00 2001 From: neosu Date: Fri, 17 Nov 2023 12:07:35 +0800 Subject: [PATCH 3/3] update log text --- br/pkg/lightning/mydump/loader_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/br/pkg/lightning/mydump/loader_test.go b/br/pkg/lightning/mydump/loader_test.go index 2b3a0be1f1bf3..041f5593323e3 100644 --- a/br/pkg/lightning/mydump/loader_test.go +++ b/br/pkg/lightning/mydump/loader_test.go @@ -1130,7 +1130,7 @@ func TestSampleParquetDataSize(t *testing.T) { pwriter.PageSize = 8 * 1024 //8K pwriter.CompressionType = parquet.CompressionCodec_SNAPPY seed := time.Now().Unix() - t.Logf("seed: %d. To reproduce the random behaviour, manually set `rand.Seed(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++ {