anya_core/infrastructure/high_availability/
mod.rs

1use serde::{Deserialize, Serialize};
2
3pub mod cluster;
4pub mod failover;
5pub mod health_check;
6pub mod load_balancing;
7pub mod replication;
8
9mod config;
10
11pub use cluster::ClusterManager;
12pub use config::HighAvailabilityConfig;
13pub use failover::FailoverManager;
14pub use health_check::HealthChecker;
15pub use load_balancing::LoadBalancer;
16pub use replication::ReplicationManager;
17
18/// High Availability module for Anya Core
19/// [AIR-3][AIS-3][AIT-3][PFM-3][SCL-3][RES-3]
20///
21/// Provides resilient infrastructure capabilities including:
22/// - Cluster management
23/// - Automatic failover
24/// - Health monitoring
25/// - Data replication
26/// - Load balancing
27///
28/// This module coordinates all high availability features to ensure
29/// system reliability and fault tolerance.
30pub struct HighAvailabilityManager {
31    config: HighAvailabilityConfig,
32    cluster_manager: ClusterManager,
33    failover_manager: FailoverManager,
34    health_checker: HealthChecker,
35    replication_manager: ReplicationManager,
36    load_balancer: LoadBalancer,
37}
38
39impl HighAvailabilityManager {
40    /// Creates a new HighAvailabilityManager with the specified configuration
41    pub fn new(config: HighAvailabilityConfig) -> Self {
42        let cluster_manager = ClusterManager::new(&config);
43        let failover_manager = FailoverManager::new(&config);
44        let health_checker = HealthChecker::new(&config);
45        let replication_manager = ReplicationManager::new(&config);
46        let load_balancer = LoadBalancer::new(&config);
47
48        Self {
49            config,
50            cluster_manager,
51            failover_manager,
52            health_checker,
53            replication_manager,
54            load_balancer,
55        }
56    }
57
58    /// Initializes all high availability components
59    pub async fn initialize(&mut self) -> Result<(), HaError> {
60        self.cluster_manager.initialize().await?;
61        self.health_checker.start_monitoring().await?;
62        self.replication_manager.initialize().await?;
63        self.load_balancer.initialize().await?;
64        self.failover_manager.initialize().await?;
65
66        Ok(())
67    }
68
69    /// Starts all high availability services
70    pub async fn start(&mut self) -> Result<(), HaError> {
71        self.cluster_manager.join_cluster().await?;
72        self.health_checker.start_monitoring().await?;
73        self.replication_manager.start_replication().await?;
74        self.load_balancer.start().await?;
75        self.failover_manager.enable().await?;
76
77        Ok(())
78    }
79
80    /// Stops all high availability services
81    pub async fn stop(&mut self) -> Result<(), HaError> {
82        self.failover_manager.disable().await?;
83        self.load_balancer.stop().await?;
84        self.replication_manager.stop_replication().await?;
85        self.health_checker.stop_monitoring().await?;
86        self.cluster_manager.leave_cluster().await?;
87
88        Ok(())
89    }
90
91    /// Gets the current cluster status
92    pub async fn get_cluster_status(&self) -> Result<ClusterStatus, HaError> {
93        self.cluster_manager.get_status().await
94    }
95
96    /// Gets the current health status
97    pub async fn get_health_status(&self) -> Result<HealthStatus, HaError> {
98        self.health_checker.get_status().await
99    }
100
101    /// Triggers a manual failover
102    pub async fn trigger_failover(&mut self) -> Result<(), HaError> {
103        self.failover_manager.trigger_manual_failover().await
104    }
105
106    /// Updates the high availability configuration
107    pub async fn update_config(&mut self, config: HighAvailabilityConfig) -> Result<(), HaError> {
108        self.config = config.clone();
109        self.cluster_manager.update_config(&config).await?;
110        self.failover_manager.update_config(&config).await?;
111        self.health_checker.update_config(&config).await?;
112        self.replication_manager.update_config(&config).await?;
113        self.load_balancer.update_config(&config).await?;
114
115        Ok(())
116    }
117}
118
119/// Error types for high availability operations
120#[derive(Debug, thiserror::Error)]
121pub enum HaError {
122    #[error("Cluster operation failed: {0}")]
123    ClusterError(String),
124
125    #[error("Failover operation failed: {0}")]
126    FailoverError(String),
127
128    #[error("Health check failed: {0}")]
129    HealthCheckError(String),
130
131    #[error("Replication error: {0}")]
132    ReplicationError(String),
133
134    #[error("Load balancing error: {0}")]
135    LoadBalancerError(String),
136
137    #[error("Configuration error: {0}")]
138    ConfigError(String),
139
140    #[error("Network error: {0}")]
141    NetworkError(String),
142
143    #[error("I/O error: {0}")]
144    IoError(#[from] std::io::Error),
145}
146
147/// Status of the cluster
148#[derive(Debug, Clone, PartialEq, Eq)]
149pub enum ClusterStatus {
150    /// Cluster is healthy with all nodes operating
151    Healthy,
152
153    /// Cluster is operating with degraded service
154    Degraded {
155        active_nodes: usize,
156        total_nodes: usize,
157        details: String,
158    },
159
160    /// Cluster is in failover state
161    Failover {
162        primary_node: String,
163        failing_node: Option<String>,
164        failover_phase: FailoverPhase,
165    },
166
167    /// Cluster is initializing
168    Initializing,
169
170    /// Cluster is down
171    Down { reason: String },
172}
173
174/// Phase of the failover process
175#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
176pub enum FailoverPhase {
177    /// Detecting failure
178    Detection,
179
180    /// Electing new primary
181    Election,
182
183    /// Promoting standby to primary
184    Promotion,
185
186    /// Redirecting clients
187    Redirection,
188
189    /// Recovering failed node
190    Recovery,
191
192    /// Completed failover
193    Completed,
194}
195
196/// Health status of the system
197#[derive(Debug, Clone, PartialEq, Eq)]
198pub struct HealthStatus {
199    /// Overall status
200    pub status: HealthState,
201
202    /// Component-specific health
203    pub components: std::collections::HashMap<String, ComponentHealth>,
204
205    /// Last check timestamp
206    pub last_check: chrono::DateTime<chrono::Utc>,
207
208    /// Status message
209    pub message: Option<String>,
210}
211
212/// General health state
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
214pub enum HealthState {
215    Healthy,
216    Degraded,
217    Critical,
218    Unknown,
219}
220
221/// Health of a specific component
222#[derive(Debug, Clone, PartialEq, Eq)]
223pub struct ComponentHealth {
224    pub name: String,
225    pub status: HealthState,
226    pub details: Option<String>,
227    pub last_check: chrono::DateTime<chrono::Utc>,
228}