use std::any::Any;
use std::future::Future;
use std::sync::Arc;
use redis_tower_commands::Watch;
use redis_tower_core::{Command, Frame, RedisConnection, RedisError};
use redis_tower_protocol::helpers::{array, bulk};
use tokio::sync::Mutex;
use crate::client::RedisClient;
use crate::executor::RedisExecutor;
use crate::pipeline::{PipelineResults, ResponseParser};
pub trait TransactionExecutor {
const SUPPORTS_TRANSACTION_RETRY: bool = true;
fn validate_transaction_retry(&self) -> Result<(), RedisError> {
if Self::SUPPORTS_TRANSACTION_RETRY {
Ok(())
} else {
Err(RedisError::Redis(
"transaction() and transaction_with_retries() are unsupported by this executor; Transaction::watch() is safe only when the body is already known; use an exclusive RedisConnection for WATCH/read/build retries"
.to_string(),
))
}
}
fn execute_transaction(
&mut self,
watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send;
}
impl TransactionExecutor for RedisConnection {
fn execute_transaction(
&mut self,
watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send {
RedisConnection::execute_transaction(self, watch_frames, command_frames)
}
}
impl<C: TransactionExecutor + Send> TransactionExecutor for Arc<Mutex<C>> {
const SUPPORTS_TRANSACTION_RETRY: bool = false;
fn execute_transaction(
&mut self,
watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send {
let arc = Arc::clone(self);
async move {
arc.lock()
.await
.execute_transaction(watch_frames, command_frames)
.await
}
}
}
impl TransactionExecutor for RedisClient {
const SUPPORTS_TRANSACTION_RETRY: bool = false;
fn execute_transaction(
&mut self,
watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send {
let arc = Arc::clone(&self.inner);
async move {
arc.lock()
.await
.execute_transaction(watch_frames, command_frames)
.await
}
}
}
struct TransactionEntry {
frame: Frame,
parser: ResponseParser,
}
pub struct Transaction {
watch_keys: Vec<String>,
entries: Vec<TransactionEntry>,
}
#[must_use = "transaction result must be handled"]
pub enum TransactionResult {
Committed(PipelineResults),
Aborted,
}
impl TransactionResult {
pub fn committed(self) -> Option<PipelineResults> {
match self {
TransactionResult::Committed(results) => Some(results),
TransactionResult::Aborted => None,
}
}
pub fn is_aborted(&self) -> bool {
matches!(self, TransactionResult::Aborted)
}
pub fn is_committed(&self) -> bool {
matches!(self, TransactionResult::Committed(_))
}
#[track_caller]
pub fn unwrap(self) -> PipelineResults {
match self {
TransactionResult::Committed(results) => results,
TransactionResult::Aborted => {
panic!("called `TransactionResult::unwrap()` on an `Aborted` value")
}
}
}
}
impl Transaction {
pub fn new() -> Self {
Self {
watch_keys: Vec::new(),
entries: Vec::new(),
}
}
pub fn watch(mut self, keys: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.watch_keys.extend(keys.into_iter().map(Into::into));
self
}
pub fn push<Cmd: Command + 'static>(mut self, cmd: Cmd) -> Self {
let frame = cmd.to_frame();
let parser = Box::new(
move |response: Frame| -> Result<Box<dyn Any + Send>, RedisError> {
let result = cmd.parse_response(response)?;
Ok(Box::new(result))
},
);
self.entries.push(TransactionEntry { frame, parser });
self
}
pub async fn execute<E: TransactionExecutor>(
self,
conn: &mut E,
) -> Result<TransactionResult, RedisError> {
let watch_frames: Vec<Frame> = if self.watch_keys.is_empty() {
Vec::new()
} else {
let mut args = vec![bulk("WATCH")];
for key in &self.watch_keys {
args.push(bulk(key.as_str()));
}
vec![array(args)]
};
let command_frames: Vec<Frame> = self.entries.iter().map(|e| e.frame.clone()).collect();
let exec_result = conn
.execute_transaction(watch_frames, command_frames)
.await?;
match exec_result {
None => Ok(TransactionResult::Aborted),
Some(responses) => {
let mut results = Vec::with_capacity(responses.len());
for (entry, response) in self.entries.into_iter().zip(responses) {
if let Frame::Error(ref e) = response {
results.push(Err(RedisError::Redis(
String::from_utf8_lossy(e).into_owned(),
)));
} else {
results.push((entry.parser)(response));
}
}
Ok(TransactionResult::Committed(PipelineResults::from_raw(
results,
)))
}
}
}
pub fn len(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
}
impl Default for Transaction {
fn default() -> Self {
Self::new()
}
}
pub const DEFAULT_TRANSACTION_RETRIES: usize = 16;
pub async fn transaction<C, F>(
conn: &mut C,
keys: impl IntoIterator<Item = impl Into<String>>,
build: F,
) -> Result<PipelineResults, RedisError>
where
C: RedisExecutor + TransactionExecutor + Send,
F: AsyncFnMut(&mut C) -> Result<Transaction, RedisError>,
{
transaction_with_retries(conn, keys, DEFAULT_TRANSACTION_RETRIES, build).await
}
pub async fn transaction_with_retries<C, F>(
conn: &mut C,
keys: impl IntoIterator<Item = impl Into<String>>,
max_retries: usize,
mut build: F,
) -> Result<PipelineResults, RedisError>
where
C: RedisExecutor + TransactionExecutor + Send,
F: AsyncFnMut(&mut C) -> Result<Transaction, RedisError>,
{
conn.validate_transaction_retry()?;
let keys: Vec<String> = keys.into_iter().map(Into::into).collect();
let mut remaining = max_retries;
loop {
if !keys.is_empty() {
conn.execute(Watch::keys(keys.clone())).await?;
}
let txn = build(conn).await?;
match txn.execute(&mut *conn).await? {
TransactionResult::Committed(results) => return Ok(results),
TransactionResult::Aborted => {
if remaining == 0 {
return Err(RedisError::TransactionAborted);
}
remaining -= 1;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(unix)]
fn connected_pair() -> (RedisConnection, tokio::net::UnixStream) {
let (client, server) = tokio::net::UnixStream::pair().unwrap();
(
RedisConnection::from_stream(redis_tower_core::RedisStream::Unix(client)),
server,
)
}
#[cfg(unix)]
async fn assert_no_wire_request(mut server: tokio::net::UnixStream) {
use tokio::io::AsyncReadExt;
let mut bytes = [0u8; 128];
assert!(
tokio::time::timeout(
std::time::Duration::from_millis(25),
server.read(&mut bytes),
)
.await
.is_err(),
"transaction helper wrote to Redis before rejecting the executor"
);
}
fn assert_retry_helper_unsupported(result: &Result<PipelineResults, RedisError>) {
let error = match result {
Err(error) => error,
Ok(_) => panic!("shared executor was accepted"),
};
let message = error.to_string();
assert!(message.contains("transaction_with_retries()"));
assert!(message.contains("Transaction::watch()"));
}
fn committed() -> TransactionResult {
TransactionResult::Committed(PipelineResults::from_raw(Vec::new()))
}
#[test]
fn retry_helper_capability_is_limited_to_exclusive_connections() {
const {
assert!(<RedisConnection as TransactionExecutor>::SUPPORTS_TRANSACTION_RETRY);
assert!(!<RedisClient as TransactionExecutor>::SUPPORTS_TRANSACTION_RETRY);
assert!(!<crate::MultiplexedClient as TransactionExecutor>::SUPPORTS_TRANSACTION_RETRY);
assert!(
!<Arc<Mutex<RedisConnection>> as TransactionExecutor>::SUPPORTS_TRANSACTION_RETRY
);
}
}
#[test]
fn committed_returns_some_for_committed() {
assert!(committed().committed().is_some());
}
#[test]
fn committed_returns_none_for_aborted() {
assert!(TransactionResult::Aborted.committed().is_none());
}
#[test]
fn is_aborted_reports_correctly() {
assert!(TransactionResult::Aborted.is_aborted());
assert!(!committed().is_aborted());
}
#[test]
fn is_committed_reports_correctly() {
assert!(committed().is_committed());
assert!(!TransactionResult::Aborted.is_committed());
}
#[test]
fn unwrap_succeeds_on_committed() {
let results = committed().unwrap();
assert!(results.is_empty());
}
#[test]
#[should_panic(expected = "Aborted")]
fn unwrap_panics_on_aborted() {
let _ = TransactionResult::Aborted.unwrap();
}
struct MockTransactionConn {
responses: Option<Vec<Frame>>,
}
impl TransactionExecutor for MockTransactionConn {
fn execute_transaction(
&mut self,
_watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send {
let responses = self.responses.take().map(|_| {
command_frames
.iter()
.map(|_| Frame::SimpleString(bytes::Bytes::from("OK")))
.collect::<Vec<_>>()
});
async move { Ok(responses) }
}
}
#[tokio::test]
async fn transaction_execute_accepts_transaction_executor() {
use redis_tower_commands::Set;
let mut conn = MockTransactionConn {
responses: Some(vec![]),
};
let result = Transaction::new()
.push(Set::new("x", "1"))
.execute(&mut conn)
.await
.unwrap();
assert!(result.is_committed());
}
#[tokio::test]
async fn transaction_execute_accepts_arc_mutex_executor() {
use redis_tower_commands::Set;
let conn = Arc::new(Mutex::new(MockTransactionConn {
responses: Some(vec![]),
}));
let mut executor = Arc::clone(&conn);
let result = Transaction::new()
.push(Set::new("x", "1"))
.execute(&mut executor)
.await
.unwrap();
assert!(result.is_committed());
}
struct RetryMockConn {
aborts_before_commit: usize,
exec_calls: usize,
watch_calls: usize,
}
impl RedisExecutor for RetryMockConn {
fn execute<Cmd: Command>(
&mut self,
cmd: Cmd,
) -> impl Future<Output = Result<Cmd::Response, RedisError>> + Send {
self.watch_calls += 1;
let result = cmd.parse_response(Frame::SimpleString(bytes::Bytes::from("OK")));
async move { result }
}
}
impl TransactionExecutor for RetryMockConn {
fn execute_transaction(
&mut self,
_watch_frames: Vec<Frame>,
command_frames: Vec<Frame>,
) -> impl Future<Output = Result<Option<Vec<Frame>>, RedisError>> + Send {
self.exec_calls += 1;
let abort = self.aborts_before_commit > 0;
if abort {
self.aborts_before_commit -= 1;
}
let responses = if abort {
None
} else {
Some(
command_frames
.iter()
.map(|_| Frame::SimpleString(bytes::Bytes::from("OK")))
.collect::<Vec<_>>(),
)
};
async move { Ok(responses) }
}
}
#[tokio::test]
async fn transaction_helper_rejects_arc_mutex_before_watch_or_build() {
use std::sync::atomic::{AtomicUsize, Ordering};
let inner = Arc::new(Mutex::new(RetryMockConn {
aborts_before_commit: 0,
exec_calls: 0,
watch_calls: 0,
}));
let mut shared = Arc::clone(&inner);
let build_calls = Arc::new(AtomicUsize::new(0));
let build_counter = Arc::clone(&build_calls);
let result = transaction_with_retries(
&mut shared,
["key"],
0,
async move |_conn: &mut Arc<Mutex<RetryMockConn>>| {
build_counter.fetch_add(1, Ordering::SeqCst);
Ok(Transaction::new())
},
)
.await;
assert_retry_helper_unsupported(&result);
assert_eq!(build_calls.load(Ordering::SeqCst), 0);
let inner = inner.lock().await;
assert_eq!(inner.watch_calls, 0);
assert_eq!(inner.exec_calls, 0);
}
#[tokio::test]
#[cfg(unix)]
async fn transaction_helper_rejects_redis_client_before_watch_or_build() {
use std::sync::atomic::{AtomicUsize, Ordering};
let (conn, server) = connected_pair();
let mut client = RedisClient::from_connection(conn);
let build_calls = Arc::new(AtomicUsize::new(0));
let build_counter = Arc::clone(&build_calls);
let result = transaction_with_retries(
&mut client,
["key"],
0,
async move |_conn: &mut RedisClient| {
build_counter.fetch_add(1, Ordering::SeqCst);
Ok(Transaction::new())
},
)
.await;
assert_retry_helper_unsupported(&result);
assert_eq!(build_calls.load(Ordering::SeqCst), 0);
assert_no_wire_request(server).await;
}
#[tokio::test]
#[cfg(unix)]
async fn transaction_helper_rejects_multiplexed_client_before_watch_or_build() {
use std::sync::atomic::{AtomicUsize, Ordering};
let (conn, server) = connected_pair();
let mut client = crate::MultiplexedClient::from_connection(conn);
let build_calls = Arc::new(AtomicUsize::new(0));
let build_counter = Arc::clone(&build_calls);
let result = transaction_with_retries(
&mut client,
["key"],
0,
async move |_conn: &mut crate::MultiplexedClient| {
build_counter.fetch_add(1, Ordering::SeqCst);
Ok(Transaction::new())
},
)
.await;
assert_retry_helper_unsupported(&result);
assert_eq!(build_calls.load(Ordering::SeqCst), 0);
assert_no_wire_request(server).await;
client.shutdown().await;
}
#[tokio::test]
async fn transaction_commits_on_first_attempt() {
use redis_tower_commands::Set;
let mut conn = RetryMockConn {
aborts_before_commit: 0,
exec_calls: 0,
watch_calls: 0,
};
let results = transaction(&mut conn, ["counter"], async |_c| {
Ok(Transaction::new().push(Set::new("counter", "1")))
})
.await
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(conn.exec_calls, 1, "should EXEC exactly once");
assert_eq!(conn.watch_calls, 1, "should WATCH exactly once");
}
#[tokio::test]
async fn transaction_retries_until_commit() {
use std::sync::atomic::{AtomicUsize, Ordering};
use redis_tower_commands::Set;
let mut conn = RetryMockConn {
aborts_before_commit: 2,
exec_calls: 0,
watch_calls: 0,
};
let build_calls = Arc::new(AtomicUsize::new(0));
let bc = Arc::clone(&build_calls);
let results = transaction(&mut conn, ["k"], async move |_c: &mut RetryMockConn| {
bc.fetch_add(1, Ordering::SeqCst);
Ok(Transaction::new().push(Set::new("k", "1")))
})
.await
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(build_calls.load(Ordering::SeqCst), 3);
assert_eq!(conn.exec_calls, 3);
assert_eq!(conn.watch_calls, 3);
}
#[tokio::test]
async fn transaction_gives_up_after_retry_cap() {
use std::sync::atomic::{AtomicUsize, Ordering};
use redis_tower_commands::Set;
let mut conn = RetryMockConn {
aborts_before_commit: usize::MAX,
exec_calls: 0,
watch_calls: 0,
};
let build_calls = Arc::new(AtomicUsize::new(0));
let bc = Arc::clone(&build_calls);
let result =
transaction_with_retries(&mut conn, ["k"], 2, async move |_c: &mut RetryMockConn| {
bc.fetch_add(1, Ordering::SeqCst);
Ok(Transaction::new().push(Set::new("k", "1")))
})
.await;
assert!(matches!(result, Err(RedisError::TransactionAborted)));
assert_eq!(build_calls.load(Ordering::SeqCst), 3);
assert_eq!(conn.exec_calls, 3);
}
#[tokio::test]
async fn transaction_without_keys_skips_watch() {
use redis_tower_commands::Set;
let mut conn = RetryMockConn {
aborts_before_commit: 0,
exec_calls: 0,
watch_calls: 0,
};
let empty: [&str; 0] = [];
let results = transaction(&mut conn, empty, async |_c| {
Ok(Transaction::new().push(Set::new("k", "1")))
})
.await
.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(conn.exec_calls, 1);
assert_eq!(conn.watch_calls, 0, "no keys means no WATCH round-trip");
}
#[tokio::test]
async fn transaction_zero_retries_surfaces_abort_immediately() {
use redis_tower_commands::Set;
let mut conn = RetryMockConn {
aborts_before_commit: usize::MAX,
exec_calls: 0,
watch_calls: 0,
};
let result = transaction_with_retries(&mut conn, ["k"], 0, async |_c| {
Ok(Transaction::new().push(Set::new("k", "1")))
})
.await;
assert!(matches!(result, Err(RedisError::TransactionAborted)));
assert_eq!(conn.exec_calls, 1, "cap of 0 makes a single attempt");
}
}