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));
}
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));
}
}
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);
}
Aggregations