heliosdb-nano 3.23.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
//! WAL Streaming Server & Client
//!
//! Integrates the transport layer with WAL replication.
//! Implements hybrid streaming (batch catch-up + real-time) and sync modes.
//!
//! # Architecture
//!
//! ```text
//! PRIMARY                                    STANDBY
//! ┌─────────────────────┐                   ┌─────────────────────┐
//! │ StreamingServer     │                   │ StreamingClient     │
//! │ ├─ ReplicationServer│◄──TCP Stream──────│ ├─ ReplicationConn  │
//! │ ├─ WalReplicator    │                   │ ├─ WalApplicator    │
//! │ └─ SyncModeHandler  │                   │ └─ AckSender        │
//! └─────────────────────┘                   └─────────────────────┘
//! ```
//!
//! # Sync Modes
//!
//! - **Async**: Primary doesn't wait for ACKs
//! - **SemiSync**: Primary waits for transport ACK (received, not applied)
//! - **Sync**: Primary waits for apply ACK (WAL has been applied)

use super::config::{FailoverConfig, WalStreamingConfig};
use super::ha_state::{ha_state, StandbyInfo, StandbyState as HAStandbyState, SyncMode as HASyncMode};
use super::transport::{
    AckPayload, AckType, Capabilities, HandshakeRequest, HandshakeResponse,
    HeartbeatPayload, HealthStatus, Message, MessageType, NodeRole, ReplicationConnection,
    SyncModeConfig, WalBatchPayload, WalEntryPayload, WalEntryType as TransportWalEntryType,
    WalRequestPayload, HEARTBEAT_INTERVAL,
};
use super::wal_replicator::{Lsn, WalEntry, WalEntryType};
use super::wal_store::{BatchRequest, WalStore};
use super::{ReplicationError, Result};
use bytes::Bytes;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::net::TcpListener;
use tokio::sync::{broadcast, mpsc, oneshot, RwLock};
use uuid::Uuid;

// =============================================================================
// STREAMING SERVER (PRIMARY SIDE)
// =============================================================================

/// Streaming server configuration
#[derive(Debug, Clone)]
pub struct StreamingServerConfig {
    /// Listen address for replication connections
    pub listen_addr: SocketAddr,
    /// WAL streaming configuration
    pub wal_config: WalStreamingConfig,
    /// Default sync mode
    pub sync_mode: SyncModeConfig,
    /// Failover configuration
    pub failover_config: FailoverConfig,
    /// Maximum standbys
    pub max_standbys: usize,
    /// Heartbeat interval
    pub heartbeat_interval: Duration,
}

impl Default for StreamingServerConfig {
    fn default() -> Self {
        Self {
            // SAFETY: valid const address
            listen_addr: "0.0.0.0:5433".parse().unwrap(),
            wal_config: WalStreamingConfig::default(),
            sync_mode: SyncModeConfig::Async,
            failover_config: FailoverConfig::default(),
            max_standbys: 10,
            heartbeat_interval: HEARTBEAT_INTERVAL,
        }
    }
}

/// Connected standby information
struct ConnectedStandby {
    /// Node ID
    node_id: Uuid,
    /// Remote address
    addr: SocketAddr,
    /// Negotiated sync mode
    sync_mode: SyncModeConfig,
    /// Last acknowledged LSN
    ack_lsn: Lsn,
    /// Last ACK type
    ack_type: AckType,
    /// Last heartbeat time
    last_heartbeat: Instant,
    /// Health status
    health: HealthStatus,
    /// Message sender
    msg_tx: mpsc::Sender<Message>,
    /// Pending ACK waiters (sequence -> sender)
    pending_acks: HashMap<u64, oneshot::Sender<AckPayload>>,
}

/// Streaming server for primary node
pub struct StreamingServer {
    /// Server configuration
    config: StreamingServerConfig,
    /// This node's ID
    node_id: Uuid,
    /// Current write LSN
    current_lsn: Arc<AtomicU64>,
    /// Fencing token (incremented on each primary election)
    fencing_token: Arc<AtomicU64>,
    /// Current term/epoch
    term: Arc<AtomicU64>,
    /// Is this node the primary
    is_primary: Arc<AtomicBool>,
    /// Connected standbys
    standbys: Arc<RwLock<HashMap<Uuid, ConnectedStandby>>>,
    /// WAL broadcast channel
    wal_broadcast: broadcast::Sender<WalEntry>,
    /// Shutdown signal
    shutdown_tx: broadcast::Sender<()>,
    /// WAL store for batch catch-up
    wal_store: Arc<WalStore>,
}

impl StreamingServer {
    /// Create a new streaming server
    pub fn new(config: StreamingServerConfig, node_id: Uuid, wal_store: Arc<WalStore>) -> Self {
        let (wal_broadcast, _) = broadcast::channel(config.wal_config.batch_size);
        let (shutdown_tx, _) = broadcast::channel(1);

        // Register broadcast sender with global HA state for storage engine access
        ha_state().set_wal_broadcast(wal_broadcast.clone());

        Self {
            config,
            node_id,
            current_lsn: Arc::new(AtomicU64::new(0)),
            fencing_token: Arc::new(AtomicU64::new(1)),
            term: Arc::new(AtomicU64::new(1)),
            is_primary: Arc::new(AtomicBool::new(true)),
            standbys: Arc::new(RwLock::new(HashMap::new())),
            wal_broadcast,
            shutdown_tx,
            wal_store,
        }
    }

    /// Start the streaming server
    pub async fn start(&self) -> Result<()> {
        let listener = TcpListener::bind(&self.config.listen_addr)
            .await
            .map_err(|e| ReplicationError::Network(format!("Bind failed: {}", e)))?;

        tracing::info!(
            "Streaming server listening on {} (node: {})",
            self.config.listen_addr,
            self.node_id
        );

        let mut shutdown_rx = self.shutdown_tx.subscribe();

        // Spawn heartbeat task
        let heartbeat_handle = self.spawn_heartbeat_task();

        loop {
            tokio::select! {
                accept_result = listener.accept() => {
                    match accept_result {
                        Ok((stream, addr)) => {
                            if !self.is_primary.load(Ordering::SeqCst) {
                                tracing::warn!("Rejecting connection from {} - not primary", addr);
                                continue;
                            }

                            let standbys_count = self.standbys.read().await.len();
                            if standbys_count >= self.config.max_standbys {
                                tracing::warn!("Rejecting connection from {} - max standbys reached", addr);
                                continue;
                            }

                            let conn = ReplicationConnection::from_stream(stream, addr);
                            self.handle_new_connection(conn).await;
                        }
                        Err(e) => {
                            tracing::error!("Accept error: {}", e);
                        }
                    }
                }
                _ = shutdown_rx.recv() => {
                    tracing::info!("Streaming server shutting down");
                    break;
                }
            }
        }

        heartbeat_handle.abort();
        Ok(())
    }

    /// Handle a new standby connection
    async fn handle_new_connection(&self, conn: ReplicationConnection) {
        let node_id = self.node_id;
        let fencing_token = self.fencing_token.load(Ordering::SeqCst);
        let current_lsn = self.current_lsn.load(Ordering::SeqCst);
        let standbys = self.standbys.clone();
        let wal_rx = self.wal_broadcast.subscribe();
        let shutdown_rx = self.shutdown_tx.subscribe();
        let wal_store = self.wal_store.clone();

        tokio::spawn(async move {
            if let Err(e) = Self::connection_loop(
                conn,
                node_id,
                fencing_token,
                current_lsn,
                standbys,
                wal_rx,
                shutdown_rx,
                wal_store,
            )
            .await
            {
                tracing::error!("Connection error: {}", e);
            }
        });
    }

    /// Main connection handling loop
    async fn connection_loop(
        mut conn: ReplicationConnection,
        server_node_id: Uuid,
        fencing_token: u64,
        current_lsn: Lsn,
        standbys: Arc<RwLock<HashMap<Uuid, ConnectedStandby>>>,
        mut wal_rx: broadcast::Receiver<WalEntry>,
        mut shutdown_rx: broadcast::Receiver<()>,
        wal_store: Arc<WalStore>,
    ) -> Result<()> {
        let addr = conn.remote_addr();
        tracing::info!("New connection from {}", addr);

        // Wait for handshake
        let msg = conn.recv().await?;
        if msg.header.msg_type != MessageType::HandshakeRequest {
            return Err(ReplicationError::Transport("Expected HandshakeRequest".to_string()));
        }

        let request: HandshakeRequest = bincode::deserialize(&msg.payload)
            .map_err(|e| ReplicationError::Transport(format!("Deserialize failed: {}", e)))?;

        tracing::info!(
            "Handshake from {:?} node {} at {}",
            request.role,
            request.node_id,
            addr
        );

        // Build response - respect the standby's requested sync mode
        // Standby announces its desired mode; primary honors it
        let negotiated_sync = request.sync_mode;

        let response = HandshakeResponse {
            accepted: true,
            server_node_id,
            sync_mode: negotiated_sync,
            primary_lsn: current_lsn,
            slot_name: request.slot_name.clone(),
            fencing_token,
            capabilities: Capabilities::all(),
            error: None,
        };

        let response_payload = bincode::serialize(&response)
            .map_err(|e| ReplicationError::Transport(format!("Serialize failed: {}", e)))?;

        conn.send(MessageType::HandshakeResponse, Bytes::from(response_payload))
            .await?;

        // Create message channel for this standby
        let (msg_tx, mut msg_rx) = mpsc::channel::<Message>(1000);

        // Register standby
        let standby = ConnectedStandby {
            node_id: request.node_id,
            addr,
            sync_mode: negotiated_sync,
            ack_lsn: request.current_lsn.unwrap_or(0),
            ack_type: AckType::Received,
            last_heartbeat: Instant::now(),
            health: HealthStatus::Healthy,
            msg_tx: msg_tx.clone(),
            pending_acks: HashMap::new(),
        };

        standbys.write().await.insert(request.node_id, standby);
        tracing::info!("Standby {} registered", request.node_id);

        // Register with global HA state for system views
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as i64;

        let ha_sync_mode = match negotiated_sync {
            SyncModeConfig::Async => HASyncMode::Async,
            SyncModeConfig::SemiSync { .. } => HASyncMode::SemiSync,
            SyncModeConfig::Sync { .. } => HASyncMode::Sync,
        };

        ha_state().register_standby(StandbyInfo {
            node_id: request.node_id,
            address: addr.to_string(),
            connected_at: now,
            last_heartbeat: now,
            sync_mode: ha_sync_mode,
            current_lsn: request.current_lsn.unwrap_or(0),
            flush_lsn: request.current_lsn.unwrap_or(0),
            apply_lsn: request.current_lsn.unwrap_or(0),
            lag_bytes: current_lsn.saturating_sub(request.current_lsn.unwrap_or(0)),
            lag_ms: 0,
            state: HAStandbyState::Connecting,
        });

        // Check if standby needs catch-up
        let standby_lsn = request.current_lsn.unwrap_or(0);
        if standby_lsn < current_lsn {
            tracing::info!(
                "Standby {} needs catch-up: {} -> {}",
                request.node_id,
                standby_lsn,
                current_lsn
            );

            // Send WAL batches for catch-up
            Self::send_catchup_batches(&mut conn, &wal_store, standby_lsn, current_lsn).await?;
        }

        // Main streaming loop
        let standby_node_id = request.node_id;
        loop {
            tokio::select! {
                // Receive WAL entry from broadcast
                wal_result = wal_rx.recv() => {
                    match wal_result {
                        Ok(entry) => {
                            tracing::info!("StreamingServer: Forwarding WAL entry LSN={} to standby", entry.lsn);
                            // Convert to transport format
                            let payload = Self::wal_entry_to_payload(&entry);
                            let payload_bytes = bincode::serialize(&payload)
                                .map_err(|e| ReplicationError::Transport(format!("Serialize failed: {}", e)))?;

                            conn.send(MessageType::WalEntry, Bytes::from(payload_bytes)).await?;
                            tracing::info!("StreamingServer: Sent WAL entry LSN={} to standby", entry.lsn);
                        }
                        Err(broadcast::error::RecvError::Lagged(n)) => {
                            tracing::warn!("Standby {} lagged {} entries", standby_node_id, n);
                        }
                        Err(broadcast::error::RecvError::Closed) => {
                            break;
                        }
                    }
                }

                // Receive message from standby
                msg_result = conn.recv() => {
                    match msg_result {
                        Ok(msg) => {
                            Self::handle_standby_message(
                                &standbys,
                                standby_node_id,
                                msg,
                            ).await?;
                        }
                        Err(e) => {
                            tracing::warn!("Connection error from {}: {}", standby_node_id, e);
                            break;
                        }
                    }
                }

                // Send queued messages
                Some(msg) = msg_rx.recv() => {
                    conn.send_message(&msg).await?;
                }

                // Shutdown signal
                _ = shutdown_rx.recv() => {
                    break;
                }
            }
        }

        // Cleanup
        standbys.write().await.remove(&standby_node_id);
        ha_state().remove_standby(standby_node_id);
        tracing::info!("Standby {} disconnected", standby_node_id);

        Ok(())
    }

    /// Handle message from standby
    async fn handle_standby_message(
        standbys: &Arc<RwLock<HashMap<Uuid, ConnectedStandby>>>,
        standby_id: Uuid,
        msg: Message,
    ) -> Result<()> {
        tracing::debug!("StreamingServer: Received message from standby {}: type={:?}", standby_id, msg.header.msg_type);
        match msg.header.msg_type {
            MessageType::Ack => {
                let ack: AckPayload = bincode::deserialize(&msg.payload)
                    .map_err(|e| ReplicationError::Transport(format!("Deserialize failed: {}", e)))?;
                tracing::info!("StreamingServer: Received ACK from standby {} for LSN={} type={:?}", standby_id, ack.lsn, ack.ack_type);

                let mut standbys = standbys.write().await;
                if let Some(standby) = standbys.get_mut(&standby_id) {
                    standby.ack_lsn = ack.lsn;
                    standby.ack_type = ack.ack_type;
                    standby.last_heartbeat = Instant::now();

                    // Wake up pending ACK waiter if any
                    if let Some(sender) = standby.pending_acks.remove(&ack.sequence) {
                        let _ = sender.send(ack.clone());
                    }
                }
                drop(standbys); // Release lock before updating global state

                // Update global HA state
                let now = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs() as i64;

                ha_state().update_standby(standby_id, |info| {
                    info.current_lsn = ack.lsn;
                    info.flush_lsn = ack.lsn;
                    if matches!(ack.ack_type, AckType::Applied | AckType::Checkpointed) {
                        info.apply_lsn = ack.lsn;
                    }
                    info.last_heartbeat = now;
                    info.state = HAStandbyState::Streaming;
                });
            }
            MessageType::Heartbeat => {
                let heartbeat: HeartbeatPayload = bincode::deserialize(&msg.payload)
                    .map_err(|e| ReplicationError::Transport(format!("Deserialize failed: {}", e)))?;

                let mut standbys = standbys.write().await;
                if let Some(standby) = standbys.get_mut(&standby_id) {
                    standby.ack_lsn = heartbeat.apply_lsn.unwrap_or(heartbeat.flush_lsn);
                    standby.last_heartbeat = Instant::now();
                    standby.health = heartbeat.health;
                }
                drop(standbys); // Release lock before updating global state

                // Update global HA state
                let now = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs() as i64;

                ha_state().update_standby(standby_id, |info| {
                    info.current_lsn = heartbeat.current_lsn;
                    info.flush_lsn = heartbeat.flush_lsn;
                    info.apply_lsn = heartbeat.apply_lsn.unwrap_or(heartbeat.flush_lsn);
                    info.lag_bytes = heartbeat.lag_bytes;
                    info.last_heartbeat = now;
                    info.state = HAStandbyState::Streaming;
                });
            }
            MessageType::WalRequest => {
                let request: WalRequestPayload = bincode::deserialize(&msg.payload)
                    .map_err(|e| ReplicationError::Transport(format!("Deserialize failed: {}", e)))?;

                tracing::info!(
                    "WAL request from {}: {} -> {:?}",
                    standby_id,
                    request.from_lsn,
                    request.to_lsn
                );

                // Queue a catch-up response via standby's message channel
                // Note: Actual batch sending is handled elsewhere since we don't have
                // direct access to wal_store here. The standby should use the
                // dedicated catch-up protocol on connection.
            }
            _ => {
                tracing::warn!("Unexpected message type from standby: {:?}", msg.header.msg_type);
            }
        }

        Ok(())
    }

    /// Send catch-up batches to standby
    async fn send_catchup_batches(
        conn: &mut ReplicationConnection,
        wal_store: &WalStore,
        from_lsn: Lsn,
        to_lsn: Lsn,
    ) -> Result<()> {
        const BATCH_SIZE: usize = 1000;
        const MAX_BATCH_BYTES: usize = 10 * 1024 * 1024; // 10 MB

        let mut current_from = from_lsn;
        let mut batch_num = 0u32;

        loop {
            let request = BatchRequest {
                from_lsn: current_from,
                to_lsn: Some(to_lsn),
                max_entries: BATCH_SIZE,
                max_bytes: MAX_BATCH_BYTES,
            };

            let batch = wal_store.get_batch(request).await?;

            if batch.entries.is_empty() {
                break;
            }

            batch_num += 1;
            let is_final = !batch.has_more;

            tracing::debug!(
                "Sending catch-up batch {}: {} entries ({} -> {}), final={}",
                batch_num,
                batch.entries.len(),
                batch.start_lsn,
                batch.end_lsn,
                is_final
            );

            // Convert entries to transport format
            let entry_payloads: Vec<WalEntryPayload> = batch
                .entries
                .iter()
                .map(Self::wal_entry_to_payload)
                .collect();

            let batch_payload = WalBatchPayload {
                start_lsn: batch.start_lsn,
                end_lsn: batch.end_lsn,
                entry_count: batch.entries.len() as u32,
                entries: entry_payloads,
                is_final,
            };

            let payload_bytes = bincode::serialize(&batch_payload)
                .map_err(|e| ReplicationError::Transport(format!("Serialize failed: {}", e)))?;

            conn.send(MessageType::WalBatch, Bytes::from(payload_bytes)).await?;

            if is_final {
                break;
            }

            current_from = batch.end_lsn;
        }

        tracing::info!(
            "Catch-up complete: sent {} batches, {} -> {}",
            batch_num,
            from_lsn,
            to_lsn
        );

        Ok(())
    }

    /// Convert WAL entry to transport payload
    fn wal_entry_to_payload(entry: &WalEntry) -> WalEntryPayload {
        let entry_type = match entry.entry_type {
            WalEntryType::Insert => TransportWalEntryType::Insert,
            WalEntryType::Update => TransportWalEntryType::Update,
            WalEntryType::Delete => TransportWalEntryType::Delete,
            WalEntryType::TxBegin => TransportWalEntryType::TxBegin,
            WalEntryType::TxCommit => TransportWalEntryType::TxCommit,
            WalEntryType::TxRollback => TransportWalEntryType::TxAbort,
            WalEntryType::Checkpoint => TransportWalEntryType::Checkpoint,
            WalEntryType::SchemaChange => TransportWalEntryType::SchemaChange,
            WalEntryType::BranchOp => TransportWalEntryType::BranchOp,
        };

        WalEntryPayload {
            lsn: entry.lsn,
            tx_id: entry.tx_id, // Pass through transaction ID
            entry_type,
            data: entry.data.clone(),
            timestamp_us: chrono::Utc::now().timestamp_micros() as u64,
            checksum: entry.checksum,
        }
    }

    /// Spawn heartbeat monitoring task
    fn spawn_heartbeat_task(&self) -> tokio::task::JoinHandle<()> {
        let standbys = self.standbys.clone();
        let interval = self.config.heartbeat_interval;
        let node_id = self.node_id;
        let current_lsn = self.current_lsn.clone();

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);
            loop {
                interval_timer.tick().await;

                let heartbeat = HeartbeatPayload {
                    node_id,
                    role: NodeRole::Primary,
                    current_lsn: current_lsn.load(Ordering::SeqCst),
                    flush_lsn: current_lsn.load(Ordering::SeqCst),
                    apply_lsn: None,
                    timestamp_ms: chrono::Utc::now().timestamp_millis() as u64,
                    lag_bytes: 0,
                    health: HealthStatus::Healthy,
                };

                let payload = match bincode::serialize(&heartbeat) {
                    Ok(p) => p,
                    Err(_) => continue,
                };

                let standbys = standbys.read().await;
                for (_, standby) in standbys.iter() {
                    let msg = Message::new(
                        MessageType::Heartbeat,
                        Bytes::from(payload.clone()),
                        0,
                    );
                    let _ = standby.msg_tx.send(msg).await;
                }
            }
        })
    }

    /// Broadcast a WAL entry to all standbys
    pub fn broadcast(&self, entry: WalEntry) -> Result<()> {
        self.current_lsn.store(entry.lsn, Ordering::SeqCst);
        self.wal_broadcast
            .send(entry)
            .map_err(|e| ReplicationError::WalStreaming(e.to_string()))?;
        Ok(())
    }

    /// Wait for ACK from standbys based on sync mode
    pub async fn wait_for_ack(&self, lsn: Lsn) -> Result<()> {
        match self.config.sync_mode {
            SyncModeConfig::Async => {
                // No waiting in async mode
                Ok(())
            }
            SyncModeConfig::SemiSync { min_acks, timeout_ms } => {
                self.wait_for_acks(lsn, min_acks as usize, AckType::Received, timeout_ms).await
            }
            SyncModeConfig::Sync { min_applied, timeout_ms } => {
                self.wait_for_acks(lsn, min_applied as usize, AckType::Applied, timeout_ms).await
            }
        }
    }

    /// Wait for specific number of ACKs of a given type
    async fn wait_for_acks(
        &self,
        lsn: Lsn,
        min_acks: usize,
        ack_type: AckType,
        timeout_ms: u32,
    ) -> Result<()> {
        let deadline = Instant::now() + Duration::from_millis(timeout_ms as u64);

        loop {
            if Instant::now() >= deadline {
                return Err(ReplicationError::Timeout(format!(
                    "Timeout waiting for {} {:?} ACKs for LSN {}",
                    min_acks, ack_type, lsn
                )));
            }

            let standbys = self.standbys.read().await;
            let ack_count = standbys
                .values()
                .filter(|s| s.ack_lsn >= lsn && Self::ack_type_satisfies(&s.ack_type, &ack_type))
                .count();

            if ack_count >= min_acks {
                return Ok(());
            }

            drop(standbys);
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    }

    /// Check if an ACK type satisfies the required type
    fn ack_type_satisfies(actual: &AckType, required: &AckType) -> bool {
        match (actual, required) {
            (AckType::Applied, _) | (AckType::Checkpointed, _) => true,
            (AckType::Written, AckType::Written | AckType::Received) => true,
            (AckType::Received, AckType::Received) => true,
            _ => false,
        }
    }

    /// Shutdown the server
    pub fn shutdown(&self) {
        let _ = self.shutdown_tx.send(());
    }

    /// Get connected standby count
    pub async fn standby_count(&self) -> usize {
        self.standbys.read().await.len()
    }

    /// Get standby states
    pub async fn standby_states(&self) -> Vec<(Uuid, Lsn, HealthStatus)> {
        self.standbys
            .read()
            .await
            .iter()
            .map(|(id, s)| (*id, s.ack_lsn, s.health))
            .collect()
    }
}

// =============================================================================
// STREAMING CLIENT (STANDBY SIDE)
// =============================================================================

/// Streaming client configuration
#[derive(Debug, Clone)]
pub struct StreamingClientConfig {
    /// This node's ID
    pub node_id: Uuid,
    /// Primary host:port
    pub primary_addr: SocketAddr,
    /// Sync mode to request
    pub sync_mode: SyncModeConfig,
    /// Connection timeout
    pub connect_timeout: Duration,
    /// Reconnect delay
    pub reconnect_delay: Duration,
    /// Max reconnect attempts
    pub max_reconnect_attempts: u32,
}

/// Streaming client state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamingClientState {
    Disconnected,
    Connecting,
    Handshaking,
    CatchingUp,
    Streaming,
    Reconnecting,
    Error,
}

/// Streaming client for standby nodes
pub struct StreamingClient {
    /// Configuration
    config: StreamingClientConfig,
    /// Client state
    state: Arc<RwLock<StreamingClientState>>,
    /// Applied LSN (WAL entries that have been fully applied)
    applied_lsn: Arc<AtomicU64>,
    /// Flush LSN (WAL entries that have been flushed to disk but may not be applied)
    flush_lsn: Arc<AtomicU64>,
    /// Primary's LSN (from heartbeats)
    primary_lsn: Arc<AtomicU64>,
    /// Fencing token from primary
    fencing_token: Arc<AtomicU64>,
    /// Entry receiver (for external consumption)
    entry_tx: mpsc::Sender<WalEntry>,
    /// Shutdown signal
    shutdown_tx: broadcast::Sender<()>,
}

impl StreamingClient {
    /// Create a new streaming client
    pub fn new(config: StreamingClientConfig) -> (Self, mpsc::Receiver<WalEntry>) {
        let (entry_tx, entry_rx) = mpsc::channel(10000);
        let (shutdown_tx, _) = broadcast::channel(1);

        let client = Self {
            config,
            state: Arc::new(RwLock::new(StreamingClientState::Disconnected)),
            applied_lsn: Arc::new(AtomicU64::new(0)),
            flush_lsn: Arc::new(AtomicU64::new(0)),
            primary_lsn: Arc::new(AtomicU64::new(0)),
            fencing_token: Arc::new(AtomicU64::new(0)),
            entry_tx,
            shutdown_tx,
        };

        (client, entry_rx)
    }

    /// Start the streaming client
    ///
    /// Automatically reconnects to the primary with exponential backoff.
    /// Set `max_reconnect_attempts` to 0 for unlimited reconnection attempts.
    pub async fn start(&self) -> Result<()> {
        let mut reconnect_attempts: u32 = 0;
        let mut shutdown_rx = self.shutdown_tx.subscribe();
        let base_delay = self.config.reconnect_delay;
        let max_delay = Duration::from_secs(60); // Cap at 60 seconds

        loop {
            *self.state.write().await = StreamingClientState::Connecting;

            // Track state before attempting connection
            let was_streaming_before = *self.state.read().await == StreamingClientState::Streaming;

            match self.connect_and_stream().await {
                Ok(()) => {
                    // Clean shutdown
                    break;
                }
                Err(e) => {
                    // Check if we successfully entered streaming state before the error
                    // If we did, reset the counter since the reconnection worked
                    let current_state = *self.state.read().await;
                    let was_streaming = current_state == StreamingClientState::Streaming
                        || current_state == StreamingClientState::CatchingUp;

                    if was_streaming || was_streaming_before {
                        tracing::info!(
                            "Connection lost after successful streaming - resetting reconnect counter"
                        );
                        reconnect_attempts = 0;
                    }

                    reconnect_attempts += 1;

                    // Check if we've exceeded max attempts (0 = unlimited)
                    let unlimited = self.config.max_reconnect_attempts == 0;
                    if !unlimited && reconnect_attempts >= self.config.max_reconnect_attempts {
                        tracing::error!(
                            "Streaming error (attempt {}/{}): {} - giving up",
                            reconnect_attempts,
                            self.config.max_reconnect_attempts,
                            e
                        );
                        *self.state.write().await = StreamingClientState::Error;
                        return Err(e);
                    }

                    // Calculate exponential backoff delay
                    // delay = base_delay * 2^(attempts-1), capped at max_delay
                    let backoff_multiplier = 2u32.saturating_pow(reconnect_attempts.saturating_sub(1).min(6));
                    let delay = std::cmp::min(
                        base_delay.saturating_mul(backoff_multiplier),
                        max_delay,
                    );

                    if unlimited {
                        tracing::warn!(
                            "Streaming error (attempt {}): {} - reconnecting in {:?}",
                            reconnect_attempts,
                            e,
                            delay
                        );
                    } else {
                        tracing::warn!(
                            "Streaming error (attempt {}/{}): {} - reconnecting in {:?}",
                            reconnect_attempts,
                            self.config.max_reconnect_attempts,
                            e,
                            delay
                        );
                    }

                    *self.state.write().await = StreamingClientState::Reconnecting;

                    // Wait before reconnecting with exponential backoff
                    tokio::select! {
                        _ = tokio::time::sleep(delay) => {}
                        _ = shutdown_rx.recv() => {
                            tracing::info!("Streaming client shutdown requested during reconnect");
                            break;
                        }
                    }
                }
            }
        }

        *self.state.write().await = StreamingClientState::Disconnected;
        Ok(())
    }

    /// Connect to primary and start streaming
    async fn connect_and_stream(&self) -> Result<()> {
        let mut shutdown_rx = self.shutdown_tx.subscribe();

        // Connect to primary
        let mut conn = ReplicationConnection::connect(
            self.config.primary_addr,
            self.config.connect_timeout,
        )
        .await?;

        // Send handshake
        *self.state.write().await = StreamingClientState::Handshaking;

        let current_lsn = self.applied_lsn.load(Ordering::SeqCst);
        let request = HandshakeRequest {
            node_id: self.config.node_id,
            role: NodeRole::Standby,
            sync_mode: self.config.sync_mode,
            current_lsn: Some(current_lsn),
            slot_name: None, // Replication slots are not used; LSN-based tracking is sufficient
            capabilities: Capabilities::all(),
        };

        let response = conn.handshake_client(request).await?;

        if !response.accepted {
            return Err(ReplicationError::Transport(format!(
                "Handshake rejected: {}",
                response.error.unwrap_or_default()
            )));
        }

        self.fencing_token.store(response.fencing_token, Ordering::SeqCst);
        self.primary_lsn.store(response.primary_lsn, Ordering::SeqCst);

        tracing::info!(
            "Connected to primary (node: {}, LSN: {}, fencing: {})",
            response.server_node_id,
            response.primary_lsn,
            response.fencing_token
        );

        // Check if we need catch-up
        if current_lsn < response.primary_lsn {
            *self.state.write().await = StreamingClientState::CatchingUp;
            tracing::info!("Starting catch-up: {} -> {}", current_lsn, response.primary_lsn);

            // Request WAL batch for catch-up
            let wal_request = WalRequestPayload {
                from_lsn: current_lsn,
                to_lsn: Some(response.primary_lsn),
                max_entries: 1000,
                max_bytes: 10 * 1024 * 1024, // 10 MB
            };

            let payload = bincode::serialize(&wal_request)
                .map_err(|e| ReplicationError::Transport(format!("Serialize failed: {}", e)))?;

            conn.send(MessageType::WalRequest, Bytes::from(payload)).await?;

            // Note: Catch-up batches will be received in the main loop as WalBatch messages
            // The server sends them proactively after handshake, so this request is optional
        }

        *self.state.write().await = StreamingClientState::Streaming;

        // Main streaming loop
        let mut heartbeat_interval = tokio::time::interval(HEARTBEAT_INTERVAL);

        loop {
            tokio::select! {
                // Receive messages from primary
                msg_result = conn.recv() => {
                    match msg_result {
                        Ok(msg) => {
                            self.handle_primary_message(&mut conn, msg).await?;
                        }
                        Err(e) => {
                            return Err(e);
                        }
                    }
                }

                // Send heartbeat
                _ = heartbeat_interval.tick() => {
                    self.send_heartbeat(&mut conn).await?;
                }

                // Shutdown signal
                _ = shutdown_rx.recv() => {
                    conn.close().await?;
                    break;
                }
            }
        }

        Ok(())
    }

    /// Handle message from primary
    async fn handle_primary_message(
        &self,
        conn: &mut ReplicationConnection,
        msg: Message,
    ) -> Result<()> {
        match msg.header.msg_type {
            MessageType::WalEntry => {
                tracing::info!("StreamingClient: Received WalEntry message, payload_len={}", msg.payload.len());
                let payload: WalEntryPayload = bincode::deserialize(&msg.payload)
                    .map_err(|e| ReplicationError::Transport(format!("Deserialize failed: {}", e)))?;

                let entry = Self::payload_to_wal_entry(&payload);
                let lsn = entry.lsn;
                tracing::info!("StreamingClient: Processing WAL entry LSN={}", lsn);

                // Send to applicator
                self.entry_tx
                    .send(entry)
                    .await
                    .map_err(|e| ReplicationError::WalStreaming(e.to_string()))?;
                tracing::info!("StreamingClient: Sent WAL entry LSN={} to applicator", lsn);

                // Send ACK
                self.send_ack(conn, lsn, AckType::Received, msg.header.sequence).await?;
            }
            MessageType::WalBatch => {
                let payload: WalBatchPayload = bincode::deserialize(&msg.payload)
                    .map_err(|e| ReplicationError::Transport(format!("Deserialize failed: {}", e)))?;

                tracing::info!(
                    "Received WAL batch: {} entries, {} -> {}",
                    payload.entry_count,
                    payload.start_lsn,
                    payload.end_lsn
                );

                for entry_payload in &payload.entries {
                    let entry = Self::payload_to_wal_entry(entry_payload);
                    self.entry_tx
                        .send(entry)
                        .await
                        .map_err(|e| ReplicationError::WalStreaming(e.to_string()))?;
                }

                // ACK the batch
                self.send_ack(conn, payload.end_lsn, AckType::Received, msg.header.sequence).await?;

                if payload.is_final {
                    *self.state.write().await = StreamingClientState::Streaming;
                }
            }
            MessageType::Heartbeat => {
                let payload: HeartbeatPayload = bincode::deserialize(&msg.payload)
                    .map_err(|e| ReplicationError::Transport(format!("Deserialize failed: {}", e)))?;

                self.primary_lsn.store(payload.current_lsn, Ordering::SeqCst);
            }
            _ => {
                tracing::warn!("Unexpected message type from primary: {:?}", msg.header.msg_type);
            }
        }

        Ok(())
    }

    /// Convert transport payload to WAL entry
    fn payload_to_wal_entry(payload: &WalEntryPayload) -> WalEntry {
        let entry_type = match payload.entry_type {
            TransportWalEntryType::Insert => WalEntryType::Insert,
            TransportWalEntryType::Update => WalEntryType::Update,
            TransportWalEntryType::Delete => WalEntryType::Delete,
            TransportWalEntryType::TxBegin => WalEntryType::TxBegin,
            TransportWalEntryType::TxCommit => WalEntryType::TxCommit,
            TransportWalEntryType::TxAbort => WalEntryType::TxRollback,
            TransportWalEntryType::Checkpoint => WalEntryType::Checkpoint,
            TransportWalEntryType::SchemaChange => WalEntryType::SchemaChange,
            TransportWalEntryType::BranchOp => WalEntryType::BranchOp,
        };

        WalEntry {
            lsn: payload.lsn,
            tx_id: payload.tx_id, // Extract transaction ID from payload
            entry_type,
            data: payload.data.clone(),
            checksum: payload.checksum,
        }
    }

    /// Send ACK to primary
    async fn send_ack(
        &self,
        conn: &mut ReplicationConnection,
        lsn: Lsn,
        ack_type: AckType,
        sequence: u64,
    ) -> Result<()> {
        let ack = AckPayload {
            lsn,
            ack_type,
            node_id: self.config.node_id,
            timestamp_ms: chrono::Utc::now().timestamp_millis() as u64,
            sequence,
        };

        tracing::debug!("send_ack called for LSN={} type={:?}", lsn, ack_type);
        tracing::info!("StreamingClient: Sending ACK for LSN={} type={:?} seq={}", lsn, ack_type, sequence);
        let result = conn.send_ack(ack).await;
        tracing::debug!("send_ack result for LSN={}: {:?}", lsn, result.is_ok());
        match &result {
            Ok(_) => tracing::info!("StreamingClient: ACK sent successfully for LSN={}", lsn),
            Err(e) => tracing::error!("StreamingClient: Failed to send ACK for LSN={}: {}", lsn, e),
        }
        result
    }

    /// Send heartbeat to primary
    async fn send_heartbeat(&self, conn: &mut ReplicationConnection) -> Result<()> {
        let applied = self.applied_lsn.load(Ordering::SeqCst);
        let flushed = self.flush_lsn.load(Ordering::SeqCst);
        let primary = self.primary_lsn.load(Ordering::SeqCst);

        let heartbeat = HeartbeatPayload {
            node_id: self.config.node_id,
            role: NodeRole::Standby,
            current_lsn: flushed.max(applied), // Current position is max of flush and apply
            flush_lsn: flushed,                 // Separately tracked flush position
            apply_lsn: Some(applied),
            timestamp_ms: chrono::Utc::now().timestamp_millis() as u64,
            lag_bytes: primary.saturating_sub(flushed),
            health: HealthStatus::Healthy,
        };

        let payload = bincode::serialize(&heartbeat)
            .map_err(|e| ReplicationError::Transport(format!("Serialize failed: {}", e)))?;

        conn.send(MessageType::Heartbeat, Bytes::from(payload)).await?;
        Ok(())
    }

    /// Report that an entry has been applied
    pub fn report_applied(&self, lsn: Lsn) {
        self.applied_lsn.fetch_max(lsn, Ordering::SeqCst);
    }

    /// Report that an entry has been flushed to disk (but not yet applied)
    pub fn report_flushed(&self, lsn: Lsn) {
        self.flush_lsn.fetch_max(lsn, Ordering::SeqCst);
    }

    /// Get flush LSN
    pub fn flush_lsn(&self) -> Lsn {
        self.flush_lsn.load(Ordering::SeqCst)
    }

    /// Get current state
    pub async fn state(&self) -> StreamingClientState {
        *self.state.read().await
    }

    /// Get applied LSN
    pub fn applied_lsn(&self) -> Lsn {
        self.applied_lsn.load(Ordering::SeqCst)
    }

    /// Get replication lag
    pub fn lag_bytes(&self) -> u64 {
        let primary = self.primary_lsn.load(Ordering::SeqCst);
        let applied = self.applied_lsn.load(Ordering::SeqCst);
        primary.saturating_sub(applied)
    }

    /// Shutdown the client
    pub fn shutdown(&self) {
        let _ = self.shutdown_tx.send(());
    }
}

// =============================================================================
// TESTS
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_streaming_server_config_default() {
        let config = StreamingServerConfig::default();
        assert_eq!(config.max_standbys, 10);
        matches!(config.sync_mode, SyncModeConfig::Async);
    }

    #[test]
    fn test_wal_entry_to_payload_conversion() {
        let entry = WalEntry {
            lsn: 100,
            tx_id: Some(42),
            entry_type: WalEntryType::Insert,
            data: vec![1, 2, 3],
            checksum: 0xDEADBEEF,
        };

        let payload = StreamingServer::wal_entry_to_payload(&entry);
        assert_eq!(payload.lsn, 100);
        assert_eq!(payload.checksum, 0xDEADBEEF);
        matches!(payload.entry_type, TransportWalEntryType::Insert);
    }

    #[test]
    fn test_payload_to_wal_entry_conversion() {
        let payload = WalEntryPayload {
            lsn: 200,
            tx_id: Some(42),
            entry_type: TransportWalEntryType::Update,
            data: vec![4, 5, 6],
            timestamp_us: 12345678,
            checksum: 0xBEEFCAFE,
        };

        let entry = StreamingClient::payload_to_wal_entry(&payload);
        assert_eq!(entry.lsn, 200);
        assert_eq!(entry.checksum, 0xBEEFCAFE);
        matches!(entry.entry_type, WalEntryType::Update);
    }

    #[test]
    fn test_ack_type_satisfies() {
        // Applied satisfies all
        assert!(StreamingServer::ack_type_satisfies(&AckType::Applied, &AckType::Received));
        assert!(StreamingServer::ack_type_satisfies(&AckType::Applied, &AckType::Written));
        assert!(StreamingServer::ack_type_satisfies(&AckType::Applied, &AckType::Applied));

        // Written satisfies received and written
        assert!(StreamingServer::ack_type_satisfies(&AckType::Written, &AckType::Received));
        assert!(StreamingServer::ack_type_satisfies(&AckType::Written, &AckType::Written));
        assert!(!StreamingServer::ack_type_satisfies(&AckType::Written, &AckType::Applied));

        // Received only satisfies received
        assert!(StreamingServer::ack_type_satisfies(&AckType::Received, &AckType::Received));
        assert!(!StreamingServer::ack_type_satisfies(&AckType::Received, &AckType::Written));
    }

    #[test]
    fn test_streaming_client_creation() {
        let config = StreamingClientConfig {
            node_id: Uuid::new_v4(),
            primary_addr: "127.0.0.1:5433".parse().unwrap(),
            sync_mode: SyncModeConfig::SemiSync {
                min_acks: 1,
                timeout_ms: 5000,
            },
            connect_timeout: Duration::from_secs(10),
            reconnect_delay: Duration::from_secs(5),
            max_reconnect_attempts: 3,
        };

        let (client, _rx) = StreamingClient::new(config);
        assert_eq!(client.applied_lsn(), 0);
        assert_eq!(client.lag_bytes(), 0);
    }
}