1use std::collections::{HashMap, VecDeque};
7use std::time::{Duration, Instant};
8
9use super::config::{ResourceAllocationStrategy, SchedulingPriority};
10use super::platform::QuantumPlatform;
11
12pub struct UniversalResourceScheduler {
14 pub config: SchedulerConfig,
16 pub queue: SchedulingQueue,
18 pub allocator: ResourceAllocator,
20 pub performance_tracker: PerformanceTracker,
22}
23
24impl UniversalResourceScheduler {
25 pub fn new() -> Self {
27 Self {
28 config: SchedulerConfig {
29 algorithm: SchedulingAlgorithm::Priority,
30 allocation_strategy: ResourceAllocationStrategy::CostEffective,
31 fairness_policy: FairnessPolicy::ProportionalShare,
32 load_balancing: LoadBalancingConfig {
33 enabled: true,
34 threshold: 0.8,
35 frequency: Duration::from_secs(60),
36 strategy: LoadBalancingStrategy::PerformanceBased,
37 },
38 },
39 queue: SchedulingQueue {
40 pending_jobs: VecDeque::new(),
41 running_jobs: HashMap::new(),
42 completed_jobs: VecDeque::new(),
43 statistics: QueueStatistics {
44 total_jobs: 0,
45 average_wait_time: Duration::from_secs(0),
46 average_execution_time: Duration::from_secs(0),
47 throughput: 0.0,
48 utilization: 0.0,
49 },
50 },
51 allocator: ResourceAllocator {
52 config: AllocatorConfig {
53 strategy: AllocationStrategy::Optimized,
54 constraints: AllocationConstraints {
55 max_utilization: 0.9,
56 reservations: vec![],
57 affinity_constraints: vec![],
58 },
59 objectives: AllocationObjectives {
60 primary: AllocationObjective::MaximizePerformance,
61 secondary: vec![(AllocationObjective::MinimizeCost, 0.3)],
62 },
63 },
64 available_resources: HashMap::new(),
65 allocation_history: VecDeque::new(),
66 },
67 performance_tracker: PerformanceTracker {
68 config: TrackerConfig {
69 collection_interval: Duration::from_secs(10),
70 retention_period: Duration::from_secs(86_400),
71 alerting: AlertingConfig {
72 enabled: true,
73 thresholds: HashMap::new(),
74 channels: vec![],
75 },
76 },
77 metrics: HashMap::new(),
78 historical_data: VecDeque::new(),
79 },
80 }
81 }
82}
83
84impl Default for UniversalResourceScheduler {
85 fn default() -> Self {
86 Self::new()
87 }
88}
89
90#[derive(Debug, Clone)]
92pub struct SchedulerConfig {
93 pub algorithm: SchedulingAlgorithm,
95 pub allocation_strategy: ResourceAllocationStrategy,
97 pub fairness_policy: FairnessPolicy,
99 pub load_balancing: LoadBalancingConfig,
101}
102
103#[derive(Debug, Clone, PartialEq)]
105pub enum SchedulingAlgorithm {
106 FCFS,
108 ShortestJobFirst,
110 Priority,
112 RoundRobin,
114 MultilevelFeedback,
116}
117
118#[derive(Debug, Clone, PartialEq)]
120pub enum FairnessPolicy {
121 EqualShare,
123 ProportionalShare,
125 WeightedFairSharing,
127 PriorityBased,
129}
130
131#[derive(Debug, Clone)]
133pub struct LoadBalancingConfig {
134 pub enabled: bool,
136 pub threshold: f64,
138 pub frequency: Duration,
140 pub strategy: LoadBalancingStrategy,
142}
143
144#[derive(Debug, Clone, PartialEq)]
146pub enum LoadBalancingStrategy {
147 RoundRobin,
149 LeastLoaded,
151 PerformanceBased,
153 CostBased,
155}
156
157#[derive(Debug)]
159pub struct SchedulingQueue {
160 pub pending_jobs: VecDeque<ScheduledJob>,
162 pub running_jobs: HashMap<String, RunningJob>,
164 pub completed_jobs: VecDeque<CompletedJob>,
166 pub statistics: QueueStatistics,
168}
169
170#[derive(Debug, Clone)]
172pub struct ScheduledJob {
173 pub job_id: String,
175 pub priority: SchedulingPriority,
177 pub estimated_execution_time: Duration,
179 pub resource_requirements: JobResourceRequirements,
181 pub submitted_at: Instant,
183}
184
185#[derive(Debug, Clone)]
187pub struct RunningJob {
188 pub job_id: String,
190 pub started_at: Instant,
192 pub allocated_resources: AllocatedResources,
194 pub platform: QuantumPlatform,
196}
197
198#[derive(Debug, Clone)]
200pub struct CompletedJob {
201 pub job_id: String,
203 pub completed_at: Instant,
205 pub status: JobStatus,
207 pub execution_time: Duration,
209 pub wait_time: Duration,
211}
212
213#[derive(Debug, Clone)]
215pub struct JobResourceRequirements {
216 pub min_qubits: usize,
218 pub preferred_qubits: Option<usize>,
220 pub memory_mb: usize,
222 pub expected_duration: Duration,
224}
225
226#[derive(Debug, Clone)]
228pub struct AllocatedResources {
229 pub qubits: Vec<usize>,
231 pub memory_mb: usize,
233 pub time_slot: TimeSlot,
235}
236
237#[derive(Debug, Clone, PartialEq)]
239pub enum JobStatus {
240 Pending,
242 Queued,
244 Running,
246 Completed,
248 Failed,
250 Cancelled,
252 TimedOut,
254}
255
256#[derive(Debug, Clone)]
258pub struct QueueStatistics {
259 pub total_jobs: u64,
261 pub average_wait_time: Duration,
263 pub average_execution_time: Duration,
265 pub throughput: f64,
267 pub utilization: f64,
269}
270
271#[derive(Debug)]
273pub struct ResourceAllocator {
274 pub config: AllocatorConfig,
276 pub available_resources: HashMap<QuantumPlatform, AvailableResources>,
278 pub allocation_history: VecDeque<AllocationRecord>,
280}
281
282#[derive(Debug, Clone)]
284pub struct AllocatorConfig {
285 pub strategy: AllocationStrategy,
287 pub constraints: AllocationConstraints,
289 pub objectives: AllocationObjectives,
291}
292
293#[derive(Debug, Clone, PartialEq)]
295pub enum AllocationStrategy {
296 FirstFit,
298 BestFit,
300 WorstFit,
302 Optimized,
304}
305
306#[derive(Debug, Clone)]
308pub struct AllocationConstraints {
309 pub max_utilization: f64,
311 pub reservations: Vec<ResourceReservation>,
313 pub affinity_constraints: Vec<AffinityConstraint>,
315}
316
317#[derive(Debug, Clone)]
319pub struct ResourceReservation {
320 pub reservation_id: String,
322 pub resources: ReservedResources,
324 pub start_time: Instant,
326 pub duration: Duration,
328}
329
330#[derive(Debug, Clone)]
332pub struct ReservedResources {
333 pub qubits: Vec<usize>,
335 pub memory_mb: usize,
337}
338
339#[derive(Debug, Clone)]
341pub struct TimeSlot {
342 pub start_time: Instant,
344 pub end_time: Instant,
346}
347
348#[derive(Debug, Clone)]
350pub struct AffinityConstraint {
351 pub target: QuantumPlatform,
353 pub affinity_type: AffinityType,
355 pub strength: AffinityStrength,
357}
358
359#[derive(Debug, Clone, PartialEq)]
361pub enum AffinityType {
362 Required,
364 Preferred,
366 Avoid,
368}
369
370#[derive(Debug, Clone, PartialEq)]
372pub enum AffinityStrength {
373 Weak,
375 Medium,
377 Strong,
379}
380
381#[derive(Debug, Clone)]
383pub struct AllocationObjectives {
384 pub primary: AllocationObjective,
386 pub secondary: Vec<(AllocationObjective, f64)>,
388}
389
390#[derive(Debug, Clone, PartialEq)]
392pub enum AllocationObjective {
393 MaximizePerformance,
395 MinimizeCost,
397 MinimizeWaitTime,
399 MaximizeUtilization,
401 BalanceLoad,
403}
404
405#[derive(Debug, Clone)]
407pub struct AvailableResources {
408 pub platform: QuantumPlatform,
410 pub capacity: ResourceCapacity,
412 pub current_load: ResourceLoad,
414}
415
416#[derive(Debug, Clone)]
418pub struct ResourceCapacity {
419 pub total_qubits: usize,
421 pub total_memory_mb: usize,
423 pub max_concurrent_jobs: usize,
425}
426
427#[derive(Debug, Clone)]
429pub struct ResourceLoad {
430 pub used_qubits: usize,
432 pub used_memory_mb: usize,
434 pub active_jobs: usize,
436}
437
438#[derive(Debug, Clone)]
440pub struct AllocationRecord {
441 pub job_id: String,
443 pub platform: QuantumPlatform,
445 pub resources: AllocatedResources,
447 pub allocated_at: Instant,
449}
450
451#[derive(Debug)]
453pub struct PerformanceTracker {
454 pub config: TrackerConfig,
456 pub metrics: HashMap<String, MetricValue>,
458 pub historical_data: VecDeque<PerformanceSnapshot>,
460}
461
462#[derive(Debug, Clone)]
464pub struct TrackerConfig {
465 pub collection_interval: Duration,
467 pub retention_period: Duration,
469 pub alerting: AlertingConfig,
471}
472
473#[derive(Debug, Clone)]
475pub struct AlertingConfig {
476 pub enabled: bool,
478 pub thresholds: HashMap<String, f64>,
480 pub channels: Vec<AlertChannel>,
482}
483
484#[derive(Debug, Clone)]
486pub struct AlertChannel {
487 pub name: String,
489 pub channel_type: AlertChannelType,
491}
492
493#[derive(Debug, Clone, PartialEq)]
495pub enum AlertChannelType {
496 Email,
498 Slack,
500 PagerDuty,
502 Webhook,
504 Log,
506}
507
508#[derive(Debug, Clone)]
510pub enum MetricValue {
511 Counter(u64),
513 Gauge(f64),
515 Histogram(Vec<f64>),
517 Summary { count: u64, sum: f64 },
519}
520
521#[derive(Debug, Clone)]
523pub struct PerformanceSnapshot {
524 pub timestamp: Instant,
526 pub platform_metrics: HashMap<QuantumPlatform, PlatformMetrics>,
528}
529
530#[derive(Debug, Clone)]
532pub struct PlatformMetrics {
533 pub success_rate: f64,
535 pub avg_execution_time: Duration,
537 pub queue_length: usize,
539 pub utilization: f64,
541}
542
543#[derive(Debug, Clone)]
545pub struct SystemState {
546 pub queue_lengths: HashMap<QuantumPlatform, usize>,
548 pub resource_utilization: HashMap<QuantumPlatform, f64>,
550}