use super::owner::{self, Owner};
use super::schedule::{self, LocalTime};
use super::{Atelier, Live, MinPrice, Policy, PUBLIC_SQUAD};
use crate::sandbox::{Allocation, Gpus, HostResources, Limits};
use serde::Serialize;
use serde_json::{json, Value};
use std::collections::{BTreeMap, VecDeque};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Source {
PeerOffer,
SpacesLease,
HubAllocation,
EnvGuest,
AtelierJob,
}
impl Source {
pub fn as_str(&self) -> &'static str {
match self {
Source::PeerOffer => "peer_offer",
Source::SpacesLease => "spaces_lease",
Source::HubAllocation => "hub_allocation",
Source::EnvGuest => "env_guest",
Source::AtelierJob => "atelier_job",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Workload {
Function,
Environment,
Space,
HubServing,
Atelier {
backend: Option<String>,
gpu: bool,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Reason {
Squad,
Workload,
Budget,
Price,
Schedule,
Idle,
}
impl Reason {
pub const ALL: [Reason; 6] = [
Reason::Squad,
Reason::Workload,
Reason::Budget,
Reason::Price,
Reason::Schedule,
Reason::Idle,
];
pub fn as_str(&self) -> &'static str {
match self {
Reason::Squad => "squad",
Reason::Workload => "workload",
Reason::Budget => "budget",
Reason::Price => "price",
Reason::Schedule => "schedule",
Reason::Idle => "idle",
}
}
}
#[derive(Debug, Clone)]
pub struct Request {
pub source: Source,
pub squad: Option<String>,
pub own: bool,
pub workload: Workload,
pub cpus: f64,
pub memory_bytes: u64,
pub gpus: u32,
pub price_per_hour: Option<f64>,
}
impl Request {
pub fn new(source: Source, workload: Workload) -> Self {
Self {
source,
squad: None,
own: false,
workload,
cpus: 0.0,
memory_bytes: 0,
gpus: 0,
price_per_hour: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Refusal {
pub reason: Reason,
pub detail: String,
pub source: Source,
pub squad: String,
pub at: u64,
}
impl std::fmt::Display for Refusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "refused ({}): {}", self.reason.as_str(), self.detail)
}
}
pub struct BudgetView<'a> {
pub total: &'a Limits,
pub running: &'a [Allocation],
}
const MIN_UNIT_CPU_MILLIS: u64 = 1000;
const MIN_UNIT_MEMORY: u64 = 1 << 30;
fn refuse(req: &Request, reason: Reason, detail: String, now_secs: u64) -> Refusal {
Refusal {
reason,
detail,
source: req.source,
squad: req
.squad
.clone()
.unwrap_or_else(|| PUBLIC_SQUAD.to_string()),
at: now_secs,
}
}
pub fn admit(
policy: &Policy,
req: &Request,
now: LocalTime,
budget: Option<&BudgetView>,
now_secs: u64,
) -> Result<(), Refusal> {
admit_with(policy, req, now, budget, now_secs, Owner::NoSignal)
}
pub fn admit_with(
policy: &Policy,
req: &Request,
now: LocalTime,
budget: Option<&BudgetView>,
now_secs: u64,
owner: Owner,
) -> Result<(), Refusal> {
let no = |reason, detail: String| Err(refuse(req, reason, detail, now_secs));
if !req.own {
let squad = req.squad.as_deref().unwrap_or(PUBLIC_SQUAD);
if !policy.sharing.squads.iter().any(|s| s == squad) {
return no(
Reason::Squad,
if policy.sharing.squads.is_empty() {
"this node serves no squad (sharing.squads is empty)".to_string()
} else {
format!("squad {squad:?} is not in sharing.squads")
},
);
}
}
let gpu_granted = match budget {
Some(b) => b.total.gpus != Gpus::None,
None => policy.budget.gpu,
};
match &req.workload {
Workload::Function => {}
Workload::Environment if !policy.workloads.environments => {
return no(Reason::Workload, "environments are switched off".into())
}
Workload::Space if !policy.workloads.spaces => {
return no(Reason::Workload, "Spaces are switched off".into())
}
Workload::HubServing if !policy.workloads.hub_serving => {
return no(Reason::Workload, "hub serving is switched off".into())
}
Workload::Atelier { backend, gpu } => match &policy.workloads.atelier {
Atelier::Off => return no(Reason::Workload, "Atelier is switched off".into()),
Atelier::GpuOnly if !gpu_granted => {
return no(
Reason::Workload,
"Atelier is gpu-only and the budget grants no GPU".into(),
)
}
Atelier::GpuOnly if !gpu => {
return no(
Reason::Workload,
"Atelier is gpu-only: this job uses no GPU".into(),
)
}
Atelier::Backends(list) => {
if let Some(b) = backend {
if !list.contains(b) {
return no(
Reason::Workload,
format!("Atelier backend {b:?} is not enabled"),
);
}
}
}
_ => {}
},
_ => {}
}
if !req.own
&& policy.sharing.when == "schedule"
&& !schedule::is_open(&policy.sharing.schedule, now)
{
return no(
Reason::Schedule,
"outside every window of sharing.schedule".into(),
);
}
if !req.own {
if let Some(why) = owner.blocks(&policy.sharing.when) {
return no(Reason::Idle, format!("the owner is not away ({why})"));
}
}
if !req.own {
match (policy.sharing.min_price, req.price_per_hour) {
(MinPrice::Withdrawn, _) => {
return no(Reason::Price, "sharing.min_price is -1 (withdrawn)".into())
}
(MinPrice::PerHour(floor), Some(p)) if p < floor => {
return no(
Reason::Price,
format!("{p} yen/hour is below sharing.min_price {floor}"),
)
}
_ => {}
}
}
if let Some(b) = budget {
if req.gpus > 0 && !gpu_granted && !(req.source == Source::PeerOffer && req.own) {
return no(
Reason::Budget,
"it needs a GPU and budget.gpu is off".into(),
);
}
let sum = |f: fn(&Allocation) -> u64| b.running.iter().map(f).sum::<u64>();
let (cpu_floor, mem_floor) = if b.running.is_empty() {
(MIN_UNIT_CPU_MILLIS, MIN_UNIT_MEMORY)
} else {
(0, 0)
};
let cpu_left = (b.total.cpu_millis as u64)
.saturating_sub(sum(|a| a.cpu_millis as u64))
.max(cpu_floor);
let mem_left = b
.total
.memory_bytes
.saturating_sub(sum(|a| a.memory_bytes))
.max(mem_floor);
let want_cpu = (req.cpus * 1000.0).round().max(0.0) as u64;
if want_cpu > cpu_left {
return no(
Reason::Budget,
format!("cpu: asks {} of the {} millicores left", want_cpu, cpu_left),
);
}
if req.memory_bytes > mem_left {
return no(
Reason::Budget,
format!(
"memory: asks {} MiB of the {} MiB left",
req.memory_bytes >> 20,
mem_left >> 20
),
);
}
}
Ok(())
}
const RECENT: usize = 20;
const PERSIST_EVERY: Duration = Duration::from_secs(1);
const SNAPSHOT_FILE: &str = "admission.json";
#[derive(Default)]
struct Stats {
admitted: u64,
refused: BTreeMap<Reason, u64>,
recent: VecDeque<Refusal>,
}
struct Inner {
live: Live,
stats: Mutex<Stats>,
persist: Option<PathBuf>,
last_persist: Mutex<Option<Instant>>,
fixed_budget: Mutex<Option<Limits>>,
owner_home: Option<PathBuf>,
owner_cache: Mutex<Option<(Instant, Owner)>>,
fixed_owner: Mutex<Option<Owner>>,
}
const OWNER_CACHE: Duration = Duration::from_millis(250);
#[derive(Clone)]
pub struct Admission(Arc<Inner>);
impl Admission {
pub fn process() -> Self {
static P: OnceLock<Admission> = OnceLock::new();
P.get_or_init(|| {
Admission(Arc::new(Inner {
live: Live::process(),
stats: Mutex::default(),
persist: Some(PathBuf::new()),
last_persist: Mutex::new(None),
fixed_budget: Mutex::new(if cfg!(test) {
Some(Limits::new(64.0, 256.0, Gpus::All))
} else {
None
}),
owner_home: None,
owner_cache: Mutex::new(None),
fixed_owner: Mutex::new(if cfg!(test) { Some(Owner::Calm) } else { None }),
}))
})
.clone()
}
pub fn at(home: PathBuf) -> Self {
Self(Arc::new(Inner {
live: Live::at(home.clone()),
stats: Mutex::default(),
persist: None,
last_persist: Mutex::new(None),
fixed_budget: Mutex::new(None),
owner_home: Some(home),
owner_cache: Mutex::new(None),
fixed_owner: Mutex::new(None),
}))
}
#[cfg(test)]
pub fn pinned(policy: Policy, budget: Option<Limits>) -> Self {
let a = Self::at(PathBuf::from("/nonexistent-zc-policy"));
a.0.live.pin(policy);
*a.0.fixed_budget.lock().unwrap() = budget;
a
}
#[cfg(test)]
pub fn pin_owner(&self, owner: Owner) {
*self.0.fixed_owner.lock().unwrap() = Some(owner);
}
#[cfg(test)]
pub fn pin_policy(&self, policy: Policy) {
self.0.live.pin(policy);
}
pub fn policy(&self) -> Arc<Policy> {
self.0.live.current()
}
pub fn owner(&self) -> Owner {
if let Some(o) = *self.0.fixed_owner.lock().unwrap_or_else(|e| e.into_inner()) {
return o;
}
let mut cache = self.0.owner_cache.lock().unwrap_or_else(|e| e.into_inner());
if let Some((at, o)) = *cache {
if at.elapsed() < OWNER_CACHE {
return o;
}
}
let secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let home = self.0.owner_home.clone().or_else(crate::credentials::dir);
let o = home.map_or(Owner::NoSignal, |h| {
owner::read(&h, secs, owner::stale_secs())
});
*cache = Some((Instant::now(), o));
o
}
pub fn owner_yield(&self) -> Option<&'static str> {
self.owner().blocks(&self.policy().sharing.when)
}
fn total(&self, policy: &Policy) -> Option<Limits> {
if let Some(l) = self.0.fixed_budget.lock().unwrap().clone() {
return Some(l);
}
policy
.effective_budget(&|k| std::env::var(k).ok())
.and_then(|b| b.total(&HostResources::detect()))
.ok()
}
pub fn budget_limits(&self) -> Option<Limits> {
self.total(&self.policy())
}
pub fn refuse(&self, req: &Request, reason: Reason, detail: String) -> Refusal {
let secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let f = refuse(req, reason, detail, secs);
self.record(&Err(f.clone()));
f
}
pub fn workload_enabled(&self, w: &Workload) -> bool {
let p = self.policy();
match w {
Workload::Function => true,
Workload::Environment => p.workloads.environments,
Workload::Space => p.workloads.spaces,
Workload::HubServing => p.workloads.hub_serving,
Workload::Atelier { .. } => p.workloads.atelier != Atelier::Off,
}
}
pub fn check(&self, req: &Request) -> Result<(), Refusal> {
self.check_with(req, &[], LocalTime::now())
}
pub fn check_with(
&self,
req: &Request,
running: &[Allocation],
now: LocalTime,
) -> Result<(), Refusal> {
let policy = self.policy();
let sized = req.cpus > 0.0 || req.memory_bytes > 0 || req.gpus > 0;
let total = if sized { self.total(&policy) } else { None };
let view = total.as_ref().map(|t| BudgetView { total: t, running });
let secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let r = admit_with(&policy, req, now, view.as_ref(), secs, self.owner());
self.record(&r);
r
}
fn record(&self, r: &Result<(), Refusal>) {
{
let mut s = self.0.stats.lock().unwrap_or_else(|e| e.into_inner());
match r {
Ok(()) => s.admitted += 1,
Err(f) => {
*s.refused.entry(f.reason).or_insert(0) += 1;
if s.recent.len() == RECENT {
s.recent.pop_front();
}
s.recent.push_back(f.clone());
}
}
}
if r.is_err() {
self.persist();
}
}
pub fn snapshot(&self) -> Value {
let s = self.0.stats.lock().unwrap_or_else(|e| e.into_inner());
let mut refused = serde_json::Map::new();
for r in Reason::ALL {
refused.insert(
r.as_str().to_string(),
json!(s.refused.get(&r).copied().unwrap_or(0)),
);
}
json!({
"admitted": s.admitted,
"refused": refused,
"recent": s.recent.iter().collect::<Vec<_>>(),
})
}
fn persist(&self) {
let Some(base) = &self.0.persist else { return };
{
let mut last = self
.0
.last_persist
.lock()
.unwrap_or_else(|e| e.into_inner());
if last.is_some_and(|t| t.elapsed() < PERSIST_EVERY) {
return;
}
*last = Some(Instant::now());
}
let home = if base.as_os_str().is_empty() {
crate::credentials::dir()
} else {
Some(base.clone())
};
if let Some(home) = home {
let tmp = home.join(".admission.json.tmp");
if std::fs::write(&tmp, self.snapshot().to_string()).is_ok() {
let _ = std::fs::rename(&tmp, home.join(SNAPSHOT_FILE));
}
}
}
}
pub fn read_snapshot(home: &Path) -> Option<Value> {
serde_json::from_str(&std::fs::read_to_string(home.join(SNAPSHOT_FILE)).ok()?).ok()
}
#[cfg(test)]
mod tests {
use super::*;
const NOW: LocalTime = LocalTime {
weekday: 2,
minute: 12 * 60,
};
fn run(p: &Policy, r: &Request) -> Result<(), Refusal> {
admit(p, r, NOW, None, 0)
}
fn req(src: Source, w: Workload) -> Request {
Request::new(src, w)
}
fn reason(r: Result<(), Refusal>) -> &'static str {
r.unwrap_err().reason.as_str()
}
#[test]
fn the_default_policy_admits_public_work_from_every_source() {
let p = Policy::default();
for (s, w) in [
(Source::PeerOffer, Workload::Function),
(Source::SpacesLease, Workload::Space),
(Source::HubAllocation, Workload::HubServing),
(Source::EnvGuest, Workload::Environment),
] {
assert!(run(&p, &req(s, w)).is_ok(), "{s:?}");
}
}
#[test]
fn an_empty_squad_list_refuses_foreign_work_but_not_the_owners() {
let mut p = Policy::default();
p.sharing.squads = Vec::new();
assert_eq!(
reason(run(&p, &req(Source::PeerOffer, Workload::Function))),
"squad"
);
let mut own = req(Source::PeerOffer, Workload::Function);
own.own = true;
assert!(run(&p, &own).is_ok());
}
#[test]
fn a_job_from_a_squad_not_listed_is_refused_and_a_listed_one_is_taken() {
let mut p = Policy::default();
p.sharing.squads = vec!["lab".into()];
assert_eq!(
reason(run(&p, &req(Source::SpacesLease, Workload::Space))),
"squad"
);
let mut r = req(Source::SpacesLease, Workload::Space);
r.squad = Some("lab".into());
assert!(run(&p, &r).is_ok());
r.squad = Some("other".into());
assert_eq!(reason(run(&p, &r)), "squad");
}
#[test]
fn each_workload_switch_refuses_only_its_own_kind() {
let mut p = Policy::default();
p.workloads.environments = false;
assert_eq!(
reason(run(&p, &req(Source::EnvGuest, Workload::Environment))),
"workload"
);
assert!(run(&p, &req(Source::SpacesLease, Workload::Space)).is_ok());
p.workloads.spaces = false;
assert_eq!(
reason(run(&p, &req(Source::SpacesLease, Workload::Space))),
"workload"
);
p.workloads.hub_serving = false;
assert_eq!(
reason(run(&p, &req(Source::HubAllocation, Workload::HubServing))),
"workload"
);
assert!(
run(&p, &req(Source::PeerOffer, Workload::Function)).is_ok(),
"peer functions have no workload switch"
);
}
#[test]
fn atelier_gpu_only_needs_a_gpu_job_and_a_gpu_budget() {
let atelier = |gpu| Workload::Atelier {
backend: Some("peft".into()),
gpu,
};
let mut p = Policy::default();
p.workloads.atelier = Atelier::GpuOnly;
p.budget.gpu = false;
assert_eq!(
reason(run(&p, &req(Source::AtelierJob, atelier(true)))),
"workload"
);
p.budget.gpu = true;
assert!(run(&p, &req(Source::AtelierJob, atelier(true))).is_ok());
assert_eq!(
reason(run(&p, &req(Source::AtelierJob, atelier(false)))),
"workload"
);
p.workloads.atelier = Atelier::On;
assert!(run(&p, &req(Source::AtelierJob, atelier(false))).is_ok());
p.workloads.atelier = Atelier::Backends(vec!["ludwig".into()]);
assert_eq!(
reason(run(&p, &req(Source::AtelierJob, atelier(true)))),
"workload"
);
p.workloads.atelier = Atelier::Off;
assert_eq!(
reason(run(&p, &req(Source::AtelierJob, atelier(true)))),
"workload"
);
}
#[test]
fn min_price_is_a_floor_against_the_offered_price() {
let mut p = Policy::default();
p.sharing.min_price = MinPrice::PerHour(10.0);
let mut r = req(Source::PeerOffer, Workload::Function);
r.price_per_hour = Some(9.99);
assert_eq!(reason(run(&p, &r)), "price");
r.price_per_hour = Some(10.0);
assert!(run(&p, &r).is_ok(), "at the floor is enough");
r.price_per_hour = None;
assert!(run(&p, &r).is_ok(), "no price known: nothing to compare");
p.sharing.min_price = MinPrice::Default;
r.price_per_hour = Some(0.0);
assert!(run(&p, &r).is_ok(), "default sets no floor of its own");
p.sharing.min_price = MinPrice::Withdrawn;
r.price_per_hour = Some(1e9);
assert_eq!(
reason(run(&p, &r)),
"price",
"-1 withdraws the node at any price"
);
}
#[test]
fn the_schedule_only_matters_when_when_says_schedule() {
let mut p = Policy::default();
p.sharing.schedule = vec!["mon-fri 19:00-08:00".into()];
assert!(
run(&p, &req(Source::PeerOffer, Workload::Function)).is_ok(),
"idle ignores it"
);
p.sharing.when = "schedule".into();
assert_eq!(
reason(run(&p, &req(Source::PeerOffer, Workload::Function))),
"schedule"
);
let night = LocalTime {
weekday: 2,
minute: 22 * 60,
};
assert!(admit(
&p,
&req(Source::PeerOffer, Workload::Function),
night,
None,
0
)
.is_ok());
let mut own = req(Source::PeerOffer, Workload::Function);
own.own = true;
assert!(run(&p, &own).is_ok());
}
fn limits(cpu: u32, mem_gib: u64, gpu: bool) -> Limits {
Limits {
cpu_millis: cpu,
memory_bytes: mem_gib << 30,
gpus: if gpu { Gpus::All } else { Gpus::None },
}
}
#[test]
fn budget_fit_is_against_the_total_and_what_runs() {
let p = Policy::default();
let total = limits(4000, 8, false);
let mut r = req(Source::EnvGuest, Workload::Environment);
r.cpus = 2.0;
r.memory_bytes = 4 << 30;
let none: [Allocation; 0] = [];
let v = BudgetView {
total: &total,
running: &none,
};
assert!(admit(&p, &r, NOW, Some(&v), 0).is_ok());
r.memory_bytes = 9 << 30;
assert_eq!(
admit(&p, &r, NOW, Some(&v), 0).unwrap_err().reason,
Reason::Budget
);
let held = [Allocation::from_label("zk-dep-a", "3000m,1073741824").unwrap()];
let v = BudgetView {
total: &total,
running: &held,
};
r.memory_bytes = 1 << 30;
let e = admit(&p, &r, NOW, Some(&v), 0).unwrap_err();
assert_eq!(e.reason, Reason::Budget);
assert!(e.detail.contains("cpu"), "{}", e.detail);
}
#[test]
fn a_gpu_job_needs_a_gpu_budget_and_only_the_owners_peer_jobs_are_exempt() {
let p = Policy::default();
let none: [Allocation; 0] = [];
let mut r = req(Source::EnvGuest, Workload::Environment);
r.gpus = 1;
let total = limits(4000, 8, false);
let v = BudgetView {
total: &total,
running: &none,
};
assert_eq!(
admit(&p, &r, NOW, Some(&v), 0).unwrap_err().reason,
Reason::Budget
);
let gpu = limits(4000, 8, true);
let v = BudgetView {
total: &gpu,
running: &none,
};
assert!(admit(&p, &r, NOW, Some(&v), 0).is_ok());
let mut peer = req(Source::PeerOffer, Workload::Function);
peer.gpus = 1;
let v = BudgetView {
total: &total,
running: &none,
};
let e = admit(&p, &peer, NOW, Some(&v), 0).unwrap_err();
assert_eq!(e.reason, Reason::Budget);
assert!(e.detail.contains("budget.gpu"), "{}", e.detail);
peer.own = true;
assert!(admit(&p, &peer, NOW, Some(&v), 0).is_ok());
}
fn at_owner(p: &Policy, r: &Request, o: Owner) -> Result<(), Refusal> {
admit_with(p, r, NOW, None, 0, o)
}
#[test]
fn when_idle_refuses_every_source_while_the_owner_is_active() {
let p = Policy::default();
assert_eq!(p.sharing.when, "idle");
for (s, w) in [
(Source::PeerOffer, Workload::Function),
(Source::SpacesLease, Workload::Space),
(Source::HubAllocation, Workload::HubServing),
(Source::EnvGuest, Workload::Environment),
] {
let e = at_owner(&p, &req(s, w.clone()), Owner::Active("owner-active")).unwrap_err();
assert_eq!(e.reason, Reason::Idle, "{s:?}");
assert!(e.detail.contains("owner-active"), "{}", e.detail);
assert!(
at_owner(&p, &req(s, w.clone()), Owner::Calm).is_ok(),
"{s:?}"
);
assert!(
at_owner(&p, &req(s, w), Owner::NoSignal).is_ok(),
"no agent, nothing to honour"
);
}
}
#[test]
fn a_stale_signal_is_an_active_owner_but_only_for_when_idle() {
let mut p = Policy::default();
let r = req(Source::PeerOffer, Workload::Function);
let stale = Owner::Active("signal-stale");
assert_eq!(at_owner(&p, &r, stale).unwrap_err().reason, Reason::Idle);
p.sharing.when = "always".into();
assert!(at_owner(&p, &r, stale).is_ok());
}
#[test]
fn battery_and_pressure_refuse_whatever_when_says() {
let mut p = Policy::default();
p.sharing.when = "always".into();
let r = req(Source::SpacesLease, Workload::Space);
assert_eq!(
at_owner(&p, &r, Owner::Yielding("on-battery"))
.unwrap_err()
.reason,
Reason::Idle
);
}
#[test]
fn the_owners_own_work_never_waits_for_the_owner() {
let p = Policy::default();
let mut own = req(Source::PeerOffer, Workload::Function);
own.own = true;
assert!(at_owner(&p, &own, Owner::Active("owner-active")).is_ok());
assert!(at_owner(&p, &own, Owner::Yielding("on-battery")).is_ok());
let mut atelier = req(
Source::AtelierJob,
Workload::Atelier {
backend: None,
gpu: false,
},
);
atelier.own = true;
assert!(at_owner(&p, &atelier, Owner::Active("owner-active")).is_ok());
}
#[test]
fn the_admission_reads_the_published_signal_and_counts_idle_refusals() {
let home = crate::policy::test_home();
let a = Admission::at(home.clone());
let r = req(Source::EnvGuest, Workload::Environment);
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();
assert!(a.check(&r).is_ok(), "no file: nothing to honour");
owner::write(
&home,
&owner::Snapshot {
idle: false,
on_battery: false,
memory_pressure: false,
cpu_pressure: false,
at: now,
source: "test".into(),
},
)
.unwrap();
let a = Admission::at(home);
assert_eq!(a.check(&r).unwrap_err().reason, Reason::Idle);
assert_eq!(a.snapshot()["refused"]["idle"], 1);
assert_eq!(a.owner_yield(), Some("owner-active"));
}
#[test]
fn a_default_sized_offer_is_never_refused_on_a_small_node() {
let p = Policy::default();
let none: [Allocation; 0] = [];
let tiny = limits(500, 1, false); let v = BudgetView {
total: &tiny,
running: &none,
};
let mut r = req(Source::PeerOffer, Workload::Function);
r.cpus = 1.0;
r.memory_bytes = 1 << 30;
assert!(admit(&p, &r, NOW, Some(&v), 0).is_ok());
r.cpus = 2.0;
assert_eq!(
admit(&p, &r, NOW, Some(&v), 0).unwrap_err().reason,
Reason::Budget
);
}
#[test]
fn counters_and_the_recent_list_track_refusals_by_reason() {
let mut p = Policy::default();
p.sharing.squads = Vec::new();
let a = Admission::pinned(p, None);
let r = req(Source::PeerOffer, Workload::Function);
for _ in 0..25 {
assert!(a.check(&r).is_err());
}
let mut own = r.clone();
own.own = true;
assert!(a.check(&own).is_ok());
let s = a.snapshot();
assert_eq!(s["admitted"], 1);
assert_eq!(s["refused"]["squad"], 25);
assert_eq!(s["refused"]["price"], 0);
assert_eq!(s["recent"].as_array().unwrap().len(), RECENT, "bounded");
assert_eq!(s["recent"][0]["reason"], "squad");
assert_eq!(s["recent"][0]["source"], "peer_offer");
}
#[test]
fn a_sized_check_reads_the_budget_from_the_admission() {
let a = Admission::pinned(Policy::default(), Some(limits(2000, 2, false)));
let mut r = req(Source::SpacesLease, Workload::Space);
r.cpus = 4.0;
assert_eq!(a.check(&r).unwrap_err().reason, Reason::Budget);
r.cpus = 1.0;
assert!(a.check(&r).is_ok());
}
#[test]
fn workload_enabled_follows_the_policy() {
let mut p = Policy::default();
p.workloads.spaces = false;
let a = Admission::pinned(p, None);
assert!(!a.workload_enabled(&Workload::Space));
assert!(a.workload_enabled(&Workload::HubServing));
}
}