firkin-single-node 0.0.1-alpha

Production Apple/VZ runtime composition for the firkin Rust containerization library
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
//! State records for single-node sessions and snapshots.

use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use super::{Error, LogEvent, Result, SandboxResources, SnapshotRecord};

/// Persisted active session record for restart reconciliation.
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct ActiveSessionRecord {
    /// Sandbox ID.
    pub sandbox_id: String,
    /// Template ID used to create the session.
    pub template_id: String,
    /// Runtime client ID.
    pub client_id: String,
    /// Optional envd access token.
    pub envd_access_token: Option<String>,
    /// Unix timestamp when the session started.
    pub started_at_unix_seconds: i64,
    /// Unix timestamp when the session should expire.
    pub end_at_unix_seconds: i64,
    /// Resources reserved by the session.
    pub resources: SandboxResources,
    /// Whether a runtime VM/container was attached.
    pub runtime_attached: bool,
}

/// In-memory single-node runtime state used by the hot sandbox lifecycle path.
#[derive(Clone, Debug, Default)]
pub struct StateStore {
    active: Arc<Mutex<Vec<ActiveSessionRecord>>>,
    snapshots: Arc<Mutex<Vec<SnapshotRecord>>>,
    logs: Arc<Mutex<HashMap<String, Vec<LogEvent>>>>,
}

impl StateStore {
    /// Construct an empty in-memory state store.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Construct state from recovered records.
    #[must_use]
    pub fn from_records(
        active: Vec<ActiveSessionRecord>,
        snapshots: Vec<SnapshotRecord>,
        logs: HashMap<String, Vec<LogEvent>>,
    ) -> Self {
        Self {
            active: Arc::new(Mutex::new(active)),
            snapshots: Arc::new(Mutex::new(snapshots)),
            logs: Arc::new(Mutex::new(logs)),
        }
    }

    /// Load active session records from memory.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the state lock is poisoned.
    pub fn load_active(&self) -> Result<Vec<ActiveSessionRecord>> {
        Ok(self.lock_active()?.clone())
    }

    /// Replace active session records in memory.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the state lock is poisoned.
    pub fn save_active(&self, sessions: Vec<ActiveSessionRecord>) -> Result<()> {
        *self.lock_active()? = sessions;
        Ok(())
    }

    /// Load log events keyed by sandbox ID from memory.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the state lock is poisoned.
    pub fn load_logs(&self) -> Result<HashMap<String, Vec<LogEvent>>> {
        Ok(self.lock_logs()?.clone())
    }

    /// Replace log events keyed by sandbox ID in memory.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the state lock is poisoned.
    pub fn save_logs(&self, logs: HashMap<String, Vec<LogEvent>>) -> Result<()> {
        *self.lock_logs()? = logs;
        Ok(())
    }

    /// Load snapshot records from memory.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the state lock is poisoned.
    pub fn load_snapshots(&self) -> Result<Vec<SnapshotRecord>> {
        Ok(self.lock_snapshots()?.clone())
    }

    /// Replace snapshot records in memory.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the state lock is poisoned.
    pub fn save_snapshots(&self, snapshots: Vec<SnapshotRecord>) -> Result<()> {
        *self.lock_snapshots()? = snapshots;
        Ok(())
    }

    /// Drop expired active session records using the provided current unix time.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the state lock is poisoned.
    pub fn reconcile_active_entries(
        &self,
        now_unix_seconds: i64,
    ) -> Result<Vec<ActiveSessionRecord>> {
        let reconciled = self
            .load_active()?
            .into_iter()
            .filter(|session| session.end_at_unix_seconds > now_unix_seconds)
            .collect::<Vec<_>>();
        self.save_active(reconciled.clone())?;
        Ok(reconciled)
    }

    fn lock_active(&self) -> Result<std::sync::MutexGuard<'_, Vec<ActiveSessionRecord>>> {
        self.active.lock().map_err(|_| {
            Error::StatePersistenceFailed("single-node active state lock poisoned".to_owned())
        })
    }

    fn lock_snapshots(&self) -> Result<std::sync::MutexGuard<'_, Vec<SnapshotRecord>>> {
        self.snapshots.lock().map_err(|_| {
            Error::StatePersistenceFailed("single-node snapshot state lock poisoned".to_owned())
        })
    }

    fn lock_logs(&self) -> Result<std::sync::MutexGuard<'_, HashMap<String, Vec<LogEvent>>>> {
        self.logs.lock().map_err(|_| {
            Error::StatePersistenceFailed("single-node log state lock poisoned".to_owned())
        })
    }
}

/// Explicit file-backed persistence helper for restart and recovery boundaries.
#[derive(Clone, Debug)]
pub struct FileStateStore {
    dir: Arc<PathBuf>,
}

impl FileStateStore {
    /// Construct file persistence rooted at `dir`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when the directory cannot be created.
    pub fn new(dir: impl Into<PathBuf>) -> Result<Self> {
        let dir = dir.into();
        fs::create_dir_all(&dir).map_err(|error| {
            Error::StatePersistenceFailed(format!(
                "create single-node state dir {}: {error}",
                dir.display()
            ))
        })?;
        Ok(Self { dir: Arc::new(dir) })
    }

    /// Return the state root directory.
    #[must_use]
    pub fn root_dir(&self) -> &Path {
        self.dir.as_ref()
    }

    /// Return the managed snapshot artifact directory.
    #[must_use]
    pub fn snapshot_artifacts_dir(&self) -> PathBuf {
        self.dir.join("snapshot-artifacts")
    }

    /// Load all persisted records into an in-memory hot-path state store.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be read or decoded.
    pub fn load_state(&self) -> Result<StateStore> {
        Ok(StateStore::from_records(
            self.load_active()?,
            self.load_snapshots()?,
            self.load_logs()?,
        ))
    }

    /// Save all records from an in-memory state store.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be encoded or written.
    pub fn save_state(&self, state: &StateStore) -> Result<()> {
        let active = state.load_active()?;
        let snapshots = state.load_snapshots()?;
        let logs = state.load_logs()?;
        self.save_active(&active)?;
        self.save_snapshots(&snapshots)?;
        self.save_logs(&logs)
    }

    /// Load active session records.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be read or decoded.
    pub fn load_active(&self) -> Result<Vec<ActiveSessionRecord>> {
        Self::read_json(&self.active_path())
    }

    /// Save active session records.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be encoded or written.
    pub fn save_active(&self, sessions: &[ActiveSessionRecord]) -> Result<()> {
        Self::write_json(&self.active_path(), sessions)
    }

    /// Drop expired active session records from persisted state using the provided current unix time.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be read or written.
    pub fn reconcile_active_entries(
        &self,
        now_unix_seconds: i64,
    ) -> Result<Vec<ActiveSessionRecord>> {
        let state = StateStore::from_records(self.load_active()?, Vec::new(), HashMap::new());
        let reconciled = state.reconcile_active_entries(now_unix_seconds)?;
        self.save_active(&reconciled)?;
        Ok(reconciled)
    }

    /// Load log events keyed by sandbox ID.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be read or decoded.
    pub fn load_logs(&self) -> Result<HashMap<String, Vec<LogEvent>>> {
        Self::read_json(&self.logs_path())
    }

    /// Save log events keyed by sandbox ID.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be encoded or written.
    pub fn save_logs(&self, logs: &HashMap<String, Vec<LogEvent>>) -> Result<()> {
        Self::write_json(&self.logs_path(), logs)
    }

    /// Load snapshot records.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be read or decoded.
    pub fn load_snapshots(&self) -> Result<Vec<SnapshotRecord>> {
        Self::read_json(&self.snapshots_path())
    }

    /// Save snapshot records.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state cannot be encoded or written.
    pub fn save_snapshots(&self, snapshots: &[SnapshotRecord]) -> Result<()> {
        Self::write_json(&self.snapshots_path(), snapshots)
    }

    /// Reconcile managed snapshot artifact metadata against files on disk.
    ///
    /// Missing managed artifacts are dropped from metadata. Orphan files in the
    /// managed artifact directory are deleted.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when state or artifact cleanup fails.
    pub fn reconcile_snapshot_artifacts(
        &self,
        snapshots: Vec<SnapshotRecord>,
    ) -> Result<Vec<SnapshotRecord>> {
        let original_len = snapshots.len();
        let mut referenced_artifacts = HashSet::new();
        let mut reconciled = Vec::with_capacity(snapshots.len());
        for snapshot in snapshots {
            let Some(location) = snapshot.location.as_ref() else {
                reconciled.push(snapshot);
                continue;
            };
            let location = PathBuf::from(location);
            let managed_artifact = self.is_managed_snapshot_artifact(&location);
            if location.exists() {
                if managed_artifact {
                    referenced_artifacts.insert(location);
                }
                reconciled.push(snapshot);
            } else if !managed_artifact {
                reconciled.push(snapshot);
            }
        }

        self.delete_orphan_snapshot_artifacts(&referenced_artifacts)?;
        if reconciled.len() != original_len {
            self.save_snapshots(&reconciled)?;
        }
        Ok(reconciled)
    }

    /// Return the active-session state file path.
    #[must_use]
    pub fn active_path(&self) -> PathBuf {
        self.dir.join("active.json")
    }

    /// Return the log state file path.
    #[must_use]
    pub fn logs_path(&self) -> PathBuf {
        self.dir.join("logs.json")
    }

    /// Return the snapshot state file path.
    #[must_use]
    pub fn snapshots_path(&self) -> PathBuf {
        self.dir.join("snapshots.json")
    }

    fn write_json<T: serde::Serialize + ?Sized>(path: &Path, value: &T) -> Result<()> {
        let tmp_path = path.with_extension("json.tmp");
        let data = serde_json::to_vec_pretty(value).map_err(|error| {
            Error::StatePersistenceFailed(format!("serialize single-node state: {error}"))
        })?;
        fs::write(&tmp_path, data).map_err(|error| {
            Error::StatePersistenceFailed(format!(
                "write single-node state {}: {error}",
                tmp_path.display()
            ))
        })?;
        fs::rename(&tmp_path, path).map_err(|error| {
            Error::StatePersistenceFailed(format!(
                "replace single-node state {}: {error}",
                path.display()
            ))
        })?;
        Ok(())
    }

    fn read_json<T: serde::de::DeserializeOwned + Default>(path: &Path) -> Result<T> {
        match fs::read(path) {
            Ok(data) => serde_json::from_slice(&data).map_err(|error| {
                Error::StatePersistenceFailed(format!(
                    "parse single-node state {}: {error}",
                    path.display()
                ))
            }),
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(T::default()),
            Err(error) => Err(Error::StatePersistenceFailed(format!(
                "read single-node state {}: {error}",
                path.display()
            ))),
        }
    }

    fn is_managed_snapshot_artifact(&self, path: &Path) -> bool {
        path.parent()
            .is_some_and(|parent| parent == self.snapshot_artifacts_dir())
    }

    fn delete_orphan_snapshot_artifacts(&self, referenced: &HashSet<PathBuf>) -> Result<()> {
        let artifact_dir = self.snapshot_artifacts_dir();
        let entries = match fs::read_dir(&artifact_dir) {
            Ok(entries) => entries,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
            Err(error) => {
                return Err(Error::StatePersistenceFailed(format!(
                    "read single-node snapshot artifact dir {}: {error}",
                    artifact_dir.display()
                )));
            }
        };
        for entry in entries {
            let entry = entry.map_err(|error| {
                Error::StatePersistenceFailed(format!(
                    "read single-node snapshot artifact entry {}: {error}",
                    artifact_dir.display()
                ))
            })?;
            let path = entry.path();
            if path.is_file() && !referenced.contains(&path) {
                fs::remove_file(&path).map_err(|error| {
                    Error::StatePersistenceFailed(format!(
                        "delete orphan single-node snapshot artifact {}: {error}",
                        path.display()
                    ))
                })?;
            }
        }
        Ok(())
    }
}

/// Bounded log store over hot-path in-memory state.
#[derive(Clone, Debug)]
pub struct LogStore {
    state: StateStore,
    files: Option<FileStateStore>,
}

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

impl LogStore {
    const MAX_ENTRIES_PER_SANDBOX: usize = 1_000;

    /// Construct a log store over an in-memory state store.
    #[must_use]
    pub const fn new(state: StateStore) -> Self {
        Self { state, files: None }
    }

    /// Construct a log store backed by the same durable file state as sessions.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when persisted state cannot be loaded.
    pub fn with_file_state_store(files: FileStateStore) -> Result<Self> {
        Ok(Self {
            state: files.load_state()?,
            files: Some(files),
        })
    }

    fn persist_logs(&self) -> Result<()> {
        if let Some(files) = &self.files {
            let logs = self.state.load_logs()?;
            files.save_logs(&logs)?;
        }
        Ok(())
    }

    /// Record a log message for a sandbox.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when log state cannot be updated.
    pub fn record(&self, sandbox_id: &str, message: String) -> Result<()> {
        let mut logs = self.state.load_logs()?;
        let sandbox_entries = logs.entry(sandbox_id.to_owned()).or_default();
        sandbox_entries.push(LogEvent::new(message));
        let overflow = sandbox_entries
            .len()
            .saturating_sub(Self::MAX_ENTRIES_PER_SANDBOX);
        if overflow > 0 {
            sandbox_entries.drain(0..overflow);
        }
        self.state.save_logs(logs)?;
        self.persist_logs()
    }

    /// Remove all logs for a sandbox.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when log state cannot be updated.
    pub fn remove_sandbox(&self, sandbox_id: &str) -> Result<()> {
        let mut logs = self.state.load_logs()?;
        logs.remove(sandbox_id);
        self.state.save_logs(logs)?;
        self.persist_logs()
    }

    /// Return a page of logs for a sandbox.
    ///
    /// # Errors
    ///
    /// Returns [`Error::StatePersistenceFailed`] when log state cannot be read.
    pub fn entries(
        &self,
        sandbox_id: &str,
        cursor: Option<i64>,
        limit: i32,
    ) -> Result<Vec<LogEvent>> {
        let logs = self.state.load_logs()?;
        let start = usize::try_from(cursor.unwrap_or(0).max(0)).unwrap_or(usize::MAX);
        let limit = usize::try_from(limit.max(0)).unwrap_or(0);
        let limit = if limit == 0 { usize::MAX } else { limit };
        Ok(logs
            .get(sandbox_id)
            .map(|sandbox_entries| {
                sandbox_entries
                    .iter()
                    .skip(start)
                    .take(limit)
                    .cloned()
                    .collect()
            })
            .unwrap_or_default())
    }
}