use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HighAvailabilityConfig {
pub general: GeneralConfig,
pub cluster: ClusterConfig,
pub health_check: HealthCheckConfig,
pub failover: FailoverConfig,
pub replication: ReplicationConfig,
pub load_balancing: LoadBalancingConfig,
pub disaster_recovery: DisasterRecoveryConfig,
}
impl HighAvailabilityConfig {
pub fn development() -> Self {
Self {
general: GeneralConfig {
enabled: true,
environment: Environment::Development,
log_level: LogLevel::Debug,
},
cluster: ClusterConfig {
node_count: 2,
cluster_name: "anya-dev-cluster".to_string(),
discovery_method: DiscoveryMethod::Static,
static_nodes: vec!["localhost:5001".to_string(), "localhost:5002".to_string()],
..Default::default()
},
..Default::default()
}
}
pub fn production() -> Self {
Self {
general: GeneralConfig {
enabled: true,
environment: Environment::Production,
log_level: LogLevel::Info,
},
cluster: ClusterConfig {
node_count: 3,
cluster_name: "anya-prod-cluster".to_string(),
discovery_method: DiscoveryMethod::Dns,
dns_discovery_url: Some("anya-cluster.example.com".to_string()),
heartbeat_interval: Duration::from_secs(5),
..Default::default()
},
health_check: HealthCheckConfig {
enabled: true,
check_interval: Duration::from_secs(10),
critical_threshold: 3,
warning_threshold: 2,
..Default::default()
},
failover: FailoverConfig {
enabled: true,
auto_failover: true,
failover_timeout: Duration::from_secs(30),
min_nodes_for_failover: 2,
..Default::default()
},
replication: ReplicationConfig {
mode: ReplicationMode::Synchronous,
sync_timeout: Duration::from_secs(10),
..Default::default()
},
load_balancing: LoadBalancingConfig {
algorithm: LoadBalancingAlgorithm::RoundRobin,
health_check_enabled: true,
auto_scaling: true,
..Default::default()
},
disaster_recovery: DisasterRecoveryConfig {
backup_interval: Duration::from_secs(3600),
backup_retention: 7,
auto_restore: true,
..Default::default()
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneralConfig {
pub enabled: bool,
pub environment: Environment,
pub log_level: LogLevel,
}
impl Default for GeneralConfig {
fn default() -> Self {
Self {
enabled: true,
environment: Environment::Development,
log_level: LogLevel::Info,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Environment {
Development,
Staging,
Production,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterConfig {
pub node_count: usize,
pub cluster_name: String,
pub discovery_method: DiscoveryMethod,
#[serde(default)]
pub static_nodes: Vec<String>,
pub dns_discovery_url: Option<String>,
pub k8s_service_name: Option<String>,
#[serde(with = "humantime_serde")]
pub heartbeat_interval: Duration,
#[serde(with = "humantime_serde")]
pub node_timeout: Duration,
#[serde(with = "humantime_serde")]
pub gossip_interval: Duration,
}
impl Default for ClusterConfig {
fn default() -> Self {
Self {
node_count: 1,
cluster_name: "anya-cluster".to_string(),
discovery_method: DiscoveryMethod::Static,
static_nodes: vec!["localhost:5001".to_string()],
dns_discovery_url: None,
k8s_service_name: None,
heartbeat_interval: Duration::from_secs(10),
node_timeout: Duration::from_secs(30),
gossip_interval: Duration::from_secs(1),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DiscoveryMethod {
Static,
Dns,
Kubernetes,
Consul,
Etcd,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckConfig {
pub enabled: bool,
#[serde(with = "humantime_serde")]
pub check_interval: Duration,
pub warning_threshold: u32,
pub critical_threshold: u32,
#[serde(with = "humantime_serde")]
pub check_timeout: Duration,
#[serde(default)]
pub components: Vec<String>,
}
impl Default for HealthCheckConfig {
fn default() -> Self {
Self {
enabled: true,
check_interval: Duration::from_secs(30),
warning_threshold: 2,
critical_threshold: 3,
check_timeout: Duration::from_secs(5),
components: vec![
"cluster".to_string(),
"storage".to_string(),
"network".to_string(),
],
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailoverConfig {
pub enabled: bool,
pub auto_failover: bool,
#[serde(with = "humantime_serde")]
pub failover_timeout: Duration,
pub min_nodes_for_failover: usize,
pub max_auto_failovers: Option<u32>,
#[serde(with = "humantime_serde")]
pub auto_failover_period: Duration,
pub fencing_enabled: bool,
}
impl Default for FailoverConfig {
fn default() -> Self {
Self {
enabled: true,
auto_failover: true,
failover_timeout: Duration::from_secs(60),
min_nodes_for_failover: 2,
max_auto_failovers: Some(3),
auto_failover_period: Duration::from_secs(3600),
fencing_enabled: true,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationConfig {
pub mode: ReplicationMode,
#[serde(with = "humantime_serde")]
pub sync_timeout: Duration,
#[serde(with = "humantime_serde")]
pub max_lag: Duration,
pub ack_count: Option<usize>,
pub compression_enabled: bool,
pub encryption_enabled: bool,
}
impl Default for ReplicationConfig {
fn default() -> Self {
Self {
mode: ReplicationMode::SemiSynchronous,
sync_timeout: Duration::from_secs(5),
max_lag: Duration::from_millis(500),
ack_count: None,
compression_enabled: true,
encryption_enabled: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReplicationMode {
Synchronous,
SemiSynchronous,
Asynchronous,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadBalancingConfig {
pub algorithm: LoadBalancingAlgorithm,
pub health_check_enabled: bool,
pub sticky_sessions: bool,
pub auto_scaling: bool,
pub min_nodes: Option<usize>,
pub max_nodes: Option<usize>,
pub scale_up_threshold: Option<f32>,
pub scale_down_threshold: Option<f32>,
}
impl Default for LoadBalancingConfig {
fn default() -> Self {
Self {
algorithm: LoadBalancingAlgorithm::LeastConnections,
health_check_enabled: true,
sticky_sessions: false,
auto_scaling: false,
min_nodes: Some(1),
max_nodes: Some(10),
scale_up_threshold: Some(0.75),
scale_down_threshold: Some(0.25),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LoadBalancingAlgorithm {
RoundRobin,
LeastConnections,
LeastResponseTime,
WeightedRoundRobin,
ResourceBased,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DisasterRecoveryConfig {
#[serde(with = "humantime_serde")]
pub backup_interval: Duration,
pub backup_retention: u32,
pub auto_restore: bool,
pub remote_backup_location: Option<String>,
pub backup_encryption_key: Option<String>,
}
impl Default for DisasterRecoveryConfig {
fn default() -> Self {
Self {
backup_interval: Duration::from_secs(3600), backup_retention: 30, auto_restore: false,
remote_backup_location: None,
backup_encryption_key: None,
}
}
}