Search in sources :

Example 11 with LoadBalancer

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

the class PriorityLoadBalancerTest method tearDown.

@After
public void tearDown() {
    priorityLb.shutdown();
    for (LoadBalancer lb : fooBalancers) {
        verify(lb).shutdown();
    }
    for (LoadBalancer lb : barBalancers) {
        verify(lb).shutdown();
    }
    assertThat(fakeClock.getPendingTasks()).isEmpty();
}
Also used : LoadBalancer(io.grpc.LoadBalancer) After(org.junit.After)

Example 12 with LoadBalancer

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

the class ClusterImplLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    when(helper.getAuthority()).thenReturn("api.google.com");
    LoadBalancerProvider provider = new ClusterImplLoadBalancerProvider();
    LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
    assertThat(loadBalancer).isInstanceOf(ClusterImplLoadBalancer.class);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) LoadBalancerProvider(io.grpc.LoadBalancerProvider) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 13 with LoadBalancer

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

the class HealthCheckingLoadBalancerFactoryTest method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    for (int i = 0; i < NUM_SUBCHANNELS; i++) {
        HealthImpl healthImpl = new HealthImpl();
        healthImpls[i] = healthImpl;
        Server server = InProcessServerBuilder.forName("health-check-test-" + i).addService(healthImpl).directExecutor().build().start();
        servers[i] = server;
        ManagedChannel channel = InProcessChannelBuilder.forName("health-check-test-" + i).directExecutor().build();
        channels[i] = channel;
        EquivalentAddressGroup eag = new EquivalentAddressGroup(new FakeSocketAddress("address-" + i));
        eags[i] = eag;
        List<EquivalentAddressGroup> eagList = Arrays.asList(eag);
        eagLists[i] = eagList;
        mockStateListeners[i] = mock(SubchannelStateListener.class);
    }
    resolvedAddressList = Arrays.asList(eags);
    when(backoffPolicyProvider.get()).thenReturn(backoffPolicy1, backoffPolicy2);
    when(backoffPolicy1.nextBackoffNanos()).thenReturn(11L, 21L, 31L);
    when(backoffPolicy2.nextBackoffNanos()).thenReturn(12L, 22L, 32L);
    hcLbFactory = new HealthCheckingLoadBalancerFactory(origLbFactory, backoffPolicyProvider, clock.getStopwatchSupplier());
    hcLb = hcLbFactory.newLoadBalancer(origHelper);
    // Make sure all calls into the hcLb is from the syncContext
    hcLbEventDelivery = new LoadBalancer() {

        // Per LoadBalancer API, no more callbacks will be called after shutdown() is called.
        boolean shutdown;

        @Override
        public void handleResolvedAddresses(final ResolvedAddresses resolvedAddresses) {
            syncContext.execute(new Runnable() {

                @Override
                public void run() {
                    if (!shutdown) {
                        hcLb.handleResolvedAddresses(resolvedAddresses);
                    }
                }
            });
        }

        @Override
        public void handleNameResolutionError(Status error) {
            throw new AssertionError("Not supposed to be called");
        }

        @Override
        public void shutdown() {
            syncContext.execute(new Runnable() {

                @Override
                public void run() {
                    if (!shutdown) {
                        shutdown = true;
                        hcLb.shutdown();
                    }
                }
            });
        }
    };
    verify(origLbFactory).newLoadBalancer(any(Helper.class));
}
Also used : Status(io.grpc.Status) ServingStatus(io.grpc.health.v1.HealthCheckResponse.ServingStatus) SubchannelStateListener(io.grpc.LoadBalancer.SubchannelStateListener) Server(io.grpc.Server) LoadBalancer(io.grpc.LoadBalancer) Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Before(org.junit.Before)

Example 14 with LoadBalancer

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

the class HealthCheckingLoadBalancerFactory method newLoadBalancer.

@Override
public LoadBalancer newLoadBalancer(Helper helper) {
    HelperImpl wrappedHelper = new HelperImpl(helper);
    LoadBalancer delegateBalancer = delegateFactory.newLoadBalancer(wrappedHelper);
    return new HealthCheckingLoadBalancer(wrappedHelper, delegateBalancer);
}
Also used : ForwardingLoadBalancer(io.grpc.util.ForwardingLoadBalancer) LoadBalancer(io.grpc.LoadBalancer)

Example 15 with LoadBalancer

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

the class WeightedTargetLoadBalancerTest method handleNameResolutionError.

@Test
public void handleNameResolutionError() {
    ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(null);
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(null);
    // Error before any child balancer created.
    weightedTargetLb.handleNameResolutionError(Status.DATA_LOSS);
    verify(helper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
    PickResult pickResult = pickerCaptor.getValue().pickSubchannel(mock(PickSubchannelArgs.class));
    assertThat(pickResult.getStatus().getCode()).isEqualTo(Status.Code.DATA_LOSS);
    // Child configs updated.
    Map<String, WeightedPolicySelection> targets = ImmutableMap.of(// {foo, 10, config0}
    "target0", weightedLbConfig0, // {bar, 20, config1}
    "target1", weightedLbConfig1, // {bar, 30, config2}
    "target2", weightedLbConfig2, // {foo, 40, config3}
    "target3", weightedLbConfig3);
    weightedTargetLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(new WeightedTargetConfig(targets)).build());
    verify(helper).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
    // Error after child balancers created.
    weightedTargetLb.handleNameResolutionError(Status.ABORTED);
    for (LoadBalancer childBalancer : childBalancers) {
        verify(childBalancer).handleNameResolutionError(statusCaptor.capture());
        assertThat(statusCaptor.getValue().getCode()).isEqualTo(Status.Code.ABORTED);
    }
}
Also used : Status(io.grpc.Status) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PickResult(io.grpc.LoadBalancer.PickResult) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) LoadBalancer(io.grpc.LoadBalancer) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) Test(org.junit.Test)

Aggregations

LoadBalancer (io.grpc.LoadBalancer)27 Test (org.junit.Test)22 Helper (io.grpc.LoadBalancer.Helper)20 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)13 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)5 ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)4 LoadBalancerProvider (io.grpc.LoadBalancerProvider)4 Status (io.grpc.Status)4 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)4 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)4 Attributes (io.grpc.Attributes)2 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 PickResult (io.grpc.LoadBalancer.PickResult)2 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)2 SynchronizationContext (io.grpc.SynchronizationContext)2 GracefulSwitchLoadBalancer (io.grpc.util.GracefulSwitchLoadBalancer)2 ErrorPicker (io.grpc.xds.XdsSubchannelPickers.ErrorPicker)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2