Skip to content

fix(spanner): prioritize leader replica for read-write transactions in location-aware routing - #20620

Merged
rahul2393 merged 1 commit into
mainfrom
fm/spanner-rw-leader-routing
Oct 1, 2026
Merged

rahul2393 merged 1 commit into
mainfrom
fm/spanner-rw-leader-routing

Conversation

@rahul2393

Copy link
Copy Markdown
Contributor

When location-aware routing is enabled, read-write requests that prefer the leader could be routed to a follower because a non-zero operation UID on the routing hint sent fillRoutingHintAttempt down the score-aware replica selection path before the local leader was considered. There the leader only receives a cost bias, so a sufficiently faster follower would win.

Check for a healthy local leader first whenever preferLeader is set, and fall back to score-aware selection only when the leader is missing, remote, or unhealthy. This mirrors googleapis/google-cloud-java#14195.

…n location-aware routing

When location-aware routing is enabled, read-write requests that prefer the
leader could be routed to a follower because a non-zero operation UID on the
routing hint sent fillRoutingHintAttempt down the score-aware replica selection
path before the local leader was considered. There the leader only receives a
cost bias, so a sufficiently faster follower would win.

Check for a healthy local leader first whenever preferLeader is set, and fall
back to score-aware selection only when the leader is missing, remote, or
unhealthy. This mirrors googleapis/google-cloud-java#14195.
@rahul2393
rahul2393 requested review from a team as code owners October 1, 2026 16:10
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Oct 1, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request modifies the routing logic in spanner/key_range_cache.go to ensure that a healthy local leader is always selected when a request prefers the leader, even if latency scores are available. This prevents read-write transactions from being incorrectly routed to followers. Additionally, corresponding unit tests have been added in spanner/key_range_cache_test.go to verify this behavior and its fallback mechanism when the leader is unhealthy. There are no review comments to address, so no further feedback is provided.

@rahul2393
rahul2393 merged commit a524d66 into main Oct 1, 2026
17 checks passed
@rahul2393
rahul2393 deleted the fm/spanner-rw-leader-routing branch October 1, 2026 16:33
rahul2393 pushed a commit that referenced this pull request Oct 7, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.96.0](spanner/v1.95.1...spanner/v1.96.0)
(2026-10-07)


### Features

* **all:** Update supported go versions
([#20520](#20520))
([90413d3](90413d3))
* **spanner:** Add experimental vtprotobuf decoding with
ExperimentalBorrowRows under the spanner_vtproto build tag
([#20642](#20642))
([fdbd4f4](fdbd4f4))


### Bug Fixes

* **spanner:** Keep DCP transaction on its channel while it drains
([#20618](#20618))
([93dfc20](93dfc20))
* **spanner:** Prioritize leader replica for read-write transactions in
location-aware routing
([#20620](#20620))
([a524d66](a524d66))
* **spanner:** Record streaming built-in metrics per result stream
([#20596](#20596))
([be75f7e](be75f7e))
* **spanner:** Retry and resume BatchWrite streams on UNAVAILABLE
([#20623](#20623))
([a302277](a302277))


### Performance Improvements

* **spanner:** Skip builtin metrics tracer allocation when metrics are
disabled
([#20582](#20582))
([9971a0c](9971a0c))
* **spanner:** Skip reading response headers when no latency sink is
enabled
([#20583](#20583))
([3dcc2c3](3dcc2c3))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
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 Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants