use crate::scheduler::{effective_cost, Block, Inputs};
use crate::state::Job;
use std::collections::{HashMap, HashSet};
pub trait Gate<'a> {
fn blocks(&self, w: &'a Job, active: bool) -> Option<Block>;
fn admit(&mut self, w: &'a Job);
}
struct Points {
used: u32,
budget: u32,
}
impl<'a> Gate<'a> for Points {
fn blocks(&self, w: &'a Job, _: bool) -> Option<Block> {
let needs = effective_cost(w.cost, self.budget);
(self.used + needs > self.budget).then_some(Block::Points {
used: self.used,
needs,
})
}
fn admit(&mut self, w: &'a Job) {
self.used += effective_cost(w.cost, self.budget);
}
}
struct Keys<'a> {
held: HashSet<&'a str>,
}
impl<'a> Gate<'a> for Keys<'a> {
fn blocks(&self, w: &'a Job, _: bool) -> Option<Block> {
w.keys
.iter()
.any(|k| self.held.contains(k.as_str()))
.then_some(Block::Key)
}
fn admit(&mut self, w: &'a Job) {
self.held.extend(w.keys.iter().map(String::as_str));
}
}
struct Resources<'a> {
held: HashMap<&'a str, u32>,
caps: &'a std::collections::BTreeMap<String, u32>,
}
impl<'a> Gate<'a> for Resources<'a> {
fn blocks(&self, w: &'a Job, _: bool) -> Option<Block> {
w.needs
.iter()
.find(|n| {
self.held.get(n.as_str()).copied().unwrap_or(0) >= crate::scheduler::capacity(self.caps, n)
})
.map(|n| Block::Resource(n.clone()))
}
fn admit(&mut self, w: &'a Job) {
for n in &w.needs {
*self.held.entry(n.as_str()).or_default() += 1;
}
}
}
struct Disk;
impl<'a> Gate<'a> for Disk {
fn blocks(&self, w: &'a Job, active: bool) -> Option<Block> {
(active && w.disk_short).then_some(Block::Disk)
}
fn admit(&mut self, _: &'a Job) {}
}
struct Ram {
free: u64,
needs: u64,
per_point: u64,
budget: u32,
}
impl<'a> Gate<'a> for Ram {
fn blocks(&self, _: &'a Job, active: bool) -> Option<Block> {
(active && self.needs > 0 && self.free < self.needs).then_some(Block::Ram {
free: self.free,
needs: self.needs,
})
}
fn admit(&mut self, w: &'a Job) {
let take = self.per_point * effective_cost(w.cost, self.budget) as u64;
self.free = self.free.saturating_sub(take);
}
}
pub fn standard<'a>(jobs: &'a [Job], inputs: &Inputs<'a>) -> Vec<Box<dyn Gate<'a> + 'a>> {
let running = || jobs.iter().filter(|j| j.running());
let mut keys = Keys { held: HashSet::new() };
let mut resources = Resources {
held: HashMap::new(),
caps: inputs.caps,
};
for j in running() {
keys.admit(j);
resources.admit(j);
}
vec![
Box::new(Points {
used: crate::scheduler::used_points(jobs, inputs.limits.budget),
budget: inputs.limits.budget,
}),
Box::new(keys),
Box::new(resources),
Box::new(Disk),
Box::new(Ram {
free: inputs
.free_ram
.saturating_sub(crate::scheduler::recent_reservation(
jobs,
inputs.now_ms,
&inputs.limits,
)),
needs: inputs.limits.min_free_bytes,
per_point: inputs.limits.ram_per_point,
budget: inputs.limits.budget,
}),
]
}