Skip to content

Commit

Permalink
Inline AbstractManagedChannelImplBuilder: convert methods calling sup…
Browse files Browse the repository at this point in the history
…er()
  • Loading branch information
sergiitk committed Sep 14, 2020
1 parent e66dcf1 commit 561957d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,18 @@
package io.grpc.internal;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import io.grpc.Attributes;
import io.grpc.BinaryLog;
import io.grpc.ClientInterceptor;
import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
import io.grpc.EquivalentAddressGroup;
import io.grpc.InternalChannelz;
import io.grpc.ManagedChannelBuilder;
import io.grpc.NameResolver;
import io.grpc.NameResolverRegistry;
import io.grpc.ProxyDetector;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
Expand All @@ -51,8 +44,6 @@
*/
public abstract class AbstractManagedChannelImplBuilder
<T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> {
private static final String DIRECT_ADDRESS_SCHEME = "directaddress";

private static final Logger log =
Logger.getLogger(AbstractManagedChannelImplBuilder.class.getName());

Expand Down Expand Up @@ -103,11 +94,6 @@ public static ManagedChannelBuilder<?> forTarget(String target) {
// Access via getter, which may perform authority override as needed
protected NameResolver.Factory nameResolverFactory = nameResolverRegistry.asFactory();

final String target;

@Nullable
protected final SocketAddress directServerAddress;

@Nullable
String userAgent;

Expand Down Expand Up @@ -142,8 +128,6 @@ public static ManagedChannelBuilder<?> forTarget(String target) {
Map<String, ?> defaultServiceConfig;
boolean lookUpServiceConfig = true;

protected TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();

protected int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;

@Nullable
Expand All @@ -158,32 +142,6 @@ public static ManagedChannelBuilder<?> forTarget(String target) {
protected boolean recordRealTimeMetrics = false;
protected boolean tracingEnabled = true;

protected AbstractManagedChannelImplBuilder(String target) {
this.target = Preconditions.checkNotNull(target, "target");
this.directServerAddress = null;
}

/**
* Returns a target string for the SocketAddress. It is only used as a placeholder, because
* DirectAddressNameResolverFactory will not actually try to use it. However, it must be a valid
* URI.
*/
@VisibleForTesting
static String makeTargetStringForDirectAddress(SocketAddress address) {
try {
return new URI(DIRECT_ADDRESS_SCHEME, "", "/" + address, null).toString();
} catch (URISyntaxException e) {
// It should not happen.
throw new RuntimeException(e);
}
}

protected AbstractManagedChannelImplBuilder(SocketAddress directServerAddress, String authority) {
this.target = makeTargetStringForDirectAddress(directServerAddress);
this.directServerAddress = directServerAddress;
this.nameResolverFactory = new DirectAddressNameResolverFactory(directServerAddress, authority);
}

// Temporarily disable retry when stats or tracing is enabled to avoid breakage, until we know
// what should be the desired behavior for retry + stats/tracing.
// TODO(zdapeng): FIX IT
Expand Down Expand Up @@ -253,22 +211,6 @@ final List<ClientInterceptor> getEffectiveInterceptors() {
return effectiveInterceptors;
}

/**
* Subclasses should override this method to provide the {@link ClientTransportFactory}
* appropriate for this channel. This method is meant for Transport implementors and should not
* be used by normal users.
*/
protected abstract ClientTransportFactory buildTransportFactory();

/**
* Subclasses can override this method to provide a default port to {@link NameResolver} for use
* in cases where the target string doesn't include a port. The default implementation returns
* {@link GrpcUtil#DEFAULT_PORT_SSL}.
*/
protected int getDefaultPort() {
return GrpcUtil.DEFAULT_PORT_SSL;
}

/**
* Returns a {@link NameResolver.Factory} for the channel.
*/
Expand All @@ -279,48 +221,4 @@ NameResolver.Factory getNameResolverFactory() {
return new OverrideAuthorityNameResolverFactory(nameResolverFactory, authorityOverride);
}
}

private static class DirectAddressNameResolverFactory extends NameResolver.Factory {
final SocketAddress address;
final String authority;

DirectAddressNameResolverFactory(SocketAddress address, String authority) {
this.address = address;
this.authority = authority;
}

@Override
public NameResolver newNameResolver(URI notUsedUri, NameResolver.Args args) {
return new NameResolver() {
@Override
public String getServiceAuthority() {
return authority;
}

@Override
public void start(Listener2 listener) {
listener.onResult(
ResolutionResult.newBuilder()
.setAddresses(Collections.singletonList(new EquivalentAddressGroup(address)))
.setAttributes(Attributes.EMPTY)
.build());
}

@Override
public void shutdown() {}
};
}

@Override
public String getDefaultScheme() {
return DIRECT_ADDRESS_SCHEME;
}
}

/**
* Returns the internal offload executor pool for offloading tasks.
*/
protected ObjectPool<? extends Executor> getOffloadExecutorPool() {
return this.offloadExecutorPool;
}
}
119 changes: 96 additions & 23 deletions core/src/main/java/io/grpc/internal/ManagedChannelImplBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.MoreExecutors;
import io.grpc.Attributes;
import io.grpc.BinaryLog;
import io.grpc.ClientInterceptor;
import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
import io.grpc.EquivalentAddressGroup;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.NameResolver;
import io.grpc.ProxyDetector;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -44,8 +48,6 @@
public final class ManagedChannelImplBuilder
extends AbstractManagedChannelImplBuilder<ManagedChannelImplBuilder> {

private boolean authorityCheckerDisabled;

/**
* An interface for Transport implementors to provide the {@link ClientTransportFactory}
* appropriate for the channel.
Expand Down Expand Up @@ -89,16 +91,31 @@ public int getDefaultPort() {
}
}

private final class ManagedChannelDefaultPortProvider implements
private static final class ManagedChannelDefaultPortProvider implements
ChannelBuilderDefaultPortProvider {
@Override
public int getDefaultPort() {
return ManagedChannelImplBuilder.super.getDefaultPort();
return GrpcUtil.DEFAULT_PORT_SSL;
}
}

private static final String DIRECT_ADDRESS_SCHEME = "directaddress";

final String target;
@Nullable
private final SocketAddress directServerAddress;
private final ClientTransportFactoryBuilder clientTransportFactoryBuilder;
private final ChannelBuilderDefaultPortProvider channelBuilderDefaultPortProvider;
private boolean authorityCheckerDisabled;


public static ManagedChannelBuilder<?> forAddress(String name, int port) {
throw new UnsupportedOperationException("ClientTransportFactoryBuilder is required");
}

public static ManagedChannelBuilder<?> forTarget(String target) {
throw new UnsupportedOperationException("ClientTransportFactoryBuilder is required");
}

/**
* Creates a new managed channel builder with a target string, which can be either a valid {@link
Expand All @@ -108,9 +125,10 @@ public int getDefaultPort() {
public ManagedChannelImplBuilder(String target,
ClientTransportFactoryBuilder clientTransportFactoryBuilder,
@Nullable ChannelBuilderDefaultPortProvider channelBuilderDefaultPortProvider) {
super(target);
this.target = Preconditions.checkNotNull(target, "target");
this.clientTransportFactoryBuilder = Preconditions
.checkNotNull(clientTransportFactoryBuilder, "clientTransportFactoryBuilder");
this.directServerAddress = null;

if (channelBuilderDefaultPortProvider != null) {
this.channelBuilderDefaultPortProvider = channelBuilderDefaultPortProvider;
Expand All @@ -127,9 +145,11 @@ public ManagedChannelImplBuilder(String target,
public ManagedChannelImplBuilder(SocketAddress directServerAddress, String authority,
ClientTransportFactoryBuilder clientTransportFactoryBuilder,
@Nullable ChannelBuilderDefaultPortProvider channelBuilderDefaultPortProvider) {
super(directServerAddress, authority);
this.target = makeTargetStringForDirectAddress(directServerAddress);
this.clientTransportFactoryBuilder = Preconditions
.checkNotNull(clientTransportFactoryBuilder, "clientTransportFactoryBuilder");
this.directServerAddress = directServerAddress;
this.nameResolverFactory = new DirectAddressNameResolverFactory(directServerAddress, authority);

if (channelBuilderDefaultPortProvider != null) {
this.channelBuilderDefaultPortProvider = channelBuilderDefaultPortProvider;
Expand All @@ -138,13 +158,35 @@ public ManagedChannelImplBuilder(SocketAddress directServerAddress, String autho
}
}

@Override
protected ClientTransportFactory buildTransportFactory() {
/**
* Returns a target string for the SocketAddress. It is only used as a placeholder, because
* DirectAddressNameResolverFactory will not actually try to use it. However, it must be a valid
* URI.
*/
@VisibleForTesting
static String makeTargetStringForDirectAddress(SocketAddress address) {
try {
return new URI(DIRECT_ADDRESS_SCHEME, "", "/" + address, null).toString();
} catch (URISyntaxException e) {
// It should not happen.
throw new RuntimeException(e);
}
}

/**
* Transport implementors must provide {@link ClientTransportFactoryBuilder} that returns {@link
* ClientTransportFactory} appropriate the channel. This method is meant for Transport
* implementors and should not be used by normal users.
*/
ClientTransportFactory buildTransportFactory() {
return clientTransportFactoryBuilder.buildClientTransportFactory();
}

@Override
protected int getDefaultPort() {
/**
* Returns a default port to {@link NameResolver} for use in cases where the target string doesn't
* include a port. The default implementation returns {@link GrpcUtil#DEFAULT_PORT_SSL}.
*/
int getDefaultPort() {
return channelBuilderDefaultPortProvider.getDefaultPort();
}

Expand Down Expand Up @@ -272,8 +314,8 @@ public ManagedChannelImplBuilder idleTimeout(long value, TimeUnit unit) {
}

/**
* Sets the maximum message size allowed for a single gRPC frame. If an inbound messages
* larger than this limit is received it will not be processed and the RPC will fail with
* Sets the maximum message size allowed for a single gRPC frame. If an inbound messages larger
* than this limit is received it will not be processed and the RPC will fail with
* RESOURCE_EXHAUSTED.
*/
@Override
Expand Down Expand Up @@ -425,8 +467,8 @@ public void setStatsEnabled(boolean value) {
}

/**
* Disable or enable stats recording for RPC upstarts. Effective only if {@link
* #setStatsEnabled} is set to true. Enabled by default.
* Disable or enable stats recording for RPC upstarts. Effective only if {@link #setStatsEnabled}
* is set to true. Enabled by default.
*/
public void setStatsRecordStartedRpcs(boolean value) {
recordStartedRpcs = value;
Expand All @@ -451,8 +493,8 @@ public void setStatsRecordRealTimeMetrics(boolean value) {
/**
* Disable or enable tracing features. Enabled by default.
*
* <p>For the current release, calling {@code setTracingEnabled(true)} may have a side effect that
* disables retry.
* <p>For the current release, calling {@code setTracingEnabled(true)} may have a side effect
* that disables retry.
*/
public void setTracingEnabled(boolean value) {
tracingEnabled = value;
Expand Down Expand Up @@ -486,16 +528,47 @@ String checkAuthority(String authority) {
return GrpcUtil.checkAuthority(authority);
}

@Override
/**
* Returns the internal offload executor pool for offloading tasks.
*/
public ObjectPool<? extends Executor> getOffloadExecutorPool() {
return super.getOffloadExecutorPool();
return this.offloadExecutorPool;
}

public static ManagedChannelBuilder<?> forAddress(String name, int port) {
throw new UnsupportedOperationException("ClientTransportFactoryBuilder is required");
}
private static class DirectAddressNameResolverFactory extends NameResolver.Factory {
final SocketAddress address;
final String authority;

public static ManagedChannelBuilder<?> forTarget(String target) {
throw new UnsupportedOperationException("ClientTransportFactoryBuilder is required");
DirectAddressNameResolverFactory(SocketAddress address, String authority) {
this.address = address;
this.authority = authority;
}

@Override
public NameResolver newNameResolver(URI notUsedUri, NameResolver.Args args) {
return new NameResolver() {
@Override
public String getServiceAuthority() {
return authority;
}

@Override
public void start(Listener2 listener) {
listener.onResult(
ResolutionResult.newBuilder()
.setAddresses(Collections.singletonList(new EquivalentAddressGroup(address)))
.setAttributes(Attributes.EMPTY)
.build());
}

@Override
public void shutdown() {}
};
}

@Override
public String getDefaultScheme() {
return DIRECT_ADDRESS_SCHEME;
}
}
}

0 comments on commit 561957d

Please sign in to comment.