Skip to content

Commit

Permalink
Rename EtcdNameResolver to IPNameResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Aug 28, 2020
1 parent ee0153b commit 6ed4f1f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -41,10 +40,9 @@
import io.etcd.jetcd.api.AuthGrpc;
import io.etcd.jetcd.api.AuthenticateRequest;
import io.etcd.jetcd.api.AuthenticateResponse;
import io.etcd.jetcd.common.exception.ErrorCode;
import io.etcd.jetcd.common.exception.EtcdExceptionFactory;
import io.etcd.jetcd.resolver.DnsSrvNameResolver;
import io.etcd.jetcd.resolver.EtcdNameResolver;
import io.etcd.jetcd.resolver.IPNameResolver;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
Expand All @@ -62,15 +60,16 @@
import io.netty.channel.ChannelOption;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkArgument;
import static io.etcd.jetcd.Util.isInvalidTokenError;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.handleInterrupt;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.newEtcdException;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.toEtcdException;

final class ClientConnectionManager {

private static final Logger LOGGER = LoggerFactory.getLogger(ClientConnectionManager.class);
private static final Metadata.Key<String> TOKEN = Metadata.Key.of("token", Metadata.ASCII_STRING_MARSHALLER);

private final Object lock;
Expand Down Expand Up @@ -221,13 +220,16 @@ protected ManagedChannelBuilder<?> defaultChannelBuilder(Collection<URI> endpoin
} else {
target = String.format(
"%s://%s/%s",
EtcdNameResolver.SCHEME,
IPNameResolver.SCHEME,
builder.authority() != null ? builder.authority() : "",
endpoints.stream().map(e -> e.getHost() + ":" + e.getPort()).collect(Collectors.joining(",")));
}

final NettyChannelBuilder channelBuilder = NettyChannelBuilder.forTarget(target);

if (builder.authority() != null) {
channelBuilder.overrideAuthority(builder.authority());
}
if (builder.maxInboundMessageSize() != null) {
channelBuilder.maxInboundMessageSize(builder.maxInboundMessageSize());
}
Expand Down Expand Up @@ -326,12 +328,13 @@ public <S, T> CompletableFuture<T> execute(Callable<ListenableFuture<S>> task, F
* @return a CompletableFuture with type T.
*/
@SuppressWarnings("FutureReturnValueIgnored")
public <S, T> CompletableFuture<T> execute(Callable<ListenableFuture<S>> task,
public <S, T> CompletableFuture<T> execute(
Callable<ListenableFuture<S>> task,
Function<S, T> resultConvert,
Predicate<Throwable> doRetry) {

RetryPolicy<CompletableFuture<S>> retryPolicy = new RetryPolicy<CompletableFuture<S>>().handleIf(doRetry)
.onRetriesExceeded(e -> newEtcdException(ErrorCode.ABORTED, "maximum number of auto retries reached"))
.onRetriesExceeded(e -> LOGGER.warn("maximum number of auto retries reached"))
.withBackoff(builder.retryDelay(), builder.retryMaxDelay(), builder.retryChronoUnit());

if (builder.retryMaxDuration() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import io.etcd.jetcd.common.exception.ErrorCode;
import io.etcd.jetcd.common.exception.EtcdExceptionFactory;
import io.grpc.EquivalentAddressGroup;
Expand Down Expand Up @@ -141,11 +140,7 @@ private void doResolve() {
//
groups.add(new EquivalentAddressGroup(
address,
Strings.isNullOrEmpty(authority)
? io.grpc.Attributes.newBuilder()
.set(EquivalentAddressGroup.ATTR_AUTHORITY_OVERRIDE, targetUri.getAuthority())
.build()
: io.grpc.Attributes.EMPTY));
io.grpc.Attributes.EMPTY));
}

if (groups.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import io.etcd.jetcd.common.exception.ErrorCode;
import io.etcd.jetcd.common.exception.EtcdExceptionFactory;
Expand All @@ -39,10 +38,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EtcdNameResolver extends NameResolver {
public static final String SCHEME = "etcd";
public class IPNameResolver extends NameResolver {
public static final String SCHEME = "ip";

private static final Logger LOGGER = LoggerFactory.getLogger(EtcdNameResolver.class);
private static final Logger LOGGER = LoggerFactory.getLogger(IPNameResolver.class);
private static final String ETCD_CLIENT_PORT = "2379";

private final Object lock;
Expand All @@ -58,7 +57,7 @@ public class EtcdNameResolver extends NameResolver {
@GuardedBy("lock")
private Listener listener;

public EtcdNameResolver(URI targetUri) {
public IPNameResolver(URI targetUri) {
this.lock = new Object();
this.targetUri = targetUri;
this.authority = targetUri.getAuthority() != null ? targetUri.getAuthority() : SCHEME;
Expand All @@ -76,11 +75,7 @@ public EtcdNameResolver(URI targetUri) {
}).map(address -> {
return new EquivalentAddressGroup(
address,
Strings.isNullOrEmpty(authority)
? io.grpc.Attributes.newBuilder()
.set(EquivalentAddressGroup.ATTR_AUTHORITY_OVERRIDE, targetUri.getAuthority())
.build()
: io.grpc.Attributes.EMPTY);
io.grpc.Attributes.EMPTY);
}).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import io.grpc.NameResolverProvider;

@AutoService(NameResolverProvider.class)
public class EtcdResolverProvider extends NameResolverProvider {
public class IPResolverProvider extends NameResolverProvider {
@Override
protected boolean isAvailable() {
return true;
Expand All @@ -38,14 +38,14 @@ protected int priority() {

@Override
public String getDefaultScheme() {
return EtcdNameResolver.SCHEME;
return IPNameResolver.SCHEME;
}

@Nullable
@Override
public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
return EtcdNameResolver.SCHEME.equals(targetUri.getScheme())
? new EtcdNameResolver(targetUri)
return IPNameResolver.SCHEME.equals(targetUri.getScheme())
? new IPNameResolver(targetUri)
: null;
}
}

0 comments on commit 6ed4f1f

Please sign in to comment.