use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.
the class ClusterResolverLoadBalancerProviderTest method provided.
@Test
public void provided() {
LoadBalancerProvider provider = LoadBalancerRegistry.getDefaultRegistry().getProvider(XdsLbPolicies.CLUSTER_RESOLVER_POLICY_NAME);
assertThat(provider).isInstanceOf(ClusterResolverLoadBalancerProvider.class);
}
use of io.grpc.LoadBalancerProvider 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.LoadBalancerProvider in project grpc-java by grpc.
the class ClusterResolverLoadBalancer method generateEdsBasedPriorityChildConfigs.
/**
* Generates configs to be used in the priority LB policy for priorities in an EDS cluster.
*
* <p>priority LB -> cluster_impl LB (one per priority) -> (weighted_target LB
* -> round_robin / least_request_experimental (one per locality)) / ring_hash_experimental
*/
private static Map<String, PriorityChildConfig> generateEdsBasedPriorityChildConfigs(String cluster, @Nullable String edsServiceName, @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext, PolicySelection endpointLbPolicy, LoadBalancerRegistry lbRegistry, Map<String, Map<Locality, Integer>> prioritizedLocalityWeights, List<DropOverload> dropOverloads) {
Map<String, PriorityChildConfig> configs = new HashMap<>();
for (String priority : prioritizedLocalityWeights.keySet()) {
PolicySelection leafPolicy = endpointLbPolicy;
// product of each endpoint's weight and the weight of the locality it belongs to.
if (endpointLbPolicy.getProvider().getPolicyName().equals("round_robin") || endpointLbPolicy.getProvider().getPolicyName().equals("least_request_experimental")) {
Map<Locality, Integer> localityWeights = prioritizedLocalityWeights.get(priority);
Map<String, WeightedPolicySelection> targets = new HashMap<>();
for (Locality locality : localityWeights.keySet()) {
int weight = localityWeights.get(locality);
WeightedPolicySelection target = new WeightedPolicySelection(weight, endpointLbPolicy);
targets.put(localityName(locality), target);
}
LoadBalancerProvider weightedTargetLbProvider = lbRegistry.getProvider(WEIGHTED_TARGET_POLICY_NAME);
WeightedTargetConfig weightedTargetConfig = new WeightedTargetConfig(Collections.unmodifiableMap(targets));
leafPolicy = new PolicySelection(weightedTargetLbProvider, weightedTargetConfig);
}
ClusterImplConfig clusterImplConfig = new ClusterImplConfig(cluster, edsServiceName, lrsServerInfo, maxConcurrentRequests, dropOverloads, leafPolicy, tlsContext);
LoadBalancerProvider clusterImplLbProvider = lbRegistry.getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
PolicySelection clusterImplPolicy = new PolicySelection(clusterImplLbProvider, clusterImplConfig);
PriorityChildConfig priorityChildConfig = new PriorityChildConfig(clusterImplPolicy, true);
configs.put(priority, priorityChildConfig);
}
return configs;
}
use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.
the class ClusterResolverLoadBalancer method generateDnsBasedPriorityChildConfig.
/**
* Generates the config to be used in the priority LB policy for the single priority of
* logical DNS cluster.
*
* <p>priority LB -> cluster_impl LB (single hardcoded priority) -> pick_first
*/
private static PriorityChildConfig generateDnsBasedPriorityChildConfig(String cluster, @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext, LoadBalancerRegistry lbRegistry, List<DropOverload> dropOverloads) {
// Override endpoint-level LB policy with pick_first for logical DNS cluster.
PolicySelection endpointLbPolicy = new PolicySelection(lbRegistry.getProvider("pick_first"), null);
ClusterImplConfig clusterImplConfig = new ClusterImplConfig(cluster, null, lrsServerInfo, maxConcurrentRequests, dropOverloads, endpointLbPolicy, tlsContext);
LoadBalancerProvider clusterImplLbProvider = lbRegistry.getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
PolicySelection clusterImplPolicy = new PolicySelection(clusterImplLbProvider, clusterImplConfig);
return new PriorityChildConfig(clusterImplPolicy, false);
}
use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.
the class ClusterManagerLoadBalancerProviderTest method parseLoadBalancingConfig_valid.
@Test
public void parseLoadBalancingConfig_valid() throws IOException {
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) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig) {
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) {
throw new UnsupportedOperationException("Should not be called");
}
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig) {
return ConfigOrError.fromConfig(barConfig);
}
};
lbRegistry.register(lbProviderFoo);
lbRegistry.register(lbProviderBar);
String clusterManagerConfigJson = "{\n" + " \"childPolicy\": {\n" + " \"child1\": {\n" + " \"lbPolicy\": [\n" + " {\n" + " \"foo_policy\": {" + " \"config_name\": \"config_value\"\n" + " }\n" + " }\n" + " ]\n" + " },\n" + " \"child2\": {\n" + " \"lbPolicy\": [\n" + " {\n" + " \"bar_policy\": {}\n" + " }, {\n" + " \"unsupported\": {}\n" + " }\n" + " ]\n" + " }\n" + " }\n" + "}";
@SuppressWarnings("unchecked") Map<String, ?> rawLbConfigMap = (Map<String, ?>) JsonParser.parse(clusterManagerConfigJson);
ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(rawLbConfigMap);
assertThat(configOrError.getConfig()).isNotNull();
ClusterManagerConfig config = (ClusterManagerConfig) configOrError.getConfig();
assertThat(config.childPolicies).containsExactly("child1", new PolicySelection(lbProviderFoo, fooConfig), "child2", new PolicySelection(lbProviderBar, barConfig));
}
Aggregations