Skip to content

Commit

Permalink
feat: Improve upon the default gRPC Connection Pool size (#1706)
Browse files Browse the repository at this point in the history
* feat: Improve upon the default gRPC Connection Pool size

* feat: Improve upon the default gRPC Connection Pool size

* fix formatting

* add comment

* Add test

* formatting

* Add constants for min, max and init channel count

* Fix ci testing environment with java 11

* Revert "Fix ci testing environment with java 11"

This reverts commit 00c0d21.
  • Loading branch information
cindy-peng authored Feb 6, 2025
1 parent 4a18ad2 commit 7ba4531
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
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

0 comments on commit 7ba4531

Please sign in to comment.