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

feat: Improve upon the default gRPC Connection Pool size #1706

Open
wants to merge 9 commits into
base: grpc-experimental
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.cloud.datastore.Validator.validateNamespace;

import com.google.api.core.BetaApi;
import com.google.api.gax.grpc.ChannelPoolSettings;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.cloud.ServiceDefaults;
Expand Down Expand Up @@ -50,6 +51,9 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
private static final String DEFAULT_DATABASE_ID = "";
public static final String PROJECT_ID_ENV_VAR = "DATASTORE_PROJECT_ID";
public static final String LOCAL_HOST_ENV_VAR = "DATASTORE_EMULATOR_HOST";
public static final int INIT_CHANNEL_COUNT = 1;
public static final int MIN_CHANNEL_COUNT = 1;
public static final int MAX_CHANNEL_COUNT = 4;

private transient TransportChannelProvider channelProvider = null;

Expand Down Expand Up @@ -218,11 +222,20 @@ private DatastoreOptions(Builder builder) {
throw new IllegalArgumentException(
"Only gRPC transport allows setting of channel provider or credentials provider");
} else if (getTransportOptions() instanceof GrpcTransportOptions) {
// For grpc transport options, configure default gRPC Connection pool with minChannelCount = 1
// and maxChannelCount = 4
this.channelProvider =
builder.channelProvider != null
? builder.channelProvider
: GrpcTransportOptions.setUpChannelProvider(
DatastoreSettings.defaultGrpcTransportProviderBuilder(), this);
DatastoreSettings.defaultGrpcTransportProviderBuilder()
.setChannelPoolSettings(
ChannelPoolSettings.builder()
.setInitialChannelCount(INIT_CHANNEL_COUNT)
.setMinChannelCount(MIN_CHANNEL_COUNT)
.setMaxChannelCount(MAX_CHANNEL_COUNT)
.build()),
this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.api.core.InternalApi;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.grpc.ChannelPoolSettings;
import com.google.api.gax.grpc.GrpcCallContext;
import com.google.api.gax.grpc.GrpcTransportChannel;
import com.google.api.gax.rpc.ClientContext;
Expand Down Expand Up @@ -74,9 +75,19 @@ public GrpcDatastoreRpc(DatastoreOptions datastoreOptions) throws IOException {
? getClientContextForEmulator(datastoreOptions)
: getClientContext(datastoreOptions);

/* For grpc transport options, configure default gRPC Connection pool with minChannelCount = 1 and maxChannelCount = 4 */
DatastoreStubSettings datastoreStubSettings =
DatastoreStubSettings.newBuilder(clientContext)
.applyToAllUnaryMethods(retrySettingSetter(datastoreOptions))
.setTransportChannelProvider(
DatastoreSettings.defaultGrpcTransportProviderBuilder()
.setChannelPoolSettings(
ChannelPoolSettings.builder()
.setInitialChannelCount(DatastoreOptions.INIT_CHANNEL_COUNT)
.setMinChannelCount(DatastoreOptions.MIN_CHANNEL_COUNT)
.setMaxChannelCount(DatastoreOptions.MAX_CHANNEL_COUNT)
.build())
.build())
.build();
datastoreStub = GrpcDatastoreStub.create(datastoreStubSettings);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ public void testDatastore() {
assertSame(datastoreRpc, options.build().getRpc());
}

@Test
public void testGrpcDefaultChannelConfigurations() {
DatastoreOptions datastoreOptions =
DatastoreOptions.newBuilder()
.setServiceRpcFactory(datastoreRpcFactory)
.setProjectId(PROJECT_ID)
.setDatabaseId(DATABASE_ID)
.setTransportOptions(GrpcTransportOptions.newBuilder().build())
.setCredentials(NoCredentials.getInstance())
.setHost("http://localhost:" + PORT)
.build();
ChannelPoolSettings channelPoolSettings =
((InstantiatingGrpcChannelProvider) datastoreOptions.getTransportChannelProvider())
.getChannelPoolSettings();
assertEquals(channelPoolSettings.getInitialChannelCount(), DatastoreOptions.INIT_CHANNEL_COUNT);
assertEquals(channelPoolSettings.getMinChannelCount(), DatastoreOptions.MIN_CHANNEL_COUNT);
assertEquals(channelPoolSettings.getMaxChannelCount(), DatastoreOptions.MAX_CHANNEL_COUNT);
}

@Test
public void testCustomChannelAndCredentials() {
InstantiatingGrpcChannelProvider channelProvider =
Expand Down
Loading