Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit 752ab4f

Browse files
committed
incorporate changes
1 parent 418c90e commit 752ab4f

4 files changed

Lines changed: 78 additions & 35 deletions

File tree

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ConnectionProperties.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ public class ConnectionProperties {
456456
ENABLE_DYNAMIC_CHANNEL_POOL_PROPERTY_NAME,
457457
"Enable dynamic channel pooling for automatic gRPC channel scaling. When enabled, the "
458458
+ "client will automatically scale the number of channels based on load. Setting "
459-
+ "numChannels will disable dynamic channel pooling even if this is set to true.",
459+
+ "numChannels will disable dynamic channel pooling even if this is set to true. "
460+
+ "The default is currently false (disabled), but this may change to true in a "
461+
+ "future version. Set this property explicitly to ensure consistent behavior.",
460462
DEFAULT_ENABLE_DYNAMIC_CHANNEL_POOL,
461463
BOOLEANS,
462464
BooleanConverter.INSTANCE,

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/SpannerPool.java

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -421,40 +421,48 @@ Spanner createSpanner(SpannerPoolKey key, ConnectionOptions options) {
421421
if (key.numChannels != null) {
422422
builder.setNumChannels(key.numChannels);
423423
}
424-
// Configure Dynamic Channel Pooling (DCP) if enabled.
424+
// Configure Dynamic Channel Pooling (DCP) based on explicit user setting.
425425
// Note: Setting numChannels disables DCP even if enableDynamicChannelPool is true.
426-
if (Boolean.TRUE.equals(key.enableDynamicChannelPool) && key.numChannels == null) {
427-
builder.enableDynamicChannelPool();
428-
// Build custom GcpChannelPoolOptions if any DCP-specific options are set.
429-
if (key.dcpMinChannels != null
430-
|| key.dcpMaxChannels != null
431-
|| key.dcpInitialChannels != null) {
432-
// Build GcpChannelPoolOptions from scratch with custom values or Spanner defaults.
433-
int minChannels =
434-
key.dcpMinChannels != null
435-
? key.dcpMinChannels
436-
: SpannerOptions.DEFAULT_DYNAMIC_POOL_MIN_CHANNELS;
437-
int maxChannels =
438-
key.dcpMaxChannels != null
439-
? key.dcpMaxChannels
440-
: SpannerOptions.DEFAULT_DYNAMIC_POOL_MAX_CHANNELS;
441-
int initChannels =
442-
key.dcpInitialChannels != null
443-
? key.dcpInitialChannels
444-
: SpannerOptions.DEFAULT_DYNAMIC_POOL_INITIAL_SIZE;
445-
GcpChannelPoolOptions poolOptions =
446-
GcpChannelPoolOptions.newBuilder()
447-
.setMinSize(minChannels)
448-
.setMaxSize(maxChannels)
449-
.setInitSize(initChannels)
450-
.setDynamicScaling(
451-
SpannerOptions.DEFAULT_DYNAMIC_POOL_MIN_RPC,
452-
SpannerOptions.DEFAULT_DYNAMIC_POOL_MAX_RPC,
453-
SpannerOptions.DEFAULT_DYNAMIC_POOL_SCALE_DOWN_INTERVAL)
454-
.setAffinityKeyLifetime(SpannerOptions.DEFAULT_DYNAMIC_POOL_AFFINITY_KEY_LIFETIME)
455-
.setCleanupInterval(SpannerOptions.DEFAULT_DYNAMIC_POOL_CLEANUP_INTERVAL)
456-
.build();
457-
builder.setGcpChannelPoolOptions(poolOptions);
426+
if (key.enableDynamicChannelPool != null && key.numChannels == null) {
427+
if (Boolean.TRUE.equals(key.enableDynamicChannelPool)) {
428+
builder.enableDynamicChannelPool();
429+
// Build custom GcpChannelPoolOptions if any DCP-specific options are set.
430+
if (key.dcpMinChannels != null
431+
|| key.dcpMaxChannels != null
432+
|| key.dcpInitialChannels != null) {
433+
// Build GcpChannelPoolOptions from scratch with custom values or Spanner defaults.
434+
// Note: GcpChannelPoolOptions does not have a toBuilder() method, so we must
435+
// construct from scratch using SpannerOptions defaults for unspecified values.
436+
int minChannels =
437+
key.dcpMinChannels != null
438+
? key.dcpMinChannels
439+
: SpannerOptions.DEFAULT_DYNAMIC_POOL_MIN_CHANNELS;
440+
int maxChannels =
441+
key.dcpMaxChannels != null
442+
? key.dcpMaxChannels
443+
: SpannerOptions.DEFAULT_DYNAMIC_POOL_MAX_CHANNELS;
444+
int initChannels =
445+
key.dcpInitialChannels != null
446+
? key.dcpInitialChannels
447+
: SpannerOptions.DEFAULT_DYNAMIC_POOL_INITIAL_SIZE;
448+
GcpChannelPoolOptions poolOptions =
449+
GcpChannelPoolOptions.newBuilder()
450+
.setMinSize(minChannels)
451+
.setMaxSize(maxChannels)
452+
.setInitSize(initChannels)
453+
.setDynamicScaling(
454+
SpannerOptions.DEFAULT_DYNAMIC_POOL_MIN_RPC,
455+
SpannerOptions.DEFAULT_DYNAMIC_POOL_MAX_RPC,
456+
SpannerOptions.DEFAULT_DYNAMIC_POOL_SCALE_DOWN_INTERVAL)
457+
.setAffinityKeyLifetime(SpannerOptions.DEFAULT_DYNAMIC_POOL_AFFINITY_KEY_LIFETIME)
458+
.setCleanupInterval(SpannerOptions.DEFAULT_DYNAMIC_POOL_CLEANUP_INTERVAL)
459+
.build();
460+
builder.setGcpChannelPoolOptions(poolOptions);
461+
}
462+
} else {
463+
// Explicitly disable DCP when enableDynamicChannelPool=false.
464+
// This ensures consistent behavior even if the default changes in the future.
465+
builder.disableDynamicChannelPool();
458466
}
459467
}
460468
if (options.getChannelProvider() != null) {

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ConnectionOptionsTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,10 @@ public void testEnableDynamicChannelPool() {
14371437
.setCredentials(NoCredentials.getInstance())
14381438
.build()
14391439
.isEnableDynamicChannelPool());
1440-
// Disabled
1440+
}
1441+
1442+
@Test
1443+
public void testDisableDynamicChannelPool() {
14411444
assertFalse(
14421445
ConnectionOptions.newBuilder()
14431446
.setUri(

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/SpannerPoolTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,34 @@ public void testDynamicChannelPoolWithAllSettings() {
741741
.setCredentials(NoCredentials.getInstance())
742742
.build()));
743743
}
744+
745+
@Test
746+
public void testExplicitlyDisabledDynamicChannelPool() {
747+
SpannerPoolKey keyWithoutDcpSetting =
748+
SpannerPoolKey.of(
749+
ConnectionOptions.newBuilder()
750+
.setUri("cloudspanner:/projects/p/instances/i/databases/d")
751+
.setCredentials(NoCredentials.getInstance())
752+
.build());
753+
SpannerPoolKey keyWithDcpExplicitlyDisabled =
754+
SpannerPoolKey.of(
755+
ConnectionOptions.newBuilder()
756+
.setUri(
757+
"cloudspanner:/projects/p/instances/i/databases/d?enableDynamicChannelPool=false")
758+
.setCredentials(NoCredentials.getInstance())
759+
.build());
760+
761+
// Keys should be different because one has explicit false and one has null (default)
762+
assertNotEquals(keyWithoutDcpSetting, keyWithDcpExplicitlyDisabled);
763+
764+
// Verify the explicit false setting is preserved
765+
assertEquals(
766+
keyWithDcpExplicitlyDisabled,
767+
SpannerPoolKey.of(
768+
ConnectionOptions.newBuilder()
769+
.setUri(
770+
"cloudspanner:/projects/p/instances/i/databases/d?enableDynamicChannelPool=false")
771+
.setCredentials(NoCredentials.getInstance())
772+
.build()));
773+
}
744774
}

0 commit comments

Comments
 (0)