-
Notifications
You must be signed in to change notification settings - Fork 58
[nexus] Pool -> PoolBuilder, make backtrace collection configurable #9074
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
smklein
wants to merge
4
commits into
main
Choose a base branch
from
poolbuilder
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -28,6 +28,8 @@ use tokio::sync::watch; | |
/// Wrapper around a database connection pool. | ||
/// | ||
/// Expected to be used as the primary interface to the database. | ||
/// | ||
/// Can be constructed with a [`PoolBuilder`]. | ||
pub struct Pool { | ||
// IDs are assigned to each connection, acting as keys within the Quiesce | ||
// state. These are used to track the set of in-use connections and | ||
|
@@ -37,6 +39,7 @@ pub struct Pool { | |
log: Logger, | ||
terminated: std::sync::atomic::AtomicBool, | ||
quiesce: watch::Sender<Quiesce>, | ||
collect_backtraces: bool, | ||
} | ||
|
||
// Provides an alternative to the DNS resolver for cases where we want to | ||
|
@@ -46,7 +49,7 @@ struct SingleHostResolver { | |
} | ||
|
||
impl SingleHostResolver { | ||
fn new(config: &DbConfig) -> Self { | ||
fn new(config: DbConfig) -> Self { | ||
let backends = Arc::new(BTreeMap::from([( | ||
backend::Name::new("singleton"), | ||
backend::Backend { address: config.url.address() }, | ||
|
@@ -63,7 +66,7 @@ impl Resolver for SingleHostResolver { | |
} | ||
|
||
fn make_single_host_resolver( | ||
config: &DbConfig, | ||
config: DbConfig, | ||
) -> qorb::resolver::BoxedResolver { | ||
Box::new(SingleHostResolver::new(config)) | ||
} | ||
|
@@ -85,81 +88,60 @@ fn make_postgres_connector( | |
)) | ||
} | ||
|
||
impl Pool { | ||
/// Creates a new qorb-backed connection pool to the database. | ||
/// | ||
/// Creating this pool does not necessarily wait for connections to become | ||
/// available, as backends may shift over time. | ||
pub fn new(log: &Logger, resolver: &QorbResolver) -> Self { | ||
let resolver = resolver.for_service(ServiceName::Cockroach); | ||
let connector = make_postgres_connector(log); | ||
let policy = Policy::default(); | ||
let inner = match qorb::pool::Pool::new( | ||
"crdb".to_string(), | ||
resolver, | ||
connector, | ||
policy, | ||
) { | ||
Ok(pool) => { | ||
debug!(log, "registered USDT probes"); | ||
pool | ||
} | ||
Err(err) => { | ||
error!(log, "failed to register USDT probes"); | ||
err.into_inner() | ||
} | ||
}; | ||
Self::new_common(inner, log.clone()) | ||
enum ConnectWith<'a> { | ||
Resolver(&'a QorbResolver), | ||
SingleHost(Box<DbConfig>), | ||
} | ||
|
||
/// Utility for building [`Pool`]s. | ||
pub struct PoolBuilder<'a> { | ||
log: &'a Logger, | ||
connect_with: ConnectWith<'a>, | ||
policy: Option<Policy>, | ||
collect_backtraces: Option<bool>, | ||
} | ||
|
||
impl<'a> PoolBuilder<'a> { | ||
/// Uses a resolver to connect to the database. | ||
pub fn new(log: &'a Logger, resolver: &'a QorbResolver) -> Self { | ||
let connect_with = ConnectWith::Resolver(resolver); | ||
Self { log, connect_with, policy: None, collect_backtraces: None } | ||
} | ||
|
||
/// Creates a new qorb-backed connection pool to a single instance of the | ||
/// database. | ||
/// | ||
/// This is intended for tests that want to skip DNS resolution, relying | ||
/// on a single instance of the database. | ||
/// | ||
/// In production, [Self::new] should be preferred. | ||
pub fn new_single_host(log: &Logger, db_config: &DbConfig) -> Self { | ||
let resolver = make_single_host_resolver(db_config); | ||
let connector = make_postgres_connector(log); | ||
let policy = Policy::default(); | ||
let inner = match qorb::pool::Pool::new( | ||
"crdb-single-host".to_string(), | ||
resolver, | ||
connector, | ||
policy, | ||
) { | ||
Ok(pool) => { | ||
debug!(log, "registered USDT probes"); | ||
pool | ||
/// Connects to a single hard-coded database node. | ||
pub fn new_single_host(log: &'a Logger, config: DbConfig) -> Self { | ||
let connect_with = ConnectWith::SingleHost(Box::new(config)); | ||
Self { log, connect_with, policy: None, collect_backtraces: None } | ||
} | ||
|
||
pub fn policy(mut self, policy: Policy) -> Self { | ||
self.policy = Some(policy); | ||
self | ||
} | ||
|
||
pub fn collect_backtraces(mut self, collect_backtraces: bool) -> Self { | ||
self.collect_backtraces = Some(collect_backtraces); | ||
self | ||
} | ||
|
||
pub fn build(self) -> Pool { | ||
let Self { log, connect_with, policy, collect_backtraces } = self; | ||
|
||
let (resolver, name) = match connect_with { | ||
ConnectWith::Resolver(resolver) => { | ||
(resolver.for_service(ServiceName::Cockroach), "crdb") | ||
} | ||
Err(err) => { | ||
error!(log, "failed to register USDT probes"); | ||
err.into_inner() | ||
ConnectWith::SingleHost(config) => { | ||
(make_single_host_resolver(*config), "crdb-single-host") | ||
} | ||
}; | ||
Self::new_common(inner, log.clone()) | ||
} | ||
|
||
/// Creates a new qorb-backed connection pool which returns an error | ||
/// if claims are not available within one millisecond. | ||
/// | ||
/// This is intended for test-only usage, in particular for tests where | ||
/// claim requests should rapidly return errors when a backend has been | ||
/// intentionally disabled. | ||
#[cfg(any(test, feature = "testing"))] | ||
pub fn new_single_host_failfast( | ||
log: &Logger, | ||
db_config: &DbConfig, | ||
) -> Self { | ||
let resolver = make_single_host_resolver(db_config); | ||
let connector = make_postgres_connector(log); | ||
let policy = Policy { | ||
claim_timeout: tokio::time::Duration::from_millis(1), | ||
..Default::default() | ||
}; | ||
|
||
let policy = policy.unwrap_or_else(|| Policy::default()); | ||
let collect_backtraces = collect_backtraces.unwrap_or(true); | ||
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. This is where we match the current behavior for backtrace collection by default: unless requested otherwise, |
||
|
||
let inner = match qorb::pool::Pool::new( | ||
"crdb-single-host-failfast".to_string(), | ||
name.to_string(), | ||
resolver, | ||
connector, | ||
policy, | ||
|
@@ -173,12 +155,15 @@ impl Pool { | |
err.into_inner() | ||
} | ||
}; | ||
Self::new_common(inner, log.clone()) | ||
Pool::new(inner, log.clone(), collect_backtraces) | ||
} | ||
} | ||
|
||
fn new_common( | ||
impl Pool { | ||
fn new( | ||
inner: qorb::pool::Pool<AsyncConnection>, | ||
log: Logger, | ||
collect_backtraces: bool, | ||
) -> Self { | ||
let (quiesce, _) = watch::channel(Quiesce { | ||
new_claims_allowed: ClaimsAllowed::Allowed, | ||
|
@@ -190,14 +175,19 @@ impl Pool { | |
log, | ||
terminated: std::sync::atomic::AtomicBool::new(false), | ||
quiesce, | ||
collect_backtraces, | ||
} | ||
} | ||
|
||
/// Returns a connection from the pool | ||
pub async fn claim(&self) -> Result<DataStoreConnection, Error> { | ||
let id = self.next_id.fetch_add(1, Ordering::Relaxed); | ||
let held_since = Utc::now(); | ||
let debug = Backtrace::force_capture().to_string(); | ||
let debug = if self.collect_backtraces { | ||
Backtrace::force_capture().to_string() | ||
} else { | ||
"<backtraces disabled>".to_string() | ||
}; | ||
let allowed = self.quiesce.send_if_modified(|q| { | ||
if let ClaimsAllowed::Disallowed = q.new_claims_allowed { | ||
false | ||
|
@@ -366,7 +356,9 @@ mod test { | |
let mut db = crdb::test_setup_database(log).await; | ||
let cfg = crate::db::Config { url: db.pg_config().clone() }; | ||
{ | ||
let pool = Pool::new_single_host(&log, &cfg); | ||
let pool = PoolBuilder::new_single_host(&log, cfg) | ||
.collect_backtraces(false) | ||
.build(); | ||
pool.terminate().await; | ||
} | ||
db.cleanup().await.unwrap(); | ||
|
@@ -383,7 +375,9 @@ mod test { | |
let mut db = crdb::test_setup_database(log).await; | ||
let cfg = crate::db::Config { url: db.pg_config().clone() }; | ||
{ | ||
let pool = Pool::new_single_host(&log, &cfg); | ||
let pool = PoolBuilder::new_single_host(&log, cfg) | ||
.collect_backtraces(false) | ||
.build(); | ||
drop(pool); | ||
} | ||
db.cleanup().await.unwrap(); | ||
|
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
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.
Since we already have three different constructors for a Pool...
So I switched to a builder pattern instead.