[control] Store and recovery fixes: cross-process token lock, revert keeps its journal until done, epoch and deny list never roll back, journal errors propagate, lost confirm reloads edge, trial hash checked

This commit is contained in:
2026-11-07 18:51:58 +01:00
parent b9e14c6aee
commit 4448527b16
10 changed files with 305 additions and 66 deletions
+103 -17
View File
@@ -274,11 +274,14 @@ impl Control {
self.paused.lock().ok().and_then(|p| p.clone())
}
fn pause(&self, r: Reason) {
let _ = write_json(&self.cfg.state_dir.join("paused.json"), &r);
/// Pause changes. Always in memory; `Err` when it could not be saved, and then the
/// caller must keep the journal so a restart still knows.
fn pause(&self, r: Reason) -> Result<(), String> {
let saved = write_json(&self.cfg.state_dir.join("paused.json"), &r);
if let Ok(mut p) = self.paused.lock() {
*p = Some(r);
}
saved
}
fn unpause(&self) {
@@ -786,7 +789,10 @@ impl Control {
self.update(&id, |r| r.commit = Some(commit.clone()));
j.stage = "committed".into();
if let Err(e) = self.journal(&j) {
log::error!(target: "ianitor::apply", "{e}");
// The journal still says `prepared` with this commit, which recovery also reverts.
return self
.revert(&id, &j, fail_internal(e).unwrap_or_default(), "failed")
.await;
}
crash::point("committed");
@@ -837,7 +843,11 @@ impl Control {
.await;
}
j.stage = "trial_written".into();
let _ = self.journal(&j);
if let Err(e) = self.journal(&j) {
return self
.revert(&id, &j, fail_internal(e).unwrap_or_default(), "failed")
.await;
}
crash::point("trial_written");
self.update(&id, |r| {
r.step("write", "done");
@@ -897,7 +907,32 @@ impl Control {
};
j.stage = "trial_loaded".into();
j.edge_sha256 = Some(sha.clone());
let _ = self.journal(&j);
let want = crate::util::sha256_hex(edge_text.as_bytes());
if sha != want {
let r = reason(
"apply.failed.reload",
messages::params([("engine", "Pingora".into())]),
);
let detail = format!(
"edge loaded a trial with SHA-256 {sha}, not the one control wrote ({want})"
);
return self
.revert(
&id,
&j,
Reason {
detail: Some(detail),
..r
},
"failed",
)
.await;
}
if let Err(e) = self.journal(&j) {
return self
.revert(&id, &j, fail_internal(e).unwrap_or_default(), "failed")
.await;
}
crash::point("trial_loaded");
self.update(&id, |r| r.step("reload", "done"));
@@ -968,15 +1003,36 @@ impl Control {
.await;
}
j.stage = "confirmed".into();
let _ = self.journal(&j);
if let Err(e) = self.journal(&j) {
// Without a `confirmed` journal a crash would revert a kept change; so revert now
// (the revert also brings edge back to edge.json).
return self
.revert(&id, &j, fail_internal(e).unwrap_or_default(), "failed")
.await;
}
crash::point("confirmed");
if let Err(e) = self.promote() {
// Edge serves the new config and the repository has it; the files catch up at
// the next start (recovery promotes again).
log::error!(target: "ianitor::apply", "cannot promote the trial files: {e}");
let promoted = self.promote().or_else(|e| {
log::error!(target: "ianitor::apply", "cannot promote the trial files ({e}); writing them again");
write_atomic(&self.cfg.edge.config, edge_text.as_bytes(), 0o644)
.and_then(|_| write_atomic(&self.cfg.caddy.config, caddy_text.as_bytes(), 0o644))
});
if let Err(e) = promoted {
// Edge serves the change and the repository has it, but the files do not: keep
// the journal at `confirmed` (recovery promotes) and pause so nothing overwrites it.
log::error!(target: "ianitor::apply", "cannot write the config files: {e}");
let mut p = reason("control.files_unwritten", Params::new());
p.detail = Some(e.to_string());
let _ = self.pause(p);
let push_step = self.push_step();
self.update(&id, |r| r.step("push", push_step));
self.finish(&id, "kept", Some(kept_reason), Vec::new());
return;
}
j.stage = "promoted".into();
let _ = self.journal(&j);
if let Err(e) = self.journal(&j) {
// Files are promoted; a stale `confirmed` journal only makes recovery promote again.
log::error!(target: "ianitor::apply", "{e}");
}
crash::point("promoted");
// Record first, journal last: a crash in between is finished again by recovery.
let push_step = self.push_step();
@@ -1210,7 +1266,10 @@ impl Control {
let mut j = j.clone();
j.stage = "reverting".into();
j.reason = Some(why.clone());
let _ = self.journal(&j);
if let Err(e) = self.journal(&j) {
// The earlier stage stays on disk; recovery reverts from it too.
log::error!(target: "ianitor::apply", "{e}");
}
crash::point("reverting");
match self.finish_revert(&j).await {
Ok(rev) => {
@@ -1222,9 +1281,12 @@ impl Control {
log::error!(target: "ianitor::apply", "apply {id}: the revert failed: {e}");
let mut r = reason("apply.needs_attention", Params::new());
r.detail = Some(format!("{}: {e}", why.message));
self.pause(r.clone());
// The journal stays at `reverting`: a restart retries the revert, and even
// if the pause below cannot be saved, it never loads the unverified head.
if let Err(pe) = self.pause(r.clone()) {
log::error!(target: "ianitor::apply", "cannot save the pause ({pe}); the journal keeps it");
}
self.finish(id, "needs_attention", Some(r), Vec::new());
self.clear_journal();
}
}
}
@@ -1239,6 +1301,20 @@ impl Control {
log::warn!(target: "ianitor::apply", "could not tell edge to abort the trial: {e}")
}
}
// If edge confirmed the trial after all (an answer lost on the way), it still runs
// the reverted config: reload it from edge.json, the last good config.
if let (Ok(st), Ok(disk)) = (
self.edge.status().await,
std::fs::read(&self.cfg.edge.config),
) && st.trial.is_none()
&& st.config_sha256 != crate::util::sha256_hex(&disk)
{
log::warn!(target: "ianitor::apply", "edge runs a config that is not edge.json; reloading it");
self.edge
.reload()
.await
.map_err(|e| format!("edge did not reload edge.json: {e}"))?;
}
}
let head = self.repo.head_required().map_err(|e| e.to_string())?;
let mut rev = None;
@@ -1322,11 +1398,21 @@ impl Control {
};
self.finish(&j.apply_id, state, Some(why), Vec::new());
self.clear_journal();
if self
.paused()
.is_some_and(|p| p.code == "apply.needs_attention")
{
log::info!(target: "ianitor::apply", "the interrupted revert is done; changes resume");
self.unpause();
}
}
Err(e) => {
// Journal kept: the next start tries again.
let mut r = reason("apply.needs_attention", Params::new());
r.detail = Some(e);
self.pause(r.clone());
if let Err(pe) = self.pause(r.clone()) {
log::error!(target: "ianitor::apply", "cannot save the pause: {pe}");
}
self.finish(&j.apply_id, "needs_attention", Some(r), Vec::new());
}
}
@@ -1378,7 +1464,7 @@ impl Control {
Ok(x) => x,
Err(f) => {
log::error!(target: "ianitor::control", "{}", f.reason.message);
self.pause(f.reason);
let _ = self.pause(f.reason);
return;
}
};
@@ -1394,7 +1480,7 @@ impl Control {
let mut r = reason("repo.unreadable", messages::params([("file", file)]));
r.detail = fe.first().and_then(|e| e.detail.clone());
log::error!(target: "ianitor::control", "the routes repository does not validate: {}", r.detail.clone().unwrap_or_default());
self.pause(r);
let _ = self.pause(r);
return;
}
};