Skip to main content

qualia_core_db/specialized_libs/statistical_computing/
scheduler.rs

1use super::*;
2
3/// Statistical scheduler
4pub struct StatisticalScheduler {
5    scheduling_policy: SchedulingPolicy,
6    queue_manager: QueueManager,
7    load_balancer: LoadBalancer,
8}
9
10/// Scheduling policies
11#[derive(Debug, Clone, PartialEq)]
12pub enum SchedulingPolicy {
13    FIFO,
14    Priority,
15    ShortestJobFirst,
16    Deadline,
17    Adaptive,
18}
19
20/// Queue manager
21pub struct QueueManager {
22    pending_queue: Vec<QueuedOperation>,
23    running_operations: HashMap<String, RunningOperation>,
24    completed_operations: Vec<CompletedOperation>,
25}
26
27/// Queued operation
28#[derive(Debug, Clone)]
29pub struct QueuedOperation {
30    pub operation_id: String,
31    pub operation: StatisticalOperation,
32    pub priority: OperationPriority,
33    pub submitted_at: u64,
34    pub deadline: Option<u64>,
35}
36
37/// Running operation
38#[derive(Debug, Clone)]
39pub struct RunningOperation {
40    pub operation_id: String,
41    pub unit_id: String,
42    pub started_at: u64,
43    pub progress: f64,
44}
45
46/// Completed operation
47#[derive(Debug, Clone)]
48pub struct CompletedOperation {
49    pub operation_id: String,
50    pub started_at: u64,
51    pub completed_at: u64,
52    pub result: StatisticalResult,
53    pub success: bool,
54}
55
56/// Operation priorities
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub enum OperationPriority {
59    Low,
60    Normal,
61    High,
62    Critical,
63}
64
65/// Load balancer
66pub struct LoadBalancer {
67    balancing_strategy: BalancingStrategy,
68    unit_metrics: HashMap<String, UnitMetrics>,
69}
70
71/// Unit metrics
72#[derive(Debug, Clone)]
73pub struct UnitMetrics {
74    pub unit_id: String,
75    pub current_load: f64,
76    pub average_response_time: f64,
77    pub success_rate: f64,
78    pub energy_efficiency: f64,
79}
80
81impl StatisticalScheduler {
82    /// Creates a default `StatisticalScheduler`.
83    pub fn new() -> Self {
84        Self {
85            scheduling_policy: SchedulingPolicy::Priority,
86            queue_manager: QueueManager::new(),
87            load_balancer: LoadBalancer::new(),
88        }
89    }
90
91    /// Initializes the component; error propagates.
92    pub fn initialize(&mut self) -> Result<(), StatisticalError> {
93        Ok(())
94    }
95
96    /// Returns the current scheduling policy.
97    pub fn scheduling_policy(&self) -> &SchedulingPolicy {
98        &self.scheduling_policy
99    }
100
101    /// Set the scheduling policy.
102    pub fn set_scheduling_policy(&mut self, policy: SchedulingPolicy) {
103        self.scheduling_policy = policy;
104    }
105
106    /// Returns a reference to the queue manager.
107    pub fn queue_manager(&self) -> &QueueManager {
108        &self.queue_manager
109    }
110
111    /// Returns a mutable reference to the queue manager.
112    pub fn queue_manager_mut(&mut self) -> &mut QueueManager {
113        &mut self.queue_manager
114    }
115
116    /// Returns a reference to the load balancer.
117    pub fn load_balancer(&self) -> &LoadBalancer {
118        &self.load_balancer
119    }
120
121    /// Returns a mutable reference to the load balancer.
122    pub fn load_balancer_mut(&mut self) -> &mut LoadBalancer {
123        &mut self.load_balancer
124    }
125}
126
127impl QueueManager {
128    /// Creates a default `QueueManager`.
129    pub fn new() -> Self {
130        Self {
131            pending_queue: Vec::new(),
132            running_operations: HashMap::new(),
133            completed_operations: Vec::new(),
134        }
135    }
136
137    /// Enqueue a pending operation.
138    pub fn enqueue(&mut self, operation: QueuedOperation) {
139        self.pending_queue.push(operation);
140    }
141
142    /// Dequeue the next pending operation (FIFO order). Returns `None` when
143    /// the queue is empty.
144    pub fn dequeue(&mut self) -> Option<QueuedOperation> {
145        if self.pending_queue.is_empty() {
146            None
147        } else {
148            Some(self.pending_queue.remove(0))
149        }
150    }
151
152    /// Returns the pending operations currently in the queue.
153    pub fn pending_queue(&self) -> &[QueuedOperation] {
154        &self.pending_queue
155    }
156
157    /// Returns the number of pending operations.
158    pub fn pending_count(&self) -> usize {
159        self.pending_queue.len()
160    }
161
162    /// Mark an operation as running, recording it under `operation_id`.
163    pub fn start_operation(&mut self, operation: RunningOperation) {
164        self.running_operations
165            .insert(operation.operation_id.clone(), operation);
166    }
167
168    /// Look up a running operation by id.
169    pub fn get_running_operation(&self, operation_id: &str) -> Option<&RunningOperation> {
170        self.running_operations.get(operation_id)
171    }
172
173    /// Remove a running operation (e.g. when it finishes), returning it so
174    /// the caller can record completion.
175    pub fn remove_running_operation(&mut self, operation_id: &str) -> Option<RunningOperation> {
176        self.running_operations.remove(operation_id)
177    }
178
179    /// Returns the number of currently running operations.
180    pub fn running_count(&self) -> usize {
181        self.running_operations.len()
182    }
183
184    /// Record a completed operation.
185    pub fn record_completed(&mut self, operation: CompletedOperation) {
186        self.completed_operations.push(operation);
187    }
188
189    /// Returns the completed operations.
190    pub fn completed_operations(&self) -> &[CompletedOperation] {
191        &self.completed_operations
192    }
193
194    /// Returns the number of completed operations.
195    pub fn completed_count(&self) -> usize {
196        self.completed_operations.len()
197    }
198}
199
200impl LoadBalancer {
201    /// Creates a default `LoadBalancer`.
202    pub fn new() -> Self {
203        Self {
204            balancing_strategy: BalancingStrategy::LoadBased,
205            unit_metrics: HashMap::new(),
206        }
207    }
208
209    /// Returns the current balancing strategy.
210    pub fn balancing_strategy(&self) -> &BalancingStrategy {
211        &self.balancing_strategy
212    }
213
214    /// Set the balancing strategy.
215    pub fn set_balancing_strategy(&mut self, strategy: BalancingStrategy) {
216        self.balancing_strategy = strategy;
217    }
218
219    /// Record or update metrics for a computation unit.
220    pub fn set_unit_metrics(&mut self, unit_id: &str, metrics: UnitMetrics) {
221        self.unit_metrics.insert(unit_id.to_string(), metrics);
222    }
223
224    /// Look up metrics for a computation unit.
225    pub fn get_unit_metrics(&self, unit_id: &str) -> Option<&UnitMetrics> {
226        self.unit_metrics.get(unit_id)
227    }
228
229    /// Remove metrics for a computation unit.
230    pub fn remove_unit_metrics(&mut self, unit_id: &str) -> Option<UnitMetrics> {
231        self.unit_metrics.remove(unit_id)
232    }
233
234    /// Returns the number of units with recorded metrics.
235    pub fn tracked_unit_count(&self) -> usize {
236        self.unit_metrics.len()
237    }
238}