From 8e554c7d4af10f15f33926585b0eb42a2a2b6a6a Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Wed, 30 Aug 2023 10:10:22 -0400 Subject: [PATCH] Stop seeding rand with timestamps, it no longer auto-seeds with a constant See https://github.com/golang/go/issues/56319 Signed-off-by: Andrew Mason --- examples/compose/client.go | 1 - go/cmd/vtclient/vtclient.go | 1 - go/cmd/vtgate/vtgate.go | 3 --- go/netutil/netutil.go | 14 +++++--------- go/netutil/netutil_test.go | 11 +++++------ .../tabletgateway/buffer/buffer_test_helpers.go | 1 - go/test/endtoend/vreplication/cluster_test.go | 1 - .../vtgate/keyspace_watches/keyspace_watch_test.go | 3 --- go/vt/tableacl/testlib/testlib.go | 5 ----- go/vt/topo/memorytopo/memorytopo.go | 5 ----- go/vt/topotools/shard_test.go | 2 -- go/vt/vttablet/tabletserver/gc/tablegc.go | 5 ----- go/vt/vttablet/tabletserver/tabletserver_test.go | 5 ----- go/vt/vttablet/tabletserver/throttle/throttler.go | 4 ---- .../tabletserver/vstreamer/packet_size_test.go | 13 ++++--------- go/vt/vttest/environment.go | 5 ----- test/client/client.go | 1 - 17 files changed, 14 insertions(+), 66 deletions(-) diff --git a/examples/compose/client.go b/examples/compose/client.go index a4933f21833..8beaef683cd 100644 --- a/examples/compose/client.go +++ b/examples/compose/client.go @@ -42,7 +42,6 @@ var ( func main() { pflag.Parse() - rand.Seed(time.Now().UnixNano()) // Connect to vtgate. db, err := vitessdriver.Open(*server, "@primary") diff --git a/go/cmd/vtclient/vtclient.go b/go/cmd/vtclient/vtclient.go index adc060d7737..26c8cfd4806 100644 --- a/go/cmd/vtclient/vtclient.go +++ b/go/cmd/vtclient/vtclient.go @@ -197,7 +197,6 @@ func run() (*results, error) { if maxSeqID > minSeqID { go func() { if useRandom { - rand.Seed(time.Now().UnixNano()) for { seqChan <- rand.Intn(maxSeqID-minSeqID) + minSeqID } diff --git a/go/cmd/vtgate/vtgate.go b/go/cmd/vtgate/vtgate.go index 27d28aa35a8..fe2be59d116 100644 --- a/go/cmd/vtgate/vtgate.go +++ b/go/cmd/vtgate/vtgate.go @@ -18,9 +18,7 @@ package main import ( "context" - "math/rand" "strings" - "time" "github.com/spf13/pflag" @@ -56,7 +54,6 @@ func registerFlags(fs *pflag.FlagSet) { var resilientServer *srvtopo.ResilientServer func init() { - rand.Seed(time.Now().UnixNano()) servenv.RegisterDefaultFlags() servenv.RegisterFlags() servenv.RegisterGRPCServerFlags() diff --git a/go/netutil/netutil.go b/go/netutil/netutil.go index 54e53e85226..fbac6e88424 100644 --- a/go/netutil/netutil.go +++ b/go/netutil/netutil.go @@ -29,10 +29,6 @@ import ( "time" ) -func init() { - rand.Seed(time.Now().UnixNano()) -} - // byPriorityWeight sorts records by ascending priority and weight. type byPriorityWeight []*net.SRV @@ -48,7 +44,7 @@ func (addrs byPriorityWeight) Less(i, j int) bool { // shuffleByWeight shuffles SRV records by weight using the algorithm // described in RFC 2782. // NOTE(msolo) This is disabled when the weights are zero. -func (addrs byPriorityWeight) shuffleByWeight() { +func (addrs byPriorityWeight) shuffleByWeight(rand *rand.Rand) { sum := 0 for _, addr := range addrs { sum += int(addr.Weight) @@ -72,21 +68,21 @@ func (addrs byPriorityWeight) shuffleByWeight() { } } -func (addrs byPriorityWeight) sortRfc2782() { +func (addrs byPriorityWeight) sortRfc2782(rand *rand.Rand) { sort.Sort(addrs) i := 0 for j := 1; j < len(addrs); j++ { if addrs[i].Priority != addrs[j].Priority { - addrs[i:j].shuffleByWeight() + addrs[i:j].shuffleByWeight(rand) i = j } } - addrs[i:].shuffleByWeight() + addrs[i:].shuffleByWeight(rand) } // SortRfc2782 reorders SRV records as specified in RFC 2782. func SortRfc2782(srvs []*net.SRV) { - byPriorityWeight(srvs).sortRfc2782() + byPriorityWeight(srvs).sortRfc2782(rand.New(rand.NewSource(time.Now().UTC().UnixNano()))) } // SplitHostPort is an alternative to net.SplitHostPort that also parses the diff --git a/go/netutil/netutil_test.go b/go/netutil/netutil_test.go index 574bda5f26b..ab76597936b 100644 --- a/go/netutil/netutil_test.go +++ b/go/netutil/netutil_test.go @@ -22,9 +22,10 @@ import ( "net" "reflect" "testing" + "time" ) -func checkDistribution(t *testing.T, data []*net.SRV, margin float64) { +func checkDistribution(t *testing.T, rand *rand.Rand, data []*net.SRV, margin float64) { sum := 0 for _, srv := range data { sum += int(srv.Weight) @@ -36,7 +37,7 @@ func checkDistribution(t *testing.T, data []*net.SRV, margin float64) { for j := 0; j < count; j++ { d := make([]*net.SRV, len(data)) copy(d, data) - byPriorityWeight(d).shuffleByWeight() + byPriorityWeight(d).shuffleByWeight(rand) key := d[0].Target results[key] = results[key] + 1 } @@ -54,12 +55,11 @@ func checkDistribution(t *testing.T, data []*net.SRV, margin float64) { } func testUniformity(t *testing.T, size int, margin float64) { - rand.Seed(1) data := make([]*net.SRV, size) for i := 0; i < size; i++ { data[i] = &net.SRV{Target: fmt.Sprintf("%c", 'a'+i), Weight: 1} } - checkDistribution(t, data, margin) + checkDistribution(t, rand.New(rand.NewSource(time.Now().UTC().UnixNano())), data, margin) } func TestUniformity(t *testing.T) { @@ -70,13 +70,12 @@ func TestUniformity(t *testing.T) { } func testWeighting(t *testing.T, margin float64) { - rand.Seed(1) data := []*net.SRV{ {Target: "a", Weight: 60}, {Target: "b", Weight: 30}, {Target: "c", Weight: 10}, } - checkDistribution(t, data, margin) + checkDistribution(t, rand.New(rand.NewSource(1)), data, margin) } func TestWeighting(t *testing.T) { diff --git a/go/test/endtoend/tabletgateway/buffer/buffer_test_helpers.go b/go/test/endtoend/tabletgateway/buffer/buffer_test_helpers.go index 843c6800622..8a3dd4f9b73 100644 --- a/go/test/endtoend/tabletgateway/buffer/buffer_test_helpers.go +++ b/go/test/endtoend/tabletgateway/buffer/buffer_test_helpers.go @@ -251,7 +251,6 @@ func (bt *BufferingTest) createCluster() (*cluster.LocalProcessCluster, int) { if err := clusterInstance.StartVtgate(); err != nil { return nil, 1 } - rand.Seed(time.Now().UnixNano()) return clusterInstance, 0 } diff --git a/go/test/endtoend/vreplication/cluster_test.go b/go/test/endtoend/vreplication/cluster_test.go index 53384ea235a..9861f71f6fd 100644 --- a/go/test/endtoend/vreplication/cluster_test.go +++ b/go/test/endtoend/vreplication/cluster_test.go @@ -307,7 +307,6 @@ func init() { if os.Getenv("VREPLICATION_E2E_DEBUG") != "" { debugMode = true } - rand.Seed(time.Now().UTC().UnixNano()) originalVtdataroot = os.Getenv("VTDATAROOT") var mainVtDataRoot string if debugMode { diff --git a/go/test/endtoend/vtgate/keyspace_watches/keyspace_watch_test.go b/go/test/endtoend/vtgate/keyspace_watches/keyspace_watch_test.go index ab844a8ffd1..4971d03060b 100644 --- a/go/test/endtoend/vtgate/keyspace_watches/keyspace_watch_test.go +++ b/go/test/endtoend/vtgate/keyspace_watches/keyspace_watch_test.go @@ -23,10 +23,8 @@ package keyspacewatches import ( "database/sql" "fmt" - "math/rand" "os" "testing" - "time" _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/require" @@ -115,7 +113,6 @@ func createCluster(extraVTGateArgs []string) (*cluster.LocalProcessCluster, int) Host: clusterInstance.Hostname, Port: clusterInstance.VtgateMySQLPort, } - rand.Seed(time.Now().UnixNano()) return clusterInstance, 0 } diff --git a/go/vt/tableacl/testlib/testlib.go b/go/vt/tableacl/testlib/testlib.go index 3c30c43d8dc..bdde9ae800f 100644 --- a/go/vt/tableacl/testlib/testlib.go +++ b/go/vt/tableacl/testlib/testlib.go @@ -21,7 +21,6 @@ import ( "fmt" "math/rand" "testing" - "time" querypb "vitess.io/vitess/go/vt/proto/query" tableaclpb "vitess.io/vitess/go/vt/proto/tableacl" @@ -127,7 +126,3 @@ func checkAccess(config *tableaclpb.Config, tableName string, role tableacl.Role } return nil } - -func init() { - rand.Seed(time.Now().UnixNano()) -} diff --git a/go/vt/topo/memorytopo/memorytopo.go b/go/vt/topo/memorytopo/memorytopo.go index 8619ad023fe..f24b2f6c89e 100644 --- a/go/vt/topo/memorytopo/memorytopo.go +++ b/go/vt/topo/memorytopo/memorytopo.go @@ -25,7 +25,6 @@ import ( "math/rand" "strings" "sync" - "time" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/topo" @@ -349,7 +348,3 @@ func (f *Factory) recursiveDelete(n *node) { f.recursiveDelete(parent) } } - -func init() { - rand.Seed(time.Now().UnixNano()) -} diff --git a/go/vt/topotools/shard_test.go b/go/vt/topotools/shard_test.go index f38ba315034..f2fb5f50340 100644 --- a/go/vt/topotools/shard_test.go +++ b/go/vt/topotools/shard_test.go @@ -22,7 +22,6 @@ import ( "math/rand" "sync" "testing" - "time" topodatapb "vitess.io/vitess/go/vt/proto/topodata" "vitess.io/vitess/go/vt/topo/memorytopo" @@ -109,7 +108,6 @@ func TestGetOrCreateShard(t *testing.T) { // and do massive parallel GetOrCreateShard keyspace := "test_keyspace" wg := sync.WaitGroup{} - rand.Seed(time.Now().UnixNano()) for i := 0; i < 100; i++ { wg.Add(1) go func(i int) { diff --git a/go/vt/vttablet/tabletserver/gc/tablegc.go b/go/vt/vttablet/tabletserver/gc/tablegc.go index 80d6f6242b3..1e23b7440f8 100644 --- a/go/vt/vttablet/tabletserver/gc/tablegc.go +++ b/go/vt/vttablet/tabletserver/gc/tablegc.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "math" - "math/rand" "sort" "sync" "sync/atomic" @@ -84,10 +83,6 @@ type transitionRequest struct { uuid string } -func init() { - rand.Seed(time.Now().UnixNano()) -} - // TableGC is the main entity in the table garbage collection mechanism. // This service "garbage collects" tables: // - it checks for magically-named tables (e.g. _vt_EVAC_f6338b2af8af11eaa210f875a4d24e90_20200920063522) diff --git a/go/vt/vttablet/tabletserver/tabletserver_test.go b/go/vt/vttablet/tabletserver/tabletserver_test.go index f8fa06f5277..4e99082d5f6 100644 --- a/go/vt/vttablet/tabletserver/tabletserver_test.go +++ b/go/vt/vttablet/tabletserver/tabletserver_test.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "io" - "math/rand" "net/http" "net/http/httptest" "os" @@ -2683,7 +2682,3 @@ func addTabletServerSupportedQueries(db *fakesqldb.DB) { }}, }) } - -func init() { - rand.Seed(time.Now().UnixNano()) -} diff --git a/go/vt/vttablet/tabletserver/throttle/throttler.go b/go/vt/vttablet/tabletserver/throttle/throttler.go index 260f024b72b..b0d4918e31c 100644 --- a/go/vt/vttablet/tabletserver/throttle/throttler.go +++ b/go/vt/vttablet/tabletserver/throttle/throttler.go @@ -110,10 +110,6 @@ const ( ThrottleCheckSelf ) -func init() { - rand.Seed(time.Now().UnixNano()) -} - // Throttler is the main entity in the throttling mechanism. This service runs, probes, collects data, // aggregates, reads inventory, provides information, etc. type Throttler struct { diff --git a/go/vt/vttablet/tabletserver/vstreamer/packet_size_test.go b/go/vt/vttablet/tabletserver/vstreamer/packet_size_test.go index ae71603ea52..5b1ee2b5a94 100644 --- a/go/vt/vttablet/tabletserver/vstreamer/packet_size_test.go +++ b/go/vt/vttablet/tabletserver/vstreamer/packet_size_test.go @@ -21,6 +21,8 @@ import ( "math/rand" "testing" "time" + + "github.com/stretchr/testify/assert" ) type polynomial []float64 @@ -89,9 +91,6 @@ func TestPacketSizeSimulation(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - seed := time.Now().UnixNano() - rand.Seed(seed) - // Simulate a replication using the given polynomial and the dynamic packet sizer ps1 := newDynamicPacketSizer(tc.baseSize) elapsed1, sent1 := simulate(t, ps1, tc.baseSize, tc.baseSize*1000, tc.p.fit) @@ -103,12 +102,8 @@ func TestPacketSizeSimulation(t *testing.T) { // the simulation for dynamic packet sizing should always be faster then the fixed packet, // and should also send fewer packets in total delta := elapsed1 - elapsed2 - if delta > tc.error { - t.Errorf("packet-adjusted simulation is %v slower than fixed approach, seed %d", delta, seed) - } - if sent1 > sent2 { - t.Errorf("packet-adjusted simulation sent more packets (%d) than fixed approach (%d), seed %d", sent1, sent2, seed) - } + assert.LessOrEqualf(t, tc.error, delta, "packet-adjusted simulation is %v slower than fixed approach", delta) + assert.LessOrEqualf(t, sent2, sent1, "packet-adjusted simulation sent more packets (%d) than fixed approach (%d)", sent1, sent2) // t.Logf("dynamic = (%v, %d), fixed = (%v, %d)", elapsed1, sent1, elapsed2, sent2) }) } diff --git a/go/vt/vttest/environment.go b/go/vt/vttest/environment.go index 864d081c284..f856d0f038a 100644 --- a/go/vt/vttest/environment.go +++ b/go/vt/vttest/environment.go @@ -22,7 +22,6 @@ import ( "os" "path" "strings" - "time" "vitess.io/vitess/go/vt/proto/vttest" @@ -301,10 +300,6 @@ func defaultEnvFactory() (Environment, error) { return NewLocalTestEnv("", 0) } -func init() { - rand.Seed(time.Now().UnixNano()) -} - // NewDefaultEnv is an user-configurable callback that returns a new Environment // instance with the default settings. // This callback is only used in cases where the user hasn't explicitly set diff --git a/test/client/client.go b/test/client/client.go index df1d13e7d57..d1a174d8d04 100644 --- a/test/client/client.go +++ b/test/client/client.go @@ -42,7 +42,6 @@ var ( func main() { pflag.Parse() - rand.Seed(time.Now().UnixNano()) // Connect to vtgate. db, err := vitessdriver.Open(*server, "@primary")