zc2 0.0.46

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! The hub-request claim loop (spec §b "The claim loop"). Runs only while
//! `[hub_allocation].enabled`. Polls `GET /api/hub/requests?open=1`, takes the
//! first request that the purposes allow and that fits the lent slice minus
//! what is already committed, and `POST`s `/api/hub/requests/{id}/claim`.
//!
//! The claim is advisory: nothing starts on the node until the hub dispatches.
//! TODO(zc#472 follow-up): on a won claim, wait for the hub's dispatch through
//! the existing serve / deploy path and report the workload up so the hub marks
//! the request `served`. Until that lands the claim is held as "pending" for
//! `PENDING_TTL` (it counts against the slice, and blocks a second claim), then
//! released; the hub expires an unconfirmed claim on its own `confirm_by`.

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);
/// Empty polls before the interval starts doubling.
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,
}

/// Is a request of `kind` allowed by the opted-in purposes? `batch` rides on
/// `inference`.
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,
    }
}

/// Does `needs` fit the slice once `committed` (claims already held) is taken
/// out? A need the hub leaves out is 0.
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
}

/// Requests to try, in the hub's order (oldest first), that this node may take.
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()
}

/// Poll interval: 15 s while requests fit; after four empty polls it doubles
/// per empty poll (30, 60, 120 s cap); any claim resets it.
#[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;
    }
    /// `base` with +/-20 % jitter from `unit` in [0, 1), clamped to 15..=120 s.
    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,
    /// 409: someone else got it, or it is no longer open.
    Taken,
    /// 403: the opt-in is misconfigured on the hub side.
    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,
}

/// Start the loop once per process. Idle (re-reads the config each round)
/// until the machine opts in.
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;
                    }
                };
                // One unconfirmed claim at a time: while one is pending, only poll.
                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(_)));
    }
}