Skip to content

Commit

Permalink
util: Remove GracefulSwitchLb.switchTo()
Browse files Browse the repository at this point in the history
It was deprecated in 85e0a01, so has been deprecated for six
releases/over six months.
  • Loading branch information
ejona86 committed Feb 21, 2025
1 parent 892144d commit f207be3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 484 deletions.
39 changes: 2 additions & 37 deletions util/src/main/java/io/grpc/util/GracefulSwitchLoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,14 @@
* generally created by calling {@link #parseLoadBalancingPolicyConfig(List)} from a
* {@link io.grpc.LoadBalancerProvider#parseLoadBalancingPolicyConfig
* provider's parseLoadBalancingPolicyConfig()} implementation.
*
* <p>Alternatively, the balancer may {@link #switchTo(LoadBalancer.Factory) switch to} a policy
* prior to {@link
* LoadBalancer#handleResolvedAddresses(ResolvedAddresses) handling resolved addresses} for the
* first time. This causes graceful switch to ignore the service config and pass through the
* resolved addresses directly to the child policy.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5999")
@NotThreadSafe // Must be accessed in SynchronizationContext
public final class GracefulSwitchLoadBalancer extends ForwardingLoadBalancer {
private final LoadBalancer defaultBalancer = new LoadBalancer() {
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
// Most LB policies using this class will receive child policy configuration within the
// service config, so they are naturally calling switchTo() just before
// handleResolvedAddresses(), within their own handleResolvedAddresses(). If switchTo() is
// not called immediately after construction that does open up potential for bugs in the
// parent policies, where they fail to call switchTo(). So we will use the exception to try
// to notice those bugs quickly, as it will fail very loudly.
throw new IllegalStateException(
"GracefulSwitchLoadBalancer must switch to a load balancing policy before handling"
+ " ResolvedAddresses");
public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
throw new AssertionError("real LB is called instead");
}

@Override
Expand Down Expand Up @@ -104,7 +90,6 @@ public String toString() {
private LoadBalancer pendingLb = defaultBalancer;
private ConnectivityState pendingState;
private SubchannelPicker pendingPicker;
private boolean switchToCalled;

private boolean currentLbIsReady;

Expand All @@ -114,10 +99,6 @@ public GracefulSwitchLoadBalancer(Helper helper) {

@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
if (switchToCalled) {
delegate().handleResolvedAddresses(resolvedAddresses);
return;
}
Config config = (Config) resolvedAddresses.getLoadBalancingPolicyConfig();
switchToInternal(config.childFactory);
delegate().handleResolvedAddresses(
Expand All @@ -128,9 +109,6 @@ public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {

@Override
public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
if (switchToCalled) {
return delegate().acceptResolvedAddresses(resolvedAddresses);
}
Config config = (Config) resolvedAddresses.getLoadBalancingPolicyConfig();
switchToInternal(config.childFactory);
return delegate().acceptResolvedAddresses(
Expand All @@ -139,19 +117,6 @@ public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
.build());
}

/**
* Gracefully switch to a new policy defined by the given factory, if the given factory isn't
* equal to the current one.
*
* @deprecated Use {@code parseLoadBalancingPolicyConfig()} and pass the configuration to
* {@link io.grpc.LoadBalancer.ResolvedAddresses.Builder#setLoadBalancingPolicyConfig}
*/
@Deprecated
public void switchTo(LoadBalancer.Factory newBalancerFactory) {
switchToCalled = true;
switchToInternal(newBalancerFactory);
}

private void switchToInternal(LoadBalancer.Factory newBalancerFactory) {
checkNotNull(newBalancerFactory, "newBalancerFactory");

Expand Down
Loading

0 comments on commit f207be3

Please sign in to comment.