Search in sources :

Example 1 with LoadBalancerRegistry

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

the class ClusterManagerLoadBalancerProvider method parseLoadBalancingPolicyConfig.

@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
    Map<String, PolicySelection> parsedChildPolicies = new LinkedHashMap<>();
    try {
        Map<String, ?> childPolicies = JsonUtil.getObject(rawConfig, "childPolicy");
        if (childPolicies == null || childPolicies.isEmpty()) {
            return ConfigOrError.fromError(Status.INTERNAL.withDescription("No child policy provided for cluster_manager LB policy: " + rawConfig));
        }
        for (String name : childPolicies.keySet()) {
            Map<String, ?> childPolicy = JsonUtil.getObject(childPolicies, name);
            if (childPolicy == null) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config for child " + name + " in cluster_manager LB policy: " + rawConfig));
            }
            List<LbConfig> childConfigCandidates = ServiceConfigUtil.unwrapLoadBalancingConfigList(JsonUtil.getListOfObjects(childPolicy, "lbPolicy"));
            if (childConfigCandidates == null || childConfigCandidates.isEmpty()) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config specified for child " + name + " in cluster_manager Lb policy: " + rawConfig));
            }
            LoadBalancerRegistry registry = lbRegistry != null ? lbRegistry : LoadBalancerRegistry.getDefaultRegistry();
            ConfigOrError selectedConfig = ServiceConfigUtil.selectLbPolicyFromList(childConfigCandidates, registry);
            if (selectedConfig.getError() != null) {
                Status error = selectedConfig.getError();
                return ConfigOrError.fromError(Status.INTERNAL.withCause(error.getCause()).withDescription(error.getDescription()).augmentDescription("Failed to select config for child " + name));
            }
            parsedChildPolicies.put(name, (PolicySelection) selectedConfig.getConfig());
        }
    } catch (RuntimeException e) {
        return ConfigOrError.fromError(Status.fromThrowable(e).withDescription("Failed to parse cluster_manager LB config: " + rawConfig));
    }
    return ConfigOrError.fromConfig(new ClusterManagerConfig(parsedChildPolicies));
}
Also used : Status(io.grpc.Status) LbConfig(io.grpc.internal.ServiceConfigUtil.LbConfig) ConfigOrError(io.grpc.NameResolver.ConfigOrError) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LoadBalancerRegistry(io.grpc.LoadBalancerRegistry) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with LoadBalancerRegistry

use of io.grpc.LoadBalancerRegistry 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 3 with LoadBalancerRegistry

use of io.grpc.LoadBalancerRegistry 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)

Aggregations

LoadBalancerRegistry (io.grpc.LoadBalancerRegistry)3 ConfigOrError (io.grpc.NameResolver.ConfigOrError)3 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)3 LbConfig (io.grpc.internal.ServiceConfigUtil.LbConfig)2 LinkedHashMap (java.util.LinkedHashMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Helper (io.grpc.LoadBalancer.Helper)1 LoadBalancerProvider (io.grpc.LoadBalancerProvider)1 Status (io.grpc.Status)1 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)1 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)1 Map (java.util.Map)1 Test (org.junit.Test)1