feat(spanner): add connection property for enabling/disabling grpc-gcp by olavloite · Pull Request #12898 · googleapis/google-cloud-java · GitHub
Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENABLE_DYNAMIC_CHANNEL_POOL;
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENABLE_END_TO_END_TRACING;
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENABLE_EXTENDED_TRACING;
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENABLE_GRPC_GCP;
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_ENDPOINT;
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_IS_EXPERIMENTAL_HOST;
import static com.google.cloud.spanner.connection.ConnectionOptions.DEFAULT_KEEP_TRANSACTION_ALIVE;
Expand Down Expand Up @@ -85,6 +86,7 @@
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_DYNAMIC_CHANNEL_POOL_PROPERTY_NAME;
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_END_TO_END_TRACING_PROPERTY_NAME;
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_EXTENDED_TRACING_PROPERTY_NAME;
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_GRPC_GCP_PROPERTY_NAME;
import static com.google.cloud.spanner.connection.ConnectionOptions.ENABLE_GRPC_INTERCEPTOR_PROVIDER_SYSTEM_PROPERTY;
import static com.google.cloud.spanner.connection.ConnectionOptions.ENCODED_CREDENTIALS_PROPERTY_NAME;
import static com.google.cloud.spanner.connection.ConnectionOptions.ENDPOINT_PROPERTY_NAME;
Expand Down Expand Up @@ -463,6 +465,18 @@ public class ConnectionProperties {
BOOLEANS,
BooleanConverter.INSTANCE,
Context.STARTUP);
static final ConnectionProperty<Boolean> ENABLE_GRPC_GCP =
create(
ENABLE_GRPC_GCP_PROPERTY_NAME,
"Enable or disable grpc-gcp channel pool (true/false). "
+ "Setting this to false will disable grpc-gcp and use the Gax gRPC channel pool. "
+ "Disabling grpc-gcp also automatically disables dynamic channel pooling, regardless "
+ "of the value of enableDynamicChannelPool, as Spanner only supports dynamic channel "
+ "pooling in combination with grpc-gcp.",
DEFAULT_ENABLE_GRPC_GCP,
BOOLEANS,
BooleanConverter.INSTANCE,
Context.STARTUP);
static final ConnectionProperty<Integer> DCP_MIN_CHANNELS =
create(
DCP_MIN_CHANNELS_PROPERTY_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ static class SpannerPoolKey {
private final SessionPoolOptions sessionPoolOptions;
private final Integer numChannels;
private final Boolean enableDynamicChannelPool;
private final Boolean enableGrpcGcp;
private final Integer dcpMinChannels;
private final Integer dcpMaxChannels;
private final Integer dcpInitialChannels;
Expand Down Expand Up @@ -197,6 +198,7 @@ private SpannerPoolKey(ConnectionOptions options) throws IOException {
: options.getSessionPoolOptions();
this.numChannels = options.getNumChannels();
this.enableDynamicChannelPool = options.isEnableDynamicChannelPool();
this.enableGrpcGcp = options.isEnableGrpcGcp();
this.dcpMinChannels = options.getDcpMinChannels();
this.dcpMaxChannels = options.getDcpMaxChannels();
this.dcpInitialChannels = options.getDcpInitialChannels();
Expand Down Expand Up @@ -228,6 +230,7 @@ public boolean equals(Object o) {
&& Objects.equals(this.sessionPoolOptions, other.sessionPoolOptions)
&& Objects.equals(this.numChannels, other.numChannels)
&& Objects.equals(this.enableDynamicChannelPool, other.enableDynamicChannelPool)
&& Objects.equals(this.enableGrpcGcp, other.enableGrpcGcp)
&& Objects.equals(this.dcpMinChannels, other.dcpMinChannels)
&& Objects.equals(this.dcpMaxChannels, other.dcpMaxChannels)
&& Objects.equals(this.dcpInitialChannels, other.dcpInitialChannels)
Expand Down Expand Up @@ -258,6 +261,7 @@ public int hashCode() {
this.sessionPoolOptions,
this.numChannels,
this.enableDynamicChannelPool,
this.enableGrpcGcp,
this.dcpMinChannels,
this.dcpMaxChannels,
this.dcpInitialChannels,
Expand Down Expand Up @@ -421,9 +425,18 @@ Spanner createSpanner(SpannerPoolKey key, ConnectionOptions options) {
if (key.numChannels != null) {
builder.setNumChannels(key.numChannels);
}
if (key.enableGrpcGcp != null) {
if (Boolean.TRUE.equals(key.enableGrpcGcp)) {
builder.enableGrpcGcpExtension();
} else {
builder.disableGrpcGcpExtension();
}
}
// Configure Dynamic Channel Pooling (DCP) based on explicit user setting.
// Note: Setting numChannels disables DCP even if enableDynamicChannelPool is true.
if (key.enableDynamicChannelPool != null && key.numChannels == null) {
if (key.enableDynamicChannelPool != null
&& key.numChannels == null
&& !Boolean.FALSE.equals(key.enableGrpcGcp)) {
Comment thread
olavloite marked this conversation as resolved.
if (Boolean.TRUE.equals(key.enableDynamicChannelPool)) {
builder.enableDynamicChannelPool();
// Build custom GcpChannelPoolOptions if any DCP-specific options are set.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2026 Google LLC
*
* 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 com.google.cloud.spanner.connection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.connection.StatementResult.ResultType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class GrpcGcpMockServerTest extends AbstractMockServerTest {

@Before
public void setup() {
SpannerPool.closeSpannerPool();
}

@After
public void teardown() {
SpannerPool.closeSpannerPool();
}

private void verifyConnectionWorks(Connection connection) {
StatementResult result = connection.execute(Statement.of("SELECT 1"));
assertEquals(ResultType.RESULT_SET, result.getResultType());
try (ResultSet rs = result.getResultSet()) {
assertTrue(rs.next());
assertEquals(1L, rs.getLong(0));
assertFalse(rs.next());
}
}

@Test
public void testDisableGrpcGcp() {
try (Connection connection = createConnection(";enableGrpcGcp=false")) {
Spanner spanner = ((ConnectionImpl) connection).getSpanner();
assertFalse(spanner.getOptions().isGrpcGcpExtensionEnabled());
verifyConnectionWorks(connection);
}
}

@Test
public void testEnableGrpcGcp() {
try (Connection connection = createConnection(";enableGrpcGcp=true")) {
Spanner spanner = ((ConnectionImpl) connection).getSpanner();
assertTrue(spanner.getOptions().isGrpcGcpExtensionEnabled());
verifyConnectionWorks(connection);
}
}

@Test
public void testDefaultGrpcGcp() {
try (Connection connection = createConnection()) {
Spanner spanner = ((ConnectionImpl) connection).getSpanner();
// Default should be true in SpannerOptions
assertTrue(spanner.getOptions().isGrpcGcpExtensionEnabled());
verifyConnectionWorks(connection);
}
}
}
Loading