use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class ServiceConfigUtil method selectLbPolicyFromList.
/**
* Parses and selects a load balancing policy from a non-empty list of raw configs. If selection
* is successful, the returned ConfigOrError object will include a {@link
* ServiceConfigUtil.PolicySelection} as its config value.
*/
public static ConfigOrError selectLbPolicyFromList(List<LbConfig> lbConfigs, LoadBalancerRegistry lbRegistry) {
List<String> policiesTried = new ArrayList<>();
for (LbConfig lbConfig : lbConfigs) {
String policy = lbConfig.getPolicyName();
LoadBalancerProvider provider = lbRegistry.getProvider(policy);
if (provider == null) {
policiesTried.add(policy);
} else {
if (!policiesTried.isEmpty()) {
Logger.getLogger(ServiceConfigUtil.class.getName()).log(Level.FINEST, "{0} specified by Service Config are not available", policiesTried);
}
ConfigOrError parsedLbPolicyConfig = provider.parseLoadBalancingPolicyConfig(lbConfig.getRawConfigValue());
if (parsedLbPolicyConfig.getError() != null) {
return parsedLbPolicyConfig;
}
return ConfigOrError.fromConfig(new PolicySelection(provider, parsedLbPolicyConfig.getConfig()));
}
}
return ConfigOrError.fromError(Status.UNKNOWN.withDescription("None of " + policiesTried + " specified by Service Config are available."));
}
use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class ScParser method parseServiceConfig.
@Override
public ConfigOrError parseServiceConfig(Map<String, ?> rawServiceConfig) {
try {
Object loadBalancingPolicySelection;
ConfigOrError choiceFromLoadBalancer = autoLoadBalancerFactory.parseLoadBalancerPolicy(rawServiceConfig);
if (choiceFromLoadBalancer == null) {
loadBalancingPolicySelection = null;
} else if (choiceFromLoadBalancer.getError() != null) {
return ConfigOrError.fromError(choiceFromLoadBalancer.getError());
} else {
loadBalancingPolicySelection = choiceFromLoadBalancer.getConfig();
}
return ConfigOrError.fromConfig(ManagedChannelServiceConfig.fromServiceConfig(rawServiceConfig, retryEnabled, maxRetryAttemptsLimit, maxHedgedAttemptsLimit, loadBalancingPolicySelection));
} catch (RuntimeException e) {
return ConfigOrError.fromError(Status.UNKNOWN.withDescription("failed to parse service config").withCause(e));
}
}
use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class GrpclbLoadBalancerProviderTest method retrieveModeFromLbConfig_pickFirst.
@Test
public void retrieveModeFromLbConfig_pickFirst() throws Exception {
String lbConfig = "{\"childPolicy\" : [{\"pick_first\" : {}}, {\"round_robin\" : {}}]}";
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_unsupportedChildPolicy.
@Test
public void retrieveModeFromLbConfig_unsupportedChildPolicy() throws Exception {
String lbConfig = "{\"childPolicy\" : [ {\"nonono\" : {}} ]}";
ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(parseJsonObject(lbConfig));
assertThat(configOrError.getError()).isNotNull();
assertThat(configOrError.getError().getDescription()).contains("None of [nonono] specified child policies are available.");
}
use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class GrpclbLoadBalancerProviderTest method retrieveModeFromLbConfig_emptyConfigUseRoundRobin.
@Test
public void retrieveModeFromLbConfig_emptyConfigUseRoundRobin() throws Exception {
String lbConfig = "{}";
ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(parseJsonObject(lbConfig));
assertThat(configOrError.getConfig()).isNotNull();
GrpclbConfig config = (GrpclbConfig) configOrError.getConfig();
assertThat(config.getMode()).isEqualTo(Mode.ROUND_ROBIN);
assertThat(config.getServiceName()).isNull();
}
Aggregations