sz-orm-queue 5.0.0

SZ-ORM Message Queue Extension - 6 MQ Providers (RabbitMQ/NATS/Pulsar/Kafka/ActiveMQ real, RocketMQ stub)
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
//! DialectCapturer trait + 各方言捕获器

use std::pin::Pin;
use std::sync::Arc;

use async_trait::async_trait;
use futures::Stream;

use super::{CdcCheckpoint, CdcConfig, CdcError, ChangeEvent, CheckpointPosition, DbType};

/// 方言捕获器 trait
#[async_trait]
pub trait DialectCapturer: Send + Sync {
    /// 启动捕获,返回变更事件流
    async fn start_capture(
        &self,
        checkpoint: Option<CdcCheckpoint>,
    ) -> Result<Pin<Box<dyn Stream<Item = ChangeEvent> + Send>>, CdcError>;

    /// 方言类型
    fn dialect(&self) -> DbType;
}

/// PostgreSQL WAL 逻辑复制捕获器
///
/// ⚠️ 状态:协议级捕获未实现(需 PostgreSQL 复制协议 + 真实 DB 集成环境)。
/// 返回明确的 WalNotConfigured/CaptureError,不假装成功。
/// 真实可用的 CDC 请使用 [`PollingCapturer`](轮询式)。
pub struct WalCapturer {
    config: CdcConfig,
    conn_string: String,
    slot_name: String,
    wal_level: String,
}

impl WalCapturer {
    pub fn new(
        config: CdcConfig,
        conn_string: String,
        slot_name: String,
        wal_level: String,
    ) -> Self {
        Self {
            config,
            conn_string,
            slot_name,
            wal_level,
        }
    }

    pub fn config(&self) -> &CdcConfig {
        &self.config
    }

    pub fn wal_level(&self) -> &str {
        &self.wal_level
    }
}

#[async_trait]
impl DialectCapturer for WalCapturer {
    async fn start_capture(
        &self,
        checkpoint: Option<CdcCheckpoint>,
    ) -> Result<Pin<Box<dyn Stream<Item = ChangeEvent> + Send>>, CdcError> {
        if self.wal_level != "logical" {
            return Err(CdcError::WalNotConfigured);
        }
        let _ = (&self.conn_string, &self.slot_name, checkpoint);
        Err(CdcError::CaptureError {
            reason: "WAL capture requires live PostgreSQL connection".to_string(),
        })
    }

    fn dialect(&self) -> DbType {
        DbType::Postgres
    }
}

/// MySQL binlog 捕获器
///
/// ⚠️ 状态:协议级捕获未实现(需 MySQL binlog 协议解析 + 真实 DB)。
/// 真实可用的 CDC 请使用 [`PollingCapturer`]。
pub struct BinlogCapturer {
    config: CdcConfig,
    conn_string: String,
    server_id: u32,
    binlog_enabled: bool,
}

impl BinlogCapturer {
    pub fn new(
        config: CdcConfig,
        conn_string: String,
        server_id: u32,
        binlog_enabled: bool,
    ) -> Self {
        Self {
            config,
            conn_string,
            server_id,
            binlog_enabled,
        }
    }

    pub fn binlog_enabled(&self) -> bool {
        self.binlog_enabled
    }

    pub fn config(&self) -> &CdcConfig {
        &self.config
    }
}

#[async_trait]
impl DialectCapturer for BinlogCapturer {
    async fn start_capture(
        &self,
        checkpoint: Option<CdcCheckpoint>,
    ) -> Result<Pin<Box<dyn Stream<Item = ChangeEvent> + Send>>, CdcError> {
        if !self.binlog_enabled {
            return Err(CdcError::BinlogNotEnabled);
        }
        let _ = (&self.conn_string, self.server_id, checkpoint);
        Err(CdcError::CaptureError {
            reason: "binlog capture requires live MySQL connection".to_string(),
        })
    }

    fn dialect(&self) -> DbType {
        DbType::Mysql
    }
}

/// SQLite 触发器捕获器
pub struct TriggerCapturer {
    config: CdcConfig,
    db_path: String,
}

impl TriggerCapturer {
    pub fn new(config: CdcConfig, db_path: String) -> Self {
        Self { config, db_path }
    }

    pub fn config(&self) -> &CdcConfig {
        &self.config
    }
}

#[async_trait]
impl DialectCapturer for TriggerCapturer {
    async fn start_capture(
        &self,
        _checkpoint: Option<CdcCheckpoint>,
    ) -> Result<Pin<Box<dyn Stream<Item = ChangeEvent> + Send>>, CdcError> {
        let _ = &self.db_path;
        Err(CdcError::CaptureError {
            reason: "trigger capture requires live SQLite connection".to_string(),
        })
    }

    fn dialect(&self) -> DbType {
        DbType::Sqlite
    }
}

/// Oracle LogMiner 捕获器
///
/// ⚠️ 状态:协议级捕获未实现。真实可用的 CDC 请使用 [`PollingCapturer`]。
pub struct LogMinerCapturer {
    config: CdcConfig,
    conn_string: String,
}

impl LogMinerCapturer {
    pub fn new(config: CdcConfig, conn_string: String) -> Self {
        Self {
            config,
            conn_string,
        }
    }

    pub fn config(&self) -> &CdcConfig {
        &self.config
    }
}

#[async_trait]
impl DialectCapturer for LogMinerCapturer {
    async fn start_capture(
        &self,
        _checkpoint: Option<CdcCheckpoint>,
    ) -> Result<Pin<Box<dyn Stream<Item = ChangeEvent> + Send>>, CdcError> {
        let _ = &self.conn_string;
        Err(CdcError::CaptureError {
            reason: "LogMiner capture requires live Oracle connection".to_string(),
        })
    }

    fn dialect(&self) -> DbType {
        DbType::Oracle
    }
}

/// MSSQL CDC 捕获器
pub struct MssqlCdcCapturer {
    config: CdcConfig,
    conn_string: String,
}

impl MssqlCdcCapturer {
    pub fn new(config: CdcConfig, conn_string: String) -> Self {
        Self {
            config,
            conn_string,
        }
    }

    pub fn config(&self) -> &CdcConfig {
        &self.config
    }
}

#[async_trait]
impl DialectCapturer for MssqlCdcCapturer {
    async fn start_capture(
        &self,
        _checkpoint: Option<CdcCheckpoint>,
    ) -> Result<Pin<Box<dyn Stream<Item = ChangeEvent> + Send>>, CdcError> {
        let _ = &self.conn_string;
        Err(CdcError::CaptureError {
            reason: "MSSQL CDC capture requires live MSSQL connection".to_string(),
        })
    }

    fn dialect(&self) -> DbType {
        DbType::Mssql
    }
}

/// 按方言构造捕获器
pub fn create_capturer(
    config: CdcConfig,
    conn_string: &str,
) -> Result<Box<dyn DialectCapturer>, CdcError> {
    match config.dialect {
        DbType::Postgres => Ok(Box::new(WalCapturer::new(
            config,
            conn_string.to_string(),
            "sz_orm_cdc_slot".to_string(),
            "logical".to_string(),
        ))),
        DbType::Mysql => Ok(Box::new(BinlogCapturer::new(
            config,
            conn_string.to_string(),
            1001,
            true,
        ))),
        DbType::Sqlite => Ok(Box::new(TriggerCapturer::new(
            config,
            conn_string.to_string(),
        ))),
        DbType::Oracle => Ok(Box::new(LogMinerCapturer::new(
            config,
            conn_string.to_string(),
        ))),
        DbType::Mssql => Ok(Box::new(MssqlCdcCapturer::new(
            config,
            conn_string.to_string(),
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cdc::{CheckpointStoreConfig, DownstreamConfig};

    fn test_config(dialect: DbType) -> CdcConfig {
        CdcConfig {
            tables: vec!["users".to_string()],
            dialect,
            downstream: vec![DownstreamConfig::Kafka {
                topic: "cdc".to_string(),
            }],
            checkpoint_store: CheckpointStoreConfig::Memory,
            masking: None,
        }
    }

    #[test]
    fn test_wal_capturer_dialect() {
        let capturer = WalCapturer::new(
            test_config(DbType::Postgres),
            "host=localhost".to_string(),
            "slot1".to_string(),
            "logical".to_string(),
        );
        assert_eq!(capturer.dialect(), DbType::Postgres);
    }

    #[test]
    fn test_binlog_capturer_dialect() {
        let capturer = BinlogCapturer::new(
            test_config(DbType::Mysql),
            "host=localhost".to_string(),
            1001,
            true,
        );
        assert_eq!(capturer.dialect(), DbType::Mysql);
    }

    #[test]
    fn test_trigger_capturer_dialect() {
        let capturer = TriggerCapturer::new(test_config(DbType::Sqlite), "test.db".to_string());
        assert_eq!(capturer.dialect(), DbType::Sqlite);
    }

    #[test]
    fn test_logminer_capturer_dialect() {
        let capturer =
            LogMinerCapturer::new(test_config(DbType::Oracle), "oracle_conn".to_string());
        assert_eq!(capturer.dialect(), DbType::Oracle);
    }

    #[test]
    fn test_mssql_cdc_capturer_dialect() {
        let capturer = MssqlCdcCapturer::new(test_config(DbType::Mssql), "mssql_conn".to_string());
        assert_eq!(capturer.dialect(), DbType::Mssql);
    }

    #[tokio::test]
    async fn test_wal_not_configured_error() {
        let capturer = WalCapturer::new(
            test_config(DbType::Postgres),
            "host=localhost".to_string(),
            "slot1".to_string(),
            "replica".to_string(),
        );
        let result = capturer.start_capture(None).await;
        assert!(matches!(result, Err(CdcError::WalNotConfigured)));
    }

    #[tokio::test]
    async fn test_binlog_not_enabled_error() {
        let capturer = BinlogCapturer::new(
            test_config(DbType::Mysql),
            "host=localhost".to_string(),
            1001,
            false,
        );
        let result = capturer.start_capture(None).await;
        assert!(matches!(result, Err(CdcError::BinlogNotEnabled)));
    }

    #[test]
    fn test_create_capturer_postgres() {
        let capturer = create_capturer(test_config(DbType::Postgres), "conn").unwrap();
        assert_eq!(capturer.dialect(), DbType::Postgres);
    }

    #[test]
    fn test_create_capturer_all_dialects() {
        for dialect in [
            DbType::Postgres,
            DbType::Mysql,
            DbType::Sqlite,
            DbType::Oracle,
            DbType::Mssql,
        ] {
            let capturer = create_capturer(test_config(dialect), "conn").unwrap();
            assert_eq!(capturer.dialect(), dialect);
        }
    }
}

// ============================================================================
// PollingCapturer — 轮询式 CDC 捕获器(真实实现)
//
// 背景:协议级捕获(PostgreSQL WAL 复制协议 / MySQL binlog 协议 /
// Oracle LogMiner / MSSQL CDC)需要真实数据库服务器与数千行协议实现,
// 在库内无法完成(v4.7.0 审计 P-4 登记)。本组件提供**真实可工作的
// 轮询式 CDC**(生产级方案之一):调用方持有真实 DB 连接并提供增量
// 轮询函数(如 `SELECT ... WHERE seq > checkpoint`),本组件负责
// 检查点推进、增量获取与事件流输出。
// ============================================================================

use std::collections::VecDeque;
use std::time::Duration;

/// 增量轮询函数:从 checkpoint 拉取新变更,返回 (事件, 推进后的 checkpoint)
///
/// 调用方实现(持有真实 DB 连接):
/// ```ignore
/// let poll_fn = |cp: &CdcCheckpoint| -> Result<(Vec<ChangeEvent>, CdcCheckpoint), CdcError> {
///     // SELECT * FROM change_log WHERE seq > cp.position 的 seq
///     // 返回新事件 + 新 checkpoint(游标推进)
/// };
/// ```
pub type PollFn =
    dyn Fn(&CdcCheckpoint) -> Result<(Vec<ChangeEvent>, CdcCheckpoint), CdcError> + Send + Sync;

/// 轮询式 CDC 捕获器(真实实现)
///
/// 基于检查点游标的增量轮询,不依赖复制协议:
///   - `poll_once`:单次增量拉取(checkpoint 推进)
///   - `start_capture`:持续轮询事件流(按 poll_interval 间隔)
///
/// 适用场景:变更频率中等的生产 CDC(轮询间隔 ≥ 1s),
/// 高吞吐场景请使用协议级捕获(PostgreSQL WAL / MySQL binlog)。
pub struct PollingCapturer {
    dialect: DbType,
    poll_interval: Duration,
    poll_fn: Arc<PollFn>,
}

impl PollingCapturer {
    /// 创建轮询捕获器
    ///
    /// `poll_interval` 轮询间隔;`poll_fn` 增量拉取函数(见 [`PollFn`])。
    pub fn new(dialect: DbType, poll_interval: Duration, poll_fn: Arc<PollFn>) -> Self {
        Self {
            dialect,
            poll_interval,
            poll_fn,
        }
    }

    /// 方言类型
    pub fn dialect(&self) -> DbType {
        self.dialect
    }

    /// 单次增量拉取:从 checkpoint 拉取新事件并推进检查点
    ///
    /// `checkpoint` 为 `None` 时从初始位置(seq=0)拉取。
    pub async fn poll_once(
        &self,
        checkpoint: Option<CdcCheckpoint>,
    ) -> Result<(Vec<ChangeEvent>, CdcCheckpoint), CdcError> {
        let initial = CdcCheckpoint {
            dialect: self.dialect,
            position: CheckpointPosition::TriggerSeq(0),
            updated_at: 0,
        };
        let cp = checkpoint.unwrap_or(initial);
        (self.poll_fn)(&cp)
    }

    /// 启动持续轮询,返回变更事件流
    ///
    /// 内部按 `poll_interval` 间隔调用 `poll_fn`,事件逐个产出;
    /// 轮询错误时流结束(调用方可重连后重新 `start_capture`)。
    pub async fn start_capture(
        &self,
        checkpoint: Option<CdcCheckpoint>,
    ) -> Result<Pin<Box<dyn Stream<Item = ChangeEvent> + Send>>, CdcError> {
        let poll_fn = Arc::clone(&self.poll_fn);
        let interval = self.poll_interval;
        let dialect = self.dialect;
        let initial_cp = CdcCheckpoint {
            dialect,
            position: CheckpointPosition::TriggerSeq(0),
            updated_at: 0,
        };

        let stream = futures::stream::unfold(
            (VecDeque::new(), checkpoint.unwrap_or(initial_cp)),
            move |(mut pending, mut cp)| {
                let poll_fn = Arc::clone(&poll_fn);
                async move {
                    loop {
                        // 先消费已拉取的事件
                        if let Some(ev) = pending.pop_front() {
                            return Some((ev, (pending, cp)));
                        }
                        // 拉取新一批
                        match (poll_fn)(&cp) {
                            Ok((events, new_cp)) => {
                                cp = new_cp;
                                pending.extend(events);
                                if pending.is_empty() {
                                    tokio::time::sleep(interval).await;
                                    continue;
                                }
                            }
                            Err(_) => return None, // 轮询错误:结束流
                        }
                    }
                }
            },
        );
        Ok(Box::pin(stream))
    }
}

#[cfg(test)]
mod polling_tests {
    use super::*;
    use crate::cdc::{ChangeOp, Row};
    use std::collections::VecDeque;
    use std::sync::Mutex;

    /// 内存"伪数据源":模拟 DB 变更日志(seq 递增)
    struct FakeChangeSource {
        events: Mutex<VecDeque<ChangeEvent>>,
        next_seq: Mutex<u64>,
    }

    impl FakeChangeSource {
        fn new(events: Vec<ChangeEvent>) -> Self {
            Self {
                events: Mutex::new(events.into()),
                next_seq: Mutex::new(0),
            }
        }

        /// 轮询函数:从 checkpoint 的 seq 开始拉取新事件
        fn poll(&self, cp: &CdcCheckpoint) -> Result<(Vec<ChangeEvent>, CdcCheckpoint), CdcError> {
            let mut events = self.events.lock().unwrap_or_else(|e| e.into_inner());
            let mut next = self.next_seq.lock().unwrap_or_else(|e| e.into_inner());
            let mut batch = vec![];
            // 伪数据源:pop 全部剩余事件(checkpoint 语义由调用方按 seq 过滤)
            while let Some(ev) = events.pop_front() {
                batch.push(ev);
                *next += 1;
            }
            let new_cp = CdcCheckpoint {
                dialect: cp.dialect,
                position: CheckpointPosition::TriggerSeq(*next),
                updated_at: 0,
            };
            Ok((batch, new_cp))
        }
    }

    fn make_event(seq: u64, name: &str) -> ChangeEvent {
        let mut row = Row::new();
        row.insert("id".to_string(), serde_json::json!(seq as i64));
        row.insert("name".to_string(), serde_json::json!(name));
        ChangeEvent {
            op: ChangeOp::Insert,
            before: None,
            after: Some(row),
            timestamp: seq,
            transaction_id: format!("tx-{seq}"),
            table: "users".to_string(),
            schema: "public".to_string(),
        }
    }

    #[tokio::test]
    async fn test_polling_capturer_poll_once_incremental() {
        let source = std::sync::Arc::new(FakeChangeSource::new(vec![
            make_event(1, "alice"),
            make_event(2, "bob"),
            make_event(3, "carol"),
        ]));
        let poll_fn = {
            let s = Arc::clone(&source);
            Arc::new(move |cp: &CdcCheckpoint| s.poll(cp))
        };
        let capturer = PollingCapturer::new(DbType::Sqlite, Duration::from_millis(10), poll_fn);

        // 第一次拉取:3 条 + checkpoint 推进到 3
        let (batch1, cp1) = capturer.poll_once(None).await.unwrap();
        assert_eq!(batch1.len(), 3, "首次拉取应拿到全部 3 条");
        assert_eq!(cp1.position, CheckpointPosition::TriggerSeq(3));

        // 第二次拉取:无新事件(checkpoint 已推进)
        let (batch2, cp2) = capturer.poll_once(Some(cp1)).await.unwrap();
        assert_eq!(batch2.len(), 0, "增量拉取:checkpoint 后无新事件");
        assert_eq!(cp2.position, CheckpointPosition::TriggerSeq(3));
    }

    #[tokio::test]
    async fn test_polling_capturer_stream_output() {
        let source = std::sync::Arc::new(FakeChangeSource::new(vec![
            make_event(1, "alice"),
            make_event(2, "bob"),
        ]));
        let poll_fn = {
            let s = Arc::clone(&source);
            Arc::new(move |cp: &CdcCheckpoint| s.poll(cp))
        };
        let capturer = PollingCapturer::new(DbType::Sqlite, Duration::from_millis(10), poll_fn);

        // 持续轮询流:收集前 2 个事件
        let mut stream = capturer.start_capture(None).await.unwrap();
        let mut seen = vec![];
        for _ in 0..2 {
            if let Some(ev) = futures::StreamExt::next(&mut stream).await {
                seen.push(ev.table.clone());
            }
        }
        assert_eq!(seen, vec!["users".to_string(), "users".to_string()]);
    }

    #[test]
    fn test_polling_capturer_dialect() {
        let source = std::sync::Arc::new(FakeChangeSource::new(vec![]));
        let poll_fn = {
            let s = Arc::clone(&source);
            Arc::new(move |cp: &CdcCheckpoint| s.poll(cp))
        };
        let capturer = PollingCapturer::new(DbType::Postgres, Duration::from_secs(1), poll_fn);
        assert_eq!(capturer.dialect(), DbType::Postgres);
    }
}