#![allow(unsafe_code)] use std::pin::Pin; use crate::{ CoreError, Operation, ResponseData, TransactionError, TxId, execute_many_operations, execute_single_operation, }; use connector::{Connection, Transaction}; use crosstarget_utils::time::ElapsedTimeCounter; use schema::QuerySchemaRef; use telemetry::TraceParent; use tokio::time::Duration; use tracing::Span; use tracing_futures::Instrument; // Note: it's important to maintain the correct state of the transaction throughout execution. If // the transaction is ever left in the `Open` state after rollback or commit operations, it means // that the corresponding connection will never be returned to the connection pool. enum TransactionState { Open { // Note: field order is important here because fields are dropped in the declaration order. // First, we drop the `tx`, which may reference `_conn`. Only after that we drop `_conn`. tx: Box, _conn: Pin>, }, Committed, RolledBack, Expired { start_time: ElapsedTimeCounter, timeout: Duration, }, } pub enum ClosedTransaction { Committed, RolledBack, Expired { start_time: ElapsedTimeCounter, timeout: Duration, }, } impl ClosedTransaction { pub fn error_message_for(&self, operation: &str) -> String { match self { ClosedTransaction::Committed => { format!("A {operation} cannot be executed on a committed transaction") } ClosedTransaction::RolledBack => { format!("A {operation} cannot be executed on a transaction that was rolled back") } ClosedTransaction::Expired { start_time, timeout } => { format!( "A {operation} cannot be executed on an expired transaction. \ The timeout for this transaction was {} ms, however {} ms passed since the start \ of the transaction. Consider increasing the interactive transaction timeout \ or doing less work in the transaction", timeout.as_millis(), start_time.elapsed_time().as_millis(), ) } } } } impl TransactionState { async fn start_transaction( conn: Box, isolation_level: Option, ) -> crate::Result { // Note: This method creates a self-referential struct, which is why we need unsafe. Field // `tx` is referencing field `conn` in the `Self::Open` variant. let mut conn = Box::into_pin(conn); // SAFETY: We do not move out of `conn`. let conn_mut: &mut (dyn Connection + Send + Sync) = unsafe { conn.as_mut().get_unchecked_mut() }; // This creates a transaction, which borrows from the connection. let tx_borrowed_from_conn: Box = conn_mut.start_transaction(isolation_level).await?; // SAFETY: This transmute only erases the lifetime from `conn_mut`. Normally, borrow checker // guarantees that the borrowed value is not dropped. In this case, we guarantee ourselves // through the use of `Pin` on the connection. let tx_with_erased_lifetime: Box = unsafe { std::mem::transmute(tx_borrowed_from_conn) }; Ok(Self::Open { tx: tx_with_erased_lifetime, _conn: conn, }) } fn as_open(&mut self, from_operation: &str) -> crate::Result<&mut Box> { match self { Self::Open { tx, .. } => Ok(tx), tx => Err(CoreError::from(TransactionError::Closed { reason: tx.as_closed().unwrap().error_message_for(from_operation), })), } } fn as_closed(&self) -> Option { match self { Self::Open { .. } => None, Self::Committed => Some(ClosedTransaction::Committed), Self::RolledBack => Some(ClosedTransaction::RolledBack), Self::Expired { start_time, timeout } => Some(ClosedTransaction::Expired { start_time: *start_time, timeout: *timeout, }), } } } pub struct InteractiveTransaction { id: TxId, state: TransactionState, start_time: ElapsedTimeCounter, timeout: Duration, query_schema: QuerySchemaRef, } /// This macro executes the future until it's ready or the transaction's timeout expires. macro_rules! tx_timeout { ($self:expr, $operation:expr, $fut:expr) => {{ let remaining_time = $self .timeout .checked_sub($self.start_time.elapsed_time()) .unwrap_or(Duration::ZERO); tokio::select! { biased; _ = crosstarget_utils::time::sleep(remaining_time) => { let _ = $self.rollback(true).await; Err(TransactionError::Closed { reason: $self.as_closed().unwrap().error_message_for($operation), }.into()) } result = $fut => { result } } }}; } impl InteractiveTransaction { pub async fn new( id: TxId, conn: Box, timeout: Duration, query_schema: QuerySchemaRef, isolation_level: Option, ) -> crate::Result { Span::current().record("itx_id", id.to_string()); Ok(Self { id, state: TransactionState::start_transaction(conn, isolation_level).await?, start_time: ElapsedTimeCounter::start(), timeout, query_schema, }) } pub async fn execute_single( &mut self, operation: &Operation, traceparent: Option, ) -> crate::Result { tx_timeout!(self, "query", async { let conn = self.state.as_open("query")?; execute_single_operation( self.query_schema.clone(), conn.as_connection_like(), operation, traceparent, ) .instrument(info_span!("prisma:engine:itx_execute_single")) .await }) } pub async fn execute_batch( &mut self, operations: &[Operation], traceparent: Option, ) -> crate::Result>> { tx_timeout!(self, "batch query", async { let conn = self.state.as_open("batch query")?; execute_many_operations( self.query_schema.clone(), conn.as_connection_like(), operations, traceparent, ) .instrument(info_span!("prisma:engine:itx_execute_batch")) .await }) } pub async fn commit(&mut self) -> crate::Result<()> { tx_timeout!(self, "commit", async { let name = self.name(); let conn = self.state.as_open("commit")?; let span = info_span!("prisma:engine:itx_commit"); if let Err(err) = conn.commit().instrument(span).await { error!(?err, ?name, "transaction failed to commit"); // We don't know if the transaction was committed or not. Because of that, we cannot // leave it in "open" state. We attempt to rollback to get the transaction into a // known state. let _ = self.rollback(false).await; Err(err.into()) } else { debug!(?name, "transaction committed"); self.state = TransactionState::Committed; Ok(()) } }) } pub async fn rollback(&mut self, was_timeout: bool) -> crate::Result<()> { let name = self.name(); let conn = self.state.as_open("rollback")?; let span = info_span!("prisma:engine:itx_rollback"); let result = conn.rollback().instrument(span).await; if let Err(err) = &result { error!(?err, ?was_timeout, ?name, "transaction failed to roll back"); } else { debug!(?was_timeout, ?name, "transaction rolled back"); } // Ensure that the transaction isn't left in the "open" state after the rollback. if was_timeout { self.state = TransactionState::Expired { start_time: self.start_time, timeout: self.timeout, }; } else { self.state = TransactionState::RolledBack; } result.map_err(<_>::into) } pub fn as_closed(&self) -> Option { self.state.as_closed() } pub fn name(&self) -> String { format!("itx-{}", self.id) } }