use of io.grpc.internal.ServiceConfigUtil.PolicySelection 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.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class ClusterManagerLoadBalancer method handleResolvedAddresses.
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
ClusterManagerConfig config = (ClusterManagerConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
Map<String, PolicySelection> newChildPolicies = config.childPolicies;
logger.log(XdsLogLevel.INFO, "Received cluster_manager lb config: child names={0}", newChildPolicies.keySet());
for (Map.Entry<String, PolicySelection> entry : newChildPolicies.entrySet()) {
final String name = entry.getKey();
LoadBalancerProvider childPolicyProvider = entry.getValue().getProvider();
Object childConfig = entry.getValue().getConfig();
if (!childLbStates.containsKey(name)) {
childLbStates.put(name, new ChildLbState(name, childPolicyProvider));
} else {
childLbStates.get(name).reactivate(childPolicyProvider);
}
LoadBalancer childLb = childLbStates.get(name).lb;
ResolvedAddresses childAddresses = resolvedAddresses.toBuilder().setLoadBalancingPolicyConfig(childConfig).build();
childLb.handleResolvedAddresses(childAddresses);
}
for (String name : childLbStates.keySet()) {
if (!newChildPolicies.containsKey(name)) {
childLbStates.get(name).deactivate();
}
}
// Must update channel picker before return so that new RPCs will not be routed to deleted
// clusters and resolver can remove them in service config.
updateOverallBalancingState();
}
use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class PriorityLoadBalancerProviderTest method priorityLbConfig_missingChildConfig.
@SuppressWarnings("ExpectedExceptionChecker")
@Test
public void priorityLbConfig_missingChildConfig() {
Map<String, PriorityChildConfig> childConfigs = ImmutableMap.of("p1", new PriorityChildConfig(new PolicySelection(mock(LoadBalancerProvider.class), null), true));
List<String> priorities = ImmutableList.of("p0", "p1");
thrown.expect(IllegalArgumentException.class);
new PriorityLbConfig(childConfigs, priorities);
}
use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class ClusterImplLoadBalancerTest method handleResolvedAddresses_propagateToChildPolicy.
@Test
public void handleResolvedAddresses_propagateToChildPolicy() {
FakeLoadBalancerProvider weightedTargetProvider = new FakeLoadBalancerProvider(XdsLbPolicies.WEIGHTED_TARGET_POLICY_NAME);
Object weightedTargetConfig = new Object();
ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
EquivalentAddressGroup endpoint = makeAddress("endpoint-addr", locality);
deliverAddressesAndConfig(Collections.singletonList(endpoint), config);
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(downstreamBalancers);
assertThat(Iterables.getOnlyElement(childBalancer.addresses)).isEqualTo(endpoint);
assertThat(childBalancer.config).isSameInstanceAs(weightedTargetConfig);
assertThat(childBalancer.attributes.get(InternalXdsAttributes.XDS_CLIENT_POOL)).isSameInstanceAs(xdsClientPool);
}
use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.
the class ClusterImplLoadBalancerTest method endpointAddressesAttachedWithClusterName.
@Test
public void endpointAddressesAttachedWithClusterName() {
LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
// One locality with two endpoints.
EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr1", locality);
EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr2", locality);
deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
// one leaf balancer
assertThat(downstreamBalancers).hasSize(1);
FakeLoadBalancer leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
assertThat(leafBalancer.name).isEqualTo("round_robin");
// Simulates leaf load balancer creating subchannels.
CreateSubchannelArgs args = CreateSubchannelArgs.newBuilder().setAddresses(leafBalancer.addresses).build();
Subchannel subchannel = leafBalancer.helper.createSubchannel(args);
for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
assertThat(eag.getAttributes().get(InternalXdsAttributes.ATTR_CLUSTER_NAME)).isEqualTo(CLUSTER);
}
}
Aggregations