Skip to content

Commit

Permalink
grpclb: fix pick_first mode shutdown without subchannels. (#6072)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangkun83 committed Aug 16, 2019
1 parent e68e400 commit 2c0b2de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
6 changes: 4 additions & 2 deletions grpclb/src/main/java/io/grpc/grpclb/GrpclbState.java
Expand Up @@ -341,8 +341,10 @@ void shutdown() {
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);
Expand Down
25 changes: 25 additions & 0 deletions grpclb/src/test/java/io/grpc/grpclb/GrpclbLoadBalancerTest.java
Expand Up @@ -1813,6 +1813,31 @@ public void grpclbWorking_pickFirstMode() throws Exception {
.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 {
Expand Down

0 comments on commit 2c0b2de

Please sign in to comment.