reinhardt-db 0.1.0

Django-style database layer for Reinhardt framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! CockroachDB Distributed Transaction Support
//!
//! This module implements distributed transaction handling for CockroachDB,
//! including automatic retry logic for serialization conflicts and support
//! for AS OF SYSTEM TIME queries.
//!
//! CockroachDB uses serializable isolation by default and may require
//! transaction retries when conflicts occur. This module provides helpers
//! to handle these scenarios gracefully.

use sqlx::{PgPool, Postgres, Row, Transaction};
use std::sync::Arc;
use std::time::Duration;

use crate::backends::error::{DatabaseError, Result};

/// Maximum number of transaction retry attempts
const MAX_RETRIES: u32 = 5;

/// Base backoff duration for retry logic
const BASE_BACKOFF_MS: u64 = 100;

/// CockroachDB distributed transaction manager
///
/// Handles transaction retries and AS OF SYSTEM TIME queries for CockroachDB.
///
/// # Examples
///
/// ```no_run
/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
/// use sqlx::PgPool;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
/// let tx_manager = CockroachDBTransactionManager::new(pool);
///
/// // Execute a transaction with automatic retry
/// tx_manager.execute_with_retry(|tx| Box::pin(async move {
///     sqlx::query("INSERT INTO users (name) VALUES ($1)")
///         .bind("Alice")
///         .execute(&mut **tx)
///         .await?;
///     Ok(())
/// })).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct CockroachDBTransactionManager {
	pool: Arc<PgPool>,
	max_retries: u32,
	base_backoff: Duration,
}

impl CockroachDBTransactionManager {
	/// Create a new CockroachDB transaction manager
	///
	/// # Examples
	///
	/// ```no_run
	/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
	/// use sqlx::PgPool;
	///
	/// # async fn example() -> Result<(), sqlx::Error> {
	/// let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
	/// let tx_manager = CockroachDBTransactionManager::new(pool);
	/// # Ok(())
	/// # }
	/// ```
	pub fn new(pool: PgPool) -> Self {
		Self {
			pool: Arc::new(pool),
			max_retries: MAX_RETRIES,
			base_backoff: Duration::from_millis(BASE_BACKOFF_MS),
		}
	}

	/// Create from an `Arc<PgPool>`
	pub fn from_pool_arc(pool: Arc<PgPool>) -> Self {
		Self {
			pool,
			max_retries: MAX_RETRIES,
			base_backoff: Duration::from_millis(BASE_BACKOFF_MS),
		}
	}

	/// Set maximum retry attempts
	///
	/// # Examples
	///
	/// ```no_run
	/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
	/// use sqlx::PgPool;
	///
	/// # async fn example() -> Result<(), sqlx::Error> {
	/// let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
	/// let tx_manager = CockroachDBTransactionManager::new(pool)
	///     .with_max_retries(10);
	/// # Ok(())
	/// # }
	/// ```
	pub fn with_max_retries(mut self, max_retries: u32) -> Self {
		self.max_retries = max_retries;
		self
	}

	/// Set base backoff duration
	///
	/// # Examples
	///
	/// ```no_run
	/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
	/// use sqlx::PgPool;
	/// use std::time::Duration;
	///
	/// # async fn example() -> Result<(), sqlx::Error> {
	/// let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
	/// let tx_manager = CockroachDBTransactionManager::new(pool)
	///     .with_base_backoff(Duration::from_millis(200));
	/// # Ok(())
	/// # }
	/// ```
	pub fn with_base_backoff(mut self, base_backoff: Duration) -> Self {
		self.base_backoff = base_backoff;
		self
	}

	/// Execute a transaction with automatic retry on serialization conflicts
	///
	/// This method will retry the transaction if it encounters a serialization
	/// error (SQLSTATE 40001). The retry uses exponential backoff.
	///
	/// # Examples
	///
	/// ```no_run
	/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
	/// use sqlx::PgPool;
	///
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// # let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
	/// # let tx_manager = CockroachDBTransactionManager::new(pool);
	/// tx_manager.execute_with_retry(|tx| Box::pin(async move {
	///     sqlx::query("UPDATE accounts SET balance = balance + $1 WHERE id = $2")
	///         .bind(100)
	///         .bind(1)
	///         .execute(&mut **tx)
	///         .await?;
	///     Ok(())
	/// })).await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn execute_with_retry<F, T>(&self, mut f: F) -> Result<T>
	where
		F: for<'a> FnMut(
			&'a mut Transaction<'_, Postgres>,
		) -> std::pin::Pin<
			Box<dyn std::future::Future<Output = Result<T>> + Send + 'a>,
		>,
		T: Send,
	{
		let mut attempt = 0;

		loop {
			let mut tx = self.pool.begin().await.map_err(DatabaseError::from)?;

			match f(&mut tx).await {
				Ok(result) => {
					tx.commit().await.map_err(DatabaseError::from)?;
					return Ok(result);
				}
				Err(e) => {
					let _ = tx.rollback().await;

					if Self::is_serialization_error(&e) && attempt < self.max_retries {
						attempt += 1;
						let _backoff = self.calculate_backoff(attempt);
						continue;
					}

					return Err(e);
				}
			}
		}
	}

	/// Get a reference to the underlying pool for direct queries
	///
	/// Use this to construct custom queries with AS OF SYSTEM TIME.
	///
	/// # Examples
	///
	/// ```no_run
	/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
	/// use sqlx::PgPool;
	///
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// # let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
	/// # let tx_manager = CockroachDBTransactionManager::new(pool);
	/// // Use the pool to execute queries with AS OF SYSTEM TIME
	/// let pool = tx_manager.pool();
	/// let rows = sqlx::query("SELECT * FROM users AS OF SYSTEM TIME '-5s'")
	///     .fetch_all(pool)
	///     .await?;
	/// # Ok(())
	/// # }
	/// ```
	pub fn pool(&self) -> &PgPool {
		&self.pool
	}

	/// Execute a transaction with a specified priority
	///
	/// CockroachDB supports transaction priorities (LOW, NORMAL, HIGH).
	/// Higher priority transactions are less likely to be aborted.
	///
	/// # Examples
	///
	/// ```no_run
	/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
	/// use sqlx::PgPool;
	///
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// # let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
	/// # let tx_manager = CockroachDBTransactionManager::new(pool);
	/// tx_manager.execute_with_priority("HIGH", |tx| Box::pin(async move {
	///     sqlx::query("INSERT INTO users (name) VALUES ($1)")
	///         .bind("Alice")
	///         .execute(&mut **tx)
	///         .await?;
	///     Ok(())
	/// })).await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn execute_with_priority<F, T>(&self, priority: &str, mut f: F) -> Result<T>
	where
		F: for<'a> FnMut(
			&'a mut Transaction<'_, Postgres>,
		) -> std::pin::Pin<
			Box<dyn std::future::Future<Output = Result<T>> + Send + 'a>,
		>,
		T: Send,
	{
		let mut tx = self.pool.begin().await.map_err(DatabaseError::from)?;

		let sql = format!("SET TRANSACTION PRIORITY {}", priority);
		sqlx::raw_sql(&sql)
			.execute(&mut *tx)
			.await
			.map_err(DatabaseError::from)?;

		let result = f(&mut tx).await?;
		tx.commit().await.map_err(DatabaseError::from)?;

		Ok(result)
	}

	/// Check if error is a serialization/retry error
	fn is_serialization_error(error: &DatabaseError) -> bool {
		match error {
			DatabaseError::QueryError(msg) => {
				// CockroachDB serialization error (SQLSTATE 40001)
				msg.contains("40001")
					|| msg.contains("restart transaction")
					|| msg.contains("serialization failure")
			}
			_ => false,
		}
	}

	/// Calculate exponential backoff with jitter
	fn calculate_backoff(&self, attempt: u32) -> Duration {
		let backoff = self.base_backoff.as_millis() as u64 * 2u64.pow(attempt);
		let jitter = (rand::random::<f64>() * 0.3 + 0.85) * backoff as f64;
		Duration::from_millis(jitter as u64)
	}

	/// Get cluster information
	///
	/// Query SHOW CLUSTER SETTING version for cluster version info.
	///
	/// # Examples
	///
	/// ```no_run
	/// use reinhardt_db::backends::drivers::cockroachdb::distributed_tx::CockroachDBTransactionManager;
	/// use sqlx::PgPool;
	///
	/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
	/// # let pool = PgPool::connect("postgresql://localhost:26257/mydb").await?;
	/// # let tx_manager = CockroachDBTransactionManager::new(pool);
	/// let info = tx_manager.get_cluster_info().await?;
	/// println!("Cluster version: {}", info.version);
	/// # Ok(())
	/// # }
	/// ```
	pub async fn get_cluster_info(&self) -> Result<ClusterInfo> {
		let row = sqlx::query("SHOW CLUSTER SETTING version")
			.fetch_one(self.pool.as_ref())
			.await
			.map_err(DatabaseError::from)?;

		let version: String = row.try_get(0).map_err(DatabaseError::from)?;

		Ok(ClusterInfo { version })
	}
}

/// CockroachDB cluster information
#[derive(Debug, Clone, PartialEq)]
pub struct ClusterInfo {
	/// CockroachDB version
	pub version: String,
}

// Add rand dependency internally for jitter calculation
mod rand {
	use std::cell::RefCell;
	use std::time::{SystemTime, UNIX_EPOCH};

	thread_local! {
		static RNG: RefCell<u64> = RefCell::new(
			SystemTime::now()
				.duration_since(UNIX_EPOCH)
				.unwrap_or_default()
				.as_nanos() as u64
		);
	}

	pub(crate) fn random<T: RandomValue>() -> T {
		T::random()
	}

	pub(crate) trait RandomValue {
		fn random() -> Self;
	}

	impl RandomValue for f64 {
		fn random() -> Self {
			RNG.with(|rng| {
				let mut state = rng.borrow_mut();
				// Simple LCG for jitter
				*state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
				(*state >> 11) as f64 / ((1u64 << 53) as f64)
			})
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_cluster_info_creation() {
		let info = ClusterInfo {
			version: "v23.1.0".to_string(),
		};

		assert_eq!(info.version, "v23.1.0");
	}

	#[test]
	fn test_is_serialization_error() {
		let err1 = DatabaseError::QueryError("SQLSTATE 40001: restart transaction".to_string());
		assert!(CockroachDBTransactionManager::is_serialization_error(&err1));

		let err2 = DatabaseError::QueryError("serialization failure".to_string());
		assert!(CockroachDBTransactionManager::is_serialization_error(&err2));

		let err3 = DatabaseError::QueryError("some other error".to_string());
		assert!(!CockroachDBTransactionManager::is_serialization_error(
			&err3
		));

		let err4 = DatabaseError::ConnectionError("connection failed".to_string());
		assert!(!CockroachDBTransactionManager::is_serialization_error(
			&err4
		));
	}

	#[tokio::test]
	async fn test_with_max_retries() {
		let pool = Arc::new(
			PgPool::connect_lazy("postgresql://localhost:26257/testdb")
				.expect("Failed to create lazy pool"),
		);
		let tx_manager = CockroachDBTransactionManager::from_pool_arc(pool).with_max_retries(10);

		assert_eq!(tx_manager.max_retries, 10);
	}

	#[tokio::test]
	async fn test_with_base_backoff() {
		let pool = Arc::new(
			PgPool::connect_lazy("postgresql://localhost:26257/testdb")
				.expect("Failed to create lazy pool"),
		);
		let tx_manager = CockroachDBTransactionManager::from_pool_arc(pool)
			.with_base_backoff(Duration::from_millis(200));

		assert_eq!(tx_manager.base_backoff, Duration::from_millis(200));
	}

	#[tokio::test]
	async fn test_calculate_backoff() {
		let pool = Arc::new(
			PgPool::connect_lazy("postgresql://localhost:26257/testdb")
				.expect("Failed to create lazy pool"),
		);
		let tx_manager = CockroachDBTransactionManager::from_pool_arc(pool);

		// Test that backoff increases with attempts
		let backoff1 = tx_manager.calculate_backoff(1);
		let backoff2 = tx_manager.calculate_backoff(2);

		// backoff2 should be roughly double backoff1 (with jitter)
		assert!(backoff2 > backoff1);
	}

	#[test]
	fn test_random_f64() {
		let val1: f64 = rand::random();
		let val2: f64 = rand::random();

		// Check values are in range [0, 1)
		assert!((0.0..1.0).contains(&val1));
		assert!((0.0..1.0).contains(&val2));

		// Values should be different (extremely unlikely to be equal)
		assert_ne!(val1, val2);
	}
}