Skip to content

chore(spanner): implement latency-aware replica selection and scoped tracking - #6572

Merged
olavloite merged 4 commits into
googleapis:mainfrom
olavloite:spanner-location-aware-replica-selection
Aug 28, 2026
Merged

chore(spanner): implement latency-aware replica selection and scoped tracking#6572
olavloite merged 4 commits into
googleapis:mainfrom
olavloite:spanner-location-aware-replica-selection

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Implement Power of Two Choices (P2C) replica selection in LocationRouter weighted by EWMA latency and active in-flight request count:

  • Encapsulate database_scope: String within LocationRouter so recording and replica cost queries are strictly and immutably scoped to the database.
  • Add connection presence and health checks to is_tablet_routable so P2C only considers pre-warmed and ready candidate replicas, avoiding false fallbacks to the default gateway.
  • Expose record_latency and record_routing_error on DatabaseClient.

@olavloite
olavloite requested review from a team as code owners August 28, 2026 08:16
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Aug 28, 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 integrates the LatencyRegistry and a Power of Two Choices (P2C) replica selector into the LocationRouter and DatabaseClient to enable latency-aware routing for Spanner. It introduces methods to record latency and routing errors, and updates connection resolution to select healthy replicas weighted by EWMA latency and active request counts. Feedback suggests optimizing select_healthy_tablet to perform a single lookup in the ConnectionCache instead of querying it twice per replica, which reduces lock contention.

Comment thread src/spanner/src/routing/location_router.rs Outdated
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.44979% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.60%. Comparing base (2808196) to head (815ac0e).

Files with missing lines Patch % Lines
src/spanner/src/routing/key_range_cache.rs 0.00% 3 Missing ⚠️
src/spanner/src/routing/location_router.rs 99.82% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6572      +/-   ##
==========================================
+ Coverage   96.58%   96.60%   +0.01%     
==========================================
  Files         304      304              
  Lines       91285    91987     +702     
==========================================
+ Hits        88165    88861     +696     
- Misses       3120     3126       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…tracking

Implement Power of Two Choices (P2C) replica selection in LocationRouter
weighted by EWMA latency and active in-flight request count:
- Encapsulate `database_scope: String` within `LocationRouter` so recording
  and replica cost queries are strictly and immutably scoped to the database.
- Add connection presence and health checks to `is_tablet_routable` so P2C
  only considers pre-warmed and ready candidate replicas, avoiding false
  fallbacks to the default gateway.
- Expose `record_latency` and `record_routing_error` on `DatabaseClient`.
@olavloite
olavloite force-pushed the spanner-location-aware-replica-selection branch from fa7b666 to f69b9c8 Compare August 28, 2026 09:35
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 integrates latency-aware routing and replica selection into the Spanner database client. It introduces a LatencyRegistry and uses a Power of Two Choices (P2C) selector within the LocationRouter to dynamically route requests to healthy replicas based on EWMA latency and active in-flight requests. Feedback is provided to optimize the hot path in select_healthy_replica by sampling candidate indices first, which avoids heap allocations and unnecessary connection cloning.

Comment thread src/spanner/src/routing/location_router.rs Outdated
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 integrates latency-aware routing into the Spanner database client. It introduces latency and error penalty recording on the DatabaseClient and updates the LocationRouter to perform Power-of-Two-Choice (P2C) replica selection weighted by EWMA latency and active in-flight requests. Key updates include the addition of LatencyRegistry and PowerOfTwoSelector integrations, stack-allocated candidate buffers to avoid heap allocations on the hot path, and comprehensive unit tests. Feedback on the changes suggests defining a constant for the magic number 8 used in the stack buffer array initialization and using a slice instead of a fixed-size array reference in select_p2c_winner to make the code more idiomatic.

Comment thread src/spanner/src/routing/location_router.rs Outdated
Comment thread src/spanner/src/routing/location_router.rs Outdated
@olavloite
olavloite force-pushed the spanner-location-aware-replica-selection branch from 539518b to b06d4da Compare August 28, 2026 12:16
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 integrates latency-aware routing and replica selection into the Spanner client. It introduces a LatencyRegistry to track round-trip latency samples and RPC error penalties scoped by database. The LocationRouter is updated to perform Power-of-Two-Choice (P2C) replica selection among healthy follower replicas, weighted by EWMA latency and active in-flight requests, using a stack-allocated buffer to avoid heap allocations on the hot path. Comprehensive unit and integration tests are added to verify the routing, latency recording, and P2C selection behavior. I have no feedback to provide as the implementation is clean, well-tested, and conforms to the repository's style guidelines.

@olavloite
olavloite merged commit 836e762 into googleapis:main Aug 28, 2026
42 checks passed
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