arcbox-api 0.6.3

API server for ArcBox (gRPC + REST)
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
//! System service — daemon readiness, version, and diagnostics.
//!
//! Provides setup status queries so the desktop app (or any client) can
//! observe daemon readiness without managing its lifecycle.
//!
//! `GetVirtioDebug` is deliberately served from `early_runtime` rather than
//! the ready handle: a boot that never reaches READY is its main use case.

use std::sync::Arc;

use arcbox_connect::v1 as pb;
use arcbox_connect::v1::{SetupStatus, SystemVmBackend, setup_status};
use connectrpc::{
    ConnectError, RequestContext, Response, ServiceRequest, ServiceResult, ServiceStream,
};
use tokio::sync::{broadcast, watch};

use super::SharedRuntime;

use super::ConnectRuntimeExt as _;

/// Buffered updates per streaming client. Startup publishes on the order of a
/// dozen; the margin covers a client that stalls briefly mid-boot without
/// dropping a phase.
const UPDATE_BUFFER: usize = 64;

/// Shared state tracking daemon startup progress.
///
/// Updated by the daemon as it progresses through startup phases.
/// Observed by `SystemServiceImpl` to serve gRPC queries.
#[derive(Debug, Clone)]
pub struct SetupState {
    /// Latest snapshot, for `GetSetupStatus` and for seeding a new stream.
    tx: Arc<watch::Sender<SetupStatus>>,
    /// Every update, in order.
    ///
    /// A `watch` keeps only the newest value, so two updates in quick
    /// succession collapse into one for a subscriber that has not polled in
    /// between — and a phase a client never receives is indistinguishable
    /// from one the daemon never published. `NETWORK_READY` and `READY` are
    /// ~300 µs apart with no await point between them, so on that pair the
    /// collapse is a live race rather than a theoretical one.
    updates: broadcast::Sender<SetupStatus>,
}

impl SetupState {
    /// Creates a new setup state starting in the INITIALIZING phase.
    pub fn new() -> Self {
        let initial = SetupStatus {
            phase: setup_status::Phase::Initializing.into(),
            message: "Daemon starting...".to_string(),
            ..Default::default()
        };
        let (tx, _) = watch::channel(initial);
        Self {
            tx: Arc::new(tx),
            updates: broadcast::channel(UPDATE_BUFFER).0,
        }
    }

    /// Applies an update to the snapshot and publishes it to every stream.
    ///
    /// The broadcast happens inside `send_modify`, under the write lock, so a
    /// concurrent publisher cannot interleave the two halves and deliver
    /// updates in an order the snapshot never passed through — recovery and
    /// the route loop write flags while startup writes phases.
    fn publish(&self, update: impl FnOnce(&mut SetupStatus)) {
        self.tx.send_modify(|status| {
            update(status);
            // Fails only when nobody is streaming, which is the common case.
            let _ = self.updates.send(status.clone());
        });
    }

    /// Updates the current setup phase.
    pub fn set_phase(&self, phase: setup_status::Phase, message: &str) {
        self.publish(|s| {
            s.phase = phase.into();
            message.clone_into(&mut s.message);
        });
    }

    /// Marks startup as fatally failed. The daemon exits shortly after;
    /// the error rides the final update so streaming clients learn
    /// the cause instead of seeing a bare disconnect.
    pub fn set_failed(&self, error: &str) {
        self.publish(|s| {
            s.phase = setup_status::Phase::Failed.into();
            "Daemon startup failed".clone_into(&mut s.message);
            error.clone_into(&mut s.error);
        });
    }

    /// Updates a specific infrastructure flag.
    pub fn set_dns_installed(&self, installed: bool) {
        self.publish(|s| s.dns_resolver_installed = installed);
    }

    pub fn set_docker_socket_linked(&self, linked: bool) {
        self.publish(|s| s.docker_socket_linked = linked);
    }

    pub fn set_route_installed(&self, installed: bool) {
        self.publish(|s| s.route_installed = installed);
    }

    pub fn set_vm_running(&self, running: bool) {
        self.publish(|s| s.vm_running = running);
    }

    pub fn set_docker_tools_installed(&self, installed: bool) {
        self.publish(|s| s.docker_tools_installed = installed);
    }

    /// Returns the current snapshot plus a receiver for every update after it.
    ///
    /// Both are taken while holding the snapshot's read lock, which excludes
    /// [`Self::publish`]'s write lock. Subscribing after reading the snapshot
    /// would drop an update landing between the two; reading the snapshot
    /// after subscribing could replay one already folded into it, walking a
    /// client's phase backwards.
    fn subscribe(&self) -> (SetupStatus, broadcast::Receiver<SetupStatus>) {
        let snapshot = self.tx.borrow();
        let updates = self.updates.subscribe();
        (snapshot.clone(), updates)
    }

    /// Returns the current status snapshot.
    pub fn current(&self) -> SetupStatus {
        self.tx.borrow().clone()
    }
}

impl Default for SetupState {
    fn default() -> Self {
        Self::new()
    }
}

/// SystemService gRPC implementation.
pub struct SystemServiceImpl {
    setup_state: Arc<SetupState>,
    runtime: SharedRuntime,
    /// Diagnostics handle, filled as soon as the runtime is constructed
    /// (before the VM boots) so `GetVirtioDebug` can observe a stuck
    /// boot while `runtime` is still empty.
    early_runtime: SharedRuntime,
}

impl SystemServiceImpl {
    /// Creates a new system service.
    pub fn new(
        setup_state: Arc<SetupState>,
        runtime: SharedRuntime,
        early_runtime: SharedRuntime,
    ) -> Self {
        Self {
            setup_state,
            runtime,
            early_runtime,
        }
    }
}

/// Maps the wire enum to the core backend, or `None` for the unspecified value.
fn backend_from_proto(backend: SystemVmBackend) -> Option<arcbox_core::VmBackend> {
    match backend {
        SystemVmBackend::Hv => Some(arcbox_core::VmBackend::Hv),
        SystemVmBackend::Vz => Some(arcbox_core::VmBackend::Vz),
        SystemVmBackend::Unspecified => None,
    }
}

/// Maps the core backend to the wire enum.
fn backend_to_proto(backend: arcbox_core::VmBackend) -> SystemVmBackend {
    match backend {
        arcbox_core::VmBackend::Hv => SystemVmBackend::Hv,
        arcbox_core::VmBackend::Vz => SystemVmBackend::Vz,
    }
}

/// Maps a core virtio device snapshot to the wire message. (A `From`
/// impl is impossible here — both types are foreign to this crate.)
fn device_debug_to_proto(device: arcbox_core::DeviceDebug) -> pb::VirtioDeviceDebug {
    pb::VirtioDeviceDebug {
        id: device.id,
        device_type: device.device_type,
        name: device.name,
        status: u32::from(device.status),
        interrupt_status: device.interrupt_status,
        event_idx: device.event_idx,
        interrupts: device.interrupts,
        queues: device
            .queues
            .into_iter()
            .map(|queue| pb::VirtioQueueDebug {
                index: u32::from(queue.index),
                size: u32::from(queue.size),
                ready: queue.ready,
                kicks: queue.kicks,
                avail_idx: queue.avail_idx.map(u32::from),
                used_idx: queue.used_idx.map(u32::from),
                avail_flags: queue.avail_flags.map(u32::from),
                used_flags: queue.used_flags.map(u32::from),
                used_event: queue.used_event.map(u32::from),
                avail_event: queue.avail_event.map(u32::from),
                ..Default::default()
            })
            .collect(),
        ..Default::default()
    }
}

#[allow(
    refining_impl_trait,
    reason = "the trait returns `impl Encodable<M>`; naming the concrete body \
              type is strictly more informative and these impls are registered on a \
              Router rather than named by callers"
)]
impl pb::SystemService for SystemServiceImpl {
    async fn get_info(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::GetInfoRequest>,
    ) -> ServiceResult<pb::GetInfoResponse> {
        // TODO: Populate from runtime.
        Err(ConnectError::unimplemented("get_info not yet implemented"))
    }

    async fn get_version(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::GetVersionRequest>,
    ) -> ServiceResult<pb::GetVersionResponse> {
        let resp = pb::GetVersionResponse {
            version: env!("CARGO_PKG_VERSION").to_string(),
            api_version: "1.0".to_string(),
            min_api_version: "1.0".to_string(),
            ..Default::default()
        };
        Response::ok(resp)
    }

    async fn ping(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::SystemPingRequest>,
    ) -> ServiceResult<pb::SystemPingResponse> {
        let resp = pb::SystemPingResponse {
            api_version: "1.0".to_string(),
            build_version: env!("CARGO_PKG_VERSION").to_string(),
            ..Default::default()
        };
        Response::ok(resp)
    }

    async fn events(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::EventsRequest>,
    ) -> ServiceResult<ServiceStream<pb::Event>> {
        // TODO: Implement event streaming.
        Err(ConnectError::unimplemented("events not yet implemented"))
    }

    async fn prune(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::PruneRequest>,
    ) -> ServiceResult<pb::PruneResponse> {
        // TODO: Implement resource pruning.
        Err(ConnectError::unimplemented("prune not yet implemented"))
    }

    async fn get_setup_status(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::Empty>,
    ) -> ServiceResult<pb::SetupStatus> {
        Response::ok(self.setup_state.current())
    }

    async fn watch_setup_status(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::Empty>,
    ) -> ServiceResult<ServiceStream<pb::SetupStatus>> {
        let (initial, mut updates) = self.setup_state.subscribe();
        let stream = async_stream::stream! {
            // Send current state immediately, then every update after it.
            yield Ok(initial);
            loop {
                match updates.recv().await {
                    Ok(status) => yield Ok(status),
                    // Too slow to keep up: the next recv resumes from the
                    // oldest retained update, so the client stays live and
                    // converges — it has only missed intermediate phases.
                    Err(broadcast::error::RecvError::Lagged(skipped)) => {
                        tracing::warn!(skipped, "setup status client lagged");
                    }
                    Err(broadcast::error::RecvError::Closed) => break,
                }
            }
        };
        Response::ok(Box::pin(stream))
    }

    async fn get_system_vm_backend(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::Empty>,
    ) -> ServiceResult<pb::SystemVmBackendInfo> {
        let runtime = self.runtime.ready()?;
        let info = pb::SystemVmBackendInfo {
            backend: backend_to_proto(runtime.system_vm_backend()).into(),
            ..Default::default()
        };
        Response::ok(info)
    }

    async fn get_virtio_debug(
        &self,
        _ctx: RequestContext,
        _request: ServiceRequest<'_, pb::Empty>,
    ) -> ServiceResult<pb::VirtioDebugInfo> {
        // The early handle exists as soon as the runtime is constructed —
        // a boot that never reaches READY is this RPC's main use case.
        let runtime = self.early_runtime.ready()?;
        let snapshot = runtime
            .system_vm_debug_snapshot()
            .map_err(|e| ConnectError::failed_precondition(e.to_string()))?;
        let info = pb::VirtioDebugInfo {
            devices: snapshot
                .devices
                .into_iter()
                .map(device_debug_to_proto)
                .collect(),
            vcpus: snapshot
                .vcpus
                .into_iter()
                .map(|v| pb::VcpuDebug {
                    vcpu: v.vcpu,
                    mmio_reads: v.mmio_reads,
                    mmio_writes: v.mmio_writes,
                    wfi: v.wfi,
                    hvc: v.hvc,
                    smc: v.smc,
                    vtimer: v.vtimer,
                    kicks_received: v.kicks_received,
                    sysreg: v.sysreg,
                    other: v.other,
                    ..Default::default()
                })
                .collect(),
            kick_broadcasts: snapshot.kick_broadcasts,
            unpark_broadcasts: snapshot.unpark_broadcasts,
            ..Default::default()
        };
        Response::ok(info)
    }

    async fn resolve_container_fs(
        &self,
        _ctx: RequestContext,
        request: ServiceRequest<'_, pb::ResolveContainerFsRequest>,
    ) -> ServiceResult<pb::ResolveContainerFsResponse> {
        let runtime = self.runtime.ready()?;
        let req = request.to_owned_message();
        if req.container_id.is_empty() {
            return Err(ConnectError::invalid_argument(
                "container_id must not be empty",
            ));
        }
        let paths = runtime
            .container_fs_paths(&req.container_id)
            .await
            .map_err(|e| ConnectError::failed_precondition(e.to_string()))?;
        let resp = pb::ResolveContainerFsResponse {
            upper_dir: paths.upper_dir,
            lower_dirs: paths.lower_dirs,
            ..Default::default()
        };
        Response::ok(resp)
    }

    async fn resolve_image_fs(
        &self,
        _ctx: RequestContext,
        request: ServiceRequest<'_, pb::ResolveImageFsRequest>,
    ) -> ServiceResult<pb::ResolveImageFsResponse> {
        let runtime = self.runtime.ready()?;
        let req = request.to_owned_message();
        if req.top_chain_id.is_empty() {
            return Err(ConnectError::invalid_argument(
                "top_chain_id must not be empty",
            ));
        }
        let paths = runtime
            .image_fs_paths(&req.top_chain_id)
            .await
            .map_err(|e| ConnectError::failed_precondition(e.to_string()))?;
        let resp = pb::ResolveImageFsResponse {
            lower_dirs: paths.lower_dirs,
            ..Default::default()
        };
        Response::ok(resp)
    }

    async fn set_system_vm_backend(
        &self,
        _ctx: RequestContext,
        request: ServiceRequest<'_, pb::SetSystemVmBackendRequest>,
    ) -> ServiceResult<pb::SystemVmBackendInfo> {
        let req = request.to_owned_message();
        let backend = backend_from_proto(req.backend.as_known().unwrap_or_default())
            .ok_or_else(|| ConnectError::invalid_argument("backend must be HV or VZ"))?;
        let runtime = self.runtime.ready()?;
        runtime
            .switch_system_vm_backend(backend)
            .await
            .map_err(|e| ConnectError::internal(e.to_string()))?;
        let info = pb::SystemVmBackendInfo {
            backend: backend_to_proto(runtime.system_vm_backend()).into(),
            ..Default::default()
        };
        Response::ok(info)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Every update currently buffered for `updates`, as phases.
    fn drain(updates: &mut broadcast::Receiver<SetupStatus>) -> Vec<setup_status::Phase> {
        std::iter::from_fn(|| updates.try_recv().ok())
            .map(|status| status.phase.as_known().unwrap_or_default())
            .collect()
    }

    /// The regression this exists for: `NETWORK_READY` and `READY` are
    /// published ~300 µs apart with no await point between them, so a
    /// snapshot-only channel hands a client just the last one — a phase that
    /// was published but never observed, which is exactly what CORE-67 set
    /// out to make impossible.
    #[test]
    fn back_to_back_phases_are_all_delivered() {
        let state = SetupState::new();
        let (_snapshot, mut updates) = state.subscribe();

        state.set_phase(setup_status::Phase::NetworkReady, "network");
        state.set_phase(setup_status::Phase::Ready, "ready");

        assert_eq!(
            drain(&mut updates),
            [
                setup_status::Phase::NetworkReady,
                setup_status::Phase::Ready
            ]
        );
    }

    /// A subscriber's snapshot already folds in every earlier update, so
    /// replaying one would walk the client's phase backwards.
    #[test]
    fn the_snapshot_is_not_replayed_as_an_update() {
        let state = SetupState::new();
        state.set_phase(setup_status::Phase::AssetsReady, "assets");

        let (snapshot, mut updates) = state.subscribe();

        assert_eq!(snapshot.phase, setup_status::Phase::AssetsReady);
        assert!(drain(&mut updates).is_empty());
    }

    /// Flags travel the same path as phases: a client watching for the route
    /// must not have to wait for an unrelated phase change to learn of it.
    #[test]
    fn flag_updates_reach_subscribers() {
        let state = SetupState::new();
        let (_snapshot, mut updates) = state.subscribe();

        state.set_route_installed(true);

        let update = updates.try_recv().expect("flag update delivered");
        assert!(update.route_installed);
    }
}