-
Notifications
You must be signed in to change notification settings - Fork 4.6k
pickfirstleaf: Fix shuffling of addresses in resolver updates without endpoints #8610
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Fix shuffling of addresses
- Loading branch information
commit 27b6b6819a042cd109b29b71bafdb1e464c9b37d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ package pickfirst_test | |
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
@@ -28,11 +29,14 @@ import ( | |
|
|
||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/backoff" | ||
| "google.golang.org/grpc/balancer" | ||
| pfbalancer "google.golang.org/grpc/balancer/pickfirst" | ||
| pfinternal "google.golang.org/grpc/balancer/pickfirst/internal" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/connectivity" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/internal" | ||
| "google.golang.org/grpc/internal/balancer/stub" | ||
| "google.golang.org/grpc/internal/channelz" | ||
| "google.golang.org/grpc/internal/grpctest" | ||
| "google.golang.org/grpc/internal/stubserver" | ||
|
|
@@ -463,6 +467,85 @@ func (s) TestPickFirst_ShuffleAddressList(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // Tests the PF LB policy with shuffling enabled. It explicitly unsets the | ||
| // Endpoints field in the resolver update to test the shuffling of the | ||
| // Addresses. | ||
| func (s) TestPickFirst_ShuffleAddressListNoEndpoints(t *testing.T) { | ||
| // Install a shuffler that always reverses two entries. | ||
| origShuf := pfinternal.RandShuffle | ||
| defer func() { pfinternal.RandShuffle = origShuf }() | ||
| pfinternal.RandShuffle = func(n int, f func(int, int)) { | ||
| if n != 2 { | ||
| t.Errorf("Shuffle called with n=%v; want 2", n) | ||
| return | ||
| } | ||
| f(0, 1) // reverse the two addresses | ||
| } | ||
|
|
||
| pfBuilder := balancer.Get(pfbalancer.Name) | ||
| shffleConfig, err := pfBuilder.(balancer.ConfigParser).ParseConfig(json.RawMessage(`{ "shuffleAddressList": true }`)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| noShffleConfig, err := pfBuilder.(balancer.ConfigParser).ParseConfig(json.RawMessage(`{ "shuffleAddressList": false }`)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| activeCfg := noShffleConfig | ||
|
||
|
|
||
| bf := stub.BalancerFuncs{ | ||
| Init: func(bd *stub.BalancerData) { | ||
| bd.ChildBalancer = pfBuilder.Build(bd.ClientConn, bd.BuildOptions) | ||
| }, | ||
| Close: func(bd *stub.BalancerData) { | ||
| bd.ChildBalancer.Close() | ||
| }, | ||
| UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error { | ||
| ccs.BalancerConfig = activeCfg | ||
| ccs.ResolverState.Endpoints = nil | ||
| return bd.ChildBalancer.UpdateClientConnState(ccs) | ||
| }, | ||
| } | ||
|
|
||
| stub.Register(t.Name(), bf) | ||
| svcCfg := fmt.Sprintf(`{ "loadBalancingConfig": [{%q: {}}] }`, t.Name()) | ||
| // Set up our backends. | ||
| cc, r, backends := setupPickFirst(t, 2, grpc.WithDefaultServiceConfig(svcCfg)) | ||
| addrs := stubBackendsToResolverAddrs(backends) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
| defer cancel() | ||
|
|
||
| // Push an update with both addresses and shuffling disabled. We should | ||
| // connect to backend 0. | ||
| activeCfg = noShffleConfig | ||
| resolverState := resolver.State{Addresses: addrs} | ||
| r.UpdateState(resolverState) | ||
| if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Send a config with shuffling enabled. This will reverse the addresses, | ||
| // but the channel should still be connected to backend 0. | ||
| activeCfg = shffleConfig | ||
| r.UpdateState(resolverState) | ||
| if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Send a resolver update with no addresses. This should push the channel | ||
| // into TransientFailure. | ||
| r.UpdateState(resolver.State{}) | ||
| testutils.AwaitState(ctx, t, cc, connectivity.TransientFailure) | ||
|
|
||
| // Send the same config as last time with shuffling enabled. Since we are | ||
| // not connected to backend 0, we should connect to backend 1. | ||
| r.UpdateState(resolverState) | ||
| if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[1]); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| // Test config parsing with the env var turned on and off for various scenarios. | ||
| func (s) TestPickFirst_ParseConfig_Success(t *testing.T) { | ||
| // Install a shuffler that always reverses two entries. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Lol, here and in the next statement, prefer adding the
uback intoshuffle. Saving one character in the variable name is not going to be very useful :)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 to using complete words.