Search in sources :

Example 46 with Helper

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_noLbPolicySelected_defaultToPickFirst.

@Test
public void handleResolvedAddressGroups_noLbPolicySelected_defaultToPickFirst() {
    final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper() {

        @Override
        public Subchannel createSubchannel(CreateSubchannelArgs args) {
            assertThat(args.getAddresses()).isEqualTo(servers);
            return new TestSubchannel(args);
        }
    };
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(null).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isInstanceOf(PickFirstLoadBalancer.class);
}
Also used : ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 47 with Helper

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_propagateLbConfigToDelegate.

@Test
@SuppressWarnings("unchecked")
public void handleResolvedAddressGroups_propagateLbConfigToDelegate() throws Exception {
    Map<String, ?> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { \"setting1\": \"high\" } } ] }");
    ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
    assertThat(lbConfigs.getConfig()).isNotNull();
    final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper();
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    verify(testLbBalancerProvider).newLoadBalancer(same(helper));
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
    ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(testLbBalancer).handleResolvedAddresses(resultCaptor.capture());
    assertThat(resultCaptor.getValue().getAddresses()).containsExactlyElementsIn(servers).inOrder();
    verify(testLbBalancer, atLeast(0)).canHandleEmptyAddressListFromNameResolution();
    ArgumentCaptor<Map<String, ?>> lbConfigCaptor = ArgumentCaptor.forClass(Map.class);
    verify(testLbBalancerProvider).parseLoadBalancingPolicyConfig(lbConfigCaptor.capture());
    assertThat(lbConfigCaptor.getValue()).containsExactly("setting1", "high");
    verifyNoMoreInteractions(testLbBalancer);
    rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { \"setting1\": \"low\" } } ] }");
    lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
    handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(testLbBalancer, times(2)).handleResolvedAddresses(resultCaptor.capture());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(resultCaptor.getValue().getAddresses()).containsExactlyElementsIn(servers).inOrder();
    verify(testLbBalancerProvider, times(2)).parseLoadBalancingPolicyConfig(lbConfigCaptor.capture());
    assertThat(lbConfigCaptor.getValue()).containsExactly("setting1", "low");
    // Service config didn't change policy, thus the delegateLb is not swapped
    verifyNoMoreInteractions(testLbBalancer);
    verify(testLbBalancerProvider).newLoadBalancer(any(Helper.class));
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ConfigOrError(io.grpc.NameResolver.ConfigOrError) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Map(java.util.Map) Test(org.junit.Test)

Example 48 with Helper

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_keepOldBalancer.

@Test
public void handleResolvedAddressGroups_keepOldBalancer() {
    final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper() {

        @Override
        public Subchannel createSubchannel(CreateSubchannelArgs args) {
            assertThat(args.getAddresses()).isEqualTo(servers);
            return new TestSubchannel(args);
        }
    };
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    LoadBalancer oldDelegate = lb.getDelegate();
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(Attributes.EMPTY).setLoadBalancingPolicyConfig(null).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isSameInstanceAs(oldDelegate);
}
Also used : ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) LoadBalancer(io.grpc.LoadBalancer) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 49 with Helper

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_propagateAddrsToDelegate.

@Test
public void handleResolvedAddressGroups_propagateAddrsToDelegate() throws Exception {
    Map<String, ?> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb\": { \"setting1\": \"high\" } } ] }");
    ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
    assertThat(lbConfigs.getConfig()).isNotNull();
    Helper helper = new TestHelper();
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new InetSocketAddress(8080) {
    }));
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    verify(testLbBalancerProvider).newLoadBalancer(same(helper));
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer);
    ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(testLbBalancer).handleResolvedAddresses(resultCaptor.capture());
    assertThat(resultCaptor.getValue().getAddresses()).containsExactlyElementsIn(servers).inOrder();
    servers = Collections.singletonList(new EquivalentAddressGroup(new InetSocketAddress(9090) {
    }));
    handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    verify(testLbBalancer, times(2)).handleResolvedAddresses(resultCaptor.capture());
    assertThat(resultCaptor.getValue().getAddresses()).containsExactlyElementsIn(servers).inOrder();
}
Also used : ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) InetSocketAddress(java.net.InetSocketAddress) ConfigOrError(io.grpc.NameResolver.ConfigOrError) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 50 with Helper

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_useSelectedLbPolicy.

@Test
public void handleResolvedAddressGroups_useSelectedLbPolicy() throws Exception {
    Map<String, ?> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [{\"round_robin\": {}}]}");
    ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
    assertThat(lbConfigs.getConfig()).isNotNull();
    assertThat(((PolicySelection) lbConfigs.getConfig()).provider.getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
    final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper() {

        @Override
        public Subchannel createSubchannel(CreateSubchannelArgs args) {
            assertThat(args.getAddresses()).isEqualTo(servers);
            return new TestSubchannel(args);
        }
    };
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate().getClass().getName()).isEqualTo("io.grpc.util.RoundRobinLoadBalancer");
}
Also used : AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ConfigOrError(io.grpc.NameResolver.ConfigOrError) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Aggregations

Helper (io.grpc.LoadBalancer.Helper)59 Test (org.junit.Test)57 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)28 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)21 LoadBalancer (io.grpc.LoadBalancer)20 MockClientTransportInfo (io.grpc.internal.TestUtils.MockClientTransportInfo)20 Subchannel (io.grpc.LoadBalancer.Subchannel)18 Metadata (io.grpc.Metadata)16 Status (io.grpc.Status)14 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)13 SocketAddress (java.net.SocketAddress)13 ForwardingSubchannel (io.grpc.util.ForwardingSubchannel)12 UnsupportedClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder)10 ConfigOrError (io.grpc.NameResolver.ConfigOrError)9 AutoConfiguredLoadBalancer (io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer)9 ClientTransportFactoryBuilder (io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder)9 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)9 ForwardingLoadBalancerHelper (io.grpc.util.ForwardingLoadBalancerHelper)9 ClientStreamTracer (io.grpc.ClientStreamTracer)7 ManagedChannel (io.grpc.ManagedChannel)7