From ac214a023e3d823a8231c365656cfa27703f27d5 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 30 Nov 2026 13:13:44 +0000 Subject: enstate: reactions after applying, and after a switch After applying, units are restarted or reloaded, and started if they aren't running: they may have failed for want of their views. That happens with the store's lock released: a unit restarted may want the one that runs --migrate, which waits for the lock. After a switch, --migrate runs from the unit that units using views are ordered after, so only running units react, without waiting for them (--no-block try-restart, try-reload-or-restart). --- src/main.rs | 4 ++-- src/migrate.rs | 4 ++-- src/plan.rs | 44 ++++++++++++++++++++++++++++++++++---------- tests/common/mod.rs | 24 ++++++++++++++++++++---- tests/migrations.rs | 5 ++++- tests/receptor.rs | 4 +++- 6 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3539d76..49b8558 100644 --- a/src/main.rs +++ b/src/main.rs @@ -230,13 +230,13 @@ fn run() -> Result { let systemctl = settings .systemctl .unwrap_or_else(|| PathBuf::from("systemctl")); - let _lock = store.lock()?; + let lock = store.lock()?; match command { "plan" | "apply" => { let submission = Submission::load(path.as_deref().unwrap())?; let planned = plan::plan(&schema, &store, &submission)?; let plan = if command == "apply" { - planned.apply(&store, &schema_bytes, &systemctl)? + planned.apply(&store, &schema_bytes, &systemctl, lock)? } else { planned.plan }; diff --git a/src/migrate.rs b/src/migrate.rs index 340180f..dbbf38d 100644 --- a/src/migrate.rs +++ b/src/migrate.rs @@ -23,7 +23,7 @@ use serde::Serialize; use crate::content::Content; use crate::name::Address; -use crate::plan::{ViewOutcome, react}; +use crate::plan::{Reacting, ViewOutcome, react}; use crate::schema::{Reactions, Schema}; use crate::scratch::Scratch; use crate::store::{Kept, Snapshot, Store}; @@ -311,7 +311,7 @@ pub fn migrate( let failed = if dry_run { Vec::new() } else { - react(&reactions, systemctl) + react(&reactions, systemctl, Reacting::Migrate) }; Ok(Migration { accepted: refused.is_empty(), diff --git a/src/plan.rs b/src/plan.rs index 80e2bad..8627dd8 100644 --- a/src/plan.rs +++ b/src/plan.rs @@ -21,7 +21,7 @@ use crate::name::Address; use crate::program::{self, Given, ProgramFailed, Role}; use crate::schema::{Reactions, Schema}; use crate::scratch::Scratch; -use crate::store::{Kept, Store}; +use crate::store::{Kept, Lock, Store}; use crate::submission::{Change, Item, Submission}; use crate::view::{self, FileChange}; @@ -469,8 +469,16 @@ fn apply_items(base: &Content, items: &[Item], excluded: &BTreeSet) -> Co impl Planned { /// Applies the plan, if it was accepted: the new snapshot, the views that change, - /// and their reactions, by `systemctl`. - pub fn apply(self, store: &Store, schema_bytes: &[u8], systemctl: &Path) -> Result { + /// and, with `lock` released, their reactions. Units restarted may want the unit + /// that runs `--migrate`, which waits for the lock: holding it while they restart + /// would deadlock. + pub fn apply( + self, + store: &Store, + schema_bytes: &[u8], + systemctl: &Path, + lock: Lock, + ) -> Result { let mut plan = self.plan; if !plan.accepted { return Ok(plan); @@ -491,22 +499,38 @@ impl Planned { .with_context(|| format!("view {name}"))?; } } - plan.failed = react(&plan.reactions, systemctl); + drop(lock); + plan.failed = react(&plan.reactions, systemctl, Reacting::Apply); Ok(plan) } } -/// Runs `reactions`; what failed. -pub fn react(reactions: &Reactions, systemctl: &Path) -> Vec { +/// How reactions are run. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Reacting { + /// After applying a submission: units are restarted or reloaded, and started if + /// they aren't running (they may have failed for want of their views). + Apply, + /// After a generation change, from the unit that runs `--migrate`, which units + /// using views are ordered after: only units that are running, without waiting + /// for them (`--no-block`). A unit that isn't running starts with the views in + /// place. + Migrate, +} + +/// Runs `reactions` by `systemctl`, each unit once; returns what failed. +pub fn react(reactions: &Reactions, systemctl: &Path, how: Reacting) -> Vec { let mut failed = Vec::new(); - for (verb, units) in [ - ("restart", &reactions.restart), - ("reload", &reactions.reload), - ] { + let (options, restart, reload): (&[&str], _, _) = match how { + Reacting::Apply => (&[], "restart", "reload-or-restart"), + Reacting::Migrate => (&["--no-block"], "try-restart", "try-reload-or-restart"), + }; + for (verb, units) in [(restart, &reactions.restart), (reload, &reactions.reload)] { if units.is_empty() { continue; } let result = Command::new(systemctl) + .args(options) .arg(verb) .args(units) .status() diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 751d809..f1f3f74 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -73,17 +73,33 @@ impl Host { pub fn apply(&self, schema: &(Schema, Vec), dir: &Path) -> Plan { let submission = Submission::load(dir).unwrap(); let store = self.store(); - let _lock = store.lock().unwrap(); + let lock = store.lock().unwrap(); plan::plan(&schema.0, &store, &submission) .unwrap() - .apply(&store, &schema.1, &self.systemctl()) + .apply(&store, &schema.1, &self.systemctl(), lock) .unwrap() } - /// A systemctl that writes down what it was asked. + /// A systemctl that writes down what it was asked, and whether the store's lock + /// was free then. pub fn systemctl(&self) -> PathBuf { let log = self.root.join("systemctl.log"); - PathBuf::from(self.program("systemctl", &format!("echo \"$*\" >> {}", log.display()))) + let lock = self.root.join("state/lock"); + let locks = self.root.join("locks.log"); + PathBuf::from(self.program( + "systemctl", + &format!( + "echo \"$*\" >> {log}\nif flock -n {lock} true; then echo free; else echo held; fi >> {locks}", + log = log.display(), + lock = lock.display(), + locks = locks.display(), + ), + )) + } + + /// Whether the lock was free each time systemctl ran. + pub fn locks(&self) -> String { + std::fs::read_to_string(self.root.join("locks.log")).unwrap_or_default() } pub fn reactions(&self) -> String { diff --git a/tests/migrations.rs b/tests/migrations.rs index 7b78b8e..b91972c 100644 --- a/tests/migrations.rs +++ b/tests/migrations.rs @@ -61,7 +61,10 @@ fn a_renamed_slot_and_a_rollback() { ); assert_eq!(host.view("retiolum.key", "retiolum.key").unwrap(), b"K"); assert!(host.view("tinc.key", "tinc.key").is_none()); - assert!(host.reactions().contains("restart tincr.service")); + assert!( + host.reactions() + .contains("--no-block try-restart tincr.service") + ); let stored = host.store().current("secrets").unwrap().unwrap(); assert_eq!(stored.id, first); assert_eq!(stored.schema, s1.1); diff --git a/tests/receptor.rs b/tests/receptor.rs index 034e2a8..843eaa6 100644 --- a/tests/receptor.rs +++ b/tests/receptor.rs @@ -70,8 +70,10 @@ fn apply_rotate_and_nothing_to_change() { // A unit both restarted and reloaded is restarted. assert_eq!( host.reactions(), - "restart svc.service\nreload other.service\n" + "restart svc.service\nreload-or-restart other.service\n" ); + // With the lock released: a unit restarted may want the one that runs --migrate. + assert_eq!(host.locks(), "free\nfree\n"); // The same again changes nothing: no new snapshot, no reactions. let again = host.submission( -- cgit v1.3.1