use crate::snapshot::{RestoreReport, SnapshotReport};
use serde::Serialize;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ContinuityPolicy {
pub min_interval: Duration,
}
impl Default for ContinuityPolicy {
fn default() -> Self {
Self {
min_interval: Duration::ZERO,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LostName {
pub name: String,
pub reason: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinuityReport {
pub reason: String,
pub snapshot_path: String,
pub restored: Vec<String>,
pub lost: Vec<LostName>,
pub interrupted_cell: bool,
pub unsaved_cells: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub restore_error: Option<String>,
}
#[derive(Debug, Clone)]
pub(crate) struct Crash {
pub reason: String,
pub interrupted_cell: bool,
pub unsaved_cells: u64,
pub restore_error: Option<String>,
}
fn lost_from(names: &[String], reason: impl Fn(&str) -> Option<String>) -> Vec<LostName> {
names
.iter()
.map(|name| LostName {
name: name.clone(),
reason: reason(name).unwrap_or_else(|| "not restorable".into()),
})
.collect()
}
pub(crate) fn recovery_report(
crash: Crash,
snapshot_path: String,
restore: Result<Option<RestoreReport>, String>,
checkpoint: Option<&SnapshotReport>,
) -> ContinuityReport {
let all_checkpoint_names = |why: &str| -> Vec<LostName> {
let Some(checkpoint) = checkpoint else {
return Vec::new();
};
let mut lost = lost_from(&checkpoint.saved, |_| Some(why.to_string()));
lost.extend(lost_from(&checkpoint.skipped, |name| {
checkpoint.reason(name).map(String::from)
}));
lost
};
let (restored, mut lost, restore_error) = match restore {
Ok(Some(report)) if !report.missing => {
let mut lost = lost_from(&report.skipped, |name| report.reason(name).map(String::from));
if let Some(checkpoint) = checkpoint {
for name in &checkpoint.saved {
if !report.restored.contains(name) && !report.skipped.contains(name) {
lost.push(LostName {
name: name.clone(),
reason: "absent from the restored snapshot".into(),
});
}
}
}
(report.restored, lost, crash.restore_error)
}
Ok(_) => (
Vec::new(),
all_checkpoint_names("snapshot file is missing"),
crash.restore_error,
),
Err(error) => (
Vec::new(),
all_checkpoint_names(&format!("snapshot not restored: {error}")),
Some(error),
),
};
lost.sort_by(|left, right| left.name.cmp(&right.name));
lost.dedup_by(|left, right| left.name == right.name);
ContinuityReport {
reason: crash.reason,
snapshot_path,
restored,
lost,
interrupted_cell: crash.interrupted_cell,
unsaved_cells: crash.unsaved_cells,
restore_error,
}
}
impl ContinuityReport {
pub fn render(&self) -> String {
let mut text = format!("[RLM kernel restarted after: {}]\n", self.reason);
match &self.restore_error {
Some(error) => text.push_str(&format!(
"[State not restored ({error}); the namespace started empty]\n"
)),
None if self.restored.is_empty() => text.push_str("[No state restored]\n"),
None => text.push_str(&format!("[Restored: {}]\n", self.restored.join(", "))),
}
if !self.lost.is_empty() {
let lost: Vec<String> = self
.lost
.iter()
.map(|lost| format!("{} ({})", lost.name, lost.reason))
.collect();
text.push_str(&format!("[Lost: {}]\n", lost.join("; ")));
}
if self.interrupted_cell {
text.push_str("[The cell running at the failure did not finish; its changes to Python state are lost]\n");
}
if self.unsaved_cells > 0 {
text.push_str(&format!(
"[{} completed cell(s) after the last checkpoint are lost; restored values predate them]\n",
self.unsaved_cells
));
}
text
}
}