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    pub fn new() -> Self {
83        Self {
84            scheduling_policy: SchedulingPolicy::Priority,
85            queue_manager: QueueManager::new(),
86            load_balancer: LoadBalancer::new(),
87        }
88    }
89
90    pub fn initialize(&mut self) -> Result<(), StatisticalError> {
91        Ok(())
92    }
93
94    /// Returns the current scheduling policy.
95    pub fn scheduling_policy(&self) -> &SchedulingPolicy {
96        &self.scheduling_policy
97    }
98
99    /// Set the scheduling policy.
100    pub fn set_scheduling_policy(&mut self, policy: SchedulingPolicy) {
101        self.scheduling_policy = policy;
102    }
103
104    /// Returns a reference to the queue manager.
105    pub fn queue_manager(&self) -> &QueueManager {
106        &self.queue_manager
107    }
108
109    /// Returns a mutable reference to the queue manager.
110    pub fn queue_manager_mut(&mut self) -> &mut QueueManager {
111        &mut self.queue_manager
112    }
113
114    /// Returns a reference to the load balancer.
115    pub fn load_balancer(&self) -> &LoadBalancer {
116        &self.load_balancer
117    }
118
119    /// Returns a mutable reference to the load balancer.
120    pub fn load_balancer_mut(&mut self) -> &mut LoadBalancer {
121        &mut self.load_balancer
122    }
123}
124
125impl QueueManager {
126    pub fn new() -> Self {
127        Self {
128            pending_queue: Vec::new(),
129            running_operations: HashMap::new(),
130            completed_operations: Vec::new(),
131        }
132    }
133
134    /// Enqueue a pending operation.
135    pub fn enqueue(&mut self, operation: QueuedOperation) {
136        self.pending_queue.push(operation);
137    }
138
139    /// Dequeue the next pending operation (FIFO order). Returns `None` when
140    /// the queue is empty.
141    pub fn dequeue(&mut self) -> Option<QueuedOperation> {
142        if self.pending_queue.is_empty() {
143            None
144        } else {
145            Some(self.pending_queue.remove(0))
146        }
147    }
148
149    /// Returns the pending operations currently in the queue.
150    pub fn pending_queue(&self) -> &[QueuedOperation] {
151        &self.pending_queue
152    }
153
154    /// Returns the number of pending operations.
155    pub fn pending_count(&self) -> usize {
156        self.pending_queue.len()
157    }
158
159    /// Mark an operation as running, recording it under `operation_id`.
160    pub fn start_operation(&mut self, operation: RunningOperation) {
161        self.running_operations
162            .insert(operation.operation_id.clone(), operation);
163    }
164
165    /// Look up a running operation by id.
166    pub fn get_running_operation(&self, operation_id: &str) -> Option<&RunningOperation> {
167        self.running_operations.get(operation_id)
168    }
169
170    /// Remove a running operation (e.g. when it finishes), returning it so
171    /// the caller can record completion.
172    pub fn remove_running_operation(&mut self, operation_id: &str) -> Option<RunningOperation> {
173        self.running_operations.remove(operation_id)
174    }
175
176    /// Returns the number of currently running operations.
177    pub fn running_count(&self) -> usize {
178        self.running_operations.len()
179    }
180
181    /// Record a completed operation.
182    pub fn record_completed(&mut self, operation: CompletedOperation) {
183        self.completed_operations.push(operation);
184    }
185
186    /// Returns the completed operations.
187    pub fn completed_operations(&self) -> &[CompletedOperation] {
188        &self.completed_operations
189    }
190
191    /// Returns the number of completed operations.
192    pub fn completed_count(&self) -> usize {
193        self.completed_operations.len()
194    }
195}
196
197impl LoadBalancer {
198    pub fn new() -> Self {
199        Self {
200            balancing_strategy: BalancingStrategy::LoadBased,
201            unit_metrics: HashMap::new(),
202        }
203    }
204
205    /// Returns the current balancing strategy.
206    pub fn balancing_strategy(&self) -> &BalancingStrategy {
207        &self.balancing_strategy
208    }
209
210    /// Set the balancing strategy.
211    pub fn set_balancing_strategy(&mut self, strategy: BalancingStrategy) {
212        self.balancing_strategy = strategy;
213    }
214
215    /// Record or update metrics for a computation unit.
216    pub fn set_unit_metrics(&mut self, unit_id: &str, metrics: UnitMetrics) {
217        self.unit_metrics.insert(unit_id.to_string(), metrics);
218    }
219
220    /// Look up metrics for a computation unit.
221    pub fn get_unit_metrics(&self, unit_id: &str) -> Option<&UnitMetrics> {
222        self.unit_metrics.get(unit_id)
223    }
224
225    /// Remove metrics for a computation unit.
226    pub fn remove_unit_metrics(&mut self, unit_id: &str) -> Option<UnitMetrics> {
227        self.unit_metrics.remove(unit_id)
228    }
229
230    /// Returns the number of units with recorded metrics.
231    pub fn tracked_unit_count(&self) -> usize {
232        self.unit_metrics.len()
233    }
234}