use of io.grpc.NameResolver in project grpc-java by grpc.
the class ManagedChannelImpl method getNameResolver.
@VisibleForTesting
static NameResolver getNameResolver(String target, NameResolver.Factory nameResolverFactory, Attributes nameResolverParams) {
// Finding a NameResolver. Try using the target string as the URI. If that fails, try prepending
// "dns:///".
URI targetUri = null;
StringBuilder uriSyntaxErrors = new StringBuilder();
try {
targetUri = new URI(target);
// For "localhost:8080" this would likely cause newNameResolver to return null, because
// "localhost" is parsed as the scheme. Will fall into the next branch and try
// "dns:///localhost:8080".
} catch (URISyntaxException e) {
// Can happen with ip addresses like "[::1]:1234" or 127.0.0.1:1234.
uriSyntaxErrors.append(e.getMessage());
}
if (targetUri != null) {
NameResolver resolver = nameResolverFactory.newNameResolver(targetUri, nameResolverParams);
if (resolver != null) {
return resolver;
}
// "foo.googleapis.com:8080" cause resolver to be null, because "foo.googleapis.com" is an
// unmapped scheme. Just fall through and will try "dns:///foo.googleapis.com:8080"
}
// If we reached here, the targetUri couldn't be used.
if (!URI_PATTERN.matcher(target).matches()) {
// scheme from the factory.
try {
targetUri = new URI(nameResolverFactory.getDefaultScheme(), "", "/" + target, null);
} catch (URISyntaxException e) {
// Should not be possible.
throw new IllegalArgumentException(e);
}
NameResolver resolver = nameResolverFactory.newNameResolver(targetUri, nameResolverParams);
if (resolver != null) {
return resolver;
}
}
throw new IllegalArgumentException(String.format("cannot find a NameResolver for %s%s", target, uriSyntaxErrors.length() > 0 ? " (" + uriSyntaxErrors + ")" : ""));
}
use of io.grpc.NameResolver in project sharding-jdbc by shardingjdbc.
the class EtcdNameSolverFactory method newNameResolver.
@Nullable
@Override
public NameResolver newNameResolver(final URI targetUri, final Attributes params) {
if (!scheme.equals(targetUri.getPath())) {
return null;
}
return new NameResolver() {
@Override
public String getServiceAuthority() {
return scheme;
}
@Override
public void start(final Listener listener) {
if (shutdown) {
return;
}
List<EquivalentAddressGroup> equivalentAddressGroups = Lists.newArrayList();
for (String each : endpoints) {
try {
URI uri = new URI(each.trim());
if (!Strings.isNullOrEmpty(uri.getAuthority()) && SCHEMAS.matcher(uri.getScheme()).matches()) {
InetSocketAddress inetSocketAddress = new InetSocketAddress(uri.getHost(), uri.getPort());
equivalentAddressGroups.add(new EquivalentAddressGroup(inetSocketAddress));
}
} catch (final URISyntaxException ex) {
listener.onError(Status.INVALID_ARGUMENT);
log.warn("Ignored illegal endpoint, %s", ex.getMessage());
}
}
listener.onAddresses(equivalentAddressGroups, Attributes.EMPTY);
}
@Override
public void shutdown() {
if (!shutdown) {
shutdown = true;
executor = SharedResourceHolder.release(GrpcUtil.SHARED_CHANNEL_EXECUTOR, executor);
}
}
};
}
use of io.grpc.NameResolver in project grpc-java by grpc.
the class ManagedChannelImpl method getNameResolver.
private static NameResolver getNameResolver(String target, NameResolver.Factory nameResolverFactory, NameResolver.Args nameResolverArgs) {
// Finding a NameResolver. Try using the target string as the URI. If that fails, try prepending
// "dns:///".
URI targetUri = null;
StringBuilder uriSyntaxErrors = new StringBuilder();
try {
targetUri = new URI(target);
// For "localhost:8080" this would likely cause newNameResolver to return null, because
// "localhost" is parsed as the scheme. Will fall into the next branch and try
// "dns:///localhost:8080".
} catch (URISyntaxException e) {
// Can happen with ip addresses like "[::1]:1234" or 127.0.0.1:1234.
uriSyntaxErrors.append(e.getMessage());
}
if (targetUri != null) {
NameResolver resolver = nameResolverFactory.newNameResolver(targetUri, nameResolverArgs);
if (resolver != null) {
return resolver;
}
// "foo.googleapis.com:8080" cause resolver to be null, because "foo.googleapis.com" is an
// unmapped scheme. Just fall through and will try "dns:///foo.googleapis.com:8080"
}
// If we reached here, the targetUri couldn't be used.
if (!URI_PATTERN.matcher(target).matches()) {
// scheme from the factory.
try {
targetUri = new URI(nameResolverFactory.getDefaultScheme(), "", "/" + target, null);
} catch (URISyntaxException e) {
// Should not be possible.
throw new IllegalArgumentException(e);
}
NameResolver resolver = nameResolverFactory.newNameResolver(targetUri, nameResolverArgs);
if (resolver != null) {
return resolver;
}
}
throw new IllegalArgumentException(String.format("cannot find a NameResolver for %s%s", target, uriSyntaxErrors.length() > 0 ? " (" + uriSyntaxErrors + ")" : ""));
}
use of io.grpc.NameResolver in project grpc-java by grpc.
the class ManagedChannelImplTest method nameResolverArgsPropagation.
@Test
public void nameResolverArgsPropagation() {
final AtomicReference<NameResolver.Args> capturedArgs = new AtomicReference<>();
final NameResolver noopResolver = new NameResolver() {
@Override
public String getServiceAuthority() {
return "fake-authority";
}
@Override
public void start(Listener2 listener) {
}
@Override
public void shutdown() {
}
};
ProxyDetector neverProxy = new ProxyDetector() {
@Override
public ProxiedSocketAddress proxyFor(SocketAddress targetAddress) {
return null;
}
};
NameResolver.Factory factory = new NameResolver.Factory() {
@Override
public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
capturedArgs.set(args);
return noopResolver;
}
@Override
public String getDefaultScheme() {
return "fakescheme";
}
};
channelBuilder.nameResolverFactory(factory).proxyDetector(neverProxy);
createChannel();
NameResolver.Args args = capturedArgs.get();
assertThat(args).isNotNull();
assertThat(args.getDefaultPort()).isEqualTo(DEFAULT_PORT);
assertThat(args.getProxyDetector()).isSameInstanceAs(neverProxy);
verify(offloadExecutor, never()).execute(any(Runnable.class));
args.getOffloadExecutor().execute(new Runnable() {
@Override
public void run() {
}
});
verify(offloadExecutor, times(1)).execute(any(Runnable.class));
}
use of io.grpc.NameResolver in project grpc-java by grpc.
the class ManagedChannelImplTest method badServiceConfigIsRecoverable.
@Test
public void badServiceConfigIsRecoverable() throws Exception {
final List<EquivalentAddressGroup> addresses = ImmutableList.of(new EquivalentAddressGroup(new SocketAddress() {
}));
final class FakeNameResolver extends NameResolver {
Listener2 listener;
@Override
public String getServiceAuthority() {
return "also fake";
}
@Override
public void start(Listener2 listener) {
this.listener = listener;
listener.onResult(ResolutionResult.newBuilder().setAddresses(addresses).setServiceConfig(ConfigOrError.fromError(Status.INTERNAL.withDescription("kaboom is invalid"))).build());
}
@Override
public void shutdown() {
}
}
final class FakeNameResolverFactory2 extends NameResolver.Factory {
FakeNameResolver resolver;
@Nullable
@Override
public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
return (resolver = new FakeNameResolver());
}
@Override
public String getDefaultScheme() {
return "fake";
}
}
FakeNameResolverFactory2 factory = new FakeNameResolverFactory2();
ManagedChannelImplBuilder customBuilder = new ManagedChannelImplBuilder(TARGET, new ClientTransportFactoryBuilder() {
@Override
public ClientTransportFactory buildClientTransportFactory() {
return mockTransportFactory;
}
}, null);
customBuilder.executorPool = executorPool;
customBuilder.channelz = channelz;
ManagedChannel mychannel = customBuilder.nameResolverFactory(factory).build();
ClientCall<Void, Void> call1 = mychannel.newCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT);
ListenableFuture<Void> future1 = ClientCalls.futureUnaryCall(call1, null);
executor.runDueTasks();
try {
future1.get(1, TimeUnit.SECONDS);
Assert.fail();
} catch (ExecutionException e) {
assertThat(Throwables.getStackTraceAsString(e.getCause())).contains("kaboom");
}
// ok the service config is bad, let's fix it.
Map<String, Object> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [{\"round_robin\": {}}]}");
Object fakeLbConfig = new Object();
PolicySelection lbConfigs = new PolicySelection(mockLoadBalancerProvider, fakeLbConfig);
mockLoadBalancerProvider.parseLoadBalancingPolicyConfig(rawServiceConfig);
ManagedChannelServiceConfig managedChannelServiceConfig = createManagedChannelServiceConfig(rawServiceConfig, lbConfigs);
factory.resolver.listener.onResult(ResolutionResult.newBuilder().setAddresses(addresses).setServiceConfig(ConfigOrError.fromConfig(managedChannelServiceConfig)).build());
ClientCall<Void, Void> call2 = mychannel.newCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT.withDeadlineAfter(5, TimeUnit.SECONDS));
ListenableFuture<Void> future2 = ClientCalls.futureUnaryCall(call2, null);
timer.forwardTime(1234, TimeUnit.SECONDS);
executor.runDueTasks();
try {
future2.get();
Assert.fail();
} catch (ExecutionException e) {
assertThat(Throwables.getStackTraceAsString(e.getCause())).contains("deadline");
}
mychannel.shutdownNow();
}
Aggregations