Skip to content

Commit

Permalink
multiple: switch to math/rand/v2 (#7711)
Browse files Browse the repository at this point in the history
Co-authored-by: Arvind Bright <[email protected]>
  • Loading branch information
marcoferrer and arvindbr8 authored Oct 29, 2024
1 parent 6fd86d3 commit 52d7f6a
Show file tree
Hide file tree
Showing 31 changed files with 69 additions and 69 deletions.
5 changes: 3 additions & 2 deletions balancer/endpointsharding/endpointsharding.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ package endpointsharding
import (
"encoding/json"
"errors"
"math/rand"
"sync"
"sync/atomic"

rand "math/rand/v2"

"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/base"
"google.golang.org/grpc/connectivity"
Expand Down Expand Up @@ -225,7 +226,7 @@ func (es *endpointSharding) updateState() {
p := &pickerWithChildStates{
pickers: pickers,
childStates: childStates,
next: uint32(rand.Intn(len(pickers))),
next: uint32(rand.IntN(len(pickers))),
}
es.cc.UpdateState(balancer.State{
ConnectivityState: aggState,
Expand Down
6 changes: 3 additions & 3 deletions balancer/grpclb/grpclb_picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package grpclb

import (
"math/rand"
rand "math/rand/v2"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -112,7 +112,7 @@ type rrPicker struct {
func newRRPicker(readySCs []balancer.SubConn) *rrPicker {
return &rrPicker{
subConns: readySCs,
subConnsNext: rand.Intn(len(readySCs)),
subConnsNext: rand.IntN(len(readySCs)),
}
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func newLBPicker(serverList []*lbpb.Server, readySCs []balancer.SubConn, stats *
return &lbPicker{
serverList: serverList,
subConns: readySCs,
subConnsNext: rand.Intn(len(readySCs)),
subConnsNext: rand.IntN(len(readySCs)),
stats: stats,
}
}
Expand Down
2 changes: 1 addition & 1 deletion balancer/leastrequest/leastrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package leastrequest
import (
"encoding/json"
"fmt"
"math/rand"
rand "math/rand/v2"
"sync/atomic"

"google.golang.org/grpc/balancer"
Expand Down
2 changes: 1 addition & 1 deletion balancer/pickfirst/pickfirst.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math/rand"
rand "math/rand/v2"

"google.golang.org/grpc/balancer"
"google.golang.org/grpc/balancer/pickfirst/internal"
Expand Down
6 changes: 3 additions & 3 deletions balancer/rls/internal/adaptive/adaptive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
package adaptive

import (
"math/rand"
rand "math/rand/v2"
"sync"
"time"
)

// For overriding in unittests.
var (
timeNowFunc = func() time.Time { return time.Now() }
randFunc = func() float64 { return rand.Float64() }
timeNowFunc = time.Now
randFunc = rand.Float64
)

const (
Expand Down
4 changes: 2 additions & 2 deletions balancer/roundrobin/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package roundrobin

import (
"math/rand"
rand "math/rand/v2"
"sync/atomic"

"google.golang.org/grpc/balancer"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (*rrPickerBuilder) Build(info base.PickerBuildInfo) balancer.Picker {
// Start at a random index, as the same RR balancer rebuilds a new
// picker when SubConn states change, and we don't want to apply excess
// load to the first server in the list.
next: uint32(rand.Intn(len(scs))),
next: uint32(rand.IntN(len(scs))),
}
}

Expand Down
2 changes: 1 addition & 1 deletion balancer/weightedroundrobin/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math/rand"
rand "math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand Down
6 changes: 3 additions & 3 deletions benchmark/benchmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
"fmt"
"io"
"log"
"math/rand"
rand "math/rand/v2"
"net"
"os"
"reflect"
Expand Down Expand Up @@ -283,7 +283,7 @@ func unconstrainedStreamBenchmark(start startFunc, stop ucStopFunc, bf stats.Fea
defer wg.Done()
for {
if maxSleep > 0 {
time.Sleep(time.Duration(rand.Intn(maxSleep)))
time.Sleep(time.Duration(rand.IntN(maxSleep)))
}
t := time.Now()
if t.After(bmEnd) {
Expand Down Expand Up @@ -574,7 +574,7 @@ func runBenchmark(caller rpcCallFunc, start startFunc, stop stopFunc, bf stats.F
defer wg.Done()
for {
if maxSleep > 0 {
time.Sleep(time.Duration(rand.Intn(maxSleep)))
time.Sleep(time.Duration(rand.IntN(maxSleep)))
}
t := time.Now()
if t.After(bmEnd) {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"fmt"
"io"
"log"
"math/rand"
rand "math/rand/v2"
"net"
"strconv"
"time"
Expand Down Expand Up @@ -187,7 +187,7 @@ func (s *testServer) UnconstrainedStreamingCall(stream testgrpc.BenchmarkService
go func() {
for {
if maxSleep > 0 {
time.Sleep(time.Duration(rand.Intn(maxSleep)))
time.Sleep(time.Duration(rand.IntN(maxSleep)))
}
var err error
if preloadMsgSize > 0 {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/stats/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"encoding/hex"
"fmt"
"math"
"math/rand"
rand "math/rand/v2"
"os"
"sort"
"strconv"
Expand Down Expand Up @@ -74,7 +74,7 @@ func (pcr *payloadCurveRange) chooseRandom() int {
return int(pcr.from)
}

return int(rand.Int31n(pcr.to-pcr.from+1) + pcr.from)
return int(rand.Int32N(pcr.to-pcr.from+1) + pcr.from)
}

// sha256file is a helper function that returns a hex string matching the
Expand Down
2 changes: 1 addition & 1 deletion benchmark/worker/benchmark_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"context"
"flag"
"math"
"math/rand"
rand "math/rand/v2"
"runtime"
"sync"
"time"
Expand Down
4 changes: 2 additions & 2 deletions examples/features/debugging/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package main
import (
"context"
"log"
"math/rand"
rand "math/rand/v2"
"net"
"time"

Expand Down Expand Up @@ -55,7 +55,7 @@ type slowServer struct {
// SayHello implements helloworld.GreeterServer
func (s *slowServer) SayHello(_ context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
// Delay 100ms ~ 200ms before replying
time.Sleep(time.Duration(100+rand.Intn(100)) * time.Millisecond)
time.Sleep(time.Duration(100+rand.IntN(100)) * time.Millisecond)
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion examples/features/xds/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"flag"
"fmt"
"log"
"math/rand"
rand "math/rand/v2"
"net"
"os"

Expand Down
13 changes: 6 additions & 7 deletions examples/route_guide/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"flag"
"io"
"log"
"math/rand"
rand "math/rand/v2"
"time"

"google.golang.org/grpc"
Expand Down Expand Up @@ -81,11 +81,10 @@ func printFeatures(client pb.RouteGuideClient, rect *pb.Rectangle) {
// runRecordRoute sends a sequence of points to server and expects to get a RouteSummary from server.
func runRecordRoute(client pb.RouteGuideClient) {
// Create a random number of random points
r := rand.New(rand.NewSource(time.Now().UnixNano()))
pointCount := int(r.Int31n(100)) + 2 // Traverse at least two points
pointCount := int(rand.Int32N(100)) + 2 // Traverse at least two points
var points []*pb.Point
for i := 0; i < pointCount; i++ {
points = append(points, randomPoint(r))
points = append(points, randomPoint())
}
log.Printf("Traversing %d points.", len(points))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down Expand Up @@ -146,9 +145,9 @@ func runRouteChat(client pb.RouteGuideClient) {
<-waitc
}

func randomPoint(r *rand.Rand) *pb.Point {
lat := (r.Int31n(180) - 90) * 1e7
long := (r.Int31n(360) - 180) * 1e7
func randomPoint() *pb.Point {
lat := (rand.Int32N(180) - 90) * 1e7
long := (rand.Int32N(360) - 180) * 1e7
return &pb.Point{Latitude: lat, Longitude: long}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/backoff/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package backoff
import (
"context"
"errors"
"math/rand"
rand "math/rand/v2"
"time"

grpcbackoff "google.golang.org/grpc/backoff"
Expand Down
4 changes: 2 additions & 2 deletions internal/resolver/dns/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
rand "math/rand/v2"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -425,7 +425,7 @@ func chosenByPercentage(a *int) bool {
if a == nil {
return true
}
return rand.Intn(100)+1 <= *a
return rand.IntN(100)+1 <= *a
}

func canaryingSC(js string) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/serviceconfig/duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package serviceconfig
import (
"fmt"
"math"
"math/rand"
rand "math/rand/v2"
"strings"
"testing"
"time"
Expand Down
4 changes: 2 additions & 2 deletions internal/transport/http2_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
rand "math/rand/v2"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -1455,7 +1455,7 @@ func getJitter(v time.Duration) time.Duration {
}
// Generate a jitter between +/- 10% of the value.
r := int64(v / 10)
j := rand.Int63n(2*r) - r
j := rand.Int64N(2*r) - r
return time.Duration(j)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/wrr/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package wrr

import (
"fmt"
"math/rand"
rand "math/rand/v2"
"sort"
)

Expand All @@ -46,19 +46,19 @@ func NewRandom() WRR {
return &randomWRR{}
}

var randInt63n = rand.Int63n
var randInt64n = rand.Int64N

func (rw *randomWRR) Next() (item any) {
if len(rw.items) == 0 {
return nil
}
if rw.equalWeights {
return rw.items[randInt63n(int64(len(rw.items)))].item
return rw.items[randInt64n(int64(len(rw.items)))].item
}

sumOfWeights := rw.items[len(rw.items)-1].accumulatedWeight
// Random number in [0, sumOfWeights).
randomWeight := randInt63n(sumOfWeights)
randomWeight := randInt64n(sumOfWeights)
// Item's accumulated weights are in ascending order, because item's weight >= 0.
// Binary search rw.items to find first item whose accumulatedWeight > randomWeight
// The return i is guaranteed to be in range [0, len(rw.items)) because randomWeight < last item's accumulatedWeight
Expand Down
8 changes: 4 additions & 4 deletions internal/wrr/wrr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package wrr
import (
"errors"
"math"
"math/rand"
rand "math/rand/v2"
"strconv"
"testing"

Expand Down Expand Up @@ -146,7 +146,7 @@ func BenchmarkRandomWRRNext(b *testing.B) {
w := NewRandom()
var sumOfWeights int64
for i := 0; i < n; i++ {
weight := rand.Int63n(maxWeight + 1)
weight := rand.Int64N(maxWeight + 1)
w.Add(i, weight)
sumOfWeights += weight
}
Expand Down Expand Up @@ -188,6 +188,6 @@ func BenchmarkRandomWRRNext(b *testing.B) {
}

func init() {
r := rand.New(rand.NewSource(0))
randInt63n = r.Int63n
r := rand.New(rand.NewPCG(0, 0))
randInt64n = r.Int64N
}
4 changes: 2 additions & 2 deletions interop/stress/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"context"
"flag"
"fmt"
"math/rand"
rand "math/rand/v2"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -130,7 +130,7 @@ func newWeightedRandomTestSelector(tests []testCaseWithWeight) *weightedRandomTe
}

func (selector weightedRandomTestSelector) getNextTest() string {
random := rand.Intn(selector.totalWeight)
random := rand.IntN(selector.totalWeight)
var weightSofar int
for _, test := range selector.tests {
weightSofar += test.weight
Expand Down
4 changes: 2 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"errors"
"io"
"math"
"math/rand"
rand "math/rand/v2"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -710,7 +710,7 @@ func (a *csAttempt) shouldRetry(err error) (bool, error) {
if max := float64(rp.MaxBackoff); cur > max {
cur = max
}
dur = time.Duration(rand.Int63n(int64(cur)))
dur = time.Duration(rand.Int64N(int64(cur)))
cs.numRetriesSincePushback++
}

Expand Down
Loading

0 comments on commit 52d7f6a

Please sign in to comment.