Skip to content

Commit 44f27fc

Browse files
harshachintarajatbhattagcf-owl-bot[bot]
authored
feat: increase default number of channels when gRPC-GCP channel pool is enabled (#1997)
* increase default number of channels when grpc-gcp channel pool is * 🦉 Updates from OwlBot post-processor Co-authored-by: Rajat Bhatta <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent f62c659 commit 44f27fc

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public class SpannerOptions extends ServiceOptions<Spanner, SpannerOptions> {
8989
"https://www.googleapis.com/auth/spanner.admin",
9090
"https://www.googleapis.com/auth/spanner.data");
9191
private static final int MAX_CHANNELS = 256;
92+
@VisibleForTesting static final int DEFAULT_CHANNELS = 4;
93+
// Set the default number of channels to GRPC_GCP_ENABLED_DEFAULT_CHANNELS when gRPC-GCP extension
94+
// is enabled, to make sure there are sufficient channels available to move the sessions to a
95+
// different channel if a network connection in a particular channel fails.
96+
@VisibleForTesting static final int GRPC_GCP_ENABLED_DEFAULT_CHANNELS = 8;
9297
private final TransportChannelProvider channelProvider;
9398

9499
@SuppressWarnings("rawtypes")
@@ -669,8 +674,7 @@ public static class Builder
669674

670675
private GrpcInterceptorProvider interceptorProvider;
671676

672-
/** By default, we create 4 channels per {@link SpannerOptions} */
673-
private int numChannels = 4;
677+
private Integer numChannels;
674678

675679
private String transportChannelExecutorThreadNameFormat = "Cloud-Spanner-TransportChannel-%d";
676680

@@ -1122,8 +1126,7 @@ public Builder setHost(String host) {
11221126

11231127
/** Enables gRPC-GCP extension with the default settings. */
11241128
public Builder enableGrpcGcpExtension() {
1125-
this.grpcGcpExtensionEnabled = true;
1126-
return this;
1129+
return this.enableGrpcGcpExtension(null);
11271130
}
11281131

11291132
/**
@@ -1166,6 +1169,11 @@ public SpannerOptions build() {
11661169
// As we are using plain text, we should never send any credentials.
11671170
this.setCredentials(NoCredentials.getInstance());
11681171
}
1172+
if (this.numChannels == null) {
1173+
this.numChannels =
1174+
this.grpcGcpExtensionEnabled ? GRPC_GCP_ENABLED_DEFAULT_CHANNELS : DEFAULT_CHANNELS;
1175+
}
1176+
11691177
return new SpannerOptions(this);
11701178
}
11711179
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java

+69
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.hamcrest.CoreMatchers.is;
2121
import static org.hamcrest.MatcherAssert.assertThat;
2222
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertNotSame;
2324
import static org.junit.Assert.assertSame;
2425
import static org.junit.Assert.assertThrows;
2526
import static org.mockito.Mockito.mock;
@@ -918,4 +919,72 @@ public void testCustomAsyncExecutorProvider() {
918919
.build();
919920
assertSame(service, options.getAsyncExecutorProvider().getExecutor());
920921
}
922+
923+
@Test
924+
public void testDefaultNumChannelsWithGrpcGcpExtensionEnabled() {
925+
SpannerOptions options =
926+
SpannerOptions.newBuilder().setProjectId("test-project").enableGrpcGcpExtension().build();
927+
928+
assertEquals(SpannerOptions.GRPC_GCP_ENABLED_DEFAULT_CHANNELS, options.getNumChannels());
929+
}
930+
931+
@Test
932+
public void testDefaultNumChannelsWithGrpcGcpExtensionDisabled() {
933+
SpannerOptions options = SpannerOptions.newBuilder().setProjectId("test-project").build();
934+
935+
assertEquals(SpannerOptions.DEFAULT_CHANNELS, options.getNumChannels());
936+
}
937+
938+
@Test
939+
public void testNumChannelsWithGrpcGcpExtensionEnabled() {
940+
// Set number of channels explicitly, before enabling gRPC-GCP channel pool in SpannerOptions
941+
// builder.
942+
int numChannels = 5;
943+
SpannerOptions options1 =
944+
SpannerOptions.newBuilder()
945+
.setProjectId("test-project")
946+
.setNumChannels(numChannels)
947+
.enableGrpcGcpExtension()
948+
.build();
949+
950+
assertEquals(numChannels, options1.getNumChannels());
951+
952+
// Set number of channels explicitly, after enabling gRPC-GCP channel pool in SpannerOptions
953+
// builder.
954+
SpannerOptions options2 =
955+
SpannerOptions.newBuilder()
956+
.setProjectId("test-project")
957+
.enableGrpcGcpExtension()
958+
.setNumChannels(numChannels)
959+
.build();
960+
961+
assertEquals(numChannels, options2.getNumChannels());
962+
}
963+
964+
@Test
965+
public void checkCreatedInstanceWhenGrpcGcpExtensionDisabled() {
966+
SpannerOptions options = SpannerOptions.newBuilder().setProjectId("test-project").build();
967+
SpannerOptions options1 = options.toBuilder().build();
968+
assertEquals(false, options.isGrpcGcpExtensionEnabled());
969+
assertEquals(options.isGrpcGcpExtensionEnabled(), options1.isGrpcGcpExtensionEnabled());
970+
971+
Spanner spanner1 = options.getService();
972+
Spanner spanner2 = options1.getService();
973+
974+
assertNotSame(spanner1, spanner2);
975+
}
976+
977+
@Test
978+
public void checkCreatedInstanceWhenGrpcGcpExtensionEnabled() {
979+
SpannerOptions options =
980+
SpannerOptions.newBuilder().setProjectId("test-project").enableGrpcGcpExtension().build();
981+
SpannerOptions options1 = options.toBuilder().build();
982+
assertEquals(true, options.isGrpcGcpExtensionEnabled());
983+
assertEquals(options.isGrpcGcpExtensionEnabled(), options1.isGrpcGcpExtensionEnabled());
984+
985+
Spanner spanner1 = options.getService();
986+
Spanner spanner2 = options1.getService();
987+
988+
assertNotSame(spanner1, spanner2);
989+
}
921990
}

0 commit comments

Comments
 (0)