use serde::{Deserialize, Serialize};
use crate::budget::{plan, Budget, Declared, Plan};
use crate::residency::{LostReason, Millis, Observation, Residency};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Slot {
pub declared: Declared,
pub state: Residency,
pub endpoint: Option<String>,
pub admitted: bool,
pub short_bytes: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "route", rename_all = "snake_case")]
pub enum Route {
Ready {
model: String,
endpoint: String,
},
Warming {
model: String,
for_ms: Millis,
},
Lost {
model: String,
reason: LostReason,
operator_fault: bool,
},
Failed {
model: String,
reason: String,
},
NotAdmitted {
model: String,
short_bytes: u64,
},
NotDeclared {
model: String,
},
Unknown {
model: String,
},
}
impl Route {
pub fn is_ready(&self) -> bool {
matches!(self, Route::Ready { .. })
}
pub fn should_try_elsewhere(&self) -> bool {
!matches!(self, Route::Ready { .. } | Route::Warming { .. })
}
pub fn operator_fault(&self) -> bool {
match self {
Route::Lost { operator_fault, .. } => *operator_fault,
Route::Failed { .. } => true,
_ => false,
}
}
}
#[derive(Debug, Clone)]
pub struct Fleet {
budget: Budget,
slots: Vec<Slot>,
}
impl Fleet {
pub fn declare(budget: Budget, declared: Vec<Declared>) -> Fleet {
let p: Plan = plan(budget, &declared);
let slots = declared
.into_iter()
.map(|d| {
let rejection = p.rejected.iter().find(|r| r.model == d.model);
Slot {
admitted: rejection.is_none(),
short_bytes: rejection.map(|r| r.short_bytes()).unwrap_or(0),
declared: d,
state: Residency::Unknown,
endpoint: None,
}
})
.collect();
Fleet { budget, slots }
}
pub fn budget(&self) -> Budget {
self.budget
}
pub fn slots(&self) -> &[Slot] {
&self.slots
}
pub fn slot(&self, model: &str) -> Option<&Slot> {
self.slots.iter().find(|s| s.declared.model == model)
}
pub fn live_committed_bytes(&self) -> u64 {
self.slots.iter().map(|s| s.state.vram_bytes()).sum()
}
pub fn live_free_bytes(&self) -> u64 {
self.budget
.usable_bytes()
.saturating_sub(self.live_committed_bytes())
}
pub fn set_endpoint(&mut self, model: &str, endpoint: impl Into<String>) {
if let Some(s) = self.slots.iter_mut().find(|s| s.declared.model == model) {
s.endpoint = Some(endpoint.into());
}
}
pub fn observe(&mut self, model: &str, obs: &Observation, now: Millis) {
if let Some(s) = self.slots.iter_mut().find(|s| s.declared.model == model) {
s.state = s.state.observe(obs, now);
}
}
pub fn route(&self, model: &str, now: Millis) -> Route {
let Some(slot) = self.slot(model) else {
return Route::NotDeclared {
model: model.to_string(),
};
};
if !slot.admitted {
return Route::NotAdmitted {
model: model.to_string(),
short_bytes: slot.short_bytes,
};
}
match &slot.state {
Residency::Resident { .. } => match &slot.endpoint {
Some(endpoint) => Route::Ready {
model: model.to_string(),
endpoint: endpoint.clone(),
},
None => Route::Unknown {
model: model.to_string(),
},
},
Residency::Loading { .. } => Route::Warming {
model: model.to_string(),
for_ms: slot.state.loading_for(now).unwrap_or(0),
},
Residency::Lost { reason, .. } => Route::Lost {
model: model.to_string(),
reason: *reason,
operator_fault: reason.is_operator_fault(),
},
Residency::Failed { reason, .. } => Route::Failed {
model: model.to_string(),
reason: reason.clone(),
},
Residency::Stopped { .. } | Residency::Unknown => Route::Unknown {
model: model.to_string(),
},
}
}
pub fn next_to_load(&self) -> Option<&Slot> {
let free = self.live_free_bytes();
self.slots.iter().find(|s| {
s.admitted
&& !matches!(
s.state,
Residency::Resident { .. }
| Residency::Loading { .. }
| Residency::Stopped { .. }
)
&& s.declared.total_bytes() <= free
})
}
pub fn report(&self, now: Millis) -> String {
let mut out = format!(
"{:.1} / {:.1} GiB held ({} declared, {} admitted)\n",
crate::budget::gib(self.live_committed_bytes()),
crate::budget::gib(self.budget.usable_bytes()),
self.slots.len(),
self.slots.iter().filter(|s| s.admitted).count(),
);
for s in &self.slots {
let what = if s.admitted {
s.state.explain(now)
} else {
format!(
"not admitted — short by {:.1} GiB",
crate::budget::gib(s.short_bytes)
)
};
out.push_str(&format!(" {:<28} {}\n", s.declared.model, what));
}
out
}
}