heliosdb-nano 3.30.0

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
//! WAL Applicator - Tier 1 Warm Standby
//!
//! Receives and applies WAL segments on standby nodes.
//! Maintains synchronization with primary and supports failover.
//!
//! The WAL Applicator deserializes replicated WAL entries and applies them
//! to the local storage engine, keeping the standby synchronized with the primary.

use super::config::PrimaryConfig;
use super::wal_replicator::{Lsn, WalEntry};
use super::ha_state::ha_state;
use super::{ReplicationError, Result};
use crate::storage::WalOperation;
use crate::storage::StorageEngine;
use std::sync::Arc;
use tokio::sync::{mpsc, RwLock, broadcast};
use tracing::{debug, info, error};

/// Application result for a WAL entry
#[derive(Debug, Clone)]
pub struct ApplyResult {
    /// Applied LSN
    pub lsn: Lsn,
    /// Whether the entry was applied (vs skipped as duplicate)
    pub applied: bool,
    /// Any warnings during application
    pub warnings: Vec<String>,
}

/// WAL Applicator state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApplicatorState {
    /// Not connected to primary
    Disconnected,
    /// Connecting to primary
    Connecting,
    /// Catching up with primary (initial sync)
    CatchingUp,
    /// Streaming in real-time
    Streaming,
    /// Paused (manual intervention)
    Paused,
    /// Error state
    Error,
}

/// WAL Applicator - receives and applies WAL on standby
pub struct WalApplicator {
    /// Primary configuration
    primary_config: PrimaryConfig,
    /// Current applied LSN
    applied_lsn: Arc<RwLock<Lsn>>,
    /// Applicator state
    state: Arc<RwLock<ApplicatorState>>,
    /// Queue sender (for external use)
    queue_tx: mpsc::Sender<WalEntry>,
    /// Queue receiver (moved to run loop)
    queue_rx: Arc<RwLock<Option<mpsc::Receiver<WalEntry>>>>,
    /// Shutdown signal sender
    shutdown_tx: broadcast::Sender<()>,
    /// Statistics
    entries_applied: Arc<RwLock<u64>>,
    entries_skipped: Arc<RwLock<u64>>,
    errors_count: Arc<RwLock<u64>>,
}

impl WalApplicator {
    /// Create a new WAL applicator
    pub fn new(primary_config: PrimaryConfig) -> Self {
        let (queue_tx, queue_rx) = mpsc::channel(10000);
        let (shutdown_tx, _) = broadcast::channel(1);

        Self {
            primary_config,
            applied_lsn: Arc::new(RwLock::new(0)),
            state: Arc::new(RwLock::new(ApplicatorState::Disconnected)),
            queue_tx,
            queue_rx: Arc::new(RwLock::new(Some(queue_rx))),
            shutdown_tx,
            entries_applied: Arc::new(RwLock::new(0)),
            entries_skipped: Arc::new(RwLock::new(0)),
            errors_count: Arc::new(RwLock::new(0)),
        }
    }

    /// Start the WAL applicator with a storage engine
    ///
    /// This spawns a background task that processes incoming WAL entries
    /// and applies them to the storage engine.
    pub async fn start_with_storage(&self, storage: Arc<StorageEngine>) -> Result<()> {
        // Take ownership of the receiver
        let queue_rx = {
            let mut rx_guard = self.queue_rx.write().await;
            rx_guard.take()
        };

        let Some(mut queue_rx) = queue_rx else {
            return Err(ReplicationError::WalStreaming("Applicator already started".to_string()));
        };

        *self.state.write().await = ApplicatorState::Streaming;
        info!("WAL Applicator started, ready to receive entries");

        let applied_lsn = self.applied_lsn.clone();
        let state = self.state.clone();
        let entries_applied = self.entries_applied.clone();
        let entries_skipped = self.entries_skipped.clone();
        let errors_count = self.errors_count.clone();
        let mut shutdown_rx = self.shutdown_tx.subscribe();

        // Spawn the applicator task
        tokio::spawn(async move {
            info!("WAL Applicator background task started");

            loop {
                tokio::select! {
                    // Check for shutdown signal - only break on Ok, ignore errors
                    result = shutdown_rx.recv() => {
                        match result {
                            Ok(()) => {
                                info!("WAL Applicator received shutdown signal");
                                break;
                            }
                            Err(e) => {
                                debug!("Shutdown channel error (ignoring): {:?}", e);
                                // Re-subscribe and continue - this can happen with Lagged errors
                                shutdown_rx = shutdown_rx.resubscribe();
                            }
                        }
                    }

                    // Process incoming entries
                    entry = queue_rx.recv() => {
                        let Some(entry) = entry else {
                            info!("WAL Applicator queue closed");
                            break;
                        };

                        info!("WAL Applicator: Received entry LSN={} from queue", entry.lsn);

                        // Check if we're paused
                        if *state.read().await == ApplicatorState::Paused {
                            debug!("WAL Applicator paused, skipping entry {}", entry.lsn);
                            continue;
                        }

                        // Apply the entry
                        info!("WAL Applicator: Applying entry LSN={}", entry.lsn);
                        match Self::apply_entry_to_storage(&storage, &entry, &applied_lsn).await {
                            Ok(result) => {
                                if result.applied {
                                    *entries_applied.write().await += 1;
                                    info!("WAL Applicator: Applied entry LSN={} successfully", entry.lsn);

                                    // Update HA state
                                    ha_state().update_primary(|p| {
                                        p.local_lsn = entry.lsn;
                                        p.lag_bytes = p.primary_lsn.saturating_sub(entry.lsn);
                                    });
                                } else {
                                    *entries_skipped.write().await += 1;
                                    debug!("Skipped WAL entry LSN {} (already applied)", entry.lsn);
                                }
                            }
                            Err(e) => {
                                *errors_count.write().await += 1;
                                error!("Failed to apply WAL entry {}: {}", entry.lsn, e);
                            }
                        }
                    }
                }
            }

            *state.write().await = ApplicatorState::Disconnected;
            info!("WAL Applicator background task stopped");
        });

        Ok(())
    }

    /// Apply a single WAL entry to storage
    async fn apply_entry_to_storage(
        storage: &Arc<StorageEngine>,
        entry: &WalEntry,
        applied_lsn: &Arc<RwLock<Lsn>>,
    ) -> Result<ApplyResult> {
        let current_lsn = *applied_lsn.read().await;

        // Skip already-applied entries (idempotency)
        if entry.lsn <= current_lsn {
            return Ok(ApplyResult {
                lsn: entry.lsn,
                applied: false,
                warnings: vec!["Entry already applied".to_string()],
            });
        }

        // Deserialize the operation from entry data
        let operation: WalOperation = bincode::deserialize(&entry.data)
            .map_err(|e| ReplicationError::WalStreaming(
                format!("Failed to deserialize WAL operation: {}", e)
            ))?;

        // Apply the operation to storage
        // The storage engine's apply_wal_operation handles all operation types
        storage.apply_replicated_operation(operation)
            .map_err(|e| ReplicationError::Storage(
                format!("Failed to apply WAL operation: {}", e)
            ))?;

        // Update applied LSN
        *applied_lsn.write().await = entry.lsn;

        // Update HA state LSN
        ha_state().set_lsn(entry.lsn);

        Ok(ApplyResult {
            lsn: entry.lsn,
            applied: true,
            warnings: vec![],
        })
    }

    /// Start the WAL applicator (legacy method - use start_with_storage instead)
    pub async fn start(&self) -> Result<()> {
        *self.state.write().await = ApplicatorState::Connecting;
        info!("WAL Applicator started (no storage engine - entries will be queued only)");
        Ok(())
    }

    /// Stop the WAL applicator
    pub async fn stop(&self) -> Result<()> {
        let _ = self.shutdown_tx.send(());
        *self.state.write().await = ApplicatorState::Disconnected;
        info!("WAL Applicator stopped");
        Ok(())
    }

    /// Pause WAL application
    pub async fn pause(&self) -> Result<()> {
        *self.state.write().await = ApplicatorState::Paused;
        info!("WAL Applicator paused");
        Ok(())
    }

    /// Resume WAL application
    pub async fn resume(&self) -> Result<()> {
        *self.state.write().await = ApplicatorState::Streaming;
        info!("WAL Applicator resumed");
        Ok(())
    }

    /// Apply a single WAL entry (for direct application without queue)
    pub async fn apply(&self, entry: WalEntry) -> Result<ApplyResult> {
        let current_lsn = *self.applied_lsn.read().await;

        // Skip already-applied entries (idempotency)
        if entry.lsn <= current_lsn {
            return Ok(ApplyResult {
                lsn: entry.lsn,
                applied: false,
                warnings: vec!["Entry already applied".to_string()],
            });
        }

        // Without a storage engine, we just update the LSN
        // This is used for testing or when entries are applied externally
        *self.applied_lsn.write().await = entry.lsn;

        Ok(ApplyResult {
            lsn: entry.lsn,
            applied: true,
            warnings: vec!["No storage engine - entry not persisted".to_string()],
        })
    }

    /// Get current applied LSN
    pub async fn applied_lsn(&self) -> Lsn {
        *self.applied_lsn.read().await
    }

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

    /// Get replication lag (current_primary_lsn - applied_lsn)
    pub async fn lag(&self, primary_lsn: Lsn) -> u64 {
        let applied = *self.applied_lsn.read().await;
        primary_lsn.saturating_sub(applied)
    }

    /// Queue an entry for application
    pub async fn queue_entry(&self, entry: WalEntry) -> Result<()> {
        self.queue_tx
            .send(entry)
            .await
            .map_err(|e| ReplicationError::WalStreaming(e.to_string()))
    }

    /// Get the queue sender for direct entry submission
    pub fn get_queue_sender(&self) -> mpsc::Sender<WalEntry> {
        self.queue_tx.clone()
    }

    /// Get statistics
    pub async fn stats(&self) -> (u64, u64, u64) {
        (
            *self.entries_applied.read().await,
            *self.entries_skipped.read().await,
            *self.errors_count.read().await,
        )
    }

    /// Promote this standby to primary
    ///
    /// Called during failover to make this node the new primary.
    pub async fn promote(&self) -> Result<()> {
        // Stop accepting WAL from old primary
        let _ = self.shutdown_tx.send(());

        // Update state
        *self.state.write().await = ApplicatorState::Disconnected;

        let final_lsn = self.applied_lsn().await;
        info!("Standby promoted to primary at LSN {}", final_lsn);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::super::wal_replicator::WalEntryType;
    use std::time::Duration;

    #[tokio::test]
    async fn test_wal_applicator_creation() {
        let config = PrimaryConfig {
            host: "localhost".to_string(),
            port: 5432,
            connect_timeout: Duration::from_secs(10),
            use_tls: false,
        };
        let applicator = WalApplicator::new(config);
        assert_eq!(applicator.applied_lsn().await, 0);
        assert_eq!(applicator.state().await, ApplicatorState::Disconnected);
    }

    #[tokio::test]
    async fn test_apply_entry() {
        let config = PrimaryConfig {
            host: "localhost".to_string(),
            port: 5432,
            connect_timeout: Duration::from_secs(10),
            use_tls: false,
        };
        let applicator = WalApplicator::new(config);

        let entry = WalEntry {
            lsn: 1,
            tx_id: None,
            entry_type: WalEntryType::Insert,
            data: vec![1, 2, 3],
            checksum: 0,
        };

        let result = applicator.apply(entry).await.expect("apply failed");
        assert!(result.applied);
        assert_eq!(result.lsn, 1);
        assert_eq!(applicator.applied_lsn().await, 1);
    }

    #[tokio::test]
    async fn test_idempotent_apply() {
        let config = PrimaryConfig {
            host: "localhost".to_string(),
            port: 5432,
            connect_timeout: Duration::from_secs(10),
            use_tls: false,
        };
        let applicator = WalApplicator::new(config);

        let entry = WalEntry {
            lsn: 1,
            tx_id: None,
            entry_type: WalEntryType::Insert,
            data: vec![1, 2, 3],
            checksum: 0,
        };

        // Apply once
        applicator.apply(entry.clone()).await.expect("apply failed");

        // Apply again - should be idempotent
        let result = applicator.apply(entry).await.expect("apply failed");
        assert!(!result.applied);
    }

    #[tokio::test]
    async fn test_queue_entry() {
        let config = PrimaryConfig {
            host: "localhost".to_string(),
            port: 5432,
            connect_timeout: Duration::from_secs(10),
            use_tls: false,
        };
        let applicator = WalApplicator::new(config);

        let entry = WalEntry {
            lsn: 1,
            tx_id: None,
            entry_type: WalEntryType::Insert,
            data: vec![1, 2, 3],
            checksum: 0,
        };

        // Queue should succeed
        applicator.queue_entry(entry).await.expect("queue failed");
    }
}