Skip to content

RUST-107 Convenient transactions #849

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

Merged
merged 25 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .evergreen/MSRV-Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions src/client/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub struct ClientSession {
pub(crate) transaction: Transaction,
pub(crate) snapshot_time: Option<Timestamp>,
pub(crate) operation_time: Option<Timestamp>,
#[cfg(test)]
pub(crate) convenient_transaction_timeout: Option<Duration>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -216,6 +218,8 @@ impl ClientSession {
transaction: Default::default(),
snapshot_time: None,
operation_time: None,
#[cfg(test)]
convenient_transaction_timeout: None,
}
}

Expand Down Expand Up @@ -561,13 +565,117 @@ impl ClientSession {
}
}

/// Starts a transaction, runs the given callback, and commits or aborts the transaction.
/// Transient transaction errors will cause the callback or the commit to be retried;
/// other errors will cause the transaction to be aborted and the error returned to the
/// caller. If the callback needs to provide its own error information, the
/// [`Error::custom`](crate::error::Error::custom) method can accept an arbitrary payload that
/// can be retrieved via [`Error::get_custom`](crate::error::Error::get_custom).
///
/// Because the callback can be repeatedly executed and because it returns a future, the rust
/// closure borrowing rules for captured values can be overly restrictive. As a
/// convenience, `with_transaction` accepts a context argument that will be passed to the
/// callback along with the session:
///
/// ```no_run
/// # use mongodb::{bson::{doc, Document}, error::Result, Client};
/// # use futures::FutureExt;
/// # async fn wrapper() -> Result<()> {
/// # let client = Client::with_uri_str("mongodb://example.com").await?;
/// # let mut session = client.start_session(None).await?;
/// let coll = client.database("mydb").collection::<Document>("mycoll");
/// let my_data = "my data".to_string();
/// // This works:
/// session.with_transaction(
/// (&coll, &my_data),
/// |session, (coll, my_data)| async move {
/// coll.insert_one_with_session(doc! { "data": *my_data }, None, session).await
/// }.boxed(),
/// None,
/// ).await?;
/// /* This will not compile with a "variable moved due to use in generator" error:
/// session.with_transaction(
/// (),
/// |session, _| async move {
/// coll.insert_one_with_session(doc! { "data": my_data }, None, session).await
/// }.boxed(),
/// None,
/// ).await?;
/// */
/// # Ok(())
/// # }
/// ```
pub async fn with_transaction<R, C, F>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll also need to add this method to the sync API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Unfortunately, I had to duplicate the body of the function rather than just wrapping the async one to avoid nested block_on calls from within the callback.

&mut self,
mut context: C,
mut callback: F,
options: impl Into<Option<TransactionOptions>>,
) -> Result<R>
where
F: for<'a> FnMut(&'a mut ClientSession, &'a mut C) -> BoxFuture<'a, Result<R>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we could rewrite this signature to avoid including BoxFuture? futures_core is a pre-1.0 crate, so I think we need to be cautious about including its API in our public API. I know BoxFuture specifically was broken a while ago to add the lifetime parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that's unfortunate. The type erasure is necessary - there's no way to express for<'a> FnMut(&'a mut ClientSession, &'a mut C) -> impl Future<'a, Result<R>> currently without it. Since BoxFuture is just a type alias, I've duplicated the definition here instead of using the one from the futures_core crate.

{
let options = options.into();
let timeout = Duration::from_secs(120);
#[cfg(test)]
let timeout = self.convenient_transaction_timeout.unwrap_or(timeout);
let start = Instant::now();

use crate::error::{TRANSIENT_TRANSACTION_ERROR, UNKNOWN_TRANSACTION_COMMIT_RESULT};

'transaction: loop {
self.start_transaction(options.clone()).await?;
let ret = match callback(self, &mut context).await {
Ok(v) => v,
Err(e) => {
if matches!(
self.transaction.state,
TransactionState::Starting | TransactionState::InProgress
) {
self.abort_transaction().await?;
}
if e.contains_label(TRANSIENT_TRANSACTION_ERROR) && start.elapsed() < timeout {
continue 'transaction;
}
return Err(e);
}
};
if matches!(
self.transaction.state,
TransactionState::None
| TransactionState::Aborted
| TransactionState::Committed { .. }
) {
return Ok(ret);
}
'commit: loop {
match self.commit_transaction().await {
Ok(()) => return Ok(ret),
Err(e) => {
if e.is_max_time_ms_expired_error() || start.elapsed() >= timeout {
return Err(e);
}
if e.contains_label(UNKNOWN_TRANSACTION_COMMIT_RESULT) {
continue 'commit;
}
if e.contains_label(TRANSIENT_TRANSACTION_ERROR) {
continue 'transaction;
}
return Err(e);
}
}
}
}
}

fn default_transaction_options(&self) -> Option<&TransactionOptions> {
self.options
.as_ref()
.and_then(|options| options.default_transaction_options.as_ref())
}
}

pub type BoxFuture<'a, T> = std::pin::Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;

struct DroppedClientSession {
cluster_time: Option<ClusterTime>,
server_session: ServerSession,
Expand All @@ -590,6 +698,8 @@ impl From<DroppedClientSession> for ClientSession {
transaction: dropped_session.transaction,
snapshot_time: dropped_session.snapshot_time,
operation_time: dropped_session.operation_time,
#[cfg(test)]
convenient_transaction_timeout: None,
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Contains the `Error` and `Result` types that `mongodb` uses.

use std::{
any::Any,
collections::{HashMap, HashSet},
fmt::{self, Debug},
sync::Arc,
Expand Down Expand Up @@ -52,6 +53,22 @@ pub struct Error {
}

impl Error {
/// Create a new `Error` wrapping an arbitrary value. Can be used to abort transactions in
/// callbacks for [`ClientSession::with_transaction`](crate::ClientSession::with_transaction).
pub fn custom(e: impl Any + Send + Sync) -> Self {
Self::new(ErrorKind::Custom(Arc::new(e)), None::<Option<String>>)
}

/// Retrieve a reference to a value provided to `Error::custom`. Returns `None` if this is not
/// a custom error or if the payload types mismatch.
pub fn get_custom<E: Any>(&self) -> Option<&E> {
if let ErrorKind::Custom(c) = &*self.kind {
c.downcast_ref()
} else {
None
}
}

pub(crate) fn new(kind: ErrorKind, labels: Option<impl IntoIterator<Item = String>>) -> Self {
let mut labels: HashSet<String> = labels
.map(|labels| labels.into_iter().collect())
Expand Down Expand Up @@ -140,6 +157,10 @@ impl Error {
matches!(self.kind.as_ref(), ErrorKind::ServerSelection { .. })
}

pub(crate) fn is_max_time_ms_expired_error(&self) -> bool {
self.code() == Some(50)
}

/// Whether a read operation should be retried if this error occurs.
pub(crate) fn is_read_retryable(&self) -> bool {
if self.is_network_error() {
Expand Down Expand Up @@ -423,6 +444,7 @@ impl Error {
| ErrorKind::IncompatibleServer { .. }
| ErrorKind::MissingResumeToken
| ErrorKind::Authentication { .. }
| ErrorKind::Custom(_)
| ErrorKind::GridFs(_) => {}
#[cfg(feature = "in-use-encryption-unstable")]
ErrorKind::Encryption(_) => {}
Expand Down Expand Up @@ -578,6 +600,10 @@ pub enum ErrorKind {
#[cfg(feature = "in-use-encryption-unstable")]
#[error("An error occurred during client-side encryption: {0}")]
Encryption(mongocrypt::error::Error),

/// A custom value produced by user code.
#[error("Custom user error")]
Custom(Arc<dyn Any + Send + Sync>),
}

impl ErrorKind {
Expand Down
68 changes: 68 additions & 0 deletions src/sync/client/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,72 @@ impl ClientSession {
pub fn abort_transaction(&mut self) -> Result<()> {
runtime::block_on(self.async_client_session.abort_transaction())
}

/// Starts a transaction, runs the given callback, and commits or aborts the transaction.
/// Transient transaction errors will cause the callback or the commit to be retried;
/// other errors will cause the transaction to be aborted and the error returned to the
/// caller. If the callback needs to provide its own error information, the
/// [`Error::custom`](crate::error::Error::custom) method can accept an arbitrary payload that
/// can be retrieved via [`Error::get_custom`](crate::error::Error::get_custom).
pub fn with_transaction<R, F>(
&mut self,
mut callback: F,
options: impl Into<Option<TransactionOptions>>,
) -> Result<R>
where
F: for<'a> FnMut(&'a mut ClientSession) -> Result<R>,
{
let options = options.into();
let timeout = std::time::Duration::from_secs(120);
let start = std::time::Instant::now();

use crate::{
client::session::TransactionState,
error::{TRANSIENT_TRANSACTION_ERROR, UNKNOWN_TRANSACTION_COMMIT_RESULT},
};

'transaction: loop {
self.start_transaction(options.clone())?;
let ret = match callback(self) {
Ok(v) => v,
Err(e) => {
if matches!(
self.async_client_session.transaction.state,
TransactionState::Starting | TransactionState::InProgress
) {
self.abort_transaction()?;
}
if e.contains_label(TRANSIENT_TRANSACTION_ERROR) && start.elapsed() < timeout {
continue 'transaction;
}
return Err(e);
}
};
if matches!(
self.async_client_session.transaction.state,
TransactionState::None
| TransactionState::Aborted
| TransactionState::Committed { .. }
) {
return Ok(ret);
}
'commit: loop {
match self.commit_transaction() {
Ok(()) => return Ok(ret),
Err(e) => {
if e.is_max_time_ms_expired_error() || start.elapsed() >= timeout {
return Err(e);
}
if e.contains_label(UNKNOWN_TRANSACTION_COMMIT_RESULT) {
continue 'commit;
}
if e.contains_label(TRANSIENT_TRANSACTION_ERROR) {
continue 'transaction;
}
return Err(e);
}
}
}
}
}
}
Loading