Search in sources :

Example 26 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class WeightedTargetLoadBalancerProviderTest method parseWeightedTargetConfig.

@Test
public void parseWeightedTargetConfig() throws Exception {
    LoadBalancerRegistry lbRegistry = new LoadBalancerRegistry();
    WeightedTargetLoadBalancerProvider weightedTargetLoadBalancerProvider = new WeightedTargetLoadBalancerProvider(lbRegistry);
    final Object fooConfig = new Object();
    LoadBalancerProvider lbProviderFoo = new LoadBalancerProvider() {

        @Override
        public boolean isAvailable() {
            return true;
        }

        @Override
        public int getPriority() {
            return 5;
        }

        @Override
        public String getPolicyName() {
            return "foo_policy";
        }

        @Override
        public LoadBalancer newLoadBalancer(Helper helper) {
            return mock(LoadBalancer.class);
        }

        @Override
        public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
            return ConfigOrError.fromConfig(fooConfig);
        }
    };
    final Object barConfig = new Object();
    LoadBalancerProvider lbProviderBar = new LoadBalancerProvider() {

        @Override
        public boolean isAvailable() {
            return true;
        }

        @Override
        public int getPriority() {
            return 5;
        }

        @Override
        public String getPolicyName() {
            return "bar_policy";
        }

        @Override
        public LoadBalancer newLoadBalancer(Helper helper) {
            return mock(LoadBalancer.class);
        }

        @Override
        public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
            return ConfigOrError.fromConfig(barConfig);
        }
    };
    lbRegistry.register(lbProviderFoo);
    lbRegistry.register(lbProviderBar);
    String weightedTargetConfigJson = ("{" + "  'targets' : {" + "    'target_1' : {" + "      'weight' : 10," + "      'childPolicy' : [" + "        {'unsupported_policy' : {}}," + "        {'foo_policy' : {}}" + "      ]" + "    }," + "    'target_2' : {" + "      'weight' : 20," + "      'childPolicy' : [" + "        {'unsupported_policy' : {}}," + "        {'bar_policy' : {}}" + "      ]" + "    }" + "  }" + "}").replace('\'', '"');
    @SuppressWarnings("unchecked") Map<String, ?> rawLbConfigMap = (Map<String, ?>) JsonParser.parse(weightedTargetConfigJson);
    ConfigOrError parsedConfig = weightedTargetLoadBalancerProvider.parseLoadBalancingPolicyConfig(rawLbConfigMap);
    ConfigOrError expectedConfig = ConfigOrError.fromConfig(new WeightedTargetConfig(ImmutableMap.of("target_1", new WeightedPolicySelection(10, new PolicySelection(lbProviderFoo, fooConfig)), "target_2", new WeightedPolicySelection(20, new PolicySelection(lbProviderBar, barConfig)))));
    assertThat(parsedConfig).isEqualTo(expectedConfig);
}
Also used : WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) LoadBalancerRegistry(io.grpc.LoadBalancerRegistry) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Helper(io.grpc.LoadBalancer.Helper) LoadBalancerProvider(io.grpc.LoadBalancerProvider) ConfigOrError(io.grpc.NameResolver.ConfigOrError) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 27 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method parseLoadBalancerConfig_policyShouldBeIgnoredIfConfigExists.

@Test
public void parseLoadBalancerConfig_policyShouldBeIgnoredIfConfigExists() throws Exception {
    Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [{\"round_robin\": {} } ]," + "\"loadBalancingPolicy\": \"pick_first\" }");
    ConfigOrError parsed = lbf.parseLoadBalancerPolicy(serviceConfig);
    assertThat(parsed).isNotNull();
    assertThat(parsed.getError()).isNull();
    assertThat(parsed.getConfig()).isInstanceOf(PolicySelection.class);
    assertThat(((PolicySelection) parsed.getConfig()).provider.getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
}
Also used : ConfigOrError(io.grpc.NameResolver.ConfigOrError) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Test(org.junit.Test)

Example 28 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method parseLoadBalancerConfig_multipleValidPolicies.

@Test
public void parseLoadBalancerConfig_multipleValidPolicies() throws Exception {
    Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [" + "{\"round_robin\": {}}," + "{\"test_lb\": {} } ] }");
    ConfigOrError parsed = lbf.parseLoadBalancerPolicy(serviceConfig);
    assertThat(parsed).isNotNull();
    assertThat(parsed.getError()).isNull();
    assertThat(parsed.getConfig()).isInstanceOf(PolicySelection.class);
    assertThat(((PolicySelection) parsed.getConfig()).provider.getClass().getName()).isEqualTo("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider");
}
Also used : ConfigOrError(io.grpc.NameResolver.ConfigOrError) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Test(org.junit.Test)

Example 29 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection 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)

Example 30 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method parseLoadBalancerConfig_firstValidSecondInvalidPolicy.

@Test
@SuppressWarnings("unchecked")
public void parseLoadBalancerConfig_firstValidSecondInvalidPolicy() throws Exception {
    when(testLbBalancerProvider.parseLoadBalancingPolicyConfig(any(Map.class))).thenReturn(ConfigOrError.fromError(Status.UNKNOWN));
    Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingConfig\": [" + "{\"round_robin\": {}}," + "{\"test_lb\": {} } ] }");
    ConfigOrError parsed = lbf.parseLoadBalancerPolicy(serviceConfig);
    assertThat(parsed).isNotNull();
    assertThat(parsed.getConfig()).isNotNull();
    assertThat(((PolicySelection) parsed.getConfig()).config).isNotNull();
}
Also used : ConfigOrError(io.grpc.NameResolver.ConfigOrError) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Map(java.util.Map) Test(org.junit.Test)

Aggregations

PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)36 Test (org.junit.Test)27 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)20 LoadBalancerProvider (io.grpc.LoadBalancerProvider)13 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)12 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)12 Helper (io.grpc.LoadBalancer.Helper)10 ConfigOrError (io.grpc.NameResolver.ConfigOrError)10 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)10 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)10 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)9 Subchannel (io.grpc.LoadBalancer.Subchannel)8 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)7 PickResult (io.grpc.LoadBalancer.PickResult)6 LoadBalancer (io.grpc.LoadBalancer)5 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 SocketAddress (java.net.SocketAddress)4 Map (java.util.Map)4 Attributes (io.grpc.Attributes)3