use of io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig in project grpc-java by grpc.
the class ClusterManagerLoadBalancerTest method buildConfig.
private ClusterManagerConfig buildConfig(Map<String, String> childPolicies) {
Map<String, PolicySelection> childPolicySelections = new LinkedHashMap<>();
for (String name : childPolicies.keySet()) {
String childPolicyName = childPolicies.get(name);
Object childConfig = lbConfigInventory.get(name);
PolicySelection policy = new PolicySelection(new FakeLoadBalancerProvider(childPolicyName), childConfig);
childPolicySelections.put(name, policy);
}
return new ClusterManagerConfig(childPolicySelections);
}
use of io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig 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));
}
use of io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig 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();
}
Aggregations