4444 * generally created by calling {@link #parseLoadBalancingPolicyConfig(List)} from a
4545 * {@link io.grpc.LoadBalancerProvider#parseLoadBalancingPolicyConfig
4646 * provider's parseLoadBalancingPolicyConfig()} implementation.
47- *
48- * <p>Alternatively, the balancer may {@link #switchTo(LoadBalancer.Factory) switch to} a policy
49- * prior to {@link
50- * LoadBalancer#handleResolvedAddresses(ResolvedAddresses) handling resolved addresses} for the
51- * first time. This causes graceful switch to ignore the service config and pass through the
52- * resolved addresses directly to the child policy.
5347 */
5448@ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/5999" )
5549@ NotThreadSafe // Must be accessed in SynchronizationContext
5650public final class GracefulSwitchLoadBalancer extends ForwardingLoadBalancer {
5751 private final LoadBalancer defaultBalancer = new LoadBalancer () {
5852 @ Override
59- public void handleResolvedAddresses (ResolvedAddresses resolvedAddresses ) {
60- // Most LB policies using this class will receive child policy configuration within the
61- // service config, so they are naturally calling switchTo() just before
62- // handleResolvedAddresses(), within their own handleResolvedAddresses(). If switchTo() is
63- // not called immediately after construction that does open up potential for bugs in the
64- // parent policies, where they fail to call switchTo(). So we will use the exception to try
65- // to notice those bugs quickly, as it will fail very loudly.
66- throw new IllegalStateException (
67- "GracefulSwitchLoadBalancer must switch to a load balancing policy before handling"
68- + " ResolvedAddresses" );
53+ public Status acceptResolvedAddresses (ResolvedAddresses resolvedAddresses ) {
54+ throw new AssertionError ("real LB is called instead" );
6955 }
7056
7157 @ Override
@@ -104,7 +90,6 @@ public String toString() {
10490 private LoadBalancer pendingLb = defaultBalancer ;
10591 private ConnectivityState pendingState ;
10692 private SubchannelPicker pendingPicker ;
107- private boolean switchToCalled ;
10893
10994 private boolean currentLbIsReady ;
11095
@@ -114,10 +99,6 @@ public GracefulSwitchLoadBalancer(Helper helper) {
11499
115100 @ Override
116101 public void handleResolvedAddresses (ResolvedAddresses resolvedAddresses ) {
117- if (switchToCalled ) {
118- delegate ().handleResolvedAddresses (resolvedAddresses );
119- return ;
120- }
121102 Config config = (Config ) resolvedAddresses .getLoadBalancingPolicyConfig ();
122103 switchToInternal (config .childFactory );
123104 delegate ().handleResolvedAddresses (
@@ -128,9 +109,6 @@ public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
128109
129110 @ Override
130111 public Status acceptResolvedAddresses (ResolvedAddresses resolvedAddresses ) {
131- if (switchToCalled ) {
132- return delegate ().acceptResolvedAddresses (resolvedAddresses );
133- }
134112 Config config = (Config ) resolvedAddresses .getLoadBalancingPolicyConfig ();
135113 switchToInternal (config .childFactory );
136114 return delegate ().acceptResolvedAddresses (
@@ -139,19 +117,6 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
139117 .build ());
140118 }
141119
142- /**
143- * Gracefully switch to a new policy defined by the given factory, if the given factory isn't
144- * equal to the current one.
145- *
146- * @deprecated Use {@code parseLoadBalancingPolicyConfig()} and pass the configuration to
147- * {@link io.grpc.LoadBalancer.ResolvedAddresses.Builder#setLoadBalancingPolicyConfig}
148- */
149- @ Deprecated
150- public void switchTo (LoadBalancer .Factory newBalancerFactory ) {
151- switchToCalled = true ;
152- switchToInternal (newBalancerFactory );
153- }
154-
155120 private void switchToInternal (LoadBalancer .Factory newBalancerFactory ) {
156121 checkNotNull (newBalancerFactory , "newBalancerFactory" );
157122
0 commit comments