rustcdc 0.3.0

Embeddable Rust CDC library focused on correctness-first capture primitives
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
#![cfg(feature = "mysql")]

use std::{
    path::{Path, PathBuf},
    process::{Child, Command},
    time::Duration,
};

use rustcdc::{
    checkpoint::{Checkpoint, FileCheckpoint, MysqlOffset},
    core::Operation,
    schema_history::InMemorySchemaHistory,
    CdcRuntime, MysqlSourceConfig, RuntimeConfig, RuntimeSourceConfig, TransportConfig,
};
#[cfg(feature = "encryption")]
use rustcdc::{
    core::SecretString,
    transform::{MaskHashConfig, MaskHashTransform, MaskRule},
};
use sqlx::Row;
use testcontainers::{
    core::{IntoContainerPort, WaitFor},
    runners::AsyncRunner,
    GenericImage, ImageExt,
};

mod process_crash_marker;
mod process_crash_worker;
use process_crash_marker::{read_worker_batch_len, read_worker_marker, wait_for_marker};
use process_crash_worker::resolve_xtask_worker_bin;

async fn connect_admin_pool(dsn: &str) -> rustcdc::Result<sqlx::MySqlPool> {
    let mut last_error = None;
    for _ in 0..30 {
        match sqlx::mysql::MySqlPoolOptions::new()
            .max_connections(1)
            .connect(dsn)
            .await
        {
            Ok(pool) => return Ok(pool),
            Err(error) => {
                last_error = Some(error);
                tokio::time::sleep(Duration::from_millis(500)).await;
            }
        }
    }

    Err(rustcdc::Error::SourceError(format!(
        "failed to connect mysql admin pool: {}",
        last_error
            .map(|error| error.to_string())
            .unwrap_or_else(|| "unknown error".to_string())
    )))
}

#[tokio::test]
async fn runtime_mysql_process_kill_replays_uncommitted_batch() -> rustcdc::Result<()> {
    run_mysql_process_kill_replay_scenario(false).await
}

#[tokio::test]
async fn runtime_mysql_process_kill_resumes_snapshot_after_committed_batch() -> rustcdc::Result<()>
{
    if std::env::var("CDC_RS_RUN_DOCKER_TESTS").as_deref() != Ok("1") {
        eprintln!(
            "skipping mysql snapshot crash-resume integration test (set CDC_RS_RUN_DOCKER_TESTS=1)"
        );
        return Ok(());
    }

    let container = GenericImage::new("mysql", "8.0")
        .with_exposed_port(3306.tcp())
        .with_wait_for(WaitFor::message_on_stderr("ready for connections"))
        .with_env_var("MYSQL_ROOT_PASSWORD", "rootpass")
        .with_env_var("MYSQL_DATABASE", "cdc")
        .start()
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    let host = container
        .get_host()
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;
    let host_text = host.to_string();
    let port = container
        .get_host_port_ipv4(3306.tcp())
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    let admin_dsn = format!("mysql://root:rootpass@{host}:{port}/cdc");
    let admin_pool = connect_admin_pool(&admin_dsn).await?;

    sqlx::query("DROP TABLE IF EXISTS runtime_crash_snapshot_users")
        .execute(&admin_pool)
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    sqlx::query(
        "CREATE TABLE runtime_crash_snapshot_users (
            id BIGINT PRIMARY KEY AUTO_INCREMENT,
            payload VARCHAR(255) NOT NULL
        ) ENGINE=InnoDB",
    )
    .execute(&admin_pool)
    .await
    .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    let total_rows = 600_i64;
    for id in 1_i64..=total_rows {
        sqlx::query("INSERT INTO runtime_crash_snapshot_users (payload) VALUES (?)")
            .bind(format!("payload-{id}"))
            .execute(&admin_pool)
            .await
            .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;
    }

    let checkpoint_dir = tempfile::tempdir().map_err(rustcdc::Error::IoError)?;
    let marker_file = checkpoint_dir.path().join("worker-polled.marker");

    let mut worker = spawn_crash_worker(
        &host_text,
        port,
        checkpoint_dir.path(),
        &marker_file,
        410,
        Some("runtime_crash_snapshot_users"),
        true,
    )?;

    wait_for_marker(&marker_file, Duration::from_secs(90))?;
    let marker = read_worker_marker(&marker_file)?;
    assert!(marker.acked, "worker should ack first snapshot batch");
    assert!(!marker.ids.is_empty(), "worker should record acked ids");

    worker.kill().map_err(rustcdc::Error::IoError)?;
    let _ = worker.wait().map_err(rustcdc::Error::IoError)?;

    let reader_after_worker = FileCheckpoint::new(checkpoint_dir.path());
    let saved = reader_after_worker.load().await?.ok_or_else(|| {
        rustcdc::Error::StateError("checkpoint should exist after worker ack".into())
    })?;
    assert_eq!(saved.source_type(), "mysql_snapshot");
    assert_eq!(
        reader_after_worker.get_committed_count().await?,
        marker.events as u64
    );

    let source_cfg = MysqlSourceConfig {
        host: host_text,
        port,
        user: "root".to_string(),
        password: "rootpass".to_string().into(),
        database: "cdc".to_string(),
        server_id: 411,
        gtid_mode_enabled: false,
        binlog_format_check: true,
        transport: TransportConfig::plaintext(),
        conn_timeout_secs: 30,
        stream_poll_interval_ms: 50,
        max_events_per_poll: 1_000,
        ..Default::default()
    };

    let mut runtime = CdcRuntime::new(
        RuntimeConfig::new(
            RuntimeSourceConfig::Mysql(source_cfg),
            FileCheckpoint::new(checkpoint_dir.path()),
            InMemorySchemaHistory::default(),
        )
        .with_snapshot_tables(vec!["runtime_crash_snapshot_users".to_string()])
        .with_max_buffer_size(256)
        .with_max_poll_wait_ms(150),
    )?;

    runtime.start().await?;

    let mut resumed_snapshot_ids = std::collections::HashSet::new();
    for _ in 0..80 {
        let batch = runtime.poll_event_batch().await?;
        if batch.is_empty() {
            continue;
        }

        for event in batch.events() {
            if event.op != Operation::Read {
                continue;
            }
            let id = event
                .after
                .as_ref()
                .and_then(|after| after.get("id"))
                .and_then(|value| value.as_i64())
                .ok_or_else(|| rustcdc::Error::StateError("snapshot event id missing".into()))?;
            resumed_snapshot_ids.insert(id.to_string());
        }

        runtime.commit_ack(batch.ack_mode()).await?;
        if resumed_snapshot_ids.len() >= (total_rows as usize).saturating_sub(marker.ids.len()) {
            break;
        }
    }

    assert!(
        marker.ids.is_disjoint(&resumed_snapshot_ids),
        "resumed snapshot should not replay ids already commit-acked before crash"
    );
    assert!(
        resumed_snapshot_ids.len() >= (total_rows as usize).saturating_sub(marker.ids.len()),
        "expected resumed snapshot to deliver remaining rows"
    );

    Ok(())
}

#[cfg(feature = "encryption")]
#[tokio::test]
async fn runtime_mysql_process_kill_replays_uncommitted_batch_with_encryption_transform(
) -> rustcdc::Result<()> {
    run_mysql_process_kill_replay_scenario(true).await
}

async fn run_mysql_process_kill_replay_scenario(
    _enable_encryption_transform: bool,
) -> rustcdc::Result<()> {
    if std::env::var("CDC_RS_RUN_DOCKER_TESTS").as_deref() != Ok("1") {
        eprintln!("skipping mysql process crash integration test (set CDC_RS_RUN_DOCKER_TESTS=1)");
        return Ok(());
    }

    let container = GenericImage::new("mysql", "8.0")
        .with_exposed_port(3306.tcp())
        .with_wait_for(WaitFor::message_on_stderr("ready for connections"))
        .with_env_var("MYSQL_ROOT_PASSWORD", "rootpass")
        .with_env_var("MYSQL_DATABASE", "cdc")
        .start()
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    let host = container
        .get_host()
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;
    let host_text = host.to_string();
    let port = container
        .get_host_port_ipv4(3306.tcp())
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    let admin_dsn = format!("mysql://root:rootpass@{host}:{port}/cdc");
    let admin_pool = connect_admin_pool(&admin_dsn).await?;

    sqlx::query("DROP TABLE IF EXISTS runtime_crash_users")
        .execute(&admin_pool)
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    sqlx::query(
        "CREATE TABLE runtime_crash_users (
            id BIGINT PRIMARY KEY AUTO_INCREMENT,
            payload VARCHAR(255) NOT NULL
        ) ENGINE=InnoDB",
    )
    .execute(&admin_pool)
    .await
    .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;

    let status: sqlx::mysql::MySqlRow = sqlx::query("SHOW MASTER STATUS")
        .fetch_one(&admin_pool)
        .await
        .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;
    let baseline_file: String = status.try_get(0).unwrap_or_default();
    let baseline_pos: u32 = status.try_get::<u64, _>(1).unwrap_or(4) as u32;
    let baseline_gtid: String = sqlx::query_scalar("SELECT @@GLOBAL.GTID_EXECUTED")
        .fetch_optional(&admin_pool)
        .await
        .ok()
        .flatten()
        .unwrap_or_default();

    let checkpoint_dir = tempfile::tempdir().map_err(rustcdc::Error::IoError)?;
    let marker_file = checkpoint_dir.path().join("worker-polled.marker");

    let mut seed_checkpoint = FileCheckpoint::new(checkpoint_dir.path());
    seed_checkpoint
        .save(
            &MysqlOffset {
                gtid: baseline_gtid,
                binlog_file: baseline_file,
                binlog_pos: baseline_pos,
            },
            0,
        )
        .await?;
    drop(seed_checkpoint);

    for id in 1_i64..=100_i64 {
        sqlx::query("INSERT INTO runtime_crash_users (payload) VALUES (?)")
            .bind(format!("payload-{id}"))
            .execute(&admin_pool)
            .await
            .map_err(|error| rustcdc::Error::SourceError(error.to_string()))?;
    }

    let mut worker = spawn_crash_worker(
        &host_text,
        port,
        checkpoint_dir.path(),
        &marker_file,
        402,
        None,
        false,
    )?;

    wait_for_marker(&marker_file, Duration::from_secs(90))?;
    let worker_batch_len = read_worker_batch_len(&marker_file)?;

    worker.kill().map_err(rustcdc::Error::IoError)?;
    let _ = worker.wait().map_err(rustcdc::Error::IoError)?;

    let reader_before = FileCheckpoint::new(checkpoint_dir.path());
    assert_eq!(reader_before.get_committed_count().await?, 0);

    let source_cfg = MysqlSourceConfig {
        host: host_text,
        port,
        user: "root".to_string(),
        password: "rootpass".to_string().into(),
        database: "cdc".to_string(),
        server_id: 403,
        gtid_mode_enabled: false,
        binlog_format_check: true,
        transport: TransportConfig::plaintext(),
        conn_timeout_secs: 30,
        stream_poll_interval_ms: 50,
        max_events_per_poll: 1_000,
        ..Default::default()
    };

    let mut runtime = CdcRuntime::new(
        RuntimeConfig::new(
            RuntimeSourceConfig::Mysql(source_cfg),
            FileCheckpoint::new(checkpoint_dir.path()),
            InMemorySchemaHistory::default(),
        )
        .with_max_buffer_size(256)
        .with_max_poll_wait_ms(150),
    )?;

    #[cfg(feature = "encryption")]
    if _enable_encryption_transform {
        use ahash::AHashMap as HashMap;

        let mut mask_rules = HashMap::new();
        mask_rules.insert(
            "payload".to_string(),
            MaskRule::Encrypt(SecretString::new("integration-key")),
        );
        runtime.add_transform(Box::new(MaskHashTransform::new(MaskHashConfig {
            mask_rules,
            default_rule: MaskRule::UnsaltedSha256,
        })));
    }

    runtime.start().await?;

    let replay_batch = poll_until_batch_at_least(&mut runtime, 1, 50).await?;
    assert_eq!(replay_batch.len(), worker_batch_len);

    #[cfg(feature = "encryption")]
    if _enable_encryption_transform {
        for event in replay_batch.events() {
            if let Some(payload) = event
                .after
                .as_ref()
                .and_then(|after| after.get("payload"))
                .and_then(|value| value.as_str())
            {
                assert!(
                    payload.starts_with("enc:"),
                    "expected encrypted payload format, got: {payload}"
                );
            }
        }
    }

    runtime.commit_ack(replay_batch.ack_mode()).await?;

    let reader_after = FileCheckpoint::new(checkpoint_dir.path());
    assert_eq!(
        reader_after.get_committed_count().await?,
        worker_batch_len as u64
    );

    Ok(())
}

fn spawn_crash_worker(
    host: &str,
    port: u16,
    checkpoint_dir: &Path,
    marker_file: &Path,
    server_id: u32,
    snapshot_table: Option<&str>,
    ack_first_batch: bool,
) -> rustcdc::Result<Child> {
    let worker_bin = resolve_worker_bin()?;

    let mut command = Command::new(worker_bin);
    command
        .env("CDC_RS_WORKER_HOST", host)
        .env("CDC_RS_WORKER_PORT", port.to_string())
        .env("CDC_RS_WORKER_USER", "root")
        .env("CDC_RS_WORKER_PASSWORD", "rootpass")
        .env("CDC_RS_WORKER_DATABASE", "cdc")
        .env("CDC_RS_WORKER_SERVER_ID", server_id.to_string())
        .env("CDC_RS_WORKER_CHECKPOINT_DIR", checkpoint_dir)
        .env("CDC_RS_WORKER_MARKER_FILE", marker_file)
        .env(
            "CDC_RS_WORKER_ACK_FIRST_BATCH",
            if ack_first_batch { "1" } else { "0" },
        );
    if let Some(table) = snapshot_table {
        command.env("CDC_RS_WORKER_SNAPSHOT_TABLES", table);
    }
    command.spawn().map_err(rustcdc::Error::IoError)
}

fn resolve_worker_bin() -> rustcdc::Result<PathBuf> {
    resolve_xtask_worker_bin(
        "mysql_crash_worker",
        "mysql",
        "CARGO_BIN_EXE_mysql_crash_worker",
        "mysql crash worker binary not found; build with `cargo build -p xtask --bin mysql_crash_worker --features mysql`",
    )
}

async fn poll_until_batch_at_least(
    runtime: &mut CdcRuntime,
    expected: usize,
    rounds: usize,
) -> rustcdc::Result<rustcdc::EventBatch> {
    for _ in 0..rounds {
        let batch = runtime.poll_event_batch().await?;
        if batch.len() >= expected {
            return Ok(batch);
        }
    }

    Err(rustcdc::Error::TimeoutError(format!(
        "timed out waiting for event batch of at least {expected} events"
    )))
}