Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel builders extend public API #7359

Merged
merged 6 commits into from Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,6 +20,7 @@

import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Server;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
Expand All @@ -30,7 +31,6 @@
import io.grpc.benchmarks.qps.AsyncServer;
import io.grpc.inprocess.InProcessChannelBuilder;
import io.grpc.inprocess.InProcessServerBuilder;
import io.grpc.internal.AbstractManagedChannelImplBuilder;
import io.grpc.internal.AbstractServerImplBuilder;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
Expand Down Expand Up @@ -81,7 +81,7 @@ public enum Transport {
@Setup
public void setUp() throws Exception {
AbstractServerImplBuilder<?> serverBuilder;
AbstractManagedChannelImplBuilder<?> channelBuilder;
ManagedChannelBuilder<?> channelBuilder;
switch (transport) {
case INPROCESS:
{
Expand Down
38 changes: 30 additions & 8 deletions core/src/main/java/io/grpc/inprocess/InProcessChannelBuilder.java
Expand Up @@ -21,11 +21,14 @@

import io.grpc.ChannelLogger;
import io.grpc.ExperimentalApi;
import io.grpc.ForwardingChannelBuilder;
import io.grpc.Internal;
import io.grpc.internal.AbstractManagedChannelImplBuilder;
import io.grpc.ManagedChannelBuilder;
import io.grpc.internal.ClientTransportFactory;
import io.grpc.internal.ConnectionClientTransport;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.ManagedChannelImplBuilder;
import io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder;
import io.grpc.internal.SharedResourceHolder;
import java.net.SocketAddress;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -42,7 +45,7 @@
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1783")
public final class InProcessChannelBuilder extends
AbstractManagedChannelImplBuilder<InProcessChannelBuilder> {
ForwardingChannelBuilder<InProcessChannelBuilder> {
/**
* Create a channel builder that will connect to the server with the given name.
*
Expand All @@ -67,18 +70,35 @@ public static InProcessChannelBuilder forAddress(String name, int port) {
throw new UnsupportedOperationException("call forName() instead");
}

private final ManagedChannelImplBuilder managedChannelImplBuilder;
private final String name;
private ScheduledExecutorService scheduledExecutorService;
private int maxInboundMetadataSize = Integer.MAX_VALUE;
private boolean transportIncludeStatusCause = false;

private InProcessChannelBuilder(String name) {
super(new InProcessSocketAddress(name), "localhost");
this.name = checkNotNull(name, "name");

final class InProcessChannelTransportFactoryBuilder implements ClientTransportFactoryBuilder {
@Override
public ClientTransportFactory buildClientTransportFactory() {
return buildTransportFactory();
}
}

managedChannelImplBuilder = new ManagedChannelImplBuilder(new InProcessSocketAddress(name),
"localhost", new InProcessChannelTransportFactoryBuilder(), null);

// In-process transport should not record its traffic to the stats module.
// https://github.com/grpc/grpc-java/issues/2284
setStatsRecordStartedRpcs(false);
setStatsRecordFinishedRpcs(false);
managedChannelImplBuilder.setStatsRecordStartedRpcs(false);
managedChannelImplBuilder.setStatsRecordFinishedRpcs(false);
}

@Internal
@Override
protected ManagedChannelBuilder<?> delegate() {
return managedChannelImplBuilder;
}

@Override
Expand Down Expand Up @@ -177,13 +197,15 @@ public InProcessChannelBuilder propagateCauseWithStatus(boolean enable) {
return this;
}

@Override
@Internal
protected ClientTransportFactory buildTransportFactory() {
ClientTransportFactory buildTransportFactory() {
return new InProcessClientTransportFactory(
name, scheduledExecutorService, maxInboundMetadataSize, transportIncludeStatusCause);
}

void setStatsEnabled(boolean value) {
this.managedChannelImplBuilder.setStatsEnabled(value);
}

/**
* Creates InProcess transports. Exposed for internal use, as it should be private.
*/
Expand Down
@@ -0,0 +1,33 @@
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.inprocess;

import io.grpc.Internal;

/**
* Internal {@link InProcessChannelBuilder} accessor. This is intended for usage internal to the
* gRPC team. If you *really* think you need to use this, contact the gRPC team first.
*/
@Internal
public final class InternalInProcessChannelBuilder {

public static void setStatsEnabled(InProcessChannelBuilder builder, boolean value) {
builder.setStatsEnabled(value);
}

private InternalInProcessChannelBuilder() {}
}
Expand Up @@ -51,7 +51,7 @@
import javax.annotation.Nullable;

/**
* The base class for channel builders.
* Abstract base class for channel builders.
*
* @param <T> The concrete type of this builder.
*/
Expand Down
198 changes: 198 additions & 0 deletions core/src/main/java/io/grpc/internal/ManagedChannelImplBuilder.java
@@ -0,0 +1,198 @@
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import com.google.common.base.Preconditions;
import io.grpc.ManagedChannelBuilder;
import java.net.SocketAddress;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;

/**
* Default managed channel builder, for usage in Transport implementations.
*/
public final class ManagedChannelImplBuilder
extends AbstractManagedChannelImplBuilder<ManagedChannelImplBuilder> {

private boolean authorityCheckerDisabled;
@Deprecated
@Nullable
private OverrideAuthorityChecker authorityChecker;

/**
* An interface for Transport implementors to provide the {@link ClientTransportFactory}
* appropriate for the channel.
*/
public interface ClientTransportFactoryBuilder {
ClientTransportFactory buildClientTransportFactory();
}

/**
* An interface for Transport implementors to provide a default port to {@link
* io.grpc.NameResolver} for use in cases where the target string doesn't include a port. The
* default implementation returns {@link GrpcUtil#DEFAULT_PORT_SSL}.
*/
public interface ChannelBuilderDefaultPortProvider {
int getDefaultPort();
}

/**
* Default implementation of {@link ChannelBuilderDefaultPortProvider} that returns a fixed port.
*/
public static final class FixedPortProvider implements ChannelBuilderDefaultPortProvider {
private final int port;

public FixedPortProvider(int port) {
this.port = port;
}

@Override
public int getDefaultPort() {
return port;
}
}

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

private final ClientTransportFactoryBuilder clientTransportFactoryBuilder;
private final ChannelBuilderDefaultPortProvider channelBuilderDefaultPortProvider;

/**
* Creates a new managed channel builder with a target string, which can be either a valid {@link
* io.grpc.NameResolver}-compliant URI, or an authority string. Transport implementors must
* provide client transport factory builder, and may set custom channel default port provider.
*/
public ManagedChannelImplBuilder(String target,
ClientTransportFactoryBuilder clientTransportFactoryBuilder,
@Nullable ChannelBuilderDefaultPortProvider channelBuilderDefaultPortProvider) {
super(target);
this.clientTransportFactoryBuilder = Preconditions
.checkNotNull(clientTransportFactoryBuilder, "clientTransportFactoryBuilder");

if (channelBuilderDefaultPortProvider != null) {
this.channelBuilderDefaultPortProvider = channelBuilderDefaultPortProvider;
} else {
this.channelBuilderDefaultPortProvider = new ManagedChannelDefaultPortProvider();
}
}

/**
* Creates a new managed channel builder with the given server address, authority string of the
* channel. Transport implementors must provide client transport factory builder, and may set
* custom channel default port provider.
*/
public ManagedChannelImplBuilder(SocketAddress directServerAddress, String authority,
ClientTransportFactoryBuilder clientTransportFactoryBuilder,
@Nullable ChannelBuilderDefaultPortProvider channelBuilderDefaultPortProvider) {
super(directServerAddress, authority);
this.clientTransportFactoryBuilder = Preconditions
.checkNotNull(clientTransportFactoryBuilder, "clientTransportFactoryBuilder");

if (channelBuilderDefaultPortProvider != null) {
this.channelBuilderDefaultPortProvider = channelBuilderDefaultPortProvider;
} else {
this.channelBuilderDefaultPortProvider = new ManagedChannelDefaultPortProvider();
}
}

@Override
protected ClientTransportFactory buildTransportFactory() {
return clientTransportFactoryBuilder.buildClientTransportFactory();
}

@Override
protected int getDefaultPort() {
return channelBuilderDefaultPortProvider.getDefaultPort();
}

/** Disable the check whether the authority is valid. */
public ManagedChannelImplBuilder disableCheckAuthority() {
authorityCheckerDisabled = true;
return this;
}

/** Enable previously disabled authority check. */
public ManagedChannelImplBuilder enableCheckAuthority() {
authorityCheckerDisabled = false;
return this;
}

@Deprecated
public interface OverrideAuthorityChecker {
String checkAuthority(String authority);
}

@Deprecated
public void overrideAuthorityChecker(@Nullable OverrideAuthorityChecker authorityChecker) {
sergiitk marked this conversation as resolved.
Show resolved Hide resolved
this.authorityChecker = authorityChecker;
}

@Override
protected String checkAuthority(String authority) {
if (authorityCheckerDisabled) {
return authority;
}
if (authorityChecker != null) {
return authorityChecker.checkAuthority(authority);
}
return super.checkAuthority(authority);
}

@Override
public void setStatsEnabled(boolean value) {
super.setStatsEnabled(value);
}

@Override
public void setStatsRecordStartedRpcs(boolean value) {
super.setStatsRecordStartedRpcs(value);
}

@Override
public void setStatsRecordFinishedRpcs(boolean value) {
super.setStatsRecordFinishedRpcs(value);
}

@Override
public void setStatsRecordRealTimeMetrics(boolean value) {
super.setStatsRecordRealTimeMetrics(value);
}

@Override
public void setTracingEnabled(boolean value) {
super.setTracingEnabled(value);
}

@Override
public ObjectPool<? extends Executor> getOffloadExecutorPool() {
return super.getOffloadExecutorPool();
}

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");
}
}