use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};
use super::config::{ResourceAllocationStrategy, SchedulingPriority};
use super::platform::QuantumPlatform;
pub struct UniversalResourceScheduler {
pub config: SchedulerConfig,
pub queue: SchedulingQueue,
pub allocator: ResourceAllocator,
pub performance_tracker: PerformanceTracker,
}
impl UniversalResourceScheduler {
pub fn new() -> Self {
Self {
config: SchedulerConfig {
algorithm: SchedulingAlgorithm::Priority,
allocation_strategy: ResourceAllocationStrategy::CostEffective,
fairness_policy: FairnessPolicy::ProportionalShare,
load_balancing: LoadBalancingConfig {
enabled: true,
threshold: 0.8,
frequency: Duration::from_secs(60),
strategy: LoadBalancingStrategy::PerformanceBased,
},
},
queue: SchedulingQueue {
pending_jobs: VecDeque::new(),
running_jobs: HashMap::new(),
completed_jobs: VecDeque::new(),
statistics: QueueStatistics {
total_jobs: 0,
average_wait_time: Duration::from_secs(0),
average_execution_time: Duration::from_secs(0),
throughput: 0.0,
utilization: 0.0,
},
},
allocator: ResourceAllocator {
config: AllocatorConfig {
strategy: AllocationStrategy::Optimized,
constraints: AllocationConstraints {
max_utilization: 0.9,
reservations: vec![],
affinity_constraints: vec![],
},
objectives: AllocationObjectives {
primary: AllocationObjective::MaximizePerformance,
secondary: vec![(AllocationObjective::MinimizeCost, 0.3)],
},
},
available_resources: HashMap::new(),
allocation_history: VecDeque::new(),
},
performance_tracker: PerformanceTracker {
config: TrackerConfig {
collection_interval: Duration::from_secs(10),
retention_period: Duration::from_secs(86_400),
alerting: AlertingConfig {
enabled: true,
thresholds: HashMap::new(),
channels: vec![],
},
},
metrics: HashMap::new(),
historical_data: VecDeque::new(),
},
}
}
}
impl Default for UniversalResourceScheduler {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct SchedulerConfig {
pub algorithm: SchedulingAlgorithm,
pub allocation_strategy: ResourceAllocationStrategy,
pub fairness_policy: FairnessPolicy,
pub load_balancing: LoadBalancingConfig,
}
#[derive(Debug, Clone, PartialEq)]
pub enum SchedulingAlgorithm {
FCFS,
ShortestJobFirst,
Priority,
RoundRobin,
MultilevelFeedback,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FairnessPolicy {
EqualShare,
ProportionalShare,
WeightedFairSharing,
PriorityBased,
}
#[derive(Debug, Clone)]
pub struct LoadBalancingConfig {
pub enabled: bool,
pub threshold: f64,
pub frequency: Duration,
pub strategy: LoadBalancingStrategy,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LoadBalancingStrategy {
RoundRobin,
LeastLoaded,
PerformanceBased,
CostBased,
}
#[derive(Debug)]
pub struct SchedulingQueue {
pub pending_jobs: VecDeque<ScheduledJob>,
pub running_jobs: HashMap<String, RunningJob>,
pub completed_jobs: VecDeque<CompletedJob>,
pub statistics: QueueStatistics,
}
#[derive(Debug, Clone)]
pub struct ScheduledJob {
pub job_id: String,
pub priority: SchedulingPriority,
pub estimated_execution_time: Duration,
pub resource_requirements: JobResourceRequirements,
pub submitted_at: Instant,
}
#[derive(Debug, Clone)]
pub struct RunningJob {
pub job_id: String,
pub started_at: Instant,
pub allocated_resources: AllocatedResources,
pub platform: QuantumPlatform,
}
#[derive(Debug, Clone)]
pub struct CompletedJob {
pub job_id: String,
pub completed_at: Instant,
pub status: JobStatus,
pub execution_time: Duration,
pub wait_time: Duration,
}
#[derive(Debug, Clone)]
pub struct JobResourceRequirements {
pub min_qubits: usize,
pub preferred_qubits: Option<usize>,
pub memory_mb: usize,
pub expected_duration: Duration,
}
#[derive(Debug, Clone)]
pub struct AllocatedResources {
pub qubits: Vec<usize>,
pub memory_mb: usize,
pub time_slot: TimeSlot,
}
#[derive(Debug, Clone, PartialEq)]
pub enum JobStatus {
Pending,
Queued,
Running,
Completed,
Failed,
Cancelled,
TimedOut,
}
#[derive(Debug, Clone)]
pub struct QueueStatistics {
pub total_jobs: u64,
pub average_wait_time: Duration,
pub average_execution_time: Duration,
pub throughput: f64,
pub utilization: f64,
}
#[derive(Debug)]
pub struct ResourceAllocator {
pub config: AllocatorConfig,
pub available_resources: HashMap<QuantumPlatform, AvailableResources>,
pub allocation_history: VecDeque<AllocationRecord>,
}
#[derive(Debug, Clone)]
pub struct AllocatorConfig {
pub strategy: AllocationStrategy,
pub constraints: AllocationConstraints,
pub objectives: AllocationObjectives,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AllocationStrategy {
FirstFit,
BestFit,
WorstFit,
Optimized,
}
#[derive(Debug, Clone)]
pub struct AllocationConstraints {
pub max_utilization: f64,
pub reservations: Vec<ResourceReservation>,
pub affinity_constraints: Vec<AffinityConstraint>,
}
#[derive(Debug, Clone)]
pub struct ResourceReservation {
pub reservation_id: String,
pub resources: ReservedResources,
pub start_time: Instant,
pub duration: Duration,
}
#[derive(Debug, Clone)]
pub struct ReservedResources {
pub qubits: Vec<usize>,
pub memory_mb: usize,
}
#[derive(Debug, Clone)]
pub struct TimeSlot {
pub start_time: Instant,
pub end_time: Instant,
}
#[derive(Debug, Clone)]
pub struct AffinityConstraint {
pub target: QuantumPlatform,
pub affinity_type: AffinityType,
pub strength: AffinityStrength,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AffinityType {
Required,
Preferred,
Avoid,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AffinityStrength {
Weak,
Medium,
Strong,
}
#[derive(Debug, Clone)]
pub struct AllocationObjectives {
pub primary: AllocationObjective,
pub secondary: Vec<(AllocationObjective, f64)>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AllocationObjective {
MaximizePerformance,
MinimizeCost,
MinimizeWaitTime,
MaximizeUtilization,
BalanceLoad,
}
#[derive(Debug, Clone)]
pub struct AvailableResources {
pub platform: QuantumPlatform,
pub capacity: ResourceCapacity,
pub current_load: ResourceLoad,
}
#[derive(Debug, Clone)]
pub struct ResourceCapacity {
pub total_qubits: usize,
pub total_memory_mb: usize,
pub max_concurrent_jobs: usize,
}
#[derive(Debug, Clone)]
pub struct ResourceLoad {
pub used_qubits: usize,
pub used_memory_mb: usize,
pub active_jobs: usize,
}
#[derive(Debug, Clone)]
pub struct AllocationRecord {
pub job_id: String,
pub platform: QuantumPlatform,
pub resources: AllocatedResources,
pub allocated_at: Instant,
}
#[derive(Debug)]
pub struct PerformanceTracker {
pub config: TrackerConfig,
pub metrics: HashMap<String, MetricValue>,
pub historical_data: VecDeque<PerformanceSnapshot>,
}
#[derive(Debug, Clone)]
pub struct TrackerConfig {
pub collection_interval: Duration,
pub retention_period: Duration,
pub alerting: AlertingConfig,
}
#[derive(Debug, Clone)]
pub struct AlertingConfig {
pub enabled: bool,
pub thresholds: HashMap<String, f64>,
pub channels: Vec<AlertChannel>,
}
#[derive(Debug, Clone)]
pub struct AlertChannel {
pub name: String,
pub channel_type: AlertChannelType,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AlertChannelType {
Email,
Slack,
PagerDuty,
Webhook,
Log,
}
#[derive(Debug, Clone)]
pub enum MetricValue {
Counter(u64),
Gauge(f64),
Histogram(Vec<f64>),
Summary { count: u64, sum: f64 },
}
#[derive(Debug, Clone)]
pub struct PerformanceSnapshot {
pub timestamp: Instant,
pub platform_metrics: HashMap<QuantumPlatform, PlatformMetrics>,
}
#[derive(Debug, Clone)]
pub struct PlatformMetrics {
pub success_rate: f64,
pub avg_execution_time: Duration,
pub queue_length: usize,
pub utilization: f64,
}
#[derive(Debug, Clone)]
pub struct SystemState {
pub queue_lengths: HashMap<QuantumPlatform, usize>,
pub resource_utilization: HashMap<QuantumPlatform, f64>,
}