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..b8cfc563acb 100644 --- a/go/netutil/netutil_test.go +++ b/go/netutil/netutil_test.go @@ -24,7 +24,7 @@ import ( "testing" ) -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 +36,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 +54,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(1)), data, margin) } func TestUniformity(t *testing.T) { @@ -70,13 +69,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/tools/asthelpergen/asthelpergen.go b/go/tools/asthelpergen/asthelpergen.go index c0e870c21c5..89aa40b6127 100644 --- a/go/tools/asthelpergen/asthelpergen.go +++ b/go/tools/asthelpergen/asthelpergen.go @@ -26,6 +26,8 @@ import ( "strings" "github.com/dave/jennifer/jen" + "golang.org/x/text/cases" + "golang.org/x/text/language" "golang.org/x/tools/go/packages" "vitess.io/vitess/go/tools/codegen" @@ -304,7 +306,7 @@ func printableTypeName(t types.Type) string { case *types.Named: return t.Obj().Name() case *types.Basic: - return strings.Title(t.Name()) // nolint + return cases.Title(language.AmericanEnglish).String(t.Name()) case *types.Interface: return t.String() default: diff --git a/go/vt/mysqlctl/grpcmysqlctlclient/client.go b/go/vt/mysqlctl/grpcmysqlctlclient/client.go index 77d2cdbbfd0..150402a8c44 100644 --- a/go/vt/mysqlctl/grpcmysqlctlclient/client.go +++ b/go/vt/mysqlctl/grpcmysqlctlclient/client.go @@ -26,6 +26,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/status" "vitess.io/vitess/go/vt/grpcclient" @@ -41,9 +42,14 @@ type client struct { func factory(network, addr string) (mysqlctlclient.MysqlctlClient, error) { // create the RPC client - cc, err := grpcclient.Dial(addr, grpcclient.FailFast(false), grpc.WithInsecure(), grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { //nolint:staticcheck - return net.DialTimeout(network, addr, timeout) - })) + cc, err := grpcclient.Dial( + addr, + grpcclient.FailFast(false), + grpc.WithTransportCredentials(insecure.NewCredentials()), + grpc.WithContextDialer(func(ctx context.Context, addr string, + ) (net.Conn, error) { + return new(net.Dialer).DialContext(ctx, network, addr) + })) if err != nil { return nil, err } 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/tlstest/tlstest.go b/go/vt/tlstest/tlstest.go index 0529ea4ef09..ae560115e8d 100644 --- a/go/vt/tlstest/tlstest.go +++ b/go/vt/tlstest/tlstest.go @@ -348,8 +348,8 @@ func RevokeCertAndRegenerateCRL(root, parent, name string) { log.Fatal(err) } - revoked := crlList.RevokedCertificates - revoked = append(revoked, pkix.RevokedCertificate{ + revoked := crlList.RevokedCertificateEntries + revoked = append(revoked, x509.RevocationListEntry{ SerialNumber: certificate.SerialNumber, RevocationTime: time.Now(), }) @@ -365,8 +365,8 @@ func RevokeCertAndRegenerateCRL(root, parent, name string) { var crlNumber big.Int newCrl, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{ - RevokedCertificates: revoked, - Number: crlNumber.Add(crlList.Number, big.NewInt(1)), + RevokedCertificateEntries: revoked, + Number: crlNumber.Add(crlList.Number, big.NewInt(1)), }, caCert, caKey.(crypto.Signer)) if err != nil { log.Fatal(err) 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..35542e0a879 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 @@ -33,7 +35,7 @@ func (p polynomial) fit(x float64) float64 { return y } -func simulate(t *testing.T, ps PacketSizer, base, mustSend int, interpolate func(float64) float64) (time.Duration, int) { +func simulate(t *testing.T, rand *rand.Rand, ps PacketSizer, base, mustSend int, interpolate func(float64) float64) (time.Duration, int) { t.Helper() var elapsed time.Duration @@ -90,25 +92,21 @@ func TestPacketSizeSimulation(t *testing.T) { for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { seed := time.Now().UnixNano() - rand.Seed(seed) + rand := rand.New(rand.NewSource(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) + elapsed1, sent1 := simulate(t, rand, ps1, tc.baseSize, tc.baseSize*1000, tc.p.fit) // Simulate the same polynomial using a fixed packet size ps2 := newFixedPacketSize(tc.baseSize) - elapsed2, sent2 := simulate(t, ps2, tc.baseSize, tc.baseSize*1000, tc.p.fit) + elapsed2, sent2 := simulate(t, rand, ps2, tc.baseSize, tc.baseSize*1000, tc.p.fit) // 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, delta, tc.error, "packet-adjusted simulation is %v slower than fixed approach", delta) + assert.LessOrEqualf(t, sent1, sent2, "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/go/vt/vttls/crl.go b/go/vt/vttls/crl.go index 63c97ecfa81..fde4f0b2b66 100644 --- a/go/vt/vttls/crl.go +++ b/go/vt/vttls/crl.go @@ -33,7 +33,7 @@ func certIsRevoked(cert *x509.Certificate, crl *x509.RevocationList) bool { log.Warningf("The current Certificate Revocation List (CRL) is past expiry date and must be updated. Revoked certificates will still be rejected in this state.") } - for _, revoked := range crl.RevokedCertificates { + for _, revoked := range crl.RevokedCertificateEntries { if cert.SerialNumber.Cmp(revoked.SerialNumber) == 0 { return true } 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")