zc2 0.0.52

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
419
420
421
422
423
424
425
426
427
//! Capacity advert v2 (ADR ZC-0025 §2): what this node can lend, per resource
//! class, with `total` (what the machine has), `budget` (what the policy lends)
//! and `free` (what is left of the budget right now) kept apart.
//!
//! The v1 advert and the hub sync mix them: `cpu_cores` and `ram_gb` are free
//! figures, the GPU figure is a free count, and the peer advert drops VRAM and
//! the model. This view is additive: it travels in the signed peer advert's
//! `capacity` member and in the hub sync's per-worker `node_capacity`, and
//! nothing that already existed changes meaning.
//!
//! Pure: the workers, the budget and what is held are arguments.

use super::worker::{Worker, WorkerStatus};
use crate::sandbox::{Gpus, Limits};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::hash::{Hash, Hasher};

pub const CAPACITY_VERSION: u32 = 2;

/// One resource in its three meanings. `free <= budget <= total`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Share<T> {
    pub total: T,
    pub budget: T,
    pub free: T,
}

/// One GPU class: workers with the same model and VRAM per GPU.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct GpuClass {
    /// `None` when the worker does not report a model.
    pub model: Option<String>,
    /// GiB per GPU; `None` when not reported.
    pub vram_gb: Option<u32>,
    /// What this class bills, yen per hour (the cheapest worker in it).
    pub price_per_hour: Option<f64>,
    pub gpus: Share<u32>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Capacity {
    /// Always [`CAPACITY_VERSION`].
    #[serde(rename = "v")]
    pub version: u32,
    pub cpu_cores: Share<f64>,
    pub memory_bytes: Share<u64>,
    pub gpu: Vec<GpuClass>,
    /// What the node bills per hour for a CPU job: the cheapest healthy
    /// worker's effective price (after the hub's override). This is the
    /// number the peer advert announces (ADR ZC-0025 §3).
    pub price_per_hour: Option<f64>,
    /// Healthy local workers behind the figures.
    pub workers: u32,
}

/// What offers admitted but not finished hold, node-wide.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Held {
    pub cpus: f64,
    pub memory_bytes: u64,
    pub gpus: u32,
}

const GIB: f64 = (1u64 << 30) as f64;

fn cheaper(a: Option<f64>, b: f64) -> Option<f64> {
    Some(a.map_or(b, |a| a.min(b)))
}

/// The GPUs the budget lends out of `total`: none with `budget.gpu = false`
/// (the admission check refuses GPU offers then too), an explicit count caps it.
fn gpu_budget(total: u32, limits: Option<&Limits>) -> u32 {
    match limits.map(|l| &l.gpus) {
        Some(Gpus::None) => 0,
        Some(Gpus::Count(n)) => total.min(*n),
        _ => total,
    }
}

/// Build the view from the node's own healthy workers.
pub fn compute(workers: &[Worker], budget: Option<&Limits>, held: Held) -> Capacity {
    let local: Vec<&Worker> = workers
        .iter()
        .filter(|w| w.source_node.is_none() && w.status == WorkerStatus::Healthy)
        .collect();

    let total_cpu: f64 = local.iter().map(|w| w.resources.cpus_total.max(0.0)).sum();
    let avail_cpu: f64 = local
        .iter()
        .map(|w| {
            w.resources
                .cpus_available
                .clamp(0.0, w.resources.cpus_total.max(0.0))
        })
        .sum();
    let total_mem: u64 = local.iter().map(|w| w.resources.memory_total).sum();
    let avail_mem: u64 = local
        .iter()
        .map(|w| w.resources.memory_available.min(w.resources.memory_total))
        .sum();

    let budget_cpu = budget
        .map(|b| b.cpu_millis as f64 / 1000.0)
        .map_or(total_cpu, |b| b.min(total_cpu));
    let budget_mem = budget
        .map(|b| b.memory_bytes)
        .map_or(total_mem, |b| b.min(total_mem));
    let free_cpu = avail_cpu.min((budget_cpu - held.cpus).max(0.0));
    let free_mem = avail_mem.min(budget_mem.saturating_sub(held.memory_bytes));

    // GPU classes, biggest card first.
    let mut classes: BTreeMap<(std::cmp::Reverse<u32>, String), GpuClass> = BTreeMap::new();
    for w in &local {
        let n = w.resources.gpus_total;
        if n == 0 {
            continue;
        }
        let key = (
            std::cmp::Reverse(w.hardware.gpu_vram_gb.unwrap_or(0)),
            w.hardware.gpu_model.clone().unwrap_or_default(),
        );
        let c = classes.entry(key).or_insert_with(|| GpuClass {
            model: w.hardware.gpu_model.clone(),
            vram_gb: w.hardware.gpu_vram_gb,
            ..GpuClass::default()
        });
        c.gpus.total += n;
        c.gpus.free += w.resources.gpus_available.min(n);
        c.price_per_hour = cheaper(c.price_per_hour, w.pricing.price_per_hour);
    }
    let mut gpu: Vec<GpuClass> = classes.into_values().collect();
    let total_gpus: u32 = gpu.iter().map(|c| c.gpus.total).sum();
    let mut budget_left = gpu_budget(total_gpus, budget);
    let mut cap = budget_left.saturating_sub(held.gpus);
    for c in &mut gpu {
        c.gpus.budget = c.gpus.total.min(budget_left);
        budget_left -= c.gpus.budget;
        c.gpus.free = c.gpus.free.min(c.gpus.budget).min(cap);
        cap -= c.gpus.free;
    }

    let price_per_hour = local
        .iter()
        .fold(None, |acc, w| cheaper(acc, w.pricing.price_per_hour));

    Capacity {
        version: CAPACITY_VERSION,
        cpu_cores: Share {
            total: total_cpu,
            budget: budget_cpu,
            free: free_cpu,
        },
        memory_bytes: Share {
            total: total_mem,
            budget: budget_mem,
            free: free_mem,
        },
        gpu,
        price_per_hour,
        workers: local.len() as u32,
    }
}

impl Capacity {
    /// A hash of the figures rounded the way a requester can act on them
    /// (whole cores, whole GiB, whole GPUs, yen), so measurement noise does
    /// not count as a change and the push gate stays quiet.
    pub fn signature(&self) -> u64 {
        let mut h = std::collections::hash_map::DefaultHasher::new();
        let r = |c: &Share<f64>| {
            (
                c.total.floor() as i64,
                c.budget.floor() as i64,
                c.free.floor() as i64,
            )
        };
        let m = |c: &Share<u64>| {
            (
                (c.total as f64 / GIB).floor() as i64,
                (c.budget as f64 / GIB).floor() as i64,
                (c.free as f64 / GIB).floor() as i64,
            )
        };
        r(&self.cpu_cores).hash(&mut h);
        m(&self.memory_bytes).hash(&mut h);
        for c in &self.gpu {
            (
                &c.model,
                c.vram_gb,
                c.gpus.total,
                c.gpus.budget,
                c.gpus.free,
            )
                .hash(&mut h);
            c.price_per_hour.map(|p| p.round() as i64).hash(&mut h);
        }
        self.price_per_hour.map(|p| p.round() as i64).hash(&mut h);
        self.workers.hash(&mut h);
        h.finish()
    }

    /// Free GPUs across classes with at least `min_vram_gb` per GPU (`None`
    /// = any card; an unreported VRAM does not satisfy a minimum).
    pub fn free_gpus(&self, min_vram_gb: Option<u32>) -> u32 {
        self.gpu
            .iter()
            .filter(|c| min_vram_gb.is_none_or(|m| c.vram_gb.is_some_and(|v| v >= m)))
            .map(|c| c.gpus.free)
            .sum()
    }

    /// Whether a request of this size fits what is free now.
    pub fn fits(&self, cpus: f64, memory_bytes: u64, gpus: u32, min_vram_gb: Option<u32>) -> bool {
        self.cpu_cores.free >= cpus
            && self.memory_bytes.free >= memory_bytes
            && (gpus == 0 || self.free_gpus(min_vram_gb) >= gpus)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::broker::worker::{
        HardwareInfo, WorkerRegistration, WorkerRegistry, WorkerResources,
    };

    fn worker(name: &str, res: WorkerResources, gpu: Option<(&str, u32)>, price: f64) -> Worker {
        let reg = WorkerRegistry::new();
        let reg_req: WorkerRegistration = serde_json::from_value(serde_json::json!({
            "name": name, "uri": format!("http://127.0.0.1:1/{name}"), "worker_type": "cpu"
        }))
        .unwrap();
        let mut w = reg.register(reg_req);
        w.resources = res;
        w.hardware = HardwareInfo {
            gpu_model: gpu.map(|g| g.0.to_string()),
            gpu_vram_gb: gpu.map(|g| g.1),
            ..HardwareInfo::default()
        };
        w.pricing.price_per_hour = price;
        w
    }

    fn res(
        cpus: f64,
        free_cpus: f64,
        mem_gib: u64,
        free_gib: u64,
        gpus: u32,
        free_gpus: u32,
    ) -> WorkerResources {
        WorkerResources {
            cpus_total: cpus,
            cpus_available: free_cpus,
            memory_total: mem_gib << 30,
            memory_available: free_gib << 30,
            gpus_total: gpus,
            gpus_available: free_gpus,
        }
    }

    fn limits(cpu: u32, mem_gib: u64, gpus: Gpus) -> Limits {
        Limits {
            cpu_millis: cpu,
            memory_bytes: mem_gib << 30,
            gpus,
        }
    }

    #[test]
    fn total_budget_and_free_are_three_different_numbers() {
        let w = [worker("a", res(16.0, 12.0, 64, 40, 0, 0), None, 540.0)];
        let c = compute(&w, Some(&limits(8000, 32, Gpus::None)), Held::default());
        assert_eq!(c.cpu_cores.total, 16.0);
        assert_eq!(c.cpu_cores.budget, 8.0, "the policy lends half");
        assert_eq!(c.cpu_cores.free, 8.0, "12 are idle but only 8 are lent");
        assert_eq!(c.memory_bytes.total, 64 << 30);
        assert_eq!(c.memory_bytes.budget, 32 << 30);
        assert_eq!(c.memory_bytes.free, 32 << 30);
        assert_eq!(c.version, 2);
        assert_eq!(c.price_per_hour, Some(540.0));
    }

    #[test]
    fn what_offers_hold_comes_off_the_budget_and_idle_cores_cap_free() {
        let w = [worker("a", res(16.0, 3.0, 64, 40, 0, 0), None, 540.0)];
        let held = Held {
            cpus: 2.0,
            memory_bytes: 4 << 30,
            gpus: 0,
        };
        let c = compute(&w, Some(&limits(8000, 32, Gpus::None)), held);
        assert_eq!(c.cpu_cores.free, 3.0, "the worker only has 3 idle");
        let c = compute(&w, Some(&limits(4000, 32, Gpus::None)), held);
        assert_eq!(c.cpu_cores.free, 2.0, "budget 4 minus 2 held");
        assert_eq!(c.memory_bytes.free, 28 << 30);
    }

    #[test]
    fn budget_gpu_off_advertises_no_gpus_and_a_count_caps_them() {
        let w = [worker(
            "a",
            res(8.0, 8.0, 32, 32, 2, 2),
            Some(("RTX 4090", 24)),
            900.0,
        )];
        let c = compute(&w, Some(&limits(8000, 32, Gpus::None)), Held::default());
        assert_eq!(c.gpu[0].gpus.total, 2, "the machine still has them");
        assert_eq!(c.gpu[0].gpus.budget, 0, "the policy lends none");
        assert_eq!(c.gpu[0].gpus.free, 0);
        assert_eq!(c.free_gpus(None), 0);
        let c = compute(&w, Some(&limits(8000, 32, Gpus::Count(1))), Held::default());
        assert_eq!(c.gpu[0].gpus.budget, 1);
        let c = compute(&w, Some(&limits(8000, 32, Gpus::All)), Held::default());
        assert_eq!(c.gpu[0].gpus.budget, 2);
    }

    #[test]
    fn gpu_classes_keep_model_and_vram_apart() {
        let w = [
            worker(
                "a",
                res(8.0, 8.0, 32, 32, 2, 1),
                Some(("RTX 4090", 24)),
                900.0,
            ),
            worker(
                "b",
                res(8.0, 8.0, 32, 32, 1, 1),
                Some(("RTX 2080 Ti", 11)),
                400.0,
            ),
            worker(
                "c",
                res(8.0, 8.0, 32, 32, 2, 2),
                Some(("RTX 4090", 24)),
                800.0,
            ),
        ];
        let c = compute(&w, None, Held::default());
        assert_eq!(c.gpu.len(), 2);
        assert_eq!(
            c.gpu[0].model.as_deref(),
            Some("RTX 4090"),
            "biggest card first"
        );
        assert_eq!(c.gpu[0].vram_gb, Some(24));
        assert_eq!(
            c.gpu[0].gpus,
            Share {
                total: 4,
                budget: 4,
                free: 3
            }
        );
        assert_eq!(c.gpu[0].price_per_hour, Some(800.0));
        assert_eq!(c.gpu[1].gpus.free, 1);
        assert_eq!(c.free_gpus(Some(16)), 3);
        assert_eq!(c.free_gpus(Some(48)), 0);
        assert_eq!(c.free_gpus(None), 4);
        assert!(c.fits(1.0, 1 << 30, 2, Some(24)));
        assert!(!c.fits(1.0, 1 << 30, 4, Some(24)));
        assert_eq!(c.price_per_hour, Some(400.0));
    }

    #[test]
    fn a_gpu_count_budget_caps_the_classes_and_held_gpus_reduce_free() {
        let w = [worker(
            "a",
            res(8.0, 8.0, 32, 32, 4, 4),
            Some(("A100", 80)),
            1.0,
        )];
        let c = compute(&w, Some(&limits(8000, 32, Gpus::Count(2))), Held::default());
        assert_eq!(
            c.gpu[0].gpus,
            Share {
                total: 4,
                budget: 2,
                free: 2
            }
        );
        let held = Held {
            gpus: 1,
            ..Held::default()
        };
        let c = compute(&w, Some(&limits(8000, 32, Gpus::Count(2))), held);
        assert_eq!(c.gpu[0].gpus.free, 1);
    }

    #[test]
    fn only_healthy_local_workers_count() {
        let mut peer = worker("p", res(64.0, 64.0, 256, 256, 0, 0), None, 1.0);
        peer.source_node = Some("other".into());
        let mut down = worker("d", res(32.0, 32.0, 128, 128, 0, 0), None, 1.0);
        down.status = WorkerStatus::Unhealthy;
        let up = worker("u", res(4.0, 4.0, 8, 8, 0, 0), None, 7.0);
        let c = compute(&[peer, down, up], None, Held::default());
        assert_eq!((c.workers, c.cpu_cores.total), (1, 4.0));
        assert_eq!(c.price_per_hour, Some(7.0));
        assert_eq!(compute(&[], None, Held::default()).price_per_hour, None);
    }

    #[test]
    fn the_signature_ignores_noise_and_sees_real_changes() {
        let a = [worker("a", res(16.0, 12.2, 64, 40, 0, 0), None, 540.0)];
        let b = [worker("a", res(16.0, 12.7, 64, 40, 0, 0), None, 540.0)];
        let c = [worker("a", res(16.0, 11.2, 64, 40, 0, 0), None, 540.0)];
        let sig = |w: &[Worker]| compute(w, None, Held::default()).signature();
        assert_eq!(sig(&a), sig(&b), "12.2 and 12.7 free cores are one figure");
        assert_ne!(sig(&a), sig(&c));
    }

    #[test]
    fn it_round_trips_and_an_old_reader_can_ignore_it() {
        let c = compute(
            &[worker("a", res(2.0, 2.0, 4, 4, 0, 0), None, 540.0)],
            None,
            Held::default(),
        );
        let v = serde_json::to_value(&c).unwrap();
        assert_eq!(v["v"], 2);
        assert!(v["cpu_cores"]["budget"].is_number());
        assert_eq!(serde_json::from_value::<Capacity>(v).unwrap(), c);
    }
}