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

all: stop using math/rand.Seed #16428

Closed
1 of 4 tasks
callthingsoff opened this issue Aug 16, 2023 · 10 comments · Fixed by #16447
Closed
1 of 4 tasks

all: stop using math/rand.Seed #16428

callthingsoff opened this issue Aug 16, 2023 · 10 comments · Fixed by #16447

Comments

@callthingsoff
Copy link
Contributor

callthingsoff commented Aug 16, 2023

Bug report criteria

What happened?

It's updated to Go 1.20 minor release, however math/rand.Seed is marked "Deprecated":

// Prior to Go 1.20, the generator was seeded like Seed(1) at program startup.
// To force the old behavior, call Seed(1) at program startup.
// Alternately, set GODEBUG=randautoseed=0 in the environment
// before making any calls to functions in this package.
//
// Deprecated: As of Go 1.20 there is no reason to call Seed with
// a random value. Programs that call Seed with a known value to get
// a specific sequence of results should use New(NewSource(seed)) to
// obtain a local random generator.

What did you expect to happen?

Stop using math/rand.Seed.

How can we reproduce it (as minimally and precisely as possible)?

Use the new API as suggested.

Anything else we need to know?

No response

Etcd version (please run commands below)

Versions updated to Go 1.20.

Etcd configuration (command line flags or environment variables)

No response

Etcd debug information (please run commands below, feel free to obfuscate the IP address or FQDN in the output)

No response

Relevant log output

No response

@jmhbnz
Copy link
Member

jmhbnz commented Aug 16, 2023

Thanks for raising this @gocurr. More context here for anyone interested: golang/go#54880

Looks like eight instances to consider updating:

 ➜ find . -name "*.go" -type f -print | xargs grep .Seed
./pkg/stringutil/rand.go:                                   rand.Seed(time.Now().UnixNano())
./pkg/adt/interval_tree_test.go:                            rand.Seed(time.Now().UnixNano())
./pkg/proxy/server.go:                                      mrand.Seed(int64(now.Nanosecond()))
./pkg/proxy/server_test.go:                                 rand.Seed(now)
./tests/integration/clientv3/snapshot/v3_snapshot_test.go:  rand.Seed(int64(time.Now().Nanosecond()))
./server/lease/lessor_bench_test.go:                        rand.Seed(time.Now().UTC().UnixNano())
./server/etcdserver/server.go:                              rand.Seed(time.Now().UnixNano())                                       

@callthingsoff
Copy link
Contributor Author

I think perhaps you should check other deprecated APIs.

@jmhbnz
Copy link
Member

jmhbnz commented Aug 16, 2023

I think perhaps you should check other deprecated APIs.

The only other formal deprecation I can see that impacts us for 1.20 is deprecation of math/rand.Read in favor of crypto/rand.Read. I'll raise a separate issue for that. Please let us know if you spot anything else that needs a cleanup.

@callthingsoff
Copy link
Contributor Author

Anyone want to fix it?

@jmhbnz
Copy link
Member

jmhbnz commented Aug 17, 2023

We welcome contributions so please feel free if you have capacity! Happy to assign it to you just let us know.

@callthingsoff
Copy link
Contributor Author

Seems to me, there are two approaches.

  1. Since "If Seed is not called, the generator is seeded randomly at program startup." just remove rand.Seed calls.
  2. "Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator." it depends on the situations.

@Aditya-Sood
Copy link

hi @gocurr @jmhbnz
is the fix in-progress or open for contribution?

@jmhbnz
Copy link
Member

jmhbnz commented Aug 19, 2023

I'm not working on this currently. @gocurr did you make a start on this?

  • Since "If Seed is not called, the generator is seeded randomly at program startup." just remove rand.Seed calls.
  • "Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator." it depends on the situations.

Ideally we should be just removing the .Seed calls.

@callthingsoff
Copy link
Contributor Author

callthingsoff commented Aug 20, 2023

hi @gocurr @jmhbnz
is the fix in-progress or open for contribution?

A PR to this issue was just created.

@callthingsoff
Copy link
Contributor Author

I'm not working on this currently. @gocurr did you make a start on this?

  • Since "If Seed is not called, the generator is seeded randomly at program startup." just remove rand.Seed calls.
  • "Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator." it depends on the situations.

Ideally we should be just removing the .Seed calls.

Please take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

4 participants