Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: remove unnecessary rand init #6207

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions pkg/core/region_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package core
import (
"bytes"
"math/rand"
"time"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/log"
Expand Down Expand Up @@ -343,7 +342,3 @@ func (t *regionTree) TotalWriteRate() (bytesRate, keysRate float64) {
}
return t.totalWriteBytesRate, t.totalWriteKeysRate
}

func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
}
2 changes: 0 additions & 2 deletions pkg/mcs/tso/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"crypto/tls"
"fmt"
"math/rand"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -627,7 +626,6 @@ func (s *Server) startServer() (err error) {

// CreateServer creates the Server
func CreateServer(ctx context.Context, cfg *Config) *Server {
rand.New(rand.NewSource(time.Now().UnixNano()))
svr := &Server{
DiagnosticsServer: sysutil.NewDiagnosticsServer(cfg.Log.File.Filename),
startTimestamp: time.Now().Unix(),
Expand Down
3 changes: 2 additions & 1 deletion pkg/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ func (m *EmbeddedEtcdMember) ResignEtcdLeader(ctx context.Context, from string,
if len(etcdLeaderIDs) == 0 {
return errors.New("no valid pd to transfer etcd leader")
}
nextEtcdLeaderID := etcdLeaderIDs[rand.Intn(len(etcdLeaderIDs))]
r := rand.New(rand.NewSource(time.Now().UnixNano()))
nextEtcdLeaderID := etcdLeaderIDs[r.Intn(len(etcdLeaderIDs))]
return m.MoveEtcdLeader(ctx, m.ID(), nextEtcdLeaderID)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/movingaverage/moving_average_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
)

func addRandData(ma MovingAvg, n int, mx float64) {
rand.New(rand.NewSource(time.Now().UnixNano()))
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < n; i++ {
ma.Add(rand.Float64() * mx)
ma.Add(r.Float64() * mx)
}
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/schedule/filter/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package filter
import (
"math/rand"
"sort"
"time"

"github.com/tikv/pd/pkg/core"
"github.com/tikv/pd/pkg/schedule/config"
Expand All @@ -26,12 +27,13 @@ import (
// StoreCandidates wraps store list and provide utilities to select source or
// target store to schedule.
type StoreCandidates struct {
r *rand.Rand
Stores []*core.StoreInfo
}

// NewCandidates creates StoreCandidates with store list.
func NewCandidates(stores []*core.StoreInfo) *StoreCandidates {
return &StoreCandidates{Stores: stores}
return &StoreCandidates{r: rand.New(rand.NewSource(time.Now().UnixNano())), Stores: stores}
}

// FilterSource keeps stores that can pass all source filters.
Expand All @@ -54,7 +56,7 @@ func (c *StoreCandidates) Sort(less StoreComparer) *StoreCandidates {

// Shuffle reorders all candidates randomly.
func (c *StoreCandidates) Shuffle() *StoreCandidates {
rand.Shuffle(len(c.Stores), func(i, j int) { c.Stores[i], c.Stores[j] = c.Stores[j], c.Stores[i] })
c.r.Shuffle(len(c.Stores), func(i, j int) { c.Stores[i], c.Stores[j] = c.Stores[j], c.Stores[i] })
return c
}

Expand Down Expand Up @@ -108,7 +110,7 @@ func (c *StoreCandidates) RandomPick() *core.StoreInfo {
if len(c.Stores) == 0 {
return nil
}
return c.Stores[rand.Intn(len(c.Stores))]
return c.Stores[c.r.Intn(len(c.Stores))]
}

// PickAll return all stores in candidate list.
Expand Down
5 changes: 0 additions & 5 deletions pkg/schedule/waiting_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package schedule

import (
"math/rand"
"time"

"github.com/tikv/pd/pkg/schedule/operator"
)
Expand Down Expand Up @@ -118,7 +117,3 @@ func NewWaitingOperatorStatus() *WaitingOperatorStatus {
make(map[string]uint64),
}
}

func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
}
3 changes: 2 additions & 1 deletion pkg/utils/etcdutil/etcdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ func InitOrGetClusterID(c *clientv3.Client, key string) (uint64, error) {
defer cancel()

// Generate a random cluster ID.
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ts := uint64(time.Now().Unix())
clusterID := (ts << 32) + uint64(rand.Uint32())
clusterID := (ts << 32) + uint64(r.Uint32())
value := typeutil.Uint64ToBytes(clusterID)

// Multiple servers may try to init the cluster ID at the same time.
Expand Down
2 changes: 0 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"context"
"fmt"
"math/rand"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -219,7 +218,6 @@ func CreateServer(ctx context.Context, cfg *config.Config, services []string, le
mode = APIServiceMode
}
log.Info(fmt.Sprintf("%s config", mode), zap.Reflect("config", cfg))
rand.New(rand.NewSource(time.Now().UnixNano()))
serviceMiddlewareCfg := config.NewServiceMiddlewareConfig()

s := &Server{
Expand Down