Skip to content

chore(spanner): implement server connection pool for location-aware routing - #6236

Open
olavloite wants to merge 1 commit into
googleapis:mainfrom
olavloite:spanner-channel-endpoint-cache
Open

chore(spanner): implement server connection pool for location-aware routing#6236
olavloite wants to merge 1 commit into
googleapis:mainfrom
olavloite:spanner-channel-endpoint-cache

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

Add ServerConnection and ConnectionCache manage server connections and inflight request metrics for location-aware routing.

@olavloite
olavloite requested review from a team as code owners July 30, 2026 16:04
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Jul 30, 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 introduces location-aware routing and endpoint connection pooling for Spanner clients by adding a thread-safe ConnectionCache and a ServerConnection wrapper to track health states and active requests. The reviewer provided several key optimization suggestions: making ActiveRequestGuard 'static by owning ServerConnection to better support async contexts, replacing the lock-free CAS loop with a simpler fetch_sub to avoid CPU contention, using proper Acquire/Release memory ordering for health state transitions, and utilizing tokio::sync::OnceCell in the connection cache to prevent concurrent cache stampedes.

Comment thread src/spanner/src/routing/server_connection.rs
Comment thread src/spanner/src/routing/server_connection.rs Outdated
Comment thread src/spanner/src/routing/server_connection.rs
Comment thread src/spanner/src/routing/server_connection.rs
Comment thread src/spanner/src/routing/connection_cache.rs
@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.07%. Comparing base (8581760) to head (2914b1f).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6236      +/-   ##
==========================================
+ Coverage   96.06%   96.07%   +0.01%     
==========================================
  Files         269      271       +2     
  Lines       68394    68764     +370     
==========================================
+ Hits        65700    66067     +367     
- Misses       2694     2697       +3     

☔ 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.

@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from 1a72834 to bf08b60 Compare July 30, 2026 16:34
@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 introduces thread-safe connection caching and server connection tracking for Spanner location-aware routing. It adds a ConnectionCache to manage server connections and a ServerConnection wrapper to track health states and active inflight requests. The review feedback highlights a critical underflow risk in decrement_active_requests where decrementing a zero count wraps around to usize::MAX, suggesting a lock-free saturating subtraction instead, along with a corresponding test case to verify underflow protection.

Comment thread src/spanner/src/routing/server_connection.rs
Comment thread src/spanner/src/routing/server_connection.rs
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from bf08b60 to 50375aa Compare July 30, 2026 16:49
@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 introduces location-aware routing and endpoint connection pooling for Spanner clients by adding a thread-safe ConnectionCache and a ServerConnection wrapper to track active inflight requests. The review feedback suggests optimizing ActiveRequestGuard to only hold an Arc<AtomicUsize> reference to avoid expensive clones of ServerConnection (which contains a String and a Channel). Additionally, it is recommended to check the read lock first in ConnectionCache::get before acquiring a write lock to minimize lock contention under high concurrency.

Comment thread src/spanner/src/routing/server_connection.rs
Comment thread src/spanner/src/routing/server_connection.rs
Comment thread src/spanner/src/routing/connection_cache.rs Outdated
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from 50375aa to ca07b61 Compare July 30, 2026 17:00
@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 introduces a new routing module in the Spanner client, adding a thread-safe ConnectionCache and a ServerConnection wrapper to support location-aware routing and endpoint connection pooling. Feedback on these changes highlights a compilation issue where OnceCell::initialized() is called but does not exist, suggesting cell.get().is_some() as a replacement. Additionally, reviewers recommended optimizing the ConnectionCache::clear method using retain to avoid unnecessary allocations, and improving performance on the hot path by relaxing atomic ordering from SeqCst to Relaxed for tracking active requests.

Comment thread src/spanner/src/routing/connection_cache.rs Outdated
Comment thread src/spanner/src/routing/connection_cache.rs
Comment thread src/spanner/src/routing/server_connection.rs Outdated
Comment thread src/spanner/src/routing/server_connection.rs
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from ca07b61 to 556fb24 Compare July 30, 2026 19:27
@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 introduces location-aware routing and connection pooling capabilities to the Spanner client by adding the routing::connection_cache and routing::server_connection modules, along with comprehensive unit tests. Feedback focuses on optimizing lock acquisition and reducing string allocations in ConnectionCache::get, as well as replacing the CAS loop in ActiveRequestGuard::drop with a simpler fetch_sub operation to reduce concurrency overhead.

Comment thread src/spanner/src/routing/connection_cache.rs Outdated
Comment thread src/spanner/src/routing/server_connection.rs
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from 556fb24 to ffe6d66 Compare July 30, 2026 19:40
@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 introduces location-aware routing and connection caching for the Spanner client, adding the routing, connection_cache, and server_connection modules. The feedback suggests two performance optimizations: wrapping the inner state of ServerConnection in a single Arc to reduce allocation and cloning overhead on the hot path, and refactoring ConnectionCache::is_empty to use .any() instead of self.len() == 0 to enable short-circuiting.

Comment thread src/spanner/src/routing/server_connection.rs
Comment thread src/spanner/src/routing/connection_cache.rs
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from ffe6d66 to 89d31da Compare July 30, 2026 19:47
@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 introduces a thread-safe connection cache and server connection wrapper to support location-aware routing in the Spanner client. Feedback was provided on ActiveRequestGuard::drop to use fetch_update with saturating_sub instead of fetch_sub to prevent potential underflow of the active request counter.

Comment thread src/spanner/src/routing/server_connection.rs
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from 89d31da to a25e78d Compare July 30, 2026 19:51
@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 introduces a thread-safe ConnectionCache and ServerConnection wrapper to support location-aware routing and endpoint connection pooling for Spanner clients. Feedback was provided on ConnectionCache::get to avoid an unnecessary String clone by taking the endpoint out of the configuration after creating the channel.

Comment thread src/spanner/src/routing/connection_cache.rs
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from a25e78d to 4bc3f3b Compare July 30, 2026 19:58
@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 introduces a new location-aware routing module for Spanner clients, adding a thread-safe ConnectionCache and a ServerConnection wrapper to track connection health and active requests. The feedback suggests simplifying the connection initialization logic in ConnectionCache::get by cloning the address string upfront instead of using take().unwrap_or_default(), which avoids unnecessary complexity and potential error swallowing in line with the style guide.

Comment thread src/spanner/src/routing/connection_cache.rs
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from 4bc3f3b to 1995010 Compare July 30, 2026 20:03
@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 introduces location-aware routing components for Spanner, adding ServerConnection to track health and active requests, and ConnectionCache to manage these connections thread-safely. The reviewer suggested a more idiomatic initialization of the default connection cell in ConnectionCache using OnceCell::from.

Comment thread src/spanner/src/routing/connection_cache.rs Outdated
…outing

Add `ServerConnection` and `ConnectionCache` manage server connections and
inflight request metrics for location-aware routing.
@olavloite
olavloite force-pushed the spanner-channel-endpoint-cache branch from 1995010 to 2914b1f Compare July 30, 2026 20:09
@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 introduces location-aware routing modules (routing, connection_cache, and server_connection) to the Spanner client. This includes ServerConnection for wrapping gRPC channels and tracking active requests/health states, and ConnectionCache for thread-safe connection pooling. Feedback highlights that a concurrent cache test performs real network I/O by calling Channel::create, which can lead to slow and flaky tests, and suggests mocking the channel creation for unit tests.

Comment thread src/spanner/src/routing/connection_cache.rs
@olavloite
olavloite requested a review from sakthivelmanii July 31, 2026 05:23
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.

1 participant