1- use crate :: lsn_state:: ReplicationState ;
21use crate :: pg_replicate:: util:: PostgresTableRow ;
32use crate :: pg_replicate:: {
43 conversions:: { cdc_event:: CdcEvent , table_row:: TableRow } ,
54 table:: { SrcTableId , TableSchema } ,
65} ;
76use moonlink:: TableEvent ;
7+ use moonlink:: { CommitState , ReplicationState } ;
88use more_asserts as ma;
99use postgres_replication:: protocol:: Column as ReplicationColumn ;
1010use std:: collections:: HashMap ;
@@ -35,7 +35,7 @@ struct ColumnInfo {
3535}
3636pub struct Sink {
3737 event_senders : HashMap < SrcTableId , Sender < TableEvent > > ,
38- commit_lsn_txs : HashMap < SrcTableId , watch :: Sender < u64 > > ,
38+ commit_lsn_txs : HashMap < SrcTableId , Arc < CommitState > > ,
3939 streaming_transactions_state : HashMap < u32 , TransactionState > ,
4040 transaction_state : TransactionState ,
4141 replication_state : Arc < ReplicationState > ,
@@ -98,7 +98,7 @@ impl Sink {
9898 & mut self ,
9999 src_table_id : SrcTableId ,
100100 event_sender : Sender < TableEvent > ,
101- commit_lsn_tx : watch :: Sender < u64 > ,
101+ commit_lsn_tx : Arc < CommitState > ,
102102 table_schema : & TableSchema ,
103103 ) {
104104 self . event_senders . insert ( src_table_id, event_sender) ;
@@ -215,10 +215,8 @@ impl Sink {
215215 ma:: assert_ge!( commit_body. end_lsn( ) , self . max_keepalive_lsn_seen) ;
216216 for table_id in & self . transaction_state . touched_tables {
217217 let event_sender = self . event_senders . get ( table_id) ;
218- if let Some ( commit_lsn_tx) = self . commit_lsn_txs . get ( table_id) . cloned ( ) {
219- if let Err ( e) = commit_lsn_tx. send ( commit_body. end_lsn ( ) ) {
220- warn ! ( error = ?e, "failed to send commit lsn" ) ;
221- }
218+ if let Some ( commit_lsn_tx) = self . commit_lsn_txs . get ( table_id) {
219+ commit_lsn_tx. mark ( commit_body. end_lsn ( ) ) ;
222220 }
223221 if let Some ( event_sender) = event_sender {
224222 if let Err ( e) = Self :: send_table_event (
@@ -252,10 +250,8 @@ impl Sink {
252250 if let Some ( tables_in_txn) = self . streaming_transactions_state . get ( & xact_id) {
253251 for table_id in & tables_in_txn. touched_tables {
254252 let event_sender = self . event_senders . get ( table_id) ;
255- if let Some ( commit_lsn_tx) = self . commit_lsn_txs . get ( table_id) . cloned ( ) {
256- if let Err ( e) = commit_lsn_tx. send ( stream_commit_body. end_lsn ( ) ) {
257- warn ! ( error = ?e, "failed to send stream commit lsn" ) ;
258- }
253+ if let Some ( commit_lsn_tx) = self . commit_lsn_txs . get ( table_id) {
254+ commit_lsn_tx. mark ( stream_commit_body. end_lsn ( ) ) ;
259255 }
260256 if let Some ( event_sender) = event_sender {
261257 if let Err ( e) = Self :: send_table_event (
@@ -443,9 +439,9 @@ mod tests {
443439 // Setup one table with event sender and commit lsn channel
444440 let table_id: SrcTableId = 1 ;
445441 let ( tx, mut rx) = mpsc:: channel :: < TableEvent > ( 64 ) ;
446- let ( commit_tx , _commit_rx ) = watch :: channel :: < u64 > ( 0 ) ;
442+ let commit_state = CommitState :: new ( ) ;
447443 let schema = make_table_schema ( table_id) ;
448- sink. add_table ( table_id, tx, commit_tx , & schema) ;
444+ sink. add_table ( table_id, tx, commit_state , & schema) ;
449445
450446 // Many inserts for the same (xid, table) pair
451447 let xid = Some ( 42u32 ) ;
@@ -493,10 +489,10 @@ mod tests {
493489 let b: SrcTableId = 12 ;
494490 let ( tx_a, mut rx_a) = mpsc:: channel :: < TableEvent > ( 8 ) ;
495491 let ( tx_b, mut rx_b) = mpsc:: channel :: < TableEvent > ( 8 ) ;
496- let ( commit_tx_a , _rx_a ) = watch :: channel :: < u64 > ( 0 ) ;
497- let ( commit_tx_b , _rx_b ) = watch :: channel :: < u64 > ( 0 ) ;
498- sink. add_table ( a, tx_a, commit_tx_a , & make_table_schema ( a) ) ;
499- sink. add_table ( b, tx_b, commit_tx_b , & make_table_schema ( b) ) ;
492+ let commit_state_a = CommitState :: new ( ) ;
493+ let commit_state_b = CommitState :: new ( ) ;
494+ sink. add_table ( a, tx_a, commit_state_a . clone ( ) , & make_table_schema ( a) ) ;
495+ sink. add_table ( b, tx_b, commit_state_b . clone ( ) , & make_table_schema ( b) ) ;
500496
501497 // Many inserts into A then into B within the same non-streaming transaction
502498 for _ in 0 ..5 {
@@ -528,12 +524,17 @@ mod tests {
528524 #[ tokio:: test]
529525 async fn cached_sender_cleared_on_drop_table ( ) {
530526 let replication_state = ReplicationState :: new ( ) ;
527+ let commit_state = CommitState :: new ( ) ;
531528 let mut sink = Sink :: new ( replication_state) ;
532529
533530 let table_id: SrcTableId = 21 ;
534531 let ( tx, _rx) = mpsc:: channel :: < TableEvent > ( 4 ) ;
535- let ( commit_tx, _commit_rx) = watch:: channel :: < u64 > ( 0 ) ;
536- sink. add_table ( table_id, tx, commit_tx, & make_table_schema ( table_id) ) ;
532+ sink. add_table (
533+ table_id,
534+ tx,
535+ commit_state. clone ( ) ,
536+ & make_table_schema ( table_id) ,
537+ ) ;
537538
538539 // Populate sender cache
539540 let _ = sink. get_event_sender_for ( table_id) ;
@@ -547,12 +548,18 @@ mod tests {
547548 #[ tokio:: test]
548549 async fn interleaved_streams_do_not_use_stale_cache ( ) {
549550 let replication_state = ReplicationState :: new ( ) ;
551+ let commit_state = CommitState :: new ( ) ;
550552 let mut sink = Sink :: new ( replication_state) ;
551553
552554 let table_id: SrcTableId = 31 ;
553555 let ( tx, mut rx) = mpsc:: channel :: < TableEvent > ( 16 ) ;
554556 let ( commit_tx, _commit_rx) = watch:: channel :: < u64 > ( 0 ) ;
555- sink. add_table ( table_id, tx, commit_tx, & make_table_schema ( table_id) ) ;
557+ sink. add_table (
558+ table_id,
559+ tx,
560+ commit_state. clone ( ) ,
561+ & make_table_schema ( table_id) ,
562+ ) ;
556563
557564 let xid1 = Some ( 100u32 ) ;
558565 let xid2 = Some ( 200u32 ) ;
@@ -611,16 +618,17 @@ mod tests {
611618 #[ tokio:: test]
612619 async fn cache_updates_on_table_change_same_xid ( ) {
613620 let replication_state = ReplicationState :: new ( ) ;
621+ let commit_state = CommitState :: new ( ) ;
614622 let mut sink = Sink :: new ( replication_state) ;
615623
616624 let a: SrcTableId = 41 ;
617625 let b: SrcTableId = 42 ;
618626 let ( tx_a, mut rx_a) = mpsc:: channel :: < TableEvent > ( 8 ) ;
619627 let ( tx_b, mut rx_b) = mpsc:: channel :: < TableEvent > ( 8 ) ;
620- let ( commit_tx_a , _rx_a ) = watch :: channel :: < u64 > ( 0 ) ;
621- let ( commit_tx_b , _rx_b ) = watch :: channel :: < u64 > ( 0 ) ;
622- sink. add_table ( a, tx_a, commit_tx_a , & make_table_schema ( a) ) ;
623- sink. add_table ( b, tx_b, commit_tx_b , & make_table_schema ( b) ) ;
628+ let commit_state_a = CommitState :: new ( ) ;
629+ let commit_state_b = CommitState :: new ( ) ;
630+ sink. add_table ( a, tx_a, commit_state_a . clone ( ) , & make_table_schema ( a) ) ;
631+ sink. add_table ( b, tx_b, commit_state_b . clone ( ) , & make_table_schema ( b) ) ;
624632
625633 let xid = Some ( 777u32 ) ;
626634 // A then B under same xid
@@ -651,12 +659,17 @@ mod tests {
651659 #[ tokio:: test]
652660 async fn sender_cache_persists_across_xid_and_stream_like_boundaries ( ) {
653661 let replication_state = ReplicationState :: new ( ) ;
662+ let commit_state = CommitState :: new ( ) ;
654663 let mut sink = Sink :: new ( replication_state) ;
655664
656665 let table_id: SrcTableId = 51 ;
657666 let ( tx, mut rx) = mpsc:: channel :: < TableEvent > ( 8 ) ;
658- let ( commit_tx, _commit_rx) = watch:: channel :: < u64 > ( 0 ) ;
659- sink. add_table ( table_id, tx, commit_tx, & make_table_schema ( table_id) ) ;
667+ sink. add_table (
668+ table_id,
669+ tx,
670+ commit_state. clone ( ) ,
671+ & make_table_schema ( table_id) ,
672+ ) ;
660673
661674 let xid1 = Some ( 1u32 ) ;
662675 let xid2 = Some ( 2u32 ) ;
@@ -692,12 +705,17 @@ mod tests {
692705 #[ tokio:: test]
693706 async fn non_streaming_state_resets_between_transactions ( ) {
694707 let replication_state = ReplicationState :: new ( ) ;
708+ let commit_state = CommitState :: new ( ) ;
695709 let mut sink = Sink :: new ( replication_state) ;
696710
697711 let table_id: SrcTableId = 61 ;
698712 let ( tx, mut rx) = mpsc:: channel :: < TableEvent > ( 8 ) ;
699- let ( commit_tx, _commit_rx) = watch:: channel :: < u64 > ( 0 ) ;
700- sink. add_table ( table_id, tx, commit_tx, & make_table_schema ( table_id) ) ;
713+ sink. add_table (
714+ table_id,
715+ tx,
716+ commit_state. clone ( ) ,
717+ & make_table_schema ( table_id) ,
718+ ) ;
701719
702720 // First transaction: several inserts (non-streaming)
703721 for _ in 0 ..3 {
0 commit comments