pg2any_lib 0.11.0

PostgreSQL to Any database library with Change Data Capture (CDC) and logical replication support
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
use crate::error::Result;
use crate::monitoring::{MetricsCollector, MetricsCollectorTrait};
use crate::transaction_manager::{
    PendingTransactionFile, TransactionFileMetadata, TransactionManager,
};
use crate::types::{EventType, Lsn};
use chrono::{DateTime, Utc};
use pg_walstream::LogicalReplicationStream;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{mpsc, oneshot, Mutex};
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};

/// Commit a transaction file and notify the consumer.
///
/// Moves the transaction from `sql_received_tx/` to `sql_pending_tx/`, reads the
/// resulting metadata, and sends a notification through the mpsc channel so the
/// consumer can pick it up for execution.
pub(crate) async fn handle_transaction_commit(
    transaction_id: u32,
    event_lsn: Option<Lsn>,
    transaction_type: &str,
    transaction_file_manager: &Arc<TransactionManager>,
    commit_notifier: &mpsc::Sender<PendingTransactionFile>,
    metrics_collector: &Arc<MetricsCollector>,
) -> Result<()> {
    match transaction_file_manager
        .commit_transaction(transaction_id, event_lsn)
        .await
    {
        Ok(pending_path) => {
            info!(
                "Committed {} transaction file to: {:?} (commit_lsn: {:?})",
                transaction_type, pending_path, event_lsn
            );

            match tokio::fs::read_to_string(&pending_path).await {
                Ok(content) => match serde_json::from_str::<TransactionFileMetadata>(&content) {
                    Ok(metadata) => {
                        let notification = PendingTransactionFile {
                            file_path: pending_path.clone(),
                            metadata,
                        };
                        if let Err(e) = commit_notifier.send(notification).await {
                            warn!(
                                    "Failed to send commit notification to consumer: {}. Consumer may have stopped.",
                                    e
                                );
                        }
                    }
                    Err(e) => {
                        error!("Failed to parse metadata from {:?}: {}", pending_path, e);
                        metrics_collector.record_error("metadata_parse_failed", "producer");
                    }
                },
                Err(e) => {
                    error!("Failed to read metadata from {:?}: {}", pending_path, e);
                    metrics_collector.record_error("metadata_read_failed", "producer");
                }
            }
            Ok(())
        }
        Err(e) => {
            error!(
                "Failed to commit {} transaction file for tx {}: {}",
                transaction_type, transaction_id, e
            );
            metrics_collector.record_error("transaction_file_commit_failed", "producer");
            Err(e)
        }
    }
}

/// File-based producer task: reads events from the PostgreSQL replication stream
/// and persists them as transaction files.
///
/// The producer collects events between BEGIN and COMMIT, writing them to
/// transaction files in `sql_data_tx/`. On COMMIT the metadata is moved from
/// `sql_received_tx/` to `sql_pending_tx/`, making it available for the consumer.
///
/// ## Shutdown Coordination
///
/// When the cancellation token fires:
/// 1. Producer flushes buffers and drops the mpsc sender ("no more messages")
/// 2. Sends oneshot signal to notify consumer it has stopped
/// 3. Consumer then drains mpsc channel and processes remaining transactions
pub(crate) async fn run_producer(
    replication_stream: Arc<Mutex<LogicalReplicationStream>>,
    cancellation_token: CancellationToken,
    start_lsn: Lsn,
    metrics_collector: Arc<MetricsCollector>,
    transaction_file_manager: Arc<TransactionManager>,
    commit_notifier: mpsc::Sender<PendingTransactionFile>,
    producer_shutdown_signal: oneshot::Sender<()>,
) -> Result<()> {
    info!(
        "Starting replication producer, last time start_lsn: {}",
        start_lsn
    );

    metrics_collector.update_source_connection_status(true);

    // Restore active transaction files from sql_received_tx/
    let active_tx_files = transaction_file_manager
        .restore_received_transactions()
        .await?;

    info!(
        "Producer starting with {} active transaction file(s) from sql_received_tx/",
        active_tx_files.len()
    );

    let mut current_normal_tx: Option<(u32, DateTime<Utc>)> = None;
    let mut streaming_txs: HashMap<u32, DateTime<Utc>> = HashMap::new();
    let mut active_streaming_dml_txid: Option<u32> = None;

    // Restore transactions from active_tx_files based on their type
    for metadata in active_tx_files {
        let tx_id = metadata.transaction_id;
        let timestamp = metadata.commit_timestamp;

        if metadata.transaction_type == "streaming" {
            streaming_txs.insert(tx_id, timestamp);
            info!(
                "Restored streaming transaction {} from sql_received_tx/",
                tx_id
            );
        } else {
            if current_normal_tx.is_some() {
                warn!(
                    "Multiple normal transactions found in sql_received_tx/, only one expected. Keeping tx {}",
                    tx_id
                );
            }
            current_normal_tx = Some((tx_id, timestamp));
            info!(
                "Restored normal transaction {} from sql_received_tx/",
                tx_id
            );
        }
    }

    while !cancellation_token.is_cancelled() {
        let event_result = {
            let mut stream = replication_stream.lock().await;
            stream.next_event_with_retry(&cancellation_token).await
        };

        match event_result {
            Ok(event) => {
                metrics_collector.record_received_lsn(event.lsn.0);
                metrics_collector.record_event(&event);

                match &event.event_type {
                    EventType::Begin {
                        transaction_id,
                        commit_timestamp,
                        ..
                    } => {
                        debug!(
                            "BEGIN: transaction_id={}, commit_timestamp={}",
                            transaction_id, commit_timestamp
                        );

                        if current_normal_tx.is_some() {
                            warn!("BEGIN received while normal transaction already active - replacing");
                        }

                        match transaction_file_manager
                            .begin_transaction(*transaction_id, *commit_timestamp, "normal")
                            .await
                        {
                            Ok(_) => {
                                current_normal_tx = Some((*transaction_id, *commit_timestamp));
                                debug!("Created normal transaction file for tx {}", transaction_id);
                            }
                            Err(e) => {
                                error!(
                                    "Failed to create transaction file for tx {}: {}",
                                    transaction_id, e
                                );
                                metrics_collector
                                    .record_error("transaction_file_create_failed", "producer");
                                return Err(e);
                            }
                        }
                    }

                    EventType::Commit { .. } => {
                        if let Some((tx_id, _)) = current_normal_tx.take() {
                            info!("Producer: Committing normal transaction {}", tx_id);

                            if let Err(e) = handle_transaction_commit(
                                tx_id,
                                Some(event.lsn),
                                "normal",
                                &transaction_file_manager,
                                &commit_notifier,
                                &metrics_collector,
                            )
                            .await
                            {
                                error!(
                                    "Failed to handle normal transaction commit for tx {}: {}",
                                    tx_id, e
                                );
                                return Err(e);
                            }
                        } else {
                            warn!("Received COMMIT without active normal transaction, ignoring");
                        }
                    }

                    EventType::StreamStart {
                        transaction_id,
                        first_segment,
                    } => {
                        debug!(
                            "StreamStart: transaction_id={}, first_segment={}",
                            transaction_id, first_segment
                        );

                        active_streaming_dml_txid = Some(*transaction_id);

                        if *first_segment {
                            let timestamp = chrono::Utc::now();
                            match transaction_file_manager
                                .begin_transaction(*transaction_id, timestamp, "streaming")
                                .await
                            {
                                Ok(_) => {
                                    debug!(
                                        "Created streaming transaction file for tx {}",
                                        transaction_id
                                    );
                                    streaming_txs.insert(*transaction_id, timestamp);
                                }
                                Err(e) => {
                                    error!(
                                        "Failed to create streaming transaction file for tx {}: {}",
                                        transaction_id, e
                                    );
                                    metrics_collector
                                        .record_error("transaction_file_create_failed", "producer");
                                    return Err(e);
                                }
                            }
                        }
                    }

                    EventType::StreamStop => {
                        if let Some(txid) = active_streaming_dml_txid {
                            info!("Producer: StreamStop for streaming transaction {}", txid);
                            active_streaming_dml_txid = None;
                        } else {
                            warn!("Producer: StreamStop received but no active streaming DML transaction");
                        }
                    }

                    EventType::StreamCommit {
                        transaction_id,
                        commit_timestamp: _,
                        ..
                    } => {
                        info!("Producer: StreamCommit for transaction {}", transaction_id);

                        if streaming_txs.remove(transaction_id).is_some() {
                            if let Err(e) = handle_transaction_commit(
                                *transaction_id,
                                Some(event.lsn),
                                "streaming",
                                &transaction_file_manager,
                                &commit_notifier,
                                &metrics_collector,
                            )
                            .await
                            {
                                error!(
                                    "Failed to handle streaming transaction commit for tx {}: {}",
                                    transaction_id, e
                                );
                                return Err(e);
                            }
                        } else {
                            warn!("StreamCommit for unknown transaction {}", transaction_id);
                        }
                    }

                    EventType::StreamAbort { transaction_id, .. } => {
                        debug!("StreamAbort: transaction_id={}", transaction_id);

                        if let Some(timestamp) = streaming_txs.remove(transaction_id) {
                            match transaction_file_manager
                                .abort_transaction(*transaction_id, timestamp)
                                .await
                            {
                                Ok(_) => {
                                    debug!(
                                        "Aborted streaming transaction file for tx {}",
                                        transaction_id
                                    );
                                }
                                Err(e) => {
                                    error!(
                                        "Failed to abort transaction file for tx {}: {}",
                                        transaction_id, e
                                    );
                                    metrics_collector
                                        .record_error("transaction_file_abort_failed", "producer");
                                }
                            }
                        } else {
                            warn!("StreamAbort for unknown transaction {}", transaction_id);
                        }
                    }

                    EventType::Insert { .. }
                    | EventType::Update { .. }
                    | EventType::Delete { .. }
                    | EventType::Truncate(_) => {
                        let target_tx_id = if let Some((tx_id, _)) = current_normal_tx {
                            debug!("Appending DML to normal transaction {}", tx_id);
                            Some(tx_id)
                        } else if let Some(streaming_txid) = active_streaming_dml_txid {
                            if streaming_txs.contains_key(&streaming_txid) {
                                debug!("Appending DML to streaming transaction {}", streaming_txid);
                                Some(streaming_txid)
                            } else {
                                error!(
                                    "Active streaming DML txid {} not found in streaming_txs map",
                                    streaming_txid
                                );
                                None
                            }
                        } else {
                            warn!(
                                "Received DML event with no active transaction (normal or streaming): {:?}",
                                event.event_type
                            );
                            None
                        };

                        if let Some(tx_id) = target_tx_id {
                            if let Err(e) =
                                transaction_file_manager.append_event(tx_id, &event).await
                            {
                                error!("Failed to append event to transaction {}: {}", tx_id, e);
                                metrics_collector
                                    .record_error("transaction_file_append_failed", "producer");
                                return Err(e);
                            }
                        }
                    }

                    _ => {
                        debug!("Skipping metadata event: {:?}", event.event_type);
                    }
                }
            }
            Err(e) => {
                error!("Error reading from replication stream: {}", e);
                metrics_collector.record_error("replication_stream_error", "producer");
                break;
            }
        }
    }

    info!("File-based producer shutting down");

    if let Err(e) = transaction_file_manager.flush_all_buffers().await {
        error!("Failed to flush buffers during shutdown: {}", e);
        metrics_collector.record_error("buffer_flush_failed", "producer");
    } else {
        info!("Successfully flushed all buffers");
    }

    let total_incomplete = if let Some((tx_id, _)) = current_normal_tx {
        info!(
            "Producer shutdown with incomplete normal transaction {}",
            tx_id
        );
        1 + streaming_txs.len()
    } else {
        streaming_txs.len()
    };

    if total_incomplete > 0 {
        info!(
            "Producer shutdown with {} incomplete transaction(s) in sql_received_tx/ (will be recovered on restart)",
            total_incomplete
        );
    }

    metrics_collector.update_source_connection_status(false);

    if producer_shutdown_signal.send(()).is_err() {
        warn!("Producer: Failed to send shutdown signal (consumer may have already stopped)");
    } else {
        info!("Producer: Sent shutdown notification to consumer");
    }

    info!("Producer shutdown complete");
    Ok(())
}