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
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
//! # Enhanced Cluster Health Monitoring
//!
//! Comprehensive health monitoring with predictive alerts for proactive
//! cluster management. Tracks node health, resource utilization, and
//! predicts potential failures before they occur.

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

use crate::raft::OxirsNodeId;

/// Health monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthMonitoringConfig {
    /// Health check interval (seconds)
    pub check_interval_secs: u64,
    /// Alert threshold for CPU usage (0.0-1.0)
    pub cpu_alert_threshold: f64,
    /// Alert threshold for memory usage (0.0-1.0)
    pub memory_alert_threshold: f64,
    /// Alert threshold for disk usage (0.0-1.0)
    pub disk_alert_threshold: f64,
    /// Number of historical samples to keep
    pub history_size: usize,
    /// Prediction window size (samples)
    pub prediction_window: usize,
    /// Alert cooldown period (seconds)
    pub alert_cooldown_secs: u64,
}

impl Default for HealthMonitoringConfig {
    fn default() -> Self {
        Self {
            check_interval_secs: 30,
            cpu_alert_threshold: 0.85,
            memory_alert_threshold: 0.90,
            disk_alert_threshold: 0.85,
            history_size: 1000,
            prediction_window: 10,
            alert_cooldown_secs: 300,
        }
    }
}

/// Node health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HealthStatus {
    /// Node is healthy
    Healthy,
    /// Node is degraded but operational
    Degraded,
    /// Node is critical and may fail
    Critical,
    /// Node is unresponsive
    Unresponsive,
}

/// Resource utilization metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceMetrics {
    /// CPU usage (0.0-1.0)
    pub cpu_usage: f64,
    /// Memory usage (0.0-1.0)
    pub memory_usage: f64,
    /// Disk usage (0.0-1.0)
    pub disk_usage: f64,
    /// Network bandwidth usage (bytes/sec)
    pub network_usage: u64,
    /// Active connections count
    pub active_connections: usize,
    /// Timestamp of measurement
    pub timestamp: SystemTime,
}

impl Default for ResourceMetrics {
    fn default() -> Self {
        Self {
            cpu_usage: 0.0,
            memory_usage: 0.0,
            disk_usage: 0.0,
            network_usage: 0,
            active_connections: 0,
            timestamp: SystemTime::now(),
        }
    }
}

/// Node health information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeHealth {
    /// Node ID
    pub node_id: OxirsNodeId,
    /// Current health status
    pub status: HealthStatus,
    /// Current resource metrics
    pub current_metrics: ResourceMetrics,
    /// Historical metrics
    pub metrics_history: VecDeque<ResourceMetrics>,
    /// Last health check timestamp
    pub last_check: SystemTime,
    /// Number of consecutive failures
    pub consecutive_failures: u32,
}

/// Health alert types
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum AlertType {
    /// High CPU usage
    HighCpu,
    /// High memory usage
    HighMemory,
    /// High disk usage
    HighDisk,
    /// Node unresponsive
    NodeUnresponsive,
    /// Predicted resource exhaustion
    PredictedFailure,
    /// Degraded performance
    DegradedPerformance,
}

/// Health alert
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthAlert {
    /// Alert type
    pub alert_type: AlertType,
    /// Affected node
    pub node_id: OxirsNodeId,
    /// Alert severity (0.0-1.0)
    pub severity: f64,
    /// Alert message
    pub message: String,
    /// Alert timestamp
    pub timestamp: SystemTime,
    /// Predicted time to failure (if applicable)
    pub time_to_failure: Option<Duration>,
}

/// Health monitoring statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HealthMonitoringStats {
    /// Total health checks performed
    pub total_health_checks: u64,
    /// Total alerts generated
    pub total_alerts: u64,
    /// Alerts by type
    pub alerts_by_type: BTreeMap<String, u64>,
    /// Average response time (ms)
    pub avg_response_time_ms: f64,
    /// Nodes currently degraded
    pub degraded_nodes: usize,
    /// Nodes currently critical
    pub critical_nodes: usize,
}

/// Enhanced cluster health monitoring
pub struct HealthMonitoring {
    config: HealthMonitoringConfig,
    node_health: Arc<RwLock<BTreeMap<OxirsNodeId, NodeHealth>>>,
    active_alerts: Arc<RwLock<Vec<HealthAlert>>>,
    alert_history: Arc<RwLock<VecDeque<HealthAlert>>>,
    last_alert_time: Arc<RwLock<BTreeMap<(OxirsNodeId, AlertType), SystemTime>>>,
    stats: Arc<RwLock<HealthMonitoringStats>>,
}

impl HealthMonitoring {
    /// Create a new health monitoring system
    pub fn new(config: HealthMonitoringConfig) -> Self {
        Self {
            config,
            node_health: Arc::new(RwLock::new(BTreeMap::new())),
            active_alerts: Arc::new(RwLock::new(Vec::new())),
            alert_history: Arc::new(RwLock::new(VecDeque::new())),
            last_alert_time: Arc::new(RwLock::new(BTreeMap::new())),
            stats: Arc::new(RwLock::new(HealthMonitoringStats::default())),
        }
    }

    /// Register a node for health monitoring
    pub async fn register_node(&self, node_id: OxirsNodeId) {
        let mut node_health = self.node_health.write().await;
        node_health.insert(
            node_id.clone(),
            NodeHealth {
                node_id,
                status: HealthStatus::Healthy,
                current_metrics: ResourceMetrics::default(),
                metrics_history: VecDeque::with_capacity(self.config.history_size),
                last_check: SystemTime::now(),
                consecutive_failures: 0,
            },
        );
    }

    /// Unregister a node from health monitoring
    pub async fn unregister_node(&self, node_id: &OxirsNodeId) {
        let mut node_health = self.node_health.write().await;
        node_health.remove(node_id);
    }

    /// Update node health metrics
    pub async fn update_metrics(&self, node_id: &OxirsNodeId, metrics: ResourceMetrics) {
        let mut node_health = self.node_health.write().await;

        if let Some(health) = node_health.get_mut(node_id) {
            // Update current metrics
            health.current_metrics = metrics.clone();
            health.last_check = SystemTime::now();
            health.consecutive_failures = 0;

            // Add to history
            health.metrics_history.push_back(metrics);
            if health.metrics_history.len() > self.config.history_size {
                health.metrics_history.pop_front();
            }

            // Update health status
            self.update_health_status(health).await;
        }

        let mut stats = self.stats.write().await;
        stats.total_health_checks += 1;
    }

    /// Update health status based on metrics
    async fn update_health_status(&self, health: &mut NodeHealth) {
        let metrics = &health.current_metrics;

        let new_status = if metrics.cpu_usage > self.config.cpu_alert_threshold
            || metrics.memory_usage > self.config.memory_alert_threshold
            || metrics.disk_usage > self.config.disk_alert_threshold
        {
            HealthStatus::Critical
        } else if metrics.cpu_usage > self.config.cpu_alert_threshold * 0.8
            || metrics.memory_usage > self.config.memory_alert_threshold * 0.8
            || metrics.disk_usage > self.config.disk_alert_threshold * 0.8
        {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        };

        if new_status != health.status {
            debug!(
                "Node {:?} health status changed from {:?} to {:?}",
                health.node_id, health.status, new_status
            );
            health.status = new_status;
        }
    }

    /// Record a health check failure
    pub async fn record_failure(&self, node_id: &OxirsNodeId) {
        let mut node_health = self.node_health.write().await;

        let should_alert = if let Some(health) = node_health.get_mut(node_id) {
            health.consecutive_failures += 1;
            health.last_check = SystemTime::now();

            if health.consecutive_failures >= 3 {
                health.status = HealthStatus::Unresponsive;
                Some(health.consecutive_failures)
            } else {
                None
            }
        } else {
            None
        };

        drop(node_health);

        if let Some(failures) = should_alert {
            self.generate_alert(
                *node_id,
                AlertType::NodeUnresponsive,
                1.0,
                format!("Node has failed {} consecutive health checks", failures),
                None,
            )
            .await;
        }
    }

    /// Check all nodes for potential issues
    pub async fn check_health(&self) -> Vec<HealthAlert> {
        let node_health = self.node_health.read().await;
        let mut alerts = Vec::new();

        for (node_id, health) in node_health.iter() {
            // Check CPU usage
            if health.current_metrics.cpu_usage > self.config.cpu_alert_threshold {
                if self
                    .should_generate_alert(node_id, &AlertType::HighCpu)
                    .await
                {
                    alerts.push(HealthAlert {
                        alert_type: AlertType::HighCpu,
                        node_id: node_id.clone(),
                        severity: health.current_metrics.cpu_usage,
                        message: format!(
                            "High CPU usage: {:.1}%",
                            health.current_metrics.cpu_usage * 100.0
                        ),
                        timestamp: SystemTime::now(),
                        time_to_failure: None,
                    });
                }
            }

            // Check memory usage
            if health.current_metrics.memory_usage > self.config.memory_alert_threshold {
                if self
                    .should_generate_alert(node_id, &AlertType::HighMemory)
                    .await
                {
                    alerts.push(HealthAlert {
                        alert_type: AlertType::HighMemory,
                        node_id: node_id.clone(),
                        severity: health.current_metrics.memory_usage,
                        message: format!(
                            "High memory usage: {:.1}%",
                            health.current_metrics.memory_usage * 100.0
                        ),
                        timestamp: SystemTime::now(),
                        time_to_failure: None,
                    });
                }
            }

            // Check disk usage
            if health.current_metrics.disk_usage > self.config.disk_alert_threshold {
                if self
                    .should_generate_alert(node_id, &AlertType::HighDisk)
                    .await
                {
                    alerts.push(HealthAlert {
                        alert_type: AlertType::HighDisk,
                        node_id: node_id.clone(),
                        severity: health.current_metrics.disk_usage,
                        message: format!(
                            "High disk usage: {:.1}%",
                            health.current_metrics.disk_usage * 100.0
                        ),
                        timestamp: SystemTime::now(),
                        time_to_failure: None,
                    });
                }
            }

            // Predictive analysis
            if let Some((alert_type, time_to_failure)) = self.predict_failure(health).await {
                if self
                    .should_generate_alert(node_id, &AlertType::PredictedFailure)
                    .await
                {
                    alerts.push(HealthAlert {
                        alert_type,
                        node_id: node_id.clone(),
                        severity: 0.8,
                        message: format!("Predicted resource exhaustion in {:?}", time_to_failure),
                        timestamp: SystemTime::now(),
                        time_to_failure: Some(time_to_failure),
                    });
                }
            }
        }

        // Store alerts
        for alert in &alerts {
            self.generate_alert(
                alert.node_id.clone(),
                alert.alert_type.clone(),
                alert.severity,
                alert.message.clone(),
                alert.time_to_failure,
            )
            .await;
        }

        alerts
    }

    /// Predict potential failures based on historical trends
    async fn predict_failure(&self, health: &NodeHealth) -> Option<(AlertType, Duration)> {
        if health.metrics_history.len() < self.config.prediction_window {
            return None;
        }

        let recent_metrics: Vec<_> = health
            .metrics_history
            .iter()
            .rev()
            .take(self.config.prediction_window)
            .collect();

        // Predict CPU exhaustion
        if let Some(ttf) = self.predict_resource_exhaustion(
            &recent_metrics,
            |m| m.cpu_usage,
            self.config.cpu_alert_threshold,
        ) {
            return Some((AlertType::PredictedFailure, ttf));
        }

        // Predict memory exhaustion
        if let Some(ttf) = self.predict_resource_exhaustion(
            &recent_metrics,
            |m| m.memory_usage,
            self.config.memory_alert_threshold,
        ) {
            return Some((AlertType::PredictedFailure, ttf));
        }

        // Predict disk exhaustion
        if let Some(ttf) = self.predict_resource_exhaustion(
            &recent_metrics,
            |m| m.disk_usage,
            self.config.disk_alert_threshold,
        ) {
            return Some((AlertType::PredictedFailure, ttf));
        }

        None
    }

    /// Predict when a resource will be exhausted based on linear regression
    fn predict_resource_exhaustion<F>(
        &self,
        metrics: &[&ResourceMetrics],
        extractor: F,
        threshold: f64,
    ) -> Option<Duration>
    where
        F: Fn(&ResourceMetrics) -> f64,
    {
        if metrics.len() < 2 {
            return None;
        }

        // Calculate trend using simple linear regression
        let values: Vec<f64> = metrics.iter().map(|m| extractor(m)).collect();

        // Calculate mean
        let sum: f64 = values.iter().sum();
        let mean = sum / values.len() as f64;

        // Calculate slope (rate of change per sample)
        let mut slope_sum = 0.0;
        for i in 1..values.len() {
            slope_sum += values[i] - values[i - 1];
        }
        let slope = slope_sum / (values.len() - 1) as f64;

        // If slope is negative or zero, no exhaustion predicted
        if slope <= 0.0 {
            return None;
        }

        // Calculate current value
        let current = values.last().copied().unwrap_or(mean);

        // If already above threshold, return immediately
        if current >= threshold {
            return None;
        }

        // Calculate samples until exhaustion
        let samples_to_exhaustion = ((threshold - current) / slope).ceil() as u64;

        // Assume each sample represents check_interval_secs
        let seconds_to_exhaustion =
            samples_to_exhaustion.saturating_mul(self.config.check_interval_secs);

        // Only alert if exhaustion predicted within reasonable timeframe (e.g., 1 hour)
        if seconds_to_exhaustion > 0 && seconds_to_exhaustion <= 3600 {
            Some(Duration::from_secs(seconds_to_exhaustion))
        } else {
            None
        }
    }

    /// Check if an alert should be generated (respects cooldown)
    async fn should_generate_alert(&self, node_id: &OxirsNodeId, alert_type: &AlertType) -> bool {
        let last_alert_time = self.last_alert_time.read().await;
        let key = (node_id.clone(), alert_type.clone());

        if let Some(last_time) = last_alert_time.get(&key) {
            if let Ok(elapsed) = SystemTime::now().duration_since(*last_time) {
                if elapsed.as_secs() < self.config.alert_cooldown_secs {
                    return false;
                }
            }
        }

        true
    }

    /// Generate a health alert
    async fn generate_alert(
        &self,
        node_id: OxirsNodeId,
        alert_type: AlertType,
        severity: f64,
        message: String,
        time_to_failure: Option<Duration>,
    ) {
        let alert = HealthAlert {
            alert_type: alert_type.clone(),
            node_id: node_id.clone(),
            severity,
            message: message.clone(),
            timestamp: SystemTime::now(),
            time_to_failure,
        };

        warn!("Health alert: {:?}", alert);

        // Add to active alerts
        let mut active_alerts = self.active_alerts.write().await;
        active_alerts.push(alert.clone());

        // Add to history
        let mut alert_history = self.alert_history.write().await;
        alert_history.push_back(alert.clone());
        if alert_history.len() > self.config.history_size {
            alert_history.pop_front();
        }

        // Update last alert time
        let mut last_alert_time = self.last_alert_time.write().await;
        last_alert_time.insert((node_id, alert_type.clone()), SystemTime::now());

        // Update stats
        let mut stats = self.stats.write().await;
        stats.total_alerts += 1;
        *stats
            .alerts_by_type
            .entry(format!("{:?}", alert_type))
            .or_insert(0) += 1;

        // Update node counts
        let node_health = self.node_health.read().await;
        stats.degraded_nodes = node_health
            .values()
            .filter(|h| h.status == HealthStatus::Degraded)
            .count();
        stats.critical_nodes = node_health
            .values()
            .filter(|h| h.status == HealthStatus::Critical)
            .count();
    }

    /// Get current health status for a node
    pub async fn get_node_health(&self, node_id: &OxirsNodeId) -> Option<NodeHealth> {
        let node_health = self.node_health.read().await;
        node_health.get(node_id).cloned()
    }

    /// Get health status for all nodes
    pub async fn get_all_health(&self) -> BTreeMap<OxirsNodeId, NodeHealth> {
        self.node_health.read().await.clone()
    }

    /// Get active alerts
    pub async fn get_active_alerts(&self) -> Vec<HealthAlert> {
        self.active_alerts.read().await.clone()
    }

    /// Clear resolved alerts
    pub async fn clear_alerts(&self, node_id: &OxirsNodeId) {
        let mut active_alerts = self.active_alerts.write().await;
        active_alerts.retain(|alert| &alert.node_id != node_id);
    }

    /// Get alert history
    pub async fn get_alert_history(&self) -> Vec<HealthAlert> {
        self.alert_history.read().await.iter().cloned().collect()
    }

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

    /// Get overall cluster health
    pub async fn get_cluster_health(&self) -> HealthStatus {
        let node_health = self.node_health.read().await;

        let critical_count = node_health
            .values()
            .filter(|h| {
                h.status == HealthStatus::Critical || h.status == HealthStatus::Unresponsive
            })
            .count();

        let degraded_count = node_health
            .values()
            .filter(|h| h.status == HealthStatus::Degraded)
            .count();

        let total_nodes = node_health.len();

        if critical_count > 0 || (degraded_count as f64 / total_nodes as f64) > 0.5 {
            HealthStatus::Critical
        } else if degraded_count > 0 {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        }
    }
}

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

    #[tokio::test]
    async fn test_health_monitoring_creation() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let stats = monitor.get_stats().await;
        assert_eq!(stats.total_health_checks, 0);
        assert_eq!(stats.total_alerts, 0);
    }

    #[tokio::test]
    async fn test_register_node() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let health = monitor.get_node_health(&node_id).await;
        assert!(health.is_some());

        let health = health.unwrap();
        assert_eq!(health.status, HealthStatus::Healthy);
        assert_eq!(health.consecutive_failures, 0);
    }

    #[tokio::test]
    async fn test_update_metrics() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let metrics = ResourceMetrics {
            cpu_usage: 0.5,
            memory_usage: 0.5,
            disk_usage: 0.5,
            network_usage: 1000,
            active_connections: 10,
            timestamp: SystemTime::now(),
        };

        monitor.update_metrics(&node_id, metrics).await;

        let health = monitor.get_node_health(&node_id).await.unwrap();
        assert_eq!(health.status, HealthStatus::Healthy);
        assert_eq!(health.current_metrics.cpu_usage, 0.5);
    }

    #[tokio::test]
    async fn test_high_cpu_alert() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let metrics = ResourceMetrics {
            cpu_usage: 0.95,
            memory_usage: 0.5,
            disk_usage: 0.5,
            network_usage: 1000,
            active_connections: 10,
            timestamp: SystemTime::now(),
        };

        monitor.update_metrics(&node_id, metrics).await;

        let alerts = monitor.check_health().await;
        assert!(!alerts.is_empty());
        assert!(alerts.iter().any(|a| a.alert_type == AlertType::HighCpu));
    }

    #[tokio::test]
    async fn test_health_status_degraded() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let metrics = ResourceMetrics {
            cpu_usage: 0.75,
            memory_usage: 0.75,
            disk_usage: 0.5,
            network_usage: 1000,
            active_connections: 10,
            timestamp: SystemTime::now(),
        };

        monitor.update_metrics(&node_id, metrics).await;

        let health = monitor.get_node_health(&node_id).await.unwrap();
        assert_eq!(health.status, HealthStatus::Degraded);
    }

    #[tokio::test]
    async fn test_health_status_critical() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let metrics = ResourceMetrics {
            cpu_usage: 0.95,
            memory_usage: 0.95,
            disk_usage: 0.95,
            network_usage: 1000,
            active_connections: 10,
            timestamp: SystemTime::now(),
        };

        monitor.update_metrics(&node_id, metrics).await;

        let health = monitor.get_node_health(&node_id).await.unwrap();
        assert_eq!(health.status, HealthStatus::Critical);
    }

    #[tokio::test]
    async fn test_record_failure() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        monitor.record_failure(&node_id).await;
        monitor.record_failure(&node_id).await;
        monitor.record_failure(&node_id).await;

        let health = monitor.get_node_health(&node_id).await.unwrap();
        assert_eq!(health.status, HealthStatus::Unresponsive);
        assert_eq!(health.consecutive_failures, 3);
    }

    #[tokio::test]
    async fn test_alert_history() {
        let config = HealthMonitoringConfig {
            alert_cooldown_secs: 0,
            ..Default::default()
        };
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let metrics = ResourceMetrics {
            cpu_usage: 0.95,
            memory_usage: 0.5,
            disk_usage: 0.5,
            network_usage: 1000,
            active_connections: 10,
            timestamp: SystemTime::now(),
        };

        monitor.update_metrics(&node_id, metrics).await;
        monitor.check_health().await;

        let history = monitor.get_alert_history().await;
        assert!(!history.is_empty());
    }

    #[tokio::test]
    async fn test_clear_alerts() {
        let config = HealthMonitoringConfig {
            alert_cooldown_secs: 0,
            ..Default::default()
        };
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let metrics = ResourceMetrics {
            cpu_usage: 0.95,
            memory_usage: 0.5,
            disk_usage: 0.5,
            network_usage: 1000,
            active_connections: 10,
            timestamp: SystemTime::now(),
        };

        monitor.update_metrics(&node_id, metrics).await;
        monitor.check_health().await;

        let alerts_before = monitor.get_active_alerts().await;
        assert!(!alerts_before.is_empty());

        monitor.clear_alerts(&node_id).await;

        let alerts_after = monitor.get_active_alerts().await;
        assert!(alerts_after.is_empty());
    }

    #[tokio::test]
    async fn test_get_all_health() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node1: OxirsNodeId = 1;
        let node2: OxirsNodeId = 2;

        monitor.register_node(node1).await;
        monitor.register_node(node2).await;

        let all_health = monitor.get_all_health().await;
        assert_eq!(all_health.len(), 2);
    }

    #[tokio::test]
    async fn test_cluster_health() {
        let config = HealthMonitoringConfig::default();
        let monitor = HealthMonitoring::new(config);

        let node1: OxirsNodeId = 1;
        let node2: OxirsNodeId = 2;

        monitor.register_node(node1).await;
        monitor.register_node(node2).await;

        let cluster_health = monitor.get_cluster_health().await;
        assert_eq!(cluster_health, HealthStatus::Healthy);
    }

    #[tokio::test]
    async fn test_stats_tracking() {
        let config = HealthMonitoringConfig {
            alert_cooldown_secs: 0,
            ..Default::default()
        };
        let monitor = HealthMonitoring::new(config);

        let node_id: OxirsNodeId = 1;
        monitor.register_node(node_id).await;

        let metrics = ResourceMetrics {
            cpu_usage: 0.95,
            memory_usage: 0.5,
            disk_usage: 0.5,
            network_usage: 1000,
            active_connections: 10,
            timestamp: SystemTime::now(),
        };

        monitor.update_metrics(&node_id, metrics).await;
        monitor.check_health().await;

        let stats = monitor.get_stats().await;
        assert!(stats.total_health_checks > 0);
        assert!(stats.total_alerts > 0);
        assert_eq!(stats.critical_nodes, 1);
    }
}