omni_orchestrator/app_autoscaler/
policy.rs

1use std::collections::HashMap;
2use std::time::Duration;
3use serde::{Serialize, Deserialize};
4
5use super::metrics::MetricThreshold;
6
7/// Configuration for scaling operations
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct ScalingPolicy {
10    /// The maximum number of worker nodes allowed in the cluster
11    pub max_worker_count: usize,
12    /// The minimum number of worker nodes allowed in the cluster
13    pub min_worker_count: usize,
14    /// The cooldown period between scaling actions
15    pub cooldown_period: Duration,
16    /// Custom metrics and their thresholds for scaling decisions
17    pub metrics_thresholds: HashMap<String, MetricThreshold>,
18    /// Number of workers to add during scale up
19    pub scale_up_increment: usize,
20    /// Number of workers to remove during scale down
21    pub scale_down_increment: usize,
22    /// Time to wait before considering a scale down action
23    pub scale_down_delay: Duration,
24    /// Maximum percentage of workers that can be scaled down at once
25    pub max_scale_down_percentage: f32,
26    /// Whether to enable automatic scaling
27    pub autoscaling_enabled: bool,
28}
29
30impl Default for ScalingPolicy {
31    fn default() -> Self {
32        Self {
33            max_worker_count: 10,
34            min_worker_count: 1,
35            cooldown_period: Duration::from_secs(300), // 5 minutes
36            metrics_thresholds: HashMap::new(),
37            scale_up_increment: 1,
38            scale_down_increment: 1,
39            scale_down_delay: Duration::from_secs(600), // 10 minutes
40            max_scale_down_percentage: 0.25, // 25%
41            autoscaling_enabled: true,
42        }
43    }
44}
45
46// Example of a configuration function
47pub fn create_default_cpu_memory_scaling_policy() -> ScalingPolicy {
48    let mut policy = ScalingPolicy::default();
49    
50    let mut thresholds = HashMap::new();
51    thresholds.insert("cpu_utilization".to_string(), MetricThreshold::Float(70.0));
52    thresholds.insert("memory_utilization".to_string(), MetricThreshold::Float(80.0));
53    
54    policy.metrics_thresholds = thresholds;
55    policy
56}