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

Commit 63f0188

Browse files
committed
add ability to disable grpc-gcp if needed
1 parent eaeef52 commit 63f0188

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public class SpannerOptions extends ServiceOptions<Spanner, SpannerOptions> {
151151
private final InstanceAdminStubSettings instanceAdminStubSettings;
152152
private final DatabaseAdminStubSettings databaseAdminStubSettings;
153153
private final Duration partitionedDmlTimeout;
154+
private final boolean grpcGcpExtensionEnabled;
154155
private final GcpManagedChannelOptions grpcGcpOptions;
155156
private final boolean autoThrottleAdministrativeRequests;
156157
private final RetrySettings retryAdministrativeRequestsSettings;
@@ -797,6 +798,7 @@ protected SpannerOptions(Builder builder) {
797798
throw SpannerExceptionFactory.newSpannerException(e);
798799
}
799800
partitionedDmlTimeout = builder.partitionedDmlTimeout;
801+
grpcGcpExtensionEnabled = builder.grpcGcpExtensionEnabled;
800802
grpcGcpOptions = builder.grpcGcpOptions;
801803
autoThrottleAdministrativeRequests = builder.autoThrottleAdministrativeRequests;
802804
retryAdministrativeRequestsSettings = builder.retryAdministrativeRequestsSettings;
@@ -1023,6 +1025,7 @@ public static class Builder
10231025
private DatabaseAdminStubSettings.Builder databaseAdminStubSettingsBuilder =
10241026
DatabaseAdminStubSettings.newBuilder();
10251027
private Duration partitionedDmlTimeout = Duration.ofHours(2L);
1028+
private boolean grpcGcpExtensionEnabled = true;
10261029
private GcpManagedChannelOptions grpcGcpOptions;
10271030
private RetrySettings retryAdministrativeRequestsSettings =
10281031
DEFAULT_ADMIN_REQUESTS_LIMIT_EXCEEDED_RETRY_SETTINGS;
@@ -1094,6 +1097,7 @@ protected Builder() {
10941097
this.instanceAdminStubSettingsBuilder = options.instanceAdminStubSettings.toBuilder();
10951098
this.databaseAdminStubSettingsBuilder = options.databaseAdminStubSettings.toBuilder();
10961099
this.partitionedDmlTimeout = options.partitionedDmlTimeout;
1100+
this.grpcGcpExtensionEnabled = options.grpcGcpExtensionEnabled;
10971101
this.grpcGcpOptions = options.grpcGcpOptions;
10981102
this.autoThrottleAdministrativeRequests = options.autoThrottleAdministrativeRequests;
10991103
this.retryAdministrativeRequestsSettings = options.retryAdministrativeRequestsSettings;
@@ -1569,12 +1573,14 @@ public Builder enableGrpcGcpExtension() {
15691573
* Multiplexed sessions are not supported for gRPC-GCP.
15701574
*/
15711575
public Builder enableGrpcGcpExtension(GcpManagedChannelOptions options) {
1576+
this.grpcGcpExtensionEnabled = true;
15721577
this.grpcGcpOptions = options;
15731578
return this;
15741579
}
15751580

1576-
/** Disables gRPC-GCP extension. */
1581+
/** Disables gRPC-GCP extension and uses GAX channel pool instead. */
15771582
public Builder disableGrpcGcpExtension() {
1583+
this.grpcGcpExtensionEnabled = false;
15781584
return this;
15791585
}
15801586

@@ -1787,7 +1793,8 @@ public SpannerOptions build() {
17871793
credentials = environment.getDefaultExperimentalHostCredentials();
17881794
}
17891795
if (this.numChannels == null) {
1790-
this.numChannels = GRPC_GCP_ENABLED_DEFAULT_CHANNELS;
1796+
this.numChannels =
1797+
this.grpcGcpExtensionEnabled ? GRPC_GCP_ENABLED_DEFAULT_CHANNELS : DEFAULT_CHANNELS;
17911798
}
17921799

17931800
synchronized (lock) {
@@ -1982,7 +1989,7 @@ public Duration getPartitionedDmlTimeoutDuration() {
19821989
}
19831990

19841991
public boolean isGrpcGcpExtensionEnabled() {
1985-
return true;
1992+
return grpcGcpExtensionEnabled;
19861993
}
19871994

19881995
public GcpManagedChannelOptions getGrpcGcpOptions() {

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public class GapicSpannerRpc implements SpannerRpc {
279279
private final boolean leaderAwareRoutingEnabled;
280280
private final boolean endToEndTracingEnabled;
281281
private final int numChannels;
282+
private final boolean isGrpcGcpExtensionEnabled;
282283

283284
private final GrpcCallContext baseGrpcCallContext;
284285

@@ -334,6 +335,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
334335
this.leaderAwareRoutingEnabled = options.isLeaderAwareRoutingEnabled();
335336
this.endToEndTracingEnabled = options.isEndToEndTracingEnabled();
336337
this.numChannels = options.getNumChannels();
338+
this.isGrpcGcpExtensionEnabled = options.isGrpcGcpExtensionEnabled();
337339
this.baseGrpcCallContext = createBaseCallContext();
338340

339341
if (initializeStubs) {
@@ -590,6 +592,10 @@ private static GcpManagedChannelOptions grpcGcpOptionsWithMetrics(SpannerOptions
590592
private static void maybeEnableGrpcGcpExtension(
591593
InstantiatingGrpcChannelProvider.Builder defaultChannelProviderBuilder,
592594
final SpannerOptions options) {
595+
if (!options.isGrpcGcpExtensionEnabled()) {
596+
return;
597+
}
598+
593599
final String jsonApiConfig = parseGrpcGcpApiConfig();
594600
final GcpManagedChannelOptions grpcGcpOptions = grpcGcpOptionsWithMetrics(options);
595601

@@ -2031,14 +2037,20 @@ <ReqT, RespT> GrpcCallContext newCallContext(
20312037
GrpcCallContext context = this.baseGrpcCallContext;
20322038
Long affinity = options == null ? null : Option.CHANNEL_HINT.getLong(options);
20332039
if (affinity != null) {
2034-
// Set channel affinity in gRPC-GCP.
2035-
// Compute bounded channel hint to prevent gRPC-GCP affinity map from getting unbounded.
2036-
int boundedChannelHint = affinity.intValue() % this.numChannels;
2037-
context =
2038-
context.withCallOptions(
2039-
context
2040-
.getCallOptions()
2041-
.withOption(GcpManagedChannel.AFFINITY_KEY, String.valueOf(boundedChannelHint)));
2040+
if (this.isGrpcGcpExtensionEnabled) {
2041+
// Set channel affinity in gRPC-GCP.
2042+
// Compute bounded channel hint to prevent gRPC-GCP affinity map from getting unbounded.
2043+
int boundedChannelHint = affinity.intValue() % this.numChannels;
2044+
context =
2045+
context.withCallOptions(
2046+
context
2047+
.getCallOptions()
2048+
.withOption(
2049+
GcpManagedChannel.AFFINITY_KEY, String.valueOf(boundedChannelHint)));
2050+
} else {
2051+
// Set channel affinity in GAX.
2052+
context = context.withChannelAffinity(affinity.intValue());
2053+
}
20422054
}
20432055
if (options != null) {
20442056
// TODO(@odeke-em): Infer the affinity if it doesn't match up with in the request-id.

0 commit comments

Comments
 (0)