Search in sources :

Example 46 with ConfigOrError

use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_delegateAcceptsEmptyAddressList.

@Test
public void handleResolvedAddressGroups_delegateAcceptsEmptyAddressList() throws Exception {
    Helper helper = new TestHelper();
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    Map<String, ?> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"test_lb2\": { \"setting1\": \"high\" } } ] }");
    ConfigOrError lbConfigs = lbf.parseLoadBalancerPolicy(rawServiceConfig);
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(Collections.<EquivalentAddressGroup>emptyList()).setLoadBalancingPolicyConfig(lbConfigs.getConfig()).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isSameInstanceAs(testLbBalancer2);
    assertThat(testLbBalancer2.canHandleEmptyAddressListFromNameResolution()).isTrue();
    ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(testLbBalancer2).handleResolvedAddresses(resultCaptor.capture());
    assertThat(resultCaptor.getValue().getAddresses()).isEmpty();
    assertThat(resultCaptor.getValue().getLoadBalancingPolicyConfig()).isEqualTo(nextParsedConfigOrError2.get().getConfig());
}
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) ConfigOrError(io.grpc.NameResolver.ConfigOrError) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 47 with ConfigOrError

use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.

the class AutoConfiguredLoadBalancerFactoryTest method parseLoadBalancerPolicy_failedOnUnknown.

@Test
public void parseLoadBalancerPolicy_failedOnUnknown() throws Exception {
    Map<String, ?> serviceConfig = parseConfig("{\"loadBalancingPolicy\": \"magic_balancer\"}");
    ConfigOrError parsed = lbf.parseLoadBalancerPolicy(serviceConfig);
    assertThat(parsed.getError()).isNotNull();
    assertThat(parsed.getError().getDescription()).isEqualTo("None of [magic_balancer] specified by Service Config are available.");
}
Also used : ConfigOrError(io.grpc.NameResolver.ConfigOrError) Test(org.junit.Test)

Example 48 with ConfigOrError

use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.

the class WeightedTargetLoadBalancerProvider method parseLoadBalancingPolicyConfig.

@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
    try {
        Map<String, ?> targets = JsonUtil.getObject(rawConfig, "targets");
        if (targets == null || targets.isEmpty()) {
            return ConfigOrError.fromError(Status.INTERNAL.withDescription("No targets provided for weighted_target LB policy:\n " + rawConfig));
        }
        Map<String, WeightedPolicySelection> parsedChildConfigs = new LinkedHashMap<>();
        for (String name : targets.keySet()) {
            Map<String, ?> rawWeightedTarget = JsonUtil.getObject(targets, name);
            if (rawWeightedTarget == null || rawWeightedTarget.isEmpty()) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config for target " + name + " in weighted_target LB policy:\n " + rawConfig));
            }
            Integer weight = JsonUtil.getNumberAsInteger(rawWeightedTarget, "weight");
            if (weight == null || weight < 1) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("Wrong weight for target " + name + " in weighted_target LB policy:\n " + rawConfig));
            }
            List<LbConfig> childConfigCandidates = ServiceConfigUtil.unwrapLoadBalancingConfigList(JsonUtil.getListOfObjects(rawWeightedTarget, "childPolicy"));
            if (childConfigCandidates == null || childConfigCandidates.isEmpty()) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No child policy for target " + name + " in weighted_target LB policy:\n " + rawConfig));
            }
            LoadBalancerRegistry lbRegistry = this.lbRegistry == null ? LoadBalancerRegistry.getDefaultRegistry() : this.lbRegistry;
            ConfigOrError selectedConfig = ServiceConfigUtil.selectLbPolicyFromList(childConfigCandidates, lbRegistry);
            if (selectedConfig.getError() != null) {
                return selectedConfig;
            }
            PolicySelection policySelection = (PolicySelection) selectedConfig.getConfig();
            parsedChildConfigs.put(name, new WeightedPolicySelection(weight, policySelection));
        }
        return ConfigOrError.fromConfig(new WeightedTargetConfig(parsedChildConfigs));
    } catch (RuntimeException e) {
        return ConfigOrError.fromError(Status.fromThrowable(e).withDescription("Failed to parse weighted_target LB config: " + rawConfig));
    }
}
Also used : LoadBalancerRegistry(io.grpc.LoadBalancerRegistry) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LinkedHashMap(java.util.LinkedHashMap) LbConfig(io.grpc.internal.ServiceConfigUtil.LbConfig) ConfigOrError(io.grpc.NameResolver.ConfigOrError)

Example 49 with ConfigOrError

use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.

the class GrpclbLoadBalancerProviderTest method retrieveModeFromLbConfig_skipUnsupportedChildPolicy.

@Test
public void retrieveModeFromLbConfig_skipUnsupportedChildPolicy() throws Exception {
    String lbConfig = "{\"childPolicy\" : [ {\"nono\" : {}}, {\"pick_first\" : {} } ]}";
    ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(parseJsonObject(lbConfig));
    assertThat(configOrError.getConfig()).isNotNull();
    GrpclbConfig config = (GrpclbConfig) configOrError.getConfig();
    assertThat(config.getMode()).isEqualTo(Mode.PICK_FIRST);
    assertThat(config.getServiceName()).isNull();
}
Also used : ConfigOrError(io.grpc.NameResolver.ConfigOrError) Test(org.junit.Test)

Example 50 with ConfigOrError

use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.

the class GrpclbLoadBalancerProviderTest method retrieveModeFromLbConfig_nullConfigUseRoundRobin.

@Test
public void retrieveModeFromLbConfig_nullConfigUseRoundRobin() throws Exception {
    ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(null);
    assertThat(configOrError.getConfig()).isNotNull();
    GrpclbConfig config = (GrpclbConfig) configOrError.getConfig();
    assertThat(config.getMode()).isEqualTo(Mode.ROUND_ROBIN);
    assertThat(config.getServiceName()).isNull();
}
Also used : ConfigOrError(io.grpc.NameResolver.ConfigOrError) Test(org.junit.Test)

Aggregations

ConfigOrError (io.grpc.NameResolver.ConfigOrError)57 Test (org.junit.Test)52 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)10 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)9 Helper (io.grpc.LoadBalancer.Helper)9 Status (io.grpc.Status)9 AutoConfiguredLoadBalancer (io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer)7 ForwardingLoadBalancerHelper (io.grpc.util.ForwardingLoadBalancerHelper)7 Map (java.util.Map)7 InetSocketAddress (java.net.InetSocketAddress)6 SocketAddress (java.net.SocketAddress)4 Random (java.util.Random)4 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)3 ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)3 LoadBalancerProvider (io.grpc.LoadBalancerProvider)3 LoadBalancerRegistry (io.grpc.LoadBalancerRegistry)3 LeastRequestConfig (io.grpc.xds.LeastRequestLoadBalancer.LeastRequestConfig)3 LbConfig (io.grpc.internal.ServiceConfigUtil.LbConfig)2 RingHashConfig (io.grpc.xds.RingHashLoadBalancer.RingHashConfig)2 LinkedHashMap (java.util.LinkedHashMap)2