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

Replace usage of math/rand with math/rand/v2 #6817

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ravjot07
Copy link

Contribution towards #6648

@ravjot07
Copy link
Author

Hey, @antoninbas
I've updated the codebase according to the requirements we discussed. Specifically:

  • Random Number Generation: For most use cases, I've retained the global random generator, as it's sufficient and simplifies the implementation.
  • Repeatability in Tests: In scenarios where repeatability is essential, such as testing, I've implemented local generators using NewPCG(seed1, seed2) with fixed seeds. This ensures consistent and deterministic behavior across test runs.

Please review the changes at your earliest convenience and let me know if there are any further adjustments needed.

@@ -17,7 +17,7 @@ package e2e
import (
"encoding/json"
"fmt"
"math/rand"
"math/rand/v2" // Updated import path to use math/rand/v2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a helpful comment to have in the code

@@ -33,6 +33,13 @@ import (
e2euttils "antrea.io/antrea/test/e2e/utils"
)

const (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are these new constants? I'm really confused because they seem unused and completely unrelated to the contents of this file

@@ -458,7 +464,7 @@ func (data *MCTestData) testStretchedNetworkPolicyUpdatePolicy(t *testing.T) {
// Update the policy to select the eastRegularClient.
acnpBuilder.AddStretchedIngressRule(map[string]string{"antrea-e2e": eastRegularClientName}, nil, "", nil, crdv1beta1.RuleActionDrop)
if _, err := data.createOrUpdateACNP(westCluster, acnpBuilder.Get()); err != nil {
t.Fatalf("Error updateing ACNP %s: %v", acnpBuilder.Name, err)
t.Fatalf("Error updating ACNP %s: %v", acnpBuilder.Name, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while these changes are legit (fixing typos), they are unrelated to the main PR
I would recommend making a separate PR for them

Comment on lines +518 to +519
mainRand := rand.New(rand.NewPCG(1, 0)) // Initialize with fixed seeds for deterministic behavior
gwIdx := mainRand.IntN(len(nodes)) // #nosec G404: for test only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if deterministic behavior is really important / desired here. Maybe try with the main random generator provided by the package.

Overall I think maybe avoiding deterministic behavior is a good thing for e2e tests.

@@ -40,7 +40,7 @@ import (
)

// #nosec G404: random number generator not used for security purposes.
var icmpEchoID = rand.Int31n(1 << 16)
var icmpEchoID = rand.IntN(1 << 16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, you should remove the explicit casts to int in the rest of the code. Look for occurrences of int(icmpEchoID) in this file.

@@ -28,7 +28,7 @@ import (
)

// #nosec G404: random number generator not used for security purposes
var pktRand = rand.New(rand.NewSource(time.Now().UnixNano()))
var pktRand = rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), 0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could use rand.New(rand.NewPCG(rand.Uint64(), rand.Uint64())) here. No need to use the time package anymore to seed?

Comment on lines +72 to +73
// Initialize random number generator with a seed
ran = rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), 0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed? why can't we use the main random number generator which is randomly seeded and is also thread-safe?

// #nosec G404: random number generator not used for security purposes
var pktRandInstance = rand.New(rand.NewPCG(1, 0)) // Fixed seeds: 1 and 0

// Override pktRand in the main package with pktRandInstance during tests.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"main package" means something else. We are not in the main package here.

Comment on lines +154 to +158
// Initialize the PCG generator
pcg := rand.PCG{}
// Provide both 'state' and 'sequence' seeds
pcg.Seed(uint64(seed), uint64(1))
rnd := rand.New(&pcg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we using this verbose code here instead of rand.New(rand.NewPCG(seed, 0)) like in the rest of the PR?

Comment on lines -157 to +166
for _, ok := existingCIDRs[cidr]; ok; {
for {
if _, ok := existingCIDRs[cidr]; !ok {
break
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why this is an improvement to the existing code?

@antoninbas antoninbas changed the title Updated maths/rand to maths/rand/v2 Replace usage of math/rand with math/rand/v2 Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants