Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner): set manual affinity incase of gRPC-GCP extenstion #3215

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.google.api.pathtemplate.PathTemplate;
import com.google.cloud.RetryHelper;
import com.google.cloud.RetryHelper.RetryHelperException;
import com.google.cloud.grpc.GcpManagedChannel;
import com.google.cloud.grpc.GcpManagedChannelBuilder;
import com.google.cloud.grpc.GcpManagedChannelOptions;
import com.google.cloud.grpc.GcpManagedChannelOptions.GcpMetricsOptions;
Expand Down Expand Up @@ -266,6 +267,7 @@ public class GapicSpannerRpc implements SpannerRpc {
private static final ConcurrentMap<String, RateLimiter> ADMINISTRATIVE_REQUESTS_RATE_LIMITERS =
new ConcurrentHashMap<>();
private final boolean leaderAwareRoutingEnabled;
private final int numChannels;

public static GapicSpannerRpc create(SpannerOptions options) {
return new GapicSpannerRpc(options);
Expand Down Expand Up @@ -317,6 +319,7 @@ public GapicSpannerRpc(final SpannerOptions options) {
this.callCredentialsProvider = options.getCallCredentialsProvider();
this.compressorName = options.getCompressorName();
this.leaderAwareRoutingEnabled = options.isLeaderAwareRoutingEnabled();
this.numChannels = options.getNumChannels();

if (initializeStubs) {
// First check if SpannerOptions provides a TransportChannelProvider. Create one
Expand Down Expand Up @@ -1950,7 +1953,17 @@ <ReqT, RespT> GrpcCallContext newCallContext(
boolean routeToLeader) {
GrpcCallContext context = GrpcCallContext.createDefault();
if (options != null) {
// Set channel affinity in GAX. This is a no-op for gRPC-GCP.
context = context.withChannelAffinity(Option.CHANNEL_HINT.getLong(options).intValue());

// Set channel affinity in gRPC-GCP. This is a no-op for GAX.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is it possible to only do this if grpc-gcp is being used? I know that it is a no-op for gax, and so does not affect anything in that way, but context.withCallOptions(..) always creates a new CallContext instance. That means that we reserve a small bit of additional memory for every RPC that we invoke that then needs to be cleaned up again afterwards. (And yes, that's a bit of a pre-emptive optimization, so if it is not possible or hard, then please ignore this comment, but if there's a simple if-condition that we can use, then let's do that.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made changes to do it only when grpcgcp is enabled. Can you please take a look?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// Compute bounded channel hint to prevent gRPC-GCP affinity map from getting unbounded.
int boundedChannelHint = Option.CHANNEL_HINT.getLong(options).intValue() % this.numChannels;
context =
context.withCallOptions(
context
.getCallOptions()
.withOption(GcpManagedChannel.AFFINITY_KEY, String.valueOf(boundedChannelHint)));
}
if (compressorName != null) {
// This sets the compressor for Client -> Server.
Expand Down
Loading