use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.
the class ClusterResolverLoadBalancerProviderTest method providesLoadBalancer.
@Test
public void providesLoadBalancer() {
Helper helper = mock(Helper.class);
SynchronizationContext syncContext = new SynchronizationContext(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
throw new AssertionError(e);
}
});
FakeClock fakeClock = new FakeClock();
NameResolverRegistry nsRegistry = new NameResolverRegistry();
NameResolver.Args args = NameResolver.Args.newBuilder().setDefaultPort(8080).setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR).setSynchronizationContext(syncContext).setServiceConfigParser(mock(ServiceConfigParser.class)).setChannelLogger(mock(ChannelLogger.class)).build();
when(helper.getNameResolverRegistry()).thenReturn(nsRegistry);
when(helper.getNameResolverArgs()).thenReturn(args);
when(helper.getSynchronizationContext()).thenReturn(syncContext);
when(helper.getScheduledExecutorService()).thenReturn(fakeClock.getScheduledExecutorService());
when(helper.getAuthority()).thenReturn("api.google.com");
LoadBalancerProvider provider = new ClusterResolverLoadBalancerProvider();
LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
assertThat(loadBalancer).isInstanceOf(ClusterResolverLoadBalancer.class);
}
use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.
the class CdsLoadBalancerProviderTest method provided.
@Test
public void provided() {
LoadBalancerProvider provider = LoadBalancerRegistry.getDefaultRegistry().getProvider(XdsLbPolicies.CDS_POLICY_NAME);
assertThat(provider).isInstanceOf(CdsLoadBalancerProvider.class);
}
use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.
the class CdsLoadBalancerProviderTest method providesLoadBalancer.
@Test
public void providesLoadBalancer() {
Helper helper = mock(Helper.class);
SynchronizationContext syncContext = new SynchronizationContext(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
throw new AssertionError(e);
}
});
when(helper.getSynchronizationContext()).thenReturn(syncContext);
LoadBalancerProvider provider = new CdsLoadBalancerProvider();
LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
assertThat(loadBalancer).isInstanceOf(CdsLoadBalancer2.class);
}
use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.
the class WeightedTargetLoadBalancerProviderTest method parseWeightedTargetConfig.
@Test
public void parseWeightedTargetConfig() throws Exception {
LoadBalancerRegistry lbRegistry = new LoadBalancerRegistry();
WeightedTargetLoadBalancerProvider weightedTargetLoadBalancerProvider = new WeightedTargetLoadBalancerProvider(lbRegistry);
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) {
return mock(LoadBalancer.class);
}
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
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) {
return mock(LoadBalancer.class);
}
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
return ConfigOrError.fromConfig(barConfig);
}
};
lbRegistry.register(lbProviderFoo);
lbRegistry.register(lbProviderBar);
String weightedTargetConfigJson = ("{" + " 'targets' : {" + " 'target_1' : {" + " 'weight' : 10," + " 'childPolicy' : [" + " {'unsupported_policy' : {}}," + " {'foo_policy' : {}}" + " ]" + " }," + " 'target_2' : {" + " 'weight' : 20," + " 'childPolicy' : [" + " {'unsupported_policy' : {}}," + " {'bar_policy' : {}}" + " ]" + " }" + " }" + "}").replace('\'', '"');
@SuppressWarnings("unchecked") Map<String, ?> rawLbConfigMap = (Map<String, ?>) JsonParser.parse(weightedTargetConfigJson);
ConfigOrError parsedConfig = weightedTargetLoadBalancerProvider.parseLoadBalancingPolicyConfig(rawLbConfigMap);
ConfigOrError expectedConfig = ConfigOrError.fromConfig(new WeightedTargetConfig(ImmutableMap.of("target_1", new WeightedPolicySelection(10, new PolicySelection(lbProviderFoo, fooConfig)), "target_2", new WeightedPolicySelection(20, new PolicySelection(lbProviderBar, barConfig)))));
assertThat(parsedConfig).isEqualTo(expectedConfig);
}
use of io.grpc.LoadBalancerProvider 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