grpclb: fix pick_first mode shutdown without subchannels. (#6072)

This commit is contained in:
Kun Zhang 2019-08-16 10:21:25 -07:00 committed by GitHub
parent e68e4004e4
commit 2c0b2de862
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -341,8 +341,10 @@ final class GrpclbState {
subchannelPool.clear();
break;
case PICK_FIRST:
checkState(subchannels.size() == 1, "Excessive Subchannels: %s", subchannels);
subchannels.values().iterator().next().shutdown();
if (!subchannels.isEmpty()) {
checkState(subchannels.size() == 1, "Excessive Subchannels: %s", subchannels);
subchannels.values().iterator().next().shutdown();
}
break;
default:
throw new AssertionError("Missing case for " + mode);

View File

@ -1813,6 +1813,31 @@ public class GrpclbLoadBalancerTest {
.returnSubchannel(any(Subchannel.class), any(ConnectivityStateInfo.class));
}
@Test
public void shutdownWithoutSubchannel_roundRobin() throws Exception {
subtestShutdownWithoutSubchannel("round_robin");
}
@Test
public void shutdownWithoutSubchannel_pickFirst() throws Exception {
subtestShutdownWithoutSubchannel("pick_first");
}
private void subtestShutdownWithoutSubchannel(String childPolicy) throws Exception {
String lbConfig = "{\"childPolicy\" : [ {\"" + childPolicy + "\" : {}} ]}";
List<EquivalentAddressGroup> grpclbResolutionList = createResolvedServerAddresses(true);
Attributes grpclbResolutionAttrs = Attributes.newBuilder().set(
LoadBalancer.ATTR_LOAD_BALANCING_CONFIG, parseJsonObject(lbConfig)).build();
deliverResolvedAddresses(grpclbResolutionList, grpclbResolutionAttrs);
verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
assertEquals(1, lbRequestObservers.size());
StreamObserver<LoadBalanceRequest> requestObserver = lbRequestObservers.poll();
verify(requestObserver, never()).onCompleted();
balancer.shutdown();
verify(requestObserver).onCompleted();
}
@SuppressWarnings("deprecation")
@Test
public void grpclbWorking_pickFirstMode_expectBackendsShuffled() throws Exception {