This repository was archived by the owner on Apr 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
chore: add routing hints and cache updates for begin/commit #4392
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
82c3244
chore(spanner): add routing hints and cache updates for begin/commit
rahul2393 3c99d2a
chore: generate libraries at Wed Mar 25 07:01:43 UTC 2026
cloud-java-bot e836a5d
incorporate suggestions
rahul2393 0f39595
use same heuristic for selecting mutation for commit routing which we…
rahul2393 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| import com.google.protobuf.ByteString; | ||
| import com.google.spanner.v1.BeginTransactionRequest; | ||
| import com.google.spanner.v1.CommitRequest; | ||
| import com.google.spanner.v1.CommitResponse; | ||
| import com.google.spanner.v1.ExecuteSqlRequest; | ||
| import com.google.spanner.v1.PartialResultSet; | ||
| import com.google.spanner.v1.ReadRequest; | ||
|
|
@@ -47,9 +48,10 @@ | |
| /** | ||
| * ManagedChannel that routes eligible requests using location-aware routing hints. | ||
| * | ||
| * <p>Routing hints are applied to streaming read/query and unary ExecuteSql. Commit/Rollback use | ||
| * transaction affinity when available. BeginTransaction is routed only when a mutation key is | ||
| * provided. | ||
| * <p>Routing hints are applied to streaming read/query and unary ExecuteSql. Mutation-based | ||
| * BeginTransaction and Commit requests also carry routing hints when recipes are available. | ||
| * Commit/Rollback use transaction affinity when available. BeginTransaction is routed only when a | ||
| * mutation key is provided. | ||
| */ | ||
| @InternalApi | ||
| final class KeyAwareChannel extends ManagedChannel { | ||
|
|
@@ -355,8 +357,10 @@ public void sendMessage(RequestT message) { | |
| BeginTransactionRequest.Builder reqBuilder = | ||
| ((BeginTransactionRequest) message).toBuilder(); | ||
| String databaseId = parentChannel.extractDatabaseIdFromSession(reqBuilder.getSession()); | ||
| if (databaseId != null && reqBuilder.hasMutationKey()) { | ||
| if (databaseId != null) { | ||
| finder = parentChannel.getOrCreateChannelFinder(databaseId); | ||
| } | ||
| if (finder != null && reqBuilder.hasMutationKey()) { | ||
| endpoint = finder.findServer(reqBuilder); | ||
| } | ||
| if (reqBuilder.hasOptions() && reqBuilder.getOptions().hasReadOnly()) { | ||
|
|
@@ -368,10 +372,23 @@ public void sendMessage(RequestT message) { | |
| message = (RequestT) reqBuilder.build(); | ||
| } else if (message instanceof CommitRequest) { | ||
| CommitRequest request = (CommitRequest) message; | ||
| String databaseId = parentChannel.extractDatabaseIdFromSession(request.getSession()); | ||
| if (databaseId != null) { | ||
| finder = parentChannel.getOrCreateChannelFinder(databaseId); | ||
| } | ||
| CommitRequest.Builder reqBuilder = null; | ||
| if (finder != null && request.getMutationsCount() > 0) { | ||
| reqBuilder = request.toBuilder(); | ||
| endpoint = finder.fillRoutingHint(reqBuilder); | ||
| request = reqBuilder.build(); | ||
| } | ||
| if (!request.getTransactionId().isEmpty()) { | ||
| endpoint = parentChannel.affinityEndpoint(request.getTransactionId()); | ||
| transactionIdToClear = request.getTransactionId(); | ||
| } | ||
|
Comment on lines
+376
to
+392
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if (reqBuilder != null) { | ||
| message = (RequestT) request; | ||
| } | ||
| } else if (message instanceof RollbackRequest) { | ||
| RollbackRequest request = (RollbackRequest) message; | ||
| if (!request.getTransactionId().isEmpty()) { | ||
|
|
@@ -610,7 +627,15 @@ public void onMessage(ResponseT message) { | |
| transactionId = transactionIdFromMetadata(response); | ||
| } else if (message instanceof Transaction) { | ||
| Transaction response = (Transaction) message; | ||
| if (response.hasCacheUpdate() && call.channelFinder != null) { | ||
| call.channelFinder.update(response.getCacheUpdate()); | ||
| } | ||
| transactionId = transactionIdFromTransaction(response); | ||
| } else if (message instanceof CommitResponse) { | ||
| CommitResponse response = (CommitResponse) message; | ||
| if (response.hasCacheUpdate() && call.channelFinder != null) { | ||
| call.channelFinder.update(response.getCacheUpdate()); | ||
| } | ||
| } | ||
| if (transactionId != null) { | ||
| if (call.isReadOnlyBegin) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,9 +158,7 @@ public void computeKeys(ReadRequest.Builder reqBuilder) { | |
| long reqFp = fingerprint(reqBuilder.buildPartial()); | ||
|
|
||
| RoutingHint.Builder hintBuilder = reqBuilder.getRoutingHintBuilder(); | ||
| if (!schemaGeneration.isEmpty()) { | ||
| hintBuilder.setSchemaGeneration(schemaGeneration); | ||
| } | ||
| applySchemaGeneration(hintBuilder); | ||
|
|
||
| PreparedRead preparedRead = getIfPresent(preparedReads, reqFp); | ||
| if (preparedRead == null) { | ||
|
|
@@ -186,10 +184,7 @@ public void computeKeys(ReadRequest.Builder reqBuilder) { | |
|
|
||
| try { | ||
| TargetRange target = recipe.keySetToTargetRange(reqBuilder.getKeySet()); | ||
| hintBuilder.setKey(target.start); | ||
| if (!target.limit.isEmpty()) { | ||
| hintBuilder.setLimitKey(target.limit); | ||
| } | ||
| applyTargetRange(hintBuilder, target); | ||
| } catch (IllegalArgumentException e) { | ||
| logger.fine("Failed key encoding: " + e.getMessage()); | ||
| } | ||
|
|
@@ -199,9 +194,7 @@ public void computeKeys(ExecuteSqlRequest.Builder reqBuilder) { | |
| long reqFp = fingerprint(reqBuilder.buildPartial()); | ||
|
|
||
| RoutingHint.Builder hintBuilder = reqBuilder.getRoutingHintBuilder(); | ||
| if (!schemaGeneration.isEmpty()) { | ||
| hintBuilder.setSchemaGeneration(schemaGeneration); | ||
| } | ||
| applySchemaGeneration(hintBuilder); | ||
|
|
||
| PreparedQuery preparedQuery = getIfPresent(preparedQueries, reqFp); | ||
| if (preparedQuery == null) { | ||
|
|
@@ -221,15 +214,25 @@ public void computeKeys(ExecuteSqlRequest.Builder reqBuilder) { | |
|
|
||
| try { | ||
| TargetRange target = recipe.queryParamsToTargetRange(reqBuilder.getParams()); | ||
| hintBuilder.setKey(target.start); | ||
| if (!target.limit.isEmpty()) { | ||
| hintBuilder.setLimitKey(target.limit); | ||
| } | ||
| applyTargetRange(hintBuilder, target); | ||
| } catch (IllegalArgumentException e) { | ||
| logger.fine("Failed query param encoding: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| void applySchemaGeneration(RoutingHint.Builder hintBuilder) { | ||
| if (!schemaGeneration.isEmpty()) { | ||
| hintBuilder.setSchemaGeneration(schemaGeneration); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This updates the hint builder, but the method might still return false if the |
||
| } | ||
| } | ||
|
|
||
| void applyTargetRange(RoutingHint.Builder hintBuilder, TargetRange target) { | ||
| hintBuilder.setKey(target.start); | ||
| if (!target.limit.isEmpty()) { | ||
| hintBuilder.setLimitKey(target.limit); | ||
| } | ||
| } | ||
|
|
||
| public TargetRange mutationToTargetRange(Mutation mutation) { | ||
| if (mutation == null) { | ||
| return null; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overrides the endpoint that was filled above, even in the case if
parentChannel.affinityEndpoint(..)returns null, which means that it will fall back to the default endpoint. Is that intentional? (I'm not sure if it can happen in real life, though....)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case when transactionID is present but no affinity, we should just forward it to the default channel, example
For ExecuteSql / Read, if the first statement falls back to the default endpoint, we do not record default-host affinity because allowDefaultAffinity is false on that path. That means a later CommitRequest can carry a valid transactionId while affinityEndpoint still returns null, in this case we should just forward to default host.