Browse Source

math/rand/v2

pull/7160/head
chrislu 1 month ago
parent
commit
102a081dd9
  1. 2
      test/s3/copying/s3_copying_test.go
  2. 2
      test/s3/iam/s3_iam_framework.go
  3. 2
      unmaintained/stress_filer_upload/write_files/write_files.go
  4. 2
      weed/filer/filechunks_test.go
  5. 4
      weed/mount/weedfs.go
  6. 9
      weed/mq/broker/broker_connect.go
  7. 4
      weed/mq/broker/broker_grpc_pub.go
  8. 11
      weed/mq/pub_balancer/allocate.go
  9. 5
      weed/mq/pub_balancer/balance_brokers.go
  10. 7
      weed/mq/pub_balancer/repair.go
  11. 4
      weed/sftpd/auth/password.go
  12. 4
      weed/sftpd/user/user.go
  13. 15
      weed/shell/shell_liner.go
  14. 4
      weed/storage/volume_vacuum_test.go
  15. 4
      weed/util/skiplist/skiplist_test.go

2
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 { func getNewBucketName() string {
timestamp := time.Now().UnixNano() timestamp := time.Now().UnixNano()
// Add random suffix to prevent collisions when tests run quickly // 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) return fmt.Sprintf("%s%d-%d", defaultConfig.BucketPrefix, timestamp, randomSuffix)
} }

2
test/s3/iam/s3_iam_framework.go

@ -656,7 +656,7 @@ func (f *S3IAMTestFramework) GenerateUniqueBucketName(prefix string) string {
testName = strings.ReplaceAll(testName, "_", "-") testName = strings.ReplaceAll(testName, "_", "-")
// Add random suffix to handle parallel tests // Add random suffix to handle parallel tests
randomSuffix := mathrand.Intn(10000)
randomSuffix := mathrand.IntN(10000)
return fmt.Sprintf("%s-%s-%d", prefix, testName, randomSuffix) return fmt.Sprintf("%s-%s-%d", prefix, testName, randomSuffix)
} }

2
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)) f, err := os.Create(fmt.Sprintf("%s/file%05d", *toDir, i))
check(err) check(err)
fileSize := *minSize + rand.Intn(*maxSize-*minSize)
fileSize := *minSize + rand.IntN(*maxSize-*minSize)
startTime := time.Now() startTime := time.Now()
fmt.Printf("write %s %d bytes: ", f.Name(), fileSize) fmt.Printf("write %s %d bytes: ", f.Name(), fileSize)

2
weed/filer/filechunks_test.go

@ -71,7 +71,7 @@ func TestRandomFileChunksCompact(t *testing.T) {
var chunks []*filer_pb.FileChunk var chunks []*filer_pb.FileChunk
for i := 0; i < 15; i++ { 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 { if start > stop {
start, stop = stop, start start, stop = stop, start
} }

4
weed/mount/weedfs.go

@ -3,7 +3,7 @@ package mount
import ( import (
"context" "context"
"errors" "errors"
"math/rand"
"math/rand/v2"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -110,7 +110,7 @@ func NewSeaweedFileSystem(option *Option) *WFS {
fhLockTable: util.NewLockTable[FileHandleId](), 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() wfs.option.setupUniqueCacheDirectory()
if option.CacheSizeMBForRead > 0 { if option.CacheSizeMBForRead > 0 {
wfs.chunkCache = chunk_cache.NewTieredChunkCache(256, option.getUniqueCacheDirForRead(), option.CacheSizeMBForRead, 1024*1024) wfs.chunkCache = chunk_cache.NewTieredChunkCache(256, option.getUniqueCacheDirForRead(), option.CacheSizeMBForRead, 1024*1024)

9
weed/mq/broker/broker_connect.go

@ -3,12 +3,13 @@ package broker
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"math/rand/v2"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
"io"
"math/rand"
"time"
) )
// BrokerConnectToBalancer connects to the broker balancer and sends stats // 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) // 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)
} }
}) })
} }

4
weed/mq/broker/broker_grpc_pub.go

@ -4,7 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"math/rand"
"math/rand/v2"
"net" "net"
"sync/atomic" "sync/atomic"
"time" "time"
@ -71,7 +71,7 @@ func (b *MessageQueueBroker) PublishMessage(stream mq_pb.SeaweedMessaging_Publis
var isClosed bool var isClosed bool
// process each published messages // 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() publisher := topic.NewLocalPublisher()
localTopicPartition.Publishers.AddPublisher(clientName, publisher) localTopicPartition.Publishers.AddPublisher(clientName, publisher)

11
weed/mq/pub_balancer/allocate.go

@ -1,12 +1,13 @@
package pub_balancer package pub_balancer
import ( import (
"math/rand/v2"
"time"
cmap "github.com/orcaman/concurrent-map/v2" cmap "github.com/orcaman/concurrent-map/v2"
"github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb" "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
"github.com/seaweedfs/seaweedfs/weed/pb/schema_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) { 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) pickedBrokers := make([]string, 0, count)
for i := int32(0); i < count; i++ { for i := int32(0); i < count; i++ {
p := rand.Intn(len(candidates))
p := rand.IntN(len(candidates))
pickedBrokers = append(pickedBrokers, candidates[p]) pickedBrokers = append(pickedBrokers, candidates[p])
} }
return pickedBrokers return pickedBrokers
@ -59,7 +60,7 @@ func pickBrokersExcluded(brokers []string, count int, excludedLeadBroker string,
if len(pickedBrokers) < count { if len(pickedBrokers) < count {
pickedBrokers = append(pickedBrokers, broker) pickedBrokers = append(pickedBrokers, broker)
} else { } else {
j := rand.Intn(i + 1)
j := rand.IntN(i + 1)
if j < count { if j < count {
pickedBrokers[j] = broker pickedBrokers[j] = broker
} }
@ -69,7 +70,7 @@ func pickBrokersExcluded(brokers []string, count int, excludedLeadBroker string,
// shuffle the picked brokers // shuffle the picked brokers
count = len(pickedBrokers) count = len(pickedBrokers)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
j := rand.Intn(count)
j := rand.IntN(count)
pickedBrokers[i], pickedBrokers[j] = pickedBrokers[j], pickedBrokers[i] pickedBrokers[i], pickedBrokers[j] = pickedBrokers[j], pickedBrokers[i]
} }

5
weed/mq/pub_balancer/balance_brokers.go

@ -1,9 +1,10 @@
package pub_balancer package pub_balancer
import ( import (
"math/rand/v2"
cmap "github.com/orcaman/concurrent-map/v2" cmap "github.com/orcaman/concurrent-map/v2"
"github.com/seaweedfs/seaweedfs/weed/mq/topic" "github.com/seaweedfs/seaweedfs/weed/mq/topic"
"math/rand"
) )
func BalanceTopicPartitionOnBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats]) BalanceAction { func BalanceTopicPartitionOnBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats]) BalanceAction {
@ -28,7 +29,7 @@ func BalanceTopicPartitionOnBrokers(brokers cmap.ConcurrentMap[string, *BrokerSt
maxPartitionCountPerBroker = brokerStats.Val.TopicPartitionCount maxPartitionCountPerBroker = brokerStats.Val.TopicPartitionCount
sourceBroker = brokerStats.Key sourceBroker = brokerStats.Key
// select a random partition from the source broker // select a random partition from the source broker
randomePartitionIndex := rand.Intn(int(brokerStats.Val.TopicPartitionCount))
randomePartitionIndex := rand.IntN(int(brokerStats.Val.TopicPartitionCount))
index := 0 index := 0
for topicPartitionStats := range brokerStats.Val.TopicPartitionStats.IterBuffered() { for topicPartitionStats := range brokerStats.Val.TopicPartitionStats.IterBuffered() {
if index == randomePartitionIndex { if index == randomePartitionIndex {

7
weed/mq/pub_balancer/repair.go

@ -1,11 +1,12 @@
package pub_balancer package pub_balancer
import ( import (
"math/rand/v2"
"sort"
cmap "github.com/orcaman/concurrent-map/v2" cmap "github.com/orcaman/concurrent-map/v2"
"github.com/seaweedfs/seaweedfs/weed/mq/topic" "github.com/seaweedfs/seaweedfs/weed/mq/topic"
"math/rand"
"modernc.org/mathutil" "modernc.org/mathutil"
"sort"
) )
func (balancer *PubBalancer) RepairTopics() []BalanceAction { func (balancer *PubBalancer) RepairTopics() []BalanceAction {
@ -56,7 +57,7 @@ func RepairMissingTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStat
Topic: t, Topic: t,
Partition: partition, Partition: partition,
}, },
TargetBroker: candidates[rand.Intn(len(candidates))],
TargetBroker: candidates[rand.IntN(len(candidates))],
}) })
} }
} }

4
weed/sftpd/auth/password.go

@ -2,7 +2,7 @@ package auth
import ( import (
"fmt" "fmt"
"math/rand"
"math/rand/v2"
"time" "time"
"github.com/seaweedfs/seaweedfs/weed/sftpd/user" "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 // 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") return nil, fmt.Errorf("authentication failed")
} }

4
weed/sftpd/user/user.go

@ -2,7 +2,7 @@
package user package user
import ( import (
"math/rand"
"math/rand/v2"
"path/filepath" "path/filepath"
) )
@ -22,7 +22,7 @@ func NewUser(username string) *User {
// Generate a random UID/GID between 1000 and 60000 // Generate a random UID/GID between 1000 and 60000
// This range is typically safe for regular users in most systems // This range is typically safe for regular users in most systems
// 0-999 are often reserved for system users // 0-999 are often reserved for system users
randomId := 1000 + rand.Intn(59000)
randomId := 1000 + rand.IntN(59000)
return &User{ return &User{
Username: username, Username: username,

15
weed/shell/shell_liner.go

@ -3,19 +3,20 @@ package shell
import ( import (
"context" "context"
"fmt" "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" "io"
"math/rand"
"math/rand/v2"
"os" "os"
"path" "path"
"regexp" "regexp"
"slices" "slices"
"strings" "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" "github.com/peterh/liner"
) )
@ -69,7 +70,7 @@ func RunShell(options ShellOptions) {
fmt.Printf("master: %s ", *options.Masters) fmt.Printf("master: %s ", *options.Masters)
if len(filers) > 0 { if len(filers) > 0 {
fmt.Printf("filers: %v", filers) fmt.Printf("filers: %v", filers)
commandEnv.option.FilerAddress = filers[rand.Intn(len(filers))]
commandEnv.option.FilerAddress = filers[rand.IntN(len(filers))]
} }
fmt.Println() fmt.Println()
} }

4
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) // println("written file", i, "checksum", n.Checksum.Value(), "size", size)
if rand.Float64() < 0.03 { if rand.Float64() < 0.03 {
toBeDeleted := rand.Intn(i) + 1
toBeDeleted := rand.IntN(i) + 1
oldNeedle := newEmptyNeedle(uint64(toBeDeleted)) oldNeedle := newEmptyNeedle(uint64(toBeDeleted))
v.deleteNeedle2(oldNeedle) v.deleteNeedle2(oldNeedle)
// println("deleted file", toBeDeleted) // println("deleted file", toBeDeleted)
@ -175,7 +175,7 @@ type needleInfo struct {
func newRandomNeedle(id uint64) *needle.Needle { func newRandomNeedle(id uint64) *needle.Needle {
n := new(needle.Needle) n := new(needle.Needle)
n.Data = make([]byte, rand.Intn(1024))
n.Data = make([]byte, rand.IntN(1024))
rand.Read(n.Data) rand.Read(n.Data)
n.Checksum = needle.NewCRC(n.Data) n.Checksum = needle.NewCRC(n.Data)

4
weed/util/skiplist/skiplist_test.go

@ -235,11 +235,11 @@ func TestFindGreaterOrEqual(t *testing.T) {
list = New(memStore) list = New(memStore)
for i := 0; i < maxN; i++ { 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++ { for i := 0; i < maxN; i++ {
key := Element(rand.Intn(maxNumber))
key := Element(rand.IntN(maxNumber))
if _, v, ok, _ := list.FindGreaterOrEqual(key); ok { if _, v, ok, _ := list.FindGreaterOrEqual(key); ok {
// if f is v should be bigger than the element before // if f is v should be bigger than the element before
if v.Prev != nil && bytes.Compare(key, v.Prev.Key) < 0 { if v.Prev != nil && bytes.Compare(key, v.Prev.Key) < 0 {

Loading…
Cancel
Save