From 102a081dd99e163ebf7424722a39bcb229fe6000 Mon Sep 17 00:00:00 2001 From: chrislu Date: Wed, 27 Aug 2025 13:45:31 -0700 Subject: [PATCH] math/rand/v2 --- test/s3/copying/s3_copying_test.go | 2 +- test/s3/iam/s3_iam_framework.go | 2 +- .../write_files/write_files.go | 2 +- weed/filer/filechunks_test.go | 2 +- weed/mount/weedfs.go | 4 ++-- weed/mq/broker/broker_connect.go | 9 +++++---- weed/mq/broker/broker_grpc_pub.go | 4 ++-- weed/mq/pub_balancer/allocate.go | 11 ++++++----- weed/mq/pub_balancer/balance_brokers.go | 5 +++-- weed/mq/pub_balancer/repair.go | 7 ++++--- weed/sftpd/auth/password.go | 4 ++-- weed/sftpd/user/user.go | 4 ++-- weed/shell/shell_liner.go | 15 ++++++++------- weed/storage/volume_vacuum_test.go | 4 ++-- weed/util/skiplist/skiplist_test.go | 4 ++-- 15 files changed, 42 insertions(+), 37 deletions(-) diff --git a/test/s3/copying/s3_copying_test.go b/test/s3/copying/s3_copying_test.go index 4bad01de4..75ea161d3 100644 --- a/test/s3/copying/s3_copying_test.go +++ b/test/s3/copying/s3_copying_test.go @@ -91,7 +91,7 @@ func waitForS3Service(t *testing.T, client *s3.Client, timeout time.Duration) { func getNewBucketName() string { timestamp := time.Now().UnixNano() // Add random suffix to prevent collisions when tests run quickly - randomSuffix := mathrand.Intn(100000) + randomSuffix := mathrand.IntN(100000) return fmt.Sprintf("%s%d-%d", defaultConfig.BucketPrefix, timestamp, randomSuffix) } diff --git a/test/s3/iam/s3_iam_framework.go b/test/s3/iam/s3_iam_framework.go index 622d9ddd8..8ec16c1bf 100644 --- a/test/s3/iam/s3_iam_framework.go +++ b/test/s3/iam/s3_iam_framework.go @@ -656,7 +656,7 @@ func (f *S3IAMTestFramework) GenerateUniqueBucketName(prefix string) string { testName = strings.ReplaceAll(testName, "_", "-") // Add random suffix to handle parallel tests - randomSuffix := mathrand.Intn(10000) + randomSuffix := mathrand.IntN(10000) return fmt.Sprintf("%s-%s-%d", prefix, testName, randomSuffix) } diff --git a/unmaintained/stress_filer_upload/write_files/write_files.go b/unmaintained/stress_filer_upload/write_files/write_files.go index 508e37d14..40c13d649 100644 --- a/unmaintained/stress_filer_upload/write_files/write_files.go +++ b/unmaintained/stress_filer_upload/write_files/write_files.go @@ -33,7 +33,7 @@ func main() { f, err := os.Create(fmt.Sprintf("%s/file%05d", *toDir, i)) check(err) - fileSize := *minSize + rand.Intn(*maxSize-*minSize) + fileSize := *minSize + rand.IntN(*maxSize-*minSize) startTime := time.Now() fmt.Printf("write %s %d bytes: ", f.Name(), fileSize) diff --git a/weed/filer/filechunks_test.go b/weed/filer/filechunks_test.go index 4af2af3f6..a99768d6e 100644 --- a/weed/filer/filechunks_test.go +++ b/weed/filer/filechunks_test.go @@ -71,7 +71,7 @@ func TestRandomFileChunksCompact(t *testing.T) { var chunks []*filer_pb.FileChunk for i := 0; i < 15; i++ { - start, stop := rand.Intn(len(data)), rand.Intn(len(data)) + start, stop := rand.IntN(len(data)), rand.IntN(len(data)) if start > stop { start, stop = stop, start } diff --git a/weed/mount/weedfs.go b/weed/mount/weedfs.go index 41896ff87..95864ef00 100644 --- a/weed/mount/weedfs.go +++ b/weed/mount/weedfs.go @@ -3,7 +3,7 @@ package mount import ( "context" "errors" - "math/rand" + "math/rand/v2" "os" "path" "path/filepath" @@ -110,7 +110,7 @@ func NewSeaweedFileSystem(option *Option) *WFS { fhLockTable: util.NewLockTable[FileHandleId](), } - wfs.option.filerIndex = int32(rand.Intn(len(option.FilerAddresses))) + wfs.option.filerIndex = int32(rand.IntN(len(option.FilerAddresses))) wfs.option.setupUniqueCacheDirectory() if option.CacheSizeMBForRead > 0 { wfs.chunkCache = chunk_cache.NewTieredChunkCache(256, option.getUniqueCacheDirForRead(), option.CacheSizeMBForRead, 1024*1024) diff --git a/weed/mq/broker/broker_connect.go b/weed/mq/broker/broker_connect.go index c92fc299c..c0f2192a4 100644 --- a/weed/mq/broker/broker_connect.go +++ b/weed/mq/broker/broker_connect.go @@ -3,12 +3,13 @@ package broker import ( "context" "fmt" + "io" + "math/rand/v2" + "time" + "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" - "io" - "math/rand" - "time" ) // BrokerConnectToBalancer connects to the broker balancer and sends stats @@ -61,7 +62,7 @@ func (b *MessageQueueBroker) BrokerConnectToBalancer(brokerBalancer string, stop } // glog.V(3).Infof("sent stats: %+v", stats) - time.Sleep(time.Millisecond*5000 + time.Duration(rand.Intn(1000))*time.Millisecond) + time.Sleep(time.Millisecond*5000 + time.Duration(rand.IntN(1000))*time.Millisecond) } }) } diff --git a/weed/mq/broker/broker_grpc_pub.go b/weed/mq/broker/broker_grpc_pub.go index c7cb81fcc..cd072503c 100644 --- a/weed/mq/broker/broker_grpc_pub.go +++ b/weed/mq/broker/broker_grpc_pub.go @@ -4,7 +4,7 @@ import ( "context" "fmt" "io" - "math/rand" + "math/rand/v2" "net" "sync/atomic" "time" @@ -71,7 +71,7 @@ func (b *MessageQueueBroker) PublishMessage(stream mq_pb.SeaweedMessaging_Publis var isClosed bool // process each published messages - clientName := fmt.Sprintf("%v-%4d", findClientAddress(stream.Context()), rand.Intn(10000)) + clientName := fmt.Sprintf("%v-%4d", findClientAddress(stream.Context()), rand.IntN(10000)) publisher := topic.NewLocalPublisher() localTopicPartition.Publishers.AddPublisher(clientName, publisher) diff --git a/weed/mq/pub_balancer/allocate.go b/weed/mq/pub_balancer/allocate.go index 46d423b30..efde44965 100644 --- a/weed/mq/pub_balancer/allocate.go +++ b/weed/mq/pub_balancer/allocate.go @@ -1,12 +1,13 @@ package pub_balancer import ( + "math/rand/v2" + "time" + cmap "github.com/orcaman/concurrent-map/v2" "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb" - "math/rand" - "time" ) func AllocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment) { @@ -43,7 +44,7 @@ func pickBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats], count int32) } pickedBrokers := make([]string, 0, count) for i := int32(0); i < count; i++ { - p := rand.Intn(len(candidates)) + p := rand.IntN(len(candidates)) pickedBrokers = append(pickedBrokers, candidates[p]) } return pickedBrokers @@ -59,7 +60,7 @@ func pickBrokersExcluded(brokers []string, count int, excludedLeadBroker string, if len(pickedBrokers) < count { pickedBrokers = append(pickedBrokers, broker) } else { - j := rand.Intn(i + 1) + j := rand.IntN(i + 1) if j < count { pickedBrokers[j] = broker } @@ -69,7 +70,7 @@ func pickBrokersExcluded(brokers []string, count int, excludedLeadBroker string, // shuffle the picked brokers count = len(pickedBrokers) for i := 0; i < count; i++ { - j := rand.Intn(count) + j := rand.IntN(count) pickedBrokers[i], pickedBrokers[j] = pickedBrokers[j], pickedBrokers[i] } diff --git a/weed/mq/pub_balancer/balance_brokers.go b/weed/mq/pub_balancer/balance_brokers.go index a6b25b7ca..06118bc8e 100644 --- a/weed/mq/pub_balancer/balance_brokers.go +++ b/weed/mq/pub_balancer/balance_brokers.go @@ -1,9 +1,10 @@ package pub_balancer import ( + "math/rand/v2" + cmap "github.com/orcaman/concurrent-map/v2" "github.com/seaweedfs/seaweedfs/weed/mq/topic" - "math/rand" ) func BalanceTopicPartitionOnBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats]) BalanceAction { @@ -28,7 +29,7 @@ func BalanceTopicPartitionOnBrokers(brokers cmap.ConcurrentMap[string, *BrokerSt maxPartitionCountPerBroker = brokerStats.Val.TopicPartitionCount sourceBroker = brokerStats.Key // select a random partition from the source broker - randomePartitionIndex := rand.Intn(int(brokerStats.Val.TopicPartitionCount)) + randomePartitionIndex := rand.IntN(int(brokerStats.Val.TopicPartitionCount)) index := 0 for topicPartitionStats := range brokerStats.Val.TopicPartitionStats.IterBuffered() { if index == randomePartitionIndex { diff --git a/weed/mq/pub_balancer/repair.go b/weed/mq/pub_balancer/repair.go index d16715406..9af81d27f 100644 --- a/weed/mq/pub_balancer/repair.go +++ b/weed/mq/pub_balancer/repair.go @@ -1,11 +1,12 @@ package pub_balancer import ( + "math/rand/v2" + "sort" + cmap "github.com/orcaman/concurrent-map/v2" "github.com/seaweedfs/seaweedfs/weed/mq/topic" - "math/rand" "modernc.org/mathutil" - "sort" ) func (balancer *PubBalancer) RepairTopics() []BalanceAction { @@ -56,7 +57,7 @@ func RepairMissingTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStat Topic: t, Partition: partition, }, - TargetBroker: candidates[rand.Intn(len(candidates))], + TargetBroker: candidates[rand.IntN(len(candidates))], }) } } diff --git a/weed/sftpd/auth/password.go b/weed/sftpd/auth/password.go index a42c3f5b8..21216d3ff 100644 --- a/weed/sftpd/auth/password.go +++ b/weed/sftpd/auth/password.go @@ -2,7 +2,7 @@ package auth import ( "fmt" - "math/rand" + "math/rand/v2" "time" "github.com/seaweedfs/seaweedfs/weed/sftpd/user" @@ -47,7 +47,7 @@ func (a *PasswordAuthenticator) Authenticate(conn ssh.ConnMetadata, password []b } // Add delay to prevent brute force attacks - time.Sleep(time.Duration(100+rand.Intn(100)) * time.Millisecond) + time.Sleep(time.Duration(100+rand.IntN(100)) * time.Millisecond) return nil, fmt.Errorf("authentication failed") } diff --git a/weed/sftpd/user/user.go b/weed/sftpd/user/user.go index 3c42988fd..9edaf1a6b 100644 --- a/weed/sftpd/user/user.go +++ b/weed/sftpd/user/user.go @@ -2,7 +2,7 @@ package user import ( - "math/rand" + "math/rand/v2" "path/filepath" ) @@ -22,7 +22,7 @@ func NewUser(username string) *User { // Generate a random UID/GID between 1000 and 60000 // This range is typically safe for regular users in most systems // 0-999 are often reserved for system users - randomId := 1000 + rand.Intn(59000) + randomId := 1000 + rand.IntN(59000) return &User{ Username: username, diff --git a/weed/shell/shell_liner.go b/weed/shell/shell_liner.go index 00884700b..0eb2ad4a3 100644 --- a/weed/shell/shell_liner.go +++ b/weed/shell/shell_liner.go @@ -3,19 +3,20 @@ package shell import ( "context" "fmt" - "github.com/seaweedfs/seaweedfs/weed/cluster" - "github.com/seaweedfs/seaweedfs/weed/pb" - "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" - "github.com/seaweedfs/seaweedfs/weed/util" - "github.com/seaweedfs/seaweedfs/weed/util/grace" "io" - "math/rand" + "math/rand/v2" "os" "path" "regexp" "slices" "strings" + "github.com/seaweedfs/seaweedfs/weed/cluster" + "github.com/seaweedfs/seaweedfs/weed/pb" + "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" + "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/seaweedfs/seaweedfs/weed/util/grace" + "github.com/peterh/liner" ) @@ -69,7 +70,7 @@ func RunShell(options ShellOptions) { fmt.Printf("master: %s ", *options.Masters) if len(filers) > 0 { fmt.Printf("filers: %v", filers) - commandEnv.option.FilerAddress = filers[rand.Intn(len(filers))] + commandEnv.option.FilerAddress = filers[rand.IntN(len(filers))] } fmt.Println() } diff --git a/weed/storage/volume_vacuum_test.go b/weed/storage/volume_vacuum_test.go index 797452bb3..710e4a76a 100644 --- a/weed/storage/volume_vacuum_test.go +++ b/weed/storage/volume_vacuum_test.go @@ -157,7 +157,7 @@ func doSomeWritesDeletes(i int, v *Volume, t *testing.T, infos []*needleInfo) { } // println("written file", i, "checksum", n.Checksum.Value(), "size", size) if rand.Float64() < 0.03 { - toBeDeleted := rand.Intn(i) + 1 + toBeDeleted := rand.IntN(i) + 1 oldNeedle := newEmptyNeedle(uint64(toBeDeleted)) v.deleteNeedle2(oldNeedle) // println("deleted file", toBeDeleted) @@ -175,7 +175,7 @@ type needleInfo struct { func newRandomNeedle(id uint64) *needle.Needle { n := new(needle.Needle) - n.Data = make([]byte, rand.Intn(1024)) + n.Data = make([]byte, rand.IntN(1024)) rand.Read(n.Data) n.Checksum = needle.NewCRC(n.Data) diff --git a/weed/util/skiplist/skiplist_test.go b/weed/util/skiplist/skiplist_test.go index cced73700..dcd313ff4 100644 --- a/weed/util/skiplist/skiplist_test.go +++ b/weed/util/skiplist/skiplist_test.go @@ -235,11 +235,11 @@ func TestFindGreaterOrEqual(t *testing.T) { list = New(memStore) for i := 0; i < maxN; i++ { - list.InsertByKey(Element(rand.Intn(maxNumber)), 0, Element(i)) + list.InsertByKey(Element(rand.IntN(maxNumber)), 0, Element(i)) } for i := 0; i < maxN; i++ { - key := Element(rand.Intn(maxNumber)) + key := Element(rand.IntN(maxNumber)) if _, v, ok, _ := list.FindGreaterOrEqual(key); ok { // if f is v should be bigger than the element before if v.Prev != nil && bytes.Compare(key, v.Prev.Key) < 0 {