qualia_core_db/inference/ambient_orchestration/
scheduler.rs1use super::*;
4use std::time::Instant;
5
6pub struct TaskScheduler {
8 scheduling_policy: SchedulingPolicy,
9 task_queue: TaskQueue,
10 execution_history: Vec<TaskExecutionRecord>,
11}
12
13pub struct TaskQueue {
15 pending_tasks: Vec<Task>,
16 running_tasks: Vec<Task>,
17 completed_tasks: Vec<Task>,
18}
19
20impl TaskScheduler {
21 pub fn new() -> Self {
23 Self {
24 scheduling_policy: SchedulingPolicy::Adaptive,
25 task_queue: TaskQueue::new(),
26 execution_history: Vec::new(),
27 }
28 }
29
30 pub fn submit_task(&mut self, task: Task) -> Result<(), AmbientError> {
32 self.task_queue.pending_tasks.push(task);
33 self.sort_pending();
35 Ok(())
36 }
37
38 fn sort_pending(&mut self) {
40 match self.scheduling_policy {
41 SchedulingPolicy::Fifo => {
42 }
44 SchedulingPolicy::Priority => {
45 self.task_queue
47 .pending_tasks
48 .sort_by(|a, b| b.priority.cmp(&a.priority));
49 }
50 SchedulingPolicy::ShortestJobFirst => {
51 self.task_queue
53 .pending_tasks
54 .sort_by(|a, b| a.estimated_duration.cmp(&b.estimated_duration));
55 }
56 SchedulingPolicy::Deadline => {
57 self.task_queue
59 .pending_tasks
60 .sort_by(|a, b| match (a.deadline, b.deadline) {
61 (Some(da), Some(db)) => da.cmp(&db),
62 (Some(_), None) => std::cmp::Ordering::Less,
63 (None, Some(_)) => std::cmp::Ordering::Greater,
64 (None, None) => std::cmp::Ordering::Equal,
65 });
66 }
67 SchedulingPolicy::Adaptive => {
68 self.task_queue.pending_tasks.sort_by(|a, b| {
70 b.priority
71 .cmp(&a.priority)
72 .then_with(|| a.estimated_duration.cmp(&b.estimated_duration))
73 });
74 }
75 }
76 }
77
78 pub fn get_pending_tasks(&self) -> Vec<Task> {
80 self.task_queue.pending_tasks.clone()
81 }
82
83 pub fn get_pending_tasks_into(&self, out: &mut [TaskHandle]) -> Result<usize, AmbientError> {
85 if out.len() < self.task_queue.pending_tasks.len() {
86 return Err(AmbientError::InsufficientResources(
87 "task output buffer full".to_string(),
88 ));
89 }
90
91 for (index, task) in self.task_queue.pending_tasks.iter().enumerate() {
92 out[index] = TaskHandle {
93 task_id_hash: crate::q_hash(&task.task_id),
94 task_type: task.task_type.clone(),
95 priority: task.priority.clone(),
96 compute_units: task.resource_requirements.compute_units,
97 memory: task.resource_requirements.memory,
98 };
99 }
100
101 Ok(self.task_queue.pending_tasks.len())
102 }
103
104 pub fn dispatch_next(&mut self) -> Option<Task> {
107 if self.task_queue.pending_tasks.is_empty() {
108 return None;
109 }
110 let task = self.task_queue.pending_tasks.remove(0);
111 self.task_queue.running_tasks.push(task.clone());
112 Some(task)
113 }
114
115 pub fn complete_task(
117 &mut self,
118 task_id: &str,
119 device_id: &str,
120 success: bool,
121 usage: ResourceUsage,
122 ) {
123 if let Some(pos) = self
125 .task_queue
126 .running_tasks
127 .iter()
128 .position(|t| t.task_id == task_id)
129 {
130 let task = self.task_queue.running_tasks.remove(pos);
131 self.task_queue.completed_tasks.push(task.clone());
132
133 self.execution_history.push(TaskExecutionRecord {
135 task_id: task.task_id.clone(),
136 device_id: device_id.to_string(),
137 start_time: Instant::now() - task.estimated_duration,
138 end_time: Instant::now(),
139 actual_duration: task.estimated_duration,
140 success,
141 resource_usage: usage,
142 });
143
144 if self.execution_history.len() > 500 {
146 let drop = self.execution_history.len() - 500;
147 self.execution_history.drain(0..drop);
148 }
149 if self.task_queue.completed_tasks.len() > 200 {
151 let drop = self.task_queue.completed_tasks.len() - 200;
152 self.task_queue.completed_tasks.drain(0..drop);
153 }
154 }
155 }
156
157 pub fn running_count(&self) -> usize {
159 self.task_queue.running_tasks.len()
160 }
161
162 pub fn completed_count(&self) -> usize {
164 self.task_queue.completed_tasks.len()
165 }
166
167 pub fn recent_history(&self, n: usize) -> &[TaskExecutionRecord] {
169 let start = self.execution_history.len().saturating_sub(n);
170 &self.execution_history[start..]
171 }
172
173 pub fn set_policy(&mut self, policy: SchedulingPolicy) {
175 self.scheduling_policy = policy;
176 self.sort_pending();
177 }
178}
179
180impl TaskQueue {
181 pub fn new() -> Self {
183 Self {
184 pending_tasks: Vec::new(),
185 running_tasks: Vec::new(),
186 completed_tasks: Vec::new(),
187 }
188 }
189}