Search in sources :

Example 1 with LbConfig

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

the class ServiceConfigUtilTest method unwrapLoadBalancingConfig.

@Test
public void unwrapLoadBalancingConfig() throws Exception {
    String lbConfig = "{\"xds_experimental\" : { " + "\"childPolicy\" : [{\"round_robin\" : {}}, {\"lbPolicy2\" : {\"key\" : \"val\"}}]" + "}}";
    LbConfig config = ServiceConfigUtil.unwrapLoadBalancingConfig(checkObject(JsonParser.parse(lbConfig)));
    assertThat(config.getPolicyName()).isEqualTo("xds_experimental");
    assertThat(config.getRawConfigValue()).isEqualTo(JsonParser.parse("{\"childPolicy\" : [{\"round_robin\" : {}}, {\"lbPolicy2\" : {\"key\" : \"val\"}}]" + "}"));
}
Also used : LbConfig(io.grpc.internal.ServiceConfigUtil.LbConfig) Test(org.junit.Test)

Example 2 with LbConfig

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

the class GrpclbLoadBalancerProvider method parseLoadBalancingConfigPolicyInternal.

ConfigOrError parseLoadBalancingConfigPolicyInternal(Map<String, ?> rawLoadBalancingPolicyConfig) {
    if (rawLoadBalancingPolicyConfig == null) {
        return ConfigOrError.fromConfig(GrpclbConfig.create(DEFAULT_MODE));
    }
    String serviceName = JsonUtil.getString(rawLoadBalancingPolicyConfig, "serviceName");
    List<?> rawChildPolicies = JsonUtil.getList(rawLoadBalancingPolicyConfig, "childPolicy");
    List<LbConfig> childPolicies = null;
    if (rawChildPolicies != null) {
        childPolicies = ServiceConfigUtil.unwrapLoadBalancingConfigList(JsonUtil.checkObjectList(rawChildPolicies));
    }
    if (childPolicies == null || childPolicies.isEmpty()) {
        return ConfigOrError.fromConfig(GrpclbConfig.create(DEFAULT_MODE, serviceName));
    }
    List<String> policiesTried = new ArrayList<>();
    for (LbConfig childPolicy : childPolicies) {
        String childPolicyName = childPolicy.getPolicyName();
        switch(childPolicyName) {
            case "round_robin":
                return ConfigOrError.fromConfig(GrpclbConfig.create(Mode.ROUND_ROBIN, serviceName));
            case "pick_first":
                return ConfigOrError.fromConfig(GrpclbConfig.create(Mode.PICK_FIRST, serviceName));
            default:
                policiesTried.add(childPolicyName);
        }
    }
    return ConfigOrError.fromError(Status.INVALID_ARGUMENT.withDescription("None of " + policiesTried + " specified child policies are available."));
}
Also used : LbConfig(io.grpc.internal.ServiceConfigUtil.LbConfig) ArrayList(java.util.ArrayList)

Example 3 with LbConfig

use of io.grpc.internal.ServiceConfigUtil.LbConfig 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 4 with LbConfig

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

Aggregations

LbConfig (io.grpc.internal.ServiceConfigUtil.LbConfig)4 LoadBalancerRegistry (io.grpc.LoadBalancerRegistry)2 ConfigOrError (io.grpc.NameResolver.ConfigOrError)2 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)2 LinkedHashMap (java.util.LinkedHashMap)2 Status (io.grpc.Status)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1