Skip to main content

qualia_core_db/inference/ambient_orchestration/
scheduler.rs

1//! Task scheduling: policy-driven task queue and execution history.
2
3use super::*;
4use std::time::Instant;
5
6/// Task scheduler
7pub struct TaskScheduler {
8    scheduling_policy: SchedulingPolicy,
9    task_queue: TaskQueue,
10    execution_history: Vec<TaskExecutionRecord>,
11}
12
13/// Task queue
14pub struct TaskQueue {
15    pending_tasks: Vec<Task>,
16    running_tasks: Vec<Task>,
17    completed_tasks: Vec<Task>,
18}
19
20impl TaskScheduler {
21    /// Create new task scheduler
22    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    /// Submit task
31    pub fn submit_task(&mut self, task: Task) -> Result<(), AmbientError> {
32        self.task_queue.pending_tasks.push(task);
33        // Sort pending tasks according to the scheduling policy.
34        self.sort_pending();
35        Ok(())
36    }
37
38    /// Sort pending tasks according to the current scheduling policy.
39    fn sort_pending(&mut self) {
40        match self.scheduling_policy {
41            SchedulingPolicy::Fifo => {
42                // FIFO: keep insertion order (no sort needed).
43            }
44            SchedulingPolicy::Priority => {
45                // Priority: highest priority first.
46                self.task_queue
47                    .pending_tasks
48                    .sort_by(|a, b| b.priority.cmp(&a.priority));
49            }
50            SchedulingPolicy::ShortestJobFirst => {
51                // SJF: shortest estimated duration first.
52                self.task_queue
53                    .pending_tasks
54                    .sort_by(|a, b| a.estimated_duration.cmp(&b.estimated_duration));
55            }
56            SchedulingPolicy::Deadline => {
57                // Deadline: earliest deadline first (tasks without deadlines go last).
58                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                // Adaptive: priority first, then shortest job as tiebreaker.
69                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    /// Get pending tasks
79    pub fn get_pending_tasks(&self) -> Vec<Task> {
80        self.task_queue.pending_tasks.clone()
81    }
82
83    /// Gets `pending-tasks-into`.
84    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    /// Dispatch the next pending task to a device, moving it to running.
105    /// Returns the dispatched task, or `None` if no tasks are pending.
106    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    /// Mark a running task as completed, recording it in execution history.
116    pub fn complete_task(
117        &mut self,
118        task_id: &str,
119        device_id: &str,
120        success: bool,
121        usage: ResourceUsage,
122    ) {
123        // Remove from running tasks.
124        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            // Record in execution history.
134            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            // Trim history.
145            if self.execution_history.len() > 500 {
146                let drop = self.execution_history.len() - 500;
147                self.execution_history.drain(0..drop);
148            }
149            // Trim completed tasks.
150            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    /// Get the number of currently running tasks.
158    pub fn running_count(&self) -> usize {
159        self.task_queue.running_tasks.len()
160    }
161
162    /// Get the number of completed tasks.
163    pub fn completed_count(&self) -> usize {
164        self.task_queue.completed_tasks.len()
165    }
166
167    /// Get recent execution history records.
168    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    /// Set the scheduling policy.
174    pub fn set_policy(&mut self, policy: SchedulingPolicy) {
175        self.scheduling_policy = policy;
176        self.sort_pending();
177    }
178}
179
180impl TaskQueue {
181    /// Creates a default `TaskQueue`.
182    pub fn new() -> Self {
183        Self {
184            pending_tasks: Vec::new(),
185            running_tasks: Vec::new(),
186            completed_tasks: Vec::new(),
187        }
188    }
189}