[control] Fix round 3: revocations never wait for an apply (files and edge at once; under Caddy the reload is left to the end of a running trial), confirmed before the deadline is claimed, the guard marks a trial expired only after a verified load and retries, caddy-revert clears a stale deadline when Pingora serves
This commit is contained in:
@@ -13,9 +13,12 @@
|
||||
//! - `ianitor-control caddy-guard` (its own unit next to caddy-fallback, so a hung control is
|
||||
//! covered too): once a second, it reverts a trial whose deadline has passed.
|
||||
//!
|
||||
//! Control *arms* the deadline (`caddy-trial.json` in its state dir) before it loads a trial
|
||||
//! and *claims* it (an atomic rename) before it confirms; the guard *expires* it with the same
|
||||
//! kind of rename. Whoever renames first wins, so a trial is either confirmed by control or
|
||||
//! Control *arms* the deadline (`caddy-trial.json` in its state dir) before it loads a trial,
|
||||
//! and *claims* it (an atomic rename) right after its journal says `confirmed`; the guard
|
||||
//! takes it with the same kind of rename (to `.reverting`, then `.expired` once Caddy is
|
||||
//! verified on caddy.json; given back if the load fails, so it is tried again). A crash between
|
||||
//! `confirmed` and the claim leaves both: the guard and caddy-revert see `confirmed` and leave
|
||||
//! the trial to recovery, which promotes it. Whoever renames first wins, so a trial is either confirmed by control or
|
||||
//! reverted by the guard, never both. caddy.json itself is only ever written with a confirmed
|
||||
//! config, so loading it is always safe.
|
||||
|
||||
@@ -65,7 +68,7 @@ pub fn claim(state_dir: &Path) -> bool {
|
||||
|
||||
/// After a trial is reverted (or at start): nothing is out any more.
|
||||
pub fn disarm(state_dir: &Path) {
|
||||
for s in ["", ".claimed", ".expired"] {
|
||||
for s in ["", ".claimed", ".expired", ".reverting"] {
|
||||
let _ = std::fs::remove_file(path(state_dir, s));
|
||||
}
|
||||
}
|
||||
@@ -97,43 +100,99 @@ pub fn verdict(armed: Option<&TrialDeadline>, confirmed: bool, now: u64, at_stop
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the deadline away from control (rename) and load the last good caddy.json.
|
||||
/// Whether Caddy is the engine that serves (the engine file; Pingora without `[engine]`).
|
||||
fn caddy_serves(cfg: &Config) -> bool {
|
||||
cfg.engine
|
||||
.as_ref()
|
||||
.is_some_and(|e| crate::switch::read_engine_file(&e.file) == "caddy")
|
||||
}
|
||||
|
||||
/// A revert that stopped half way (the guard died while loading) counts as armed again.
|
||||
fn resume_half_done(dir: &Path) {
|
||||
let reverting = path(dir, ".reverting");
|
||||
if reverting.exists() && !path(dir, "").exists() {
|
||||
let _ = std::fs::rename(reverting, path(dir, ""));
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the deadline away from control (rename to `.reverting`), load the last good
|
||||
/// caddy.json and check that Caddy runs it; only then is the deadline marked expired. If the
|
||||
/// load fails, the deadline is given back, so the next try (or control's own claim, while
|
||||
/// Caddy still runs the trial) decides.
|
||||
async fn revert(cfg: &Config, why: &str) -> Result<bool, String> {
|
||||
let dir = &cfg.state_dir;
|
||||
let expired = path(dir, ".expired");
|
||||
if std::fs::rename(path(dir, ""), &expired).is_err() {
|
||||
let reverting = path(dir, ".reverting");
|
||||
if std::fs::rename(path(dir, ""), &reverting).is_err() {
|
||||
// Control claimed it a moment ago: it confirms; nothing to do.
|
||||
return Ok(false);
|
||||
}
|
||||
let sock = cfg
|
||||
.caddy_compile()
|
||||
.caddy_admin_socket
|
||||
.clone()
|
||||
.ok_or("no caddy_admin_socket is configured")?;
|
||||
let text = std::fs::read(&cfg.caddy.config)
|
||||
.map_err(|e| format!("cannot read {}: {e}", cfg.caddy.config.display()))?;
|
||||
crate::caddy_link::load(Path::new(&sock), &text).await?;
|
||||
log::warn!(target: "ianitor::caddy-guard", "{why}: Caddy is back on the last good config");
|
||||
Ok(true)
|
||||
let loaded = async {
|
||||
let sock = cfg
|
||||
.caddy_compile()
|
||||
.caddy_admin_socket
|
||||
.clone()
|
||||
.ok_or("no caddy_admin_socket is configured")?;
|
||||
let text = std::fs::read(&cfg.caddy.config)
|
||||
.map_err(|e| format!("cannot read {}: {e}", cfg.caddy.config.display()))?;
|
||||
crate::caddy_link::load(Path::new(&sock), &text).await?;
|
||||
if !crate::caddy_link::runs(Path::new(&sock), &text).await? {
|
||||
return Err("Caddy took caddy.json but does not run it".to_owned());
|
||||
}
|
||||
Ok::<_, String>(())
|
||||
}
|
||||
.await;
|
||||
match loaded {
|
||||
Ok(()) => {
|
||||
let _ = std::fs::rename(&reverting, path(dir, ".expired"));
|
||||
log::warn!(target: "ianitor::caddy-guard", "{why}: Caddy is back on the last good config");
|
||||
Ok(true)
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = std::fs::rename(&reverting, path(dir, ""));
|
||||
Err(format!("{why}, but Caddy could not be put back yet: {e}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `ianitor-control caddy-revert`: after control stops, a trial still out goes back at once.
|
||||
/// `ianitor-control caddy-revert`: after control stops, a trial still out goes back at once
|
||||
/// (retried a few times, 1, 2, 4 and 8 s apart). With another engine serving there is no
|
||||
/// Caddy trial: a deadline left behind is cleared and nothing fails.
|
||||
pub async fn revert_after_stop(cfg: &Config) -> Result<bool, String> {
|
||||
let dir = &cfg.state_dir;
|
||||
match verdict(
|
||||
armed(dir).as_ref(),
|
||||
confirmed(dir),
|
||||
crate::util::now(),
|
||||
true,
|
||||
) {
|
||||
Verdict::Revert => revert(cfg, "control stopped with a trial loaded into Caddy").await,
|
||||
Verdict::Nothing => Ok(false),
|
||||
if !caddy_serves(cfg) {
|
||||
disarm(dir);
|
||||
return Ok(false);
|
||||
}
|
||||
resume_half_done(dir);
|
||||
let mut wait = 1;
|
||||
loop {
|
||||
let r = match verdict(
|
||||
armed(dir).as_ref(),
|
||||
confirmed(dir),
|
||||
crate::util::now(),
|
||||
true,
|
||||
) {
|
||||
Verdict::Revert => revert(cfg, "control stopped with a trial loaded into Caddy").await,
|
||||
Verdict::Nothing => Ok(false),
|
||||
};
|
||||
match r {
|
||||
Err(e) if wait <= 8 => {
|
||||
log::error!(target: "ianitor::caddy-guard", "{e}; trying again in {wait} s");
|
||||
tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
|
||||
wait *= 2;
|
||||
}
|
||||
other => return other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One tick of `ianitor-control caddy-guard`.
|
||||
pub async fn tick(cfg: &Config) -> Result<bool, String> {
|
||||
let dir = &cfg.state_dir;
|
||||
if !caddy_serves(cfg) {
|
||||
return Ok(false);
|
||||
}
|
||||
resume_half_done(dir);
|
||||
match verdict(
|
||||
armed(dir).as_ref(),
|
||||
confirmed(dir),
|
||||
@@ -145,14 +204,20 @@ pub async fn tick(cfg: &Config) -> Result<bool, String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `ianitor-control caddy-guard`: forever, once a second.
|
||||
/// `ianitor-control caddy-guard`: forever, once a second; after a failed revert it tries
|
||||
/// again with a growing pause (2, 4, … 30 s).
|
||||
pub async fn run(cfg: Config) -> Result<(), String> {
|
||||
log::info!(target: "ianitor::caddy-guard", "watching {} for trials under Caddy", cfg.state_dir.display());
|
||||
let mut pause = 1u64;
|
||||
loop {
|
||||
if let Err(e) = tick(&cfg).await {
|
||||
log::error!(target: "ianitor::caddy-guard", "{e}");
|
||||
match tick(&cfg).await {
|
||||
Ok(_) => pause = 1,
|
||||
Err(e) => {
|
||||
pause = (pause * 2).min(30);
|
||||
log::error!(target: "ianitor::caddy-guard", "{e}; trying again in {pause} s");
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
tokio::time::sleep(std::time::Duration::from_secs(pause)).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user