-
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
balancergroup: Propagate balancer.BuildOptions to child policies #4184
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
package balancergroup | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
@@ -38,6 +39,9 @@ import ( | |
"google.golang.org/grpc/balancer" | ||
"google.golang.org/grpc/balancer/roundrobin" | ||
"google.golang.org/grpc/connectivity" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"google.golang.org/grpc/internal/balancer/stub" | ||
itestutils "google.golang.org/grpc/internal/testutils" | ||
"google.golang.org/grpc/resolver" | ||
"google.golang.org/grpc/xds/internal/balancer/weightedtarget/weightedaggregator" | ||
"google.golang.org/grpc/xds/internal/client/load" | ||
|
@@ -74,7 +78,7 @@ func newTestBalancerGroup(t *testing.T, loadStore load.PerClusterReporter) (*tes | |
cc := testutils.NewTestClientConn(t) | ||
gator := weightedaggregator.New(cc, nil, testutils.NewTestWRR) | ||
gator.Start() | ||
bg := New(cc, gator, loadStore, nil) | ||
bg := New(cc, balancer.BuildOptions{}, gator, loadStore, nil) | ||
bg.Start() | ||
return cc, gator, bg | ||
} | ||
|
@@ -501,7 +505,7 @@ func (s) TestBalancerGroup_start_close(t *testing.T) { | |
cc := testutils.NewTestClientConn(t) | ||
gator := weightedaggregator.New(cc, nil, testutils.NewTestWRR) | ||
gator.Start() | ||
bg := New(cc, gator, nil, nil) | ||
bg := New(cc, balancer.BuildOptions{}, gator, nil, nil) | ||
|
||
// Add two balancers to group and send two resolved addresses to both | ||
// balancers. | ||
|
@@ -590,16 +594,20 @@ func (s) TestBalancerGroup_start_close(t *testing.T) { | |
// whenever it gets an address update. It's expected that start() doesn't block | ||
// because of deadlock. | ||
func (s) TestBalancerGroup_start_close_deadlock(t *testing.T) { | ||
const balancerName = "stub-TestBalancerGroup_start_close_deadlock" | ||
stub.Register(balancerName, stub.BalancerFuncs{}) | ||
builder := balancer.Get(balancerName) | ||
|
||
cc := testutils.NewTestClientConn(t) | ||
gator := weightedaggregator.New(cc, nil, testutils.NewTestWRR) | ||
gator.Start() | ||
bg := New(cc, gator, nil, nil) | ||
bg := New(cc, balancer.BuildOptions{}, gator, nil, nil) | ||
|
||
gator.Add(testBalancerIDs[0], 2) | ||
bg.Add(testBalancerIDs[0], &testutils.TestConstBalancerBuilder{}) | ||
bg.Add(testBalancerIDs[0], builder) | ||
bg.UpdateClientConnState(testBalancerIDs[0], balancer.ClientConnState{ResolverState: resolver.State{Addresses: testBackendAddrs[0:2]}}) | ||
gator.Add(testBalancerIDs[1], 1) | ||
bg.Add(testBalancerIDs[1], &testutils.TestConstBalancerBuilder{}) | ||
bg.Add(testBalancerIDs[1], builder) | ||
bg.UpdateClientConnState(testBalancerIDs[1], balancer.ClientConnState{ResolverState: resolver.State{Addresses: testBackendAddrs[2:4]}}) | ||
|
||
bg.Start() | ||
|
@@ -695,7 +703,7 @@ func initBalancerGroupForCachingTest(t *testing.T) (*weightedaggregator.Aggregat | |
cc := testutils.NewTestClientConn(t) | ||
gator := weightedaggregator.New(cc, nil, testutils.NewTestWRR) | ||
gator.Start() | ||
bg := New(cc, gator, nil, nil) | ||
bg := New(cc, balancer.BuildOptions{}, gator, nil, nil) | ||
|
||
// Add two balancers to group and send two resolved addresses to both | ||
// balancers. | ||
|
@@ -931,3 +939,51 @@ func (s) TestBalancerGroup_locality_caching_readd_with_different_builder(t *test | |
t.Fatalf("want %v, got %v", want, err) | ||
} | ||
} | ||
|
||
// TestBalancerGroupBuildOptions verifies that the balancer.BuildOptions passed | ||
// to the balancergroup at creation time is passed to child policies. | ||
func (s) TestBalancerGroupBuildOptions(t *testing.T) { | ||
const ( | ||
balancerName = "stubBalancer-TestBalancerGroupBuildOptions" | ||
parent = int64(1234) | ||
userAgent = "ua" | ||
defaultTestTimeout = 1 * time.Second | ||
) | ||
|
||
// Setup the stub balancer such that we can read the build options passed to | ||
// it in the UpdateClientConnState method. | ||
ccsCh := itestutils.NewChannel() | ||
bOpts := balancer.BuildOptions{ | ||
DialCreds: insecure.NewCredentials(), | ||
ChannelzParentID: parent, | ||
CustomUserAgent: userAgent, | ||
} | ||
stub.Register(balancerName, stub.BalancerFuncs{ | ||
UpdateClientConnState: func(bd *stub.BalancerData, _ balancer.ClientConnState) error { | ||
if !cmp.Equal(bd.BuildOptions, bOpts) { | ||
err := fmt.Errorf("buildOptions in child balancer: %v, want %v", bd, bOpts) | ||
ccsCh.Send(err) | ||
return err | ||
} | ||
ccsCh.Send(nil) | ||
return nil | ||
}, | ||
}) | ||
cc := testutils.NewTestClientConn(t) | ||
bg := New(cc, bOpts, nil, nil, nil) | ||
bg.Start() | ||
|
||
// Add the stub balancer build above as a child policy. | ||
balancerBuilder := balancer.Get(balancerName) | ||
bg.Add(testBalancerIDs[0], balancerBuilder) | ||
|
||
// Send an empty clientConn state change. This should trigger the | ||
// verification of the buildOptions being passed to the child policy. | ||
bg.UpdateClientConnState(testBalancerIDs[0], balancer.ClientConnState{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this function not return the error from the stub? I think Unlike e.g. cluster_manager, where we get errors from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the catch. Done. |
||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
if v, err := ccsCh.Receive(ctx); err != nil { | ||
err2 := v.(error) | ||
t.Fatal(err2) | ||
} | ||
} |
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.
Another option is to export
stub.bb
, and add aNewBB()
. So you don't need to register and get.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 will send a separate PR for that since I would have to touch other existing call sites as well. Thanks.