Search in sources :

Example 1 with ReadyPicker

use of io.grpc.xds.LeastRequestLoadBalancer.ReadyPicker in project grpc-java by grpc.

the class LeastRequestLoadBalancerTest method internalPickerComparisons.

@Test
public void internalPickerComparisons() {
    EmptyPicker emptyOk1 = new EmptyPicker(Status.OK);
    EmptyPicker emptyOk2 = new EmptyPicker(Status.OK.withDescription("different OK"));
    EmptyPicker emptyErr = new EmptyPicker(Status.UNKNOWN.withDescription("¯\\_(ツ)_//¯"));
    Iterator<Subchannel> subchannelIterator = subchannels.values().iterator();
    Subchannel sc1 = subchannelIterator.next();
    Subchannel sc2 = subchannelIterator.next();
    ReadyPicker ready1 = new ReadyPicker(Arrays.asList(sc1, sc2), 2, mockRandom);
    ReadyPicker ready2 = new ReadyPicker(Arrays.asList(sc1), 2, mockRandom);
    ReadyPicker ready3 = new ReadyPicker(Arrays.asList(sc2, sc1), 2, mockRandom);
    ReadyPicker ready4 = new ReadyPicker(Arrays.asList(sc1, sc2), 2, mockRandom);
    ReadyPicker ready5 = new ReadyPicker(Arrays.asList(sc2, sc1), 2, mockRandom);
    ReadyPicker ready6 = new ReadyPicker(Arrays.asList(sc2, sc1), 8, mockRandom);
    assertTrue(emptyOk1.isEquivalentTo(emptyOk2));
    assertFalse(emptyOk1.isEquivalentTo(emptyErr));
    assertFalse(ready1.isEquivalentTo(ready2));
    assertTrue(ready1.isEquivalentTo(ready3));
    assertTrue(ready3.isEquivalentTo(ready4));
    assertTrue(ready4.isEquivalentTo(ready5));
    assertFalse(emptyOk1.isEquivalentTo(ready1));
    assertFalse(ready1.isEquivalentTo(emptyOk1));
    assertFalse(ready5.isEquivalentTo(ready6));
}
Also used : EmptyPicker(io.grpc.xds.LeastRequestLoadBalancer.EmptyPicker) Subchannel(io.grpc.LoadBalancer.Subchannel) ReadyPicker(io.grpc.xds.LeastRequestLoadBalancer.ReadyPicker) Test(org.junit.Test)

Example 2 with ReadyPicker

use of io.grpc.xds.LeastRequestLoadBalancer.ReadyPicker in project grpc-java by grpc.

the class LeastRequestLoadBalancerTest method pickerLeastRequest.

@Test
public void pickerLeastRequest() throws Exception {
    int choiceCount = 2;
    // This should add inFlight counters to all subchannels.
    loadBalancer.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(Attributes.EMPTY).setLoadBalancingPolicyConfig(new LeastRequestConfig(choiceCount)).build());
    assertEquals(3, loadBalancer.getSubchannels().size());
    List<Subchannel> subchannels = Lists.newArrayList(loadBalancer.getSubchannels());
    // Make sure all inFlight counters have started at 0
    assertEquals(0, subchannels.get(0).getAttributes().get(IN_FLIGHTS).get());
    assertEquals(0, subchannels.get(1).getAttributes().get(IN_FLIGHTS).get());
    assertEquals(0, subchannels.get(2).getAttributes().get(IN_FLIGHTS).get());
    for (Subchannel sc : subchannels) {
        deliverSubchannelState(sc, ConnectivityStateInfo.forNonError(READY));
    }
    // Capture the active ReadyPicker once all subchannels are READY
    verify(mockHelper, times(4)).updateBalancingState(any(ConnectivityState.class), pickerCaptor.capture());
    assertThat(pickerCaptor.getValue()).isInstanceOf(ReadyPicker.class);
    ReadyPicker picker = (ReadyPicker) pickerCaptor.getValue();
    assertThat(picker.getList()).containsExactlyElementsIn(subchannels);
    // Make random return 0, then 2 for the sample indexes.
    when(mockRandom.nextInt(subchannels.size())).thenReturn(0, 2);
    PickResult pickResult1 = picker.pickSubchannel(mockArgs);
    verify(mockRandom, times(choiceCount)).nextInt(subchannels.size());
    assertEquals(subchannels.get(0), pickResult1.getSubchannel());
    // This simulates sending the actual RPC on the picked channel
    ClientStreamTracer streamTracer1 = pickResult1.getStreamTracerFactory().newClientStreamTracer(StreamInfo.newBuilder().build(), new Metadata());
    streamTracer1.streamCreated(Attributes.EMPTY, new Metadata());
    assertEquals(1, pickResult1.getSubchannel().getAttributes().get(IN_FLIGHTS).get());
    // For the second pick it should pick the one with lower inFlight.
    when(mockRandom.nextInt(subchannels.size())).thenReturn(0, 2);
    PickResult pickResult2 = picker.pickSubchannel(mockArgs);
    // Since this is the second pick we expect the total random samples to be choiceCount * 2
    verify(mockRandom, times(choiceCount * 2)).nextInt(subchannels.size());
    assertEquals(subchannels.get(2), pickResult2.getSubchannel());
    // For the third pick we unavoidably pick subchannel with index 1.
    when(mockRandom.nextInt(subchannels.size())).thenReturn(1, 1);
    PickResult pickResult3 = picker.pickSubchannel(mockArgs);
    verify(mockRandom, times(choiceCount * 3)).nextInt(subchannels.size());
    assertEquals(subchannels.get(1), pickResult3.getSubchannel());
    // Finally ensure a finished RPC decreases inFlight
    streamTracer1.streamClosed(Status.OK);
    assertEquals(0, pickResult1.getSubchannel().getAttributes().get(IN_FLIGHTS).get());
}
Also used : LeastRequestConfig(io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig) ClientStreamTracer(io.grpc.ClientStreamTracer) ConnectivityState(io.grpc.ConnectivityState) Subchannel(io.grpc.LoadBalancer.Subchannel) PickResult(io.grpc.LoadBalancer.PickResult) Metadata(io.grpc.Metadata) ReadyPicker(io.grpc.xds.LeastRequestLoadBalancer.ReadyPicker) Test(org.junit.Test)

Aggregations

Subchannel (io.grpc.LoadBalancer.Subchannel)2 ReadyPicker (io.grpc.xds.LeastRequestLoadBalancer.ReadyPicker)2 Test (org.junit.Test)2 ClientStreamTracer (io.grpc.ClientStreamTracer)1 ConnectivityState (io.grpc.ConnectivityState)1 PickResult (io.grpc.LoadBalancer.PickResult)1 Metadata (io.grpc.Metadata)1 EmptyPicker (io.grpc.xds.LeastRequestLoadBalancer.EmptyPicker)1 LeastRequestConfig (io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig)1