sz-orm-core 7.6.0

Core ORM engine: Model trait, ActiveRecord, QueryBuilder, Pool, Transaction, migration, and SQL dialect abstraction
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
//! v6.6.0 Saga 分布式事务协调器
//!
//! Saga 模式:正向执行 + 补偿编排,保证最终一致性。
//!
//! # 执行流程
//! 1. 依次执行正向步骤
//! 2. 步骤 i 失败 → 逆序执行已完成步骤的补偿事务
//! 3. 全部成功 → Saga 完成
//!
//! # 超时处理
//! - 全局超时:整个 Saga 的最大执行时间
//! - 单步骤超时:每个步骤的最大执行时间

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use futures::future::BoxFuture;
use serde::{Deserialize, Serialize};

/// Saga 实例 ID
pub type SagaId = String;

/// Saga 步骤索引
pub type StepIndex = usize;

/// Saga 执行状态
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SagaStatus {
    /// 正在执行
    Running,
    /// 全部正向步骤成功
    Completed,
    /// 补偿完成(某步骤失败后补偿成功)
    Compensated,
    /// 补偿失败
    Failed,
    /// 超时
    TimedOut,
}

/// Saga 步骤
pub struct SagaStep {
    /// 步骤名称
    pub name: String,
    /// 正向操作
    pub action: Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send>,
    /// �)偿操作
    pub compensate: Option<Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send>>,
}

impl std::fmt::Debug for SagaStep {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SagaStep")
            .field("name", &self.name)
            .field("has_compensate", &self.compensate.is_some())
            .finish()
    }
}

/// Saga 执行结果
#[derive(Debug, Clone)]
pub struct SagaResult {
    pub saga_id: SagaId,
    pub status: SagaStatus,
    pub completed_steps: Vec<StepIndex>,
    pub failed_step: Option<StepIndex>,
    pub error: Option<String>,
    pub elapsed: Duration,
}

/// Saga 超时配置
#[derive(Debug, Clone)]
pub struct TimeoutConfig {
    /// 全局超时(None = 无限制)
    pub overall: Option<Duration>,
    /// 单步骤超时(None = 无限制)
    pub per_step: Option<Duration>,
}

impl Default for TimeoutConfig {
    fn default() -> Self {
        Self {
            overall: Some(Duration::from_secs(30)),
            per_step: Some(Duration::from_secs(10)),
        }
    }
}

/// Saga 状态存储(内存实现)
pub struct SagaStore {
    instances: Mutex<HashMap<SagaId, StoredInstance>>,
}

struct StoredInstance {
    status: SagaStatus,
    completed_steps: Vec<StepIndex>,
    failed_step: Option<StepIndex>,
}

impl SagaStore {
    pub fn new() -> Self {
        Self {
            instances: Mutex::new(HashMap::new()),
        }
    }

    pub fn create(&self, saga_id: &str) {
        let mut instances = self.instances.lock().unwrap();
        instances.insert(
            saga_id.to_string(),
            StoredInstance {
                status: SagaStatus::Running,
                completed_steps: Vec::new(),
                failed_step: None,
            },
        );
    }

    pub fn mark_step_complete(&self, saga_id: &str, step: StepIndex) {
        let mut instances = self.instances.lock().unwrap();
        if let Some(inst) = instances.get_mut(saga_id) {
            inst.completed_steps.push(step);
        }
    }

    pub fn mark_failed(&self, saga_id: &str, step: StepIndex) {
        let mut instances = self.instances.lock().unwrap();
        if let Some(inst) = instances.get_mut(saga_id) {
            inst.failed_step = Some(step);
            inst.status = SagaStatus::Failed;
        }
    }

    pub fn update_status(&self, saga_id: &str, status: SagaStatus) {
        let mut instances = self.instances.lock().unwrap();
        if let Some(inst) = instances.get_mut(saga_id) {
            inst.status = status;
        }
    }

    pub fn get_status(&self, saga_id: &str) -> Option<SagaStatus> {
        let instances = self.instances.lock().unwrap();
        instances.get(saga_id).map(|i| i.status.clone())
    }

    pub fn get_completed_steps(&self, saga_id: &str) -> Vec<StepIndex> {
        let instances = self.instances.lock().unwrap();
        instances
            .get(saga_id)
            .map(|i| i.completed_steps.clone())
            .unwrap_or_default()
    }
}

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

/// Saga 协调器
pub struct SagaCoordinator {
    store: Arc<SagaStore>,
    timeout_config: TimeoutConfig,
}

impl SagaCoordinator {
    pub fn new(store: Arc<SagaStore>, timeout_config: TimeoutConfig) -> Self {
        Self {
            store,
            timeout_config,
        }
    }

    pub fn store(&self) -> &Arc<SagaStore> {
        &self.store
    }

    /// 执行 Saga
    ///
    /// 依次执行正向步骤,失败时逆序补偿。
    pub async fn execute(&self, saga_id: &str, steps: Vec<SagaStep>) -> SagaResult {
        let start = Instant::now();
        let _n = steps.len();
        self.store.create(saga_id);

        let mut completed: Vec<StepIndex> = Vec::new();
        let mut compensates: Vec<(
            StepIndex,
            Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send>,
        )> = Vec::new();

        for (i, step) in steps.into_iter().enumerate() {
            if let Some(overall) = self.timeout_config.overall {
                if start.elapsed() >= overall {
                    self.store.update_status(saga_id, SagaStatus::TimedOut);
                    return SagaResult {
                        saga_id: saga_id.to_string(),
                        status: SagaStatus::TimedOut,
                        completed_steps: completed,
                        failed_step: None,
                        error: Some("overall timeout".into()),
                        elapsed: start.elapsed(),
                    };
                }
            }

            let action = step.action;
            if let Some(c) = step.compensate {
                compensates.push((i, c));
            }

            let step_result = if let Some(per_step) = self.timeout_config.per_step {
                match tokio::time::timeout(per_step, action()).await {
                    Ok(r) => r,
                    Err(_) => Err(format!("step {} timed out", step.name)),
                }
            } else {
                action().await
            };

            match step_result {
                Ok(()) => {
                    completed.push(i);
                    self.store.mark_step_complete(saga_id, i);
                }
                Err(e) => {
                    self.store.mark_failed(saga_id, i);
                    let _ = self.compensate(saga_id, &mut compensates).await;
                    let status = self.store.get_status(saga_id).unwrap_or(SagaStatus::Failed);
                    return SagaResult {
                        saga_id: saga_id.to_string(),
                        status,
                        completed_steps: completed,
                        failed_step: Some(i),
                        error: Some(e),
                        elapsed: start.elapsed(),
                    };
                }
            }
        }

        self.store.update_status(saga_id, SagaStatus::Completed);
        SagaResult {
            saga_id: saga_id.to_string(),
            status: SagaStatus::Completed,
            completed_steps: completed,
            failed_step: None,
            error: None,
            elapsed: start.elapsed(),
        }
    }

    async fn compensate(
        &self,
        saga_id: &str,
        compensates: &mut Vec<(
            StepIndex,
            Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send>,
        )>,
    ) -> Result<(), String> {
        let mut all_ok = true;
        while let Some((idx, comp)) = compensates.pop() {
            let result = if let Some(per_step) = self.timeout_config.per_step {
                match tokio::time::timeout(per_step, comp()).await {
                    Ok(r) => r,
                    Err(_) => Err(format!("compensate step {} timed out", idx)),
                }
            } else {
                comp().await
            };
            if result.is_err() {
                all_ok = false;
            }
        }
        if all_ok {
            self.store.update_status(saga_id, SagaStatus::Compensated);
            Ok(())
        } else {
            self.store.update_status(saga_id, SagaStatus::Failed);
            Err("compensation failed".into())
        }
    }
}

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

    fn ok_action() -> Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send> {
        Box::new(|| {
            let f: BoxFuture<'static, Result<(), String>> = Box::pin(async { Ok(()) });
            f
        })
    }

    fn fail_action(
        msg: &str,
    ) -> Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send> {
        let msg = msg.to_string();
        Box::new(move || {
            let f: BoxFuture<'static, Result<(), String>> = Box::pin(async move { Err(msg) });
            f
        })
    }

    fn ok_compensate() -> Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send> {
        Box::new(|| {
            let f: BoxFuture<'static, Result<(), String>> = Box::pin(async { Ok(()) });
            f
        })
    }

    #[tokio::test]
    async fn test_saga_all_success() {
        let store = Arc::new(SagaStore::new());
        let coord = SagaCoordinator::new(store.clone(), TimeoutConfig::default());
        let steps = vec![
            SagaStep {
                name: "step1".into(),
                action: ok_action(),
                compensate: Some(ok_compensate()),
            },
            SagaStep {
                name: "step2".into(),
                action: ok_action(),
                compensate: Some(ok_compensate()),
            },
        ];
        let result = coord.execute("saga1", steps).await;
        assert_eq!(result.status, SagaStatus::Completed);
        assert_eq!(result.completed_steps, vec![0, 1]);
        assert!(result.error.is_none());
    }

    #[tokio::test]
    async fn test_saga_step2_fails_compensate() {
        let store = Arc::new(SagaStore::new());
        let coord = SagaCoordinator::new(store.clone(), TimeoutConfig::default());
        let steps = vec![
            SagaStep {
                name: "step1".into(),
                action: ok_action(),
                compensate: Some(ok_compensate()),
            },
            SagaStep {
                name: "step2".into(),
                action: fail_action("step2 failed"),
                compensate: Some(ok_compensate()),
            },
        ];
        let result = coord.execute("saga2", steps).await;
        assert_eq!(result.status, SagaStatus::Compensated);
        assert_eq!(result.completed_steps, vec![0]);
        assert_eq!(result.failed_step, Some(1));
        assert!(result.error.is_some());
    }

    #[tokio::test]
    async fn test_saga_no_compensate_on_fail() {
        let store = Arc::new(SagaStore::new());
        let coord = SagaCoordinator::new(store.clone(), TimeoutConfig::default());
        let steps = vec![
            SagaStep {
                name: "step1".into(),
                action: ok_action(),
                compensate: None,
            },
            SagaStep {
                name: "step2".into(),
                action: fail_action("fail"),
                compensate: None,
            },
        ];
        let result = coord.execute("saga2", steps).await;
        assert_eq!(result.status, SagaStatus::Compensated);
    }

    #[tokio::test]
    async fn test_saga_empty_steps() {
        let store = Arc::new(SagaStore::new());
        let coord = SagaCoordinator::new(store.clone(), TimeoutConfig::default());
        let result = coord.execute("saga3", vec![]).await;
        assert_eq!(result.status, SagaStatus::Completed);
        assert_eq!(result.completed_steps, Vec::<usize>::new());
    }

    #[tokio::test]
    async fn test_saga_per_step_timeout() {
        let store = Arc::new(SagaStore::new());
        let config = TimeoutConfig {
            overall: Some(Duration::from_secs(5)),
            per_step: Some(Duration::from_millis(10)),
        };
        let coord = SagaCoordinator::new(store.clone(), config);
        let slow: Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send> =
            Box::new(|| {
                let f: BoxFuture<'static, Result<(), String>> = Box::pin(async {
                    tokio::time::sleep(Duration::from_secs(1)).await;
                    Ok(())
                });
                f
            });
        let steps = vec![SagaStep {
            name: "slow".into(),
            action: slow,
            compensate: None,
        }];
        let result = coord.execute("saga4", steps).await;
        assert_eq!(result.status, SagaStatus::Compensated);
        assert!(result.error.is_some());
    }

    #[tokio::test]
    async fn test_saga_store_persistence() {
        let store = SagaStore::new();
        store.create("s1");
        assert_eq!(store.get_status("s1"), Some(SagaStatus::Running));
        store.mark_step_complete("s1", 0);
        assert_eq!(store.get_completed_steps("s1"), vec![0]);
        store.update_status("s1", SagaStatus::Completed);
        assert_eq!(store.get_status("s1"), Some(SagaStatus::Completed));
    }

    #[tokio::test]
    async fn test_saga_compensate_failed() {
        let store = Arc::new(SagaStore::new());
        let coord = SagaCoordinator::new(store.clone(), TimeoutConfig::default());
        let bad_comp: Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send> =
            Box::new(|| {
                let f: BoxFuture<'static, Result<(), String>> =
                    Box::pin(async { Err("comp failed".into()) });
                f
            });
        let steps = vec![
            SagaStep {
                name: "s1".into(),
                action: ok_action(),
                compensate: Some(bad_comp),
            },
            SagaStep {
                name: "s2".into(),
                action: fail_action("s2 fail"),
                compensate: None,
            },
        ];
        let result = coord.execute("saga5", steps).await;
        assert_eq!(result.status, SagaStatus::Failed);
    }

    #[tokio::test]
    async fn test_saga_overall_timeout() {
        let store = Arc::new(SagaStore::new());
        let config = TimeoutConfig {
            overall: Some(Duration::from_millis(5)),
            per_step: None,
        };
        let coord = SagaCoordinator::new(store.clone(), config);
        let slow: Box<dyn FnOnce() -> BoxFuture<'static, Result<(), String>> + Send> =
            Box::new(|| {
                let f: BoxFuture<'static, Result<(), String>> = Box::pin(async {
                    tokio::time::sleep(Duration::from_millis(20)).await;
                    Ok(())
                });
                f
            });
        let steps = vec![
            SagaStep {
                name: "slow1".into(),
                action: slow,
                compensate: None,
            },
            SagaStep {
                name: "slow2".into(),
                action: ok_action(),
                compensate: None,
            },
        ];
        let result = coord.execute("saga6", steps).await;
        assert_eq!(result.status, SagaStatus::TimedOut);
    }

    #[tokio::test]
    async fn test_saga_three_steps_middle_fails() {
        let store = Arc::new(SagaStore::new());
        let coord = SagaCoordinator::new(store.clone(), TimeoutConfig::default());
        let steps = vec![
            SagaStep {
                name: "s1".into(),
                action: ok_action(),
                compensate: Some(ok_compensate()),
            },
            SagaStep {
                name: "s2".into(),
                action: fail_action("s2 fail"),
                compensate: Some(ok_compensate()),
            },
            SagaStep {
                name: "s3".into(),
                action: ok_action(),
                compensate: Some(ok_compensate()),
            },
        ];
        let result = coord.execute("saga7", steps).await;
        assert_eq!(result.status, SagaStatus::Compensated);
        assert_eq!(result.completed_steps, vec![0]);
        assert_eq!(result.failed_step, Some(1));
    }
}