Skip to content

Commit 3156151

Browse files
authored
grpclb: teach the manual resolver to handle restarts (#6635)
1 parent 1457a96 commit 3156151

File tree

2 files changed

+8
-64
lines changed

2 files changed

+8
-64
lines changed

balancer/grpclb/grpclb.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"google.golang.org/grpc/internal/pretty"
4444
"google.golang.org/grpc/internal/resolver/dns"
4545
"google.golang.org/grpc/resolver"
46+
"google.golang.org/grpc/resolver/manual"
4647

4748
durationpb "github.com/golang/protobuf/ptypes/duration"
4849
lbpb "google.golang.org/grpc/balancer/grpclb/grpc_lb_v1"
@@ -135,7 +136,11 @@ func (b *lbBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) bal
135136
// This generates a manual resolver builder with a fixed scheme. This
136137
// scheme will be used to dial to remote LB, so we can send filtered
137138
// address updates to remote LB ClientConn using this manual resolver.
138-
r := &lbManualResolver{scheme: "grpclb-internal", ccb: cc}
139+
mr := manual.NewBuilderWithScheme("grpclb-internal")
140+
// ResolveNow() on this manual resolver is forwarded to the parent
141+
// ClientConn, so when grpclb client loses contact with the remote balancer,
142+
// the parent ClientConn's resolver will re-resolve.
143+
mr.ResolveNowCallback = cc.ResolveNow
139144

140145
lb := &lbBalancer{
141146
cc: newLBCacheClientConn(cc),
@@ -145,7 +150,7 @@ func (b *lbBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) bal
145150
fallbackTimeout: b.fallbackTimeout,
146151
doneCh: make(chan struct{}),
147152

148-
manualResolver: r,
153+
manualResolver: mr,
149154
subConns: make(map[resolver.Address]balancer.SubConn),
150155
scStates: make(map[balancer.SubConn]connectivity.State),
151156
picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),
@@ -193,7 +198,7 @@ type lbBalancer struct {
193198
// manualResolver is used in the remote LB ClientConn inside grpclb. When
194199
// resolved address updates are received by grpclb, filtered updates will be
195200
// send to remote LB ClientConn through this resolver.
196-
manualResolver *lbManualResolver
201+
manualResolver *manual.Resolver
197202
// The ClientConn to talk to the remote balancer.
198203
ccRemoteLB *remoteBalancerCCWrapper
199204
// backoff for calling remote balancer.

balancer/grpclb/grpclb_util.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,67 +27,6 @@ import (
2727
"google.golang.org/grpc/resolver"
2828
)
2929

30-
// The parent ClientConn should re-resolve when grpclb loses connection to the
31-
// remote balancer. When the ClientConn inside grpclb gets a TransientFailure,
32-
// it calls lbManualResolver.ResolveNow(), which calls parent ClientConn's
33-
// ResolveNow, and eventually results in re-resolve happening in parent
34-
// ClientConn's resolver (DNS for example).
35-
//
36-
// parent
37-
// ClientConn
38-
// +-----------------------------------------------------------------+
39-
// | parent +---------------------------------+ |
40-
// | DNS ClientConn | grpclb | |
41-
// | resolver balancerWrapper | | |
42-
// | + + | grpclb grpclb | |
43-
// | | | | ManualResolver ClientConn | |
44-
// | | | | + + | |
45-
// | | | | | | Transient | |
46-
// | | | | | | Failure | |
47-
// | | | | | <--------- | | |
48-
// | | | <--------------- | ResolveNow | | |
49-
// | | <--------- | ResolveNow | | | | |
50-
// | | ResolveNow | | | | | |
51-
// | | | | | | | |
52-
// | + + | + + | |
53-
// | +---------------------------------+ |
54-
// +-----------------------------------------------------------------+
55-
56-
// lbManualResolver is used by the ClientConn inside grpclb. It's a manual
57-
// resolver with a special ResolveNow() function.
58-
//
59-
// When ResolveNow() is called, it calls ResolveNow() on the parent ClientConn,
60-
// so when grpclb client lose contact with remote balancers, the parent
61-
// ClientConn's resolver will re-resolve.
62-
type lbManualResolver struct {
63-
scheme string
64-
ccr resolver.ClientConn
65-
66-
ccb balancer.ClientConn
67-
}
68-
69-
func (r *lbManualResolver) Build(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
70-
r.ccr = cc
71-
return r, nil
72-
}
73-
74-
func (r *lbManualResolver) Scheme() string {
75-
return r.scheme
76-
}
77-
78-
// ResolveNow calls resolveNow on the parent ClientConn.
79-
func (r *lbManualResolver) ResolveNow(o resolver.ResolveNowOptions) {
80-
r.ccb.ResolveNow(o)
81-
}
82-
83-
// Close is a noop for Resolver.
84-
func (*lbManualResolver) Close() {}
85-
86-
// UpdateState calls cc.UpdateState.
87-
func (r *lbManualResolver) UpdateState(s resolver.State) {
88-
r.ccr.UpdateState(s)
89-
}
90-
9130
const subConnCacheTime = time.Second * 10
9231

9332
// lbCacheClientConn is a wrapper balancer.ClientConn with a SubConn cache.

0 commit comments

Comments
 (0)