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

resolver/manual: support restarts, required for channel idleness #6638

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 38 additions & 9 deletions clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,8 @@ func (s) TestUpdateAddresses_NoopIfCalledWithSameAddresses(t *testing.T) {
}

func (s) TestDefaultServiceConfig(t *testing.T) {
r := manual.NewBuilderWithScheme("whatever")
addr := r.Scheme() + ":///non.existent"
js := `{
const defaultSC = `
{
"methodConfig": [
{
"name": [
Expand All @@ -1073,10 +1072,40 @@ func (s) TestDefaultServiceConfig(t *testing.T) {
}
]
}`
testInvalidDefaultServiceConfig(t)
testDefaultServiceConfigWhenResolverServiceConfigDisabled(t, r, addr, js)
testDefaultServiceConfigWhenResolverDoesNotReturnServiceConfig(t, r, addr, js)
testDefaultServiceConfigWhenResolverReturnInvalidServiceConfig(t, r, addr, js)
tests := []struct {
name string
testF func(t *testing.T, r *manual.Resolver, addr, sc string)
sc string
}{
{
name: "invalid-service-config",
testF: testInvalidDefaultServiceConfig,
sc: "",
},
{
name: "resolver-service-config-disabled",
testF: testDefaultServiceConfigWhenResolverServiceConfigDisabled,
sc: defaultSC,
},
{
name: "resolver-does-not-return-service-config",
testF: testDefaultServiceConfigWhenResolverDoesNotReturnServiceConfig,
sc: defaultSC,
},
{
name: "resolver-returns-invalid-service-config",
testF: testDefaultServiceConfigWhenResolverReturnInvalidServiceConfig,
sc: defaultSC,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := manual.NewBuilderWithScheme("whatever")
addr := r.Scheme() + ":///non.existent"
test.testF(t, r, addr, test.sc)
})
}
}

func verifyWaitForReadyEqualsTrue(cc *ClientConn) bool {
Expand All @@ -1091,8 +1120,8 @@ func verifyWaitForReadyEqualsTrue(cc *ClientConn) bool {
return i != 10
}

func testInvalidDefaultServiceConfig(t *testing.T) {
_, err := Dial("fake.com", WithTransportCredentials(insecure.NewCredentials()), WithDefaultServiceConfig(""))
func testInvalidDefaultServiceConfig(t *testing.T, r *manual.Resolver, addr, sc string) {
_, err := Dial(addr, WithTransportCredentials(insecure.NewCredentials()), WithResolvers(r), WithDefaultServiceConfig(sc))
if !strings.Contains(err.Error(), invalidDefaultServiceConfigErrPrefix) {
t.Fatalf("Dial got err: %v, want err contains: %v", err, invalidDefaultServiceConfigErrPrefix)
}
Expand Down
25 changes: 15 additions & 10 deletions resolver/manual/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"google.golang.org/grpc/resolver"
)

// NewBuilderWithScheme creates a new test resolver builder with the given scheme.
// NewBuilderWithScheme creates a new manual resolver builder with the given scheme.
Copy link
Member

Choose a reason for hiding this comment

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

Should we document that this may only be used with a single ClientConn? Otherwise bad things happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

func NewBuilderWithScheme(scheme string) *Resolver {
return &Resolver{
BuildCallback: func(resolver.Target, resolver.ClientConn, resolver.BuildOptions) {},
Expand Down Expand Up @@ -58,30 +58,34 @@ type Resolver struct {
scheme string

// Fields actually belong to the resolver.
mu sync.Mutex // Guards access to CC.
CC resolver.ClientConn
bootstrapState *resolver.State
// Guards access to below fields.
mu sync.Mutex
CC resolver.ClientConn
// Storing the most recent state update makes this resolver resilient to
// restarts, which is possible with channel idleness.
lastSeenState *resolver.State
}

// InitialState adds initial state to the resolver so that UpdateState doesn't
// need to be explicitly called after Dial.
func (r *Resolver) InitialState(s resolver.State) {
r.bootstrapState = &s
r.lastSeenState = &s
}

// Build returns itself for Resolver, because it's both a builder and a resolver.
func (r *Resolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
r.BuildCallback(target, cc, opts)
r.mu.Lock()
r.CC = cc
r.mu.Unlock()
r.BuildCallback(target, cc, opts)
if r.bootstrapState != nil {
r.UpdateState(*r.bootstrapState)
if r.lastSeenState != nil {
err := r.CC.UpdateState(*r.lastSeenState)
r.UpdateStateCallback(err)
dfawley marked this conversation as resolved.
Show resolved Hide resolved
}
r.mu.Unlock()
return r, nil
}

// Scheme returns the test scheme.
// Scheme returns the manual resolver's scheme.
func (r *Resolver) Scheme() string {
return r.scheme
}
Expand All @@ -100,6 +104,7 @@ func (r *Resolver) Close() {
func (r *Resolver) UpdateState(s resolver.State) {
r.mu.Lock()
err := r.CC.UpdateState(s)
r.lastSeenState = &s
r.mu.Unlock()
r.UpdateStateCallback(err)
}
Expand Down
Loading