Skip to content

Commit

Permalink
Stop seeding rand with timestamps, it no longer auto-seeds with a con…
Browse files Browse the repository at this point in the history
…stant

See golang/go#56319

Signed-off-by: Andrew Mason <andrew@planetscale.com>
  • Loading branch information
Andrew Mason committed Aug 30, 2023
1 parent 07a12fc commit 8e554c7
Show file tree
Hide file tree
Showing 17 changed files with 14 additions and 66 deletions.
1 change: 0 additions & 1 deletion examples/compose/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ var (

func main() {
pflag.Parse()
rand.Seed(time.Now().UnixNano())

// Connect to vtgate.
db, err := vitessdriver.Open(*server, "@primary")
Expand Down
1 change: 0 additions & 1 deletion go/cmd/vtclient/vtclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions go/cmd/vtgate/vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package main

import (
"context"
"math/rand"
"strings"
"time"

"github.com/spf13/pflag"

Expand Down Expand Up @@ -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()
Expand Down
14 changes: 5 additions & 9 deletions go/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions go/netutil/netutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion go/test/endtoend/vreplication/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 0 additions & 5 deletions go/vt/tableacl/testlib/testlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -127,7 +126,3 @@ func checkAccess(config *tableaclpb.Config, tableName string, role tableacl.Role
}
return nil
}

func init() {
rand.Seed(time.Now().UnixNano())
}
5 changes: 0 additions & 5 deletions go/vt/topo/memorytopo/memorytopo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"math/rand"
"strings"
"sync"
"time"

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
Expand Down Expand Up @@ -349,7 +348,3 @@ func (f *Factory) recursiveDelete(n *node) {
f.recursiveDelete(parent)
}
}

func init() {
rand.Seed(time.Now().UnixNano())
}
2 changes: 0 additions & 2 deletions go/vt/topotools/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 0 additions & 5 deletions go/vt/vttablet/tabletserver/gc/tablegc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"math"
"math/rand"
"sort"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions go/vt/vttablet/tabletserver/tabletserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -2683,7 +2682,3 @@ func addTabletServerSupportedQueries(db *fakesqldb.DB) {
}},
})
}

func init() {
rand.Seed(time.Now().UnixNano())
}
4 changes: 0 additions & 4 deletions go/vt/vttablet/tabletserver/throttle/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 4 additions & 9 deletions go/vt/vttablet/tabletserver/vstreamer/packet_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

type polynomial []float64
Expand Down Expand Up @@ -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)
Expand All @@ -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)
})
}
Expand Down
5 changes: 0 additions & 5 deletions go/vt/vttest/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"path"
"strings"
"time"

"vitess.io/vitess/go/vt/proto/vttest"

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion test/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ var (

func main() {
pflag.Parse()
rand.Seed(time.Now().UnixNano())

// Connect to vtgate.
db, err := vitessdriver.Open(*server, "@primary")
Expand Down

0 comments on commit 8e554c7

Please sign in to comment.