Skip to content

Commit 36f49b5

Browse files
committed
fix(spanner): lazily initialize cloudpath fallback pool
1 parent b02389f commit 36f49b5

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

java-spanner/google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpc.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -708,18 +708,19 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
708708
ManagedChannelBuilder<?> fallbackBuilder = cloudPathBuilder;
709709
if (options.isGrpcGcpExtensionEnabled()) {
710710
String jsonApiConfig = parseGrpcGcpApiConfig();
711-
GcpManagedChannelOptions gcpOptions = grpcGcpOptionsWithMetricsAndDcp(options);
712-
if (gcpOptions == null) {
713-
gcpOptions = GcpManagedChannelOptions.newBuilder().build();
714-
}
711+
GcpManagedChannelOptions primaryGcpOptions = grpcGcpOptionsWithMetricsAndDcp(options);
712+
GcpManagedChannelOptions fallbackGcpOptions =
713+
GcpManagedChannelOptions.newBuilder(primaryGcpOptions)
714+
.withChannelPoolOptions(getGrpcGcpLazyFallbackChannelPoolOptions(options))
715+
.build();
715716
primaryBuilder =
716717
GcpManagedChannelBuilder.forDelegateBuilder(builder)
717718
.withApiConfigJsonString(jsonApiConfig)
718-
.withOptions(gcpOptions);
719+
.withOptions(primaryGcpOptions);
719720
fallbackBuilder =
720721
GcpManagedChannelBuilder.forDelegateBuilder(cloudPathBuilder)
721722
.withApiConfigJsonString(jsonApiConfig)
722-
.withOptions(gcpOptions);
723+
.withOptions(fallbackGcpOptions);
723724
}
724725

725726
GcpFallbackOpenTelemetry fallbackTelemetry =
@@ -849,6 +850,14 @@ static GcpChannelPoolOptions getGrpcGcpChannelPoolOptions(SpannerOptions options
849850
.build();
850851
}
851852

853+
@VisibleForTesting
854+
static GcpChannelPoolOptions getGrpcGcpLazyFallbackChannelPoolOptions(SpannerOptions options) {
855+
return GcpChannelPoolOptions.newBuilder(getGrpcGcpChannelPoolOptions(options))
856+
.setMinSize(0)
857+
.setInitSize(0)
858+
.build();
859+
}
860+
852861
@VisibleForTesting
853862
static GcpChannelPoolOptions getGrpcGcpEndpointChannelPoolOptions(SpannerOptions options) {
854863
GcpChannelPoolOptions channelPoolOptions = options.getGcpChannelPoolOptions();

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpcTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,27 @@ public void testGrpcGcpOptionsIncludeStaticChannelPoolSettingsWithoutDcp() throw
12641264
assertEquals(Duration.ZERO, grpcGcpOptions.getChannelPoolOptions().getScaleDownInterval());
12651265
}
12661266

1267+
@Test
1268+
public void testGrpcGcpLazyFallbackChannelPoolOptionsStartWithoutChannels() {
1269+
SpannerOptions options =
1270+
SpannerOptions.newBuilder()
1271+
.setProjectId("[PROJECT]")
1272+
.enableGrpcGcpExtension()
1273+
.disableDynamicChannelPool()
1274+
.setNumChannels(8)
1275+
.build();
1276+
1277+
GcpChannelPoolOptions poolOptions =
1278+
GapicSpannerRpc.getGrpcGcpLazyFallbackChannelPoolOptions(options);
1279+
1280+
assertEquals(8, poolOptions.getMaxSize());
1281+
assertEquals(0, poolOptions.getMinSize());
1282+
assertEquals(0, poolOptions.getInitSize());
1283+
assertEquals(0, poolOptions.getMinRpcPerChannel());
1284+
assertEquals(0, poolOptions.getMaxRpcPerChannel());
1285+
assertEquals(Duration.ZERO, poolOptions.getScaleDownInterval());
1286+
}
1287+
12671288
@Test
12681289
public void testGrpcGcpOptionsRetainDynamicChannelPoolSettingsWithDcp() throws Exception {
12691290
Duration affinityKeyLifetime = Duration.ofMinutes(10);
@@ -1434,7 +1455,7 @@ private SpannerOptions createSpannerOptions() {
14341455
}
14351456

14361457
@Test
1437-
public void testDirectPathFallbackCreatesOneGrpcGcpLayerPerPath() {
1458+
public void testDirectPathFallbackCreatesLazyCloudPathGrpcGcpPool() {
14381459
SpannerOptions.useEnvironment(new SpannerOptions.SpannerEnvironment() {});
14391460
GapicSpannerRpc rpc = null;
14401461
try {
@@ -1445,8 +1466,8 @@ public void testDirectPathFallbackCreatesOneGrpcGcpLayerPerPath() {
14451466
GrpcGcpObjectCounts before = countGrpcGcpObjectsFromChannelz();
14461467
rpc = new GapicSpannerRpc(options);
14471468
GrpcGcpObjectCounts counts = countGrpcGcpObjectsFromChannelz().minus(before);
1448-
assertEquals(counts.debugString(), 6, counts.gcpManagedChannels);
1449-
assertEquals(counts.debugString(), 48, counts.channelRefs);
1469+
assertEquals(counts.debugString(), 3, counts.gcpManagedChannels);
1470+
assertEquals(counts.debugString(), 24, counts.channelRefs);
14501471
} finally {
14511472
if (rpc != null) {
14521473
rpc.shutdown();

0 commit comments

Comments
 (0)