Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public void handleNameResolutionError(Status error) {
}

private void processSubchannelState(Subchannel subchannel, ConnectivityStateInfo stateInfo) {
if (subchannel != this.subchannel) {
return;
}
ConnectivityState newState = stateInfo.getState();
if (newState == SHUTDOWN) {
return;
Expand Down Expand Up @@ -161,6 +164,7 @@ private void updateBalancingState(ConnectivityState state, SubchannelPicker pick
public void shutdown() {
if (subchannel != null) {
subchannel.shutdown();
subchannel = null;
}
}

Expand Down
23 changes: 23 additions & 0 deletions core/src/test/java/io/grpc/internal/PickFirstLoadBalancerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,29 @@ public void requestConnection() {
verify(mockSubchannel, times(2)).requestConnection();
}

@Test
public void ignoreStaleSubchannelStateChange() throws Exception {
loadBalancer.acceptResolvedAddresses(
ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(affinity).build());
verify(mockSubchannel).start(stateListenerCaptor.capture());
SubchannelStateListener oldListener = stateListenerCaptor.getValue();

// Name resolution error occurs, shutting down old subchannel and resetting subchannel reference
loadBalancer.handleNameResolutionError(Status.UNAVAILABLE);

// New resolution result arrives, creating a new subchannel
Subchannel newSubchannel = org.mockito.Mockito.mock(Subchannel.class);
when(mockHelper.createSubchannel(any())).thenReturn(newSubchannel);
loadBalancer.acceptResolvedAddresses(
ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(affinity).build());

// Old subchannel (e.g. during delayed shutdown) fires READY state update
oldListener.onSubchannelState(ConnectivityStateInfo.forNonError(READY));

// Verify that the LB state was NOT updated to READY with the old subchannel
verify(mockHelper, never()).updateBalancingState(eq(READY), any(SubchannelPicker.class));
}

private static class FakeSocketAddress extends SocketAddress {
final String name;

Expand Down
Loading