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