@@ -278,15 +278,15 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
278278 if creds := cc .dopts .copts .TransportCredentials ; creds != nil {
279279 credsClone = creds .Clone ()
280280 }
281- cc .balancerBuildOpts = balancer.BuildOptions {
281+ cc .balancerWrapper = newCCBalancerWrapper ( cc , balancer.BuildOptions {
282282 DialCreds : credsClone ,
283283 CredsBundle : cc .dopts .copts .CredsBundle ,
284284 Dialer : cc .dopts .copts .Dialer ,
285285 Authority : cc .authority ,
286286 CustomUserAgent : cc .dopts .copts .UserAgent ,
287287 ChannelzParentID : cc .channelzID ,
288288 Target : cc .parsedTarget ,
289- }
289+ })
290290
291291 // Build the resolver.
292292 rWrapper , err := newCCResolverWrapper (cc , resolverBuilder )
@@ -465,12 +465,12 @@ type ClientConn struct {
465465 cancel context.CancelFunc // Cancelled on close.
466466
467467 // The following are initialized at dial time, and are read-only after that.
468- target string // User's dial target.
469- parsedTarget resolver.Target // See parseTargetAndFindResolver().
470- authority string // See determineAuthority().
471- dopts dialOptions // Default and user specified dial options.
472- balancerBuildOpts balancer. BuildOptions // TODO: delete once we move to the gracefulswitch balancer .
473- channelzID * channelz. Identifier // Channelz identifier for the channel .
468+ target string // User's dial target.
469+ parsedTarget resolver.Target // See parseTargetAndFindResolver().
470+ authority string // See determineAuthority().
471+ dopts dialOptions // Default and user specified dial options.
472+ channelzID * channelz. Identifier // Channelz identifier for the channel .
473+ balancerWrapper * ccBalancerWrapper // Uses gracefulswitch.balancer underneath .
474474
475475 // The following provide their own synchronization, and therefore don't
476476 // require cc.mu to be held to access them.
@@ -491,8 +491,6 @@ type ClientConn struct {
491491 sc * ServiceConfig // Latest service config received from the resolver.
492492 conns map [* addrConn ]struct {} // Set to nil on close.
493493 mkp keepalive.ClientParameters // May be updated upon receipt of a GoAway.
494- curBalancerName string // TODO: delete as part of https://github.com/grpc/grpc-go/issues/5229.
495- balancerWrapper * ccBalancerWrapper // TODO: Use gracefulswitch balancer to be able to initialize this once and never rewrite.
496494
497495 lceMu sync.Mutex // protects lastConnectionError
498496 lastConnectionError error
@@ -537,14 +535,7 @@ func (cc *ClientConn) GetState() connectivity.State {
537535// Notice: This API is EXPERIMENTAL and may be changed or removed in a later
538536// release.
539537func (cc * ClientConn ) Connect () {
540- cc .mu .Lock ()
541- defer cc .mu .Unlock ()
542- if cc .balancerWrapper .exitIdle () {
543- return
544- }
545- for ac := range cc .conns {
546- go ac .connect ()
547- }
538+ cc .balancerWrapper .exitIdle ()
548539}
549540
550541func (cc * ClientConn ) scWatcher () {
@@ -666,21 +657,9 @@ func (cc *ClientConn) updateResolverState(s resolver.State, err error) error {
666657 if cc .sc != nil && cc .sc .lbConfig != nil {
667658 balCfg = cc .sc .lbConfig .cfg
668659 }
669-
670- cbn := cc .curBalancerName
671660 bw := cc .balancerWrapper
672661 cc .mu .Unlock ()
673- if cbn != grpclbName {
674- // Filter any grpclb addresses since we don't have the grpclb balancer.
675- var addrs []resolver.Address
676- for _ , addr := range s .Addresses {
677- if addr .Type == resolver .GRPCLB {
678- continue
679- }
680- addrs = append (addrs , addr )
681- }
682- s .Addresses = addrs
683- }
662+
684663 uccsErr := bw .updateClientConnState (& balancer.ClientConnState {ResolverState : s , BalancerConfig : balCfg })
685664 if ret == nil {
686665 ret = uccsErr // prefer ErrBadResolver state since any other error is
@@ -709,50 +688,8 @@ func (cc *ClientConn) applyFailingLB(sc *serviceconfig.ParseResult) {
709688 cc .csMgr .updateState (connectivity .TransientFailure )
710689}
711690
712- // switchBalancer starts the switching from current balancer to the balancer
713- // with the given name.
714- //
715- // It will NOT send the current address list to the new balancer. If needed,
716- // caller of this function should send address list to the new balancer after
717- // this function returns.
718- //
719- // Caller must hold cc.mu.
720- func (cc * ClientConn ) switchBalancer (name string ) {
721- if strings .EqualFold (cc .curBalancerName , name ) {
722- return
723- }
724-
725- channelz .Infof (logger , cc .channelzID , "ClientConn switching balancer to %q" , name )
726- // Don't hold cc.mu while closing the balancers. The balancers may call
727- // methods that require cc.mu (e.g. cc.NewSubConn()). Holding the mutex
728- // would cause a deadlock in that case.
729- cc .mu .Unlock ()
730- cc .balancerWrapper .close ()
731- cc .mu .Lock ()
732-
733- builder := balancer .Get (name )
734- if builder == nil {
735- channelz .Warningf (logger , cc .channelzID , "Channel switches to new LB policy %q due to fallback from invalid balancer name" , PickFirstBalancerName )
736- channelz .Infof (logger , cc .channelzID , "failed to get balancer builder for: %v, using pick_first instead" , name )
737- builder = newPickfirstBuilder ()
738- } else {
739- channelz .Infof (logger , cc .channelzID , "Channel switches to new LB policy %q" , name )
740- }
741-
742- cc .curBalancerName = builder .Name ()
743- cc .balancerWrapper = newCCBalancerWrapper (cc , builder , cc .balancerBuildOpts )
744- }
745-
746691func (cc * ClientConn ) handleSubConnStateChange (sc balancer.SubConn , s connectivity.State , err error ) {
747- cc .mu .Lock ()
748- if cc .conns == nil {
749- cc .mu .Unlock ()
750- return
751- }
752- // TODO(bar switching) send updates to all balancer wrappers when balancer
753- // gracefully switching is supported.
754- cc .balancerWrapper .handleSubConnStateChange (sc , s , err )
755- cc .mu .Unlock ()
692+ cc .balancerWrapper .updateSubConnState (sc , s , err )
756693}
757694
758695// newAddrConn creates an addrConn for addrs and adds it to cc.conns.
@@ -1002,8 +939,6 @@ func (cc *ClientConn) applyServiceConfigAndBalancer(sc *ServiceConfig, configSel
1002939 cc .retryThrottler .Store ((* retryThrottler )(nil ))
1003940 }
1004941
1005- // Only look at balancer types and switch balancer if balancer dial
1006- // option is not set.
1007942 var newBalancerName string
1008943 if cc .sc != nil && cc .sc .lbConfig != nil {
1009944 newBalancerName = cc .sc .lbConfig .name
@@ -1023,7 +958,7 @@ func (cc *ClientConn) applyServiceConfigAndBalancer(sc *ServiceConfig, configSel
1023958 newBalancerName = PickFirstBalancerName
1024959 }
1025960 }
1026- cc .switchBalancer (newBalancerName )
961+ cc .balancerWrapper . switchTo (newBalancerName )
1027962}
1028963
1029964func (cc * ClientConn ) resolveNow (o resolver.ResolveNowOptions ) {
@@ -1074,11 +1009,11 @@ func (cc *ClientConn) Close() error {
10741009 rWrapper := cc .resolverWrapper
10751010 cc .resolverWrapper = nil
10761011 bWrapper := cc .balancerWrapper
1077- cc .balancerWrapper = nil
10781012 cc .mu .Unlock ()
10791013
1014+ // The order of closing matters here since the balancer wrapper assumes the
1015+ // picker is closed before it is closed.
10801016 cc .blockingpicker .close ()
1081-
10821017 if bWrapper != nil {
10831018 bWrapper .close ()
10841019 }
0 commit comments