oxirs-cluster 0.2.4

Raft-backed distributed dataset for high availability and horizontal scaling
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
//! # Node Status Tracking with State Machine
//!
//! Comprehensive node lifecycle management using a finite state machine.
//! Tracks node states, transitions, and provides integration with health
//! monitoring for proactive cluster management.

use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, VecDeque};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::RwLock;
use tracing::{info, warn};

use crate::raft::OxirsNodeId;

/// Node state in the cluster lifecycle
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum NodeState {
    /// Node is initializing
    Initializing,
    /// Node is joining the cluster
    Joining,
    /// Node is active and healthy
    Active,
    /// Node is active but degraded
    Degraded,
    /// Node is suspected to have issues
    Suspect,
    /// Node has failed
    Failed,
    /// Node is gracefully leaving
    Leaving,
    /// Node has left the cluster
    Left,
    /// Node is under maintenance
    Maintenance,
}

impl NodeState {
    /// Check if the state represents an operational node
    pub fn is_operational(&self) -> bool {
        matches!(self, NodeState::Active | NodeState::Degraded)
    }

    /// Check if the state represents a problematic node
    pub fn is_problematic(&self) -> bool {
        matches!(self, NodeState::Suspect | NodeState::Failed)
    }

    /// Check if the state is transitional
    pub fn is_transitional(&self) -> bool {
        matches!(
            self,
            NodeState::Initializing | NodeState::Joining | NodeState::Leaving
        )
    }
}

/// State transition event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateTransition {
    /// Node ID
    pub node_id: OxirsNodeId,
    /// Previous state
    pub from_state: NodeState,
    /// New state
    pub to_state: NodeState,
    /// Reason for transition
    pub reason: String,
    /// Timestamp
    pub timestamp: SystemTime,
    /// Transition duration (if applicable)
    pub duration: Option<Duration>,
}

/// Node status information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeStatus {
    /// Node ID
    pub node_id: OxirsNodeId,
    /// Current state
    pub current_state: NodeState,
    /// Previous state
    pub previous_state: Option<NodeState>,
    /// Time in current state
    pub time_in_state: Duration,
    /// Last state change
    pub last_state_change: SystemTime,
    /// Total state transitions
    pub transition_count: u64,
    /// Node uptime (time since Initializing)
    pub uptime: Duration,
    /// Node start time
    pub start_time: SystemTime,
    /// Additional metadata
    pub metadata: BTreeMap<String, String>,
}

/// State machine configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateMachineConfig {
    /// Maximum history size
    pub max_history_size: usize,
    /// Enable automatic state transitions
    pub enable_auto_transitions: bool,
    /// Suspect timeout (seconds)
    pub suspect_timeout_secs: u64,
    /// Failed timeout (seconds)
    pub failed_timeout_secs: u64,
    /// Enable state validation
    pub enable_validation: bool,
}

impl Default for StateMachineConfig {
    fn default() -> Self {
        Self {
            max_history_size: 1000,
            enable_auto_transitions: true,
            suspect_timeout_secs: 30,
            failed_timeout_secs: 60,
            enable_validation: true,
        }
    }
}

/// State machine statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StateMachineStats {
    /// Total nodes tracked
    pub total_nodes: usize,
    /// Nodes by state
    pub nodes_by_state: BTreeMap<String, usize>,
    /// Total transitions
    pub total_transitions: u64,
    /// Invalid transition attempts
    pub invalid_transitions: u64,
    /// Average time in each state (seconds)
    pub avg_time_in_state: BTreeMap<String, f64>,
}

/// Node status tracker with state machine
pub struct NodeStatusTracker {
    config: StateMachineConfig,
    /// Node statuses
    node_statuses: Arc<RwLock<BTreeMap<OxirsNodeId, NodeStatus>>>,
    /// Transition history
    transition_history: Arc<RwLock<VecDeque<StateTransition>>>,
    /// Statistics
    stats: Arc<RwLock<StateMachineStats>>,
    /// Valid state transitions
    valid_transitions: BTreeMap<NodeState, Vec<NodeState>>,
}

impl NodeStatusTracker {
    /// Create a new node status tracker
    pub fn new(config: StateMachineConfig) -> Self {
        // Define valid state transitions
        let mut valid_transitions = BTreeMap::new();

        valid_transitions.insert(
            NodeState::Initializing,
            vec![NodeState::Joining, NodeState::Failed],
        );

        valid_transitions.insert(
            NodeState::Joining,
            vec![NodeState::Active, NodeState::Failed],
        );

        valid_transitions.insert(
            NodeState::Active,
            vec![
                NodeState::Degraded,
                NodeState::Suspect,
                NodeState::Leaving,
                NodeState::Maintenance,
            ],
        );

        valid_transitions.insert(
            NodeState::Degraded,
            vec![
                NodeState::Active,
                NodeState::Suspect,
                NodeState::Failed,
                NodeState::Leaving,
            ],
        );

        valid_transitions.insert(
            NodeState::Suspect,
            vec![NodeState::Active, NodeState::Degraded, NodeState::Failed],
        );

        valid_transitions.insert(
            NodeState::Failed,
            vec![NodeState::Initializing, NodeState::Left],
        );

        valid_transitions.insert(NodeState::Leaving, vec![NodeState::Left]);

        valid_transitions.insert(NodeState::Left, vec![NodeState::Initializing]);

        valid_transitions.insert(
            NodeState::Maintenance,
            vec![NodeState::Active, NodeState::Degraded, NodeState::Failed],
        );

        Self {
            config,
            node_statuses: Arc::new(RwLock::new(BTreeMap::new())),
            transition_history: Arc::new(RwLock::new(VecDeque::new())),
            stats: Arc::new(RwLock::new(StateMachineStats::default())),
            valid_transitions,
        }
    }

    /// Register a new node
    pub async fn register_node(&self, node_id: OxirsNodeId) {
        let now = SystemTime::now();

        let status = NodeStatus {
            node_id,
            current_state: NodeState::Initializing,
            previous_state: None,
            time_in_state: Duration::from_secs(0),
            last_state_change: now,
            transition_count: 0,
            uptime: Duration::from_secs(0),
            start_time: now,
            metadata: BTreeMap::new(),
        };

        {
            let mut statuses = self.node_statuses.write().await;
            statuses.insert(node_id, status);
        } // Drop write lock before calling update_stats

        info!("Registered node {} in state Initializing", node_id);

        self.update_stats().await;
    }

    /// Unregister a node
    pub async fn unregister_node(&self, node_id: &OxirsNodeId) {
        {
            let mut statuses = self.node_statuses.write().await;
            statuses.remove(node_id);
        } // Drop write lock before calling update_stats

        info!("Unregistered node {}", node_id);

        self.update_stats().await;
    }

    /// Transition node to a new state
    pub async fn transition_state(
        &self,
        node_id: OxirsNodeId,
        new_state: NodeState,
        reason: String,
    ) -> Result<(), String> {
        let old_state = {
            let statuses = self.node_statuses.read().await;
            statuses
                .get(&node_id)
                .map(|s| s.current_state)
                .ok_or_else(|| format!("Node {} not found", node_id))?
        };

        // Validate transition
        if self.config.enable_validation && !self.is_valid_transition(old_state, new_state) {
            let mut stats = self.stats.write().await;
            stats.invalid_transitions += 1;

            return Err(format!(
                "Invalid transition from {:?} to {:?}",
                old_state, new_state
            ));
        }

        let mut statuses = self.node_statuses.write().await;

        let status = statuses
            .get_mut(&node_id)
            .ok_or_else(|| format!("Node {} not found", node_id))?;

        // Calculate time in previous state
        let now = SystemTime::now();
        let duration = now
            .duration_since(status.last_state_change)
            .unwrap_or(Duration::from_secs(0));

        // Update status
        status.previous_state = Some(old_state);
        status.current_state = new_state;
        status.time_in_state = Duration::from_secs(0);
        status.last_state_change = now;
        status.transition_count += 1;

        // Update uptime
        status.uptime = now
            .duration_since(status.start_time)
            .unwrap_or(Duration::from_secs(0));

        // Record transition
        let transition = StateTransition {
            node_id,
            from_state: old_state,
            to_state: new_state,
            reason: reason.clone(),
            timestamp: now,
            duration: Some(duration),
        };

        drop(statuses);

        // Store transition in history
        {
            let mut history = self.transition_history.write().await;
            history.push_back(transition.clone());

            if history.len() > self.config.max_history_size {
                history.pop_front();
            }
        } // Drop write lock before calling update_stats

        info!(
            "Node {} transitioned from {:?} to {:?}: {}",
            node_id, old_state, new_state, reason
        );

        self.update_stats().await;

        Ok(())
    }

    /// Check if a state transition is valid
    fn is_valid_transition(&self, from: NodeState, to: NodeState) -> bool {
        if from == to {
            return true;
        }

        self.valid_transitions
            .get(&from)
            .map(|valid| valid.contains(&to))
            .unwrap_or(false)
    }

    /// Get node status
    pub async fn get_node_status(&self, node_id: &OxirsNodeId) -> Option<NodeStatus> {
        let statuses = self.node_statuses.read().await;
        statuses.get(node_id).cloned()
    }

    /// Get all node statuses
    pub async fn get_all_statuses(&self) -> BTreeMap<OxirsNodeId, NodeStatus> {
        self.node_statuses.read().await.clone()
    }

    /// Get nodes in a specific state
    pub async fn get_nodes_in_state(&self, state: NodeState) -> Vec<OxirsNodeId> {
        let statuses = self.node_statuses.read().await;
        statuses
            .iter()
            .filter(|(_, status)| status.current_state == state)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Get transition history
    pub async fn get_transition_history(&self) -> Vec<StateTransition> {
        self.transition_history
            .read()
            .await
            .iter()
            .cloned()
            .collect()
    }

    /// Get transition history for a specific node
    pub async fn get_node_transition_history(&self, node_id: &OxirsNodeId) -> Vec<StateTransition> {
        let history = self.transition_history.read().await;
        history
            .iter()
            .filter(|t| &t.node_id == node_id)
            .cloned()
            .collect()
    }

    /// Update node metadata
    pub async fn update_metadata(
        &self,
        node_id: &OxirsNodeId,
        key: String,
        value: String,
    ) -> Result<(), String> {
        let mut statuses = self.node_statuses.write().await;

        let status = statuses
            .get_mut(node_id)
            .ok_or_else(|| format!("Node {} not found", node_id))?;

        status.metadata.insert(key, value);

        Ok(())
    }

    /// Get statistics
    pub async fn get_stats(&self) -> StateMachineStats {
        self.stats.read().await.clone()
    }

    /// Update statistics
    async fn update_stats(&self) {
        let mut stats = StateMachineStats {
            total_nodes: 0,
            nodes_by_state: BTreeMap::new(),
            total_transitions: 0,
            invalid_transitions: 0,
            avg_time_in_state: BTreeMap::new(),
        };

        // Collect data from statuses (scope read lock)
        {
            let statuses = self.node_statuses.read().await;
            stats.total_nodes = statuses.len();

            // Count nodes by state
            for status in statuses.values() {
                let state_name = format!("{:?}", status.current_state);
                *stats.nodes_by_state.entry(state_name).or_insert(0) += 1;
                stats.total_transitions += status.transition_count;
            }
        } // Drop statuses read lock

        // Calculate average time in each state (scope read lock)
        {
            let history = self.transition_history.read().await;
            let mut state_durations: BTreeMap<String, Vec<u64>> = BTreeMap::new();

            for transition in history.iter() {
                if let Some(duration) = transition.duration {
                    let state_name = format!("{:?}", transition.from_state);
                    state_durations
                        .entry(state_name)
                        .or_default()
                        .push(duration.as_secs());
                }
            }

            for (state, durations) in state_durations.iter() {
                if !durations.is_empty() {
                    let avg = durations.iter().sum::<u64>() as f64 / durations.len() as f64;
                    stats.avg_time_in_state.insert(state.clone(), avg);
                }
            }
        } // Drop history read lock

        // Preserve invalid transitions count and update (scope locks separately)
        {
            let old_stats = self.stats.read().await;
            stats.invalid_transitions = old_stats.invalid_transitions;
        } // Drop stats read lock before acquiring write lock

        *self.stats.write().await = stats;
    }

    /// Perform automatic state checks
    pub async fn perform_auto_checks(&self) {
        if !self.config.enable_auto_transitions {
            return;
        }

        let statuses = self.node_statuses.read().await;
        let now = SystemTime::now();
        let mut transitions = Vec::new();

        for (node_id, status) in statuses.iter() {
            let time_in_state = now
                .duration_since(status.last_state_change)
                .unwrap_or(Duration::from_secs(0));

            // Check for suspect timeout
            if status.current_state == NodeState::Suspect
                && time_in_state.as_secs() > self.config.suspect_timeout_secs
            {
                transitions.push((
                    *node_id,
                    NodeState::Failed,
                    "Suspect timeout exceeded".to_string(),
                ));
            }

            // Check for failed timeout
            if status.current_state == NodeState::Failed
                && time_in_state.as_secs() > self.config.failed_timeout_secs
            {
                transitions.push((
                    *node_id,
                    NodeState::Left,
                    "Failed timeout exceeded".to_string(),
                ));
            }
        }

        drop(statuses);

        // Apply transitions
        for (node_id, new_state, reason) in transitions {
            if let Err(e) = self.transition_state(node_id, new_state, reason).await {
                warn!("Auto-transition failed for node {}: {}", node_id, e);
            }
        }
    }

    /// Clear all data
    pub async fn clear(&self) {
        self.node_statuses.write().await.clear();
        self.transition_history.write().await.clear();
        *self.stats.write().await = StateMachineStats::default();
    }
}

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

    #[tokio::test]
    async fn test_node_status_tracker_creation() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        let stats = tracker.get_stats().await;
        assert_eq!(stats.total_nodes, 0);
    }

    #[tokio::test]
    async fn test_register_node() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;

        let status = tracker.get_node_status(&1).await;
        assert!(status.is_some());

        let status = status.unwrap();
        assert_eq!(status.current_state, NodeState::Initializing);
        assert_eq!(status.transition_count, 0);
    }

    #[tokio::test]
    async fn test_valid_transition() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;

        let result = tracker
            .transition_state(1, NodeState::Joining, "Starting join process".to_string())
            .await;

        assert!(result.is_ok());

        let status = tracker.get_node_status(&1).await.unwrap();
        assert_eq!(status.current_state, NodeState::Joining);
        assert_eq!(status.previous_state, Some(NodeState::Initializing));
        assert_eq!(status.transition_count, 1);
    }

    #[tokio::test]
    async fn test_invalid_transition() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;

        let result = tracker
            .transition_state(1, NodeState::Active, "Invalid transition".to_string())
            .await;

        assert!(result.is_err());

        let stats = tracker.get_stats().await;
        assert_eq!(stats.invalid_transitions, 1);
    }

    #[tokio::test]
    async fn test_state_machine_flow() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;

        // Initializing -> Joining
        tracker
            .transition_state(1, NodeState::Joining, "Joining cluster".to_string())
            .await
            .unwrap();

        // Joining -> Active
        tracker
            .transition_state(1, NodeState::Active, "Joined successfully".to_string())
            .await
            .unwrap();

        // Active -> Degraded
        tracker
            .transition_state(1, NodeState::Degraded, "Performance degraded".to_string())
            .await
            .unwrap();

        let status = tracker.get_node_status(&1).await.unwrap();
        assert_eq!(status.current_state, NodeState::Degraded);
        assert_eq!(status.transition_count, 3);
    }

    #[tokio::test]
    async fn test_get_nodes_in_state() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;
        tracker.register_node(2).await;
        tracker.register_node(3).await;

        tracker
            .transition_state(1, NodeState::Joining, "test".to_string())
            .await
            .unwrap();
        tracker
            .transition_state(2, NodeState::Joining, "test".to_string())
            .await
            .unwrap();

        let joining_nodes = tracker.get_nodes_in_state(NodeState::Joining).await;
        assert_eq!(joining_nodes.len(), 2);
        assert!(joining_nodes.contains(&1));
        assert!(joining_nodes.contains(&2));
    }

    #[tokio::test]
    async fn test_transition_history() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;

        tracker
            .transition_state(1, NodeState::Joining, "test1".to_string())
            .await
            .unwrap();
        tracker
            .transition_state(1, NodeState::Active, "test2".to_string())
            .await
            .unwrap();

        let history = tracker.get_node_transition_history(&1).await;
        assert_eq!(history.len(), 2);
        assert_eq!(history[0].from_state, NodeState::Initializing);
        assert_eq!(history[0].to_state, NodeState::Joining);
        assert_eq!(history[1].from_state, NodeState::Joining);
        assert_eq!(history[1].to_state, NodeState::Active);
    }

    #[tokio::test]
    async fn test_metadata() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;

        tracker
            .update_metadata(&1, "version".to_string(), "1.0.0".to_string())
            .await
            .unwrap();
        tracker
            .update_metadata(&1, "region".to_string(), "us-west".to_string())
            .await
            .unwrap();

        let status = tracker.get_node_status(&1).await.unwrap();
        assert_eq!(status.metadata.get("version"), Some(&"1.0.0".to_string()));
        assert_eq!(status.metadata.get("region"), Some(&"us-west".to_string()));
    }

    #[tokio::test]
    async fn test_stats() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;
        tracker.register_node(2).await;
        tracker.register_node(3).await;

        tracker
            .transition_state(1, NodeState::Joining, "test".to_string())
            .await
            .unwrap();
        tracker
            .transition_state(2, NodeState::Joining, "test".to_string())
            .await
            .unwrap();
        tracker
            .transition_state(1, NodeState::Active, "test".to_string())
            .await
            .unwrap();

        let stats = tracker.get_stats().await;
        assert_eq!(stats.total_nodes, 3);
        assert_eq!(stats.total_transitions, 3);
    }

    #[tokio::test]
    async fn test_unregister_node() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;
        assert!(tracker.get_node_status(&1).await.is_some());

        tracker.unregister_node(&1).await;
        assert!(tracker.get_node_status(&1).await.is_none());
    }

    #[tokio::test]
    async fn test_node_state_helpers() {
        assert!(NodeState::Active.is_operational());
        assert!(NodeState::Degraded.is_operational());
        assert!(!NodeState::Failed.is_operational());

        assert!(NodeState::Suspect.is_problematic());
        assert!(NodeState::Failed.is_problematic());
        assert!(!NodeState::Active.is_problematic());

        assert!(NodeState::Initializing.is_transitional());
        assert!(NodeState::Joining.is_transitional());
        assert!(!NodeState::Active.is_transitional());
    }

    #[tokio::test]
    async fn test_clear() {
        let config = StateMachineConfig::default();
        let tracker = NodeStatusTracker::new(config);

        tracker.register_node(1).await;
        tracker.register_node(2).await;

        tracker.clear().await;

        let stats = tracker.get_stats().await;
        assert_eq!(stats.total_nodes, 0);
    }
}