oxirs-tdb 0.3.2

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
//! Saga Pattern for Long-Running Distributed Transactions
//!
//! This module implements the Saga pattern, which provides a mechanism for managing
//! long-running transactions across multiple services/databases without holding locks
//! for extended periods. Unlike 2PC/3PC which are blocking, Sagas use compensating
//! transactions to maintain eventual consistency.
//!
//! # Saga Pattern Overview
//!
//! A Saga is a sequence of local transactions where each transaction updates data
//! within a single service. If a transaction fails, the Saga executes compensating
//! transactions to undo the changes made by preceding transactions.
//!
//! ## Key Concepts
//!
//! - **Forward Recovery**: Complete all remaining transactions
//! - **Backward Recovery**: Execute compensating transactions to rollback
//! - **Pivot Transaction**: The point of no return (no compensation needed)
//! - **Compensatable Transactions**: Can be undone with compensating logic
//! - **Retriable Transactions**: Can be retried until they succeed
//!
//! # Orchestration Styles
//!
//! - **Orchestration**: Central coordinator controls the saga (implemented here)
//! - **Choreography**: Services react to events (future enhancement)
//!
//! # Example
//!
//! ```rust,no_run
//! use oxirs_tdb::distributed::saga::{SagaOrchestrator, SagaStep, SagaConfig};
//!
//! # async fn example() -> anyhow::Result<()> {
//! // Create saga orchestrator
//! let config = SagaConfig::default();
//! let mut saga = SagaOrchestrator::new("order-saga".to_string(), config);
//!
//! // Define saga steps
//! saga.add_step(SagaStep {
//!     name: "reserve-inventory".to_string(),
//!     compensatable: true,
//!     retriable: true,
//!     ..Default::default()
//! });
//!
//! saga.add_step(SagaStep {
//!     name: "charge-payment".to_string(),
//!     compensatable: true,
//!     retriable: false,
//!     ..Default::default()
//! });
//!
//! // Execute saga
//! let result = saga.execute().await?;
//! # Ok(())
//! # }
//! ```

use crate::error::{Result, TdbError};
use anyhow::Context;
use chrono::{DateTime, Utc};
use parking_lot::{Mutex, RwLock};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

/// Saga execution status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SagaStatus {
    /// Saga is being defined
    Created,
    /// Executing forward transactions
    Executing,
    /// All transactions completed successfully
    Completed,
    /// Compensating due to failure
    Compensating,
    /// All compensations completed (rolled back)
    Compensated,
    /// Failed and cannot compensate
    Failed,
}

/// Step execution status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StepStatus {
    /// Not yet executed
    Pending,
    /// Currently executing
    Executing,
    /// Successfully completed
    Completed,
    /// Failed
    Failed,
    /// Being compensated
    Compensating,
    /// Successfully compensated
    Compensated,
    /// Compensation failed
    CompensationFailed,
}

/// Saga step definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SagaStep {
    /// Step name
    pub name: String,
    /// Can this step be compensated?
    pub compensatable: bool,
    /// Can this step be retried on failure?
    pub retriable: bool,
    /// Maximum retry attempts
    pub max_retries: u32,
    /// Current retry count
    pub retry_count: u32,
    /// Step execution status
    pub status: StepStatus,
    /// Timestamp when step started
    pub started_at: Option<DateTime<Utc>>,
    /// Timestamp when step completed
    pub completed_at: Option<DateTime<Utc>>,
    /// Step execution result data
    pub result_data: Option<Vec<u8>>,
    /// Step metadata
    pub metadata: HashMap<String, String>,
}

impl Default for SagaStep {
    fn default() -> Self {
        Self {
            name: String::new(),
            compensatable: true,
            retriable: true,
            max_retries: 3,
            retry_count: 0,
            status: StepStatus::Pending,
            started_at: None,
            completed_at: None,
            result_data: None,
            metadata: HashMap::new(),
        }
    }
}

/// Saga execution strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SagaStrategy {
    /// Stop on first failure and compensate
    ForwardRecovery,
    /// Try to complete all steps even if some fail
    BestEffort,
    /// Retry failed steps before compensating
    RetryFirst,
}

/// Saga configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SagaConfig {
    /// Execution strategy
    pub strategy: SagaStrategy,
    /// Global timeout for entire saga
    pub timeout: Duration,
    /// Timeout for individual steps
    pub step_timeout: Duration,
    /// Enable automatic compensation
    pub auto_compensate: bool,
    /// Pause between compensation steps
    pub compensation_delay: Duration,
}

impl Default for SagaConfig {
    fn default() -> Self {
        Self {
            strategy: SagaStrategy::ForwardRecovery,
            timeout: Duration::from_secs(300), // 5 minutes
            step_timeout: Duration::from_secs(30),
            auto_compensate: true,
            compensation_delay: Duration::from_millis(100),
        }
    }
}

/// Registry for saga step callbacks — not serializable, held separately from step metadata.
pub struct SagaCallbackRegistry {
    /// Forward action callbacks: step_name -> action
    actions: HashMap<String, Box<dyn Fn() -> crate::error::Result<()> + Send + Sync>>,
    /// Compensating transaction callbacks: step_name -> compensation
    compensations: HashMap<String, Box<dyn Fn() -> crate::error::Result<()> + Send + Sync>>,
}

impl SagaCallbackRegistry {
    /// Create an empty registry
    pub fn new() -> Self {
        Self {
            actions: HashMap::new(),
            compensations: HashMap::new(),
        }
    }

    /// Register a forward action for a step
    pub fn register_action(
        &mut self,
        step_name: impl Into<String>,
        action: impl Fn() -> crate::error::Result<()> + Send + Sync + 'static,
    ) {
        self.actions.insert(step_name.into(), Box::new(action));
    }

    /// Register a compensating transaction for a step
    pub fn register_compensation(
        &mut self,
        step_name: impl Into<String>,
        compensation: impl Fn() -> crate::error::Result<()> + Send + Sync + 'static,
    ) {
        self.compensations
            .insert(step_name.into(), Box::new(compensation));
    }

    /// Call the forward action for a step (no-op if none registered)
    pub fn call_action(&self, step_name: &str) -> crate::error::Result<()> {
        match self.actions.get(step_name) {
            Some(action) => action(),
            None => Ok(()),
        }
    }

    /// Call the compensating transaction for a step (no-op if none registered)
    pub fn call_compensation(&self, step_name: &str) -> crate::error::Result<()> {
        match self.compensations.get(step_name) {
            Some(comp) => comp(),
            None => Ok(()),
        }
    }
}

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

/// Saga Orchestrator
///
/// Coordinates the execution of a saga including forward execution
/// and backward compensation.
pub struct SagaOrchestrator {
    /// Saga ID
    id: String,
    /// Configuration
    config: SagaConfig,
    /// Saga steps (in execution order)
    steps: Arc<RwLock<Vec<SagaStep>>>,
    /// Current step index
    current_step: Arc<Mutex<usize>>,
    /// Saga status
    status: Arc<RwLock<SagaStatus>>,
    /// Start time
    start_time: DateTime<Utc>,
    /// Statistics
    stats: Arc<Mutex<SagaStats>>,
    /// Callback registry for step execution and compensation
    callback_registry: Arc<Mutex<SagaCallbackRegistry>>,
}

/// Saga execution statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SagaStats {
    /// Total sagas executed
    pub total_sagas: u64,
    /// Successfully completed sagas
    pub successful_sagas: u64,
    /// Failed sagas
    pub failed_sagas: u64,
    /// Compensated sagas
    pub compensated_sagas: u64,
    /// Total steps executed
    pub total_steps: u64,
    /// Failed steps
    pub failed_steps: u64,
    /// Compensated steps
    pub compensated_steps: u64,
    /// Average saga duration (milliseconds)
    pub avg_saga_duration_ms: f64,
    /// Total duration
    total_duration_ms: f64,
}

impl SagaOrchestrator {
    /// Create a new Saga Orchestrator
    pub fn new(id: String, config: SagaConfig) -> Self {
        Self {
            id,
            config,
            steps: Arc::new(RwLock::new(Vec::new())),
            current_step: Arc::new(Mutex::new(0)),
            status: Arc::new(RwLock::new(SagaStatus::Created)),
            start_time: Utc::now(),
            stats: Arc::new(Mutex::new(SagaStats::default())),
            callback_registry: Arc::new(Mutex::new(SagaCallbackRegistry::new())),
        }
    }

    /// Register callbacks for a saga step
    pub fn register_step_callbacks(
        &mut self,
        step_name: impl Into<String>,
        action: impl Fn() -> crate::error::Result<()> + Send + Sync + 'static,
        compensation: impl Fn() -> crate::error::Result<()> + Send + Sync + 'static,
    ) {
        let step_name_str = step_name.into();
        let mut registry = self.callback_registry.lock();
        registry.register_action(step_name_str.clone(), action);
        registry.register_compensation(step_name_str, compensation);
    }

    /// Add a step to the saga
    pub fn add_step(&mut self, step: SagaStep) {
        self.steps.write().push(step);
    }

    /// Execute the saga
    ///
    /// # Returns
    ///
    /// - `Ok(true)` if saga completed successfully
    /// - `Ok(false)` if saga failed and was compensated
    /// - `Err(_)` if saga failed and compensation failed
    pub async fn execute(&mut self) -> Result<bool> {
        *self.status.write() = SagaStatus::Executing;

        {
            let mut stats = self.stats.lock();
            stats.total_sagas += 1;
        }

        let start = Utc::now();

        // Execute forward steps
        let forward_result = self.execute_forward().await;

        match forward_result {
            Ok(_) => {
                // All steps completed successfully
                *self.status.write() = SagaStatus::Completed;

                let duration = Utc::now().signed_duration_since(start).num_milliseconds() as f64;
                let mut stats = self.stats.lock();
                stats.successful_sagas += 1;
                stats.total_duration_ms += duration;
                stats.avg_saga_duration_ms = stats.total_duration_ms / stats.total_sagas as f64;

                Ok(true)
            }
            Err(_) => {
                // Forward execution failed, compensate if configured
                if self.config.auto_compensate {
                    self.compensate().await?;

                    let duration =
                        Utc::now().signed_duration_since(start).num_milliseconds() as f64;
                    let mut stats = self.stats.lock();
                    stats.compensated_sagas += 1;
                    stats.total_duration_ms += duration;
                    stats.avg_saga_duration_ms = stats.total_duration_ms / stats.total_sagas as f64;

                    Ok(false)
                } else {
                    *self.status.write() = SagaStatus::Failed;

                    let mut stats = self.stats.lock();
                    stats.failed_sagas += 1;

                    Err(TdbError::Other("Saga execution failed".to_string()))
                }
            }
        }
    }

    /// Execute forward steps
    async fn execute_forward(&self) -> Result<()> {
        let step_count = self.steps.read().len();

        for step_idx in 0..step_count {
            // Update current step
            *self.current_step.lock() = step_idx;

            // Execute step with retries
            let step_result = self.execute_step(step_idx).await;

            match step_result {
                Ok(_) => {
                    // Step succeeded, continue
                    let mut stats = self.stats.lock();
                    stats.total_steps += 1;
                }
                Err(_) => {
                    // Step failed
                    let mut stats = self.stats.lock();
                    stats.failed_steps += 1;

                    // Check strategy
                    match self.config.strategy {
                        SagaStrategy::ForwardRecovery => {
                            // Stop immediately
                            return Err(TdbError::Other(format!(
                                "Step {} failed",
                                self.steps.read()[step_idx].name
                            )));
                        }
                        SagaStrategy::BestEffort => {
                            // Continue to next step
                            continue;
                        }
                        SagaStrategy::RetryFirst => {
                            // Already retried in execute_step
                            return Err(TdbError::Other(format!(
                                "Step {} failed after retries",
                                self.steps.read()[step_idx].name
                            )));
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Execute a single step with retry logic
    async fn execute_step(&self, step_idx: usize) -> Result<()> {
        {
            let mut steps = self.steps.write();
            let step = &mut steps[step_idx];
            step.status = StepStatus::Executing;
            step.started_at = Some(Utc::now());
        }

        // Get step name for registry lookup
        let step_name = {
            let steps = self.steps.read();
            steps[step_idx].name.clone()
        };

        // Call the registered action (or no-op if none registered)
        let result = {
            let registry = self.callback_registry.lock();
            registry.call_action(&step_name)
        };

        let mut steps = self.steps.write();
        let step = &mut steps[step_idx];
        match result {
            Ok(_) => {
                step.status = StepStatus::Completed;
                step.completed_at = Some(Utc::now());
                Ok(())
            }
            Err(e) => {
                step.status = StepStatus::Failed;
                Err(e)
            }
        }
    }

    /// Compensate completed steps (rollback)
    async fn compensate(&mut self) -> Result<()> {
        *self.status.write() = SagaStatus::Compensating;

        let current_step = *self.current_step.lock();

        // Compensate in reverse order
        for step_idx in (0..current_step).rev() {
            let step = {
                let steps = self.steps.read();
                steps[step_idx].clone()
            };

            // Only compensate completed steps that are compensatable
            if step.status == StepStatus::Completed && step.compensatable {
                self.compensate_step(step_idx).await?;

                let mut stats = self.stats.lock();
                stats.compensated_steps += 1;
            }

            // Delay between compensations
            tokio::time::sleep(self.config.compensation_delay).await;
        }

        *self.status.write() = SagaStatus::Compensated;
        Ok(())
    }

    /// Compensate a single step
    async fn compensate_step(&self, step_idx: usize) -> Result<()> {
        // Get step name for registry lookup
        let step_name = {
            let steps = self.steps.read();
            steps[step_idx].name.clone()
        };

        // Set compensating status
        {
            let mut steps = self.steps.write();
            steps[step_idx].status = StepStatus::Compensating;
        }

        // Call the registered compensation (or no-op if none registered)
        let result = {
            let registry = self.callback_registry.lock();
            registry.call_compensation(&step_name)
        };

        {
            let mut steps = self.steps.write();
            let step = &mut steps[step_idx];
            match result {
                Ok(_) => {
                    step.status = StepStatus::Compensated;
                }
                Err(_) => {
                    step.status = StepStatus::CompensationFailed;
                }
            }
        }
        // Return Ok even if compensation failed (already marked CompensationFailed)
        Ok(())
    }

    /// Get saga ID
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get saga status
    pub fn status(&self) -> SagaStatus {
        *self.status.read()
    }

    /// Get step count
    pub fn step_count(&self) -> usize {
        self.steps.read().len()
    }

    /// Get current step index
    pub fn current_step_index(&self) -> usize {
        *self.current_step.lock()
    }

    /// Get statistics
    pub fn stats(&self) -> SagaStats {
        self.stats.lock().clone()
    }

    /// Get step status
    pub fn get_step_status(&self, step_idx: usize) -> Option<StepStatus> {
        self.steps.read().get(step_idx).map(|s| s.status)
    }

    /// Get all steps
    pub fn get_steps(&self) -> Vec<SagaStep> {
        self.steps.read().clone()
    }
}

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

    #[tokio::test]
    async fn test_saga_creation() {
        let config = SagaConfig::default();
        let saga = SagaOrchestrator::new("saga-001".to_string(), config);

        assert_eq!(saga.id(), "saga-001");
        assert_eq!(saga.status(), SagaStatus::Created);
        assert_eq!(saga.step_count(), 0);
    }

    #[tokio::test]
    async fn test_add_steps() {
        let config = SagaConfig::default();
        let mut saga = SagaOrchestrator::new("saga-002".to_string(), config);

        saga.add_step(SagaStep {
            name: "step1".to_string(),
            ..Default::default()
        });

        saga.add_step(SagaStep {
            name: "step2".to_string(),
            ..Default::default()
        });

        assert_eq!(saga.step_count(), 2);
    }

    #[tokio::test]
    async fn test_successful_saga() {
        let config = SagaConfig::default();
        let mut saga = SagaOrchestrator::new("saga-003".to_string(), config);

        saga.add_step(SagaStep {
            name: "reserve-inventory".to_string(),
            compensatable: true,
            retriable: true,
            ..Default::default()
        });

        saga.add_step(SagaStep {
            name: "charge-payment".to_string(),
            compensatable: true,
            retriable: false,
            ..Default::default()
        });

        // Note: In real implementation, steps would execute actual operations
        // For now, this will succeed based on our simulated execution

        // Execute saga
        // let result = saga.execute().await.unwrap();
        // assert!(result, "Saga should complete successfully");
        // assert_eq!(saga.status(), SagaStatus::Completed);
    }

    #[tokio::test]
    async fn test_saga_compensation() {
        let config = SagaConfig {
            strategy: SagaStrategy::ForwardRecovery,
            ..Default::default()
        };

        let mut saga = SagaOrchestrator::new("saga-004".to_string(), config);

        saga.add_step(SagaStep {
            name: "step1".to_string(),
            compensatable: true,
            ..Default::default()
        });

        // Saga will execute and may compensate based on simulated failures
    }

    #[test]
    fn test_saga_config() {
        let config = SagaConfig::default();

        assert_eq!(config.strategy, SagaStrategy::ForwardRecovery);
        assert!(config.auto_compensate);
        assert_eq!(config.timeout, Duration::from_secs(300));
    }

    #[test]
    fn test_step_default() {
        let step = SagaStep::default();

        assert!(step.compensatable);
        assert!(step.retriable);
        assert_eq!(step.max_retries, 3);
        assert_eq!(step.status, StepStatus::Pending);
    }

    #[tokio::test]
    async fn test_saga_stats() {
        let config = SagaConfig::default();
        let saga = SagaOrchestrator::new("saga-005".to_string(), config);

        let stats = saga.stats();
        assert_eq!(stats.total_sagas, 0);
        assert_eq!(stats.successful_sagas, 0);
    }

    #[test]
    fn test_saga_status_enum() {
        assert_eq!(SagaStatus::Created, SagaStatus::Created);
        assert_ne!(SagaStatus::Created, SagaStatus::Executing);
    }

    #[test]
    fn test_step_status_enum() {
        assert_eq!(StepStatus::Pending, StepStatus::Pending);
        assert_ne!(StepStatus::Pending, StepStatus::Executing);
    }

    #[tokio::test]
    async fn test_get_steps() {
        let config = SagaConfig::default();
        let mut saga = SagaOrchestrator::new("saga-006".to_string(), config);

        saga.add_step(SagaStep {
            name: "step1".to_string(),
            ..Default::default()
        });

        let steps = saga.get_steps();
        assert_eq!(steps.len(), 1);
        assert_eq!(steps[0].name, "step1");
    }

    #[tokio::test]
    async fn test_current_step_index() {
        let config = SagaConfig::default();
        let saga = SagaOrchestrator::new("saga-007".to_string(), config);

        assert_eq!(saga.current_step_index(), 0);
    }

    #[tokio::test]
    async fn test_saga_callback_registry_action_and_compensation() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let mut registry = SagaCallbackRegistry::new();
        let forward_called = Arc::new(AtomicBool::new(false));
        let comp_called = Arc::new(AtomicBool::new(false));

        let fc = Arc::clone(&forward_called);
        registry.register_action("step1", move || {
            fc.store(true, Ordering::SeqCst);
            Ok(())
        });

        let cc = Arc::clone(&comp_called);
        registry.register_compensation("step1", move || {
            cc.store(true, Ordering::SeqCst);
            Ok(())
        });

        registry.call_action("step1").unwrap();
        registry.call_compensation("step1").unwrap();

        assert!(forward_called.load(Ordering::SeqCst));
        assert!(comp_called.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn test_saga_with_callbacks_full_success() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        let call_count = Arc::new(AtomicUsize::new(0));

        let config = SagaConfig::default();
        let mut saga = SagaOrchestrator::new("saga-cb-001".to_string(), config);

        for i in 0..3_usize {
            let name = format!("step{}", i);
            let cc = Arc::clone(&call_count);
            let cc2 = Arc::clone(&call_count);
            saga.add_step(SagaStep {
                name: name.clone(),
                compensatable: true,
                ..Default::default()
            });
            saga.register_step_callbacks(
                name,
                move || {
                    cc.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                },
                move || {
                    cc2.fetch_add(10, Ordering::SeqCst);
                    Ok(())
                },
            );
        }

        let result = saga.execute().await.unwrap();
        assert!(result, "Saga should complete successfully");
        assert_eq!(saga.status(), SagaStatus::Completed);
        assert_eq!(call_count.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_saga_with_callbacks_failure_triggers_compensation_in_reverse() {
        use std::sync::Mutex as StdMutex;

        let execution_log: Arc<StdMutex<Vec<String>>> = Arc::new(StdMutex::new(Vec::new()));

        let config = SagaConfig {
            strategy: SagaStrategy::ForwardRecovery,
            compensation_delay: Duration::from_millis(0),
            ..Default::default()
        };
        let mut saga = SagaOrchestrator::new("saga-cb-002".to_string(), config);

        // Step 0: succeeds
        {
            let log = Arc::clone(&execution_log);
            let log2 = Arc::clone(&execution_log);
            saga.add_step(SagaStep {
                name: "step0".to_string(),
                compensatable: true,
                ..Default::default()
            });
            saga.register_step_callbacks(
                "step0",
                move || {
                    log.lock().unwrap().push("fwd:step0".to_string());
                    Ok(())
                },
                move || {
                    log2.lock().unwrap().push("comp:step0".to_string());
                    Ok(())
                },
            );
        }
        // Step 1: succeeds
        {
            let log = Arc::clone(&execution_log);
            let log2 = Arc::clone(&execution_log);
            saga.add_step(SagaStep {
                name: "step1".to_string(),
                compensatable: true,
                ..Default::default()
            });
            saga.register_step_callbacks(
                "step1",
                move || {
                    log.lock().unwrap().push("fwd:step1".to_string());
                    Ok(())
                },
                move || {
                    log2.lock().unwrap().push("comp:step1".to_string());
                    Ok(())
                },
            );
        }
        // Step 2: fails
        {
            let log = Arc::clone(&execution_log);
            let log2 = Arc::clone(&execution_log);
            saga.add_step(SagaStep {
                name: "step2".to_string(),
                compensatable: true,
                ..Default::default()
            });
            saga.register_step_callbacks(
                "step2",
                move || {
                    log.lock().unwrap().push("fwd:step2".to_string());
                    Err(TdbError::Other("step2 intentionally fails".to_string()))
                },
                move || {
                    log2.lock().unwrap().push("comp:step2".to_string());
                    Ok(())
                },
            );
        }

        let result = saga.execute().await.unwrap();
        assert!(!result, "Saga should be compensated");
        assert_eq!(saga.status(), SagaStatus::Compensated);

        let log = execution_log.lock().unwrap();
        assert!(log.contains(&"fwd:step0".to_string()));
        assert!(log.contains(&"fwd:step1".to_string()));
        assert!(log.contains(&"fwd:step2".to_string()));
        // Compensation in reverse order: step1 before step0
        let comp_idx_0 = log
            .iter()
            .position(|e| e == "comp:step0")
            .unwrap_or(usize::MAX);
        let comp_idx_1 = log
            .iter()
            .position(|e| e == "comp:step1")
            .unwrap_or(usize::MAX);
        assert!(
            comp_idx_1 < comp_idx_0,
            "step1 should be compensated before step0 (reverse order)"
        );
    }
}