[control] Fix round 4: a device change during an engine switch is never skipped silently (devices_problem until the serving engine is told), and every switch ends by telling the engine that serves, also after going back

This commit is contained in:
2026-11-08 12:48:57 +01:00
parent c45dafa2a3
commit 0213021bb1
4 changed files with 60 additions and 22 deletions
+37 -19
View File
@@ -193,29 +193,25 @@ impl Control {
Err(_) => Some(self.engine_lock.lock().await),
};
// Tell the serving engine every time, changed or not: a repeated Revoke nudges again.
// During a switch it is not told now, and never silently (QC round 4): the problem
// stays set, so the retry tells whichever engine serves once the switch is over (the
// switch itself also ends by telling it).
let mut pending = None;
if result.is_ok() {
let caddy = self.engine_running() == "caddy";
result = if engine.is_none() {
log::info!(target: "ianitor::pki", "an engine switch is under way; the engine it starts reads the new device list");
Ok(())
} else if caddy && crate::caddy_guard::armed(&self.cfg.state_dir).is_some() {
log::info!(target: "ianitor::pki", "a change is being tried on Caddy; Caddy reads the new device list when it ends");
Ok(())
} else if caddy {
self.caddy_reload_checked()
.await
.map_err(|e| format!("Caddy did not reload the device list: {e}"))
} else if self.edge.attached() {
self.edge
.reload_devices()
.await
.map_err(|e| format!("edge did not re-read the device list: {e}"))
if engine.is_none() {
pending = Some(
"an engine switch is under way; the engine that serves after it is told then"
.to_owned(),
);
} else {
Ok(())
};
result = self.nudge_devices().await;
}
}
if let Ok(mut pr) = self.devices_problem.lock() {
*pr = result.as_ref().err().cloned();
*pr = result.as_ref().err().cloned().or(pending.clone());
}
if let Some(p) = &pending {
log::info!(target: "ianitor::pki", "{p}");
}
if let Err(e) = &result {
log::error!(target: "ianitor::pki", "{e}; retrying");
@@ -223,6 +219,28 @@ impl Control {
result
}
/// Tell the engine that serves to read the device files again. The caller holds the engine
/// lock. Under Caddy with a trial out, the end of that apply does it (promote, revert, the
/// guard and caddy-revert all load caddy.json, which reads the files).
pub(crate) async fn nudge_devices(&self) -> Result<(), String> {
let caddy = self.engine_running() == "caddy";
if caddy && crate::caddy_guard::armed(&self.cfg.state_dir).is_some() {
log::info!(target: "ianitor::pki", "a change is being tried on Caddy; Caddy reads the new device list when it ends");
Ok(())
} else if caddy {
self.caddy_reload_checked()
.await
.map_err(|e| format!("Caddy did not reload the device list: {e}"))
} else if self.edge.attached() {
self.edge
.reload_devices()
.await
.map_err(|e| format!("edge did not re-read the device list: {e}"))
} else {
Ok(())
}
}
async fn reload_serving_engine(&self) {
let _engine = self.engine_lock.lock().await;
if self.engine_running() == "caddy" {
+11
View File
@@ -254,6 +254,17 @@ impl Control {
});
}
}
// Still under the engine lock: whichever engine serves now reads the device files again
// (QC round 4). A device change during the switch only wrote them; a Caddy that was
// already running, or started before they changed, would keep the old list.
if self.pki.is_some()
&& let Err(e) = self.nudge_devices().await
{
log::error!(target: "ianitor::engine", "after the switch: {e}; retrying");
if let Ok(mut p) = self.devices_problem.lock() {
*p = Some(e);
}
}
}
fn units_and_cfg(&self) -> Result<(Arc<dyn Units>, EngineConfig), String> {
+8 -2
View File
@@ -78,6 +78,12 @@ esac
let deadline = Instant::now() + Duration::from_secs(30);
loop {
let r = env.call("GET", "/api/v1/engine", None).await;
// On a loaded machine a switch takes longer: stay under the API's 120 calls a
// minute rather than fail on a 429.
if r.status == 429 {
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
assert_eq!(r.status, 200, "{}", r.text);
if r.json["state"] == "running" && r.json["running"] == running {
return r.json;
@@ -85,7 +91,7 @@ esac
if Instant::now() > deadline {
panic!("engine is {}\n{}", r.text, env.control_log());
}
tokio::time::sleep(Duration::from_millis(100)).await;
tokio::time::sleep(Duration::from_millis(300)).await;
}
}
@@ -654,7 +660,7 @@ esac
return;
}
assert!(Instant::now() < deadline, "never {state}: {}", r.text);
tokio::time::sleep(Duration::from_millis(100)).await;
tokio::time::sleep(Duration::from_millis(250)).await;
}
}
+4 -1
View File
@@ -309,7 +309,10 @@ allows exactly `ianitor-edge.service` and `caddy-fallback.service`, verbs start
waits until it serves; if it does not within 20 s, it starts the old one again and reports
`engine.switch_failed`. The switch waits for a running apply (same lock, first come first
served: a queued switch runs right after the apply in progress, before any apply queued after
it). Back on Pingora, edge is reloaded so it serves what was applied while Caddy served.
it). Back on Pingora, edge is reloaded so it serves what was applied while Caddy served. Every
switch, kept or gone back, ends by telling the engine that serves to read the device files again;
a device change during a switch only writes them and shows `devices_problem` ("an engine switch is
under way") until then, and the 2 s retry tells the serving engine too.
Refusals: `engine.unmanaged` (no `[engine]` in control.toml), `engine.same`, `engine.switching`,
`engine.not_ready` (Caddy's settings cannot be built from the current routes).