diff --git a/src/control_plane.rs b/src/control_plane.rs index 7943698..22ad024 100644 --- a/src/control_plane.rs +++ b/src/control_plane.rs @@ -39,7 +39,8 @@ use crate::deployment_plan::{ use crate::drivers::{ ChallengeFailure, CultCacheTopologyDriver, CultCacheWriteLeaseDriver, DockerRunnerDriver, FrozenSourceReceipt, GitSourceDriver, InstalledReleaseObservation, IsolationEvidence, NginxRouteDriver, - ProcessIdentity, RouteObservation, RoutePreflightReceipt, RunnerPort, SourcePort, + ProcessIdentity, RouteActuation, RouteActuationGate, RouteActuationRefused, RouteActuators, + RouteObservation, RoutePreflightReceipt, RunnerPort, SourcePort, SystemdTransientWorkloadDriver, TopologyPort, WorkloadObservation, WorkloadPort, WriteLeasePort, }; @@ -52,15 +53,32 @@ const DEPLOYMENT_COMMAND_SCHEMA: &str = "idunn.deployment_command.v2"; const DEPLOYMENT_TRANSACTION_SCHEMA: &str = "idunn.deployment_transaction.v4"; const DEPLOYMENT_TRANSACTION_SCHEMA_V3: &str = "idunn.deployment_transaction.v3"; const DEPLOYMENT_TRANSACTION_SCHEMA_V2: &str = "idunn.deployment_transaction.v2"; -const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v3"; +const ADMITTED_GENERATION_SCHEMA: &str = "idunn.admitted_generation.v4"; +const ADMITTED_GENERATION_SCHEMA_V3: &str = "idunn.admitted_generation.v3"; const ADMITTED_GENERATION_SCHEMA_V2: &str = "idunn.admitted_generation.v2"; /// The capability whose declaration makes a target Odin-correlated. pub(crate) const ODIN_RENDEZVOUS_CAPABILITY: &str = "odin.verse-rendezvous"; -/// How many times continuity will restart one admitted release before it -/// concludes the release itself is the problem. More than one because a start -/// can fail for a passing reason -- a port still held, a peer not yet up -- -/// and few because each attempt holds the target against any deployment. -const CONTINUITY_RESTART_ATTEMPTS: usize = 3; +const TARGET_SUPERVISION_SCHEMA: &str = "idunn.target_supervision.v1"; +/// How many times continuity will restart one target inside one window before +/// it concludes the target itself is the problem. More than one because a +/// start can fail for a passing reason -- a port still held, a peer not yet up +/// -- and bounded because each attempt holds the target against any +/// deployment. Counted per target, so a restart that succeeds and dies again +/// does not start the count over. +const CONTINUITY_RESTART_ATTEMPTS: usize = 6; +const CONTINUITY_RESTART_WINDOW_MILLIS: u64 = 3_600_000; +/// The wait after the first restart in a window; it doubles with each further one. +const CONTINUITY_RESTART_BACKOFF_MILLIS: u64 = 5_000; + +/// Forward route actuations (fragment write, firewall, `nginx -t`, reload, +/// private-mount validation) one target may perform per sliding window. A +/// healthy target performs none; a legitimate deployment performs one or two. +/// Survival actuations are counted against it but never refused. +const ROUTE_ACTUATION_CEILING: usize = 12; +const ROUTE_ACTUATION_WINDOW_MILLIS: u64 = 3_600_000; +/// The longest wait between route challenges while proofs keep failing. The +/// shortest is the observation max age. +const ROUTE_CHALLENGE_BACKOFF_CAP_MILLIS: u64 = 600_000; /// How long continuity waits after it could not prepare a restart (the /// projection would not demote) before trying again. @@ -858,8 +876,10 @@ enum TerminalRecovery { }, } -/// Route supervision's durable memory for one admitted routed target. Carried -/// across commits except for the per-incarnation parts. +/// Route supervision's durable memory for one admitted routed incarnation: +/// challenge pacing and the degradation mark. It belongs to the incarnation, so +/// a new generation starts from `default()`. What a target may actuate outlives +/// every generation and lives in `TargetSupervision`. #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] struct RouteSupervisionState { @@ -869,56 +889,199 @@ struct RouteSupervisionState { /// Set while route proof keeps failing. Marks the route degraded so /// dependents stop selecting it; it never authorizes a restart. degraded_since_unix_millis: Option, - actuations: ActuationWindow, -} - -/// Route actuations (write, reload, firewall) inside a rolling window, so a -/// per-target ceiling survives an Idunn restart. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct ActuationWindow { - window_started_at_unix_millis: u64, - count: u32, } impl RouteSupervisionState { - /// A new incarnation starts unobserved and undegraded, but inherits the - /// target's actuation window: a ceiling that reset on every commit would - /// not be a ceiling. - fn for_new_incarnation(incumbent: Option<&Self>) -> Self { - Self { - actuations: incumbent.map(|state| state.actuations).unwrap_or_default(), - ..Self::default() - } + /// The next challenge is not due yet. + fn is_waiting(&self, now: u64, maximum_age_millis: u64) -> bool { + self.next_challenge_at_unix_millis.is_some_and(|next| { + is_waiting( + now, + next, + ROUTE_CHALLENGE_BACKOFF_CAP_MILLIS.max(maximum_age_millis), + ) + }) + } + + /// A failed proof, or a failed repair of the fragment it needs. Observation + /// state only: the route is marked degraded and the next challenge waits, + /// twice as long each time, from the observation max age up to a cap. + fn record_failed_challenge(&mut self, now: u64, maximum_age_millis: u64) { + self.consecutive_failures = self.consecutive_failures.saturating_add(1); + let doublings = self.consecutive_failures.saturating_sub(1).min(16); + let wait = maximum_age_millis + .saturating_mul(1 << doublings) + .min(ROUTE_CHALLENGE_BACKOFF_CAP_MILLIS) + .max(maximum_age_millis); + self.last_challenge_at_unix_millis = Some(now); + self.next_challenge_at_unix_millis = Some(now.saturating_add(wait)); + self.degraded_since_unix_millis.get_or_insert(now); + } + + fn record_proved_challenge(&mut self, now: u64) { + self.consecutive_failures = 0; + self.last_challenge_at_unix_millis = Some(now); + self.next_challenge_at_unix_millis = None; + self.degraded_since_unix_millis = None; } fn validate(&self) -> Result<()> { if let Some(since) = self.degraded_since_unix_millis { ensure!(since > 0, "route degradation has no start time"); } - ensure!( - self.actuations.count == 0 || self.actuations.window_started_at_unix_millis > 0, - "route actuation window has a count but no start" - ); Ok(()) } } -/// Continuity restarts of one target inside a rolling window. Belongs to the -/// target, not the generation: a restart that succeeds must not reset it. -#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -struct ContinuityBackoff { - window_started_at_unix_millis: Option, - attempts: u32, - next_restart_at_unix_millis: Option, +/// What one target has been allowed to do, kept across every generation and +/// present before the first: route actuations and continuity restarts, each a +/// sliding log of the times they happened. Owned by the meter primitives below; +/// the route driver only asks, and no generation write carries it. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.target_supervision", + schema = "idunn.target_supervision.v1" +)] +struct TargetSupervision { + #[cultcache(key = 0)] + schema_version: String, + #[cultcache(key = 1)] + target: String, + /// The newest `ROUTE_ACTUATION_CEILING` actuation times, ascending. + #[cultcache(key = 2)] + route_actuations: Vec, + /// The newest `CONTINUITY_RESTART_ATTEMPTS` restart times, ascending. + #[cultcache(key = 3)] + continuity_restarts: Vec, + /// A restart whose projection could not be prepared waits until this time. + #[cultcache(key = 4)] + continuity_deferred_until: Option, + #[cultcache(key = 5)] + continuity_deferral_reason: Option, } -impl ContinuityBackoff { +/// The entries of a sliding log still inside `window` at `now`. An entry from +/// the future means the clock stepped back; it counts as `now`, so a backwards +/// step holds a meter for at most one window however far the clock moved. +fn live_entries(log: &[u64], now: u64, window: u64) -> Vec { + log.iter() + .map(|&at| at.min(now)) + .filter(|&at| now - at < window) + .collect() +} + +/// Record `now`, keeping the newest `limit` entries. That is exact for "at most +/// `limit` in any window", even after a forced entry pushes past the limit. +fn record_entry(log: &mut Vec, now: u64, limit: usize) { + for at in log.iter_mut() { + *at = (*at).min(now); + } + log.push(now); + if log.len() > limit { + log.drain(..log.len() - limit); + } +} + +impl TargetSupervision { + fn new(target: &str) -> Self { + Self { + schema_version: TARGET_SUPERVISION_SCHEMA.into(), + target: target.into(), + route_actuations: Vec::new(), + continuity_restarts: Vec::new(), + continuity_deferred_until: None, + continuity_deferral_reason: None, + } + } + + fn route_used(&self, now: u64) -> usize { + live_entries(&self.route_actuations, now, ROUTE_ACTUATION_WINDOW_MILLIS).len() + } + + /// When the oldest counted actuation leaves the window, if the ceiling is reached. + fn route_reopens_at(&self, now: u64) -> Option { + let live = live_entries(&self.route_actuations, now, ROUTE_ACTUATION_WINDOW_MILLIS); + (live.len() >= ROUTE_ACTUATION_CEILING) + .then(|| live[live.len() - ROUTE_ACTUATION_CEILING] + ROUTE_ACTUATION_WINDOW_MILLIS) + } + + /// Count one route actuation. Only a `Forward` change can be refused; a + /// `Survival` is always admitted and always counted. + fn charge_route(&mut self, now: u64, kind: RouteActuation) -> Result<(), RouteActuationRefused> { + if kind == RouteActuation::Forward + && let Some(reopens_at) = self.route_reopens_at(now) + { + return Err(RouteActuationRefused { + target: self.target.clone(), + used: self.route_used(now), + reopens_at_unix_millis: reopens_at, + }); + } + record_entry(&mut self.route_actuations, now, ROUTE_ACTUATION_CEILING); + Ok(()) + } + + fn restarts_used(&self, now: u64) -> usize { + live_entries(&self.continuity_restarts, now, CONTINUITY_RESTART_WINDOW_MILLIS).len() + } + + fn restarts_exhausted(&self, now: u64) -> bool { + self.restarts_used(now) >= CONTINUITY_RESTART_ATTEMPTS + } + + /// The next restart is due one doubling wait after the last, from the + /// number of restarts still inside the window. + fn next_restart_at(&self, now: u64) -> Option { + let live = live_entries(&self.continuity_restarts, now, CONTINUITY_RESTART_WINDOW_MILLIS); + let last = *live.last()?; + Some(last.saturating_add(CONTINUITY_RESTART_BACKOFF_MILLIS << (live.len() - 1))) + } + + /// Whether continuity must still wait: the doubling wait after the last + /// restart, or the deferral after a projection that would not demote. + fn continuity_is_waiting(&self, now: u64) -> bool { + self.next_restart_at(now).is_some_and(|due| { + is_waiting( + now, + due, + CONTINUITY_RESTART_BACKOFF_MILLIS << (CONTINUITY_RESTART_ATTEMPTS - 1), + ) + }) || self + .continuity_deferred_until + .is_some_and(|due| is_waiting(now, due, CONTINUITY_DEFERRAL_MILLIS)) + } + + /// One restart scheduled. It ends any deferral: the projection was prepared. + fn record_restart(&mut self, now: u64) { + record_entry(&mut self.continuity_restarts, now, CONTINUITY_RESTART_ATTEMPTS); + self.continuity_deferred_until = None; + self.continuity_deferral_reason = None; + } + + fn defer_continuity(&mut self, now: u64, reason: &str) { + self.continuity_deferred_until = Some(now.saturating_add(CONTINUITY_DEFERRAL_MILLIS)); + self.continuity_deferral_reason = Some(reason.to_owned()); + } + fn validate(&self) -> Result<()> { ensure!( - self.attempts == 0 || self.window_started_at_unix_millis.is_some(), - "continuity backoff has attempts but no window" + self.schema_version == TARGET_SUPERVISION_SCHEMA, + "target supervision schema is unsupported" + ); + require_id(&self.target, "supervised target")?; + for (log, limit, what) in [ + (&self.route_actuations, ROUTE_ACTUATION_CEILING, "route actuation"), + (&self.continuity_restarts, CONTINUITY_RESTART_ATTEMPTS, "continuity restart"), + ] { + ensure!(log.len() <= limit, "{what} log exceeds its bound"); + ensure!( + log.iter().all(|&at| at > 0) && log.windows(2).all(|pair| pair[0] <= pair[1]), + "{what} log is not an ascending list of times" + ); + } + ensure!( + self.continuity_deferred_until.is_some() == self.continuity_deferral_reason.is_some(), + "continuity deferral has a time or a reason but not both" ); Ok(()) } @@ -1936,7 +2099,7 @@ impl AdmittedOdinAuthority { #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] #[cultcache( type = "idunn.admitted_generation", - schema = "idunn.admitted_generation.v3" + schema = "idunn.admitted_generation.v4" )] struct AdmittedGeneration { #[cultcache(key = 0)] @@ -1979,14 +2142,16 @@ struct AdmittedGeneration { /// Meaningful only for an Odin-correlated generation. #[cultcache(key = 17)] odin_publisher_sequence_cursor: u64, - /// Still readable, decides nothing new: route supervision state replaces it. - #[cultcache(key = 18)] - route_repair_started_at_unix_millis: Option, + // Keys 18 (route repair start) and 20 (continuity backoff) are retired. + // The slots stay as gaps: the layout is positional, so a slot cannot be + // reused without a bump. /// Present exactly when `routing` is Promoted. #[cultcache(key = 19)] route_supervision: Option, - #[cultcache(key = 20)] - continuity_backoff: ContinuityBackoff, + /// Why this generation is held or down, for `status`. Cleared when the + /// generation is replaced. + #[cultcache(key = 21)] + last_error: Option, } /// The Odin-side receipts of an Odin-correlated generation. Decisions that @@ -2023,7 +2188,6 @@ impl AdmittedGeneration { fn from_transaction( transaction: &DeploymentTransaction, odin_authority: Option, - incumbent: Option<&AdmittedGeneration>, now: u64, ) -> Result { ensure!( @@ -2042,11 +2206,8 @@ impl AdmittedGeneration { ReadinessEvidence::RouteProof { .. } => (None, None), }; let routing = required(&transaction.routing, "route disposition")?.clone(); - let route_supervision = matches!(&routing, RoutingEvidence::Promoted { .. }).then(|| { - RouteSupervisionState::for_new_incarnation( - incumbent.and_then(|value| value.route_supervision.as_ref()), - ) - }); + let route_supervision = matches!(&routing, RoutingEvidence::Promoted { .. }) + .then(RouteSupervisionState::default); let generation = Self { schema_version: ADMITTED_GENERATION_SCHEMA.into(), target: transaction.target.clone(), @@ -2067,11 +2228,8 @@ impl AdmittedGeneration { routing, odin_authority, odin_publisher_sequence_cursor: transaction.odin_publisher_sequence_cursor, - route_repair_started_at_unix_millis: None, route_supervision, - continuity_backoff: incumbent - .map(|value| value.continuity_backoff.clone()) - .unwrap_or_default(), + last_error: None, }; generation.validate()?; Ok(generation) @@ -2136,13 +2294,6 @@ impl AdmittedGeneration { "admitted route observation belongs to another runtime instance" ); } - if let Some(started_at) = self.route_repair_started_at_unix_millis { - ensure!(started_at > 0, "admitted route repair has no start time"); - ensure!( - matches!(&self.routing, RoutingEvidence::Promoted { .. }), - "only a promoted routed generation can own route repair intent" - ); - } ensure!( self.route_supervision.is_some() == matches!(&self.routing, RoutingEvidence::Promoted { .. }), @@ -2151,7 +2302,6 @@ impl AdmittedGeneration { if let Some(state) = &self.route_supervision { state.validate()?; } - self.continuity_backoff.validate()?; Ok(()) } } @@ -2175,6 +2325,9 @@ struct RuntimeOptions { /// can be served; a binding that names one then fails at its first /// driver call, not at startup, so an Idunn without hosts is unchanged. host_actuator_bind: Option, + /// Where route drivers find nginx, systemctl, ufw and systemd-run, and + /// their scratch directory. Every route driver is built from this. + route_actuators: RouteActuators, } impl Default for RuntimeOptions { @@ -2200,6 +2353,7 @@ impl Default for RuntimeOptions { topology_maximum_future_skew_millis: DEFAULT_TOPOLOGY_MAXIMUM_FUTURE_SKEW_MILLIS, poll_millis: 500, host_actuator_bind: None, + route_actuators: RouteActuators::default(), } } } @@ -2565,6 +2719,7 @@ struct ControlSnapshot { commands: Vec>, transactions: Vec>, admitted: Vec>, + targets: Vec>, } impl ControlSnapshot { @@ -2607,6 +2762,19 @@ impl ControlSnapshot { ); snapshot.admitted.push(Stored { envelope, value }); } + TargetSupervision::TYPE => { + ensure!( + envelope.schema_id.as_deref() == Some(TARGET_SUPERVISION_SCHEMA), + "Idunn control store contains an unsupported target supervision record" + ); + let value: TargetSupervision = decode_record(&envelope)?; + value.validate()?; + ensure!( + envelope.key == value.target, + "target supervision key is not its target" + ); + snapshot.targets.push(Stored { envelope, value }); + } _ => bail!("Idunn control store contains a foreign document"), } } @@ -2642,6 +2810,15 @@ impl ControlSnapshot { admitted_targets.len() == self.admitted.len(), "Idunn control store contains duplicate current generations" ); + ensure!( + self.targets + .iter() + .map(|stored| stored.value.target.as_str()) + .collect::>() + .len() + == self.targets.len(), + "Idunn control store contains duplicate target supervision records" + ); for transaction in &self.transactions { let command = self .commands @@ -2730,6 +2907,19 @@ impl ControlSnapshot { .find(|stored| stored.value.target == target) } + fn supervision_for(&self, target: &str) -> Option<&Stored> { + self.targets + .iter() + .find(|stored| stored.value.target == target) + } + + /// The target's meters, or empty ones: a target that was never metered has + /// used nothing. + fn supervision_or_new(&self, target: &str) -> TargetSupervision { + self.supervision_for(target) + .map_or_else(|| TargetSupervision::new(target), |stored| stored.value.clone()) + } + /// The admitted generation of the target that provides /// `odin.verse-rendezvous`: Odin is whichever target says so, not /// whichever is named "odin". @@ -3016,7 +3206,8 @@ struct LegacyAdmittedGeneration { impl LegacyAdmittedGeneration { /// Every v2 generation was admitted on Odin receipts, so it migrates as /// Odin-correlated with those receipts unchanged. Route supervision state - /// starts empty for a promoted route; backoff starts empty. + /// starts empty for a promoted route. The v2 repair start decided nothing + /// after S1 and is not carried. fn into_current(self) -> AdmittedGeneration { let route_supervision = matches!(&self.routing, RoutingEvidence::Promoted { .. }) .then(RouteSupervisionState::default); @@ -3041,13 +3232,133 @@ impl LegacyAdmittedGeneration { routing: self.routing, odin_authority: Some(self.odin_authority), odin_publisher_sequence_cursor: self.odin_publisher_sequence_cursor, - route_repair_started_at_unix_millis: self.route_repair_started_at_unix_millis, route_supervision, - continuity_backoff: ContinuityBackoff::default(), + last_error: None, } } } +/// The v3 admitted-generation layout. Its meters (`actuations`, the continuity +/// backoff, the repair start) are retired: no released Idunn ever wrote a +/// nonzero one, and a record that shows one is refused instead of lifted. +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache( + type = "idunn.admitted_generation", + schema = "idunn.admitted_generation.v3" +)] +struct LegacyAdmittedGenerationV3 { + #[cultcache(key = 0)] + schema_version: String, + #[cultcache(key = 1)] + target: String, + #[cultcache(key = 2)] + generation_id: String, + #[cultcache(key = 3)] + command_id: String, + #[cultcache(key = 4)] + transaction_id: String, + #[cultcache(key = 5)] + admitted_at_unix_millis: u64, + #[cultcache(key = 6)] + plan: CompiledDeploymentPlan, + #[cultcache(key = 7)] + sealed_release: SealedRelease, + #[cultcache(key = 8)] + installed_release: InstalledReleaseObservation, + #[cultcache(key = 9)] + expected: IdunnExpectedIncarnationRecord, + #[cultcache(key = 10)] + activation: IdunnRuntimeActivationRecord, + #[cultcache(key = 11)] + workload: WorkloadObservation, + #[cultcache(key = 12)] + leasing: LeasingEvidence, + #[cultcache(key = 13)] + ready: ReadinessEvidence, + #[cultcache(key = 14)] + latest_odin_observation: Option, + #[cultcache(key = 15)] + routing: RoutingEvidence, + #[cultcache(key = 16)] + odin_authority: Option, + #[cultcache(key = 17)] + odin_publisher_sequence_cursor: u64, + #[cultcache(key = 18)] + route_repair_started_at_unix_millis: Option, + #[cultcache(key = 19)] + route_supervision: Option, + #[cultcache(key = 20)] + continuity_backoff: LegacyContinuityBackoffV3, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct LegacyRouteSupervisionStateV3 { + last_challenge_at_unix_millis: Option, + consecutive_failures: u32, + next_challenge_at_unix_millis: Option, + degraded_since_unix_millis: Option, + actuations: LegacyActuationWindowV3, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct LegacyActuationWindowV3 { + window_started_at_unix_millis: u64, + count: u32, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct LegacyContinuityBackoffV3 { + window_started_at_unix_millis: Option, + attempts: u32, + next_restart_at_unix_millis: Option, +} + +impl LegacyAdmittedGenerationV3 { + /// Drops the retired meters. The lift creates no `TargetSupervision`: every + /// target starts unmetered, which is what no released binary ever changed. + fn into_current(self) -> Result { + ensure!( + self.continuity_backoff.attempts == 0 + && self + .route_supervision + .as_ref() + .is_none_or(|state| state.actuations.count == 0), + "admitted generation of {} records route actuations or continuity restarts that no released Idunn wrote; refusing to drop them", + self.target + ); + Ok(AdmittedGeneration { + schema_version: ADMITTED_GENERATION_SCHEMA.into(), + target: self.target, + generation_id: self.generation_id, + command_id: self.command_id, + transaction_id: self.transaction_id, + admitted_at_unix_millis: self.admitted_at_unix_millis, + plan: self.plan, + sealed_release: self.sealed_release, + installed_release: self.installed_release, + expected: self.expected, + activation: self.activation, + workload: self.workload, + leasing: self.leasing, + ready: self.ready, + latest_odin_observation: self.latest_odin_observation, + routing: self.routing, + odin_authority: self.odin_authority, + odin_publisher_sequence_cursor: self.odin_publisher_sequence_cursor, + route_supervision: self.route_supervision.map(|state| RouteSupervisionState { + last_challenge_at_unix_millis: state.last_challenge_at_unix_millis, + consecutive_failures: state.consecutive_failures, + next_challenge_at_unix_millis: state.next_challenge_at_unix_millis, + degraded_since_unix_millis: state.degraded_since_unix_millis, + }), + last_error: None, + }) + } +} + /// Decode a v2 or v3 transaction and lift it to the current shape, without /// validating it. History reads stop here; the control store validates. fn lift_legacy_transaction(envelope: &CultCacheEnvelope) -> Result { @@ -3097,8 +3408,8 @@ fn read_transaction_record(envelope: &CultCacheEnvelope) -> Result Result { match envelope.schema_id.as_deref() { Some(ADMITTED_GENERATION_SCHEMA) => { @@ -3106,6 +3417,17 @@ fn read_generation_record(envelope: &CultCacheEnvelope) -> Result { + let legacy: LegacyAdmittedGenerationV3 = rmp_serde::from_slice(&envelope.payload) + .context("decoding a v3 admitted generation")?; + ensure!( + legacy.schema_version == ADMITTED_GENERATION_SCHEMA_V3, + "stored generation schema differs from its envelope" + ); + let value = legacy.into_current()?; + value.validate()?; + Ok(value) + } Some(ADMITTED_GENERATION_SCHEMA_V2) => { let legacy: LegacyAdmittedGeneration = rmp_serde::from_slice(&envelope.payload) .context("decoding a v2 admitted generation")?; @@ -3121,7 +3443,7 @@ fn read_generation_record(envelope: &CultCacheEnvelope) -> Result Result { Some(DEPLOYMENT_TRANSACTION_SCHEMA_V2 | DEPLOYMENT_TRANSACTION_SCHEMA_V3) )) || (envelope.r#type == AdmittedGeneration::TYPE - && schema == Some(ADMITTED_GENERATION_SCHEMA_V2)) + && matches!( + schema, + Some(ADMITTED_GENERATION_SCHEMA_V2 | ADMITTED_GENERATION_SCHEMA_V3) + )) }) .collect::>(); let mut migrated = 0; @@ -3205,6 +3530,17 @@ fn admitted_envelope(value: &AdmittedGeneration, now: u64) -> Result Result { + value.validate()?; + typed_envelope( + &value.target, + TargetSupervision::TYPE, + TARGET_SUPERVISION_SCHEMA, + value, + now, + ) +} + fn typed_envelope( key: &str, record_type: &str, @@ -3347,9 +3683,11 @@ fn backoff_wait(poll_millis: u64, failures: u32) -> u64 { .min(RESUME_BACKOFF_CEILING_MILLIS) } -/// Whether an attempt due at `not_before` must still wait at `now`. -fn is_waiting(now: u64, not_before: u64) -> bool { - now < not_before +/// Whether an attempt due at `not_before` must still wait at `now`. A due time +/// further ahead than any wait its owner can set means the clock stepped back, +/// so the attempt is due: a step stalls nothing longer than the wait itself. +fn is_waiting(now: u64, not_before: u64, longest_wait: u64) -> bool { + now < not_before && not_before - now <= longest_wait } /// Whether a transaction moved between two reads. An error note (`last_error`) @@ -3751,9 +4089,83 @@ fn status(store_path: &Path, command_id: Option<&str>) -> Result<()> { } } } + // What supervision holds for each target: the union of the admitted + // generations and the metered targets, so a target that has only been + // deployed to (no generation yet) still shows what it has used. + let now = now_millis()?; + let targets = snapshot + .admitted + .iter() + .map(|stored| stored.value.target.as_str()) + .chain(snapshot.targets.iter().map(|stored| stored.value.target.as_str())) + .collect::>(); + for target in targets { + for line in render_supervision( + target, + snapshot.admitted_for(target).map(|stored| &stored.value), + snapshot.supervision_for(target).map(|stored| &stored.value), + now, + ) { + println!("{line}"); + } + } Ok(()) } +/// One target's meters and route pacing, one line each, times in unix +/// milliseconds. The counts are what is still inside the window at `now`. +fn render_supervision( + target: &str, + generation: Option<&AdmittedGeneration>, + supervision: Option<&TargetSupervision>, + now: u64, +) -> Vec { + let at = |time: Option| time.map_or("none".to_owned(), |millis| millis.to_string()); + let meters = supervision.cloned().unwrap_or_else(|| TargetSupervision::new(target)); + let mut lines = vec![format!( + "target {target} {}", + generation.map_or("no-admitted-generation", |value| value.generation_id.as_str()) + )]; + if let Some(reason) = generation.and_then(|value| value.last_error.as_deref()) { + lines.push(format!(" held: {reason}")); + } + lines.push(format!( + " continuity restarts {}/{} next-restart-at {}", + meters.restarts_used(now), + CONTINUITY_RESTART_ATTEMPTS, + at(meters.next_restart_at(now)) + )); + if let (Some(until), Some(reason)) = ( + meters.continuity_deferred_until, + &meters.continuity_deferral_reason, + ) { + lines.push(format!(" continuity deferred until {until}: {reason}")); + } + if let Some(route) = generation.and_then(|value| value.route_supervision.as_ref()) { + lines.push(format!( + " route {} consecutive-failures {} last-challenge-at {} next-challenge-at {}", + match route.degraded_since_unix_millis { + Some(since) => format!("degraded-since {since}"), + None => "healthy".to_owned(), + }, + route.consecutive_failures, + at(route.last_challenge_at_unix_millis), + at(route.next_challenge_at_unix_millis) + )); + } + if generation.is_none_or(|value| value.route_supervision.is_some()) + || !meters.route_actuations.is_empty() + { + lines.push(format!( + " route actuations {}/{} in window, reopens-at {}", + meters.route_used(now), + ROUTE_ACTUATION_CEILING, + at(meters.route_reopens_at(now)) + )); + } + lines +} + fn derived_command_status(transactions: &[&DeploymentTransaction]) -> (&'static str, String) { if transactions.is_empty() { return ("queued", String::new()); @@ -3870,6 +4282,19 @@ struct Engine { resume_backoff: Mutex>, } +/// The route driver's door to the actuation ceiling: every group of host +/// mutations a driver makes for `target` is counted through here. +struct EngineRouteGate<'a> { + engine: &'a Engine, + target: &'a str, +} + +impl RouteActuationGate for EngineRouteGate<'_> { + fn admit(&self, kind: RouteActuation) -> Result<()> { + self.engine.charge_route_actuation(self.target, kind) + } +} + #[derive(Clone, Copy)] struct ResumeBackoff { failures: u32, @@ -3941,6 +4366,90 @@ impl Engine { host_anchors_from(&self.options.bindings_dir) } + /// Every route driver comes from here, so the actuator paths are the + /// options' and nothing else's. + fn route_driver(&self, binding: RouteBinding) -> NginxRouteDriver { + NginxRouteDriver::with_actuators(binding, &self.options.route_actuators) + } + + fn route_gate<'a>(&'a self, target: &'a str) -> EngineRouteGate<'a> { + EngineRouteGate { + engine: self, + target, + } + } + + /// Count one route actuation against the target's sliding ceiling, in its + /// `TargetSupervision`, before the driver acts. The record is created by + /// the first charge, so a first deployment is metered like any other. A + /// refused `Forward` is the typed `RouteActuationRefused`; a `Survival` + /// is always counted and never refused. + fn charge_route_actuation(&self, target: &str, kind: RouteActuation) -> Result<()> { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let mut next = snapshot.supervision_or_new(target); + let now = now_millis()?; + next.charge_route(now, kind).map_err(anyhow::Error::new)?; + self.write_target_supervision(snapshot.supervision_for(target), &next, now) + } + + /// Replace a target's meters, or create them when `seen` is `None`, by + /// compare-exchange against exactly what was read. + fn write_target_supervision( + &self, + seen: Option<&Stored>, + next: &TargetSupervision, + now: u64, + ) -> Result<()> { + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: TargetSupervision::TYPE.into(), + key: next.target.clone(), + current: seen.map(|stored| stored.envelope.clone()), + }], + &[target_supervision_envelope(next, now)?], + )?, + "target supervision changed before its meters were written" + ); + Ok(()) + } + + /// Replace an admitted generation by compare-exchange against the envelope + /// that was read. + fn replace_generation( + &self, + seen: &Stored, + next: &AdmittedGeneration, + now: u64, + ) -> Result<()> { + ensure!( + SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( + &[CultCacheExpectedEnvelope { + r#type: AdmittedGeneration::TYPE.into(), + key: seen.value.target.clone(), + current: Some(seen.envelope.clone()), + }], + &[admitted_envelope(next, now)?], + )?, + "admitted generation changed before its write" + ); + Ok(()) + } + + /// Say a fault once while it lasts, under `key`. + fn report_once(&self, key: &str, message: String) { + let offered = self + .fault_reports + .lock() + .expect("fault report mutex") + .entry(key.to_string()) + .or_default() + .offer(Some(message)); + if let Some(message) = offered { + eprintln!("{message}"); + } + } + fn host_access(&self) -> Result> { let hub = self .host_actuators @@ -4550,7 +5059,9 @@ impl Engine { .expect("resume backoff mutex") .get(id) .is_some_and(|backoff| { - now_millis().is_ok_and(|now| is_waiting(now, backoff.not_before_unix_millis)) + now_millis().is_ok_and(|now| { + is_waiting(now, backoff.not_before_unix_millis, RESUME_BACKOFF_CEILING_MILLIS) + }) }) { return Ok(false); @@ -4977,41 +5488,15 @@ impl Engine { // admitted release will not start, rescheduling it forever keeps // the target permanently occupied -- and a target with a live // transaction accepts no deployment, so the one action that could - // fix it is exactly the one that is locked out. Give up after a few - // attempts and leave the target free for a replacement. - // - // Counted against this generation id, so a successful restart -- - // which admits a new generation -- starts the count over rather - // than carrying old failures forward. - // Failed restarts retire to history the moment they finish, so - // the count must read history: counting only the live set found - // zero every time and restarted a release that could not start - // forever, which is the loop the give-up exists to end. - let is_refused_restart = |transaction: &DeploymentTransaction| { - transaction.target == current.value.target - && transaction.command_kind == CommandKind::Continuity - && transaction.incumbent_generation_id.as_deref() - == Some(current.value.generation_id.as_str()) - && matches!( - transaction.completion, - Some(TransactionCompletion::FailedBeforeFencing { .. }) - | Some(TransactionCompletion::FailedAfterFencing { .. }) - ) - }; - // An unreadable history counts as zero refusals only by lying: - // the ceiling would never trip. Without it, nothing is actuated. - let Some(history) = self.history_for_decision() else { - continue; - }; - let refused_restarts = snapshot - .transactions - .iter() - .filter(|stored| is_refused_restart(&stored.value)) - .count() - + history - .iter() - .filter(|transaction| is_refused_restart(transaction)) - .count(); + // fix it is exactly the one that is locked out. Restarts are + // counted in the target's own log, in a sliding window, and spaced + // by a doubling wait. The log outlives every generation, so a + // release that starts, dies and starts again is bounded the same as + // one that never starts. Nothing here reads history: an unreadable + // history file must not stop crash recovery. + let now = now_millis()?; + let supervision = snapshot.supervision_or_new(¤t.value.target); + let continuity_key = format!("continuity:{}", current.value.target); if let Some(blocker) = blocker { // Yielding a *deployment* to continuity is right: the incumbent @@ -5026,7 +5511,7 @@ impl Engine { // the deployment is the only thing left that can fix the // target -- aborting it would close the last door. if blocker.value.command_kind == CommandKind::Deploy - && refused_restarts < CONTINUITY_RESTART_ATTEMPTS + && !supervision.restarts_exhausted(now) && blocker.value.phase < DeploymentPhase::Fencing && blocker.value.pre_fencing_abort.is_none() // A held record is reported and never aborted by Idunn. @@ -5042,13 +5527,21 @@ impl Engine { } continue; } - if refused_restarts >= CONTINUITY_RESTART_ATTEMPTS { - eprintln!( - "Idunn stopped restarting admitted {}: its release failed to start {} times. The target is free for a deployment to replace it: {workload_error:#}", - current.value.target, refused_restarts + if supervision.restarts_exhausted(now) { + self.report_once( + &continuity_key, + format!( + "Idunn stopped restarting admitted {}: {} restarts inside the window. The target is free for a deployment to replace it: {workload_error:#}", + current.value.target, + supervision.restarts_used(now) + ), ); continue; } + self.clear_fault(&continuity_key); + if supervision.continuity_is_waiting(now) { + continue; + } // The projected activation names an incarnation that is gone. Every // restart is issued a fresh one, so leaving the old record standing @@ -5058,17 +5551,6 @@ impl Engine { // Demote to Expected-only, the same shape an aborted deployment // leaves, and let the restart publish its own activation once it is // observed. - let now = now_millis()?; - // A restart whose projection could not be prepared is deferred in - // the generation's own backoff, not retried every tick. - if current - .value - .continuity_backoff - .next_restart_at_unix_millis - .is_some_and(|not_before| now < not_before) - { - continue; - } let demotion = (|| -> Result<()> { let topology = self.topology(); let provider_anchor = self.provider_anchor_for_plan(¤t.value.plan)?; @@ -5094,7 +5576,13 @@ impl Engine { "Idunn deferred continuity for admitted {} after refusing to demote its projection: {error:#}", current.value.target ); - self.defer_continuity_restart(current, now)?; + let mut deferred = supervision.clone(); + deferred.defer_continuity(now, &truncate(&format!("{error:#}"), 2048)); + self.write_target_supervision( + snapshot.supervision_for(¤t.value.target), + &deferred, + now, + )?; progressed = true; continue; } @@ -5118,6 +5606,10 @@ impl Engine { command.validate()?; let transaction = DeploymentTransaction::from_continuity(&command, ¤t.value, now)?; + // The restart is counted in the same CAS that mints it, so a crash + // cannot schedule one the ceiling never saw. + let mut counted = supervision; + counted.record_restart(now); ensure!( SingleFileMessagePackBackingStore::new(&self.options.state_store) .compare_exchange( @@ -5132,47 +5624,31 @@ impl Engine { key: transaction.transaction_id.clone(), current: None, }, + CultCacheExpectedEnvelope { + r#type: TargetSupervision::TYPE.into(), + key: counted.target.clone(), + current: snapshot + .supervision_for(&counted.target) + .map(|stored| stored.envelope.clone()), + }, ], &[ command_envelope(&command, now)?, transaction_envelope(&transaction, now)?, + target_supervision_envelope(&counted, now)?, ], )?, - "continuity scheduling lost its command/transaction CAS" + "continuity scheduling lost its command/transaction/meter CAS" ); progressed = true; } Ok(progressed) } - /// Record that continuity may not restart this generation before - /// `now + CONTINUITY_DEFERRAL_MILLIS`. The generation's `ContinuityBackoff` - /// owns "not before": only its `next_restart_at_unix_millis` is written, - /// so the attempts and window that count real restarts are untouched. - fn defer_continuity_restart(&self, current: &Stored, now: u64) -> Result<()> { - let mut next = current.value.clone(); - next.continuity_backoff.next_restart_at_unix_millis = - Some(now + CONTINUITY_DEFERRAL_MILLIS); - next.validate()?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: AdmittedGeneration::TYPE.into(), - key: current.value.target.clone(), - current: Some(current.envelope.clone()), - }], - &[admitted_envelope(&next, now)?], - )?, - "admitted generation changed before continuity deferral CAS" - ); - Ok(()) - } - fn supervise_admitted_route(&self, current: &Stored) -> Result { let Some(expected_route) = current.value.expected.route.as_ref() else { ensure!( - matches!(¤t.value.routing, RoutingEvidence::SkippedUnrouted) - && current.value.route_repair_started_at_unix_millis.is_none(), + matches!(¤t.value.routing, RoutingEvidence::SkippedUnrouted), "unrouted admitted generation carries route authority" ); return Ok(false); @@ -5184,6 +5660,17 @@ impl Engine { else { bail!("routed admitted generation has no promoted route receipt") }; + let now = now_millis()?; + // A route whose proofs keep failing is challenged on a widening + // schedule, not every tick. + if current + .value + .route_supervision + .as_ref() + .is_some_and(|state| state.is_waiting(now, self.options.topology_maximum_age_millis)) + { + return Ok(false); + } ensure!( observation.route_id == expected_route.route_id && observation.runtime_instance_id == current.value.activation.runtime_instance_id, @@ -5208,9 +5695,9 @@ impl Engine { let route_binding = binding .route .context("routed admitted generation has no operator route binding")?; - let driver = NginxRouteDriver::new(route_binding); + let driver = self.route_driver(route_binding); + let gate = self.route_gate(¤t.value.target); - let now = now_millis()?; if driver.observe_membership(¤t.value.expected, &observation.membership_sha256)? && route_observation_is_current( observation.observed_at_unix_millis, @@ -5221,51 +5708,79 @@ impl Engine { { return Ok(false); } - // Actuates only when the fragment on disk differs from the admitted - // membership; an exact fragment makes this a no-op. - driver - .restore_admitted_membership(¤t.value.expected, &observation.membership_sha256)?; - let authority = self.runtime_authority_parts( - ¤t.value.plan, - ¤t.value.expected, - ¤t.value.activation, - )?; - let refreshed = self.prove_stable_route_against( - ¤t.value.expected, - ¤t.value.activation, - &authority, - current.value.leasing.lease_sha256(), - &driver, - observation.membership_sha256.clone(), - )?; - ensure!( - driver.observe_membership(¤t.value.expected, &refreshed.membership_sha256)?, - "admitted route membership changed during its continuity challenge" - ); - if let (Some(lease_driver), Some(lease)) = (&lease_driver, current.value.leasing.lease()) { + let route_key = format!("route:{}", current.value.target); + // A failed repair or a failed proof changes observation state only: it + // marks the route degraded and widens the wait. It is a write, so it + // ends this target's supervision pass (`Ok(true)`), exactly as a + // proved challenge does: the caller's snapshot no longer matches the + // generation, and a later step in the same pass would lose its CAS. + let challenge = (|| -> Result { + // Actuates only when the fragment on disk differs from the + // admitted membership; an exact fragment makes this a no-op. It is + // survival: restoring the admitted route is never refused. + driver.restore_admitted_membership( + ¤t.value.expected, + &observation.membership_sha256, + &gate, + )?; + let authority = self.runtime_authority_parts( + ¤t.value.plan, + ¤t.value.expected, + ¤t.value.activation, + )?; + let refreshed = self.prove_stable_route_against( + ¤t.value.expected, + ¤t.value.activation, + &authority, + current.value.leasing.lease_sha256(), + &driver, + observation.membership_sha256.clone(), + )?; ensure!( - lease_driver.observe_exact(lease)?, - "admitted process write lease changed during its continuity challenge" + driver.observe_membership(¤t.value.expected, &refreshed.membership_sha256)?, + "admitted route membership changed during its continuity challenge" ); - } + if let (Some(lease_driver), Some(lease)) = (&lease_driver, current.value.leasing.lease()) + { + ensure!( + lease_driver.observe_exact(lease)?, + "admitted process write lease changed during its continuity challenge" + ); + } + Ok(refreshed) + })(); let mut next = current.value.clone(); - next.routing = RoutingEvidence::Promoted { - observation: refreshed, - promoted_at_unix_millis: *promoted_at_unix_millis, - }; - next.route_repair_started_at_unix_millis = None; - next.validate()?; - ensure!( - SingleFileMessagePackBackingStore::new(&self.options.state_store).compare_exchange( - &[CultCacheExpectedEnvelope { - r#type: AdmittedGeneration::TYPE.into(), - key: current.value.target.clone(), - current: Some(current.envelope.clone()), - }], - &[admitted_envelope(&next, now)?], - )?, - "admitted generation changed before route continuity receipt CAS" - ); + match challenge { + Ok(refreshed) => { + next.routing = RoutingEvidence::Promoted { + observation: refreshed, + promoted_at_unix_millis: *promoted_at_unix_millis, + }; + if let Some(state) = next.route_supervision.as_mut() { + state.record_proved_challenge(now); + } + self.clear_fault(&route_key); + self.replace_generation(current, &next, now)?; + } + Err(error) => { + if let Some(state) = next.route_supervision.as_mut() { + state.record_failed_challenge(now, self.options.topology_maximum_age_millis); + } + self.report_once( + &route_key, + format!( + "Idunn could not prove admitted {} on its stable route: {error:#}", + current.value.target + ), + ); + if let Err(record) = self.replace_generation(current, &next, now) { + eprintln!( + "Idunn could not record the failed route challenge of {}: {record:#}", + current.value.target + ); + } + } + } Ok(true) } @@ -5698,11 +6213,12 @@ impl Engine { let route_binding = binding .route .context("routed Expected lost route binding")?; - let driver = NginxRouteDriver::new(route_binding); + let driver = self.route_driver(route_binding); let receipt = driver.preflight( expected, &activation.runtime_instance_id, incumbent_route.as_ref(), + &self.route_gate(¤t.value.target), )?; return self.persist_same_phase(current, |next| { next.route_preflight = Some(receipt); @@ -6148,7 +6664,8 @@ impl Engine { let binding = admitted.value.plan.as_ref().unwrap().parsed_inputs()?.1; let route_binding = binding.route.context("routed plan has no route binding")?; let preflight = required(&admitted.value.route_preflight, "route preflight")?; - let driver = NginxRouteDriver::new(route_binding); + let driver = self.route_driver(route_binding); + let gate = self.route_gate(&admitted.value.target); ensure!( preflight.candidate_runtime_instance_id == activation.runtime_instance_id, "route preflight belongs to another runtime instance" @@ -6162,6 +6679,7 @@ impl Engine { &activation.runtime_instance_id, preflight, rollback_allowed, + &gate, )?; let observation = match self.prove_stable_route( &admitted.value, @@ -6179,6 +6697,7 @@ impl Engine { expected, &activation.runtime_instance_id, preflight, + &gate, ) { Ok(()) => Err(proof_error) .context("candidate did not answer its stable route challenge"), @@ -6277,7 +6796,7 @@ impl Engine { .parsed_inputs()? .1; let driver = - NginxRouteDriver::new(binding.route.context("routed plan has no route binding")?); + self.route_driver(binding.route.context("routed plan has no route binding")?); ensure!( driver.observe_membership(expected, &route.membership_sha256)?, "route membership changed before admission commit" @@ -6346,7 +6865,6 @@ impl Engine { let generation = AdmittedGeneration::from_transaction( &commit_current.value, odin_authority, - incumbent.map(|stored| &stored.value), now, )?; let post_commit_cleanup = PostCommitCleanup { @@ -6982,7 +7500,7 @@ impl Engine { let binding = required(&transaction.plan, "first Odin plan")? .parsed_inputs()? .1; - let driver = NginxRouteDriver::new( + let driver = self.route_driver( binding .route .context("first Odin bootstrap has no candidate route binding")?, @@ -7320,7 +7838,7 @@ impl Engine { let binding = required(&transaction.plan, "candidate plan")? .parsed_inputs()? .1; - let driver = NginxRouteDriver::new( + let driver = self.route_driver( binding .route .context("route-proof candidate has no route binding")?, @@ -7649,9 +8167,9 @@ impl Engine { let preflight = required(¤t.value.route_preflight, "route preflight receipt")?; let binding = current.value.plan.as_ref().unwrap().parsed_inputs()?.1; let driver = - NginxRouteDriver::new(binding.route.context("routed plan has no route binding")?); + self.route_driver(binding.route.context("routed plan has no route binding")?); driver - .withdraw_candidate_membership(preflight) + .withdraw_candidate_membership(preflight, &self.route_gate(¤t.value.target)) .context("restoring the route the candidate found")?; return self.persist_same_phase(current, |next| { next.post_fencing_abort.as_mut().unwrap().route_restoration = diff --git a/src/drivers.rs b/src/drivers.rs index 6af3559..3a6d8d4 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -5055,15 +5055,81 @@ pub struct NginxRouteDriver { pub preflight_root: PathBuf, } +/// The host programs and scratch directory the route driver actuates through. +/// The daemon owns one value and every driver is built from it, so the paths +/// are chosen in one place and a test can point them at stubs. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RouteActuators { + pub nginx: PathBuf, + pub systemd_run: PathBuf, + pub systemctl: PathBuf, + pub ufw: PathBuf, + pub preflight_root: PathBuf, +} + +impl Default for RouteActuators { + fn default() -> Self { + Self { + nginx: PathBuf::from("/usr/sbin/nginx"), + systemd_run: PathBuf::from("/usr/bin/systemd-run"), + systemctl: PathBuf::from("/usr/bin/systemctl"), + ufw: PathBuf::from("/usr/sbin/ufw"), + preflight_root: PathBuf::from("/run/idunn/route-preflight"), + } + } +} + +/// Why a route actuation is being asked for. Only a `Forward` change can be +/// refused: restoring the admitted or incumbent route is survival, not +/// deployment, so a `Survival` is always admitted and always counted. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RouteActuation { + Forward, + Survival, +} + +/// A forward route actuation the ceiling refused. `reopens_at_unix_millis` is +/// when the oldest counted actuation leaves the window. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct RouteActuationRefused { + pub target: String, + pub used: usize, + pub reopens_at_unix_millis: u64, +} + +impl std::fmt::Display for RouteActuationRefused { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + formatter, + "route actuation ceiling reached for {}: {} in the window, reopens at unix ms {}", + self.target, self.used, self.reopens_at_unix_millis + ) + } +} + +impl std::error::Error for RouteActuationRefused {} + +/// The one rate authority over route actuation. The driver asks it before +/// every group of host mutations (fragment write, firewall, `nginx -t`, +/// reload, private-mount validation) and does none of them when it refuses. +/// Its owner keeps the count durably, so the ceiling survives a restart. +pub trait RouteActuationGate { + fn admit(&self, kind: RouteActuation) -> Result<()>; +} + impl NginxRouteDriver { pub fn new(binding: RouteBinding) -> Self { + Self::with_actuators(binding, &RouteActuators::default()) + } + + pub fn with_actuators(binding: RouteBinding, actuators: &RouteActuators) -> Self { Self { binding, - nginx_program: PathBuf::from("/usr/sbin/nginx"), - systemd_run_program: PathBuf::from("/usr/bin/systemd-run"), - systemctl_program: PathBuf::from("/usr/bin/systemctl"), - ufw_program: PathBuf::from("/usr/sbin/ufw"), - preflight_root: PathBuf::from("/run/idunn/route-preflight"), + nginx_program: actuators.nginx.clone(), + systemd_run_program: actuators.systemd_run.clone(), + systemctl_program: actuators.systemctl.clone(), + ufw_program: actuators.ufw.clone(), + preflight_root: actuators.preflight_root.clone(), } } @@ -5265,7 +5331,8 @@ impl NginxRouteDriver { } } - fn restore(&self, prior: Option<&[u8]>) -> Result<()> { + fn restore(&self, prior: Option<&[u8]>, gate: &dyn RouteActuationGate) -> Result<()> { + gate.admit(RouteActuation::Survival)?; self.write_fragment(prior)?; self.reload()?; // No prior membership means the stable endpoint no longer routes to @@ -5281,8 +5348,9 @@ impl NginxRouteDriver { prior: Option<&[u8]>, failure: anyhow::Error, context: &str, + gate: &dyn RouteActuationGate, ) -> Result { - match self.restore(prior) { + match self.restore(prior, gate) { Ok(()) => Err(failure).context(context.to_owned()), Err(rollback) => Err(failure).context(format!( "{context}; route rollback also failed: {rollback:#}" @@ -5295,6 +5363,7 @@ impl NginxRouteDriver { expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, incumbent: Option<&RouteObservation>, + gate: &dyn RouteActuationGate, ) -> Result { let rendered = self.render(expected)?; ensure!(!rendered.is_empty(), "candidate route rendered empty"); @@ -5312,6 +5381,7 @@ impl NginxRouteDriver { "route preflight found an unadmitted incumbent" ), } + gate.admit(RouteActuation::Forward)?; self.validate_candidate_in_private_mount(&rendered)?; let after = self.current_configuration()?; ensure!( @@ -5341,6 +5411,7 @@ impl NginxRouteDriver { runtime_instance_id: &str, preflight: &RoutePreflightReceipt, rollback_allowed: bool, + gate: &dyn RouteActuationGate, ) -> Result { preflight.validate()?; let rendered = self.render(expected)?; @@ -5359,6 +5430,7 @@ impl NginxRouteDriver { && prior_sha256 == preflight.incumbent_membership_sha256), "route baseline changed after preflight" ); + gate.admit(RouteActuation::Forward)?; if !candidate_already_written { atomic_replace(&self.binding.config_path, &rendered)?; } @@ -5368,6 +5440,7 @@ impl NginxRouteDriver { preflight.incumbent_configuration.as_deref(), error, "candidate route validation or reload failed", + gate, ); } return Err(error).context( @@ -5406,13 +5479,17 @@ impl NginxRouteDriver { /// candidate's membership was installed, so restoring it is precise whether /// there was an incumbent (its bytes) or none (removal). Used when a /// transaction is abandoned after the fence. - pub fn withdraw_candidate_membership(&self, preflight: &RoutePreflightReceipt) -> Result<()> { + pub fn withdraw_candidate_membership( + &self, + preflight: &RoutePreflightReceipt, + gate: &dyn RouteActuationGate, + ) -> Result<()> { preflight.validate()?; ensure!( preflight.route_id == self.binding.route_id, "route preflight receipt describes another route" ); - self.restore(preflight.incumbent_configuration.as_deref()) + self.restore(preflight.incumbent_configuration.as_deref(), gate) } /// A fragment already equal to the admitted membership is left alone: no @@ -5421,6 +5498,7 @@ impl NginxRouteDriver { &self, expected: &IdunnExpectedIncarnationRecord, membership_sha256: &str, + gate: &dyn RouteActuationGate, ) -> Result<()> { let rendered = self.render(expected)?; ensure!( @@ -5430,6 +5508,7 @@ impl NginxRouteDriver { if self.current_configuration()?.as_deref() == Some(rendered.as_slice()) { return Ok(()); } + gate.admit(RouteActuation::Survival)?; self.validate_candidate_in_private_mount(&rendered)?; atomic_replace(&self.binding.config_path, &rendered)?; if let Err(reload) = self.admit_endpoint().and_then(|()| self.reload()) { @@ -5557,6 +5636,7 @@ impl NginxRouteDriver { expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, preflight: &RoutePreflightReceipt, + gate: &dyn RouteActuationGate, ) -> Result<()> { preflight.validate()?; let rendered = self.render(expected)?; @@ -5570,7 +5650,7 @@ impl NginxRouteDriver { self.current_configuration()?.as_deref() == Some(rendered.as_slice()), "candidate route changed before rollback" ); - self.restore(preflight.incumbent_configuration.as_deref()) + self.restore(preflight.incumbent_configuration.as_deref(), gate) } } @@ -8248,6 +8328,15 @@ fn apply_identity(command: &mut Command, identity: Option) -> R mod tests { use super::*; + /// A gate that never refuses, for driver tests that are not about the ceiling. + struct Unmetered; + + impl RouteActuationGate for Unmetered { + fn admit(&self, _kind: RouteActuation) -> Result<()> { + Ok(()) + } + } + // --------------------------------------------------------------------- // A direct challenge fails one of two ways. Silence waits; a bad answer, // or a challenge that cannot be made, is an error. @@ -11530,13 +11619,160 @@ Content-Le".to_vec()), |_| {}), "allow in to 10.77.0.1 port 17971 proto udp" ); - driver.restore(None)?; + driver.restore(None, &Unmetered)?; assert_eq!( fs::read_to_string(ufw.with_extension("calls"))?, "delete allow in to 10.77.0.1 port 17971 proto udp\n" ); fs::write(ufw.with_extension("absent"), b"gone\n")?; - driver.restore(None)?; + driver.restore(None, &Unmetered)?; + Ok(()) + } + + /// Refuses every `Forward` actuation, admits every `Survival`, and records + /// what it was asked. + struct ForwardRefusing(std::cell::RefCell>); + + impl RouteActuationGate for ForwardRefusing { + fn admit(&self, kind: RouteActuation) -> Result<()> { + self.0.borrow_mut().push(kind); + match kind { + RouteActuation::Forward => Err(RouteActuationRefused { + target: "service".into(), + used: 12, + reopens_at_unix_millis: 99, + } + .into()), + RouteActuation::Survival => Ok(()), + } + } + } + + /// Stub host programs that log every call, a driver over them, and a + /// candidate whose membership it can render. + #[cfg(unix)] + fn stubbed_route_driver( + temp: &Path, + ) -> Result<(NginxRouteDriver, IdunnExpectedIncarnationRecord, PathBuf)> { + use std::os::unix::fs::PermissionsExt; + + let calls = temp.join("calls"); + let program = |name: &str| -> Result { + let path = temp.join(name); + fs::write( + &path, + format!("#!/bin/sh\necho \"{name} $*\" >> '{}'\nexit 0\n", calls.display()), + )?; + fs::set_permissions(&path, fs::Permissions::from_mode(0o755))?; + Ok(path) + }; + let driver = NginxRouteDriver::with_actuators( + RouteBinding { + driver: RouteDriver::NginxStreamTcp, + route_id: "service-route".into(), + stable_endpoint: "http://127.0.0.1:4103".into(), + private_host: "127.0.0.1".into(), + private_port_start: 4104, + private_port_end: 4109, + config_path: temp.join("service.conf"), + reload_unit: "nginx.service".into(), + }, + &RouteActuators { + nginx: program("nginx")?, + systemd_run: program("systemd-run")?, + systemctl: program("systemctl")?, + ufw: program("ufw")?, + preflight_root: temp.join("preflight"), + }, + ); + let mut candidate = expected(); + candidate.route = Some(cultnet_rs::IdunnExpectedRoute { + route_id: "service-route".into(), + transport: "http".into(), + stable_endpoint: "http://127.0.0.1:4103".into(), + candidate_endpoint: "http://127.0.0.1:4104".into(), + }); + Ok((driver, candidate, calls)) + } + + #[cfg(unix)] + fn preflight_receipt( + driver: &NginxRouteDriver, + candidate: &IdunnExpectedIncarnationRecord, + ) -> Result { + Ok(RoutePreflightReceipt { + route_id: "service-route".into(), + candidate_runtime_instance_id: digest('a'), + candidate_membership_sha256: sha256_id(&driver.render(candidate)?), + incumbent_runtime_instance_id: None, + incumbent_membership_sha256: None, + incumbent_configuration: None, + }) + } + + /// A refused forward change stops before any host mutation: no program + /// runs, no fragment is written, and no private-mount unit starts. + #[cfg(unix)] + #[test] + fn a_refused_forward_actuation_runs_no_program_and_writes_nothing() -> Result<()> { + let temp = tempfile::tempdir()?; + let (driver, candidate, calls) = stubbed_route_driver(temp.path())?; + let gate = ForwardRefusing(Default::default()); + let receipt = preflight_receipt(&driver, &candidate)?; + + let refused = driver + .preflight(&candidate, &digest('a'), None, &gate) + .unwrap_err(); + assert!(refused.downcast_ref::().is_some()); + let refused = driver + .install(&candidate, &digest('a'), &receipt, true, &gate) + .unwrap_err(); + assert!(refused.downcast_ref::().is_some()); + + assert!(!calls.exists(), "a refused actuation must run no program"); + assert!(!driver.binding.config_path.exists()); + assert_eq!( + *gate.0.borrow(), + [RouteActuation::Forward, RouteActuation::Forward] + ); + Ok(()) + } + + /// Restoring the admitted or incumbent route is never refused, whatever + /// the gate says about forward changes, and is always asked as survival. + #[cfg(unix)] + #[test] + fn survival_actuations_run_under_a_gate_that_refuses_forward_changes() -> Result<()> { + let temp = tempfile::tempdir()?; + let (driver, candidate, calls) = stubbed_route_driver(temp.path())?; + let gate = ForwardRefusing(Default::default()); + let receipt = preflight_receipt(&driver, &candidate)?; + let rendered = driver.render(&candidate)?; + let membership = sha256_id(&rendered); + let reloads = || -> Result { + Ok(fs::read_to_string(&calls)? + .lines() + .filter(|line| line.starts_with("systemctl reload")) + .count()) + }; + + fs::write(&driver.binding.config_path, b"drifted bytes\n")?; + driver.restore_admitted_membership(&candidate, &membership, &gate)?; + assert_eq!(fs::read(&driver.binding.config_path)?, rendered); + assert_eq!(reloads()?, 1); + + driver.withdraw_candidate_membership(&receipt, &gate)?; + assert!(!driver.binding.config_path.exists()); + assert_eq!(reloads()?, 2); + + fs::write(&driver.binding.config_path, &rendered)?; + driver.rollback(&candidate, &digest('a'), &receipt, &gate)?; + assert_eq!(reloads()?, 3); + + assert_eq!( + *gate.0.borrow(), + [RouteActuation::Survival, RouteActuation::Survival, RouteActuation::Survival] + ); Ok(()) } @@ -11596,22 +11832,22 @@ Content-Le".to_vec()), |_| {}), fs::write(&config, &rendered)?; let modified = fs::metadata(&config)?.modified()?; - driver.restore_admitted_membership(&candidate, &membership_sha256)?; - driver.restore_admitted_membership(&candidate, &membership_sha256)?; + driver.restore_admitted_membership(&candidate, &membership_sha256, &Unmetered)?; + driver.restore_admitted_membership(&candidate, &membership_sha256, &Unmetered)?; assert!(!calls.exists(), "an exact fragment must run no program"); assert!(!driver.preflight_root.exists()); assert_eq!(fs::read(&config)?, rendered); assert_eq!(fs::metadata(&config)?.modified()?, modified); fs::write(&config, b"drifted bytes\n")?; - driver.restore_admitted_membership(&candidate, &membership_sha256)?; + driver.restore_admitted_membership(&candidate, &membership_sha256, &Unmetered)?; assert_eq!(fs::read(&config)?, rendered); assert_eq!(count("systemctl reload")?, 1); assert_eq!(count("systemd-run")?, 1); assert_eq!(count("ufw allow")?, 1); assert_eq!(count("nginx -t")?, 1); - driver.restore_admitted_membership(&candidate, &membership_sha256)?; + driver.restore_admitted_membership(&candidate, &membership_sha256, &Unmetered)?; assert_eq!(count("systemctl reload")?, 1, "restored bytes never reload again"); assert_eq!(count("ufw")?, 1); Ok(()) @@ -11733,13 +11969,13 @@ Content-Le".to_vec()), |_| {}), stable_endpoint: "http://127.0.0.1:4103".into(), candidate_endpoint: "http://127.0.0.1:4104".into(), }); - let preflight = driver.preflight(&candidate, &digest('a'), None)?; + let preflight = driver.preflight(&candidate, &digest('a'), None, &Unmetered)?; assert!(!config.exists()); assert_eq!(fs::read_dir(&driver.preflight_root)?.count(), 0); let candidate_bytes = driver.render(&candidate)?; fs::write(&config, &candidate_bytes)?; - let membership_sha256 = driver.install(&candidate, &digest('a'), &preflight, true)?; + let membership_sha256 = driver.install(&candidate, &digest('a'), &preflight, true, &Unmetered)?; assert_eq!( fs::read_to_string(systemctl.with_extension("calls"))?, "reload\n" @@ -11761,24 +11997,24 @@ Content-Le".to_vec()), |_| {}), observed_at_unix_millis: 1, }; - let next_preflight = driver.preflight(&candidate, &digest('b'), Some(&admitted))?; + let next_preflight = driver.preflight(&candidate, &digest('b'), Some(&admitted), &Unmetered)?; assert_eq!(fs::read(&config)?, admitted_bytes); fs::write(systemctl.with_extension("fail"), b"fail\n")?; assert!( driver - .install(&candidate, &digest('b'), &next_preflight, false) + .install(&candidate, &digest('b'), &next_preflight, false, &Unmetered) .is_err() ); assert_eq!(fs::read(&config)?, admitted_bytes); fs::remove_file(systemctl.with_extension("fail"))?; - driver.install(&candidate, &digest('b'), &next_preflight, false)?; + driver.install(&candidate, &digest('b'), &next_preflight, false, &Unmetered)?; fs::write(&config, b"foreign route\n")?; assert!( driver - .install(&candidate, &digest('b'), &next_preflight, true) + .install(&candidate, &digest('b'), &next_preflight, true, &Unmetered) .is_err() ); - driver.restore_admitted_membership(&candidate, &admitted.membership_sha256)?; + driver.restore_admitted_membership(&candidate, &admitted.membership_sha256, &Unmetered)?; assert_eq!(fs::read(&config)?, admitted_bytes); assert!(driver.observe_membership(&candidate, &admitted.membership_sha256)?); @@ -11786,12 +12022,12 @@ Content-Le".to_vec()), |_| {}), fs::write(systemctl.with_extension("fail"), b"fail\n")?; assert!( driver - .restore_admitted_membership(&candidate, &admitted.membership_sha256) + .restore_admitted_membership(&candidate, &admitted.membership_sha256, &Unmetered) .is_err() ); assert!(!config.exists()); fs::remove_file(systemctl.with_extension("fail"))?; - driver.restore_admitted_membership(&candidate, &admitted.membership_sha256)?; + driver.restore_admitted_membership(&candidate, &admitted.membership_sha256, &Unmetered)?; assert_eq!(fs::read(&config)?, admitted_bytes); Ok(()) }