Search in sources :

Example 11 with ConnectivityState

use of io.grpc.ConnectivityState in project grpc-java by grpc.

the class LeastRequestLoadBalancerTest method subchannelStateIsolation.

@Test
public void subchannelStateIsolation() throws Exception {
    Iterator<Subchannel> subchannelIterator = subchannels.values().iterator();
    Subchannel sc1 = subchannelIterator.next();
    Subchannel sc2 = subchannelIterator.next();
    Subchannel sc3 = subchannelIterator.next();
    loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(Attributes.EMPTY).build());
    verify(sc1, times(1)).requestConnection();
    verify(sc2, times(1)).requestConnection();
    verify(sc3, times(1)).requestConnection();
    deliverSubchannelState(sc1, ConnectivityStateInfo.forNonError(READY));
    deliverSubchannelState(sc2, ConnectivityStateInfo.forNonError(READY));
    deliverSubchannelState(sc3, ConnectivityStateInfo.forNonError(READY));
    deliverSubchannelState(sc2, ConnectivityStateInfo.forNonError(IDLE));
    deliverSubchannelState(sc3, ConnectivityStateInfo.forTransientFailure(Status.UNAVAILABLE));
    verify(mockHelper, times(6)).updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());
    Iterator<ConnectivityState> stateIterator = stateCaptor.getAllValues().iterator();
    Iterator<SubchannelPicker> pickers = pickerCaptor.getAllValues().iterator();
    // The picker is incrementally updated as subchannels become READY
    assertEquals(CONNECTING, stateIterator.next());
    assertThat(pickers.next()).isInstanceOf(EmptyPicker.class);
    assertEquals(READY, stateIterator.next());
    assertThat(getList(pickers.next())).containsExactly(sc1);
    assertEquals(READY, stateIterator.next());
    assertThat(getList(pickers.next())).containsExactly(sc1, sc2);
    assertEquals(READY, stateIterator.next());
    assertThat(getList(pickers.next())).containsExactly(sc1, sc2, sc3);
    // The IDLE subchannel is dropped from the picker, but a reconnection is requested
    assertEquals(READY, stateIterator.next());
    assertThat(getList(pickers.next())).containsExactly(sc1, sc3);
    verify(sc2, times(2)).requestConnection();
    // The failing subchannel is dropped from the picker, with no requested reconnect
    assertEquals(READY, stateIterator.next());
    assertThat(getList(pickers.next())).containsExactly(sc1);
    verify(sc3, times(1)).requestConnection();
    assertThat(stateIterator.hasNext()).isFalse();
    assertThat(pickers.hasNext()).isFalse();
}
Also used : SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) ConnectivityState(io.grpc.ConnectivityState) Subchannel(io.grpc.LoadBalancer.Subchannel) Test(org.junit.Test)

Example 12 with ConnectivityState

use of io.grpc.ConnectivityState in project grpc-java by grpc.

the class LeastRequestLoadBalancerTest method nameResolutionErrorWithActiveChannels.

@Test
public void nameResolutionErrorWithActiveChannels() throws Exception {
    int choiceCount = 8;
    final Subchannel readySubchannel = subchannels.values().iterator().next();
    loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setLoadBalancingPolicyConfig(new LeastRequestConfig(choiceCount)).setAddresses(servers).setAttributes(affinity).build());
    deliverSubchannelState(readySubchannel, ConnectivityStateInfo.forNonError(READY));
    loadBalancer.handleNameResolutionError(Status.NOT_FOUND.withDescription("nameResolutionError"));
    verify(mockHelper, times(3)).createSubchannel(any(CreateSubchannelArgs.class));
    verify(mockHelper, times(2)).updateBalancingState(stateCaptor.capture(), pickerCaptor.capture());
    Iterator<ConnectivityState> stateIterator = stateCaptor.getAllValues().iterator();
    assertEquals(CONNECTING, stateIterator.next());
    assertEquals(READY, stateIterator.next());
    LoadBalancer.PickResult pickResult = pickerCaptor.getValue().pickSubchannel(mockArgs);
    verify(mockRandom, times(choiceCount)).nextInt(1);
    assertEquals(readySubchannel, pickResult.getSubchannel());
    assertEquals(Status.OK.getCode(), pickResult.getStatus().getCode());
    LoadBalancer.PickResult pickResult2 = pickerCaptor.getValue().pickSubchannel(mockArgs);
    verify(mockRandom, times(choiceCount * 2)).nextInt(1);
    assertEquals(readySubchannel, pickResult2.getSubchannel());
    verifyNoMoreInteractions(mockHelper);
}
Also used : LeastRequestConfig(io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig) ConnectivityState(io.grpc.ConnectivityState) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalancer(io.grpc.LoadBalancer) PickResult(io.grpc.LoadBalancer.PickResult) Test(org.junit.Test)

Aggregations

ConnectivityState (io.grpc.ConnectivityState)12 Test (org.junit.Test)6 Subchannel (io.grpc.LoadBalancer.Subchannel)5 PickResult (io.grpc.LoadBalancer.PickResult)3 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)3 LoadBalancer (io.grpc.LoadBalancer)2 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 Attributes (io.grpc.Attributes)1 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)1 Metadata (io.grpc.Metadata)1 Status (io.grpc.Status)1 PickSubchannelArgsImpl (io.grpc.internal.PickSubchannelArgsImpl)1 CachedRouteLookupResponse (io.grpc.rls.CachingRlsLbClient.CachedRouteLookupResponse)1 RouteLookupRequest (io.grpc.rls.RlsProtoData.RouteLookupRequest)1 LeastRequestConfig (io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig)1 WeightedChildPicker (io.grpc.xds.WeightedRandomPicker.WeightedChildPicker)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 InOrder (org.mockito.InOrder)1