@@ -57,14 +57,15 @@ final class CdsLoadBalancer2 extends LoadBalancer {
5757 private final XdsLogger logger ;
5858 private final Helper helper ;
5959 private final LoadBalancerRegistry lbRegistry ;
60+ private final GracefulSwitchLoadBalancer delegate ;
6061 // Following fields are effectively final.
6162 private String clusterName ;
6263 private Subscription clusterSubscription ;
63- private LoadBalancer childLb ;
6464
6565 CdsLoadBalancer2 (Helper helper , LoadBalancerRegistry lbRegistry ) {
6666 this .helper = checkNotNull (helper , "helper" );
6767 this .lbRegistry = checkNotNull (lbRegistry , "lbRegistry" );
68+ this .delegate = new GracefulSwitchLoadBalancer (helper );
6869 logger = XdsLogger .withLogId (InternalLogId .allocate ("cds-lb" , helper .getAuthority ()));
6970 logger .log (XdsLogLevel .INFO , "Created" );
7071 }
@@ -99,7 +100,7 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
99100 XdsClusterConfig clusterConfig = clusterConfigOr .getValue ();
100101
101102 NameResolver .ConfigOrError configOrError ;
102- Object childConfig ;
103+ Object gracefulConfig ;
103104 if (clusterConfig .getChildren () instanceof EndpointConfig ) {
104105 // The LB policy config is provided in service_config.proto/JSON format.
105106 configOrError =
@@ -131,17 +132,13 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
131132 result .upstreamTlsContext (),
132133 result .filterMetadata ());
133134 }
134- childConfig = new ClusterResolverConfig (
135+ gracefulConfig = GracefulSwitchLoadBalancer .createLoadBalancingPolicyConfig (
136+ lbRegistry .getProvider (CLUSTER_RESOLVER_POLICY_NAME ),
137+ new ClusterResolverConfig (
135138 instance ,
136139 configOrError .getConfig (),
137- clusterConfig .getClusterResource ().isHttp11ProxyAvailable ());
138- if (childLb == null ) {
139- childLb = lbRegistry .getProvider (CLUSTER_RESOLVER_POLICY_NAME ).newLoadBalancer (helper );
140- }
140+ clusterConfig .getClusterResource ().isHttp11ProxyAvailable ()));
141141 } else if (clusterConfig .getChildren () instanceof AggregateConfig ) {
142- if (childLb == null ) {
143- childLb = lbRegistry .getProvider (PRIORITY_POLICY_NAME ).newLoadBalancer (helper );
144- }
145142 Map <String , PriorityChildConfig > priorityChildConfigs = new HashMap <>();
146143 List <String > leafClusters = ((AggregateConfig ) clusterConfig .getChildren ()).getLeafNames ();
147144 for (String childCluster : leafClusters ) {
@@ -152,23 +149,25 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
152149 new CdsConfig (childCluster )),
153150 false ));
154151 }
155- childConfig = new PriorityLoadBalancerProvider .PriorityLbConfig (
156- Collections .unmodifiableMap (priorityChildConfigs ), leafClusters );
152+ gracefulConfig = GracefulSwitchLoadBalancer .createLoadBalancingPolicyConfig (
153+ lbRegistry .getProvider (PRIORITY_POLICY_NAME ),
154+ new PriorityLoadBalancerProvider .PriorityLbConfig (
155+ Collections .unmodifiableMap (priorityChildConfigs ), leafClusters ));
157156 } else {
158157 return fail (Status .INTERNAL .withDescription (
159158 errorPrefix () + "Unexpected cluster children type: "
160159 + clusterConfig .getChildren ().getClass ()));
161160 }
162161
163- return childLb .acceptResolvedAddresses (
164- resolvedAddresses .toBuilder ().setLoadBalancingPolicyConfig (childConfig ).build ());
162+ return delegate .acceptResolvedAddresses (
163+ resolvedAddresses .toBuilder ().setLoadBalancingPolicyConfig (gracefulConfig ).build ());
165164 }
166165
167166 @ Override
168167 public void handleNameResolutionError (Status error ) {
169168 logger .log (XdsLogLevel .WARNING , "Received name resolution error: {0}" , error );
170- if (childLb != null ) {
171- childLb .handleNameResolutionError (error );
169+ if (delegate != null ) {
170+ delegate .handleNameResolutionError (error );
172171 } else {
173172 helper .updateBalancingState (
174173 TRANSIENT_FAILURE , new FixedResultPicker (PickResult .withError (error )));
@@ -178,10 +177,7 @@ public void handleNameResolutionError(Status error) {
178177 @ Override
179178 public void shutdown () {
180179 logger .log (XdsLogLevel .INFO , "Shutdown" );
181- if (childLb != null ) {
182- childLb .shutdown ();
183- childLb = null ;
184- }
180+ delegate .shutdown ();
185181 if (clusterSubscription != null ) {
186182 clusterSubscription .close ();
187183 clusterSubscription = null ;
@@ -190,10 +186,7 @@ public void shutdown() {
190186
191187 @ CheckReturnValue // don't forget to return up the stack after the fail call
192188 private Status fail (Status error ) {
193- if (childLb != null ) {
194- childLb .shutdown ();
195- childLb = null ;
196- }
189+ delegate .shutdown ();
197190 helper .updateBalancingState (
198191 TRANSIENT_FAILURE , new FixedResultPicker (PickResult .withError (error )));
199192 return Status .OK ; // XdsNameResolver isn't a polling NR, so this value doesn't matter
0 commit comments