Skip to content

Commit 08d2316

Browse files
authored
grpclb: recreate SubConns when switching fallback (#2899)
With pickfirst, the same SubConn is reused, only addresses are updated. But backends and fallbacks may need different credentials. This change force-removes all SubConns when switching fallback.
1 parent 8e511dc commit 08d2316

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Diff for: balancer/grpclb/grpclb_remote_balancer.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ func (lb *lbBalancer) processServerList(l *lbpb.ServerList) {
9595
//
9696
// Caller must hold lb.mu.
9797
func (lb *lbBalancer) refreshSubConns(backendAddrs []resolver.Address, fallback bool, pickFirst bool) {
98-
lb.inFallback = fallback
99-
10098
opts := balancer.NewSubConnOptions{}
10199
if !fallback {
102100
opts.CredsBundle = lb.grpclbBackendCreds
@@ -105,17 +103,29 @@ func (lb *lbBalancer) refreshSubConns(backendAddrs []resolver.Address, fallback
105103
lb.backendAddrs = backendAddrs
106104
lb.backendAddrsWithoutMetadata = nil
107105

108-
if lb.usePickFirst != pickFirst {
109-
// Remove all SubConns when switching modes.
106+
fallbackModeChanged := lb.inFallback != fallback
107+
lb.inFallback = fallback
108+
109+
balancingPolicyChanged := lb.usePickFirst != pickFirst
110+
oldUsePickFirst := lb.usePickFirst
111+
lb.usePickFirst = pickFirst
112+
113+
if fallbackModeChanged || balancingPolicyChanged {
114+
// Remove all SubConns when switching balancing policy or switching
115+
// fallback mode.
116+
//
117+
// For fallback mode switching with pickfirst, we want to recreate the
118+
// SubConn because the creds could be different.
110119
for a, sc := range lb.subConns {
111-
if lb.usePickFirst {
120+
if oldUsePickFirst {
121+
// If old SubConn were created for pickfirst, bypass cache and
122+
// remove directly.
112123
lb.cc.cc.RemoveSubConn(sc)
113124
} else {
114125
lb.cc.RemoveSubConn(sc)
115126
}
116127
delete(lb.subConns, a)
117128
}
118-
lb.usePickFirst = pickFirst
119129
}
120130

121131
if lb.usePickFirst {

0 commit comments

Comments
 (0)