-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
pickfirst: Move var for mocking the shuffle func from internal/internal to pickfirst/internal #7698
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #7698 +/- ##
==========================================
- Coverage 81.77% 81.77% -0.01%
==========================================
Files 361 361
Lines 27826 27825 -1
==========================================
- Hits 22756 22755 -1
+ Misses 3865 3863 -2
- Partials 1205 1207 +2
|
Also, the PR title has two |
And there is a typo in reference. We should feel free to edit each other's PR titles to help. |
balancer/pickfirst/pickfirst.go
Outdated
var logger = grpclog.Component("pick-first-lb") | ||
var ( | ||
logger = grpclog.Component("pick-first-lb") | ||
shuffleFunc = rand.Shuffle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I like to name these variables packagenameFunctionName
. So this would be randShuffle
. E.g.:
grpc-go/internal/wrr/random.go
Line 49 in 67e47fc
var randInt63n = rand.Int63n |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed.
balancer/pickfirst/pickfirst.go
Outdated
|
||
const ( | ||
// Name is the name of the pick_first balancer. | ||
Name = "pick_first" | ||
logPrefix = "[pick-first-lb %p] " | ||
) | ||
|
||
func init() { | ||
balancer.Register(pickfirstBuilder{}) | ||
internal.ShuffleAddressListForTesting = func(sf func(n int, swap func(i, j int))) func(n int, swap func(i, j int)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about SetPickFirstRandShuffleForTesting
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to SetRandShuffleForTesting
after moving the variable into pickfirst/internal
omitting PickFirst
because it's now in the pickfirst
directory.
Also, thank you for fixing this. It was just bothering me the other day. |
5850e5c
to
cc6a708
Compare
// Package internal contains gRPC-internal code, to avoid polluting | ||
// the godoc of the top-level grpc package. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something like?
// Package internal contains code internal to the pickfirst package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated as suggested.
var ( | ||
// SetRandShuffleForTesting pseudo-randomizes the order of addresses. n | ||
// is the number of elements. swap swaps the elements with indexes i and j. | ||
// It returns the shuffle func before it was replaced. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This last line in the comment is not true anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
SetRandShuffleForTesting func(sf func(n int, swap func(i, j int))) | ||
|
||
// RevertRandShuffleFunc sets the real shuffle function back after testing. | ||
RevertRandShuffleFunc func() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: RevertRandShuffleForTesting
to be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
|
||
// RevertRandShuffleFunc sets the real shuffle function back after testing. | ||
RevertRandShuffleFunc func() | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternate proposal:
package internal
var RandShuffle = rand.Shuffle
package pickfirst
// when it's time to shuffle, call internal.RandShuffle
package pickfirst_test
// set internal.RandShuffle directly
I think this would be a bit simpler for everyone to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be the same as the existing situation (i.e. without the changes in this PR), the only difference being the removal of the suffix ForTesting
from the shuffle function's name (internal.RandShuffle
now).
@easwars what do you think about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with this too, but I'd like the var RandShuffle
to be in pickfirst/internal
instead of the top-level internal
package. We already have too much stuff in the top-level internal
package and it would be good to move things out wherever possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed as suggested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but I'd like the var RandShuffle to be in pickfirst/internal instead of the top-level internal package
Yes, this was what I had in mind, too, just didn't explain it correctly.
e826fd8
to
1ea5a3c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
…al to pickfirst/internal (grpc#7698)
Fixes: #7515
For someone reading the
pickfirst
code, it can be a little misleading as to whypickfirst
is getting the shuffle func from a variable with a name having theForTesting
suffix. This PR provides a setter in the internal package that can be used to replace the shuffler being used as suggested in #7515.RELEASE NOTES: N/A