kamaji 0.8.46

Kamaji — yah's relocatable workload supervisor. Trait surface + supporting types; backend impls land in R484-T2. See .yah/docs/working/W199-kamaji-universal-supervisor.md.
Documentation
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
//! The restart loop, shared by every backend that supervises a host process
//! (R605-F31).
//!
//! # Why this is one module and not one per backend
//!
//! It used to be one, in [`crate::native`], and [`crate::microvm`]'s module
//! heading said in so many words that it "deliberately does not implement the
//! restart loop `crate::native` carries". That was true of a backend that could
//! only run jobs. The moment a microVM has to be able to be a *service* — a
//! long-lived guest that is restarted when it dies rather than reaped when it
//! exits — the choice is between a second restart loop and one shared one, and
//! a second one is the worse artifact: the two would agree on the day they were
//! written and drift on every day after it, and the bugs that produces are the
//! kind where a workload is restarted on one backend and not on another for
//! reasons nobody can name.
//!
//! So the state machine lives here, once, and a backend supplies the four
//! things that genuinely differ:
//!
//! | [`Supervised`] method | native | microVM |
//! |---|---|---|
//! | [`start`](Supervised::start) | fork+exec the binary | boot a guest from the VM config on disk |
//! | [`wait`](Supervised::wait) | wait on the child | wait on the VMM process |
//! | [`stop`](Supervised::stop) | SIGTERM → 5s → SIGKILL | SIGTERM (a guest power-off) → 10s → SIGKILL |
//! | [`settle`](Supervised::settle) | read the exit status | extract artifacts, free the TAP, read the guest's own status document |
//!
//! Everything else — the policy decision, the backoff, the `Restarting`
//! publication, the parked phase, the control-message handling — is identical
//! by construction rather than by inspection.
//!
//! # What the loop guarantees
//!
//! - **One owner of the instance.** The supervisor task holds it; every
//!   lifecycle operation that touches it arrives as a [`Ctrl`] message. There is
//!   no path where a trait method and the restart loop both act on the same
//!   process.
//! - **A terminal exit parks rather than ends.** The supervisor stays alive with
//!   no instance running, so a later `Restart` can revive the workload instead
//!   of finding a dead channel. This is what makes `RestartPolicy::Never` mean
//!   "the *supervisor* does not decide to restart" rather than "the workload can
//!   never run again".
//! - **Backoff is interruptible.** A teardown during a backoff wins immediately
//!   instead of waiting out the delay.
//!
//! Part of R605-F31; the board annotation lives on [`crate::microvm`].

use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use anyhow::Result;
use async_trait::async_trait;
use tokio::sync::{mpsc, oneshot, watch};
use workload_spec::{BackoffPolicy, RestartPolicy};

use crate::WorkloadStatus;

/// Fixed delay before an unconditional ([`RestartPolicy::Always`]) respawn, so a
/// workload that exits immediately can't spin the supervisor into a hot loop.
const ALWAYS_RESTART_DELAY: Duration = Duration::from_secs(1);

/// How one supervised instance ended, as the OS reported it.
///
/// Both backends supervise a `tokio::process::Child` — a forked binary on one,
/// the VMM process on the other — so this is the same type on both, and the
/// *interpretation* of it is what [`Supervised::settle`] exists to vary.
pub(crate) type Exit = std::io::Result<std::process::ExitStatus>;

/// What a backend made of an instance's exit.
///
/// The split between `succeeded` and `terminal` is load-bearing. `succeeded`
/// answers the restart policy's question ("was this a failure?"); `terminal` is
/// the status published only if no restart follows. Keeping them apart is what
/// lets the microVM backend report *the job's* exit code — read out of the
/// guest's own status document — while the policy still sees a single boolean.
pub(crate) struct Completion {
    /// Whether this run counts as a success for [`RestartPolicy::OnFailure`].
    pub succeeded: bool,
    /// Exit code to publish in [`WorkloadStatus::Restarting`].
    pub exit_code: i32,
    /// Status to publish if the supervisor is not going to restart.
    pub terminal: WorkloadStatus,
}

/// Control messages from a backend's trait methods to a workload's supervisor
/// task.
///
/// Generic over the backend's instance type purely because of [`Self::Adopt`],
/// which hands the supervisor a *pre-spawned* replacement.
pub(crate) enum Ctrl<I> {
    /// Stop the instance and end supervision (no further restarts).
    Teardown(oneshot::Sender<()>),
    /// Stop the current instance and immediately start a fresh one. Acks with
    /// the new pid (or the start error).
    Restart(oneshot::Sender<Result<u32>>),
    /// Adopt a pre-started replacement as the supervised instance (graceful
    /// upgrade). The outgoing one is handed to [`Supervised::discard`].
    ///
    /// Only [`crate::native`] sends this — a graceful upgrade is a listener
    /// handoff between two host processes, and a microVM has no equivalent — so
    /// a build with the microVM backend and not the native one sees a variant
    /// nothing constructs. That is the truth about the build, not dead code.
    #[cfg_attr(not(feature = "native-integration"), allow(dead_code))]
    Adopt {
        replacement: I,
        ack: oneshot::Sender<()>,
    },
}

/// What a backend must provide for [`supervise`] to run its restart loop.
#[async_trait]
pub(crate) trait Supervised: Send + Sync + 'static {
    /// One running instance: a forked child, a booted guest.
    type Instance: Send + 'static;

    /// OS pid of the process that represents this instance.
    fn pid(&self, inst: &Self::Instance) -> u32;

    /// Wait for the instance to end.
    async fn wait(&self, inst: &mut Self::Instance) -> Exit;

    /// Start a fresh instance from the retained definition.
    ///
    /// Called for every restart, so whatever it reads must be the *current*
    /// definition — the supervisor retains it precisely so a respawn cannot run
    /// something the deploy did not describe.
    async fn start(&self) -> Result<Self::Instance>;

    /// Stop a running instance: signal, grace, kill. Each backend owns its own
    /// grace window because they are not the same duration or the same signal
    /// semantics (a SIGTERM to a VMM is a guest power-off, not a guest signal).
    async fn stop(&self, inst: &mut Self::Instance);

    /// Everything that must happen once an instance has ended, and the verdict
    /// on how it ended.
    ///
    /// This is the backend's whole post-exit hook, not just a classifier: the
    /// microVM backend copies artifacts back out of the scratch disk here,
    /// because that can only be done once the guest has released the block
    /// device.
    async fn settle(&self, exit: Exit) -> Completion;

    /// Dispose of an instance replaced by [`Ctrl::Adopt`]. Defaults to dropping
    /// it; a backend with a drain protocol overrides.
    fn discard(&self, _replaced: Self::Instance) {}
}

/// Outcome of the running-phase select: the instance ended, or a control
/// message arrived (`None` = the channel closed, i.e. the runtime was dropped).
enum Ev<I> {
    Exited(Exit),
    Ctrl(Option<Ctrl<I>>),
}

/// Exponential backoff delay for the `attempt`-th consecutive `OnFailure`
/// restart (1-based): `initial_ms * multiplier^(attempt-1)`, capped at `max_ms`.
fn backoff_delay(b: &BackoffPolicy, attempt: u32) -> Duration {
    let factor = (b.multiplier as f64).powi(attempt.saturating_sub(1) as i32);
    let ms = (b.initial_ms as f64 * factor).min(b.max_ms as f64);
    Duration::from_millis(ms as u64)
}

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

/// Parked phase: no instance is running (a terminal exit, or a start that
/// failed). Wait for a control message that either revives the workload
/// (`Restart`/`Adopt`) or ends supervision (`Teardown` / channel closed).
async fn park<S: Supervised>(
    sup: &S,
    ctrl_rx: &mut mpsc::Receiver<Ctrl<S::Instance>>,
    pid: &Arc<AtomicU32>,
    status_tx: &watch::Sender<WorkloadStatus>,
) -> Option<S::Instance> {
    loop {
        match ctrl_rx.recv().await {
            None => return None,
            Some(Ctrl::Teardown(ack)) => {
                let _ = status_tx.send(WorkloadStatus::Stopped);
                let _ = ack.send(());
                return None;
            }
            Some(Ctrl::Adopt { replacement, ack }) => {
                pid.store(sup.pid(&replacement), Ordering::SeqCst);
                let _ = status_tx.send(WorkloadStatus::Running);
                let _ = ack.send(());
                return Some(replacement);
            }
            Some(Ctrl::Restart(ack)) => match sup.start().await {
                Ok(new) => {
                    let p = sup.pid(&new);
                    pid.store(p, Ordering::SeqCst);
                    let _ = status_tx.send(WorkloadStatus::Running);
                    let _ = ack.send(Ok(p));
                    return Some(new);
                }
                Err(e) => {
                    let _ = status_tx.send(WorkloadStatus::Failed {
                        reason: format!("restart respawn failed: {e}"),
                        oom_killed: false,
                    });
                    let _ = ack.send(Err(e));
                    // stay parked, wait for the next control message
                }
            },
        }
    }
}

/// Per-workload supervisor: owns the live instance, restarts it per `policy`,
/// and serves control messages. Exits when torn down or when the control
/// channel closes.
pub(crate) async fn supervise<S: Supervised>(
    sup: S,
    policy: RestartPolicy,
    initial: S::Instance,
    pid: Arc<AtomicU32>,
    status_tx: watch::Sender<WorkloadStatus>,
    mut ctrl_rx: mpsc::Receiver<Ctrl<S::Instance>>,
) {
    let mut inst = initial;
    // Consecutive failed exits, for OnFailure's max_attempts/backoff. Reset on a
    // clean exit, an in-place restart, or an adopt.
    let mut failure_streak: u32 = 0;
    // Total restarts, for the Restarting status payload.
    let mut restart_count: u32 = 0;

    loop {
        // ── RUNNING: own `inst`; wait for it to end or a control message. Both
        //    futures are dropped before the handler runs, so the handler is free
        //    to move `inst`.
        let ev = tokio::select! {
            r = sup.wait(&mut inst) => Ev::Exited(r),
            c = ctrl_rx.recv() => Ev::Ctrl(c),
        };

        match ev {
            // Control channel closed — the runtime was dropped. Stop and exit.
            Ev::Ctrl(None) => {
                sup.stop(&mut inst).await;
                return;
            }
            Ev::Ctrl(Some(Ctrl::Teardown(ack))) => {
                sup.stop(&mut inst).await;
                pid.store(0, Ordering::SeqCst);
                let _ = status_tx.send(WorkloadStatus::Stopped);
                let _ = ack.send(());
                return;
            }
            Ev::Ctrl(Some(Ctrl::Restart(ack))) => {
                sup.stop(&mut inst).await;
                pid.store(0, Ordering::SeqCst);
                match sup.start().await {
                    Ok(new) => {
                        restart_count += 1;
                        failure_streak = 0;
                        let p = sup.pid(&new);
                        pid.store(p, Ordering::SeqCst);
                        let _ = status_tx.send(WorkloadStatus::Running);
                        inst = new;
                        let _ = ack.send(Ok(p));
                    }
                    Err(e) => {
                        let _ = status_tx.send(WorkloadStatus::Failed {
                            reason: format!("restart respawn failed: {e}"),
                            oom_killed: false,
                        });
                        let _ = ack.send(Err(e));
                        match park(&sup, &mut ctrl_rx, &pid, &status_tx).await {
                            Some(new) => {
                                restart_count += 1;
                                failure_streak = 0;
                                inst = new;
                            }
                            None => return,
                        }
                    }
                }
            }
            Ev::Ctrl(Some(Ctrl::Adopt { replacement, ack })) => {
                // Graceful upgrade: the caller already started + settled the
                // replacement. Dispose of the outgoing instance and adopt the
                // new one; the loop now supervises the replacement.
                sup.discard(inst);
                restart_count += 1;
                failure_streak = 0;
                pid.store(sup.pid(&replacement), Ordering::SeqCst);
                let _ = status_tx.send(WorkloadStatus::Running);
                inst = replacement;
                let _ = ack.send(());
            }
            Ev::Exited(exit) => {
                pid.store(0, Ordering::SeqCst);
                let done = sup.settle(exit).await;

                let should_restart = match &policy {
                    RestartPolicy::Always => true,
                    RestartPolicy::Never => false,
                    RestartPolicy::OnFailure { max_attempts, .. } => {
                        !done.succeeded && failure_streak < *max_attempts
                    }
                };

                if !should_restart {
                    // Terminal: park until a control message arrives.
                    let _ = status_tx.send(done.terminal);
                    match park(&sup, &mut ctrl_rx, &pid, &status_tx).await {
                        Some(new) => {
                            restart_count += 1;
                            failure_streak = 0;
                            inst = new;
                            continue;
                        }
                        None => return,
                    }
                }

                // Restarting: publish the rich status, back off, start again.
                restart_count += 1;
                if !done.succeeded {
                    failure_streak += 1;
                }
                let _ = status_tx.send(WorkloadStatus::Restarting {
                    last_exit_code: done.exit_code,
                    restart_count,
                    last_finished_at_unix_ms: now_ms(),
                });

                let delay = match &policy {
                    RestartPolicy::Always => ALWAYS_RESTART_DELAY,
                    RestartPolicy::OnFailure { backoff, .. } => {
                        backoff_delay(backoff, failure_streak)
                    }
                    // Unreachable: should_restart is false for Never.
                    RestartPolicy::Never => Duration::ZERO,
                };

                // Interruptible backoff: a teardown (or a closed channel) during
                // the wait wins instead of blocking for the whole delay.
                let interrupted = tokio::select! {
                    _ = tokio::time::sleep(delay) => None,
                    c = ctrl_rx.recv() => Some(c),
                };
                match interrupted {
                    None => {} // backoff elapsed → fall through to start
                    Some(None) => return,
                    Some(Some(Ctrl::Teardown(ack))) => {
                        let _ = status_tx.send(WorkloadStatus::Stopped);
                        let _ = ack.send(());
                        return;
                    }
                    Some(Some(Ctrl::Adopt { replacement, ack })) => {
                        failure_streak = 0;
                        pid.store(sup.pid(&replacement), Ordering::SeqCst);
                        let _ = status_tx.send(WorkloadStatus::Running);
                        inst = replacement;
                        let _ = ack.send(());
                        continue;
                    }
                    Some(Some(Ctrl::Restart(ack))) => match sup.start().await {
                        Ok(new) => {
                            failure_streak = 0;
                            let p = sup.pid(&new);
                            pid.store(p, Ordering::SeqCst);
                            let _ = status_tx.send(WorkloadStatus::Running);
                            inst = new;
                            let _ = ack.send(Ok(p));
                            continue;
                        }
                        Err(e) => {
                            let _ = status_tx.send(WorkloadStatus::Failed {
                                reason: format!("restart respawn failed: {e}"),
                                oom_killed: false,
                            });
                            let _ = ack.send(Err(e));
                            match park(&sup, &mut ctrl_rx, &pid, &status_tx).await {
                                Some(new) => {
                                    failure_streak = 0;
                                    inst = new;
                                    continue;
                                }
                                None => return,
                            }
                        }
                    },
                }

                // Backoff elapsed uninterrupted: start again.
                match sup.start().await {
                    Ok(new) => {
                        pid.store(sup.pid(&new), Ordering::SeqCst);
                        let _ = status_tx.send(WorkloadStatus::Running);
                        inst = new;
                    }
                    Err(e) => {
                        let _ = status_tx.send(WorkloadStatus::Failed {
                            reason: format!("respawn failed: {e}"),
                            oom_killed: false,
                        });
                        match park(&sup, &mut ctrl_rx, &pid, &status_tx).await {
                            Some(new) => {
                                failure_streak = 0;
                                inst = new;
                            }
                            None => return,
                        }
                    }
                }
            }
        }
    }
}