From 0213021bb1c0ec8b5b8931b282005d4037e5bc68 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sun, 8 Nov 2026 12:48:57 +0100 Subject: [PATCH] [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 --- crates/control/src/pki_ctl.rs | 56 ++++++++++++++++++--------- crates/control/src/switch.rs | 11 ++++++ crates/control/tests/engine_switch.rs | 10 ++++- docs/api.md | 5 ++- 4 files changed, 60 insertions(+), 22 deletions(-) diff --git a/crates/control/src/pki_ctl.rs b/crates/control/src/pki_ctl.rs index c870e49..7a104f7 100644 --- a/crates/control/src/pki_ctl.rs +++ b/crates/control/src/pki_ctl.rs @@ -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" { diff --git a/crates/control/src/switch.rs b/crates/control/src/switch.rs index fead279..90ad0a6 100644 --- a/crates/control/src/switch.rs +++ b/crates/control/src/switch.rs @@ -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, EngineConfig), String> { diff --git a/crates/control/tests/engine_switch.rs b/crates/control/tests/engine_switch.rs index 6653efb..42ddf4a 100644 --- a/crates/control/tests/engine_switch.rs +++ b/crates/control/tests/engine_switch.rs @@ -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; } } diff --git a/docs/api.md b/docs/api.md index f7548da..8674ae6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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).