Search in sources :

Example 26 with PickSubchannelArgs

use of io.grpc.LoadBalancer.PickSubchannelArgs in project grpc-java by grpc.

the class ConfigSelectingClientCallTest method selectionErrorPropagatedToListener.

@Test
public void selectionErrorPropagatedToListener() {
    InternalConfigSelector configSelector = new InternalConfigSelector() {

        @Override
        public Result selectConfig(PickSubchannelArgs args) {
            return Result.forError(Status.FAILED_PRECONDITION);
        }
    };
    ClientCall<Void, Void> configSelectingClientCall = new ConfigSelectingClientCall<>(configSelector, channel, MoreExecutors.directExecutor(), method, CallOptions.DEFAULT);
    configSelectingClientCall.start(callListener, new Metadata());
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(null);
    verify(callListener).onClose(statusCaptor.capture(), any(Metadata.class));
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.FAILED_PRECONDITION);
    // The call should not delegate to null and fail methods with NPE.
    configSelectingClientCall.request(1);
}
Also used : Status(io.grpc.Status) InternalConfigSelector(io.grpc.InternalConfigSelector) ConfigSelectingClientCall(io.grpc.internal.ManagedChannelImpl.ConfigSelectingClientCall) Metadata(io.grpc.Metadata) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 27 with PickSubchannelArgs

use of io.grpc.LoadBalancer.PickSubchannelArgs in project grpc-java by grpc.

the class DelayedClientTransportTest method newStream_racesWithReprocessIdleMode.

@Test
public void newStream_racesWithReprocessIdleMode() throws Exception {
    SubchannelPicker picker = new SubchannelPicker() {

        @Override
        public PickResult pickSubchannel(PickSubchannelArgs args) {
            // Assume entering idle mode raced with the pick
            delayedTransport.reprocess(null);
            // Act like IDLE LB
            return PickResult.withNoResult();
        }
    };
    // Because there is no pending stream yet, it will do nothing but save the picker.
    delayedTransport.reprocess(picker);
    ClientStream stream = delayedTransport.newStream(method, headers, callOptions, tracers);
    stream.start(streamListener);
    assertTrue(delayedTransport.hasPendingStreams());
    verify(transportListener).transportInUse(true);
}
Also used : SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Example 28 with PickSubchannelArgs

use of io.grpc.LoadBalancer.PickSubchannelArgs 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 29 with PickSubchannelArgs

use of io.grpc.LoadBalancer.PickSubchannelArgs 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 30 with PickSubchannelArgs

use of io.grpc.LoadBalancer.PickSubchannelArgs 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

PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)30 Test (org.junit.Test)27 Metadata (io.grpc.Metadata)19 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)17 Subchannel (io.grpc.LoadBalancer.Subchannel)17 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)17 PickResult (io.grpc.LoadBalancer.PickResult)13 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)10 PickSubchannelArgsImpl (io.grpc.internal.PickSubchannelArgsImpl)10 RingHashConfig (io.grpc.xds.RingHashLoadBalancer.RingHashConfig)10 RoundRobinPicker (io.grpc.grpclb.GrpclbState.RoundRobinPicker)6 InOrder (org.mockito.InOrder)6 Attributes (io.grpc.Attributes)5 BackendEntry (io.grpc.grpclb.GrpclbState.BackendEntry)4 DropEntry (io.grpc.grpclb.GrpclbState.DropEntry)4 InitialLoadBalanceResponse (io.grpc.lb.v1.InitialLoadBalanceResponse)4 LoadBalanceResponse (io.grpc.lb.v1.LoadBalanceResponse)4 InternalConfigSelector (io.grpc.InternalConfigSelector)3 Helper (io.grpc.LoadBalancer.Helper)3 Status (io.grpc.Status)3