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

pickfirst: Move var for mocking the shuffle func from internal/internal to pickfirst/internal #7698

Merged
merged 5 commits into from
Oct 9, 2024

Conversation

arjan-bal
Copy link
Contributor

Fixes: #7515

For someone reading the pickfirst code, it can be a little misleading as to why pickfirst is getting the shuffle func from a variable with a name having the ForTesting 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

@arjan-bal arjan-bal added the Type: Internal Cleanup Refactors, etc label Oct 3, 2024
@arjan-bal arjan-bal added this to the 1.68 Release milestone Oct 3, 2024
@arjan-bal arjan-bal requested a review from easwars October 3, 2024 09:00
Copy link

codecov bot commented Oct 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.77%. Comparing base (5f178a8) to head (4646f45).
Report is 2 commits behind head on master.

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     
Files with missing lines Coverage Δ
balancer/pickfirst/pickfirst.go 84.42% <100.00%> (+0.68%) ⬆️

... and 13 files with indirect coverage changes

test/pickfirst_test.go Outdated Show resolved Hide resolved
internal/internal.go Outdated Show resolved Hide resolved
@easwars easwars assigned arjan-bal and unassigned easwars Oct 3, 2024
@easwars
Copy link
Contributor

easwars commented Oct 3, 2024

Also, the PR title has two setters.

@dfawley
Copy link
Member

dfawley commented Oct 3, 2024

Also, the PR title has two setters.

And there is a typo in reference. We should feel free to edit each other's PR titles to help.

@dfawley dfawley changed the title pickfirst: Provide a setter setter for the shuffle func instead of a direct refference pickfirst: Provide a setter for the shuffle func instead of a direct reference Oct 3, 2024
var logger = grpclog.Component("pick-first-lb")
var (
logger = grpclog.Component("pick-first-lb")
shuffleFunc = rand.Shuffle
Copy link
Member

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.:

var randInt63n = rand.Int63n

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed.


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)) {
Copy link
Member

Choose a reason for hiding this comment

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

How about SetPickFirstRandShuffleForTesting?

Copy link
Contributor Author

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.

internal/internal.go Outdated Show resolved Hide resolved
@dfawley
Copy link
Member

dfawley commented Oct 3, 2024

Also, thank you for fixing this. It was just bothering me the other day.

@arjan-bal arjan-bal assigned easwars and unassigned arjan-bal Oct 4, 2024
Comment on lines 18 to 19
// Package internal contains gRPC-internal code, to avoid polluting
// the godoc of the top-level grpc package.
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@easwars easwars assigned arjan-bal and unassigned easwars Oct 7, 2024
SetRandShuffleForTesting func(sf func(n int, swap func(i, j int)))

// RevertRandShuffleFunc sets the real shuffle function back after testing.
RevertRandShuffleFunc func()
Copy link
Member

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.

Copy link
Contributor Author

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()
)
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed as suggested.

Copy link
Member

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.

@arjan-bal arjan-bal assigned easwars and arjan-bal and unassigned arjan-bal and easwars Oct 8, 2024
@arjan-bal arjan-bal assigned dfawley and unassigned arjan-bal Oct 8, 2024
@arjan-bal arjan-bal requested review from easwars and dfawley October 8, 2024 17:23
@arjan-bal arjan-bal changed the title pickfirst: Provide a setter for the shuffle func instead of a direct reference pickfirst: Move var for mocking the shuffle func from internal/internal to pickfirst/internal Oct 8, 2024
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

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

Thanks!

@dfawley dfawley assigned arjan-bal and easwars and unassigned easwars and dfawley Oct 8, 2024
@arjan-bal arjan-bal merged commit b8ee37d into grpc:master Oct 9, 2024
14 checks passed
@arjan-bal arjan-bal deleted the pickfirst-cleanup branch October 9, 2024 09:39
janardhanvissa pushed a commit to janardhanvissa/grpc-go that referenced this pull request Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pickfirst: Don't use internal.ShuffleAddressListForTesting directly
3 participants