datum-cdc 0.10.3

PostgreSQL logical-replication CDC sources for Datum streams
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
#![forbid(unsafe_code)]

use std::time::{Duration, SystemTime, UNIX_EPOCH};

use datum::{Keep, Sink};
use datum_cdc::{
    CdcOffset, CdcSource, ChangeEvent, ChangeOperation, FileCheckpointStore, PgLsn,
    PostgresCdcConfig, SlotLifecycle,
};
use tokio_postgres::{Client, NoTls};

const DEFAULT_PG_URL: &str = "postgresql://datum_cdc@127.0.0.1:55433/datum_cdc";

fn pg_enabled() -> bool {
    std::env::var("DATUM_CDC_TEST_PG").as_deref() == Ok("1")
}

fn skip_pg() {
    eprintln!("skipping datum-cdc PostgreSQL integration test; set DATUM_CDC_TEST_PG=1 to run");
}

fn pg_url() -> String {
    std::env::var("DATUM_CDC_TEST_URL").unwrap_or_else(|_| DEFAULT_PG_URL.to_owned())
}

fn unique_name(prefix: &str) -> String {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system time before unix epoch")
        .as_nanos();
    format!("{prefix}_{}_{}", std::process::id(), nanos)
}

fn ident(name: &str) -> String {
    assert!(
        name.bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_'),
        "test identifiers are generated from safe ascii"
    );
    format!("\"{name}\"")
}

async fn connect(url: &str) -> Client {
    let (client, connection) = tokio_postgres::connect(url, NoTls)
        .await
        .expect("connect test PostgreSQL");
    tokio::spawn(async move {
        let _ = connection.await;
    });
    client
}

async fn setup(client: &Client, table: &str, publication: &str, slot: &str) {
    let table_ident = ident(table);
    let publication_ident = ident(publication);
    client
        .execute(
            "SELECT pg_drop_replication_slot($1)
             WHERE EXISTS (
               SELECT 1 FROM pg_replication_slots
               WHERE slot_name = $1 AND active = false
             )",
            &[&slot],
        )
        .await
        .expect("drop stale slot");
    client
        .batch_execute(&format!(
            "
            DROP PUBLICATION IF EXISTS {publication_ident};
            DROP TABLE IF EXISTS public.{table_ident};
            CREATE TABLE public.{table_ident} (
              id bigint PRIMARY KEY,
              run_id text NOT NULL,
              value bigint NOT NULL,
              op_seq bigint NOT NULL,
              kind text NOT NULL,
              commit_ns bigint NOT NULL,
              updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
            );
            ALTER TABLE public.{table_ident} REPLICA IDENTITY FULL;
            CREATE PUBLICATION {publication_ident} FOR TABLE public.{table_ident}
              WITH (publish = 'insert,update,delete,truncate');
            "
        ))
        .await
        .expect("create CDC test objects");
}

async fn cleanup(client: &Client, table: &str, publication: &str, slot: &str) {
    let table_ident = ident(table);
    let publication_ident = ident(publication);
    let _ = client
        .execute(
            "SELECT pg_drop_replication_slot($1)
             WHERE EXISTS (
               SELECT 1 FROM pg_replication_slots
               WHERE slot_name = $1 AND active = false
             )",
            &[&slot],
        )
        .await;
    let _ = client
        .batch_execute(&format!(
            "DROP PUBLICATION IF EXISTS {publication_ident}; DROP TABLE IF EXISTS public.{table_ident};"
        ))
        .await;
}

async fn wait_slot_active(client: &Client, slot: &str) {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    loop {
        let active = client
            .query_opt(
                "SELECT active FROM pg_replication_slots WHERE slot_name = $1",
                &[&slot],
            )
            .await
            .expect("query slot active")
            .map(|row| row.get::<_, bool>(0))
            .unwrap_or(false);
        if active {
            return;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "slot {slot} did not become active"
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

async fn wait_confirmed(client: &Client, slot: &str, lsn: PgLsn) {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    loop {
        let confirmed = client
            .query_opt(
                "SELECT confirmed_flush_lsn::text FROM pg_replication_slots WHERE slot_name = $1",
                &[&slot],
            )
            .await
            .expect("query confirmed lsn")
            .and_then(|row| row.get::<_, Option<String>>(0))
            .map(|value| PgLsn::parse(&value).expect("parse confirmed lsn"))
            .unwrap_or(PgLsn::ZERO);
        if confirmed >= lsn {
            return;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "slot {slot} confirmed_flush_lsn {confirmed} did not reach {lsn}"
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

fn build_source(
    url: &str,
    slot: &str,
    publication: &str,
    lifecycle: SlotLifecycle,
) -> datum_cdc::CdcResult<datum::Source<ChangeEvent, datum_cdc::CdcHandle>> {
    CdcSource::postgres()
        .connect(PostgresCdcConfig::from_url(url)?)
        .slot(slot)
        .publication(publication)
        .slot_lifecycle(lifecycle)
        .status_interval(Duration::from_millis(50))
        .idle_wakeup_interval(Duration::from_millis(50))
        .disable_reconnect()
        .build()
}

fn row_text<'a>(event: &'a ChangeEvent, column: &str) -> &'a str {
    let row = match event.op {
        ChangeOperation::Insert | ChangeOperation::Update => event.after.as_ref(),
        ChangeOperation::Delete => event.before.as_ref(),
        ChangeOperation::Truncate => None,
    }
    .expect("row event has tuple data");
    row.get_text(&event.relation, column)
        .expect("column exists as text")
}

async fn write_op(client: &Client, table: &str, run_id: &str, id: i64, op_seq: i64, op: &str) {
    let table_ident = ident(table);
    let value = id * 1_000_000 + op_seq;
    let commit_ns = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system time before unix epoch")
        .as_nanos() as i64;
    match op {
        "c" => {
            client
                .execute(
                    &format!(
                        "INSERT INTO public.{table_ident} (id, run_id, value, op_seq, kind, commit_ns)
                         VALUES ($1, $2, $3, $4, 'insert', $5)"
                    ),
                    &[&id, &run_id, &value, &op_seq, &commit_ns],
                )
                .await
                .expect("insert test row");
        }
        "u" => {
            client
                .execute(
                    &format!(
                        "UPDATE public.{table_ident}
                         SET value = $2, op_seq = $3, kind = 'update', commit_ns = $4
                         WHERE id = $1"
                    ),
                    &[&id, &value, &op_seq, &commit_ns],
                )
                .await
                .expect("update test row");
        }
        "d" => {
            client
                .execute(
                    &format!("DELETE FROM public.{table_ident} WHERE id = $1"),
                    &[&id],
                )
                .await
                .expect("delete test row");
        }
        other => panic!("unsupported op {other}"),
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn insert_update_delete_round_trip_and_per_key_order() {
    if !pg_enabled() {
        skip_pg();
        return;
    }
    let url = pg_url();
    let client = connect(&url).await;
    let table = unique_name("datum_cdc_roundtrip");
    let publication = unique_name("datum_cdc_pub");
    let slot = unique_name("datum_cdc_slot");
    setup(&client, &table, &publication, &slot).await;

    let source = build_source(&url, &slot, &publication, SlotLifecycle::CreateOwned).unwrap();
    let (handle, events_task) = {
        let graph = source.take(3).to_mat(Sink::collect(), Keep::both);
        let (handle, completion) = graph.run().expect("run CDC graph");
        (
            handle,
            tokio::task::spawn_blocking(move || completion.wait()),
        )
    };
    wait_slot_active(&client, &slot).await;
    let run_id = unique_name("run");
    write_op(&client, &table, &run_id, 1, 1, "c").await;
    write_op(&client, &table, &run_id, 1, 2, "u").await;
    write_op(&client, &table, &run_id, 1, 3, "d").await;

    let events = events_task
        .await
        .expect("join completion wait")
        .expect("collect events");
    assert_eq!(events.len(), 3);
    assert_eq!(
        events.iter().map(|event| event.op).collect::<Vec<_>>(),
        vec![
            ChangeOperation::Insert,
            ChangeOperation::Update,
            ChangeOperation::Delete
        ]
    );
    assert_eq!(
        events
            .iter()
            .map(|event| row_text(event, "id").parse::<i64>().unwrap())
            .collect::<Vec<_>>(),
        vec![1, 1, 1]
    );
    assert!(events.iter().all(|event| event.schema == "public"));
    assert!(events.iter().all(|event| event.table == table));
    let _ = handle.stop();
    let _ = handle.force_drop_slot().await;
    cleanup(&client, &table, &publication, &slot).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn checkpoint_feedback_and_restart_resume_do_not_replay_confirmed_rows() {
    if !pg_enabled() {
        skip_pg();
        return;
    }
    let url = pg_url();
    let client = connect(&url).await;
    let table = unique_name("datum_cdc_resume");
    let publication = unique_name("datum_cdc_pub");
    let slot = unique_name("datum_cdc_slot");
    setup(&client, &table, &publication, &slot).await;
    let checkpoint_dir = tempfile::tempdir().expect("checkpoint tempdir");
    let run_id = unique_name("run");

    let first_source = CdcSource::postgres()
        .connect(PostgresCdcConfig::from_url(&url).unwrap())
        .slot(&slot)
        .publication(&publication)
        .slot_lifecycle(SlotLifecycle::CreateOwned)
        .checkpoint_store(FileCheckpointStore::new(checkpoint_dir.path()))
        .status_interval(Duration::from_millis(50))
        .idle_wakeup_interval(Duration::from_millis(50))
        .disable_reconnect()
        .build()
        .unwrap();
    let (first_handle, first_events_task) = {
        let graph = first_source.take(1).to_mat(Sink::collect(), Keep::both);
        let (handle, completion) = graph.run().expect("run first CDC graph");
        (
            handle,
            tokio::task::spawn_blocking(move || completion.wait()),
        )
    };
    wait_slot_active(&client, &slot).await;
    write_op(&client, &table, &run_id, 1, 1, "c").await;
    let first_events = first_events_task
        .await
        .expect("join first completion")
        .expect("collect first event");
    assert_eq!(row_text(&first_events[0], "id"), "1");
    let first_lsn = first_events[0].lsn.tx_end_lsn;
    first_handle
        .checkpoint_handle()
        .checkpoint(first_events[0].lsn.clone())
        .expect("checkpoint first event");
    wait_confirmed(&client, &slot, first_lsn).await;
    let _ = first_handle.stop();

    let second_source = CdcSource::postgres()
        .connect(PostgresCdcConfig::from_url(&url).unwrap())
        .slot(&slot)
        .publication(&publication)
        .slot_lifecycle(SlotLifecycle::Existing)
        .checkpoint_store(FileCheckpointStore::new(checkpoint_dir.path()))
        .status_interval(Duration::from_millis(50))
        .idle_wakeup_interval(Duration::from_millis(50))
        .disable_reconnect()
        .build()
        .unwrap();
    let (second_handle, second_events_task) = {
        let graph = second_source.take(1).to_mat(Sink::collect(), Keep::both);
        let (handle, completion) = graph.run().expect("run second CDC graph");
        (
            handle,
            tokio::task::spawn_blocking(move || completion.wait()),
        )
    };
    wait_slot_active(&client, &slot).await;
    write_op(&client, &table, &run_id, 2, 2, "c").await;
    let second_events = second_events_task
        .await
        .expect("join second completion")
        .expect("collect second event");
    assert_eq!(row_text(&second_events[0], "id"), "2");
    second_handle
        .checkpoint_handle()
        .checkpoint(second_events[0].lsn.clone())
        .expect("checkpoint second event");
    wait_confirmed(&client, &slot, second_events[0].lsn.tx_end_lsn).await;
    let _ = second_handle.force_drop_slot().await;
    cleanup(&client, &table, &publication, &slot).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn lag_metric_reports_slot_pressure_fields() {
    if !pg_enabled() {
        skip_pg();
        return;
    }
    let url = pg_url();
    let client = connect(&url).await;
    let table = unique_name("datum_cdc_lag");
    let publication = unique_name("datum_cdc_pub");
    let slot = unique_name("datum_cdc_slot");
    setup(&client, &table, &publication, &slot).await;

    let source = build_source(&url, &slot, &publication, SlotLifecycle::CreateOwned).unwrap();
    let (handle, events_task) = {
        let graph = source.take(1).to_mat(Sink::collect(), Keep::both);
        let (handle, completion) = graph.run().expect("run CDC graph");
        (
            handle,
            tokio::task::spawn_blocking(move || completion.wait()),
        )
    };
    wait_slot_active(&client, &slot).await;
    let run_id = unique_name("run");
    write_op(&client, &table, &run_id, 1, 1, "c").await;
    let events = events_task
        .await
        .expect("join completion wait")
        .expect("collect event");
    let lag = handle.lag().await.expect("sample lag");
    assert!(lag.retained_wal_bytes >= 0);
    assert!(lag.confirmed_lag_bytes >= 0);
    handle
        .checkpoint_handle()
        .checkpoint(CdcOffset {
            slot: events[0].lsn.slot.clone(),
            tx_end_lsn: events[0].lsn.tx_end_lsn,
            commit_lsn: events[0].lsn.commit_lsn,
            xid: events[0].lsn.xid,
            event_index: events[0].lsn.event_index,
            event_count: events[0].lsn.event_count,
        })
        .expect("checkpoint event");
    wait_confirmed(&client, &slot, events[0].lsn.tx_end_lsn).await;
    let _ = handle.force_drop_slot().await;
    cleanup(&client, &table, &publication, &slot).await;
}