zc2 0.0.38

P2P compute broker with credit-based 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Opt-in periodic self-update for `zc broker` (`ZAKURO_AUTO_UPDATE=1`).
//!
//! LXD brokers run under systemd (`Restart=always`) from `/usr/local/bin/zc`
//! and used to fall behind: newer peers refuse a broker older than their own
//! version. When enabled, one background thread asks the release mirror for the
//! latest version every interval, waits until the broker is idle, installs
//! through the same verified path as `zc update` (sha256 + `--version` probe +
//! atomic rename, see `crate::update`), then exits with [`RESTART_EXIT_CODE`] so
//! systemd starts the new binary. OFF unless `ZAKURO_AUTO_UPDATE` is
//! `1|true|yes|on`: k8s fleets get their image from CD and must never use it.

use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use colored::Colorize;

use super::BrokerState;
use crate::update::{self, Fetcher, MirrorOutcome};

pub const ENV_ENABLE: &str = "ZAKURO_AUTO_UPDATE";
pub const ENV_INTERVAL: &str = "ZAKURO_AUTO_UPDATE_INTERVAL";
pub const DEFAULT_INTERVAL_SECS: u64 = 3600;
pub const MIN_INTERVAL_SECS: u64 = 300;
/// First check happens no sooner than this after startup.
const MIN_FIRST_DELAY_SECS: u64 = 60;
/// Idle must hold on this many consecutive polls...
const IDLE_POLLS_REQUIRED: u32 = 3;
/// ...taken this far apart.
const IDLE_POLL_SECS: u64 = 5;
/// Exit code asking systemd to restart us into the new binary (EX_TEMPFAIL).
pub const RESTART_EXIT_CODE: i32 = 75;

/// Parsed configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
    pub enabled: bool,
    pub interval_secs: u64,
}

/// Parse the two env values. Pure.
pub fn parse_config(enable: Option<&str>, interval: Option<&str>) -> Config {
    let enabled = enable.is_some_and(|v| {
        matches!(
            v.trim().to_ascii_lowercase().as_str(),
            "1" | "true" | "yes" | "on"
        )
    });
    let interval_secs = interval
        .and_then(|v| v.trim().parse::<u64>().ok())
        .unwrap_or(DEFAULT_INTERVAL_SECS)
        .max(MIN_INTERVAL_SECS);
    Config {
        enabled,
        interval_secs,
    }
}

pub fn config_from_env() -> Config {
    parse_config(
        std::env::var(ENV_ENABLE).ok().as_deref(),
        std::env::var(ENV_INTERVAL).ok().as_deref(),
    )
}

/// True only when `latest` is strictly newer than `current`; never a
/// downgrade or a reinstall. An unparseable version is not newer.
pub fn is_newer_version(latest: &str, current: &str) -> bool {
    match (
        crate::build_id::version_triple(latest),
        crate::build_id::version_triple(current),
    ) {
        (Some(l), Some(c)) => l > c,
        _ => false,
    }
}

/// The idle decision. Pure.
pub fn is_idle(in_flight: usize, local_worker_active: u64, settlement_pending: usize) -> bool {
    in_flight == 0 && local_worker_active == 0 && settlement_pending == 0
}

/// Snapshot for `/health`.
#[derive(Debug, Clone, Default)]
struct Status {
    enabled: bool,
    interval_secs: u64,
    last_check_unix: Option<u64>,
    latest_seen: Option<String>,
    last_error: Option<String>,
}

static STATUS: Mutex<Status> = Mutex::new(Status {
    enabled: false,
    interval_secs: DEFAULT_INTERVAL_SECS,
    last_check_unix: None,
    latest_seen: None,
    last_error: None,
});

fn with_status(f: impl FnOnce(&mut Status)) {
    if let Ok(mut s) = STATUS.lock() {
        f(&mut s);
    }
}

/// The `auto_update` object for `GET /health`.
pub fn health_json() -> serde_json::Value {
    let s = STATUS.lock().map(|s| s.clone()).unwrap_or_default();
    serde_json::json!({
        "enabled": s.enabled,
        "interval_secs": s.interval_secs,
        "last_check_unix": s.last_check_unix,
        "latest_seen": s.latest_seen,
        "last_error": s.last_error,
    })
}

fn now_unix() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// Cheap jitter without a rand dependency: sub-second clock noise mixed with
/// the pid, so brokers started together still spread out. In `[0, bound)`.
fn jitter(bound: u64) -> u64 {
    if bound == 0 {
        return 0;
    }
    let n = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.subsec_nanos() as u64)
        .unwrap_or(0);
    let mut x = n ^ ((std::process::id() as u64) << 32) ^ 0x9E37_79B9_7F4A_7C15;
    x ^= x >> 33;
    x = x.wrapping_mul(0xFF51_AFD7_ED55_8CCD);
    x ^= x >> 33;
    x % bound
}

fn log(msg: &str) {
    println!("  {} {}", "[AUTO-UPDATE]".cyan(), msg);
}

fn log_warn(msg: &str) {
    eprintln!("  {} {}", "[AUTO-UPDATE]".yellow(), msg);
}

/// Everything one check cycle touches, injected so it is testable.
pub struct Cycle<'a> {
    pub fetch: &'a dyn Fetcher,
    pub base: &'a str,
    pub platforms: &'a [String],
    pub current: &'a str,
    /// Is the broker idle right now?
    pub idle: &'a dyn Fn() -> bool,
    pub sleep: &'a dyn Fn(Duration),
    /// Install the (already announced) latest version.
    pub install: &'a dyn Fn() -> Result<MirrorOutcome, String>,
    pub interval: Duration,
}

/// What one cycle did.
#[derive(Debug, PartialEq, Eq)]
pub enum CycleResult {
    UpToDate(String),
    NotIdle(String),
    Installed(String),
    InstallFailed(String),
    CheckFailed(String),
}

/// One check: ask the mirror, and only if strictly newer wait for idle and install.
pub fn run_cycle(c: &Cycle) -> CycleResult {
    let latest = match update::check_latest_via(c.fetch, c.base, c.platforms) {
        Ok(v) => v,
        Err(e) => {
            let e = format!("check failed (mirror {}): {e}", c.base);
            with_status(|s| {
                s.last_check_unix = Some(now_unix());
                s.last_error = Some(e.clone());
            });
            log_warn(&e);
            return CycleResult::CheckFailed(e);
        }
    };
    with_status(|s| {
        s.last_check_unix = Some(now_unix());
        s.latest_seen = Some(latest.clone());
        s.last_error = None;
    });
    if !is_newer_version(&latest, c.current) {
        log(&format!(
            "running {}, latest {latest}: up to date",
            c.current
        ));
        return CycleResult::UpToDate(latest);
    }
    log(&format!(
        "running {}, latest {latest} (mirror {}): newer, waiting for idle",
        c.current, c.base
    ));
    // Idle on IDLE_POLLS_REQUIRED consecutive polls; otherwise keep polling
    // until the interval is spent, then give up until the next check.
    let polls_budget = (c.interval.as_secs() / IDLE_POLL_SECS).max(IDLE_POLLS_REQUIRED as u64);
    let mut streak = 0;
    let mut idle_confirmed = false;
    for _ in 0..polls_budget {
        if (c.idle)() {
            streak += 1;
            if streak >= IDLE_POLLS_REQUIRED {
                idle_confirmed = true;
                break;
            }
        } else {
            streak = 0;
        }
        (c.sleep)(Duration::from_secs(IDLE_POLL_SECS));
    }
    if !idle_confirmed {
        log(&format!(
            "broker never idle for {} polls; giving up on {latest} until the next check",
            IDLE_POLLS_REQUIRED
        ));
        return CycleResult::NotIdle(latest);
    }
    match (c.install)() {
        Ok(MirrorOutcome::Installed { version, .. }) => CycleResult::Installed(version),
        Ok(MirrorOutcome::UpToDate(v)) => CycleResult::UpToDate(v),
        Err(e) => {
            let e = format!("install failed: {e}; will retry at the next check");
            with_status(|s| s.last_error = Some(e.clone()));
            log_warn(&e);
            CycleResult::InstallFailed(e)
        }
    }
}

/// Block until `idle` holds on IDLE_POLLS_REQUIRED consecutive polls, with no
/// deadline: used after the binary is swapped, when restarting is the only
/// thing left to do.
pub fn wait_until_idle(idle: &dyn Fn() -> bool, sleep: &dyn Fn(Duration)) {
    let mut streak = 0;
    loop {
        if idle() {
            streak += 1;
            if streak >= IDLE_POLLS_REQUIRED {
                return;
            }
        } else {
            streak = 0;
        }
        sleep(Duration::from_secs(IDLE_POLL_SECS));
    }
}

/// Spawn the updater thread when enabled. Never panics the broker.
pub fn spawn(state: Arc<BrokerState>) {
    let cfg = config_from_env();
    with_status(|s| {
        s.enabled = cfg.enabled;
        s.interval_secs = cfg.interval_secs;
    });
    if !cfg.enabled {
        return;
    }
    log(&format!(
        "enabled: checking the mirror every {}s (+jitter), installing only when idle",
        cfg.interval_secs
    ));
    let spawned = std::thread::Builder::new()
        .name("zc-auto-update".into())
        .spawn(move || {
            let interval = Duration::from_secs(cfg.interval_secs);
            let first = MIN_FIRST_DELAY_SECS
                + jitter(
                    cfg.interval_secs
                        .saturating_sub(MIN_FIRST_DELAY_SECS)
                        .max(1),
                );
            std::thread::sleep(Duration::from_secs(first));
            let fetch = update::UreqFetcher::new();
            loop {
                let base = update::mirror_base();
                if let Err(e) = &base {
                    log_warn(e);
                    with_status(|s| s.last_error = Some(e.clone()));
                } else if let (Some(platforms), Ok(base)) =
                    (update::host_mirror_platforms(), base.as_ref())
                {
                    let idle = || {
                        let local_active: u64 = state
                            .workers
                            .list()
                            .iter()
                            .filter(|w| w.source_node.is_none() && state.is_local_worker(&w.uri))
                            .map(|w| w.active_requests as u64)
                            .sum();
                        is_idle(
                            state.active_requests.len(),
                            local_active,
                            state.tx_buffer.pending_count(),
                        )
                    };
                    let result = run_cycle(&Cycle {
                        fetch: &fetch,
                        base,
                        platforms: &platforms,
                        current: env!("CARGO_PKG_VERSION"),
                        idle: &idle,
                        sleep: &std::thread::sleep,
                        install: &update::install_latest_from_mirror,
                        interval,
                    });
                    if let CycleResult::Installed(v) = result {
                        // The download took time and requests may have arrived
                        // since the idle check. The new binary is already in
                        // place, so wait as long as it takes for idle again
                        // rather than cut a request off with the restart.
                        log(&format!("installed {v}, restarting once idle"));
                        wait_until_idle(&idle, &std::thread::sleep);
                        log(&format!("installed {v}, restarting"));
                        if let (Some(url), Some(key)) =
                            (&state.config.api_url, &state.config.api_key)
                        {
                            state.tx_buffer.flush_to_api(url, key, &state.wal);
                        }
                        let _ = state.wal.flush_buffer();
                        std::process::exit(RESTART_EXIT_CODE);
                    }
                } else {
                    with_status(|s| {
                        s.last_error = Some("no mirror build for this OS/architecture".into())
                    });
                }
                let extra = jitter(cfg.interval_secs / 10 + 1);
                std::thread::sleep(interval + Duration::from_secs(extra));
            }
        });
    if let Err(e) = spawned {
        log_warn(&format!("could not start updater thread: {e}"));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::update::FetchError;
    use std::cell::{Cell, RefCell};
    use std::collections::HashMap;

    #[test]
    fn env_enabled_values() {
        for v in ["1", "true", "TRUE", "yes", "On", " on "] {
            assert!(parse_config(Some(v), None).enabled, "{v}");
        }
        for v in ["0", "false", "no", "off", "", "enabled", "2"] {
            assert!(!parse_config(Some(v), None).enabled, "{v}");
        }
        assert!(!parse_config(None, None).enabled);
    }

    #[test]
    fn env_interval_default_and_clamp() {
        assert_eq!(parse_config(None, None).interval_secs, 3600);
        assert_eq!(parse_config(None, Some("garbage")).interval_secs, 3600);
        assert_eq!(parse_config(None, Some("10")).interval_secs, 300);
        assert_eq!(parse_config(None, Some("300")).interval_secs, 300);
        assert_eq!(parse_config(None, Some(" 7200 ")).interval_secs, 7200);
    }

    #[test]
    fn newer_version_decision() {
        assert!(is_newer_version("0.0.10", "0.0.9"));
        assert!(!is_newer_version("0.0.9", "0.0.10"));
        assert!(!is_newer_version("0.0.9", "0.0.9"));
        assert!(!is_newer_version("0.0.8", "0.0.9"));
        assert!(is_newer_version("v0.1.0", "0.0.99"));
        assert!(!is_newer_version("junk", "0.0.1"));
    }

    #[test]
    fn wait_until_idle_needs_a_fresh_streak() {
        // busy, idle, busy, then idle x3: returns only after the last streak.
        let seq = [false, true, false, true, true, true];
        let i = Cell::new(0);
        let slept = Cell::new(0);
        wait_until_idle(
            &|| {
                let v = seq[i.get()];
                i.set(i.get() + 1);
                v
            },
            &|_| slept.set(slept.get() + 1),
        );
        assert_eq!(i.get(), seq.len());
        assert_eq!(slept.get(), seq.len() - 1);
    }

    #[test]
    fn idle_decision() {
        assert!(is_idle(0, 0, 0));
        assert!(!is_idle(1, 0, 0));
        assert!(!is_idle(0, 2, 0));
        assert!(!is_idle(0, 0, 3));
    }

    struct Mock(HashMap<String, Vec<u8>>, RefCell<Vec<String>>);
    impl Fetcher for Mock {
        fn get(&self, url: &str) -> Result<Vec<u8>, FetchError> {
            self.1.borrow_mut().push(url.to_string());
            self.0.get(url).cloned().ok_or(FetchError::Status(404))
        }
    }

    const BASE: &str = "https://mirror.test/zc";

    fn mirror(version: &str, bin: &[u8]) -> Mock {
        let mut m = HashMap::new();
        m.insert(
            format!("{BASE}/manifest/linux_amd64"),
            serde_json::json!({"version": version, "sha256": crate::dataset::sha256_hex(bin)})
                .to_string()
                .into_bytes(),
        );
        m.insert(
            format!("{BASE}/download/linux_amd64?version={version}"),
            bin.to_vec(),
        );
        Mock(m, RefCell::new(vec![]))
    }

    fn temp_exe(tag: &str) -> std::path::PathBuf {
        let dir = std::env::temp_dir().join(format!("zc-autoupd-{tag}-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let exe = dir.join("zc");
        std::fs::write(&exe, b"old").unwrap();
        exe
    }

    fn cycle<'a>(
        f: &'a Mock,
        plats: &'a [String],
        current: &'a str,
        idle: &'a dyn Fn() -> bool,
        sleep: &'a dyn Fn(Duration),
        install: &'a dyn Fn() -> Result<MirrorOutcome, String>,
    ) -> Cycle<'a> {
        Cycle {
            fetch: f,
            base: BASE,
            platforms: plats,
            current,
            idle,
            sleep,
            install,
            interval: Duration::from_secs(300),
        }
    }

    #[test]
    fn cycle_installs_newer_when_idle() {
        let exe = temp_exe("ok");
        let f = mirror("0.0.10", b"new bin");
        let plats = vec!["linux_amd64".to_string()];
        let install = || {
            update::update_via_mirror(&f, BASE, &plats, "0.0.9", &exe, &|_, _| true)
                .map_err(|e| format!("{e:?}"))
        };
        let r = run_cycle(&cycle(&f, &plats, "0.0.9", &|| true, &|_| {}, &install));
        assert_eq!(r, CycleResult::Installed("0.0.10".into()));
        assert_eq!(std::fs::read(&exe).unwrap(), b"new bin");
    }

    #[test]
    fn cycle_skips_equal_and_older_without_installing() {
        let f = mirror("0.0.9", b"x");
        let plats = vec!["linux_amd64".to_string()];
        let called = Cell::new(false);
        let install = || {
            called.set(true);
            Err("no".to_string())
        };
        for cur in ["0.0.9", "0.0.10"] {
            let r = run_cycle(&cycle(&f, &plats, cur, &|| true, &|_| {}, &install));
            assert_eq!(r, CycleResult::UpToDate("0.0.9".into()));
        }
        assert!(!called.get());
    }

    #[test]
    fn cycle_requires_three_consecutive_idle_polls() {
        let f = mirror("0.0.10", b"x");
        let plats = vec!["linux_amd64".to_string()];
        // idle, busy, then idle forever: streak resets, so 1 + 1 + 3 polls.
        let n = Cell::new(0);
        let idle = || {
            n.set(n.get() + 1);
            n.get() != 2
        };
        let installed = Cell::new(false);
        let install = || {
            installed.set(true);
            Ok(MirrorOutcome::Installed {
                version: "0.0.10".into(),
                platform: "linux_amd64".into(),
            })
        };
        let r = run_cycle(&cycle(&f, &plats, "0.0.9", &idle, &|_| {}, &install));
        assert_eq!(r, CycleResult::Installed("0.0.10".into()));
        assert_eq!(n.get(), 5);
        assert!(installed.get());
    }

    #[test]
    fn cycle_gives_up_when_never_idle_and_never_installs() {
        let f = mirror("0.0.10", b"x");
        let plats = vec!["linux_amd64".to_string()];
        let install = || panic!("must not install while busy");
        let r = run_cycle(&cycle(&f, &plats, "0.0.9", &|| false, &|_| {}, &install));
        assert_eq!(r, CycleResult::NotIdle("0.0.10".into()));
    }

    #[test]
    fn cycle_reports_install_and_check_failures() {
        let f = mirror("0.0.10", b"x");
        let plats = vec!["linux_amd64".to_string()];
        let install = || Err("dir not writable".to_string());
        let r = run_cycle(&cycle(&f, &plats, "0.0.9", &|| true, &|_| {}, &install));
        assert!(matches!(r, CycleResult::InstallFailed(_)));
        let other = vec!["darwin_arm64".to_string()];
        let r = run_cycle(&cycle(&f, &other, "0.0.9", &|| true, &|_| {}, &install));
        assert!(matches!(r, CycleResult::CheckFailed(_)));
    }

    #[test]
    fn jitter_is_bounded() {
        for _ in 0..100 {
            assert!(jitter(60) < 60);
        }
        assert_eq!(jitter(0), 0);
    }
}