Search in sources :

Example 11 with RoundRobinPicker

use of io.grpc.grpclb.GrpclbState.RoundRobinPicker in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method roundRobinPickerWithDrop.

@Test
public void roundRobinPickerWithDrop() {
    assertTrue(DROP_PICK_RESULT.isDrop());
    GrpclbClientLoadRecorder loadRecorder = new GrpclbClientLoadRecorder(fakeClock.getTimeProvider());
    Subchannel subchannel = mock(Subchannel.class);
    // 1 out of 2 requests are to be dropped
    DropEntry d = new DropEntry(loadRecorder, "LBTOKEN0003");
    List<DropEntry> dropList = Arrays.asList(null, d);
    BackendEntry b1 = new BackendEntry(subchannel, loadRecorder, "LBTOKEN0001");
    BackendEntry b2 = new BackendEntry(subchannel, loadRecorder, "LBTOKEN0002");
    List<BackendEntry> pickList = Arrays.asList(b1, b2);
    RoundRobinPicker picker = new RoundRobinPicker(dropList, pickList);
    // dropList[0], pickList[0]
    PickSubchannelArgs args1 = mock(PickSubchannelArgs.class);
    Metadata headers1 = new Metadata();
    headers1.put(GrpclbConstants.TOKEN_METADATA_KEY, "LBTOKEN__OLD");
    when(args1.getHeaders()).thenReturn(headers1);
    assertSame(b1.result, picker.pickSubchannel(args1));
    verify(args1).getHeaders();
    assertThat(headers1.getAll(GrpclbConstants.TOKEN_METADATA_KEY)).containsExactly("LBTOKEN0001");
    // dropList[1]: drop
    PickSubchannelArgs args2 = mock(PickSubchannelArgs.class);
    Metadata headers2 = new Metadata();
    when(args2.getHeaders()).thenReturn(headers2);
    assertSame(DROP_PICK_RESULT, picker.pickSubchannel(args2));
    verify(args2, never()).getHeaders();
    // dropList[0], pickList[1]
    PickSubchannelArgs args3 = mock(PickSubchannelArgs.class);
    Metadata headers3 = new Metadata();
    when(args3.getHeaders()).thenReturn(headers3);
    assertSame(b2.result, picker.pickSubchannel(args3));
    verify(args3).getHeaders();
    assertThat(headers3.getAll(GrpclbConstants.TOKEN_METADATA_KEY)).containsExactly("LBTOKEN0002");
    // dropList[1]: drop
    PickSubchannelArgs args4 = mock(PickSubchannelArgs.class);
    Metadata headers4 = new Metadata();
    when(args4.getHeaders()).thenReturn(headers4);
    assertSame(DROP_PICK_RESULT, picker.pickSubchannel(args4));
    verify(args4, never()).getHeaders();
    // dropList[0], pickList[0]
    PickSubchannelArgs args5 = mock(PickSubchannelArgs.class);
    Metadata headers5 = new Metadata();
    when(args5.getHeaders()).thenReturn(headers5);
    assertSame(b1.result, picker.pickSubchannel(args5));
    verify(args5).getHeaders();
    assertThat(headers5.getAll(GrpclbConstants.TOKEN_METADATA_KEY)).containsExactly("LBTOKEN0001");
    verify(subchannel, never()).getAttributes();
}
Also used : BackendEntry(io.grpc.grpclb.GrpclbState.BackendEntry) RoundRobinPicker(io.grpc.grpclb.GrpclbState.RoundRobinPicker) DropEntry(io.grpc.grpclb.GrpclbState.DropEntry) Subchannel(io.grpc.LoadBalancer.Subchannel) Metadata(io.grpc.Metadata) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 12 with RoundRobinPicker

use of io.grpc.grpclb.GrpclbState.RoundRobinPicker in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method receiveNoBackendAndBalancerAddress.

@Test
public void receiveNoBackendAndBalancerAddress() {
    deliverResolvedAddresses(Collections.<EquivalentAddressGroup>emptyList(), Collections.<EquivalentAddressGroup>emptyList());
    verify(helper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
    RoundRobinPicker picker = (RoundRobinPicker) pickerCaptor.getValue();
    assertThat(picker.dropList).isEmpty();
    Status error = Iterables.getOnlyElement(picker.pickList).picked(new Metadata()).getStatus();
    assertThat(error.getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(error.getDescription()).isEqualTo("No backend or balancer addresses found");
}
Also used : Status(io.grpc.Status) RoundRobinPicker(io.grpc.grpclb.GrpclbState.RoundRobinPicker) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 13 with RoundRobinPicker

use of io.grpc.grpclb.GrpclbState.RoundRobinPicker in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method nameResolutionFailsThenRecover.

@Test
public void nameResolutionFailsThenRecover() {
    Status error = Status.NOT_FOUND.withDescription("www.google.com not found");
    deliverNameResolutionError(error);
    verify(helper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
    assertThat(logs).containsExactly("INFO: [grpclb-<api.google.com>] Created", "DEBUG: [grpclb-<api.google.com>] Error: " + error).inOrder();
    logs.clear();
    RoundRobinPicker picker = (RoundRobinPicker) pickerCaptor.getValue();
    assertThat(picker.dropList).isEmpty();
    PickResult result = picker.pickSubchannel(mock(PickSubchannelArgs.class));
    assertThat(result.getStatus().getCode()).isEqualTo(Code.UNAVAILABLE);
    assertThat(result.getStatus().getDescription()).isEqualTo(error.getDescription());
    // Recover with a subsequent success
    List<EquivalentAddressGroup> grpclbBalancerList = createResolvedBalancerAddresses(1);
    deliverResolvedAddresses(Collections.<EquivalentAddressGroup>emptyList(), grpclbBalancerList);
    verify(helper).createOobChannel(eq(xattr(grpclbBalancerList)), eq(lbAuthority(0) + NO_USE_AUTHORITY_SUFFIX));
    verify(mockLbService).balanceLoad(lbResponseObserverCaptor.capture());
}
Also used : Status(io.grpc.Status) RoundRobinPicker(io.grpc.grpclb.GrpclbState.RoundRobinPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PickResult(io.grpc.LoadBalancer.PickResult) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 14 with RoundRobinPicker

use of io.grpc.grpclb.GrpclbState.RoundRobinPicker in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method roundRobinPickerWithIdleEntry_noDrop.

@Test
public void roundRobinPickerWithIdleEntry_noDrop() {
    Subchannel subchannel = mock(Subchannel.class);
    IdleSubchannelEntry entry = new IdleSubchannelEntry(subchannel, syncContext);
    RoundRobinPicker picker = new RoundRobinPicker(Collections.<DropEntry>emptyList(), Collections.singletonList(entry));
    PickSubchannelArgs args = mock(PickSubchannelArgs.class);
    verify(subchannel, never()).requestConnection();
    assertThat(picker.pickSubchannel(args)).isSameInstanceAs(PickResult.withNoResult());
    verify(subchannel).requestConnection();
    assertThat(picker.pickSubchannel(args)).isSameInstanceAs(PickResult.withNoResult());
    // Only the first pick triggers requestConnection()
    verify(subchannel).requestConnection();
}
Also used : IdleSubchannelEntry(io.grpc.grpclb.GrpclbState.IdleSubchannelEntry) RoundRobinPicker(io.grpc.grpclb.GrpclbState.RoundRobinPicker) Subchannel(io.grpc.LoadBalancer.Subchannel) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 15 with RoundRobinPicker

use of io.grpc.grpclb.GrpclbState.RoundRobinPicker in project grpc-java by grpc.

the class GrpclbLoadBalancerTest method roundRobinPickerWithIdleEntry_andDrop.

@Test
public void roundRobinPickerWithIdleEntry_andDrop() {
    GrpclbClientLoadRecorder loadRecorder = new GrpclbClientLoadRecorder(fakeClock.getTimeProvider());
    // 1 out of 2 requests are to be dropped
    DropEntry d = new DropEntry(loadRecorder, "LBTOKEN0003");
    List<DropEntry> dropList = Arrays.asList(null, d);
    Subchannel subchannel = mock(Subchannel.class);
    IdleSubchannelEntry entry = new IdleSubchannelEntry(subchannel, syncContext);
    RoundRobinPicker picker = new RoundRobinPicker(dropList, Collections.singletonList(entry));
    PickSubchannelArgs args = mock(PickSubchannelArgs.class);
    verify(subchannel, never()).requestConnection();
    assertThat(picker.pickSubchannel(args)).isSameInstanceAs(PickResult.withNoResult());
    verify(subchannel).requestConnection();
    assertThat(picker.pickSubchannel(args)).isSameInstanceAs(DROP_PICK_RESULT);
    verify(subchannel).requestConnection();
    assertThat(picker.pickSubchannel(args)).isSameInstanceAs(PickResult.withNoResult());
    // Only the first pick triggers requestConnection()
    verify(subchannel).requestConnection();
}
Also used : IdleSubchannelEntry(io.grpc.grpclb.GrpclbState.IdleSubchannelEntry) RoundRobinPicker(io.grpc.grpclb.GrpclbState.RoundRobinPicker) DropEntry(io.grpc.grpclb.GrpclbState.DropEntry) Subchannel(io.grpc.LoadBalancer.Subchannel) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Aggregations

RoundRobinPicker (io.grpc.grpclb.GrpclbState.RoundRobinPicker)15 Test (org.junit.Test)14 Subchannel (io.grpc.LoadBalancer.Subchannel)12 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)10 BackendEntry (io.grpc.grpclb.GrpclbState.BackendEntry)10 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)9 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)8 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)8 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)8 InOrder (org.mockito.InOrder)8 Attributes (io.grpc.Attributes)7 ConnectivityState (io.grpc.ConnectivityState)7 Status (io.grpc.Status)6 PickResult (io.grpc.LoadBalancer.PickResult)5 DropEntry (io.grpc.grpclb.GrpclbState.DropEntry)5 IdleSubchannelEntry (io.grpc.grpclb.GrpclbState.IdleSubchannelEntry)5 InitialLoadBalanceRequest (io.grpc.lb.v1.InitialLoadBalanceRequest)5 LoadBalanceRequest (io.grpc.lb.v1.LoadBalanceRequest)5 ConnectivityStateInfo (io.grpc.ConnectivityStateInfo)4 Metadata (io.grpc.Metadata)4