use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class GrpclbLoadBalancerProviderTest method retrieveModeFromLbConfig_skipUnsupportedChildPolicyWithTarget.
@Test
public void retrieveModeFromLbConfig_skipUnsupportedChildPolicyWithTarget() throws Exception {
String lbConfig = "{\"childPolicy\" : [ {\"nono\" : {}}, {\"pick_first\" : {}} ]," + "\"serviceName\": \"foo.google.com\"}";
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()).isEqualTo("foo.google.com");
}
use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class GrpclbNameResolverTest method resolve_presentResourceResolver.
@Test
public void resolve_presentResourceResolver() throws Exception {
InetAddress backendAddr = InetAddress.getByAddress(new byte[] { 127, 0, 0, 0 });
InetAddress lbAddr = InetAddress.getByAddress(new byte[] { 10, 1, 0, 0 });
int lbPort = 8080;
// original name in SRV record
String lbName = "foo.example.com.";
SrvRecord srvRecord = new SrvRecord(lbName, 8080);
AddressResolver mockAddressResolver = mock(AddressResolver.class);
when(mockAddressResolver.resolveAddress(hostName)).thenReturn(Collections.singletonList(backendAddr));
when(mockAddressResolver.resolveAddress(lbName)).thenReturn(Collections.singletonList(lbAddr));
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
when(mockResourceResolver.resolveTxt(anyString())).thenReturn(Collections.singletonList("grpc_config=[{\"clientLanguage\": [\"java\"], \"serviceConfig\": {}}]"));
when(mockResourceResolver.resolveSrv(anyString())).thenReturn(Collections.singletonList(srvRecord));
when(serviceConfigParser.parseServiceConfig(ArgumentMatchers.<String, Object>anyMap())).thenAnswer(new Answer<ConfigOrError>() {
@Override
public ConfigOrError answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
return ConfigOrError.fromConfig(args[0]);
}
});
resolver.setAddressResolver(mockAddressResolver);
resolver.setResourceResolver(mockResourceResolver);
resolver.start(mockListener);
assertThat(fakeClock.runDueTasks()).isEqualTo(1);
verify(mockListener).onResult(resultCaptor.capture());
ResolutionResult result = resultCaptor.getValue();
InetSocketAddress resolvedBackendAddr = (InetSocketAddress) Iterables.getOnlyElement(Iterables.getOnlyElement(result.getAddresses()).getAddresses());
assertThat(resolvedBackendAddr.getAddress()).isEqualTo(backendAddr);
EquivalentAddressGroup resolvedBalancerAddr = Iterables.getOnlyElement(result.getAttributes().get(GrpclbConstants.ATTR_LB_ADDRS));
assertThat(resolvedBalancerAddr.getAttributes().get(GrpclbConstants.ATTR_LB_ADDR_AUTHORITY)).isEqualTo("foo.example.com");
InetSocketAddress resolvedBalancerSockAddr = (InetSocketAddress) Iterables.getOnlyElement(resolvedBalancerAddr.getAddresses());
assertThat(resolvedBalancerSockAddr.getAddress()).isEqualTo(lbAddr);
assertThat(resolvedBalancerSockAddr.getPort()).isEqualTo(lbPort);
assertThat(result.getServiceConfig().getConfig()).isNotNull();
verify(mockAddressResolver).resolveAddress(hostName);
verify(mockResourceResolver).resolveTxt("_grpc_config." + hostName);
verify(mockResourceResolver).resolveSrv("_grpclb._tcp." + hostName);
}
use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class GrpclbLoadBalancerProviderTest method retrieveModeFromLbConfig_roundRobin.
@Test
public void retrieveModeFromLbConfig_roundRobin() throws Exception {
String lbConfig = "{\"childPolicy\" : [{\"round_robin\" : {}}, {\"pick_first\" : {}}]}";
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();
}
use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class GrpclbLoadBalancerProviderTest method retrieveModeFromLbConfig_wrongChildPolicyType.
@Test
public void retrieveModeFromLbConfig_wrongChildPolicyType() throws Exception {
String lbConfig = "{\"childPolicy\" : {}}";
ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(parseJsonObject(lbConfig));
assertThat(configOrError.getError()).isNotNull();
assertThat(configOrError.getError().getCause()).hasMessageThat().contains("is not List");
}
use of io.grpc.NameResolver.ConfigOrError in project grpc-java by grpc.
the class ClusterManagerLoadBalancerProvider method parseLoadBalancingPolicyConfig.
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
Map<String, PolicySelection> parsedChildPolicies = new LinkedHashMap<>();
try {
Map<String, ?> childPolicies = JsonUtil.getObject(rawConfig, "childPolicy");
if (childPolicies == null || childPolicies.isEmpty()) {
return ConfigOrError.fromError(Status.INTERNAL.withDescription("No child policy provided for cluster_manager LB policy: " + rawConfig));
}
for (String name : childPolicies.keySet()) {
Map<String, ?> childPolicy = JsonUtil.getObject(childPolicies, name);
if (childPolicy == null) {
return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config for child " + name + " in cluster_manager LB policy: " + rawConfig));
}
List<LbConfig> childConfigCandidates = ServiceConfigUtil.unwrapLoadBalancingConfigList(JsonUtil.getListOfObjects(childPolicy, "lbPolicy"));
if (childConfigCandidates == null || childConfigCandidates.isEmpty()) {
return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config specified for child " + name + " in cluster_manager Lb policy: " + rawConfig));
}
LoadBalancerRegistry registry = lbRegistry != null ? lbRegistry : LoadBalancerRegistry.getDefaultRegistry();
ConfigOrError selectedConfig = ServiceConfigUtil.selectLbPolicyFromList(childConfigCandidates, registry);
if (selectedConfig.getError() != null) {
Status error = selectedConfig.getError();
return ConfigOrError.fromError(Status.INTERNAL.withCause(error.getCause()).withDescription(error.getDescription()).augmentDescription("Failed to select config for child " + name));
}
parsedChildPolicies.put(name, (PolicySelection) selectedConfig.getConfig());
}
} catch (RuntimeException e) {
return ConfigOrError.fromError(Status.fromThrowable(e).withDescription("Failed to parse cluster_manager LB config: " + rawConfig));
}
return ConfigOrError.fromConfig(new ClusterManagerConfig(parsedChildPolicies));
}
Aggregations