-
Notifications
You must be signed in to change notification settings - Fork 144
feat(spanner): add support for static and dynamic channel pooling #6834
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
Draft
olavloite
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
olavloite:spanner-expose-channel-pool
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.
Draft
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 |
|---|---|---|
|
|
@@ -55,16 +55,6 @@ impl TransactionAffinity { | |
| } | ||
| } | ||
|
|
||
| /// Returns the provided affinity handle, or creates a new default `ReadOnly` affinity if `None`. | ||
|
Contributor
Author
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. Removed, as they were not in use |
||
| pub(crate) fn default_read_only(existing: Option<Arc<Self>>) -> Arc<Self> { | ||
| existing.unwrap_or_else(|| Arc::new(Self::new_read_only())) | ||
| } | ||
|
|
||
| /// Returns the provided affinity handle, or creates a new default `ReadWrite` affinity if `None`. | ||
| pub(crate) fn default_read_write(existing: Option<Arc<Self>>) -> Arc<Self> { | ||
| existing.unwrap_or_else(|| Arc::new(Self::new_read_write())) | ||
| } | ||
|
|
||
| /// Returns `true` if this handle requires hard stickiness (Read/Write transactions). | ||
| pub(crate) fn is_read_write(&self) -> bool { | ||
| self.kind == AffinityKind::ReadWrite | ||
|
|
@@ -102,6 +92,16 @@ impl TransactionAffinity { | |
| } | ||
| *slot = Some(lease.rw_affinity_guard()); | ||
| } | ||
|
|
||
| /// Releases the active Read/Write transaction guard on the channel entry, | ||
| /// allowing draining channels to close once the transaction completes. | ||
| pub(crate) fn release_rw_guard(&self) { | ||
| let mut slot = self | ||
| .rw_guard | ||
| .lock() | ||
| .expect("affinity rw_guard lock poisoned"); | ||
| *slot = None; | ||
| } | ||
| } | ||
|
|
||
| /// Stickiness kind for transaction channel affinity. | ||
|
|
@@ -116,6 +116,64 @@ pub(crate) enum AffinityKind { | |
| ReadOnly, | ||
| } | ||
|
|
||
| /// Routing target for channel selection: either a round-robin hint or a transaction affinity handle. | ||
| #[derive(Clone, Copy, Debug)] | ||
| pub(crate) enum ChannelTarget<'a> { | ||
| Hint(usize), | ||
| Affinity(&'a TransactionAffinity), | ||
| } | ||
|
|
||
| impl<'a> ChannelTarget<'a> { | ||
| pub(crate) fn from_affinity_or_hint( | ||
| affinity: Option<&'a TransactionAffinity>, | ||
| hint: usize, | ||
| ) -> Self { | ||
| match affinity { | ||
| Some(affinity) => Self::Affinity(affinity), | ||
| None => Self::Hint(hint), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<usize> for ChannelTarget<'_> { | ||
| fn from(hint: usize) -> Self { | ||
| Self::Hint(hint) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<&'a TransactionAffinity> for ChannelTarget<'a> { | ||
| fn from(affinity: &'a TransactionAffinity) -> Self { | ||
| Self::Affinity(affinity) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<&'a Arc<TransactionAffinity>> for ChannelTarget<'a> { | ||
| fn from(affinity: &'a Arc<TransactionAffinity>) -> Self { | ||
| Self::Affinity(affinity) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<Option<&'a TransactionAffinity>> for ChannelTarget<'a> { | ||
| fn from(affinity: Option<&'a TransactionAffinity>) -> Self { | ||
| match affinity { | ||
| Some(affinity) => Self::Affinity(affinity), | ||
| None => Self::Hint(0), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<(Option<&'a TransactionAffinity>, usize)> for ChannelTarget<'a> { | ||
| fn from((affinity, hint): (Option<&'a TransactionAffinity>, usize)) -> Self { | ||
| Self::from_affinity_or_hint(affinity, hint) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> From<(usize, Option<&'a TransactionAffinity>)> for ChannelTarget<'a> { | ||
| fn from((hint, affinity): (usize, Option<&'a TransactionAffinity>)) -> Self { | ||
| Self::from_affinity_or_hint(affinity, hint) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| impl TransactionAffinity { | ||
| pub(crate) fn set_entry_id(&self, entry_id: u64) { | ||
|
|
@@ -155,6 +213,7 @@ mod tests { | |
| use crate::client::Channel; | ||
| use crate::generated::gapic_dataplane::stub::Spanner as SpannerStub; | ||
| use std::fmt::Debug; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| #[derive(Debug)] | ||
|
|
@@ -255,37 +314,6 @@ mod tests { | |
| ); | ||
| } | ||
|
|
||
| #[test] | ||
|
Contributor
Author
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. Removed, as the methods that it tested were removed. |
||
| fn transaction_affinity_defaults() { | ||
| let default_read_only = TransactionAffinity::default_read_only(None); | ||
| assert!( | ||
| default_read_only.is_read_only(), | ||
| "default_read_only(None) must return ReadOnly affinity" | ||
| ); | ||
|
|
||
| let custom_read_only = Arc::new(TransactionAffinity::new_read_only()); | ||
| let passed_read_only = | ||
| TransactionAffinity::default_read_only(Some(Arc::clone(&custom_read_only))); | ||
| assert!( | ||
| Arc::ptr_eq(&custom_read_only, &passed_read_only), | ||
| "default_read_only(Some(handle)) must return existing handle without recreating" | ||
| ); | ||
|
|
||
| let default_read_write = TransactionAffinity::default_read_write(None); | ||
| assert!( | ||
| default_read_write.is_read_write(), | ||
| "default_read_write(None) must return ReadWrite affinity" | ||
| ); | ||
|
|
||
| let custom_read_write = Arc::new(TransactionAffinity::new_read_write()); | ||
| let passed_read_write = | ||
| TransactionAffinity::default_read_write(Some(Arc::clone(&custom_read_write))); | ||
| assert!( | ||
| Arc::ptr_eq(&custom_read_write, &passed_read_write), | ||
| "default_read_write(Some(handle)) must return existing handle without recreating" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn attach_rw_guard_repinning_replaces_old_guard() { | ||
| let channel1 = Channel::new_for_test(DummyStub); | ||
|
|
||
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.
The default is likely to be changed to dynamic in the (near) future, once we have benchmarked it.