util: Replace BUFFER_PICKER with FixedResultPicker

I think at some point there were more usages in the tests. But now it
is pretty easy.

PriorityLb.ChildLbState.picker is initialized to
FixedResultPicker(NoResult). So now that GracefulSwitchLb is using the
same picker, equals() is able to de-dup an update.
This commit is contained in:
Eric Anderson 2025-03-28 19:49:36 +00:00 committed by GitHub
parent 2e260a4bbc
commit 2448c8b6b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 22 deletions

View File

@ -19,7 +19,6 @@ package io.grpc.util;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import io.grpc.ConnectivityState; import io.grpc.ConnectivityState;
@ -66,19 +65,6 @@ public final class GracefulSwitchLoadBalancer extends ForwardingLoadBalancer {
public void shutdown() {} public void shutdown() {}
}; };
@VisibleForTesting
static final SubchannelPicker BUFFER_PICKER = new SubchannelPicker() {
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
return PickResult.withNoResult();
}
@Override
public String toString() {
return "BUFFER_PICKER";
}
};
private final Helper helper; private final Helper helper;
// While the new policy is not fully switched on, the pendingLb is handling new updates from name // While the new policy is not fully switched on, the pendingLb is handling new updates from name
@ -128,7 +114,7 @@ public final class GracefulSwitchLoadBalancer extends ForwardingLoadBalancer {
pendingLb = defaultBalancer; pendingLb = defaultBalancer;
pendingBalancerFactory = null; pendingBalancerFactory = null;
pendingState = ConnectivityState.CONNECTING; pendingState = ConnectivityState.CONNECTING;
pendingPicker = BUFFER_PICKER; pendingPicker = new FixedResultPicker(PickResult.withNoResult());
if (newBalancerFactory.equals(currentBalancerFactory)) { if (newBalancerFactory.equals(currentBalancerFactory)) {
return; return;

View File

@ -21,7 +21,6 @@ import static io.grpc.ConnectivityState.CONNECTING;
import static io.grpc.ConnectivityState.IDLE; import static io.grpc.ConnectivityState.IDLE;
import static io.grpc.ConnectivityState.READY; import static io.grpc.ConnectivityState.READY;
import static io.grpc.ConnectivityState.TRANSIENT_FAILURE; import static io.grpc.ConnectivityState.TRANSIENT_FAILURE;
import static io.grpc.util.GracefulSwitchLoadBalancer.BUFFER_PICKER;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.inOrder;
@ -521,7 +520,11 @@ public class GracefulSwitchLoadBalancerTest {
helper0.updateBalancingState(CONNECTING, picker); helper0.updateBalancingState(CONNECTING, picker);
verify(mockHelper, never()).updateBalancingState(CONNECTING, picker); verify(mockHelper, never()).updateBalancingState(CONNECTING, picker);
inOrder.verify(mockHelper).updateBalancingState(CONNECTING, BUFFER_PICKER); ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(SubchannelPicker.class);
inOrder.verify(mockHelper).updateBalancingState(eq(CONNECTING), pickerCaptor.capture());
assertThat(pickerCaptor.getValue().pickSubchannel(mock(PickSubchannelArgs.class)).hasResult())
.isFalse();
inOrder.verify(lb0).shutdown(); // shutdown after update inOrder.verify(lb0).shutdown(); // shutdown after update
picker = mock(SubchannelPicker.class); picker = mock(SubchannelPicker.class);

View File

@ -522,8 +522,7 @@ public class PriorityLoadBalancerTest {
.setLoadBalancingPolicyConfig(priorityLbConfig) .setLoadBalancingPolicyConfig(priorityLbConfig)
.build()); .build());
// Nothing important about this verify, other than to provide a baseline // Nothing important about this verify, other than to provide a baseline
verify(helper, times(2)) verify(helper).updateBalancingState(eq(CONNECTING), pickerReturns(PickResult.withNoResult()));
.updateBalancingState(eq(CONNECTING), pickerReturns(PickResult.withNoResult()));
assertThat(fooBalancers).hasSize(1); assertThat(fooBalancers).hasSize(1);
assertThat(fooHelpers).hasSize(1); assertThat(fooHelpers).hasSize(1);
Helper helper0 = Iterables.getOnlyElement(fooHelpers); Helper helper0 = Iterables.getOnlyElement(fooHelpers);
@ -539,7 +538,7 @@ public class PriorityLoadBalancerTest {
helper0.updateBalancingState( helper0.updateBalancingState(
CONNECTING, CONNECTING,
EMPTY_PICKER); EMPTY_PICKER);
verify(helper, times(3)) verify(helper, times(2))
.updateBalancingState(eq(CONNECTING), pickerReturns(PickResult.withNoResult())); .updateBalancingState(eq(CONNECTING), pickerReturns(PickResult.withNoResult()));
// failover happens // failover happens
@ -805,7 +804,7 @@ public class PriorityLoadBalancerTest {
.setAddresses(ImmutableList.<EquivalentAddressGroup>of()) .setAddresses(ImmutableList.<EquivalentAddressGroup>of())
.setLoadBalancingPolicyConfig(priorityLbConfig) .setLoadBalancingPolicyConfig(priorityLbConfig)
.build()); .build());
verify(helper, times(2)).updateBalancingState(eq(CONNECTING), isA(SubchannelPicker.class)); verify(helper).updateBalancingState(eq(CONNECTING), isA(SubchannelPicker.class));
// LB shutdown and subchannel state change can happen simultaneously. If shutdown runs first, // LB shutdown and subchannel state change can happen simultaneously. If shutdown runs first,
// any further balancing state update should be ignored. // any further balancing state update should be ignored.
@ -843,7 +842,7 @@ public class PriorityLoadBalancerTest {
.setLoadBalancingPolicyConfig(priorityLbConfig) .setLoadBalancingPolicyConfig(priorityLbConfig)
.build()); .build());
verify(helper, times(6)).updateBalancingState(any(), any()); verify(helper, times(4)).updateBalancingState(any(), any());
} }
private void assertLatestConnectivityState(ConnectivityState expectedState) { private void assertLatestConnectivityState(ConnectivityState expectedState) {