use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use serde::Deserialize;
use serde_json::{json, Value};
use super::HubAllocation;
pub const POLL_MIN: Duration = Duration::from_secs(15);
pub const POLL_MAX: Duration = Duration::from_secs(120);
pub const EMPTY_BEFORE_BACKOFF: u32 = 4;
pub const PENDING_TTL: Duration = Duration::from_secs(600);
#[derive(Debug, Clone, Copy, Default, Deserialize, PartialEq)]
pub struct Needs {
#[serde(default)]
pub vram_gb: f64,
#[serde(default)]
pub ram_gb: f64,
#[serde(default)]
pub disk_gb: f64,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct Request {
pub id: String,
pub kind: String,
#[serde(default)]
pub needs: Needs,
}
pub fn kind_allowed(kind: &str, purpose: &[String]) -> bool {
let has = |p: &str| purpose.iter().any(|x| x == p);
match kind {
"inference" | "batch" => has("inference"),
"warmup" => has("warmup"),
_ => false,
}
}
pub fn fits(needs: &Needs, slice: &HubAllocation, committed: &Needs) -> bool {
needs.vram_gb <= slice.vram_gb as f64 - committed.vram_gb
&& needs.ram_gb <= slice.ram_gb as f64 - committed.ram_gb
&& needs.disk_gb <= slice.disk_gb as f64 - committed.disk_gb
}
pub fn candidates<'a>(
open: &'a [Request],
slice: &HubAllocation,
committed: &Needs,
) -> Vec<&'a Request> {
open.iter()
.filter(|r| kind_allowed(&r.kind, &slice.purpose) && fits(&r.needs, slice, committed))
.collect()
}
pub fn parse_requests(body: &str) -> Vec<Request> {
let Ok(v) = serde_json::from_str::<Value>(body) else {
return Vec::new();
};
v.get("requests")
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|r| serde_json::from_value(r.clone()).ok())
.collect()
})
.unwrap_or_default()
}
#[derive(Debug, Default)]
pub struct Cadence {
empty: u32,
}
impl Cadence {
pub fn base(&self) -> Duration {
if self.empty < EMPTY_BEFORE_BACKOFF {
return POLL_MIN;
}
let doublings = (self.empty - EMPTY_BEFORE_BACKOFF + 1).min(6);
(POLL_MIN * 2u32.pow(doublings)).min(POLL_MAX)
}
pub fn empty_poll(&mut self) {
self.empty = self.empty.saturating_add(1);
}
pub fn claimed(&mut self) {
self.empty = 0;
}
pub fn with_jitter(&self, unit: f64) -> Duration {
let f = 0.8 + 0.4 * unit.clamp(0.0, 1.0);
self.base().mul_f64(f).clamp(POLL_MIN, POLL_MAX)
}
}
fn jitter_unit() -> f64 {
let n = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.subsec_nanos())
.unwrap_or(0);
(n % 1000) as f64 / 1000.0
}
#[derive(Debug, Clone, PartialEq)]
pub enum ClaimOutcome {
Won,
Taken,
Forbidden,
Failed(String),
}
pub fn outcome_for_status(code: u16) -> ClaimOutcome {
match code {
200 => ClaimOutcome::Won,
409 => ClaimOutcome::Taken,
403 => ClaimOutcome::Forbidden,
c => ClaimOutcome::Failed(format!("HTTP {c}")),
}
}
pub struct HubApi {
api_url: String,
api_key: String,
node_pubkey: String,
}
impl HubApi {
pub fn new(api_url: &str, api_key: &str, node_pubkey: &str) -> HubApi {
HubApi {
api_url: api_url.trim_end_matches('/').to_string(),
api_key: api_key.to_string(),
node_pubkey: node_pubkey.to_string(),
}
}
pub fn open_requests(&self) -> Result<Vec<Request>, String> {
let resp = ureq::get(format!("{}/api/hub/requests?open=1", self.api_url))
.config()
.timeout_global(Some(Duration::from_secs(10)))
.http_status_as_error(false)
.build()
.header("X-Broker-Api-Key", &self.api_key)
.call()
.map_err(|e| e.to_string())?;
let code = resp.status().as_u16();
if !(200..300).contains(&code) {
return Err(format!("requests HTTP {code}"));
}
let body = resp
.into_body()
.read_to_string()
.map_err(|e| e.to_string())?;
Ok(parse_requests(&body))
}
pub fn claim(&self, id: &str) -> ClaimOutcome {
let body = json!({ "node_pubkey": self.node_pubkey });
let r = ureq::post(format!("{}/api/hub/requests/{id}/claim", self.api_url))
.config()
.timeout_global(Some(Duration::from_secs(10)))
.http_status_as_error(false)
.build()
.header("X-Broker-Api-Key", &self.api_key)
.header("Content-Type", "application/json")
.send(body.to_string().as_str());
match r {
Ok(resp) => outcome_for_status(resp.status().as_u16()),
Err(e) => ClaimOutcome::Failed(e.to_string()),
}
}
}
struct Pending {
id: String,
since: Instant,
}
pub fn spawn_once(state: Arc<crate::broker::BrokerState>) {
static STARTED: AtomicBool = AtomicBool::new(false);
let (Some(api_url), Some(api_key)) =
(state.config.api_url.clone(), state.config.api_key.clone())
else {
return;
};
let Some(dir) = crate::credentials::dir() else {
return;
};
if STARTED.swap(true, Ordering::SeqCst) {
return;
}
let pubkey = state.node_key.public_b64();
let _ = std::thread::Builder::new()
.name("hub-alloc-claim".into())
.spawn(move || {
let api = HubApi::new(&api_url, &api_key, &pubkey);
let mut cadence = Cadence::default();
let mut pending: Option<Pending> = None;
let mut warned_forbidden = false;
loop {
std::thread::sleep(cadence.with_jitter(jitter_unit()));
let Some(slice) = super::load(&dir).filter(|a| a.enabled) else {
pending = None;
continue;
};
if pending.as_ref().is_some_and(|p| p.since.elapsed() >= PENDING_TTL) {
if let Some(p) = pending.take() {
eprintln!(" [HUB_ALLOC] claim {} was not dispatched in time; released", p.id);
}
}
let open = match api.open_requests() {
Ok(o) => o,
Err(e) => {
eprintln!(" [HUB_ALLOC] poll failed: {e}");
cadence.empty_poll();
continue;
}
};
if pending.is_some() {
continue;
}
let none = Needs::default();
let mut won = false;
for r in candidates(&open, &slice, &none) {
match api.claim(&r.id) {
ClaimOutcome::Won => {
println!(" [HUB_ALLOC] claimed request {} ({})", r.id, r.kind);
pending = Some(Pending {
id: r.id.clone(),
since: Instant::now(),
});
won = true;
break;
}
ClaimOutcome::Taken => continue,
ClaimOutcome::Forbidden => {
if !warned_forbidden {
eprintln!(" [HUB_ALLOC] claim refused (403): the opt-in is not accepted by the hub; check `zc hub allocate`");
warned_forbidden = true;
}
break;
}
ClaimOutcome::Failed(e) => {
eprintln!(" [HUB_ALLOC] claim failed: {e}");
break;
}
}
}
if won {
cadence.claimed();
} else {
cadence.empty_poll();
}
}
});
}
#[cfg(test)]
mod tests {
use super::*;
fn slice(vram: u64, ram: u64, disk: u64, purpose: &[&str]) -> HubAllocation {
HubAllocation {
enabled: true,
vram_gb: vram,
ram_gb: ram,
disk_gb: disk,
purpose: purpose.iter().map(|s| s.to_string()).collect(),
}
}
fn req(id: &str, kind: &str, v: f64, r: f64, d: f64) -> Request {
Request {
id: id.into(),
kind: kind.into(),
needs: Needs {
vram_gb: v,
ram_gb: r,
disk_gb: d,
},
}
}
#[test]
fn fit_checks_each_dimension_against_the_slice_minus_committed() {
let s = slice(8, 16, 50, &["inference"]);
let none = Needs::default();
assert!(fits(
&req("a", "inference", 8.0, 16.0, 50.0).needs,
&s,
&none
));
assert!(!fits(
&req("a", "inference", 10.0, 16.0, 20.0).needs,
&s,
&none
));
assert!(!fits(
&req("a", "inference", 1.0, 17.0, 1.0).needs,
&s,
&none
));
let held = Needs {
vram_gb: 4.0,
ram_gb: 0.0,
disk_gb: 0.0,
};
assert!(!fits(
&req("a", "inference", 5.0, 1.0, 1.0).needs,
&s,
&held
));
assert!(fits(&req("a", "inference", 4.0, 1.0, 1.0).needs, &s, &held));
}
#[test]
fn purposes_gate_kinds_and_batch_rides_on_inference() {
let inf = vec!["inference".to_string()];
let warm = vec!["warmup".to_string()];
assert!(kind_allowed("inference", &inf));
assert!(kind_allowed("batch", &inf));
assert!(!kind_allowed("warmup", &inf));
assert!(kind_allowed("warmup", &warm));
assert!(!kind_allowed("batch", &warm));
assert!(!kind_allowed("mystery", &inf));
}
#[test]
fn candidates_keep_hub_order_and_skip_misfits() {
let s = slice(8, 16, 50, &["inference"]);
let open = vec![
req("big", "inference", 24.0, 16.0, 20.0),
req("warm", "warmup", 0.0, 4.0, 5.0),
req("ok1", "inference", 6.0, 8.0, 10.0),
req("ok2", "batch", 2.0, 2.0, 2.0),
];
let ids: Vec<&str> = candidates(&open, &s, &Needs::default())
.iter()
.map(|r| r.id.as_str())
.collect();
assert_eq!(ids, ["ok1", "ok2"]);
}
#[test]
fn parses_the_open_feed_and_tolerates_missing_needs() {
let body = r#"{"requests":[
{"id":"req-1","kind":"inference","model_id":"m","needs":{"vram_gb":10,"ram_gb":16,"disk_gb":20},"state":"open"},
{"id":"req-2","kind":"warmup","template_id":"t","state":"open"},
{"nonsense":true}]}"#;
let r = parse_requests(body);
assert_eq!(r.len(), 2);
assert_eq!(r[0].needs.vram_gb, 10.0);
assert_eq!(r[1].needs, Needs::default());
assert!(parse_requests("not json").is_empty());
}
#[test]
fn cadence_is_15s_then_doubles_to_120s_and_resets_on_claim() {
let mut c = Cadence::default();
for _ in 0..4 {
assert_eq!(c.base(), POLL_MIN);
c.empty_poll();
}
let mut seen = Vec::new();
for _ in 0..6 {
seen.push(c.base().as_secs());
c.empty_poll();
}
assert_eq!(seen, [30, 60, 120, 120, 120, 120]);
c.claimed();
assert_eq!(c.base(), POLL_MIN);
}
#[test]
fn jitter_stays_within_bounds() {
let mut c = Cadence::default();
for _ in 0..20 {
for u in [0.0, 0.5, 0.999] {
let d = c.with_jitter(u);
assert!(d >= POLL_MIN && d <= POLL_MAX, "{d:?}");
}
c.empty_poll();
}
}
#[test]
fn status_codes_map_to_outcomes() {
assert_eq!(outcome_for_status(200), ClaimOutcome::Won);
assert_eq!(outcome_for_status(409), ClaimOutcome::Taken);
assert_eq!(outcome_for_status(403), ClaimOutcome::Forbidden);
assert!(matches!(outcome_for_status(500), ClaimOutcome::Failed(_)));
}
}