Skip to content

Conversation

harshachinta
Copy link
Contributor

@harshachinta harshachinta commented Jul 17, 2024

Earlier, gRPC-GCP maintains affinity using the ApiConfig by looking into the gRPC messages to get an affinity key.

This PR sets the affinity manually to gRPC-GCP via call options. Reason - With multiplexed sessions we need to maintain transaction to channel affinity, and with regular sessions we need to maintain session to channel affinity.
Setting manual affinity will override the API config setting.

References:

  1. Java client design and benchmarking - https://docs.google.com/document/d/1iQwAGCwvatdoGxvfucd0eFAysx6roGMxJsoyygX79NY/edit?resourcekey=0-feEwEwbAPxsA-QydfaG53A&tab=t.0
  2. gRPC-GCP manual affinity design - https://docs.google.com/document/d/1NfnaCHpE6EzePU_IBTZHvevGizYTBtjuMfHlm9SZ70w/edit?tab=t.0

@harshachinta harshachinta requested a review from a team as a code owner July 17, 2024 10:25
@product-auto-label product-auto-label bot added size: s Pull request size is small. api: spanner Issues related to the googleapis/java-spanner API. labels Jul 17, 2024
private static GcpManagedChannelOptions grpcGcpOptionsWithMetrics(SpannerOptions options) {
GcpManagedChannelOptions grpcGcpOptions =
MoreObjects.firstNonNull(options.getGrpcGcpOptions(), new GcpManagedChannelOptions());
GcpChannelPoolOptions gcpChanelPoolOptions =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
GcpChannelPoolOptions gcpChanelPoolOptions =
GcpChannelPoolOptions gcpChannelPoolOptions =

.getCallOptions()
.withOption(
GcpManagedChannel.AFFINITY_KEY,
Option.CHANNEL_HINT.getLong(options).toString()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This channel hint can be any random Long, meaning that we might be using a very large range of different channel hints. That again means that we might be adding an unbounded set of channel hints to the map in grpc-gcp. We should add a computation here that makes sure that the set of hints is limited (e.g. do something like hint % options.getNumChannels()).

See

// 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/java-spanner API. size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants