[control] Recovery: record the outcome before clearing the journal; a failed revert pauses changes until a Restore, and a restart never loads the unverified head
This commit is contained in:
@@ -281,6 +281,21 @@ impl Control {
|
||||
}
|
||||
}
|
||||
|
||||
fn unpause(&self) {
|
||||
let _ = remove_if_exists(&self.cfg.state_dir.join("paused.json"));
|
||||
if let Ok(mut p) = self.paused.lock() {
|
||||
*p = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// While paused, only a Restore of a kept version may run: it is the way out of
|
||||
/// `needs_attention` (docs/ui-api.md §3.2).
|
||||
fn paused_for(&self, change: &Change) -> Option<Reason> {
|
||||
let p = self.paused()?;
|
||||
let way_out = matches!(change, Change::Restore { .. }) && p.code == "apply.needs_attention";
|
||||
(!way_out).then_some(p)
|
||||
}
|
||||
|
||||
pub fn push_state(&self) -> PushState {
|
||||
self.push.lock().map(|p| p.clone()).unwrap_or_default()
|
||||
}
|
||||
@@ -525,7 +540,7 @@ impl Control {
|
||||
base_rev: Option<String>,
|
||||
actor: &str,
|
||||
) -> Result<ApplyRecord, Fail> {
|
||||
if let Some(p) = self.paused() {
|
||||
if let Some(p) = self.paused_for(&change) {
|
||||
return Err(Fail::new(
|
||||
409,
|
||||
"control.paused",
|
||||
@@ -698,7 +713,7 @@ impl Control {
|
||||
r.started = Some(t);
|
||||
r.step("validate", "running");
|
||||
});
|
||||
if let Some(p) = self.paused() {
|
||||
if let Some(p) = self.paused_for(&job.change) {
|
||||
return self.finish(
|
||||
&id,
|
||||
"failed",
|
||||
@@ -963,10 +978,15 @@ impl Control {
|
||||
j.stage = "promoted".into();
|
||||
let _ = self.journal(&j);
|
||||
crash::point("promoted");
|
||||
self.clear_journal();
|
||||
// Record first, journal last: a crash in between is finished again by recovery.
|
||||
let push_step = self.push_step();
|
||||
self.update(&id, |r| r.step("push", push_step));
|
||||
self.finish(&id, "kept", Some(kept_reason), Vec::new());
|
||||
self.clear_journal();
|
||||
if self.paused().is_some() && matches!(job.change, Change::Restore { .. }) {
|
||||
log::info!(target: "ianitor::apply", "a kept Restore ends the pause");
|
||||
self.unpause();
|
||||
}
|
||||
self.push_wake.notify_one();
|
||||
}
|
||||
|
||||
@@ -1194,17 +1214,17 @@ impl Control {
|
||||
crash::point("reverting");
|
||||
match self.finish_revert(&j).await {
|
||||
Ok(rev) => {
|
||||
self.update(id, |r| r.revert_commit = rev);
|
||||
self.clear_journal();
|
||||
self.update(id, |r| r.revert_commit = rev.or(r.revert_commit.clone()));
|
||||
self.finish(id, state, Some(why), Vec::new());
|
||||
self.clear_journal();
|
||||
}
|
||||
Err(e) => {
|
||||
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());
|
||||
self.clear_journal();
|
||||
self.finish(id, "needs_attention", Some(r), Vec::new());
|
||||
self.clear_journal();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1260,20 +1280,23 @@ impl Control {
|
||||
let head = self.repo.head_required().map_err(|e| e.to_string())?;
|
||||
match j.stage.as_str() {
|
||||
"prepared" if head != j.commit => {
|
||||
self.clear_journal();
|
||||
self.finish(
|
||||
&j.apply_id,
|
||||
"failed",
|
||||
Some(reason("apply.interrupted.queued", Params::new())),
|
||||
Vec::new(),
|
||||
);
|
||||
self.clear_journal();
|
||||
}
|
||||
"confirmed" | "promoted" => {
|
||||
self.promote().map_err(|e| e.to_string())?;
|
||||
self.clear_journal();
|
||||
let push_step = self.push_step();
|
||||
self.update(&j.apply_id, |r| r.step("push", push_step));
|
||||
self.finish(&j.apply_id, "kept", None, Vec::new());
|
||||
let kept = self
|
||||
.record(&j.apply_id)
|
||||
.and_then(|r| r.reason.filter(|x| x.code.starts_with("apply.kept")));
|
||||
self.finish(&j.apply_id, "kept", kept, Vec::new());
|
||||
self.clear_journal();
|
||||
}
|
||||
_ => {
|
||||
let mut why = j
|
||||
@@ -1289,9 +1312,8 @@ impl Control {
|
||||
Ok(rev) => {
|
||||
self.update(&j.apply_id, |r| {
|
||||
r.commit = r.commit.clone().or(Some(j.commit.clone()));
|
||||
r.revert_commit = rev;
|
||||
r.revert_commit = rev.or(r.revert_commit.clone());
|
||||
});
|
||||
self.clear_journal();
|
||||
// A revert that was already under way keeps its own outcome.
|
||||
let state = if why.code.starts_with("apply.failed") {
|
||||
"failed"
|
||||
@@ -1299,6 +1321,7 @@ impl Control {
|
||||
"reverted"
|
||||
};
|
||||
self.finish(&j.apply_id, state, Some(why), Vec::new());
|
||||
self.clear_journal();
|
||||
}
|
||||
Err(e) => {
|
||||
let mut r = reason("apply.needs_attention", Params::new());
|
||||
@@ -1332,7 +1355,17 @@ impl Control {
|
||||
{
|
||||
let _ = self.edge.abort().await;
|
||||
}
|
||||
self.sync_files().await;
|
||||
// An apply whose revert failed left an unverified commit at the branch head: edge keeps
|
||||
// the last good config until a Restore. Any other pause is re-checked now.
|
||||
if self
|
||||
.paused()
|
||||
.is_some_and(|p| p.code == "apply.needs_attention")
|
||||
{
|
||||
log::warn!(target: "ianitor::control", "changes are paused (needs attention); edge keeps the last good config until a Restore");
|
||||
} else {
|
||||
self.unpause();
|
||||
self.sync_files().await;
|
||||
}
|
||||
if let Err(e) = self.shares.ensure_revocation_file() {
|
||||
log::error!(target: "ianitor::share", "{e}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user