Skip to content

Commit 86b306a

Browse files
authored
feat(spanner): set manual affinity incase of gRPC-GCP extenstion (#3215)
* feat(spanner): set manual affinity incase of gRPC-GCP extenstion * feat(spanner): convert channel hint to be bounded * feat(spanner): check if extension is enabled
1 parent c95d61b commit 86b306a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.google.api.pathtemplate.PathTemplate;
5757
import com.google.cloud.RetryHelper;
5858
import com.google.cloud.RetryHelper.RetryHelperException;
59+
import com.google.cloud.grpc.GcpManagedChannel;
5960
import com.google.cloud.grpc.GcpManagedChannelBuilder;
6061
import com.google.cloud.grpc.GcpManagedChannelOptions;
6162
import com.google.cloud.grpc.GcpManagedChannelOptions.GcpMetricsOptions;
@@ -267,6 +268,8 @@ public class GapicSpannerRpc implements SpannerRpc {
267268
private static final ConcurrentMap<String, RateLimiter> ADMINISTRATIVE_REQUESTS_RATE_LIMITERS =
268269
new ConcurrentHashMap<>();
269270
private final boolean leaderAwareRoutingEnabled;
271+
private final int numChannels;
272+
private final boolean isGrpcGcpExtensionEnabled;
270273

271274
public static GapicSpannerRpc create(SpannerOptions options) {
272275
return new GapicSpannerRpc(options);
@@ -318,6 +321,8 @@ public GapicSpannerRpc(final SpannerOptions options) {
318321
this.callCredentialsProvider = options.getCallCredentialsProvider();
319322
this.compressorName = options.getCompressorName();
320323
this.leaderAwareRoutingEnabled = options.isLeaderAwareRoutingEnabled();
324+
this.numChannels = options.getNumChannels();
325+
this.isGrpcGcpExtensionEnabled = options.isGrpcGcpExtensionEnabled();
321326

322327
if (initializeStubs) {
323328
// First check if SpannerOptions provides a TransportChannelProvider. Create one
@@ -1960,7 +1965,20 @@ <ReqT, RespT> GrpcCallContext newCallContext(
19601965
boolean routeToLeader) {
19611966
GrpcCallContext context = GrpcCallContext.createDefault();
19621967
if (options != null) {
1963-
context = context.withChannelAffinity(Option.CHANNEL_HINT.getLong(options).intValue());
1968+
if (this.isGrpcGcpExtensionEnabled) {
1969+
// Set channel affinity in gRPC-GCP.
1970+
// Compute bounded channel hint to prevent gRPC-GCP affinity map from getting unbounded.
1971+
int boundedChannelHint = Option.CHANNEL_HINT.getLong(options).intValue() % this.numChannels;
1972+
context =
1973+
context.withCallOptions(
1974+
context
1975+
.getCallOptions()
1976+
.withOption(
1977+
GcpManagedChannel.AFFINITY_KEY, String.valueOf(boundedChannelHint)));
1978+
} else {
1979+
// Set channel affinity in GAX.
1980+
context = context.withChannelAffinity(Option.CHANNEL_HINT.getLong(options).intValue());
1981+
}
19641982
}
19651983
if (compressorName != null) {
19661984
// This sets the compressor for Client -> Server.

0 commit comments

Comments
 (0)