anya_core/infrastructure/high_availability/
mod.rs1use 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
18pub 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 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 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 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 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 pub async fn get_cluster_status(&self) -> Result<ClusterStatus, HaError> {
93 self.cluster_manager.get_status().await
94 }
95
96 pub async fn get_health_status(&self) -> Result<HealthStatus, HaError> {
98 self.health_checker.get_status().await
99 }
100
101 pub async fn trigger_failover(&mut self) -> Result<(), HaError> {
103 self.failover_manager.trigger_manual_failover().await
104 }
105
106 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#[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#[derive(Debug, Clone, PartialEq, Eq)]
149pub enum ClusterStatus {
150 Healthy,
152
153 Degraded {
155 active_nodes: usize,
156 total_nodes: usize,
157 details: String,
158 },
159
160 Failover {
162 primary_node: String,
163 failing_node: Option<String>,
164 failover_phase: FailoverPhase,
165 },
166
167 Initializing,
169
170 Down { reason: String },
172}
173
174#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
176pub enum FailoverPhase {
177 Detection,
179
180 Election,
182
183 Promotion,
185
186 Redirection,
188
189 Recovery,
191
192 Completed,
194}
195
196#[derive(Debug, Clone, PartialEq, Eq)]
198pub struct HealthStatus {
199 pub status: HealthState,
201
202 pub components: std::collections::HashMap<String, ComponentHealth>,
204
205 pub last_check: chrono::DateTime<chrono::Utc>,
207
208 pub message: Option<String>,
210}
211
212#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
214pub enum HealthState {
215 Healthy,
216 Degraded,
217 Critical,
218 Unknown,
219}
220
221#[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}