linkrs-transaction 0.1.0

Transaction management for Linkrs
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Per-Connection Transaction Context
//!
//! Provides connection-level transaction management mirroring Ladybug's
//! `TransactionContext` hierarchy (Connection -> Transaction -> Manager).
//! Each client connection owns a `ConnectionContext` that tracks the
//! current transaction and whether the connection operates in AUTO or MANUAL mode.
//!
//! In AUTO mode every statement auto-commits (single-statement transaction).
//! In MANUAL mode the client must explicitly BEGIN/COMMIT.

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;

use parking_lot::RwLock;

use crate::error::TransactionError;
use crate::manager::TransactionManager;
use crate::types::{SavepointId, TransactionId, TransactionOptions};

/// Unique connection identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ConnectionId(pub u64);

impl std::fmt::Display for ConnectionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "conn-{}", self.0)
    }
}

impl From<u64> for ConnectionId {
    fn from(v: u64) -> Self {
        Self(v)
    }
}

impl From<ConnectionId> for u64 {
    fn from(c: ConnectionId) -> Self {
        c.0
    }
}

/// Transaction mode for a connection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TransactionMode {
    /// Each statement runs in its own auto-committed transaction.
    #[default]
    AutoCommit,
    /// Client must explicitly BEGIN and COMMIT/ROLLBACK.
    Manual,
}

impl std::fmt::Display for TransactionMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TransactionMode::AutoCommit => write!(f, "AUTO"),
            TransactionMode::Manual => write!(f, "MANUAL"),
        }
    }
}

/// Per-connection transaction context.
///
/// This is the connection-level analogue of Ladybug's `TransactionContext`.
/// It sits above `TransactionContext` (per-transaction) and is owned by the
/// client/session layer. The `TransactionManager` remains the global coordinator.
#[derive(Debug)]
pub struct ConnectionContext {
    id: ConnectionId,
    mode: RwLock<TransactionMode>,
    current_transaction: RwLock<Option<TransactionId>>,
    savepoint_stack: RwLock<Vec<SavepointId>>,
    transaction_options: RwLock<TransactionOptions>,
    auto_commit: RwLock<bool>,
    created_at: Instant,
}

impl ConnectionContext {
    /// Create a new connection context in AUTO mode.
    pub fn new(id: ConnectionId) -> Self {
        Self {
            id,
            mode: RwLock::new(TransactionMode::AutoCommit),
            current_transaction: RwLock::new(None),
            savepoint_stack: RwLock::new(Vec::new()),
            transaction_options: RwLock::new(TransactionOptions::default()),
            auto_commit: RwLock::new(true),
            created_at: Instant::now(),
        }
    }

    /// Create with explicit mode.
    pub fn with_mode(id: ConnectionId, mode: TransactionMode) -> Self {
        let auto_commit = mode == TransactionMode::AutoCommit;
        Self {
            id,
            mode: RwLock::new(mode),
            current_transaction: RwLock::new(None),
            savepoint_stack: RwLock::new(Vec::new()),
            transaction_options: RwLock::new(TransactionOptions::default()),
            auto_commit: RwLock::new(auto_commit),
            created_at: Instant::now(),
        }
    }

    pub fn id(&self) -> ConnectionId {
        self.id
    }

    pub fn mode(&self) -> TransactionMode {
        *self.mode.read()
    }

    pub fn set_mode(&self, mode: TransactionMode) {
        *self.mode.write() = mode;
        *self.auto_commit.write() = mode == TransactionMode::AutoCommit;
    }

    pub fn current_transaction(&self) -> Option<TransactionId> {
        *self.current_transaction.read()
    }

    pub fn has_active_transaction(&self) -> bool {
        self.current_transaction().is_some()
    }

    pub fn bind_transaction(&self, txn_id: TransactionId) {
        *self.current_transaction.write() = Some(txn_id);
    }

    pub fn unbind_transaction(&self) {
        *self.current_transaction.write() = None;
        self.savepoint_stack.write().clear();
    }

    pub fn is_auto_commit(&self) -> bool {
        *self.auto_commit.read()
    }

    pub fn set_auto_commit(&self, auto_commit: bool) {
        *self.auto_commit.write() = auto_commit;
        let mode = if auto_commit {
            TransactionMode::AutoCommit
        } else {
            TransactionMode::Manual
        };
        *self.mode.write() = mode;
    }

    pub fn transaction_options(&self) -> TransactionOptions {
        self.transaction_options.read().clone()
    }

    pub fn set_transaction_options(&self, options: TransactionOptions) {
        *self.transaction_options.write() = options;
    }

    pub fn push_savepoint(&self, savepoint_id: SavepointId) {
        self.savepoint_stack.write().push(savepoint_id);
    }

    pub fn savepoint_stack(&self) -> Vec<SavepointId> {
        self.savepoint_stack.read().clone()
    }

    pub fn clear_savepoints(&self) {
        self.savepoint_stack.write().clear();
    }

    pub fn savepoint_count(&self) -> usize {
        self.savepoint_stack.read().len()
    }

    pub fn created_at(&self) -> Instant {
        self.created_at
    }

    pub fn elapsed(&self) -> std::time::Duration {
        self.created_at.elapsed()
    }
}

/// Manager for per-connection contexts.
///
/// Provides connection lifecycle and AUTO/MANUAL execution helpers.
/// This reduces `TransactionManager` contention by grouping transactions
/// under their owning connection.
pub struct ConnectionManager {
    connections: dashmap::DashMap<ConnectionId, std::sync::Arc<ConnectionContext>>,
    id_generator: AtomicU64,
}

impl ConnectionManager {
    pub fn new() -> Self {
        Self {
            connections: dashmap::DashMap::new(),
            id_generator: AtomicU64::new(1),
        }
    }

    /// Create a new connection in AUTO mode and return its id.
    pub fn create_connection(&self) -> ConnectionId {
        self.create_connection_with_mode(TransactionMode::AutoCommit)
    }

    pub fn create_connection_with_mode(&self, mode: TransactionMode) -> ConnectionId {
        let id = ConnectionId(self.id_generator.fetch_add(1, Ordering::SeqCst));
        let ctx = std::sync::Arc::new(ConnectionContext::with_mode(id, mode));
        self.connections.insert(id, ctx);
        id
    }

    pub fn get_connection(&self, id: ConnectionId) -> Option<std::sync::Arc<ConnectionContext>> {
        self.connections.get(&id).map(|e| e.value().clone())
    }

    pub fn remove_connection(&self, id: ConnectionId) -> Option<std::sync::Arc<ConnectionContext>> {
        self.connections.remove(&id).map(|(_, v)| v)
    }

    pub fn connection_count(&self) -> usize {
        self.connections.len()
    }

    /// Canonical owner string for transactions bound to a connection.
    ///
    /// The transaction-level `owner` field carries this projection of the
    /// `ConnectionId`, so manager-level ownership checks
    /// (`check_transaction_owner`) and connection binding share one source
    /// instead of two parallel mechanisms. The `conn:` prefix namespaces
    /// connection owners away from session-id owners used by the HTTP/gRPC
    /// APIs (`begin_transaction_with_owner`), so a session id that happens
    /// to equal a connection id can never claim another connection's
    /// transaction.
    pub fn owner_for_connection(conn_id: ConnectionId) -> String {
        format!("conn:{conn_id}")
    }

    /// Begin a transaction for the given connection.
    ///
    /// In AUTO mode this is used internally by `execute_auto_commit`.
    /// In MANUAL mode the caller must explicitly commit/abort.
    ///
    /// The transaction is stamped with this connection as its owner, so a
    /// different connection cannot commit or abort it through the
    /// owner-checked manager APIs.
    pub fn begin_for_connection(
        &self,
        manager: &TransactionManager,
        conn_id: ConnectionId,
        options: TransactionOptions,
    ) -> Result<TransactionId, TransactionError> {
        let conn = self.get_connection(conn_id).ok_or_else(|| {
            TransactionError::internal(format!("Connection {} not found", conn_id))
        })?;
        if conn.has_active_transaction() {
            return Err(TransactionError::internal(format!(
                "Connection {} already has active transaction {:?}",
                conn_id,
                conn.current_transaction()
            )));
        }
        let txn_id = manager.begin_transaction(options)?;
        if let Err(error) =
            manager.set_transaction_owner(txn_id, Self::owner_for_connection(conn_id))
        {
            let _ = manager.abort_transaction(txn_id);
            return Err(error);
        }
        conn.bind_transaction(txn_id);
        Ok(txn_id)
    }

    /// Commit the connection's current transaction.
    ///
    /// Ownership is verified before committing: a transaction bound to
    /// another connection is rejected with `transaction_not_owner`.
    pub fn commit_for_connection(
        &self,
        manager: &TransactionManager,
        conn_id: ConnectionId,
    ) -> Result<(), TransactionError> {
        let conn = self.get_connection(conn_id).ok_or_else(|| {
            TransactionError::internal(format!("Connection {} not found", conn_id))
        })?;
        let txn_id = conn.current_transaction().ok_or_else(|| {
            TransactionError::internal(format!("Connection {} has no active transaction", conn_id))
        })?;
        manager.check_transaction_owner(txn_id, Some(&Self::owner_for_connection(conn_id)))?;
        manager.commit_transaction(txn_id)?;
        conn.unbind_transaction();
        Ok(())
    }

    /// Abort the connection's current transaction.
    ///
    /// Ownership is verified before aborting: a transaction bound to
    /// another connection is rejected with `transaction_not_owner`.
    pub fn abort_for_connection(
        &self,
        manager: &TransactionManager,
        conn_id: ConnectionId,
    ) -> Result<(), TransactionError> {
        let conn = self.get_connection(conn_id).ok_or_else(|| {
            TransactionError::internal(format!("Connection {} not found", conn_id))
        })?;
        let txn_id = conn.current_transaction().ok_or_else(|| {
            TransactionError::internal(format!("Connection {} has no active transaction", conn_id))
        })?;
        manager.check_transaction_owner(txn_id, Some(&Self::owner_for_connection(conn_id)))?;
        manager.abort_transaction(txn_id)?;
        conn.unbind_transaction();
        Ok(())
    }

    /// Execute a single statement with AUTO-commit semantics for the connection.
    ///
    /// If the connection is in AUTO mode, a transaction is begun, the closure
    /// is executed, and the transaction is committed (or aborted on error).
    /// If in MANUAL mode and a transaction is already active, the closure runs
    /// inside that transaction without auto-commit. A MANUAL connection
    /// without an active transaction is an explicit error instead of an
    /// implicit one-shot transaction.
    pub fn execute_auto_commit<F, T, E>(
        &self,
        manager: &TransactionManager,
        conn_id: ConnectionId,
        options: TransactionOptions,
        operation: F,
    ) -> Result<T, TransactionError>
    where
        F: FnOnce(&crate::context::TransactionContext) -> Result<T, E>,
        E: Into<TransactionError>,
    {
        let conn = self.get_connection(conn_id).ok_or_else(|| {
            TransactionError::internal(format!("Connection {} not found", conn_id))
        })?;

        // If MANUAL with active txn, run inside existing transaction
        if conn.mode() == TransactionMode::Manual {
            if let Some(txn_id) = conn.current_transaction() {
                let ctx = manager.get_context(txn_id)?;
                return operation(&ctx).map_err(Into::into);
            }
            return Err(TransactionError::internal(format!(
                "Connection {} in MANUAL mode has no active transaction",
                conn_id
            )));
        }

        // AUTO mode: one-shot transaction (MANUAL without an active
        // transaction was rejected above).
        let txn_id = manager.begin_transaction(options)?;
        // Only bind if MANUAL mode expects to keep it (but for auto-commit helper we commit immediately,
        // so we don't bind to connection; we just use the txn directly)
        let ctx = manager.get_context(txn_id)?;
        match operation(&ctx) {
            Ok(result) => {
                manager.commit_transaction(txn_id)?;
                Ok(result)
            }
            Err(e) => {
                let _ = manager.abort_transaction(txn_id);
                Err(e.into())
            }
        }
    }

    /// Execute with connection's existing transaction or create auto-commit.
    pub fn with_connection_transaction<F, T, E>(
        &self,
        manager: &TransactionManager,
        conn_id: ConnectionId,
        operation: F,
    ) -> Result<T, TransactionError>
    where
        F: FnOnce(&crate::context::TransactionContext) -> Result<T, E>,
        E: Into<TransactionError>,
    {
        let conn = self.get_connection(conn_id).ok_or_else(|| {
            TransactionError::internal(format!("Connection {} not found", conn_id))
        })?;

        if let Some(txn_id) = conn.current_transaction() {
            let ctx = manager.get_context(txn_id)?;
            return operation(&ctx).map_err(Into::into);
        }

        if conn.is_auto_commit() {
            self.execute_auto_commit(manager, conn_id, conn.transaction_options(), operation)
        } else {
            Err(TransactionError::internal(format!(
                "Connection {} in MANUAL mode has no active transaction",
                conn_id
            )))
        }
    }
}

impl Default for ConnectionManager {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{TransactionManager, TransactionManagerConfig};

    #[test]
    fn test_connection_context_basic() {
        let ctx = ConnectionContext::new(ConnectionId(1));
        assert_eq!(ctx.id(), ConnectionId(1));
        assert_eq!(ctx.mode(), TransactionMode::AutoCommit);
        assert!(ctx.is_auto_commit());
        assert!(!ctx.has_active_transaction());
        assert_eq!(ctx.current_transaction(), None);
    }

    #[test]
    fn test_connection_context_mode_switch() {
        let ctx = ConnectionContext::new(ConnectionId(1));
        ctx.set_mode(TransactionMode::Manual);
        assert_eq!(ctx.mode(), TransactionMode::Manual);
        assert!(!ctx.is_auto_commit());

        ctx.set_auto_commit(true);
        assert_eq!(ctx.mode(), TransactionMode::AutoCommit);
        assert!(ctx.is_auto_commit());
    }

    #[test]
    fn test_connection_context_bind_unbind() {
        let ctx = ConnectionContext::new(ConnectionId(1));
        let txn_id = TransactionId(42);
        ctx.bind_transaction(txn_id);
        assert_eq!(ctx.current_transaction(), Some(txn_id));
        assert!(ctx.has_active_transaction());

        ctx.push_savepoint(10);
        ctx.push_savepoint(20);
        assert_eq!(ctx.savepoint_count(), 2);

        ctx.unbind_transaction();
        assert_eq!(ctx.current_transaction(), None);
        assert_eq!(ctx.savepoint_count(), 0);
    }

    #[test]
    fn test_connection_manager_create_and_remove() {
        let mgr = ConnectionManager::new();
        let id1 = mgr.create_connection();
        let id2 = mgr.create_connection_with_mode(TransactionMode::Manual);
        assert_ne!(id1, id2);
        assert_eq!(mgr.connection_count(), 2);

        let ctx = mgr.get_connection(id1).unwrap();
        assert_eq!(ctx.id(), id1);

        mgr.remove_connection(id1);
        assert_eq!(mgr.connection_count(), 1);
        assert!(mgr.get_connection(id1).is_none());
    }

    #[test]
    fn test_connection_manager_begin_commit() {
        let txn_mgr = TransactionManager::new(TransactionManagerConfig::default());
        let conn_mgr = ConnectionManager::new();
        let conn_id = conn_mgr.create_connection_with_mode(TransactionMode::Manual);

        let txn_id = conn_mgr
            .begin_for_connection(&txn_mgr, conn_id, crate::TransactionOptions::default())
            .expect("begin should succeed");
        assert_eq!(
            conn_mgr
                .get_connection(conn_id)
                .unwrap()
                .current_transaction(),
            Some(txn_id)
        );

        conn_mgr
            .commit_for_connection(&txn_mgr, conn_id)
            .expect("commit should succeed");
        assert_eq!(
            conn_mgr
                .get_connection(conn_id)
                .unwrap()
                .current_transaction(),
            None
        );
    }

    #[test]
    fn test_connection_manager_auto_commit_execution() {
        let txn_mgr = TransactionManager::new(TransactionManagerConfig::default());
        let conn_mgr = ConnectionManager::new();
        let conn_id = conn_mgr.create_connection();

        let result: Result<u64, TransactionError> = conn_mgr.execute_auto_commit(
            &txn_mgr,
            conn_id,
            crate::TransactionOptions::default(),
            |ctx| Ok::<u64, TransactionError>(ctx.id.0),
        );
        assert!(result.is_ok());
        assert!(result.unwrap() > 0);
        // AUTO execution should not leave transaction bound
        assert_eq!(
            conn_mgr
                .get_connection(conn_id)
                .unwrap()
                .current_transaction(),
            None
        );
    }

    #[test]
    fn test_read_only_transaction_rejects_journal_writes() {
        use crate::{TransactionErrorKind, TransactionManagerConfig};

        let txn_mgr = TransactionManager::new(TransactionManagerConfig::default());
        let reader = txn_mgr
            .begin_read_transaction(crate::TransactionOptions::default().read_only())
            .expect("reader should begin");

        // Statement entry is intent-free and succeeds for reads.
        let (context, start) = txn_mgr
            .begin_statement(reader)
            .expect("begin should succeed");
        txn_mgr
            .finish_statement(&context, start)
            .expect("finish should succeed");
        txn_mgr
            .refresh_statement_snapshot(reader)
            .expect("refresh should succeed");

        // But the journal itself rejects writes on read-only transactions, so
        // a write that slips past the query-layer plan check fails fast here
        // instead of being silently dropped at commit.
        let context = txn_mgr.get_context(reader).expect("context should exist");
        let error = context
            .record_mutation(crate::types::MutationResult::new(
                crate::types::MutationEntityKey::Vertex(
                    crate::VertexId::try_from_int64(1).expect("test vertex id"),
                ),
            ))
            .expect_err("journal write on a read-only transaction must be rejected");
        assert_eq!(error.kind(), TransactionErrorKind::ReadOnlyTransaction);
    }

    #[test]
    fn test_connection_ownership_is_enforced_across_connections() {
        use crate::{TransactionErrorKind, TransactionManagerConfig};

        let txn_mgr = TransactionManager::new(TransactionManagerConfig::default());
        let conn_mgr = ConnectionManager::new();
        let conn_a = conn_mgr.create_connection_with_mode(TransactionMode::Manual);
        let conn_b = conn_mgr.create_connection_with_mode(TransactionMode::Manual);

        let txn_id = conn_mgr
            .begin_for_connection(&txn_mgr, conn_a, crate::TransactionOptions::default())
            .expect("begin should succeed");

        // The transaction carries connection A's ownership.
        let context = txn_mgr.get_context(txn_id).expect("context should exist");
        assert_eq!(
            context.owner().as_deref(),
            Some(ConnectionManager::owner_for_connection(conn_a).as_str())
        );

        // Connection B cannot take over through the owner-checked manager API.
        let error = txn_mgr
            .commit_transaction_as_owner(
                txn_id,
                Some(&ConnectionManager::owner_for_connection(conn_b)),
            )
            .expect_err("cross-connection commit must be rejected");
        assert_eq!(error.kind(), TransactionErrorKind::TransactionNotOwner);

        // Connection B has no transaction of its own either.
        assert!(conn_mgr.commit_for_connection(&txn_mgr, conn_b).is_err());

        // The owning connection commits normally.
        conn_mgr
            .commit_for_connection(&txn_mgr, conn_a)
            .expect("owner commit should succeed");
    }

    #[test]
    fn test_execute_auto_commit_in_manual_without_txn_fails() {
        use crate::{TransactionError, TransactionManagerConfig};

        let txn_mgr = TransactionManager::new(TransactionManagerConfig::default());
        let conn_mgr = ConnectionManager::new();
        let conn_id = conn_mgr.create_connection_with_mode(TransactionMode::Manual);

        let result: Result<(), TransactionError> = conn_mgr.execute_auto_commit(
            &txn_mgr,
            conn_id,
            crate::TransactionOptions::default(),
            |_| Ok::<(), TransactionError>(()),
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_connection_manager_manual_without_txn_fails() {
        let txn_mgr = TransactionManager::new(TransactionManagerConfig::default());
        let conn_mgr = ConnectionManager::new();
        let conn_id = conn_mgr.create_connection_with_mode(TransactionMode::Manual);

        let result: Result<(), TransactionError> =
            conn_mgr
                .with_connection_transaction(&txn_mgr, conn_id, |_| Ok::<(), TransactionError>(()));
        assert!(result.is_err());
    }
}