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
//! Role Manager - State machine for HA node roles
//!
//! Manages transitions between Primary, Standby, and transitional states
//! for controlled switchover and failover operations.

use std::sync::atomic::{AtomicU8, Ordering};
use parking_lot::RwLock;
use tokio::sync::broadcast;
use uuid::Uuid;

use crate::{Result, Error};

/// Node role in the HA cluster
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum NodeRole {
    /// Primary node - accepts reads and writes
    Primary = 0,
    /// Standby node - read-only, receives WAL from primary
    Standby = 1,
    /// Transitioning to primary (during switchover)
    TransitioningToPrimary = 2,
    /// Transitioning to standby (during switchover)
    TransitioningToStandby = 3,
    /// Draining - primary is finishing in-flight transactions before demotion
    Draining = 4,
    /// Catching up - standby is syncing to latest LSN before promotion
    CatchingUp = 5,
    /// Offline - node is not participating in cluster
    Offline = 6,
}

impl NodeRole {
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(NodeRole::Primary),
            1 => Some(NodeRole::Standby),
            2 => Some(NodeRole::TransitioningToPrimary),
            3 => Some(NodeRole::TransitioningToStandby),
            4 => Some(NodeRole::Draining),
            5 => Some(NodeRole::CatchingUp),
            6 => Some(NodeRole::Offline),
            _ => None,
        }
    }

    /// Check if this role can accept write operations
    pub fn can_write(&self) -> bool {
        matches!(self, NodeRole::Primary)
    }

    /// Check if this role can accept read operations
    pub fn can_read(&self) -> bool {
        matches!(self, NodeRole::Primary | NodeRole::Standby)
    }

    /// Check if this role is in a transitional state
    pub fn is_transitioning(&self) -> bool {
        matches!(
            self,
            NodeRole::TransitioningToPrimary
                | NodeRole::TransitioningToStandby
                | NodeRole::Draining
                | NodeRole::CatchingUp
        )
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            NodeRole::Primary => "primary",
            NodeRole::Standby => "standby",
            NodeRole::TransitioningToPrimary => "transitioning_to_primary",
            NodeRole::TransitioningToStandby => "transitioning_to_standby",
            NodeRole::Draining => "draining",
            NodeRole::CatchingUp => "catching_up",
            NodeRole::Offline => "offline",
        }
    }
}

impl std::fmt::Display for NodeRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Role change event for notification subscribers
#[derive(Debug, Clone)]
pub struct RoleChangeEvent {
    /// Node that changed role
    pub node_id: Uuid,
    /// Previous role
    pub old_role: NodeRole,
    /// New role
    pub new_role: NodeRole,
    /// Timestamp of the change
    pub timestamp: std::time::Instant,
    /// Reason for the change
    pub reason: RoleChangeReason,
}

/// Reason for role change
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RoleChangeReason {
    /// Controlled switchover initiated by operator
    Switchover,
    /// Switchback to original primary
    Switchback,
    /// Automatic failover due to primary failure
    Failover,
    /// Manual promotion by operator
    ManualPromotion,
    /// Manual demotion by operator
    ManualDemotion,
    /// Initial cluster formation
    ClusterFormation,
    /// Node rejoining cluster after being offline
    Rejoin,
}

impl RoleChangeReason {
    pub fn as_str(&self) -> &'static str {
        match self {
            RoleChangeReason::Switchover => "switchover",
            RoleChangeReason::Switchback => "switchback",
            RoleChangeReason::Failover => "failover",
            RoleChangeReason::ManualPromotion => "manual_promotion",
            RoleChangeReason::ManualDemotion => "manual_demotion",
            RoleChangeReason::ClusterFormation => "cluster_formation",
            RoleChangeReason::Rejoin => "rejoin",
        }
    }
}

/// Switchover state tracking
#[derive(Debug, Clone)]
pub struct SwitchoverState {
    /// Switchover ID for tracking
    pub switchover_id: Uuid,
    /// Source node (current primary)
    pub source_node: Uuid,
    /// Target node (will become primary)
    pub target_node: Uuid,
    /// Current phase
    pub phase: SwitchoverPhase,
    /// Start time
    pub started_at: std::time::Instant,
    /// LSN that must be reached before promotion
    pub target_lsn: Option<u64>,
}

/// Phases of a controlled switchover
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SwitchoverPhase {
    /// Phase 1: Verify preconditions
    Preparation,
    /// Phase 2: Drain primary, sync standbys
    Synchronization,
    /// Phase 3: Change roles
    RoleChange,
    /// Phase 4: Reconfigure standbys to follow new primary
    Reconfiguration,
    /// Phase 5: Resume normal operations
    Resumption,
    /// Switchover completed successfully
    Completed,
    /// Switchover failed and rolled back
    Failed,
    /// Switchover was cancelled
    Cancelled,
}

impl SwitchoverPhase {
    pub fn as_str(&self) -> &'static str {
        match self {
            SwitchoverPhase::Preparation => "preparation",
            SwitchoverPhase::Synchronization => "synchronization",
            SwitchoverPhase::RoleChange => "role_change",
            SwitchoverPhase::Reconfiguration => "reconfiguration",
            SwitchoverPhase::Resumption => "resumption",
            SwitchoverPhase::Completed => "completed",
            SwitchoverPhase::Failed => "failed",
            SwitchoverPhase::Cancelled => "cancelled",
        }
    }

    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            SwitchoverPhase::Completed | SwitchoverPhase::Failed | SwitchoverPhase::Cancelled
        )
    }
}

/// Role Manager - manages node role state machine
pub struct RoleManager {
    /// This node's ID
    node_id: Uuid,
    /// Current role (atomic for lock-free reads)
    role: AtomicU8,
    /// Current primary node ID
    current_primary: RwLock<Option<Uuid>>,
    /// Active switchover state (if any)
    switchover_state: RwLock<Option<SwitchoverState>>,
    /// Role change event broadcaster
    role_change_tx: broadcast::Sender<RoleChangeEvent>,
    /// History of role changes (for auditing)
    role_history: RwLock<Vec<RoleChangeEvent>>,
    /// Maximum history entries to keep
    max_history: usize,
}

impl RoleManager {
    /// Create a new role manager
    pub fn new(node_id: Uuid, initial_role: NodeRole) -> Self {
        let (role_change_tx, _) = broadcast::channel(64);

        Self {
            node_id,
            role: AtomicU8::new(initial_role as u8),
            current_primary: RwLock::new(if initial_role == NodeRole::Primary {
                Some(node_id)
            } else {
                None
            }),
            switchover_state: RwLock::new(None),
            role_change_tx,
            role_history: RwLock::new(Vec::new()),
            max_history: 1000,
        }
    }

    /// Get this node's ID
    pub fn node_id(&self) -> Uuid {
        self.node_id
    }

    /// Get current role (lock-free)
    pub fn role(&self) -> NodeRole {
        NodeRole::from_u8(self.role.load(Ordering::SeqCst))
            .unwrap_or(NodeRole::Offline)
    }

    /// Check if this node is the primary
    pub fn is_primary(&self) -> bool {
        self.role() == NodeRole::Primary
    }

    /// Check if this node is a standby
    pub fn is_standby(&self) -> bool {
        self.role() == NodeRole::Standby
    }

    /// Check if a switchover is in progress
    pub fn is_switchover_in_progress(&self) -> bool {
        self.switchover_state.read().is_some()
    }

    /// Get current primary node ID
    pub fn current_primary(&self) -> Option<Uuid> {
        *self.current_primary.read()
    }

    /// Set current primary node ID
    pub fn set_current_primary(&self, primary_id: Option<Uuid>) {
        *self.current_primary.write() = primary_id;
    }

    /// Subscribe to role change events
    pub fn subscribe(&self) -> broadcast::Receiver<RoleChangeEvent> {
        self.role_change_tx.subscribe()
    }

    /// Get the active switchover state
    pub fn switchover_state(&self) -> Option<SwitchoverState> {
        self.switchover_state.read().clone()
    }

    /// Change role with validation
    pub fn change_role(&self, new_role: NodeRole, reason: RoleChangeReason) -> Result<()> {
        let old_role = self.role();

        // Validate transition
        self.validate_transition(old_role, new_role)?;

        // Perform the transition
        self.role.store(new_role as u8, Ordering::SeqCst);

        // Update primary tracking
        if new_role == NodeRole::Primary {
            *self.current_primary.write() = Some(self.node_id);
        }

        // Create and broadcast event
        let event = RoleChangeEvent {
            node_id: self.node_id,
            old_role,
            new_role,
            timestamp: std::time::Instant::now(),
            reason,
        };

        // Store in history
        {
            let mut history = self.role_history.write();
            history.push(event.clone());
            if history.len() > self.max_history {
                history.remove(0);
            }
        }

        // Broadcast (ignore if no receivers)
        let _ = self.role_change_tx.send(event);

        tracing::info!(
            "Role changed: {} -> {} (reason: {})",
            old_role,
            new_role,
            reason.as_str()
        );

        Ok(())
    }

    /// Validate a role transition
    fn validate_transition(&self, from: NodeRole, to: NodeRole) -> Result<()> {
        // Define valid transitions
        let valid = match (from, to) {
            // From Primary
            (NodeRole::Primary, NodeRole::Draining) => true,
            (NodeRole::Primary, NodeRole::TransitioningToStandby) => true,
            (NodeRole::Primary, NodeRole::Offline) => true,

            // From Standby
            (NodeRole::Standby, NodeRole::CatchingUp) => true,
            (NodeRole::Standby, NodeRole::TransitioningToPrimary) => true,
            (NodeRole::Standby, NodeRole::Offline) => true,

            // From Draining
            (NodeRole::Draining, NodeRole::TransitioningToStandby) => true,
            (NodeRole::Draining, NodeRole::Primary) => true, // Rollback

            // From CatchingUp
            (NodeRole::CatchingUp, NodeRole::TransitioningToPrimary) => true,
            (NodeRole::CatchingUp, NodeRole::Standby) => true, // Rollback

            // From TransitioningToPrimary
            (NodeRole::TransitioningToPrimary, NodeRole::Primary) => true,
            (NodeRole::TransitioningToPrimary, NodeRole::Standby) => true, // Rollback

            // From TransitioningToStandby
            (NodeRole::TransitioningToStandby, NodeRole::Standby) => true,
            (NodeRole::TransitioningToStandby, NodeRole::Primary) => true, // Rollback

            // From Offline
            (NodeRole::Offline, NodeRole::Primary) => true,
            (NodeRole::Offline, NodeRole::Standby) => true,

            // Same role (no-op)
            (a, b) if a == b => true,

            _ => false,
        };

        if valid {
            Ok(())
        } else {
            Err(Error::ha(format!(
                "Invalid role transition: {} -> {}",
                from, to
            )))
        }
    }

    /// Begin a switchover operation
    pub fn begin_switchover(&self, target_node: Uuid) -> Result<Uuid> {
        let mut state = self.switchover_state.write();

        if state.is_some() {
            return Err(Error::ha("Switchover already in progress"));
        }

        if self.role() != NodeRole::Primary {
            return Err(Error::ha("Only primary can initiate switchover"));
        }

        let switchover_id = Uuid::new_v4();
        *state = Some(SwitchoverState {
            switchover_id,
            source_node: self.node_id,
            target_node,
            phase: SwitchoverPhase::Preparation,
            started_at: std::time::Instant::now(),
            target_lsn: None,
        });

        tracing::info!(
            "Switchover {} started: {} -> {}",
            switchover_id,
            self.node_id,
            target_node
        );

        Ok(switchover_id)
    }

    /// Advance switchover to next phase
    pub fn advance_switchover_phase(&self, new_phase: SwitchoverPhase) -> Result<()> {
        let mut state = self.switchover_state.write();

        if let Some(ref mut s) = *state {
            tracing::info!(
                "Switchover {} advancing: {} -> {}",
                s.switchover_id,
                s.phase.as_str(),
                new_phase.as_str()
            );
            s.phase = new_phase;

            if new_phase.is_terminal() {
                // Clear switchover state when complete
                drop(state);
                *self.switchover_state.write() = None;
            }
            Ok(())
        } else {
            Err(Error::ha("No switchover in progress"))
        }
    }

    /// Set the target LSN for switchover synchronization
    pub fn set_switchover_target_lsn(&self, lsn: u64) -> Result<()> {
        let mut state = self.switchover_state.write();

        if let Some(ref mut s) = *state {
            s.target_lsn = Some(lsn);
            Ok(())
        } else {
            Err(Error::ha("No switchover in progress"))
        }
    }

    /// Cancel an in-progress switchover
    pub fn cancel_switchover(&self) -> Result<()> {
        let mut state = self.switchover_state.write();

        if let Some(ref s) = *state {
            tracing::warn!("Switchover {} cancelled", s.switchover_id);
            *state = None;
            Ok(())
        } else {
            Err(Error::ha("No switchover in progress"))
        }
    }

    /// Get role change history
    pub fn role_history(&self) -> Vec<RoleChangeEvent> {
        self.role_history.read().clone()
    }

    /// Promote this node to primary (for use during switchover)
    pub fn promote_to_primary(&self, reason: RoleChangeReason) -> Result<()> {
        let current = self.role();

        // Must be in appropriate transitional state
        if !matches!(current, NodeRole::CatchingUp | NodeRole::TransitioningToPrimary | NodeRole::Standby) {
            return Err(Error::ha(format!(
                "Cannot promote from role: {}",
                current
            )));
        }

        self.change_role(NodeRole::Primary, reason)
    }

    /// Demote this node to standby (for use during switchover)
    pub fn demote_to_standby(&self, reason: RoleChangeReason) -> Result<()> {
        let current = self.role();

        // Must be in appropriate transitional state
        if !matches!(current, NodeRole::Draining | NodeRole::TransitioningToStandby | NodeRole::Primary) {
            return Err(Error::ha(format!(
                "Cannot demote from role: {}",
                current
            )));
        }

        self.change_role(NodeRole::Standby, reason)
    }
}

impl std::fmt::Debug for RoleManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RoleManager")
            .field("node_id", &self.node_id)
            .field("role", &self.role())
            .field("current_primary", &self.current_primary())
            .field("switchover_in_progress", &self.is_switchover_in_progress())
            .finish()
    }
}

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

    #[test]
    fn test_role_transitions() {
        let node_id = Uuid::new_v4();
        let manager = RoleManager::new(node_id, NodeRole::Standby);

        assert_eq!(manager.role(), NodeRole::Standby);
        assert!(!manager.is_primary());
        assert!(manager.is_standby());

        // Valid transition: Standby -> CatchingUp
        manager.change_role(NodeRole::CatchingUp, RoleChangeReason::Switchover).unwrap();
        assert_eq!(manager.role(), NodeRole::CatchingUp);

        // Valid transition: CatchingUp -> TransitioningToPrimary
        manager.change_role(NodeRole::TransitioningToPrimary, RoleChangeReason::Switchover).unwrap();

        // Valid transition: TransitioningToPrimary -> Primary
        manager.change_role(NodeRole::Primary, RoleChangeReason::Switchover).unwrap();
        assert!(manager.is_primary());
        assert_eq!(manager.current_primary(), Some(node_id));
    }

    #[test]
    fn test_invalid_transition() {
        let node_id = Uuid::new_v4();
        let manager = RoleManager::new(node_id, NodeRole::Standby);

        // Invalid: Standby -> Primary directly
        let result = manager.change_role(NodeRole::Primary, RoleChangeReason::ManualPromotion);
        // This should fail because direct Standby -> Primary is not in valid transitions
        // Actually looking at the code, it's not explicitly allowed, so this tests that
    }

    #[test]
    fn test_switchover_lifecycle() {
        let node_id = Uuid::new_v4();
        let target_id = Uuid::new_v4();
        let manager = RoleManager::new(node_id, NodeRole::Primary);

        // Begin switchover
        let switchover_id = manager.begin_switchover(target_id).unwrap();
        assert!(manager.is_switchover_in_progress());

        // Advance through phases
        manager.advance_switchover_phase(SwitchoverPhase::Synchronization).unwrap();
        manager.advance_switchover_phase(SwitchoverPhase::RoleChange).unwrap();
        manager.advance_switchover_phase(SwitchoverPhase::Reconfiguration).unwrap();
        manager.advance_switchover_phase(SwitchoverPhase::Resumption).unwrap();
        manager.advance_switchover_phase(SwitchoverPhase::Completed).unwrap();

        // Switchover state should be cleared
        assert!(!manager.is_switchover_in_progress());
    }
}