Initial commit
This commit is contained in:
@@ -0,0 +1,313 @@
|
||||
//! Production infrastructure for federated learning systems
|
||||
|
||||
pub mod client_manager;
|
||||
pub mod communication;
|
||||
pub mod fault_tolerance;
|
||||
pub mod monitoring;
|
||||
pub mod resource_scheduler;
|
||||
|
||||
use crate::error::{FederatedError, Result};
|
||||
use async_trait::async_trait;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// Re-export key types
|
||||
pub use client_manager::ClientManager;
|
||||
pub use communication::CommunicationOptimizer;
|
||||
pub use fault_tolerance::FaultTolerance;
|
||||
pub use monitoring::MonitoringSystem;
|
||||
pub use resource_scheduler::ResourceScheduler;
|
||||
|
||||
/// Infrastructure health status
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct InfrastructureHealth {
|
||||
/// Overall health score (0.0 to 1.0)
|
||||
pub overall_health: f64,
|
||||
/// Client manager health
|
||||
pub client_manager: ComponentHealth,
|
||||
/// Communication optimizer health
|
||||
pub communication: ComponentHealth,
|
||||
/// Fault tolerance system health
|
||||
pub fault_tolerance: ComponentHealth,
|
||||
/// Monitoring system health
|
||||
pub monitoring: ComponentHealth,
|
||||
/// Resource scheduler health
|
||||
pub resource_scheduler: ComponentHealth,
|
||||
}
|
||||
|
||||
/// Individual component health
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ComponentHealth {
|
||||
/// Health status
|
||||
pub status: HealthStatus,
|
||||
/// Health score (0.0 to 1.0)
|
||||
pub score: f64,
|
||||
/// Last health check timestamp
|
||||
pub last_check: chrono::DateTime<chrono::Utc>,
|
||||
/// Error count in last period
|
||||
pub error_count: usize,
|
||||
/// Performance metrics
|
||||
pub performance_metrics: std::collections::HashMap<String, f64>,
|
||||
}
|
||||
|
||||
/// Health status levels
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum HealthStatus {
|
||||
Healthy,
|
||||
Degraded,
|
||||
Critical,
|
||||
Down,
|
||||
}
|
||||
|
||||
/// Trait for infrastructure components
|
||||
#[async_trait]
|
||||
pub trait InfrastructureComponent: Send + Sync {
|
||||
/// Initialize the component
|
||||
async fn initialize(&mut self) -> Result<()>;
|
||||
|
||||
/// Shutdown the component gracefully
|
||||
async fn shutdown(&mut self) -> Result<()>;
|
||||
|
||||
/// Check component health
|
||||
async fn health_check(&self) -> Result<ComponentHealth>;
|
||||
|
||||
/// Get component name for monitoring
|
||||
fn component_name(&self) -> &'static str;
|
||||
|
||||
/// Handle component errors
|
||||
async fn handle_error(&mut self, error: &FederatedError) -> Result<()>;
|
||||
}
|
||||
|
||||
/// Infrastructure configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct InfrastructureConfig {
|
||||
/// Client management configuration
|
||||
pub client_config: ClientManagerConfig,
|
||||
/// Communication configuration
|
||||
pub communication_config: CommunicationConfig,
|
||||
/// Fault tolerance configuration
|
||||
pub fault_tolerance_config: FaultToleranceConfig,
|
||||
/// Monitoring configuration
|
||||
pub monitoring_config: MonitoringConfig,
|
||||
/// Resource scheduling configuration
|
||||
pub resource_config: ResourceSchedulerConfig,
|
||||
}
|
||||
|
||||
/// Client manager configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ClientManagerConfig {
|
||||
/// Maximum number of clients
|
||||
pub max_clients: usize,
|
||||
/// Client timeout (seconds)
|
||||
pub client_timeout_sec: u64,
|
||||
/// Registration rate limit
|
||||
pub registration_rate_limit: usize,
|
||||
/// Enable client authentication
|
||||
pub authentication_enabled: bool,
|
||||
}
|
||||
|
||||
/// Communication optimizer configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct CommunicationConfig {
|
||||
/// Enable compression
|
||||
pub compression_enabled: bool,
|
||||
/// Compression level (0-9)
|
||||
pub compression_level: u32,
|
||||
/// Maximum message size (bytes)
|
||||
pub max_message_size: usize,
|
||||
/// Connection timeout (seconds)
|
||||
pub connection_timeout_sec: u64,
|
||||
}
|
||||
|
||||
/// Fault tolerance configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FaultToleranceConfig {
|
||||
/// Enable automatic recovery
|
||||
pub auto_recovery_enabled: bool,
|
||||
/// Maximum retry attempts
|
||||
pub max_retries: usize,
|
||||
/// Retry backoff strategy
|
||||
pub backoff_strategy: BackoffStrategy,
|
||||
/// Checkpoint frequency (rounds)
|
||||
pub checkpoint_frequency: usize,
|
||||
}
|
||||
|
||||
/// Backoff strategies for retries
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum BackoffStrategy {
|
||||
Fixed { delay_ms: u64 },
|
||||
Exponential { base_ms: u64, max_ms: u64 },
|
||||
Linear { initial_ms: u64, increment_ms: u64 },
|
||||
}
|
||||
|
||||
/// Monitoring system configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct MonitoringConfig {
|
||||
/// Enable real-time monitoring
|
||||
pub enabled: bool,
|
||||
/// Metrics collection interval (seconds)
|
||||
pub collection_interval_sec: u64,
|
||||
/// Enable alerting
|
||||
pub alerting_enabled: bool,
|
||||
/// Dashboard port
|
||||
pub dashboard_port: Option<u16>,
|
||||
}
|
||||
|
||||
/// Resource scheduler configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ResourceSchedulerConfig {
|
||||
/// Scheduling algorithm
|
||||
pub algorithm: SchedulingAlgorithm,
|
||||
/// Resource constraints
|
||||
pub resource_constraints: ResourceConstraints,
|
||||
/// Load balancing enabled
|
||||
pub load_balancing_enabled: bool,
|
||||
/// Fair scheduling enabled
|
||||
pub fair_scheduling_enabled: bool,
|
||||
}
|
||||
|
||||
/// Scheduling algorithms
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum SchedulingAlgorithm {
|
||||
RoundRobin,
|
||||
WeightedRandom,
|
||||
ResourceBased,
|
||||
PerformanceBased,
|
||||
FairShare,
|
||||
}
|
||||
|
||||
/// Resource constraints
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ResourceConstraints {
|
||||
/// Maximum CPU usage (0.0 to 1.0)
|
||||
pub max_cpu_usage: f64,
|
||||
/// Maximum memory usage (bytes)
|
||||
pub max_memory_usage: u64,
|
||||
/// Maximum network bandwidth (Mbps)
|
||||
pub max_bandwidth_mbps: f64,
|
||||
/// Minimum battery level for mobile clients
|
||||
pub min_battery_level: Option<f64>,
|
||||
}
|
||||
|
||||
impl Default for InfrastructureConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
client_config: ClientManagerConfig {
|
||||
max_clients: 1000,
|
||||
client_timeout_sec: 300,
|
||||
registration_rate_limit: 100,
|
||||
authentication_enabled: true,
|
||||
},
|
||||
communication_config: CommunicationConfig {
|
||||
compression_enabled: true,
|
||||
compression_level: 6,
|
||||
max_message_size: 100 * 1024 * 1024, // 100MB
|
||||
connection_timeout_sec: 30,
|
||||
},
|
||||
fault_tolerance_config: FaultToleranceConfig {
|
||||
auto_recovery_enabled: true,
|
||||
max_retries: 3,
|
||||
backoff_strategy: BackoffStrategy::Exponential {
|
||||
base_ms: 1000,
|
||||
max_ms: 30000,
|
||||
},
|
||||
checkpoint_frequency: 10,
|
||||
},
|
||||
monitoring_config: MonitoringConfig {
|
||||
enabled: true,
|
||||
collection_interval_sec: 60,
|
||||
alerting_enabled: true,
|
||||
dashboard_port: Some(8080),
|
||||
},
|
||||
resource_config: ResourceSchedulerConfig {
|
||||
algorithm: SchedulingAlgorithm::ResourceBased,
|
||||
resource_constraints: ResourceConstraints {
|
||||
max_cpu_usage: 0.8,
|
||||
max_memory_usage: 8 * 1024 * 1024 * 1024, // 8GB
|
||||
max_bandwidth_mbps: 100.0,
|
||||
min_battery_level: Some(0.2),
|
||||
},
|
||||
load_balancing_enabled: true,
|
||||
fair_scheduling_enabled: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ComponentHealth {
|
||||
/// Create a new healthy component status
|
||||
pub fn healthy() -> Self {
|
||||
Self {
|
||||
status: HealthStatus::Healthy,
|
||||
score: 1.0,
|
||||
last_check: chrono::Utc::now(),
|
||||
error_count: 0,
|
||||
performance_metrics: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a degraded component status
|
||||
pub fn degraded(score: f64, error_count: usize) -> Self {
|
||||
Self {
|
||||
status: HealthStatus::Degraded,
|
||||
score: score.clamp(0.0, 1.0),
|
||||
last_check: chrono::Utc::now(),
|
||||
error_count,
|
||||
performance_metrics: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a critical component status
|
||||
pub fn critical(error_count: usize) -> Self {
|
||||
Self {
|
||||
status: HealthStatus::Critical,
|
||||
score: 0.2,
|
||||
last_check: chrono::Utc::now(),
|
||||
error_count,
|
||||
performance_metrics: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a down component status
|
||||
pub fn down() -> Self {
|
||||
Self {
|
||||
status: HealthStatus::Down,
|
||||
score: 0.0,
|
||||
last_check: chrono::Utc::now(),
|
||||
error_count: 0,
|
||||
performance_metrics: std::collections::HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add performance metric
|
||||
pub fn add_metric<S: Into<String>>(&mut self, name: S, value: f64) {
|
||||
self.performance_metrics.insert(name.into(), value);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_component_health_creation() {
|
||||
let healthy = ComponentHealth::healthy();
|
||||
assert!(matches!(healthy.status, HealthStatus::Healthy));
|
||||
assert_eq!(healthy.score, 1.0);
|
||||
assert_eq!(healthy.error_count, 0);
|
||||
|
||||
let degraded = ComponentHealth::degraded(0.7, 5);
|
||||
assert!(matches!(degraded.status, HealthStatus::Degraded));
|
||||
assert_eq!(degraded.score, 0.7);
|
||||
assert_eq!(degraded.error_count, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_infrastructure_config_default() {
|
||||
let config = InfrastructureConfig::default();
|
||||
assert_eq!(config.client_config.max_clients, 1000);
|
||||
assert!(config.communication_config.compression_enabled);
|
||||
assert!(config.fault_tolerance_config.auto_recovery_enabled);
|
||||
assert!(config.monitoring_config.enabled);
|
||||
assert!(config.resource_config.load_balancing_enabled);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user