optirs-tpu 0.3.1

OptiRS TPU coordination and pod management
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
// Fault Tolerance for TPU Pod Coordination
//
// This module provides comprehensive fault tolerance functionality for TPU pod coordination,
// including failure detection, recovery strategies, redundancy management, and checkpointing.

use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant};

use super::DeviceId;
use crate::error::{OptimError, Result};
use scirs2_core::error::ErrorContext;

/// Types of failures
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FailureType {
    DeviceFailure,
    NetworkFailure,
    MemoryFailure,
    ComputeFailure,
    SoftwareFailure,
    DataCorruption,
}

/// Recovery strategies
#[derive(Debug, Clone)]
pub enum RecoveryStrategy {
    Restart,
    Migrate,
    Replicate,
    Rollback,
    Isolate,
    Graceful,
}

/// Failure detection algorithms
#[derive(Debug, Clone, Copy)]
pub enum FailureDetectionAlgorithm {
    Timeout,
    HeartbeatMissing,
    PerformanceDegradation,
    ErrorRate,
    Consensus,
    Adaptive,
}

/// Device status for fault tolerance
#[derive(Debug, Clone, Copy)]
pub enum DeviceStatus {
    Active,
    Idle,
    Busy,
    Failed,
    Recovering,
    Offline,
}

/// Failure information
#[derive(Debug, Clone)]
pub struct FailureInfo {
    /// Failure type
    pub failure_type: FailureType,

    /// Failed device
    pub device_id: DeviceId,

    /// Detection timestamp
    pub detected_at: Instant,

    /// Failure severity (0.0 to 1.0)
    pub severity: f64,

    /// Error message
    pub error_message: String,

    /// Recovery attempts
    pub recovery_attempts: usize,

    /// Status
    pub status: FailureStatus,
}

/// Failure status
#[derive(Debug, Clone, Copy)]
pub enum FailureStatus {
    Detected,
    Analyzing,
    Recovering,
    Recovered,
    Permanent,
}

/// Recovery action
#[derive(Debug, Clone)]
pub struct RecoveryAction {
    /// Action type
    pub action_type: RecoveryStrategy,

    /// Target devices
    pub target_devices: Vec<DeviceId>,

    /// Estimated completion time
    pub estimated_completion: Duration,

    /// Priority
    pub priority: RecoveryPriority,

    /// Required resources
    pub required_resources: Vec<String>,
}

/// Recovery priority
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum RecoveryPriority {
    Low,
    Medium,
    High,
    Critical,
}

/// Checkpoint information
#[derive(Debug, Clone)]
pub struct CheckpointInfo {
    /// Checkpoint ID
    pub checkpoint_id: String,

    /// Creation timestamp
    pub created_at: Instant,

    /// Size in bytes
    pub size_bytes: usize,

    /// Associated devices
    pub devices: Vec<DeviceId>,

    /// Checkpoint type
    pub checkpoint_type: CheckpointType,

    /// Metadata
    pub metadata: HashMap<String, String>,
}

/// Checkpoint types
#[derive(Debug, Clone, Copy)]
pub enum CheckpointType {
    Full,
    Incremental,
    Differential,
    Log,
}

/// Redundancy configuration
#[derive(Debug, Clone)]
pub struct RedundancyConfig {
    /// Replication factor
    pub replication_factor: usize,

    /// Redundancy strategy
    pub strategy: RedundancyStrategy,

    /// Consistency level
    pub consistency_level: ConsistencyLevel,

    /// Failure tolerance
    pub failure_tolerance: usize,
}

/// Redundancy strategies
#[derive(Debug, Clone, Copy)]
pub enum RedundancyStrategy {
    Replication,
    ErasureCoding,
    Hybrid,
    Adaptive,
}

/// Consistency levels
#[derive(Debug, Clone, Copy)]
pub enum ConsistencyLevel {
    Eventual,
    Strong,
    Causal,
    Sequential,
    Linearizable,
}

/// Type aliases for managers
type HeartbeatManager = HashMap<DeviceId, Instant>;
type RedundancyManager = HashMap<String, f64>;
type CheckpointingSystem = HashMap<String, Vec<u8>>;
type RollbackManager = HashMap<String, Vec<u8>>;

/// Fault tolerance statistics
pub type FaultToleranceStatistics = HashMap<String, f64>;

/// Failure detector
#[derive(Debug)]
pub struct FailureDetector {
    /// Monitored devices
    monitored_devices: HashSet<DeviceId>,

    /// Heartbeat manager
    heartbeat_manager: HeartbeatManager,

    /// Failure threshold
    failure_threshold: Duration,

    /// Detection algorithm
    detection_algorithm: FailureDetectionAlgorithm,

    /// Failure history
    failure_history: Vec<FailureInfo>,

    /// Detection configuration
    detection_config: DetectionConfig,
}

/// Detection configuration
#[derive(Debug, Clone)]
pub struct DetectionConfig {
    /// Heartbeat interval
    pub heartbeat_interval: Duration,

    /// Timeout threshold
    pub timeout_threshold: Duration,

    /// Performance degradation threshold
    pub performance_threshold: f64,

    /// Error rate threshold
    pub error_rate_threshold: f64,

    /// Consensus threshold
    pub consensus_threshold: usize,
}

impl FailureDetector {
    /// Create a new failure detector
    pub fn new(config: DetectionConfig) -> Self {
        Self {
            monitored_devices: HashSet::new(),
            heartbeat_manager: HashMap::new(),
            failure_threshold: config.timeout_threshold,
            detection_algorithm: FailureDetectionAlgorithm::Timeout,
            failure_history: Vec::new(),
            detection_config: config,
        }
    }

    /// Add device to monitoring
    pub fn add_device(&mut self, device_id: DeviceId) {
        self.monitored_devices.insert(device_id);
        self.heartbeat_manager.insert(device_id, Instant::now());
    }

    /// Remove device from monitoring
    pub fn remove_device(&mut self, device_id: DeviceId) {
        self.monitored_devices.remove(&device_id);
        self.heartbeat_manager.remove(&device_id);
    }

    /// Update heartbeat for device
    pub fn update_heartbeat(&mut self, device_id: DeviceId) {
        if self.monitored_devices.contains(&device_id) {
            self.heartbeat_manager.insert(device_id, Instant::now());
        }
    }

    /// Check for failures
    pub fn check_failures(&mut self) -> Vec<FailureInfo> {
        let mut detected_failures = Vec::new();
        let now = Instant::now();

        for &device_id in &self.monitored_devices {
            if let Some(&last_heartbeat) = self.heartbeat_manager.get(&device_id) {
                let time_since_heartbeat = now.duration_since(last_heartbeat);

                if time_since_heartbeat > self.failure_threshold {
                    let failure = FailureInfo {
                        failure_type: FailureType::DeviceFailure,
                        device_id,
                        detected_at: now,
                        severity: self.calculate_failure_severity(time_since_heartbeat),
                        error_message: format!(
                            "Device {} failed to send heartbeat for {:?}",
                            device_id.0, time_since_heartbeat
                        ),
                        recovery_attempts: 0,
                        status: FailureStatus::Detected,
                    };

                    detected_failures.push(failure.clone());
                    self.failure_history.push(failure);
                }
            }
        }

        detected_failures
    }

    /// Calculate failure severity based on detection algorithm
    fn calculate_failure_severity(&self, time_since_heartbeat: Duration) -> f64 {
        match self.detection_algorithm {
            FailureDetectionAlgorithm::Timeout => {
                let ratio =
                    time_since_heartbeat.as_secs_f64() / self.failure_threshold.as_secs_f64();
                (ratio - 1.0).clamp(0.0, 1.0)
            }
            FailureDetectionAlgorithm::HeartbeatMissing
                if time_since_heartbeat > self.detection_config.heartbeat_interval * 3 =>
            {
                1.0
            }
            _ => 0.5, // Default severity for other algorithms
        }
    }

    /// Get failure statistics
    pub fn get_failure_statistics(&self) -> HashMap<String, f64> {
        let mut stats = HashMap::new();

        stats.insert(
            "monitored_devices".to_string(),
            self.monitored_devices.len() as f64,
        );

        stats.insert(
            "total_failures".to_string(),
            self.failure_history.len() as f64,
        );

        // Calculate failure rate
        let recent_failures = self
            .failure_history
            .iter()
            .filter(|f| f.detected_at.elapsed() < Duration::from_secs(3600))
            .count();
        stats.insert("recent_failure_rate".to_string(), recent_failures as f64);

        // Calculate average recovery time
        let avg_recovery_time = if self.failure_history.is_empty() {
            0.0
        } else {
            self.failure_history
                .iter()
                .filter(|f| matches!(f.status, FailureStatus::Recovered))
                .map(|f| f.detected_at.elapsed().as_secs_f64())
                .sum::<f64>()
                / self.failure_history.len() as f64
        };
        stats.insert("avg_recovery_time_secs".to_string(), avg_recovery_time);

        stats
    }

    /// Set detection algorithm
    pub fn set_detection_algorithm(&mut self, algorithm: FailureDetectionAlgorithm) {
        self.detection_algorithm = algorithm;
    }

    /// Get monitored devices
    pub fn get_monitored_devices(&self) -> &HashSet<DeviceId> {
        &self.monitored_devices
    }

    /// Get failure history
    pub fn get_failure_history(&self) -> &[FailureInfo] {
        &self.failure_history
    }
}

/// Fault tolerance manager
#[derive(Debug)]
pub struct FaultToleranceManager {
    /// Failure detector
    failure_detector: FailureDetector,

    /// Recovery strategies
    recovery_strategies: HashMap<FailureType, RecoveryStrategy>,

    /// Redundancy manager
    redundancy_manager: RedundancyManager,

    /// Checkpointing system
    checkpointing_system: CheckpointingSystem,

    /// Rollback manager
    rollback_manager: RollbackManager,

    /// Active recovery actions
    active_recoveries: HashMap<DeviceId, RecoveryAction>,

    /// Redundancy configuration
    redundancy_config: RedundancyConfig,

    /// Checkpoint configuration
    checkpoint_config: CheckpointConfig,
}

/// Checkpoint configuration
#[derive(Debug, Clone)]
pub struct CheckpointConfig {
    /// Checkpoint interval
    pub interval: Duration,

    /// Maximum checkpoints to keep
    pub max_checkpoints: usize,

    /// Compression enabled
    pub compression_enabled: bool,

    /// Encryption enabled
    pub encryption_enabled: bool,

    /// Storage path
    pub storage_path: String,
}

impl FaultToleranceManager {
    /// Create a new fault tolerance manager
    pub fn new(
        detection_config: DetectionConfig,
        redundancy_config: RedundancyConfig,
        checkpoint_config: CheckpointConfig,
    ) -> Result<Self> {
        let failure_detector = FailureDetector::new(detection_config);

        // Set up default recovery strategies
        let mut recovery_strategies = HashMap::new();
        recovery_strategies.insert(FailureType::DeviceFailure, RecoveryStrategy::Migrate);
        recovery_strategies.insert(FailureType::NetworkFailure, RecoveryStrategy::Restart);
        recovery_strategies.insert(FailureType::MemoryFailure, RecoveryStrategy::Rollback);
        recovery_strategies.insert(FailureType::ComputeFailure, RecoveryStrategy::Restart);
        recovery_strategies.insert(FailureType::SoftwareFailure, RecoveryStrategy::Restart);
        recovery_strategies.insert(FailureType::DataCorruption, RecoveryStrategy::Rollback);

        Ok(Self {
            failure_detector,
            recovery_strategies,
            redundancy_manager: HashMap::new(),
            checkpointing_system: HashMap::new(),
            rollback_manager: HashMap::new(),
            active_recoveries: HashMap::new(),
            redundancy_config,
            checkpoint_config,
        })
    }

    /// Monitor device for failures
    pub fn monitor_device(&mut self, device_id: DeviceId) {
        self.failure_detector.add_device(device_id);
    }

    /// Stop monitoring device
    pub fn stop_monitoring(&mut self, device_id: DeviceId) {
        self.failure_detector.remove_device(device_id);
    }

    /// Update device heartbeat
    pub fn update_heartbeat(&mut self, device_id: DeviceId) {
        self.failure_detector.update_heartbeat(device_id);
    }

    /// Check for failures and initiate recovery
    pub async fn check_and_recover(&mut self) -> Result<Vec<RecoveryAction>> {
        let failures = self.failure_detector.check_failures();
        let mut recovery_actions = Vec::new();

        for failure in failures {
            if let Some(strategy) = self.recovery_strategies.get(&failure.failure_type) {
                let recovery_action = self.create_recovery_action(&failure, strategy.clone())?;
                self.initiate_recovery(&failure, &recovery_action).await?;
                recovery_actions.push(recovery_action);
            }
        }

        Ok(recovery_actions)
    }

    /// Create recovery action for failure
    fn create_recovery_action(
        &self,
        failure: &FailureInfo,
        strategy: RecoveryStrategy,
    ) -> Result<RecoveryAction> {
        let priority = match failure.severity {
            s if s > 0.8 => RecoveryPriority::Critical,
            s if s > 0.6 => RecoveryPriority::High,
            s if s > 0.3 => RecoveryPriority::Medium,
            _ => RecoveryPriority::Low,
        };

        let estimated_completion = match strategy {
            RecoveryStrategy::Restart => Duration::from_secs(30),
            RecoveryStrategy::Migrate => Duration::from_secs(120),
            RecoveryStrategy::Replicate => Duration::from_secs(60),
            RecoveryStrategy::Rollback => Duration::from_secs(45),
            RecoveryStrategy::Isolate => Duration::from_secs(10),
            RecoveryStrategy::Graceful => Duration::from_secs(90),
        };

        Ok(RecoveryAction {
            action_type: strategy,
            target_devices: vec![failure.device_id],
            estimated_completion,
            priority,
            required_resources: vec!["compute".to_string(), "memory".to_string()],
        })
    }

    /// Initiate recovery for failure
    async fn initiate_recovery(
        &mut self,
        failure: &FailureInfo,
        recovery_action: &RecoveryAction,
    ) -> Result<()> {
        println!(
            "Initiating recovery for device {:?} using strategy {:?}",
            failure.device_id, recovery_action.action_type
        );

        match recovery_action.action_type {
            RecoveryStrategy::Restart => {
                self.restart_device(failure.device_id).await?;
            }
            RecoveryStrategy::Migrate => {
                self.migrate_workload(failure.device_id).await?;
            }
            RecoveryStrategy::Replicate => {
                self.replicate_data(failure.device_id).await?;
            }
            RecoveryStrategy::Rollback => {
                self.rollback_state(failure.device_id).await?;
            }
            RecoveryStrategy::Isolate => {
                self.isolate_device(failure.device_id).await?;
            }
            RecoveryStrategy::Graceful => {
                self.graceful_recovery(failure.device_id).await?;
            }
        }

        self.active_recoveries
            .insert(failure.device_id, recovery_action.clone());

        Ok(())
    }

    /// Restart failed device
    async fn restart_device(&mut self, device_id: DeviceId) -> Result<()> {
        println!("Restarting device {:?}", device_id);
        // Simulate restart delay
        tokio::time::sleep(Duration::from_millis(100)).await;
        self.failure_detector.update_heartbeat(device_id);
        Ok(())
    }

    /// Migrate workload from failed device
    async fn migrate_workload(&mut self, device_id: DeviceId) -> Result<()> {
        println!("Migrating workload from device {:?}", device_id);
        // In a real implementation, this would migrate tasks to healthy devices
        tokio::time::sleep(Duration::from_millis(200)).await;
        Ok(())
    }

    /// Replicate data for redundancy
    async fn replicate_data(&mut self, device_id: DeviceId) -> Result<()> {
        println!("Replicating data for device {:?}", device_id);
        // In a real implementation, this would create data replicas
        tokio::time::sleep(Duration::from_millis(150)).await;
        Ok(())
    }

    /// Rollback to previous state
    async fn rollback_state(&mut self, device_id: DeviceId) -> Result<()> {
        println!("Rolling back state for device {:?}", device_id);
        // In a real implementation, this would restore from checkpoint
        tokio::time::sleep(Duration::from_millis(120)).await;
        Ok(())
    }

    /// Isolate failed device
    async fn isolate_device(&mut self, device_id: DeviceId) -> Result<()> {
        println!("Isolating device {:?}", device_id);
        // In a real implementation, this would isolate the device from the network
        self.failure_detector.remove_device(device_id);
        Ok(())
    }

    /// Graceful recovery
    async fn graceful_recovery(&mut self, device_id: DeviceId) -> Result<()> {
        println!("Performing graceful recovery for device {:?}", device_id);
        // In a real implementation, this would perform a controlled recovery
        tokio::time::sleep(Duration::from_millis(180)).await;
        self.failure_detector.update_heartbeat(device_id);
        Ok(())
    }

    /// Create checkpoint
    pub async fn create_checkpoint(&mut self, checkpoint_id: String) -> Result<CheckpointInfo> {
        let checkpoint_info = CheckpointInfo {
            checkpoint_id: checkpoint_id.clone(),
            created_at: Instant::now(),
            size_bytes: 1024 * 1024, // 1MB simulated size
            devices: self
                .failure_detector
                .get_monitored_devices()
                .iter()
                .cloned()
                .collect(),
            checkpoint_type: CheckpointType::Full,
            metadata: HashMap::new(),
        };

        // Simulate checkpoint creation
        let checkpoint_data = vec![0u8; 1024]; // Simulated checkpoint data
        self.checkpointing_system
            .insert(checkpoint_id, checkpoint_data);

        println!("Created checkpoint: {}", checkpoint_info.checkpoint_id);
        Ok(checkpoint_info)
    }

    /// Restore from checkpoint
    pub async fn restore_checkpoint(&mut self, checkpoint_id: &str) -> Result<()> {
        if self.checkpointing_system.contains_key(checkpoint_id) {
            println!("Restoring from checkpoint: {}", checkpoint_id);
            // In a real implementation, this would restore system state
            tokio::time::sleep(Duration::from_millis(100)).await;
            Ok(())
        } else {
            Err(OptimError::ComputationError(ErrorContext::new(format!(
                "Checkpoint {} not found",
                checkpoint_id
            ))))
        }
    }

    /// Set recovery strategy for failure type
    pub fn set_recovery_strategy(&mut self, failure_type: FailureType, strategy: RecoveryStrategy) {
        self.recovery_strategies.insert(failure_type, strategy);
    }

    /// Get fault tolerance statistics
    pub fn get_statistics(&self) -> FaultToleranceStatistics {
        let mut stats = self.failure_detector.get_failure_statistics();

        stats.insert(
            "active_recoveries".to_string(),
            self.active_recoveries.len() as f64,
        );

        stats.insert(
            "checkpoints_count".to_string(),
            self.checkpointing_system.len() as f64,
        );

        stats.insert(
            "redundancy_level".to_string(),
            self.redundancy_config.replication_factor as f64,
        );

        // Calculate system reliability
        let total_devices = self.failure_detector.get_monitored_devices().len() as f64;
        let failed_devices = self.active_recoveries.len() as f64;
        let reliability = if total_devices > 0.0 {
            (total_devices - failed_devices) / total_devices
        } else {
            1.0
        };
        stats.insert("system_reliability".to_string(), reliability);

        stats
    }

    /// Get active recovery actions
    pub fn get_active_recoveries(&self) -> &HashMap<DeviceId, RecoveryAction> {
        &self.active_recoveries
    }

    /// Complete recovery for device
    pub fn complete_recovery(&mut self, device_id: DeviceId) -> Result<()> {
        if self.active_recoveries.remove(&device_id).is_some() {
            println!("Recovery completed for device {:?}", device_id);
            // Re-add device to monitoring if it was isolated
            self.failure_detector.add_device(device_id);
            Ok(())
        } else {
            Err(OptimError::ComputationError(ErrorContext::new(format!(
                "No active recovery for device {:?}",
                device_id
            ))))
        }
    }

    /// Update redundancy configuration
    pub fn update_redundancy_config(&mut self, config: RedundancyConfig) {
        self.redundancy_config = config;
    }

    /// Update checkpoint configuration
    pub fn update_checkpoint_config(&mut self, config: CheckpointConfig) {
        self.checkpoint_config = config;
    }
}

// Default implementations
impl Default for DetectionConfig {
    fn default() -> Self {
        Self {
            heartbeat_interval: Duration::from_secs(5),
            timeout_threshold: Duration::from_secs(30),
            performance_threshold: 0.1,
            error_rate_threshold: 0.05,
            consensus_threshold: 3,
        }
    }
}

impl Default for RedundancyConfig {
    fn default() -> Self {
        Self {
            replication_factor: 3,
            strategy: RedundancyStrategy::Replication,
            consistency_level: ConsistencyLevel::Strong,
            failure_tolerance: 1,
        }
    }
}

impl Default for CheckpointConfig {
    fn default() -> Self {
        Self {
            interval: Duration::from_secs(300), // 5 minutes
            max_checkpoints: 10,
            compression_enabled: true,
            encryption_enabled: false,
            storage_path: "/tmp/checkpoints".to_string(),
        }
    }
}

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

    #[test]
    fn test_failure_detector_creation() {
        let config = DetectionConfig::default();
        let detector = FailureDetector::new(config);
        assert_eq!(detector.get_monitored_devices().len(), 0);
    }

    #[test]
    fn test_device_monitoring() {
        let config = DetectionConfig::default();
        let mut detector = FailureDetector::new(config);

        let device_id = DeviceId(0);
        detector.add_device(device_id);

        assert!(detector.get_monitored_devices().contains(&device_id));
    }

    #[test]
    fn test_fault_tolerance_manager_creation() {
        let detection_config = DetectionConfig::default();
        let redundancy_config = RedundancyConfig::default();
        let checkpoint_config = CheckpointConfig::default();

        let manager =
            FaultToleranceManager::new(detection_config, redundancy_config, checkpoint_config);

        assert!(manager.is_ok());
    }

    #[tokio::test]
    async fn test_checkpoint_creation() {
        let detection_config = DetectionConfig::default();
        let redundancy_config = RedundancyConfig::default();
        let checkpoint_config = CheckpointConfig::default();

        let mut manager =
            FaultToleranceManager::new(detection_config, redundancy_config, checkpoint_config)
                .expect("unwrap failed");

        let checkpoint_info = manager
            .create_checkpoint("test_checkpoint".to_string())
            .await;
        assert!(checkpoint_info.is_ok());
        assert_eq!(
            checkpoint_info.expect("unwrap failed").checkpoint_id,
            "test_checkpoint"
        );
    }
}