[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 {
|
pub fn push_state(&self) -> PushState {
|
||||||
self.push.lock().map(|p| p.clone()).unwrap_or_default()
|
self.push.lock().map(|p| p.clone()).unwrap_or_default()
|
||||||
}
|
}
|
||||||
@@ -525,7 +540,7 @@ impl Control {
|
|||||||
base_rev: Option<String>,
|
base_rev: Option<String>,
|
||||||
actor: &str,
|
actor: &str,
|
||||||
) -> Result<ApplyRecord, Fail> {
|
) -> Result<ApplyRecord, Fail> {
|
||||||
if let Some(p) = self.paused() {
|
if let Some(p) = self.paused_for(&change) {
|
||||||
return Err(Fail::new(
|
return Err(Fail::new(
|
||||||
409,
|
409,
|
||||||
"control.paused",
|
"control.paused",
|
||||||
@@ -698,7 +713,7 @@ impl Control {
|
|||||||
r.started = Some(t);
|
r.started = Some(t);
|
||||||
r.step("validate", "running");
|
r.step("validate", "running");
|
||||||
});
|
});
|
||||||
if let Some(p) = self.paused() {
|
if let Some(p) = self.paused_for(&job.change) {
|
||||||
return self.finish(
|
return self.finish(
|
||||||
&id,
|
&id,
|
||||||
"failed",
|
"failed",
|
||||||
@@ -963,10 +978,15 @@ impl Control {
|
|||||||
j.stage = "promoted".into();
|
j.stage = "promoted".into();
|
||||||
let _ = self.journal(&j);
|
let _ = self.journal(&j);
|
||||||
crash::point("promoted");
|
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();
|
let push_step = self.push_step();
|
||||||
self.update(&id, |r| r.step("push", push_step));
|
self.update(&id, |r| r.step("push", push_step));
|
||||||
self.finish(&id, "kept", Some(kept_reason), Vec::new());
|
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();
|
self.push_wake.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1194,17 +1214,17 @@ impl Control {
|
|||||||
crash::point("reverting");
|
crash::point("reverting");
|
||||||
match self.finish_revert(&j).await {
|
match self.finish_revert(&j).await {
|
||||||
Ok(rev) => {
|
Ok(rev) => {
|
||||||
self.update(id, |r| r.revert_commit = rev);
|
self.update(id, |r| r.revert_commit = rev.or(r.revert_commit.clone()));
|
||||||
self.clear_journal();
|
|
||||||
self.finish(id, state, Some(why), Vec::new());
|
self.finish(id, state, Some(why), Vec::new());
|
||||||
|
self.clear_journal();
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!(target: "ianitor::apply", "apply {id}: the revert failed: {e}");
|
log::error!(target: "ianitor::apply", "apply {id}: the revert failed: {e}");
|
||||||
let mut r = reason("apply.needs_attention", Params::new());
|
let mut r = reason("apply.needs_attention", Params::new());
|
||||||
r.detail = Some(format!("{}: {e}", why.message));
|
r.detail = Some(format!("{}: {e}", why.message));
|
||||||
self.pause(r.clone());
|
self.pause(r.clone());
|
||||||
self.clear_journal();
|
|
||||||
self.finish(id, "needs_attention", Some(r), Vec::new());
|
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())?;
|
let head = self.repo.head_required().map_err(|e| e.to_string())?;
|
||||||
match j.stage.as_str() {
|
match j.stage.as_str() {
|
||||||
"prepared" if head != j.commit => {
|
"prepared" if head != j.commit => {
|
||||||
self.clear_journal();
|
|
||||||
self.finish(
|
self.finish(
|
||||||
&j.apply_id,
|
&j.apply_id,
|
||||||
"failed",
|
"failed",
|
||||||
Some(reason("apply.interrupted.queued", Params::new())),
|
Some(reason("apply.interrupted.queued", Params::new())),
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
);
|
);
|
||||||
|
self.clear_journal();
|
||||||
}
|
}
|
||||||
"confirmed" | "promoted" => {
|
"confirmed" | "promoted" => {
|
||||||
self.promote().map_err(|e| e.to_string())?;
|
self.promote().map_err(|e| e.to_string())?;
|
||||||
self.clear_journal();
|
|
||||||
let push_step = self.push_step();
|
let push_step = self.push_step();
|
||||||
self.update(&j.apply_id, |r| r.step("push", 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
|
let mut why = j
|
||||||
@@ -1289,9 +1312,8 @@ impl Control {
|
|||||||
Ok(rev) => {
|
Ok(rev) => {
|
||||||
self.update(&j.apply_id, |r| {
|
self.update(&j.apply_id, |r| {
|
||||||
r.commit = r.commit.clone().or(Some(j.commit.clone()));
|
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.
|
// A revert that was already under way keeps its own outcome.
|
||||||
let state = if why.code.starts_with("apply.failed") {
|
let state = if why.code.starts_with("apply.failed") {
|
||||||
"failed"
|
"failed"
|
||||||
@@ -1299,6 +1321,7 @@ impl Control {
|
|||||||
"reverted"
|
"reverted"
|
||||||
};
|
};
|
||||||
self.finish(&j.apply_id, state, Some(why), Vec::new());
|
self.finish(&j.apply_id, state, Some(why), Vec::new());
|
||||||
|
self.clear_journal();
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let mut r = reason("apply.needs_attention", Params::new());
|
let mut r = reason("apply.needs_attention", Params::new());
|
||||||
@@ -1332,7 +1355,17 @@ impl Control {
|
|||||||
{
|
{
|
||||||
let _ = self.edge.abort().await;
|
let _ = self.edge.abort().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;
|
self.sync_files().await;
|
||||||
|
}
|
||||||
if let Err(e) = self.shares.ensure_revocation_file() {
|
if let Err(e) = self.shares.ensure_revocation_file() {
|
||||||
log::error!(target: "ianitor::share", "{e}");
|
log::error!(target: "ianitor::share", "{e}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -436,6 +436,128 @@ mod unix {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||||
|
async fn a_queued_apply_can_be_cancelled() {
|
||||||
|
let good = Echo::start("good").await;
|
||||||
|
let mut env = Env::new(EnvSpec {
|
||||||
|
watch_secs: 3,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
env.start_control(None);
|
||||||
|
let a = env
|
||||||
|
.call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/hosts",
|
||||||
|
Some(json!({ "host": host("a.test", &good.url(), true) })),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let b = env
|
||||||
|
.call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/hosts",
|
||||||
|
Some(json!({ "host": host("b.test", &good.url(), true) })),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let bid = b.json["apply"]["id"].as_str().unwrap().to_owned();
|
||||||
|
let r = env
|
||||||
|
.call("DELETE", &format!("/api/v1/applies/{bid}"), None)
|
||||||
|
.await;
|
||||||
|
assert_eq!(r.status, 204, "{}", r.text);
|
||||||
|
let rb = env.wait_apply(&bid).await;
|
||||||
|
assert_eq!(rb["state"], "failed");
|
||||||
|
assert_eq!(rb["reason"]["code"], "apply.cancelled");
|
||||||
|
let ra = env
|
||||||
|
.wait_apply(a.json["apply"]["id"].as_str().unwrap())
|
||||||
|
.await;
|
||||||
|
assert_eq!(ra["state"], "kept");
|
||||||
|
// A running or finished apply cannot be cancelled.
|
||||||
|
let r = env
|
||||||
|
.call(
|
||||||
|
"DELETE",
|
||||||
|
&format!("/api/v1/applies/{}", ra["id"].as_str().unwrap()),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(r.status, 409);
|
||||||
|
assert!(
|
||||||
|
!env.head_files()
|
||||||
|
.iter()
|
||||||
|
.any(|(p, _)| p == "hosts/b.test.toml")
|
||||||
|
);
|
||||||
|
env.assert_consistent().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||||
|
async fn a_revert_that_fails_pauses_changes_until_a_restore() {
|
||||||
|
let good = Echo::start("good").await;
|
||||||
|
let sick = Echo::start("sick").await;
|
||||||
|
sick.health.store(500, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
let mut env = Env::new(EnvSpec {
|
||||||
|
watch_secs: 3,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
env.start_control(None);
|
||||||
|
let kept = env.add_host(host("a.test", &good.url(), true)).await;
|
||||||
|
let good_edge = env.file("edge/edge.json");
|
||||||
|
let r = env
|
||||||
|
.call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/hosts",
|
||||||
|
Some(json!({ "host": host("b.test", &sick.url(), true) })),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
let id = r.json["apply"]["id"].as_str().unwrap().to_owned();
|
||||||
|
// While it is being watched, lock the branch so the revert commit cannot land.
|
||||||
|
let deadline = std::time::Instant::now() + Duration::from_secs(10);
|
||||||
|
while env
|
||||||
|
.call("GET", &format!("/api/v1/applies/{id}"), None)
|
||||||
|
.await
|
||||||
|
.json["commit"]
|
||||||
|
.is_null()
|
||||||
|
{
|
||||||
|
assert!(std::time::Instant::now() < deadline);
|
||||||
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||||
|
}
|
||||||
|
let lock = env.repo().join("refs/heads/main.lock");
|
||||||
|
std::fs::write(&lock, "").unwrap();
|
||||||
|
let rec = env.wait_apply(&id).await;
|
||||||
|
std::fs::remove_file(&lock).unwrap();
|
||||||
|
assert_eq!(rec["state"], "needs_attention", "{rec}");
|
||||||
|
// Edge was still told to drop the trial: visitors get the last good config.
|
||||||
|
let st = env.edge_status().await;
|
||||||
|
assert_eq!(st["config_sha256"], sha256(&good_edge));
|
||||||
|
assert_eq!(env.file("edge/edge.json"), good_edge);
|
||||||
|
let o = env.call("GET", "/api/v1/overview", None).await;
|
||||||
|
assert_eq!(o.json["paused"]["code"], "control.paused", "{}", o.text);
|
||||||
|
assert_eq!(o.json["undo"]["blocked"]["code"], "undo.paused");
|
||||||
|
let r = env
|
||||||
|
.call(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/hosts",
|
||||||
|
Some(json!({ "host": host("c.test", &good.url(), false) })),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(r.status, 409);
|
||||||
|
// A control restart does not push the unverified head to edge.
|
||||||
|
env.kill_control();
|
||||||
|
env.start_control(None);
|
||||||
|
assert_eq!(env.edge_status().await["config_sha256"], sha256(&good_edge));
|
||||||
|
// Restore the last kept version: the way out.
|
||||||
|
let r = env
|
||||||
|
.apply(
|
||||||
|
"POST",
|
||||||
|
"/api/v1/restore",
|
||||||
|
Some(json!({ "change_id": kept["id"] })),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
assert_eq!(r["state"], "kept", "{r}");
|
||||||
|
let o = env.call("GET", "/api/v1/overview", None).await;
|
||||||
|
assert!(o.json["paused"].is_null(), "{}", o.text);
|
||||||
|
env.assert_consistent().await;
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------ undo, restore, conflicts
|
// ------------------------------------------------------------ undo, restore, conflicts
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||||
|
|||||||
Reference in New Issue
Block a user