-
Notifications
You must be signed in to change notification settings - Fork 137
chore(spanner): implement server connection pool for location-aware routing #6236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
olavloite
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
olavloite:spanner-channel-endpoint-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,346 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Thread-safe cache of Spanner server node connections for location-aware routing. | ||
|
|
||
| // TODO(location-aware-routing): Remove allow(dead_code) once location_router.rs integrates ConnectionCache. | ||
| #![allow(dead_code)] | ||
|
|
||
| use crate::client::Channel; | ||
| use crate::routing::server_connection::ServerConnection; | ||
| use gaxi::options::ClientConfig; | ||
| use std::collections::HashMap; | ||
| use std::sync::{Arc, RwLock}; | ||
|
|
||
| /// Cache for server connections used in location-aware routing. | ||
| /// | ||
| /// Stores and manages [`ServerConnection`] instances such that repeated calls with the same address | ||
| /// return the same connection wrapper. | ||
| /// | ||
| /// # Thread Safety | ||
| /// | ||
| /// This cache is thread-safe and allows concurrent lookups across multiple operations. | ||
| #[derive(Debug)] | ||
| pub(crate) struct ConnectionCache { | ||
| default_connection: ServerConnection, | ||
| servers: RwLock<HashMap<String, Arc<tokio::sync::OnceCell<ServerConnection>>>>, | ||
| } | ||
|
|
||
| impl ConnectionCache { | ||
| /// Creates a new `ConnectionCache` with the specified default fallback connection. | ||
| pub(crate) fn new(default_connection: ServerConnection) -> Self { | ||
| let default_cell = Arc::new(tokio::sync::OnceCell::from(default_connection.clone())); | ||
| let mut map = HashMap::new(); | ||
| map.insert(default_connection.address().to_string(), default_cell); | ||
| Self { | ||
| default_connection, | ||
| servers: RwLock::new(map), | ||
| } | ||
| } | ||
|
|
||
| /// Returns a reference to the default fallback server connection. | ||
| pub(crate) fn default_connection(&self) -> &ServerConnection { | ||
| &self.default_connection | ||
| } | ||
|
|
||
| /// Returns a cached connection for the given address without creating it if missing. | ||
| /// | ||
| /// This method is used by location-aware routing to avoid foreground connection creation on the | ||
| /// hot RPC request path. | ||
| pub(crate) fn get_if_present(&self, address: &str) -> Option<ServerConnection> { | ||
| let guard = self | ||
| .servers | ||
| .read() | ||
| .expect("connection cache read lock poisoned"); | ||
| guard.get(address).and_then(|cell| cell.get().cloned()) | ||
| } | ||
|
|
||
| /// Returns a cached connection for the given address, creating and caching a new connection | ||
| /// asynchronously if needed. | ||
| pub(crate) async fn get( | ||
| &self, | ||
| address: &str, | ||
| config: &ClientConfig, | ||
| ) -> crate::ClientBuilderResult<ServerConnection> { | ||
| let cell = { | ||
| let guard = self | ||
| .servers | ||
| .read() | ||
| .expect("connection cache read lock poisoned"); | ||
| if let Some(cell) = guard.get(address) { | ||
| if let Some(connection) = cell.get() { | ||
| return Ok(connection.clone()); | ||
| } | ||
| cell.clone() | ||
| } else { | ||
| drop(guard); | ||
| let mut guard = self | ||
| .servers | ||
| .write() | ||
| .expect("connection cache write lock poisoned"); | ||
| guard | ||
| .entry(address.to_string()) | ||
| .or_insert_with(|| Arc::new(tokio::sync::OnceCell::new())) | ||
| .clone() | ||
| } | ||
| }; | ||
|
|
||
| cell.get_or_try_init(|| async { | ||
| let mut ep_config = config.clone(); | ||
| let addr = address.to_string(); | ||
| ep_config.endpoint = Some(addr.clone()); | ||
| let channel = Channel::create(&ep_config).await?; | ||
| Ok(ServerConnection::new(addr, channel)) | ||
| }) | ||
|
olavloite marked this conversation as resolved.
|
||
| .await | ||
| .cloned() | ||
| } | ||
|
olavloite marked this conversation as resolved.
|
||
|
|
||
| /// Evicts a server connection from the cache. | ||
| /// | ||
| /// If `address` matches the default connection's address, this method does nothing and returns | ||
| /// `false`. Otherwise, returns `true` if a connection was removed from the cache. | ||
| pub(crate) fn evict(&self, address: &str) -> bool { | ||
| if self.default_connection.address() == address { | ||
| return false; | ||
| } | ||
| let mut guard = self | ||
| .servers | ||
| .write() | ||
| .expect("connection cache write lock poisoned"); | ||
| guard.remove(address).is_some() | ||
| } | ||
|
|
||
| /// Returns the number of cached server connections (including the default connection). | ||
| pub(crate) fn len(&self) -> usize { | ||
| let guard = self | ||
| .servers | ||
| .read() | ||
| .expect("connection cache read lock poisoned"); | ||
| guard.values().filter(|cell| cell.get().is_some()).count() | ||
| } | ||
|
|
||
| /// Returns whether the cache is empty. | ||
| pub(crate) fn is_empty(&self) -> bool { | ||
| let guard = self | ||
| .servers | ||
| .read() | ||
| .expect("connection cache read lock poisoned"); | ||
| !guard.values().any(|cell| cell.get().is_some()) | ||
| } | ||
|
olavloite marked this conversation as resolved.
|
||
|
|
||
| /// Clears all cached server connections while preserving the default fallback connection. | ||
| pub(crate) fn clear(&self) { | ||
| let mut guard = self | ||
| .servers | ||
| .write() | ||
| .expect("connection cache write lock poisoned"); | ||
| let default_address = self.default_connection.address(); | ||
| guard.retain(|k, _| k == default_address); | ||
| } | ||
|
olavloite marked this conversation as resolved.
|
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::sync::Barrier; | ||
| use std::thread; | ||
|
|
||
| #[derive(Debug)] | ||
| struct DummyStub; | ||
| impl crate::generated::gapic_dataplane::stub::Spanner for DummyStub {} | ||
|
|
||
| fn create_test_connection(address: &str) -> ServerConnection { | ||
| let channel = Channel::new_for_test(DummyStub); | ||
| ServerConnection::new(address.to_string(), channel) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_connection_cache_default_connection_and_get_if_present() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = ConnectionCache::new(default_conn.clone()); | ||
|
|
||
| assert_eq!(cache.len(), 1); | ||
| assert!(!cache.is_empty()); | ||
| assert_eq!( | ||
| cache.default_connection().address(), | ||
| "spanner.googleapis.com:443" | ||
| ); | ||
|
|
||
| let cached_default = cache | ||
| .get_if_present("spanner.googleapis.com:443") | ||
| .expect("default connection should be in cache"); | ||
| assert_eq!(cached_default.address(), "spanner.googleapis.com:443"); | ||
|
|
||
| assert!(cache.get_if_present("10.0.0.1:15000").is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_connection_cache_eviction_and_protection_of_default() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = ConnectionCache::new(default_conn); | ||
|
|
||
| // Manually insert a tablet connection into the cache for testing eviction. | ||
| let tablet_conn = create_test_connection("10.0.0.1:15000"); | ||
| { | ||
| let mut guard = cache.servers.write().expect("write lock poisoned"); | ||
| let cell = Arc::new(tokio::sync::OnceCell::new()); | ||
| let _ = cell.set(tablet_conn.clone()); | ||
| guard.insert(tablet_conn.address().to_string(), cell); | ||
| } | ||
| assert_eq!(cache.len(), 2); | ||
|
|
||
| // Evicting the default connection should be ignored. | ||
| assert!(!cache.evict("spanner.googleapis.com:443")); | ||
| assert_eq!(cache.len(), 2); | ||
|
|
||
| // Evicting a normal tablet connection should succeed. | ||
| assert!(cache.evict("10.0.0.1:15000")); | ||
| assert_eq!(cache.len(), 1); | ||
| assert!(cache.get_if_present("10.0.0.1:15000").is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_connection_cache_clear_preserves_default() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = ConnectionCache::new(default_conn); | ||
|
|
||
| { | ||
| let mut guard = cache.servers.write().expect("write lock poisoned"); | ||
| let cell1 = Arc::new(tokio::sync::OnceCell::new()); | ||
| let _ = cell1.set(create_test_connection("10.0.0.1:15000")); | ||
| guard.insert("10.0.0.1:15000".to_string(), cell1); | ||
|
|
||
| let cell2 = Arc::new(tokio::sync::OnceCell::new()); | ||
| let _ = cell2.set(create_test_connection("10.0.0.2:15000")); | ||
| guard.insert("10.0.0.2:15000".to_string(), cell2); | ||
| } | ||
| assert_eq!(cache.len(), 3); | ||
|
|
||
| cache.clear(); | ||
| assert_eq!(cache.len(), 1); | ||
| assert_eq!( | ||
| cache.default_connection().address(), | ||
| "spanner.googleapis.com:443" | ||
| ); | ||
| assert!(cache.get_if_present("spanner.googleapis.com:443").is_some()); | ||
| assert!(cache.get_if_present("10.0.0.1:15000").is_none()); | ||
| assert!(cache.get_if_present("10.0.0.2:15000").is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_connection_cache_concurrent_access() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = ConnectionCache::new(default_conn); | ||
| let worker_count = 10; | ||
| let iterations = 100; | ||
| let barrier = Barrier::new(worker_count); | ||
|
|
||
| thread::scope(|scope| { | ||
| for _ in 0..worker_count { | ||
| scope.spawn(|| { | ||
| barrier.wait(); | ||
| for _ in 0..iterations { | ||
| assert!(cache.get_if_present("spanner.googleapis.com:443").is_some()); | ||
| assert!(!cache.evict("spanner.googleapis.com:443")); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| assert_eq!(cache.len(), 1); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_connection_cache_get_cached() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = ConnectionCache::new(default_conn); | ||
| let config = ClientConfig::default(); | ||
|
|
||
| let ep = cache | ||
| .get("spanner.googleapis.com:443", &config) | ||
| .await | ||
| .expect("cached default connection"); | ||
| assert_eq!(ep.address(), "spanner.googleapis.com:443"); | ||
| assert_eq!(cache.len(), 1); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_connection_cache_concurrent_get_stampede_prevention() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = Arc::new(ConnectionCache::new(default_conn)); | ||
| let config = ClientConfig::default(); | ||
|
|
||
| let mut handles = Vec::new(); | ||
| for _ in 0..10 { | ||
| let cache_clone = Arc::clone(&cache); | ||
| let config_clone = config.clone(); | ||
| handles.push(tokio::spawn(async move { | ||
| cache_clone | ||
| .get("http://10.0.0.1:15000", &config_clone) | ||
| .await | ||
| .expect("should obtain connection") | ||
| })); | ||
| } | ||
|
|
||
| let mut results = Vec::new(); | ||
| for handle in handles { | ||
| results.push(handle.await.expect("task should complete")); | ||
| } | ||
|
|
||
| for conn in &results { | ||
| assert_eq!(conn.address(), "http://10.0.0.1:15000"); | ||
| } | ||
| assert_eq!(cache.len(), 2); | ||
| } | ||
|
olavloite marked this conversation as resolved.
|
||
|
|
||
| #[test] | ||
| fn test_connection_cache_uninitialized_cell_not_counted_in_len() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = ConnectionCache::new(default_conn); | ||
| assert_eq!(cache.len(), 1); | ||
| assert!(!cache.is_empty()); | ||
|
|
||
| { | ||
| let mut guard = cache.servers.write().expect("write lock poisoned"); | ||
| guard.insert( | ||
| "10.0.0.1:15000".to_string(), | ||
| Arc::new(tokio::sync::OnceCell::new()), | ||
| ); | ||
| } | ||
|
|
||
| assert_eq!(cache.len(), 1); | ||
| assert!(!cache.is_empty()); | ||
| assert!(cache.get_if_present("10.0.0.1:15000").is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_connection_cache_evict_uninitialized_cell() { | ||
| let default_conn = create_test_connection("spanner.googleapis.com:443"); | ||
| let cache = ConnectionCache::new(default_conn); | ||
|
|
||
| { | ||
| let mut guard = cache.servers.write().expect("write lock poisoned"); | ||
| guard.insert( | ||
| "10.0.0.1:15000".to_string(), | ||
| Arc::new(tokio::sync::OnceCell::new()), | ||
| ); | ||
| } | ||
|
|
||
| assert_eq!(cache.len(), 1); | ||
| assert!(cache.evict("10.0.0.1:15000")); | ||
| assert_eq!(cache.len(), 1); | ||
| assert!(cache.get_if_present("10.0.0.1:15000").is_none()); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Location-aware routing and endpoint connection pooling for Spanner clients. | ||
|
|
||
| pub(crate) mod connection_cache; | ||
| pub(crate) mod server_connection; |
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.
Uh oh!
There was an error while loading. Please reload this page.