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

Fix: fix rand.Seed() duplicate concurrent calls #1958

Merged
merged 3 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion cluster/loadbalance/p2c/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
)

func init() {
rand.Seed(randSeed())
Copy link
Member

@binbin0325 binbin0325 Jul 8, 2022

Choose a reason for hiding this comment

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

rand.Seed only need to initialize once

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think there should be a sync.Once to initialize the seed globally. Did dubbo-go have it before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Each init() is called only once in golang, and I think it is sufficient to ensure that the seed does not change after the program starts running, regardless of the number of times the rand.Seed() is called in the program.

In addition, the imported third-party package may call rand.Seed() even if we only run it once

Copy link
Member

@justxuewei justxuewei Jul 8, 2022

Choose a reason for hiding this comment

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

Well, the number of initializations of the rand seed is not a very strict limit. This PR looks good for me. BTW, I am saying that initialize the rand seed globally, instead of initialize that in init() function of each package, but it doesn't matter now.

extension.SetLoadbalance(constant.LoadBalanceKeyP2C, newP2CLoadBalance)
}

Expand Down Expand Up @@ -78,7 +79,6 @@ func (l *p2cLoadBalance) Select(invokers []protocol.Invoker, invocation protocol
if len(invokers) == 2 {
i, j = 0, 1
} else {
rand.Seed(randSeed())
i = rand.Intn(len(invokers))
j = i
for i == j {
Expand Down
9 changes: 8 additions & 1 deletion cluster/loadbalance/p2c/loadbalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package p2c

import (
"math/rand"
"testing"
)

Expand All @@ -37,16 +38,18 @@ import (
func TestLoadBalance(t *testing.T) {
lb := newP2CLoadBalance()
invocation := protoinvoc.NewRPCInvocation("TestMethod", []interface{}{}, nil)
randSeed = func() int64 {
randSeed := func() int64 {
return 0
}

t.Run("no invokers", func(t *testing.T) {
rand.Seed(randSeed())
ivk := lb.Select([]protocol.Invoker{}, invocation)
assert.Nil(t, ivk)
})

t.Run("one invoker", func(t *testing.T) {
rand.Seed(randSeed())
url0, _ := common.NewURL("dubbo://192.168.1.0:20000/com.ikurento.user.UserProvider")

ivkArr := []protocol.Invoker{
Expand All @@ -57,6 +60,7 @@ func TestLoadBalance(t *testing.T) {
})

t.Run("two invokers", func(t *testing.T) {
rand.Seed(randSeed())
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand Down Expand Up @@ -86,6 +90,7 @@ func TestLoadBalance(t *testing.T) {
})

t.Run("multiple invokers", func(t *testing.T) {
rand.Seed(randSeed())
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand Down Expand Up @@ -117,6 +122,7 @@ func TestLoadBalance(t *testing.T) {
})

t.Run("metrics i not found", func(t *testing.T) {
rand.Seed(randSeed())
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand Down Expand Up @@ -144,6 +150,7 @@ func TestLoadBalance(t *testing.T) {
})

t.Run("metrics j not found", func(t *testing.T) {
rand.Seed(randSeed())
ctrl := gomock.NewController(t)
defer ctrl.Finish()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
)

func init() {
rand.Seed(time.Now().UnixNano())
extension.SetServiceInstanceSelector("random", NewRandomServiceInstanceSelector)
}

Expand All @@ -47,7 +48,6 @@ func (r *RandomServiceInstanceSelector) Select(url *common.URL, serviceInstances
if len(serviceInstances) == 1 {
return serviceInstances[0]
}
rand.Seed(time.Now().UnixNano())
index := rand.Intn(len(serviceInstances))
return serviceInstances[index]
}