diff --git a/docs/host-actuator.md b/docs/host-actuator.md index 499ab23..f2fb1c7 100644 --- a/docs/host-actuator.md +++ b/docs/host-actuator.md @@ -82,8 +82,8 @@ running a script, and a dead `muninn serve` is seen the tick it dies. 2. No path on yggdrasil can start, stop or observe a host workload except through the actuator session bound to that host's anchor. 3. A host with no attached actuator is a failed observation, which - continuity counts and gives up on after three attempts, exactly like a - unit that will not start. + continuity counts and gives up on after six restarts inside one hour, + exactly like a unit that will not start. 4. The actuator cannot be told to run a program the recipe did not declare; `allowed_programs` on the host-native runner is enforced on the host. diff --git a/scripts/verify.sh b/scripts/verify.sh index 365694e..d0bb8a4 100755 --- a/scripts/verify.sh +++ b/scripts/verify.sh @@ -3,6 +3,8 @@ # (the Eureka verify image has both); a Windows workstation proves nothing. # The Windows check runs first: it is fast, and a hung test cannot mask it. # --lib because the tests under tests/ assume the host layout. +# The Windows --lib --tests check keeps unix-only test code from leaking out of its cfg. set -euo pipefail cargo check --locked --target x86_64-pc-windows-gnu --bin idunn-host +cargo check --locked --target x86_64-pc-windows-gnu --lib --tests cargo test --locked --lib diff --git a/src/control_plane.rs b/src/control_plane.rs index f724834..da57968 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,218 @@ 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) { + clamp_to(log, now); + log.push(now); + if log.len() > limit { + log.drain(..log.len() - limit); + } +} + +/// Make a stepped-back clock's future entries the present. `live_entries` +/// only reads them that way, so a log that is never written would keep +/// looking recent until the clock caught up; writing the clamp is what makes +/// a backwards step cost one window and no more. +fn clamp_to(log: &mut [u64], now: u64) { + for at in log.iter_mut() { + *at = (*at).min(now); + } +} + +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, + } + } + + /// Clamp both logs to `now`. Callers that decide from a log write the + /// result back when it changed. + fn settle(&mut self, now: u64) { + clamp_to(&mut self.route_actuations, now); + clamp_to(&mut self.continuity_restarts, now); + } + + 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 counted whenever the ledger can record it. + fn charge_route(&mut self, now: u64, kind: RouteActuation) -> Result<(), RouteActuationRefused> { + self.settle(now); + 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); + if live.is_empty() { + return None; + } + let last = *self.continuity_restarts.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(()) } @@ -1957,7 +2139,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)] @@ -2000,14 +2182,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 @@ -2044,7 +2228,6 @@ impl AdmittedGeneration { fn from_transaction( transaction: &DeploymentTransaction, odin_authority: Option, - incumbent: Option<&AdmittedGeneration>, now: u64, ) -> Result { ensure!( @@ -2063,11 +2246,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(), @@ -2088,11 +2268,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) @@ -2157,13 +2334,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 { .. }), @@ -2172,7 +2342,6 @@ impl AdmittedGeneration { if let Some(state) = &self.route_supervision { state.validate()?; } - self.continuity_backoff.validate()?; Ok(()) } } @@ -2196,6 +2365,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 { @@ -2221,6 +2393,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(), } } } @@ -2586,6 +2759,7 @@ struct ControlSnapshot { commands: Vec>, transactions: Vec>, admitted: Vec>, + targets: Vec>, } impl ControlSnapshot { @@ -2628,6 +2802,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"), } } @@ -2663,6 +2850,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 @@ -2745,12 +2941,51 @@ impl ControlSnapshot { Ok(()) } + /// Every stored record that concerns `target`, as read: the generation, the + /// meters and its transactions. Two reads with equal stamps saw no write + /// for the target between them. + fn target_stamp(&self, target: &str) -> Vec { + let mut stamp = self + .admitted + .iter() + .filter(|stored| stored.value.target == target) + .map(|stored| stored.envelope.clone()) + .chain( + self.targets + .iter() + .filter(|stored| stored.value.target == target) + .map(|stored| stored.envelope.clone()), + ) + .chain( + self.transactions + .iter() + .filter(|stored| stored.value.target == target) + .map(|stored| stored.envelope.clone()), + ) + .collect::>(); + stamp.sort_by(|a, b| (&a.r#type, &a.key).cmp(&(&b.r#type, &b.key))); + stamp + } + fn admitted_for(&self, target: &str) -> Option<&Stored> { self.admitted .iter() .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". @@ -3037,7 +3272,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); @@ -3062,13 +3298,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 { @@ -3118,8 +3474,8 @@ fn read_transaction_record(envelope: &CultCacheEnvelope) -> Result Result { match envelope.schema_id.as_deref() { Some(ADMITTED_GENERATION_SCHEMA) => { @@ -3127,6 +3483,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")?; @@ -3142,7 +3509,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; @@ -3226,6 +3596,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, @@ -3368,9 +3749,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`) @@ -3772,9 +4155,88 @@ 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()?; + for target in supervised_targets(&snapshot) { + 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(()) } +/// Every target supervision holds something for: the admitted generations +/// and the metered targets. +fn supervised_targets(snapshot: &ControlSnapshot) -> BTreeSet<&str> { + snapshot + .admitted + .iter() + .map(|stored| stored.value.target.as_str()) + .chain(snapshot.targets.iter().map(|stored| stored.value.target.as_str())) + .collect() +} + +/// 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()); @@ -3871,6 +4333,13 @@ impl Drop for ProcessLock { } } +/// One target's in-memory challenge wait: when it is due, and the stored +/// records of the target as they were when the record failed to land. +struct ChallengeWait { + due_unix_millis: u64, + stamp: Vec, +} + struct Engine { options: RuntimeOptions, idunn_signer: ServiceIdentitySigner, @@ -3889,6 +4358,35 @@ struct Engine { /// Process-local on purpose: a restart retries at once, and no schema /// carries a clock that only pacing reads. resume_backoff: Mutex>, + /// When each routed target's next challenge is due, held only when the + /// stored `RouteSupervisionState` could not record the last one. The stored + /// value owns the schedule and survives restarts; this spaces attempts + /// while the record did not land. An entry ends the moment any write for + /// its target lands (its stamp no longer matches the store) or the target + /// is retired or withdrawn: it never outlives the episode that made it. + challenge_wait: Mutex>, +} + +/// The route driver's door to the actuation ceiling: every group of host +/// mutations a driver makes for `target` is counted through here. The gate +/// owns which charge a request is: a driver asks for the change it is making, +/// and the command that owns the transaction decides whether that change is +/// deployment (`Forward`, refusable) or the continuity of the admitted +/// generation (`Survival`, never refused). +struct EngineRouteGate<'a> { + engine: &'a Engine, + target: &'a str, + command: CommandKind, +} + +impl RouteActuationGate for EngineRouteGate<'_> { + fn admit(&self, requested: RouteActuation) -> Result<()> { + let kind = match self.command { + CommandKind::Deploy => requested, + CommandKind::Continuity => RouteActuation::Survival, + }; + self.engine.charge_route_actuation(self.target, kind) + } } #[derive(Clone, Copy)] @@ -3955,6 +4453,7 @@ impl Engine { history_report: ReportOnce::default(), fault_reports: Mutex::default(), resume_backoff: Mutex::default(), + challenge_wait: Mutex::default(), }) } @@ -3962,6 +4461,117 @@ 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, command: CommandKind) -> EngineRouteGate<'a> { + EngineRouteGate { + engine: self, + target, + command, + } + } + + /// 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`, and a `Forward` + /// that cannot be recorded is refused too: no charge, no actuation. A + /// `Survival` is never refused, so it is counted when it can be and runs + /// regardless when it cannot: an unwritable store must not stop a rollback, + /// a withdrawal or a repair. + fn charge_route_actuation(&self, target: &str, kind: RouteActuation) -> Result<()> { + match self.record_route_actuation(target, kind) { + Ok(charged) => charged.map_err(anyhow::Error::new), + Err(error) if kind == RouteActuation::Survival => { + eprintln!( + "Idunn could not count a survival route actuation of {target}; it proceeds uncounted: {error:#}" + ); + Ok(()) + } + Err(error) => Err(error), + } + } + + /// The outer error is the ledger failing to read or write; the inner one + /// is the ceiling refusing. + fn record_route_actuation( + &self, + target: &str, + kind: RouteActuation, + ) -> Result> { + let snapshot = ControlSnapshot::read(&self.options.state_store)?; + let seen = snapshot.supervision_for(target); + let mut next = snapshot.supervision_or_new(target); + let now = now_millis()?; + let charged = next.charge_route(now, kind); + // A refusal writes nothing unless the clock had stepped back and the + // log needed settling. + if charged.is_ok() || seen.is_some_and(|stored| stored.value != next) { + self.write_target_supervision(seen, &next, now)?; + } + Ok(charged) + } + + /// 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. `Ok(false)` is a lost race: another writer moved the + /// record, so the caller's snapshot is stale. `Err` is a write the store + /// refused, which changed nothing. + fn replace_generation( + &self, + seen: &Stored, + next: &AdmittedGeneration, + now: u64, + ) -> Result { + Ok(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)?], + )?) + } + + /// 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 @@ -4571,7 +5181,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); @@ -4807,6 +5419,13 @@ impl Engine { fn supervise_one_admitted_generation(&self) -> Result { let snapshot = ControlSnapshot::read(&self.options.state_store)?; + // A wait belongs to the failed record it spaces. Once any write for its + // target has landed, or the target is no longer admitted, the stored + // schedule owns the target again. + self.challenge_wait + .lock() + .expect("challenge wait mutex") + .retain(|target, wait| snapshot.target_stamp(target) == wait.stamp); let mut progressed = false; let mut admitted = snapshot.admitted.iter().collect::>(); admitted.sort_by_key(|stored| stored.value.target.as_str()); @@ -4835,7 +5454,7 @@ impl Engine { }) { continue; } - match self.supervise_admitted_route(current) { + match self.supervise_admitted_route(&snapshot, current) { Ok(true) => { progressed = true; continue; @@ -4998,41 +5617,28 @@ 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 { + // 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); + // A stepped-back clock leaves entries in the future. Settle them + // before deciding from them; the write ends this target's pass. + let mut settled = supervision.clone(); + settled.settle(now); + if settled != supervision { + self.write_target_supervision( + snapshot.supervision_for(¤t.value.target), + &settled, + now, + )?; + progressed = true; continue; - }; - let refused_restarts = snapshot - .transactions - .iter() - .filter(|stored| is_refused_restart(&stored.value)) - .count() - + history - .iter() - .filter(|transaction| is_refused_restart(transaction)) - .count(); + } + let continuity_key = format!("continuity:{}", current.value.target); if let Some(blocker) = blocker { // Yielding a *deployment* to continuity is right: the incumbent @@ -5047,7 +5653,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. @@ -5063,13 +5669,24 @@ 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), + supervision.route_reopens_at(now).map_or_else(String::new, |at| format!( + ", but its route ceiling refuses the route change until it reopens at unix ms {at}" + )), + ), ); 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 @@ -5079,17 +5696,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)?; @@ -5115,7 +5721,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; } @@ -5139,6 +5751,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( @@ -5153,47 +5769,35 @@ 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 { + fn supervise_admitted_route( + &self, + snapshot: &ControlSnapshot, + 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); @@ -5205,6 +5809,32 @@ 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); + } + if self + .challenge_wait + .lock() + .expect("challenge wait mutex") + .get(¤t.value.target) + .is_some_and(|wait| { + is_waiting( + now, + wait.due_unix_millis, + ROUTE_CHALLENGE_BACKOFF_CAP_MILLIS.max(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, @@ -5229,9 +5859,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, CommandKind::Continuity); - let now = now_millis()?; if driver.observe_membership(¤t.value.expected, &observation.membership_sha256)? && route_observation_is_current( observation.observed_at_unix_millis, @@ -5242,52 +5872,123 @@ 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. That record is written + // like a proved one: when it lands the pass ends (`Ok(true)`), because + // the caller's snapshot no longer matches the generation; when the store + // refuses it the pass reports no progress and the in-memory wait below + // spaces the next attempt. + 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, + // The wait the record carries, also held in memory until a record lands. + let (recorded, wait_until) = 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); + ( + "proved", + now.saturating_add(self.options.topology_maximum_age_millis), + ) + } + 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 + ), + ); + let due = next + .route_supervision + .as_ref() + .and_then(|state| state.next_challenge_at_unix_millis) + .unwrap_or(now); + ("failed", due) + } }; - 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" - ); - Ok(true) + // Progress is a store that changed: our record landed, or another + // writer beat it (the snapshot is stale either way, so the pass ends). + // A write the store refused changed nothing: no progress, the scheduler + // sleeps if nothing else moved, and the in-memory wait spaces the next + // challenge. + let record_key = format!("route-record:{}", current.value.target); + match self.replace_generation(current, &next, now) { + Ok(_) => { + self.challenge_wait + .lock() + .expect("challenge wait mutex") + .remove(¤t.value.target); + self.clear_fault(&record_key); + Ok(true) + } + Err(record) => { + self.challenge_wait + .lock() + .expect("challenge wait mutex") + .insert( + current.value.target.clone(), + ChallengeWait { + due_unix_millis: wait_until, + stamp: snapshot.target_stamp(¤t.value.target), + }, + ); + self.report_once( + &record_key, + format!( + "Idunn could not record the {recorded} route challenge of {}: {}", + current.value.target, + // The root cause only: the error chain names a fresh temp file each + // attempt, which would defeat once-per-fault. + record.root_cause() + ), + ); + Ok(false) + } + } } fn refresh_admitted_topology( @@ -5719,11 +6420,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, current.value.command_kind), )?; return self.persist_same_phase(current, |next| { next.route_preflight = Some(receipt); @@ -6169,7 +6871,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, admitted.value.command_kind); ensure!( preflight.candidate_runtime_instance_id == activation.runtime_instance_id, "route preflight belongs to another runtime instance" @@ -6183,6 +6886,7 @@ impl Engine { &activation.runtime_instance_id, preflight, rollback_allowed, + &gate, )?; let observation = match self.prove_stable_route( &admitted.value, @@ -6200,6 +6904,7 @@ impl Engine { expected, &activation.runtime_instance_id, preflight, + &gate, ) { Ok(()) => Err(proof_error) .context("candidate did not answer its stable route challenge"), @@ -6298,7 +7003,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" @@ -6367,7 +7072,6 @@ impl Engine { let generation = AdmittedGeneration::from_transaction( &commit_current.value, odin_authority, - incumbent.map(|stored| &stored.value), now, )?; let post_commit_cleanup = PostCommitCleanup { @@ -7003,7 +7707,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")?, @@ -7341,7 +8045,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")?, @@ -7670,9 +8374,12 @@ 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, current.value.command_kind), + ) .context("restoring the route the candidate found")?; return self.persist_same_phase(current, |next| { next.post_fencing_abort.as_mut().unwrap().route_restoration = @@ -8459,6 +9166,70 @@ mod tests { state_store: PathBuf, odin_signer: ServiceIdentitySigner, root: PathBuf, + /// The host programs every route driver of this Engine actuates + /// through: logged stubs, so nothing touches the workstation's nginx. + #[cfg(unix)] + route_stubs: RouteStubs, + } + + /// Stub nginx, systemd-run, systemctl and ufw that append each call to one + /// log. `reload-fails` makes `systemctl reload` exit non-zero. + #[cfg(unix)] + struct RouteStubs { + calls: PathBuf, + reload_fails: PathBuf, + } + + #[cfg(unix)] + impl RouteStubs { + fn new(root: &Path) -> Result<(RouteActuators, Self)> { + use std::os::unix::fs::PermissionsExt; + + let dir = root.join("route-stubs"); + std::fs::create_dir_all(&dir)?; + let calls = dir.join("calls"); + let reload_fails = dir.join("reload-fails"); + let program = |name: &str, refuse: &str| -> Result { + let path = dir.join(name); + std::fs::write( + &path, + format!( + "#!/bin/sh\necho \"{name} $*\" >> '{}'\n{refuse}exit 0\n", + calls.display() + ), + )?; + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))?; + Ok(path) + }; + let actuators = RouteActuators { + nginx: program("nginx", "")?, + systemd_run: program("systemd-run", "")?, + systemctl: program( + "systemctl", + &format!( + "if [ \"$1\" = reload ] && [ -e '{}' ]; then exit 1; fi\n", + reload_fails.display() + ), + )?, + ufw: program("ufw", "")?, + preflight_root: root.join("route-preflight"), + }; + Ok((actuators, Self { calls, reload_fails })) + } + + fn refuse_reloads(&self) -> Result<()> { + std::fs::write(&self.reload_fails, b"x")?; + Ok(()) + } + + /// How many logged calls begin with `prefix`, e.g. `systemctl reload`. + fn count(&self, prefix: &str) -> usize { + std::fs::read_to_string(&self.calls) + .unwrap_or_default() + .lines() + .filter(|line| line.starts_with(prefix)) + .count() + } } impl EngineFixture { @@ -8487,6 +9258,8 @@ mod tests { export_service_identity_trust_anchor(&odin_signer, &odin_anchor)?; let state_store = root.join("control.cc"); + #[cfg(unix)] + let (route_actuators, route_stubs) = RouteStubs::new(root)?; let options = RuntimeOptions { state_store: state_store.clone(), bindings_dir: root.join("bindings"), @@ -8497,6 +9270,8 @@ mod tests { odin_trust_anchor: odin_anchor, idunn_identity_store: idunn, deployment_brake_operator_anchor: root.join("brake-anchor.cc"), + #[cfg(unix)] + route_actuators, ..RuntimeOptions::default() }; let engine = match workload { @@ -8509,6 +9284,8 @@ mod tests { engine, state_store, odin_signer, + #[cfg(unix)] + route_stubs, }) } } @@ -9604,6 +10381,7 @@ mod tests { }, ], admitted: Vec::new(), + targets: Vec::new(), }; assert_eq!(snapshot.max_odin_sequence("ghostlight", "odin-signer"), 7); @@ -9755,57 +10533,6 @@ mod tests { assert!(transaction.validate().is_err()); Ok(()) } - - #[test] - fn continuity_gives_up_on_a_release_that_will_not_start() -> Result<()> { - let command = DeploymentCommand { - schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), - command_id: "continuity-1".into(), - kind: CommandKind::Continuity, - selector: "odin".into(), - requested_by: "idunn-continuity".into(), - requested_at_unix_millis: 100, - }; - let failed_restart = |generation: &str, ordinal: u32| -> Result { - let mut transaction = - DeploymentTransaction::new(&command, "odin".into(), ordinal, None, 100)?; - transaction.incumbent_generation_id = Some(generation.into()); - transaction.completion = Some(TransactionCompletion::FailedBeforeFencing { - error: "systemd unit is not running".into(), - }); - Ok(transaction) - }; - - let refused = |transactions: &[DeploymentTransaction], generation: &str| -> usize { - transactions - .iter() - .filter(|value| { - value.target == "odin" - && value.command_kind == CommandKind::Continuity - && value.incumbent_generation_id.as_deref() == Some(generation) - && matches!( - value.completion, - Some(TransactionCompletion::FailedBeforeFencing { .. }) - | Some(TransactionCompletion::FailedAfterFencing { .. }) - ) - }) - .count() - }; - - let attempts = vec![ - failed_restart("generation-1", 0)?, - failed_restart("generation-1", 1)?, - failed_restart("generation-1", 2)?, - ]; - assert!(refused(&attempts, "generation-1") >= CONTINUITY_RESTART_ATTEMPTS); - - // A restart that succeeds admits a new generation, and the count is - // kept against the generation id, so the next release is not condemned - // by the failures of the one it replaced. - assert_eq!(refused(&attempts, "generation-2"), 0); - Ok(()) - } - #[test] fn candidate_cleanup_is_owed_when_an_activation_or_a_workload_exists() -> Result<()> { // (activation issued, workload observed, expected cleanup) @@ -10072,6 +10799,7 @@ mod tests { }, ], admitted: Vec::new(), + targets: Vec::new(), }; assert!(snapshot.validate_relations().is_err()); Ok(()) @@ -10096,6 +10824,7 @@ mod tests { }) .collect(), admitted: Vec::new(), + targets: Vec::new(), }; assert!(!snapshot.has_earlier_authority_sibling(&first)); assert!(snapshot.has_earlier_authority_sibling(&second)); @@ -10225,6 +10954,10 @@ mod tests { const FIXTURE_GENERATION_REPAIRING: &str = include_str!( "../tests/fixtures/idunn-control-legacy/generation-route-repair-started.hex" ); + const FIXTURE_GENERATION_V3_ODIN: &str = + include_str!("../tests/fixtures/idunn-control-legacy/generation-v3-odin.hex"); + const FIXTURE_GENERATION_V3_ROUTE_PROOF: &str = + include_str!("../tests/fixtures/idunn-control-legacy/generation-v3-route-proof.hex"); const FIXTURE_TRANSACTIONS: [(&str, &str); 6] = [ ( "fencing", @@ -10264,7 +10997,9 @@ mod tests { .step_by(2) .map(|index| u8::from_str_radix(&hex[index..index + 2], 16)) .collect::, _>>()?; - let key = if record_type == AdmittedGeneration::TYPE { + let key = if record_type == AdmittedGeneration::TYPE && schema == ADMITTED_GENERATION_SCHEMA_V3 { + rmp_serde::from_slice::(&payload)?.target + } else if record_type == AdmittedGeneration::TYPE { rmp_serde::from_slice::(&payload)?.target } else { rmp_serde::from_slice::(&payload)?.transaction_id @@ -10307,11 +11042,6 @@ mod tests { legacy.odin_publisher_sequence_cursor ); assert_eq!(lifted.ready.voucher(), Voucher::Odin); - // The repair intent still decides whatever it decided. - assert_eq!( - lifted.route_repair_started_at_unix_millis, - legacy.route_repair_started_at_unix_millis - ); assert_eq!(lifted.routing, legacy.routing); assert_eq!(lifted.leasing, legacy.leasing); assert_eq!(lifted.plan, legacy.plan); @@ -10321,17 +11051,13 @@ mod tests { assert_eq!(lifted.generation_id, legacy.generation_id); // The new state starts empty and the plan is still a v2 plan. assert_eq!(lifted.route_supervision, Some(RouteSupervisionState::default())); - assert_eq!(lifted.continuity_backoff, ContinuityBackoff::default()); + assert_eq!(lifted.last_error, None); assert_eq!(lifted.plan.schema, crate::deployment_plan::COMPILED_DEPLOYMENT_PLAN_SCHEMA_V2); assert_eq!( lifted.plan.phase_deadlines(), crate::deployment_plan::PhaseDeadlines::IDUNN_DEFAULTS ); } - let (_, repairing) = fixture_generation(FIXTURE_GENERATION_REPAIRING)?; - assert!(repairing.route_repair_started_at_unix_millis.is_some()); - let (_, plain) = fixture_generation(FIXTURE_GENERATION)?; - assert!(plain.route_repair_started_at_unix_millis.is_none()); Ok(()) } @@ -10614,51 +11340,20 @@ mod tests { } #[test] - fn a_new_generation_inherits_actuation_and_backoff_but_not_observation() -> Result<()> { - let (_, mut incumbent) = fixture_generation(FIXTURE_GENERATION)?; - let mut state = RouteSupervisionState { - last_challenge_at_unix_millis: Some(5), - consecutive_failures: 4, - next_challenge_at_unix_millis: Some(9), - degraded_since_unix_millis: Some(6), - actuations: ActuationWindow { - window_started_at_unix_millis: 3, - count: 2, - }, - }; - incumbent.route_supervision = Some(state.clone()); - incumbent.continuity_backoff = ContinuityBackoff { - window_started_at_unix_millis: Some(1), - attempts: 2, - next_restart_at_unix_millis: Some(8), - }; + fn a_new_generation_starts_unobserved_and_undegraded() -> Result<()> { let committing = fixture_transaction(FIXTURE_TRANSACTIONS[4].1)?.1; - incumbent.odin_receipts()?; + let (_, incumbent) = fixture_generation(FIXTURE_GENERATION)?; let authority = incumbent .odin_authority .clone() .context("fixture generation has no Odin authority")?; let next = AdmittedGeneration::from_transaction( - &committing, - Some(authority.clone()), - Some(&incumbent), - 1_700_000_200_000, - )?; - state = RouteSupervisionState { - actuations: state.actuations, - ..RouteSupervisionState::default() - }; - assert_eq!(next.route_supervision, Some(state)); - assert_eq!(next.continuity_backoff, incumbent.continuity_backoff); - - let first = AdmittedGeneration::from_transaction( &committing, Some(authority), - None, 1_700_000_200_000, )?; - assert_eq!(first.route_supervision, Some(RouteSupervisionState::default())); - assert_eq!(first.continuity_backoff, ContinuityBackoff::default()); + assert_eq!(next.route_supervision, Some(RouteSupervisionState::default())); + assert_eq!(next.last_error, None); Ok(()) } @@ -11599,7 +12294,7 @@ mod tests { } #[test] - fn route_supervision_and_continuity_backoff_are_checked_on_the_generation() -> Result<()> { + fn route_supervision_is_checked_on_the_generation_and_the_meters_on_their_own_record() -> Result<()> { let (_, generation) = fixture_generation(FIXTURE_GENERATION)?; generation.validate()?; let supervised = |change: fn(&mut RouteSupervisionState)| { @@ -11609,22 +12304,23 @@ mod tests { }; assert!(supervised(|state| state.degraded_since_unix_millis = Some(0)) .contains("no start time")); - assert!(supervised(|state| state.actuations.count = 1) - .contains("count but no start")); - let mut degraded = generation.clone(); - let state = degraded.route_supervision.as_mut().unwrap(); - state.degraded_since_unix_millis = Some(5); - state.actuations = ActuationWindow { - window_started_at_unix_millis: 5, - count: 2, - }; - degraded.validate()?; - let mut backoff = generation.clone(); - backoff.continuity_backoff.attempts = 2; - assert!(error_text(backoff.validate()).contains("attempts but no window")); - backoff.continuity_backoff.window_started_at_unix_millis = Some(5); - backoff.validate()?; + let meters = |change: fn(&mut TargetSupervision)| { + let mut meters = TargetSupervision::new("service"); + change(&mut meters); + error_text(meters.validate()) + }; + TargetSupervision::new("service").validate()?; + assert!(meters(|m| m.route_actuations = vec![0]).contains("ascending")); + assert!(meters(|m| m.route_actuations = vec![9, 5]).contains("ascending")); + assert!( + meters(|m| m.route_actuations = (1..=13).collect()).contains("exceeds its bound") + ); + assert!(meters(|m| m.continuity_restarts = (1..=7).collect()).contains("exceeds its bound")); + assert!(meters(|m| m.continuity_deferred_until = Some(5)).contains("time or a reason")); + assert!(meters(|m| m.continuity_deferral_reason = Some("x".into())).contains("time or a reason")); + assert!(meters(|m| m.schema_version = "idunn.target_supervision.v0".into()) + .contains("unsupported")); Ok(()) } @@ -11647,6 +12343,7 @@ mod tests { }, value: generation, }], + targets: Vec::new(), }; assert_eq!(snapshot.max_odin_sequence(&target, &authority.signer_identity_id), 88); assert_eq!(snapshot.max_odin_sequence(&target, "another-signer"), 0); @@ -12169,6 +12866,48 @@ mod tests { Ok(next) } + /// Replace or create a target's meters in the store. + fn set_meters(world: &EngineFixture, meters: &TargetSupervision) -> Result<()> { + let snapshot = ControlSnapshot::read(&world.state_store)?; + world.engine.write_target_supervision( + snapshot.supervision_for(&meters.target), + meters, + now_millis()?, + ) + } + + fn meters_of(world: &EngineFixture, target: &str) -> Result { + Ok(ControlSnapshot::read(&world.state_store)?.supervision_or_new(target)) + } + + /// Meters for "service" whose restarts happened these many milliseconds ago. + fn restarted_ago(ages: &[u64]) -> Result { + let now = now_millis()?; + let mut meters = TargetSupervision::new("service"); + meters.continuity_restarts = ages.iter().map(|age| now - age).collect(); + meters.continuity_restarts.sort_unstable(); + Ok(meters) + } + + /// Drop every live transaction and command: what a finished restart leaves. + fn clear_live_transactions(world: &EngineFixture) -> Result<()> { + let snapshot = ControlSnapshot::read(&world.state_store)?; + let envelopes = snapshot + .transactions + .iter() + .map(|stored| stored.envelope.clone()) + .chain(snapshot.commands.iter().map(|stored| stored.envelope.clone())) + .collect::>(); + if envelopes.is_empty() { + return Ok(()); + } + assert!( + SingleFileMessagePackBackingStore::new(&world.state_store) + .delete_batch_if_unchanged(&envelopes)? + ); + Ok(()) + } + fn incumbent_envelope(world: &EngineFixture) -> Result { Ok(ControlSnapshot::read(&world.state_store)? .admitted_for("service") @@ -12265,14 +13004,9 @@ mod tests { fn a_deploy_that_fences_commits_and_retires_the_incumbent_it_replaced() -> Result<()> { let world = EngineFixture::with_workload(Arc::new(StillWorkload))?; admit_incumbent(&world)?; - let backoff = ContinuityBackoff { - window_started_at_unix_millis: Some(1_000), - attempts: 2, - next_restart_at_unix_millis: Some(9_000), - }; - let incumbent = edit_incumbent(&world, |generation| { - generation.continuity_backoff = backoff.clone(); - })?; + let meters = restarted_ago(&[600_000, 60_000])?; + set_meters(&world, &meters)?; + let incumbent = edit_incumbent(&world, |_| {})?; let candidate = seeded_transaction( &world, @@ -12292,8 +13026,8 @@ mod tests { .clone(); assert_eq!(admitted.transaction_id, candidate.transaction_id); assert_ne!(admitted.generation_id, incumbent.generation_id); - // The restart ceiling belongs to the target: a commit must not reset it. - assert_eq!(admitted.continuity_backoff, backoff); + // The restart log belongs to the target: a commit must not reset it. + assert_eq!(meters_of(&world, "service")?, meters); assert_eq!(admitted.route_supervision, incumbent.route_supervision); // The commit did not stop the incumbent: it is retired after. @@ -13180,9 +13914,51 @@ mod tests { assert_eq!(backoff_wait(500, 3), 4000); assert_eq!(backoff_wait(500, 30), RESUME_BACKOFF_CEILING_MILLIS); assert_eq!(backoff_wait(u64::MAX, 5), RESUME_BACKOFF_CEILING_MILLIS); - assert!(is_waiting(9, 10)); - assert!(!is_waiting(10, 10)); - assert!(!is_waiting(11, 10)); + assert!(is_waiting(9, 10, 5)); + assert!(!is_waiting(10, 10, 5)); + assert!(!is_waiting(11, 10, 5)); + // Due further ahead than the longest wait the owner can set: the clock + // stepped back, so the attempt is due, not stalled for the step. + assert!(is_waiting(5, 10, 5)); + assert!(!is_waiting(4, 10, 5)); + } + + #[test] + fn a_resume_backoff_set_by_a_stepped_back_clock_is_due_and_a_real_one_waits() -> Result<()> { + let world = EngineFixture::with_workload(Arc::new(StillWorkload))?; + let id = transaction_at(&world, DeploymentPhase::Warming)?.transaction_id; + let not_before = |world: &EngineFixture| { + world + .engine + .resume_backoff + .lock() + .unwrap() + .get(&id) + .map(|backoff| backoff.not_before_unix_millis) + }; + let set = |world: &EngineFixture, not_before_unix_millis: u64| { + world.engine.resume_backoff.lock().unwrap().insert( + id.clone(), + ResumeBackoff { + failures: 3, + not_before_unix_millis, + }, + ); + }; + + // Half a minute ahead is a wait the backoff can set: the step is left alone. + let waiting = now_millis()? + 30_000; + set(&world, waiting); + let _ = world.engine.resume_candidate(&resident(&world)?); + assert_eq!(not_before(&world), Some(waiting)); + + // A day ahead is a clock that stepped back: the step is attempted, and + // whatever it does, it replaces the backoff. + let ahead = now_millis()? + 86_400_000; + set(&world, ahead); + let _ = world.engine.resume_candidate(&resident(&world)?); + assert!(not_before(&world).is_none_or(|due| due < ahead)); + Ok(()) } #[test] @@ -13325,7 +14101,6 @@ mod tests { let workload = SwitchWorkload::new(); let world = EngineFixture::with_workload(workload.clone())?; let incumbent = admit_incumbent(&world)?; - assert_eq!(incumbent.continuity_backoff, ContinuityBackoff::default()); // The projection names an activation that is not the incumbent's. let anchor = world.engine.provider_anchor_for_plan(&incumbent.plan)?; @@ -13366,13 +14141,14 @@ mod tests { snapshot.transactions.is_empty(), "continuity minted a restart over a projection it could not demote" ); - let deferred = snapshot - .admitted_for("service") - .context("no generation")? + let recorded = snapshot + .supervision_for("service") + .context("the failed demotion was not recorded")? .value - .continuity_backoff - .next_restart_at_unix_millis - .context("the failed demotion was not recorded")?; + .clone(); + let deferred = recorded.continuity_deferred_until.context("no deferral time")?; + assert!(recorded.continuity_deferral_reason.is_some()); + assert!(recorded.continuity_restarts.is_empty(), "a deferral spent an attempt"); let after = now_millis()?; assert!( (before + CONTINUITY_DEFERRAL_MILLIS..=after + CONTINUITY_DEFERRAL_MILLIS) @@ -13380,21 +14156,24 @@ mod tests { ); // Deferred: the next tick neither retries nor writes. - let envelope = incumbent_envelope(&world)?; assert!(!world.engine.supervise_one_admitted_generation()?); - assert_eq!(incumbent_envelope(&world)?, envelope); + assert_eq!(meters_of(&world, "service")?, recorded); assert!(ControlSnapshot::read(&world.state_store)?.transactions.is_empty()); // The projection is repaired and the deferral has run out: the restart // proceeds, from an Expected-only projection. topology.withdraw_stale_incarnation(&incumbent.expected, &anchor)?; project_admitted(&world, &incumbent)?; - edit_incumbent(&world, |generation| { - generation.continuity_backoff.next_restart_at_unix_millis = Some(1); - })?; + let mut ran_out = recorded.clone(); + ran_out.continuity_deferred_until = Some(1); + set_meters(&world, &ran_out)?; assert!(world.engine.supervise_one_admitted_generation()?); let snapshot = ControlSnapshot::read(&world.state_store)?; assert_eq!(snapshot.transactions.len(), 1); + let minted = snapshot.supervision_for("service").context("no meters")?; + assert_eq!(minted.value.continuity_restarts.len(), 1); + assert_eq!(minted.value.continuity_deferred_until, None); + assert_eq!(minted.value.continuity_deferral_reason, None); assert_eq!(snapshot.transactions[0].value.command_kind, CommandKind::Continuity); assert_eq!(projected_under(&world, &incumbent.expected)?, expected_only()); Ok(()) @@ -13597,24 +14376,18 @@ mod tests { } #[test] - fn unreadable_history_stops_continuity_instead_of_counting_no_failures() -> Result<()> { + fn unreadable_history_does_not_stop_continuity() -> Result<()> { let workload = SwitchWorkload::new(); let world = EngineFixture::with_workload(workload.clone())?; admit_incumbent(&world)?; workload.kill(); corrupt_history(&world)?; - let envelope = incumbent_envelope(&world)?; - // A dead workload would be restarted at once, with the ceiling counting - // zero refusals. With history unreadable nothing is actuated. - assert!(!world.engine.supervise_one_admitted_generation()?); - assert!(ControlSnapshot::read(&world.state_store)?.transactions.is_empty()); - assert_eq!(incumbent_envelope(&world)?, envelope); - - // Readable again, the same tick restarts it. - std::fs::remove_file(history_store_path(&world.state_store))?; + // Crash recovery reads the target's own log, never history: the dead + // workload is restarted, and the restart is counted there. assert!(world.engine.supervise_one_admitted_generation()?); assert_eq!(ControlSnapshot::read(&world.state_store)?.transactions.len(), 1); + assert_eq!(meters_of(&world, "service")?.continuity_restarts.len(), 1); Ok(()) } @@ -13687,27 +14460,128 @@ mod tests { } #[test] - fn continuity_stops_after_three_refused_restarts_counted_from_history() -> Result<()> { + fn continuity_stops_when_the_targets_own_log_has_spent_the_window() -> Result<()> { + let workload = SwitchWorkload::new(); + let world = EngineFixture::with_workload(workload.clone())?; + admit_incumbent(&world)?; + workload.kill(); + // Six restarts inside the window, the last long enough ago that its + // doubling wait (160 s) is over: only the ceiling holds. + set_meters( + &world, + &restarted_ago(&[3_000_000, 2_400_000, 1_800_000, 1_200_000, 600_000, 200_000])?, + )?; + assert!(!world.engine.supervise_one_admitted_generation()?); + assert!(ControlSnapshot::read(&world.state_store)?.transactions.is_empty()); + assert!( + world + .engine + .fault_reports + .lock() + .unwrap() + .contains_key("continuity:service"), + "exhaustion was not reported" + ); + + // The oldest leaves the window: five remain, so one more is allowed. + set_meters( + &world, + &restarted_ago(&[3_700_000, 2_400_000, 1_800_000, 1_200_000, 600_000, 200_000])?, + )?; + assert!(world.engine.supervise_one_admitted_generation()?); + assert_eq!(ControlSnapshot::read(&world.state_store)?.transactions.len(), 1); + Ok(()) + } + + #[test] + fn an_exhausted_target_says_when_its_route_ceiling_reopens_only_if_it_is_full() -> Result<()> { + let workload = SwitchWorkload::new(); + let world = EngineFixture::with_workload(workload.clone())?; + admit_incumbent(&world)?; + workload.kill(); + let said = || -> String { + world + .engine + .fault_reports + .lock() + .unwrap() + .get("continuity:service") + .and_then(|report| report.last.lock().unwrap().clone()) + .unwrap_or_default() + }; + let mut meters = + restarted_ago(&[3_000_000, 2_400_000, 1_800_000, 1_200_000, 600_000, 200_000])?; + set_meters(&world, &meters)?; + assert!(!world.engine.supervise_one_admitted_generation()?); + let open = said(); + assert!(open.contains("free for a deployment"), "{open}"); + assert!(!open.contains("route ceiling"), "{open}"); + + world.engine.clear_fault("continuity:service"); + let now = now_millis()?; + meters.route_actuations = (0..ROUTE_ACTUATION_CEILING as u64).map(|n| now - 1_000 + n).collect(); + set_meters(&world, &meters)?; + let reopens_at = meters.route_reopens_at(now).unwrap(); + assert!(!world.engine.supervise_one_admitted_generation()?); + let full = said(); + assert!(full.contains(&format!("reopens at unix ms {reopens_at}")), "{full}"); + Ok(()) + } + + #[test] + fn continuity_restarts_are_spaced_by_a_doubling_wait_and_counted_in_the_mint() -> Result<()> { + let workload = SwitchWorkload::new(); + let world = EngineFixture::with_workload(workload.clone())?; + admit_incumbent(&world)?; + workload.kill(); + let restarts = |world: &EngineFixture| -> Result { + let progressed = world.engine.supervise_one_admitted_generation()?; + let minted = !ControlSnapshot::read(&world.state_store)?.transactions.is_empty(); + assert_eq!(progressed, minted); + clear_live_transactions(world)?; + Ok(minted) + }; + + // The first restart is minted, and the same write counts it. + assert!(restarts(&world)?); + assert_eq!(meters_of(&world, "service")?.continuity_restarts.len(), 1); + // At once again: the 5 s wait has not passed. + assert!(!restarts(&world)?); + // The second wait is 10 s and the third 20 s: each is the last one doubled. + set_meters(&world, &restarted_ago(&[100_000, 6_000])?)?; + assert!(!restarts(&world)?); + set_meters(&world, &restarted_ago(&[100_000, 11_000])?)?; + assert!(restarts(&world)?); + assert_eq!(meters_of(&world, "service")?.continuity_restarts.len(), 3); + set_meters(&world, &restarted_ago(&[200_000, 100_000, 15_000])?)?; + assert!(!restarts(&world)?); + set_meters(&world, &restarted_ago(&[200_000, 100_000, 21_000])?)?; + assert!(restarts(&world)?); + Ok(()) + } + + #[test] + fn a_deployment_yields_to_a_restart_only_while_the_target_has_restarts_left() -> Result<()> { let workload = SwitchWorkload::new(); let world = EngineFixture::with_workload(workload.clone())?; let incumbent = admit_incumbent(&world)?; - for round in 0..CONTINUITY_RESTART_ATTEMPTS { - let failed = seeded_transaction( - &world, - DeploymentPhase::Warming, - CommandKind::Continuity, - Some(&incumbent), - )?; - world - .engine - .begin_pre_fencing_abort(&resident(&world)?, anyhow!("refused {round}"))?; - drive(&world, |transaction| transaction.completion.is_some())?; - assert!(record_of(&world, &failed.transaction_id)?.is_terminal()); - } + let redeploy = + seeded_transaction(&world, DeploymentPhase::Warming, CommandKind::Deploy, Some(&incumbent))?; workload.kill(); - // Three refusals are in history and none is live: the ceiling holds. - assert!(!world.engine.supervise_one_admitted_generation()?); - assert!(ControlSnapshot::read(&world.state_store)?.transactions.is_empty()); + // The target's restarts are spent: nothing is left to yield to, and + // aborting the deployment would close the last door. + set_meters( + &world, + &restarted_ago(&[3_000_000, 2_400_000, 1_800_000, 1_200_000, 600_000, 200_000])?, + )?; + world.engine.supervise_one_admitted_generation()?; + let after = resident(&world)?.value; + assert_eq!(after.transaction_id, redeploy.transaction_id); + assert!(after.pre_fencing_abort.is_none(), "yielded with no restarts left"); + + set_meters(&world, &restarted_ago(&[200_000])?)?; + world.engine.supervise_one_admitted_generation()?; + assert!(resident(&world)?.value.pre_fencing_abort.is_some()); Ok(()) } @@ -14120,6 +14994,291 @@ mod tests { Ok(()) } + // --------------------------------------------------------------------- + // B2: the meters, the v4 lift, and what `status` shows. + // --------------------------------------------------------------------- + + #[test] + fn the_actuation_log_slides() -> Result<()> { + let window = ROUTE_ACTUATION_WINDOW_MILLIS; + let t = 1_000_000_000_u64; + let mut meters = TargetSupervision::new("service"); + for offset in 0..12 { + meters.charge_route(t + offset, RouteActuation::Forward)?; + } + let refused = meters.charge_route(t + window - 1, RouteActuation::Forward).unwrap_err(); + assert_eq!((refused.used, refused.reopens_at_unix_millis), (12, t + window)); + // Only the first entry has left the window: exactly one slot reopens. + meters.charge_route(t + window, RouteActuation::Forward)?; + let refused = meters.charge_route(t + window, RouteActuation::Forward).unwrap_err(); + assert_eq!((refused.used, refused.reopens_at_unix_millis), (12, t + 1 + window)); + meters.validate()?; + Ok(()) + } + + #[test] + fn a_survival_actuation_is_counted_past_the_limit_and_never_refused() -> Result<()> { + let t = 1_000_000_000_u64; + let mut meters = TargetSupervision::new("service"); + for offset in 0..12 { + meters.charge_route(t + offset, RouteActuation::Forward)?; + } + meters.charge_route(t + 20, RouteActuation::Survival)?; + assert_eq!(meters.route_actuations.len(), ROUTE_ACTUATION_CEILING); + assert_eq!(meters.route_actuations.last(), Some(&(t + 20))); + // The survival took a slot: the forward ceiling is still exact, and + // reopens when the oldest surviving entry leaves. + let refused = meters.charge_route(t + 21, RouteActuation::Forward).unwrap_err(); + assert_eq!(refused.reopens_at_unix_millis, t + 1 + ROUTE_ACTUATION_WINDOW_MILLIS); + for _ in 0..20 { + meters.charge_route(t + 22, RouteActuation::Survival)?; + } + meters.validate()?; + Ok(()) + } + + #[test] + fn a_clock_stepped_back_stalls_nothing_past_one_window() -> Result<()> { + let t = 5_000_000_000_u64; + let now = t - 86_400_000; + let mut meters = TargetSupervision::new("service"); + meters.route_actuations = vec![t; ROUTE_ACTUATION_CEILING]; + meters.continuity_restarts = vec![t; CONTINUITY_RESTART_ATTEMPTS]; + meters.continuity_deferred_until = Some(t); + meters.continuity_deferral_reason = Some("demotion failed".into()); + meters.validate()?; + + // Entries from the future count as now: the meters hold for one window. + let read_only = meters.clone(); + assert!(read_only.restarts_exhausted(now)); + let refused = meters.charge_route(now, RouteActuation::Forward).unwrap_err(); + assert!(refused.reopens_at_unix_millis <= now + ROUTE_ACTUATION_WINDOW_MILLIS); + // The clamp is written, so the window runs from the step and no longer. + assert_eq!(meters.route_actuations, vec![now; ROUTE_ACTUATION_CEILING]); + assert_eq!(meters.continuity_restarts, vec![now; CONTINUITY_RESTART_ATTEMPTS]); + assert!(!meters.restarts_exhausted(now + CONTINUITY_RESTART_WINDOW_MILLIS)); + meters.charge_route(now + ROUTE_ACTUATION_WINDOW_MILLIS, RouteActuation::Forward)?; + + // Waits set for the future are due, not stalled for the length of the step. + assert!(!read_only.continuity_is_waiting(now)); + let route = RouteSupervisionState { + next_challenge_at_unix_millis: Some(t), + ..RouteSupervisionState::default() + }; + assert!(!route.is_waiting(now, DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS)); + assert!(!is_waiting(now, t, RESUME_BACKOFF_CEILING_MILLIS)); + // The same waits, set for a moment ahead, still wait. + let ahead = RouteSupervisionState { + next_challenge_at_unix_millis: Some(now + 5_000), + ..RouteSupervisionState::default() + }; + assert!(ahead.is_waiting(now, DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS)); + Ok(()) + } + + #[test] + fn a_route_challenge_wait_widens_to_a_cap_and_a_proof_clears_it() { + let age = DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS; + let mut state = RouteSupervisionState::default(); + let mut waits = Vec::new(); + for _ in 0..8 { + state.record_failed_challenge(1_000, age); + waits.push(state.next_challenge_at_unix_millis.unwrap() - 1_000); + } + assert_eq!(&waits[..3], &[age, age * 2, age * 4]); + assert_eq!(waits[7], ROUTE_CHALLENGE_BACKOFF_CAP_MILLIS); + assert_eq!(state.degraded_since_unix_millis, Some(1_000)); + state.record_proved_challenge(9_000); + assert_eq!(state.consecutive_failures, 0); + assert_eq!(state.next_challenge_at_unix_millis, None); + assert_eq!(state.degraded_since_unix_millis, None); + } + + #[test] + fn a_v3_generation_lifts_to_v4_once_and_reencodes_canonically() -> Result<()> { + for text in [FIXTURE_GENERATION_V3_ODIN, FIXTURE_GENERATION_V3_ROUTE_PROOF] { + let temp = TempDir::new()?; + let store = temp.path().join("control.cc"); + let envelope = fixture_envelope(text, AdmittedGeneration::TYPE)?; + let legacy: LegacyAdmittedGenerationV3 = rmp_serde::from_slice(&envelope.payload)?; + SingleFileMessagePackBackingStore::new(&store).insert_entry_if_absent(envelope.clone())?; + // The v3 bytes are not v4 bytes: only the typed lift may read them. + assert!(decode_record::(&envelope).is_err()); + + assert_eq!(migrate_control_store_to_current_schema(&store)?, 1); + assert_eq!(migrate_control_store_to_current_schema(&store)?, 0); + let snapshot = ControlSnapshot::read(&store)?; + let migrated = snapshot.admitted.first().context("the lift lost the record")?; + assert_eq!(migrated.envelope.schema_id.as_deref(), Some(ADMITTED_GENERATION_SCHEMA)); + // After the lift the canonical re-encode accepts its own bytes. + let lifted: AdmittedGeneration = decode_record(&migrated.envelope)?; + assert_eq!(lifted, migrated.value); + + // The decisions are the ones the v3 record encoded; only the retired + // fields are gone, and no meters were invented. + assert_eq!(lifted.target, legacy.target); + assert_eq!(lifted.generation_id, legacy.generation_id); + assert_eq!(lifted.plan, legacy.plan); + assert_eq!(lifted.expected, legacy.expected); + assert_eq!(lifted.activation, legacy.activation); + assert_eq!(lifted.workload, legacy.workload); + assert_eq!(lifted.leasing, legacy.leasing); + assert_eq!(lifted.ready, legacy.ready); + assert_eq!(lifted.latest_odin_observation, legacy.latest_odin_observation); + assert_eq!(lifted.routing, legacy.routing); + assert_eq!(lifted.odin_authority, legacy.odin_authority); + assert_eq!( + lifted.odin_publisher_sequence_cursor, + legacy.odin_publisher_sequence_cursor + ); + assert_eq!( + lifted.route_supervision, + legacy.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, + }) + ); + assert_eq!(lifted.last_error, None); + assert!(snapshot.targets.is_empty()); + } + Ok(()) + } + + #[test] + fn a_v3_generation_that_shows_written_meters_is_refused_not_dropped() -> Result<()> { + let envelope = fixture_envelope(FIXTURE_GENERATION_V3_ROUTE_PROOF, AdmittedGeneration::TYPE)?; + let legacy: LegacyAdmittedGenerationV3 = rmp_serde::from_slice(&envelope.payload)?; + assert!(legacy.route_supervision.is_some(), "the fixture is a routed generation"); + let refused = |change: fn(&mut LegacyAdmittedGenerationV3)| -> Result { + let mut edited = legacy.clone(); + change(&mut edited); + let mut tampered = envelope.clone(); + tampered.payload = rmp_serde::to_vec(&edited)?; + Ok(error_text(read_generation_record(&tampered).map(|_| ()))) + }; + let restarts = refused(|edited| edited.continuity_backoff.attempts = 2)?; + assert!(restarts.contains("refusing to drop"), "{restarts}"); + assert!(restarts.contains(&legacy.target), "the error names the record: {restarts}"); + let actuations = + refused(|edited| edited.route_supervision.as_mut().unwrap().actuations.count = 1)?; + assert!(actuations.contains("refusing to drop"), "{actuations}"); + + // A whole store carrying one refuses to boot, naming the record. + let temp = TempDir::new()?; + let store = temp.path().join("control.cc"); + let mut edited = legacy.clone(); + edited.continuity_backoff.attempts = 2; + let mut tampered = envelope.clone(); + tampered.payload = rmp_serde::to_vec(&edited)?; + SingleFileMessagePackBackingStore::new(&store).insert_entry_if_absent(tampered)?; + assert!(migrate_control_store_to_current_schema(&store).is_err()); + Ok(()) + } + + #[test] + fn a_v3_generation_lifts_each_route_supervision_field_to_its_own_place() -> Result<()> { + let envelope = fixture_envelope(FIXTURE_GENERATION_V3_ROUTE_PROOF, AdmittedGeneration::TYPE)?; + let mut legacy: LegacyAdmittedGenerationV3 = rmp_serde::from_slice(&envelope.payload)?; + // Four distinct non-default values, so a swap cannot land on itself. + legacy.route_supervision = Some(LegacyRouteSupervisionStateV3 { + last_challenge_at_unix_millis: Some(1_111), + consecutive_failures: 3, + next_challenge_at_unix_millis: Some(2_222), + degraded_since_unix_millis: Some(3_333), + actuations: LegacyActuationWindowV3::default(), + }); + let mut edited = envelope.clone(); + edited.payload = rmp_serde::to_vec(&legacy)?; + let lifted = read_generation_record(&edited)?; + assert_eq!( + lifted.route_supervision, + Some(RouteSupervisionState { + last_challenge_at_unix_millis: Some(1_111), + consecutive_failures: 3, + next_challenge_at_unix_millis: Some(2_222), + degraded_since_unix_millis: Some(3_333), + }) + ); + Ok(()) + } + + #[test] + fn status_covers_the_admitted_targets_and_the_metered_ones() -> Result<()> { + // "service" is admitted with no meters at all; "metered" has meters and + // no generation. Neither set contains the other. + let world = EngineFixture::with_workload(SwitchWorkload::new())?; + admit_incumbent(&world)?; + assert!(ControlSnapshot::read(&world.state_store)?.targets.is_empty()); + set_meters(&world, &TargetSupervision::new("metered"))?; + let snapshot = ControlSnapshot::read(&world.state_store)?; + assert_eq!( + supervised_targets(&snapshot).into_iter().collect::>(), + ["metered", "service"] + ); + Ok(()) + } + + #[test] + fn target_supervision_reads_back_and_its_key_must_be_its_target() -> Result<()> { + let world = EngineFixture::new()?; + let meters = restarted_ago(&[5_000])?; + set_meters(&world, &meters)?; + assert_eq!(meters_of(&world, "service")?, meters); + assert_eq!(ControlSnapshot::read(&world.state_store)?.targets.len(), 1); + + let mut elsewhere = target_supervision_envelope(&meters, 5)?; + elsewhere.key = "other".into(); + SingleFileMessagePackBackingStore::new(&world.state_store).insert_entry_if_absent(elsewhere)?; + let error = error_text(ControlSnapshot::read(&world.state_store).map(|_| ())); + assert!(error.contains("not its target"), "{error}"); + Ok(()) + } + + #[test] + fn status_shows_each_targets_meters_including_a_target_with_no_generation() -> Result<()> { + let now = 10_000_000_u64; + let mut meters = TargetSupervision::new("service"); + meters.route_actuations = (0..12).map(|offset| now - 5_000 + offset).collect(); + meters.continuity_restarts = vec![now - 9_000, now - 4_000]; + meters.continuity_deferred_until = Some(now + 500); + meters.continuity_deferral_reason = Some("projection would not demote".into()); + + let (_, mut generation) = fixture_generation(FIXTURE_GENERATION)?; + generation.last_error = Some("held".into()); + let lines = render_supervision("service", Some(&generation), Some(&meters), now).join("\n"); + assert!(lines.contains(&format!("target service {}", generation.generation_id)), "{lines}"); + assert!(lines.contains("held: held"), "{lines}"); + assert!(lines.contains(&format!( + "continuity restarts 2/6 next-restart-at {}", + now - 4_000 + 10_000 + )), "{lines}"); + assert!(lines.contains("continuity deferred until 10000500: projection would not demote"), "{lines}"); + assert!(lines.contains(&format!( + "route actuations 12/12 in window, reopens-at {}", + now - 5_000 + ROUTE_ACTUATION_WINDOW_MILLIS + )), "{lines}"); + assert!(lines.contains("route healthy consecutive-failures 0"), "{lines}"); + + // A first deployment has meters and no generation: it shows them alone. + let mut first = TargetSupervision::new("fresh"); + first.route_actuations = vec![now - 1]; + let lines = render_supervision("fresh", None, Some(&first), now).join("\n"); + assert!(lines.contains("target fresh no-admitted-generation"), "{lines}"); + assert!(lines.contains("route actuations 1/12 in window, reopens-at none"), "{lines}"); + // An unrouted, unmetered target shows no route line at all. + let lines = render_supervision("plain", Some(&unrouted_generation()?), None, now).join("\n"); + assert!(!lines.contains("route actuations"), "{lines}"); + Ok(()) + } + + fn unrouted_generation() -> Result { + let (_, mut generation) = fixture_generation(FIXTURE_GENERATION)?; + generation.route_supervision = None; + Ok(generation) + } + // --------------------------------------------------------------------- // Route-proof readiness: a routed target that declares no Odin dependency // is admitted by Idunn's own challenges. Odin is never read. @@ -14338,6 +15497,7 @@ mod tests { struct RoutedWorld { world: EngineFixture, + workload: Arc, stub: Arc, odin: Odin, poisoned: Vec, @@ -14353,14 +15513,31 @@ mod tests { phase: DeploymentPhase, provides_odin: bool, ) -> Result { - build_routed_world(odin, minimum_capacity, phase, provides_odin, false) + build_routed_world( + odin, + minimum_capacity, + phase, + provides_odin, + false, + CommandKind::Continuity, + ) + } + + /// The same world owned by a deployment command instead of a + /// continuity: its route actuations are `Forward` and can be refused. + fn routed_deploy_world( + odin: Odin, + minimum_capacity: u32, + phase: DeploymentPhase, + ) -> Result { + build_routed_world(odin, minimum_capacity, phase, false, false, CommandKind::Deploy) } /// The same world for a target with process-writable state: its /// candidate is granted a write lease and Ready must be the candidate /// reporting exactly that lease. fn stateful_routed_world(odin: Odin, phase: DeploymentPhase) -> Result { - build_routed_world(odin, 1, phase, false, true) + build_routed_world(odin, 1, phase, false, true, CommandKind::Continuity) } fn build_routed_world( @@ -14369,11 +15546,13 @@ mod tests { phase: DeploymentPhase, provides_odin: bool, stateful: bool, + kind: CommandKind, ) -> Result { use crate::deployment_plan::tests::{ BINDING, RECIPE, artifact_receipt, external_input_receipt, source, }; - let world = EngineFixture::with_workload(Arc::new(StillWorkload))?; + let switch = SwitchWorkload::new(); + let world = EngineFixture::with_workload(switch.clone())?; let candidate_listener = TcpListener::bind("127.0.0.1:0")?; let stable_listener = TcpListener::bind("127.0.0.1:0")?; let candidate_port = candidate_listener.local_addr()?.port(); @@ -14472,15 +15651,36 @@ mod tests { let now = now_millis()?; let command = DeploymentCommand { schema_version: DEPLOYMENT_COMMAND_SCHEMA.into(), - command_id: "continuity-service".into(), - kind: CommandKind::Continuity, + command_id: match kind { + CommandKind::Deploy => "up-service", + CommandKind::Continuity => "continuity-service", + } + .into(), + kind, selector: "service".into(), requested_by: "test".into(), requested_at_unix_millis: 100, }; let mut transaction = DeploymentTransaction::new(&command, "service".into(), 0, None, now)?; - transaction.lifecycle_authorized_at_unix_millis = Some(now); + match kind { + CommandKind::Continuity => { + transaction.lifecycle_authorized_at_unix_millis = Some(now); + } + CommandKind::Deploy => { + transaction.frozen_source = Some(FrozenSourceReceipt { + transaction_id: transaction.transaction_id.clone(), + plan_id: plan.plan_id.clone(), + snapshot_sha256: sha256_id(b"frozen source snapshot"), + }); + transaction.deployment_authorization = Some(deployment_authorization( + &world, + &expected, + &transaction.transaction_id, + now, + )?); + } + } let launch = IdunnRuntimeActivationLaunch::issue( &expected, runtime_instance_id(&transaction.transaction_id)?, @@ -14631,6 +15831,7 @@ mod tests { }; Ok(RoutedWorld { world, + workload: switch, stub, odin, poisoned, @@ -14700,30 +15901,19 @@ mod tests { Ok(()) } - /// Routing installs an nginx fragment and needs the host's programs, - /// so the world writes the fragment itself and records the promotion - /// the stable challenge proves, then enters Committing. - fn promote_by_hand(&self) -> Result<()> { - let current = resident(&self.world)?; - let transaction = ¤t.value; - assert_eq!(transaction.phase, DeploymentPhase::Routing); - let expected = transaction.expected.as_ref().unwrap(); - let binding = transaction.plan.as_ref().unwrap().parsed_inputs()?.1; - let driver = NginxRouteDriver::new(binding.route.unwrap()); - let rendered = driver.render(expected)?; - std::fs::write(&driver.binding.config_path, &rendered)?; - let observation = self.world.engine.prove_stable_route( - transaction, - &driver, - sha256_id(&rendered), - )?; - let mut next = transaction.clone(); - next.routing = Some(RoutingEvidence::Promoted { - promoted_at_unix_millis: observation.observed_at_unix_millis, - observation, - }); - next.enter_phase(DeploymentPhase::Committing, now_millis()?); - replace_transaction(&self.world.state_store, ¤t, &next) + /// Routing runs for real: it installs the fragment through the + /// world's stub host programs and proves the stable route against + /// the stub runtime. The world only drives the steps until the + /// transaction enters Committing. + fn promote(&self) -> Result<()> { + assert_eq!(self.transaction()?.phase, DeploymentPhase::Routing); + for _ in 0..4 { + if self.transaction()?.phase == DeploymentPhase::Committing { + return Ok(()); + } + self.step()?; + } + bail!("the transaction never reached Committing") } } @@ -14732,7 +15922,7 @@ mod tests { let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; routed.stub.set_state("active"); routed.run_to_routing()?; - routed.promote_by_hand()?; + routed.promote()?; routed.step()?; admitted(&routed) } @@ -14772,7 +15962,7 @@ mod tests { )); routed.step()?; assert_eq!(routed.transaction()?.phase, DeploymentPhase::Routing); - routed.promote_by_hand()?; + routed.promote()?; routed.step()?; let generation = admitted(&routed)?; @@ -14803,7 +15993,7 @@ mod tests { let routed = routed_world(Odin::NotReady, 1, DeploymentPhase::Fencing, false)?; routed.stub.set_state("active"); routed.run_to_routing()?; - routed.promote_by_hand()?; + routed.promote()?; routed.step()?; let generation = admitted(&routed)?; assert_eq!(generation.ready.voucher(), Voucher::Candidate); @@ -14848,7 +16038,7 @@ mod tests { let routed = routed_world(Odin::Unreachable, 2, DeploymentPhase::Fencing, false)?; routed.stub.set_state("active"); routed.run_to_routing()?; - routed.promote_by_hand()?; + routed.promote()?; routed.stub.capacity.store(1, Ordering::SeqCst); let failure = routed.step().expect_err("a shortfall must be refused"); // The shortfall is a typed value, not a sentence to search. @@ -14891,23 +16081,21 @@ mod tests { Ok(()) } + #[cfg(unix)] #[test] - fn route_admission_gets_past_its_gate_without_reading_odin() -> Result<()> { - // The step below would run the host's real nginx, so it only runs - // where there is none. - if Path::new("/usr/sbin/nginx").exists() { - return Ok(()); - } + fn route_admission_installs_through_the_host_programs_without_reading_odin() -> Result<()> { let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; routed.stub.set_state("active"); routed.run_to_routing()?; - // The step fails at installing the route: past the admission gate, - // which would have failed on Odin. - let failure = format!("{:#}", routed.step().expect_err("no nginx in the test world")); - assert!( - failure.contains("starting route actuator /usr/sbin/nginx"), - "{failure}" - ); + // One real Routing step: the fragment goes in through the world's + // stub nginx, ufw and systemctl, and the stable listener proves it. + routed.step()?; + assert!(matches!( + routed.transaction()?.routing, + Some(RoutingEvidence::Promoted { .. }) + )); + assert_eq!(routed.world.route_stubs.count("systemctl reload"), 1); + assert_eq!(routed.world.route_stubs.count("ufw allow"), 1); routed.assert_odin_untouched()?; Ok(()) } @@ -15008,7 +16196,7 @@ mod tests { routed.run_to_routing()?; let ready = routed.transaction()?; assert!(ready.ready.as_ref().and_then(ReadinessEvidence::odin).is_some()); - routed.promote_by_hand()?; + routed.promote()?; routed.step()?; let generation = admitted(&routed)?; generation.validate()?; @@ -15030,7 +16218,7 @@ mod tests { let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; routed.stub.set_state("active"); routed.run_to_routing()?; - routed.promote_by_hand()?; + routed.promote()?; routed.step()?; let snapshot = ControlSnapshot::read(&routed.world.state_store)?; routed.world.engine.validate_durable_authority(&snapshot)?; @@ -15122,7 +16310,7 @@ mod tests { routed.assert_boots()?; routed.drive_until(|t| t.phase == DeploymentPhase::Routing)?; - routed.promote_by_hand()?; + routed.promote()?; routed.assert_boots()?; routed.step()?; @@ -15380,7 +16568,7 @@ mod tests { let mut bare = routing.clone(); bare.ready = None; assert!(error_text(bare.validate()).contains("lacks Ready evidence")); - routed.promote_by_hand()?; + routed.promote()?; let mut bare = routed.transaction()?; assert_eq!(bare.phase, DeploymentPhase::Committing); bare.ready = None; @@ -15461,8 +16649,6 @@ mod tests { // The record is readable and boot accepts it: held, not refused. let snapshot = ControlSnapshot::read(&routed.world.state_store)?; routed.world.engine.validate_durable_authority(&snapshot)?; - // Stepping it would fail, on every tick, for good. - assert!(routed.step().is_err()); let hits = ( routed.stub.candidate_hits.load(Ordering::SeqCst), @@ -15514,7 +16700,7 @@ mod tests { routed.stub.set_state("active"); routed.run_to_routing()?; let at_routing = routed.transaction()?; - routed.promote_by_hand()?; + routed.promote()?; routed.step()?; let snapshot = ControlSnapshot::read(&routed.world.state_store)?; @@ -15755,7 +16941,7 @@ mod tests { #[test] fn a_stateful_target_that_provides_the_rendezvous_is_observed_directly_whatever_it_is_named() -> Result<()> { - let provider = build_routed_world(Odin::Unreachable, 1, DeploymentPhase::Warming, true, true)?; + let provider = build_routed_world(Odin::Unreachable, 1, DeploymentPhase::Warming, true, true, CommandKind::Continuity)?; let transaction = provider.transaction()?; assert_eq!(transaction.target, "service"); assert!(transaction.expected.as_ref().unwrap().write_lease_required); @@ -15829,5 +17015,730 @@ mod tests { )); Ok(()) } + + // ----------------------------------------------------------------- + // B2: the route gate, the meters and the supervision pass, through + // the Engine, over the world's stub host programs. + // ----------------------------------------------------------------- + + impl RoutedWorld { + /// Run a fresh world to an admitted generation whose transaction + /// is retired: what admitted-route supervision looks at. + fn admit(&self) -> Result<()> { + self.stub.set_state("active"); + self.run_to_routing()?; + self.promote()?; + self.drive_until(|transaction| transaction.is_terminal())?; + assert!(self.world.engine.retire_one_terminal_transaction()?); + Ok(()) + } + + fn fragment_path(&self) -> PathBuf { + self.world.root.join("service.conf") + } + + /// The target's route ledger holds `entries` recent actuations. + fn ledger(&self, entries: usize) -> Result<()> { + let now = now_millis()?; + let mut meters = TargetSupervision::new("service"); + meters.route_actuations = (0..entries as u64).map(|offset| now - 1_000 + offset).collect(); + set_meters(&self.world, &meters) + } + + #[cfg(unix)] + fn reloads(&self) -> usize { + self.world.route_stubs.count("systemctl reload") + } + + /// Something else rewrote the fragment: the admitted membership drifted. + fn drift(&self) -> Result<()> { + std::fs::write(self.fragment_path(), b"drifted bytes\n")?; + Ok(()) + } + + fn tick(&self) -> Result { + self.world.engine.supervise_one_admitted_generation() + } + } + + // A store that reads but cannot be written (ENOSPC on the state volume) + // is simulated with RLIMIT_FSIZE, which is process-wide: the test + // re-runs itself in a child process so no sibling test loses its writes. + // Returns true in the child, which runs the body; the parent returns + // false after asserting the child passed and hands back its stderr. + #[cfg(unix)] + fn in_unwritable_store_child(test: &str) -> Result> { + if std::env::var_os("IDUNN_TEST_UNWRITABLE_STORE_CHILD").is_some() { + return Ok(None); + } + let path = module_path!().split_once("::").map_or("", |(_, rest)| rest); + let output = std::process::Command::new(std::env::current_exe()?) + .args(["--exact", &format!("{path}::{test}"), "--nocapture", "--test-threads=1"]) + .env("IDUNN_TEST_UNWRITABLE_STORE_CHILD", "1") + .output()?; + let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); + assert!( + output.status.success(), + "child failed:\n{}\n{stderr}", + String::from_utf8_lossy(&output.stdout) + ); + assert!( + String::from_utf8_lossy(&output.stdout).contains("1 passed"), + "the child ran no test: {}", + String::from_utf8_lossy(&output.stdout) + ); + Ok(Some(stderr)) + } + + /// Cap file growth one byte under the store's size: reads work, every + /// write fails. Child process only. + #[cfg(unix)] + fn break_store_writes(world: &EngineFixture) { + let size = std::fs::metadata(&world.state_store).unwrap().len(); + unsafe { + libc::signal(libc::SIGXFSZ, libc::SIG_IGN); + let limit = libc::rlimit { rlim_cur: size - 1, rlim_max: libc::RLIM_INFINITY }; + assert_eq!(libc::setrlimit(libc::RLIMIT_FSIZE, &limit), 0); + } + assert!( + set_meters(world, &TargetSupervision::new("probe")).is_err(), + "the store is still writable" + ); + assert!(ControlSnapshot::read(&world.state_store).is_ok(), "the store is unreadable"); + } + + #[cfg(unix)] + fn challenge_due(world: &EngineFixture) -> Option { + world.engine.challenge_wait.lock().unwrap().get("service").map(|wait| wait.due_unix_millis) + } + + #[cfg(unix)] + #[test] + fn an_unwritable_store_neither_spins_a_failing_challenge_nor_reports_progress() -> Result<()> { + let name = "an_unwritable_store_neither_spins_a_failing_challenge_nor_reports_progress"; + let Some(stderr) = in_unwritable_store_child(name)? else { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.world.route_stubs.refuse_reloads()?; + routed.drift()?; + let before = routed.reloads(); + let began = now_millis()?; + break_store_writes(&routed.world); + let stored_wait = |routed: &RoutedWorld| -> Result> { + Ok(ControlSnapshot::read(&routed.world.state_store)? + .admitted + .iter() + .find_map(|stored| stored.value.route_supervision.as_ref()) + .and_then(|state| state.next_challenge_at_unix_millis)) + }; + let stored = stored_wait(&routed)?; + + // Fifty laps: one attempt, nothing reported as progress. + for _ in 0..50 { + assert!(!routed.tick()?, "a write the store refused was reported as progress"); + } + assert_eq!(routed.reloads() - before, 1, "the failing reload spun"); + assert_eq!(stored_wait(&routed)?, stored, "the record changed though writes fail"); + let due = challenge_due(&routed.world).expect("no in-memory wait after a failed record"); + let age = routed.world.engine.options.topology_maximum_age_millis; + assert!(due >= began + age, "the wait is shorter than one observation age: {due}"); + + // The wait expiring is the only thing that admits another + // attempt, and each one is again a single reload with no + // progress. The failed record is said once, not per attempt. + for attempt in 2..=4 { + routed.world.engine.challenge_wait.lock().unwrap().clear(); + assert!(!routed.tick()?); + assert!(!routed.tick()?); + assert_eq!(routed.reloads() - before, attempt); + } + return Ok(()); + }; + assert_eq!( + stderr.matches("could not record the failed route challenge of service").count(), + 1, + "{stderr}" + ); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn an_unwritable_store_challenges_a_stale_route_once_per_wait() -> Result<()> { + let name = "an_unwritable_store_challenges_a_stale_route_once_per_wait"; + let Some(stderr) = in_unwritable_store_child(name)? else { + let mut routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.world.engine.options.topology_maximum_age_millis = 8_000; + std::thread::sleep(Duration::from_millis(8_300)); + break_store_writes(&routed.world); + assert!(!routed.tick()?, "a write the store refused was reported as progress"); + let first = challenge_due(&routed.world).expect("the proved challenge left no wait"); + for _ in 0..5 { + std::thread::sleep(Duration::from_millis(3)); + assert!(!routed.tick()?); + } + assert_eq!(challenge_due(&routed.world), Some(first), "the route was challenged again"); + return Ok(()); + }; + assert_eq!( + stderr.matches("could not record the proved route challenge of service").count(), + 1, + "{stderr}" + ); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_landed_challenge_record_is_progress_and_leaves_no_in_memory_wait() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.world.route_stubs.refuse_reloads()?; + routed.drift()?; + let snapshot = ControlSnapshot::read(&routed.world.state_store)?; + routed.world.engine.challenge_wait.lock().unwrap().insert( + "service".into(), + ChallengeWait { due_unix_millis: 0, stamp: snapshot.target_stamp("service") }, + ); + assert!(routed.tick()?, "a landed record is progress"); + assert_eq!(challenge_due(&routed.world), None); + Ok(()) + } + + /// A fragment that re-drifts after every restore, with the store + /// unwritable, is reloaded once per wait, however many laps run. + #[cfg(unix)] + #[test] + fn a_re_drifting_fragment_is_reloaded_once_per_wait_when_the_store_is_unwritable() -> Result<()> { + let name = "a_re_drifting_fragment_is_reloaded_once_per_wait_when_the_store_is_unwritable"; + let Some(_) = in_unwritable_store_child(name)? else { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + let before = routed.reloads(); + break_store_writes(&routed.world); + for _ in 0..30 { + routed.drift()?; + assert!(!routed.tick()?, "a refused write was reported as progress"); + } + assert_eq!(routed.reloads() - before, 1, "a re-drifting fragment reloaded every lap"); + for attempt in 2..=4 { + routed.world.engine.challenge_wait.lock().unwrap().clear(); + routed.drift()?; + assert!(!routed.tick()?); + routed.drift()?; + assert!(!routed.tick()?); + assert_eq!(routed.reloads() - before, attempt); + } + return Ok(()); + }; + Ok(()) + } + + /// The wait of a proved challenge whose record failed is one + /// observation age from the attempt. + #[cfg(unix)] + #[test] + fn a_proved_challenge_that_cannot_be_recorded_waits_one_observation_age() -> Result<()> { + let name = "a_proved_challenge_that_cannot_be_recorded_waits_one_observation_age"; + let Some(_) = in_unwritable_store_child(name)? else { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + let before = routed.reloads(); + break_store_writes(&routed.world); + let began = now_millis()?; + routed.drift()?; + assert!(!routed.tick()?); + assert_eq!(routed.reloads() - before, 1, "the repair did not run"); + let due = challenge_due(&routed.world).expect("no wait"); + let age = routed.world.engine.options.topology_maximum_age_millis; + assert!(due >= began + age, "the wait is shorter than one age: {}", due - began); + return Ok(()); + }; + Ok(()) + } + + /// The stored schedule sets the in-memory wait: with three failures + /// recorded, the fourth failed challenge waits eight ages (capped), and + /// no repair runs inside that wait. + #[cfg(unix)] + #[test] + fn a_failed_challenge_that_cannot_be_recorded_waits_as_long_as_the_stored_schedule() -> Result<()> { + let name = "a_failed_challenge_that_cannot_be_recorded_waits_as_long_as_the_stored_schedule"; + let Some(_) = in_unwritable_store_child(name)? else { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.stub.hang_up.store(true, Ordering::SeqCst); + edit_incumbent(&routed.world, |generation| { + let state = generation.route_supervision.as_mut().unwrap(); + state.consecutive_failures = 3; + state.next_challenge_at_unix_millis = Some(1); + state.degraded_since_unix_millis = Some(1); + })?; + break_store_writes(&routed.world); + let began = now_millis()?; + routed.drift()?; + assert!(!routed.tick()?); + let due = challenge_due(&routed.world).expect("no wait"); + let age = routed.world.engine.options.topology_maximum_age_millis; + let want = (age * 8).min(ROUTE_CHALLENGE_BACKOFF_CAP_MILLIS); + assert!(due >= began + want, "the wait ignores the stored doubling: {}", due - began); + let reloads = routed.reloads(); + for _ in 0..10 { + routed.drift()?; + assert!(!routed.tick()?); + } + assert_eq!(routed.reloads(), reloads, "the route was repaired inside its wait"); + return Ok(()); + }; + Ok(()) + } + + /// A lost race is progress, and the next pass is governed by the + /// winner's record, not by an in-memory wait of the loser. + #[cfg(unix)] + #[test] + fn a_lost_race_leaves_no_in_memory_wait() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.world.route_stubs.refuse_reloads()?; + let old = ControlSnapshot::read(&routed.world.state_store)?; + let stale = old.admitted_for("service").context("no generation")?; + edit_incumbent(&routed.world, |generation| { + generation.route_supervision.as_mut().unwrap().last_challenge_at_unix_millis = Some(5); + })?; + routed.drift()?; + assert!(routed.world.engine.supervise_admitted_route(&old, stale)?); + assert_eq!(challenge_due(&routed.world), None, "a lost race left an in-memory wait"); + Ok(()) + } + + /// The wait left by a full disk does not outlive it: once any write for + /// the target lands, the stored schedule owns the target again and a + /// drift is repaired without waiting. + #[cfg(unix)] + #[test] + fn a_wait_left_by_a_full_disk_ends_when_any_write_for_the_target_lands() -> Result<()> { + let name = "a_wait_left_by_a_full_disk_ends_when_any_write_for_the_target_lands"; + let Some(_) = in_unwritable_store_child(name)? else { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + let before = routed.reloads(); + break_store_writes(&routed.world); + routed.drift()?; + assert!(!routed.tick()?); + assert!(challenge_due(&routed.world).is_some(), "no wait to outlive"); + assert_eq!(routed.reloads() - before, 1); + + // Space is freed. Nothing has landed yet, so the wait still holds. + unsafe { + let limit = libc::rlimit { rlim_cur: libc::RLIM_INFINITY, rlim_max: libc::RLIM_INFINITY }; + assert_eq!(libc::setrlimit(libc::RLIMIT_FSIZE, &limit), 0); + } + routed.drift()?; + assert!(!routed.tick()?); + assert_eq!(routed.reloads() - before, 1, "the wait did not hold before any write landed"); + + // Something else writes for the target: the wait is over. + routed.ledger(1)?; + routed.drift()?; + assert!(routed.tick()?, "the drift was not repaired"); + assert_eq!(routed.reloads() - before, 2, "a stale wait held the repair"); + assert_eq!(challenge_due(&routed.world), None); + return Ok(()); + }; + Ok(()) + } + + /// A wait belongs to an admitted target: once the target is retired, + /// the wait is gone. + #[cfg(unix)] + #[test] + fn a_wait_ends_when_its_target_is_retired() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + let snapshot = ControlSnapshot::read(&routed.world.state_store)?; + routed.world.engine.challenge_wait.lock().unwrap().insert( + "service".into(), + ChallengeWait { + due_unix_millis: now_millis()? + 500_000, + stamp: snapshot.target_stamp("service"), + }, + ); + assert!(!routed.tick()?); + assert!(challenge_due(&routed.world).is_some(), "an unchanged store dropped the wait"); + assert!( + SingleFileMessagePackBackingStore::new(&routed.world.state_store) + .delete_batch_if_unchanged(&[snapshot.admitted_for("service").unwrap().envelope.clone()])? + ); + routed.tick()?; + assert_eq!(challenge_due(&routed.world), None, "a retired target kept its wait"); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_full_ledger_still_restores_the_admitted_and_incumbent_route() -> Result<()> { + let full = ROUTE_ACTUATION_CEILING; + let newest = |world: &EngineFixture| -> Result { + let meters = meters_of(world, "service")?; + assert_eq!(meters.route_actuations.len(), ROUTE_ACTUATION_CEILING); + Ok(*meters.route_actuations.last().unwrap()) + }; + + // (i) A post-fence abort withdraws the candidate's membership. + let aborting = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + aborting.ledger(full)?; + let before = newest(&aborting.world)?; + aborting + .world + .engine + .begin_post_fencing_abort(&resident(&aborting.world)?, anyhow!("test"))?; + aborting.step()?; + assert_eq!(aborting.reloads(), 1, "the withdrawal did not run"); + assert!(newest(&aborting.world)? > before, "the withdrawal was not counted"); + + // (ii) Supervision repairs a drifted admitted fragment. + let supervised = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + supervised.admit()?; + supervised.ledger(full)?; + let (before, reloads) = (newest(&supervised.world)?, supervised.reloads()); + supervised.drift()?; + assert!(supervised.tick()?); + assert_eq!(supervised.reloads(), reloads + 1, "the repair did not run"); + assert!(newest(&supervised.world)? > before, "the repair was not counted"); + assert!(admitted(&supervised)?.route_supervision.unwrap().degraded_since_unix_millis.is_none()); + + // (iii) A failed proof at Routing rolls the route back, though the + // install that preceded it took the last free slot. + let failing = routed_deploy_world(Odin::Unreachable, 1, DeploymentPhase::Fencing)?; + failing.stub.set_state("active"); + failing.run_to_routing()?; + failing.ledger(full - 1)?; + failing.stub.hang_up.store(true, Ordering::SeqCst); + let before = now_millis()?; + assert!(failing.step().is_err(), "an unanswered stable route was admitted"); + assert_eq!(failing.reloads(), 2, "install then rollback"); + assert!(newest(&failing.world)? >= before, "the rollback was not counted"); + assert!(!failing.fragment_path().exists(), "the rollback left the candidate's route"); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_continuity_restart_is_never_refused_by_a_full_ledger() -> Result<()> { + // Warming: the preflight of a continuity runs and is counted. + let warming = routed_world(Odin::Unreachable, 1, DeploymentPhase::Warming, false)?; + warming.step()?; + assert!(warming.transaction()?.warming.is_some()); + warming.ledger(ROUTE_ACTUATION_CEILING)?; + let before = *meters_of(&warming.world, "service")?.route_actuations.last().unwrap(); + warming.step()?; + assert!(warming.transaction()?.route_preflight.is_some(), "the preflight was refused"); + assert!(*meters_of(&warming.world, "service")?.route_actuations.last().unwrap() > before); + + // Routing: its install runs and is counted. + let routing = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routing.stub.set_state("active"); + routing.run_to_routing()?; + routing.ledger(ROUTE_ACTUATION_CEILING)?; + let before = *meters_of(&routing.world, "service")?.route_actuations.last().unwrap(); + routing.step()?; + assert!(routing.reloads() >= 1, "the install did not run"); + assert!(routing.fragment_path().exists()); + assert!(*meters_of(&routing.world, "service")?.route_actuations.last().unwrap() > before); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_survival_that_cannot_be_counted_runs_and_a_forward_is_refused() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + let transaction = routed.transaction()?; + let preflight = transaction.route_preflight.clone().context("no preflight")?; + let driver = routed.world.engine.route_driver( + transaction + .plan + .as_ref() + .unwrap() + .parsed_inputs()? + .1 + .route + .context("no route binding")?, + ); + // The ledger cannot be read, so nothing can be counted. + std::fs::write(&routed.world.state_store, b"unreadable")?; + let engine = &routed.world.engine; + + let refused = engine + .route_gate("service", CommandKind::Deploy) + .admit(RouteActuation::Forward) + .unwrap_err(); + assert!(refused.downcast_ref::().is_none(), "{refused:#}"); + assert!(engine.charge_route_actuation("service", RouteActuation::Forward).is_err()); + + engine.charge_route_actuation("service", RouteActuation::Survival)?; + driver.withdraw_candidate_membership( + &preflight, + &engine.route_gate("service", CommandKind::Deploy), + )?; + assert_eq!(routed.reloads(), 1, "the uncounted withdrawal did not run"); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_second_failed_challenge_waits_twice_as_long_before_the_next_repair() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.stub.hang_up.store(true, Ordering::SeqCst); + let elapse = |routed: &RoutedWorld| { + edit_incumbent(&routed.world, |generation| { + generation.route_supervision.as_mut().unwrap().next_challenge_at_unix_millis = + Some(1); + }) + }; + + routed.drift()?; + assert!(routed.tick()?); + elapse(&routed)?; + routed.drift()?; + assert!(routed.tick()?); + let state = admitted(&routed)?.route_supervision.context("no route state")?; + assert_eq!(state.consecutive_failures, 2); + assert_eq!( + state.next_challenge_at_unix_millis.unwrap() - state.last_challenge_at_unix_millis.unwrap(), + 2 * DEFAULT_TOPOLOGY_MAXIMUM_AGE_MILLIS + ); + + // The wait is longer than one observation age: no challenge, so no + // repair and no reload, however often the pass runs. + let reloads = routed.reloads(); + for _ in 0..20 { + routed.drift()?; + routed.tick()?; + } + assert_eq!(routed.reloads(), reloads, "the route was repaired inside its wait"); + assert_eq!( + admitted(&routed)?.route_supervision.unwrap().consecutive_failures, + 2 + ); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_proved_challenge_that_cannot_be_recorded_ends_the_pass_without_failing_it() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + let old = ControlSnapshot::read(&routed.world.state_store)?; + let stale = old.admitted_for("service").context("no generation")?; + // Something else writes the generation after the pass read it. + edit_incumbent(&routed.world, |generation| { + generation.route_supervision.as_mut().unwrap().last_challenge_at_unix_millis = Some(5); + })?; + routed.drift()?; + assert!(routed.world.engine.supervise_admitted_route(&old, stale)?); + assert_eq!( + admitted(&routed)?.route_supervision.unwrap().last_challenge_at_unix_millis, + Some(5), + "the stale write replaced the newer generation" + ); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_first_routed_deploy_is_metered_and_refused_at_the_ceiling() -> Result<()> { + let metered = routed_deploy_world(Odin::Unreachable, 1, DeploymentPhase::Fencing)?; + metered.stub.set_state("active"); + metered.run_to_routing()?; + assert!(ControlSnapshot::read(&metered.world.state_store)?.targets.is_empty()); + metered.step()?; + assert_eq!(meters_of(&metered.world, "service")?.route_actuations.len(), 1); + + let refused = routed_deploy_world(Odin::Unreachable, 1, DeploymentPhase::Fencing)?; + refused.stub.set_state("active"); + refused.run_to_routing()?; + refused.ledger(ROUTE_ACTUATION_CEILING)?; + let error = refused.step().unwrap_err(); + assert!(error.downcast_ref::().is_some(), "{error:#}"); + assert_eq!(refused.world.route_stubs.count(""), 0, "a refused install ran a program"); + assert!(!refused.fragment_path().exists()); + + // Past the fence the refusal is resumable, not an abort: the resume + // backoff spaces it and no actuation runs while it waits. + assert!(!refused.world.engine.resume_one_transaction()?); + let waiting = refused.transaction()?; + assert_eq!(waiting.phase, DeploymentPhase::Routing); + assert!(waiting.post_fencing_abort.is_none()); + assert!(waiting.last_error.as_deref().is_some_and(|text| text.contains("ceiling"))); + assert_eq!(refused.world.route_stubs.count(""), 0); + Ok(()) + } + + #[test] + fn a_stepped_back_clock_is_settled_where_the_decision_is_made() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + let ahead = now_millis()? + 86_400_000; + + // A supervision pass that would decide from the restart log settles + // it first: the write ends the pass, and nothing is minted from + // entries that only look recent because the clock stepped back. + let mut restarts = TargetSupervision::new("service"); + restarts.continuity_restarts = vec![ahead; CONTINUITY_RESTART_ATTEMPTS]; + set_meters(&routed.world, &restarts)?; + routed.workload.kill(); + assert!(routed.tick()?); + assert!(ControlSnapshot::read(&routed.world.state_store)?.transactions.is_empty()); + let settled = meters_of(&routed.world, "service")?; + assert!(settled.continuity_restarts.iter().all(|&at| at < ahead)); + + // A refused forward actuation writes the clamp, so the window runs + // from now and not from the day the clock stood on. + let mut ledger = TargetSupervision::new("service"); + ledger.route_actuations = vec![ahead; ROUTE_ACTUATION_CEILING]; + set_meters(&routed.world, &ledger)?; + let refused = routed + .world + .engine + .charge_route_actuation("service", RouteActuation::Forward) + .unwrap_err(); + assert!(refused.downcast_ref::().is_some()); + let settled = meters_of(&routed.world, "service")?; + assert!(settled.route_actuations.iter().all(|&at| at < ahead)); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_refused_preflight_starts_no_private_mount_unit_and_fails_at_once_with_the_reopen_time() + -> Result<()> { + let routed = routed_deploy_world(Odin::Unreachable, 1, DeploymentPhase::Warming)?; + routed.step()?; + assert!(routed.transaction()?.warming.is_some()); + routed.ledger(ROUTE_ACTUATION_CEILING)?; + let reopens_at = meters_of(&routed.world, "service")? + .route_reopens_at(now_millis()?) + .context("the ledger is not full")?; + + // The refusal is an error of its own phase, before the fence: the + // deployment fails now, and its error carries the reopen time. + let id = routed.transaction()?.transaction_id; + routed.world.engine.resume_one_transaction()?; + let abort = routed + .transaction()? + .pre_fencing_abort + .context("the refusal did not abort the deployment")?; + assert!(abort.error.contains(&format!("reopens at unix ms {reopens_at}")), "{}", abort.error); + for _ in 0..8 { + if record_of(&routed.world, &id)?.completion.is_some() { + break; + } + routed.step()?; + } + let Some(TransactionCompletion::FailedBeforeFencing { error }) = + record_of(&routed.world, &id)?.completion + else { + let record = record_of(&routed.world, &id)?; + bail!( + "the deployment did not fail before the fence: phase {:?} completion {:?} abort {:?} error {:?}", + record.phase, record.completion, record.pre_fencing_abort, record.last_error + ); + }; + assert!(error.contains(&format!("reopens at unix ms {reopens_at}")), "{error}"); + assert_eq!(routed.world.route_stubs.count(""), 0, "a program ran"); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_failed_challenge_and_a_dead_unit_in_one_tick_do_not_lose_a_cas() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.workload.kill(); + routed.stub.hang_up.store(true, Ordering::SeqCst); + routed.drift()?; + + // Tick 1: the repair charges the target's meters and the proof + // fails. Both are writes, so they end the pass: nothing else is + // decided from the snapshot they made stale. + assert!(routed.tick()?); + let state = admitted(&routed)?.route_supervision.context("no route state")?; + assert_eq!(state.consecutive_failures, 1); + assert!(state.degraded_since_unix_millis.is_some()); + assert!(ControlSnapshot::read(&routed.world.state_store)?.transactions.is_empty()); + assert_eq!(meters_of(&routed.world, "service")?.route_actuations.len(), 2); + + // Tick 2: the route waits, and the dead unit is restarted from a + // fresh read, counted in the same write that mints it. + assert!(routed.tick()?); + assert_eq!(ControlSnapshot::read(&routed.world.state_store)?.transactions.len(), 1); + assert_eq!(meters_of(&routed.world, "service")?.continuity_restarts.len(), 1); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_proved_challenge_clears_the_degradation() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.stub.hang_up.store(true, Ordering::SeqCst); + routed.drift()?; + assert!(routed.tick()?); + let degraded = admitted(&routed)?.route_supervision.context("no route state")?; + assert_eq!(degraded.consecutive_failures, 1); + assert!(degraded.degraded_since_unix_millis.is_some()); + assert!(degraded.next_challenge_at_unix_millis.is_some()); + + routed.stub.hang_up.store(false, Ordering::SeqCst); + edit_incumbent(&routed.world, |generation| { + generation.route_supervision.as_mut().unwrap().next_challenge_at_unix_millis = Some(1); + })?; + routed.drift()?; + assert!(routed.tick()?); + let healed = admitted(&routed)?.route_supervision.context("no route state")?; + assert_eq!(healed.consecutive_failures, 0); + assert_eq!(healed.next_challenge_at_unix_millis, None); + assert_eq!(healed.degraded_since_unix_millis, None); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_failing_reload_that_deletes_the_fragment_backs_the_route_off() -> Result<()> { + let routed = routed_world(Odin::Unreachable, 1, DeploymentPhase::Fencing, false)?; + routed.admit()?; + routed.world.route_stubs.refuse_reloads()?; + let reloads = routed.reloads(); + routed.drift()?; + for _ in 0..200 { + routed.tick()?; + } + assert_eq!(routed.reloads() - reloads, 1, "the route was reloaded again and again"); + assert!(!routed.fragment_path().exists()); + assert!(admitted(&routed)?.route_supervision.unwrap().degraded_since_unix_millis.is_some()); + Ok(()) + } + + #[cfg(unix)] + #[test] + fn a_candidate_whose_route_proof_fails_reloads_at_most_the_ceiling() -> Result<()> { + let routed = routed_deploy_world(Odin::Unreachable, 1, DeploymentPhase::Fencing)?; + routed.stub.set_state("active"); + routed.run_to_routing()?; + routed.stub.hang_up.store(true, Ordering::SeqCst); + let mut last = None; + for _ in 0..30 { + last = Some(routed.step().unwrap_err()); + } + // Every install is followed by its rollback and both are counted, so + // twelve reloads spend the window; the rest are refused. + assert_eq!(routed.reloads(), ROUTE_ACTUATION_CEILING); + let last = last.unwrap(); + assert!(last.downcast_ref::().is_some(), "{last:#}"); + Ok(()) + } } } diff --git a/src/drivers.rs b/src/drivers.rs index 8743b94..af18a80 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -5055,15 +5055,82 @@ 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, and every actuation of a +/// continuity, is survival, not deployment, so a `Survival` is always admitted +/// and counted whenever the ledger can record it. +#[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 +5332,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 +5349,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 +5364,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 +5382,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 +5412,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 +5431,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 +5441,7 @@ impl NginxRouteDriver { preflight.incumbent_configuration.as_deref(), error, "candidate route validation or reload failed", + gate, ); } return Err(error).context( @@ -5406,13 +5480,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 +5499,7 @@ impl NginxRouteDriver { &self, expected: &IdunnExpectedIncarnationRecord, membership_sha256: &str, + gate: &dyn RouteActuationGate, ) -> Result<()> { let rendered = self.render(expected)?; ensure!( @@ -5430,6 +5509,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 +5637,7 @@ impl NginxRouteDriver { expected: &IdunnExpectedIncarnationRecord, runtime_instance_id: &str, preflight: &RoutePreflightReceipt, + gate: &dyn RouteActuationGate, ) -> Result<()> { preflight.validate()?; let rendered = self.render(expected)?; @@ -5570,7 +5651,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) } } @@ -8254,6 +8335,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. @@ -11570,13 +11660,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(()) } @@ -11636,22 +11873,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(()) @@ -11773,13 +12010,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" @@ -11801,24 +12038,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)?); @@ -11826,12 +12063,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(()) } diff --git a/tests/fixtures/idunn-control-legacy/README.md b/tests/fixtures/idunn-control-legacy/README.md index d1b00ec..3d420f3 100644 --- a/tests/fixtures/idunn-control-legacy/README.md +++ b/tests/fixtures/idunn-control-legacy/README.md @@ -42,3 +42,21 @@ borrowed from `transaction-fencing`. | `transaction-pre-fence-abort-terminal` | the same abort finished: Complete, `FailedBeforeFencing` | | `transaction-post-fence-abort` | Fencing continuity, post-fencing abort in flight | | `provider-anchor` | one line: the provider trust-anchor file bytes, hex. A test writes it to the fixed path so the Engine can read the plan's anchor | + +## Generation v3 (the B2 lift) + +`generation-v3-odin` and `generation-v3-route-proof` are the exact +`idunn.admitted_generation.v3` payloads the encoder at Idunn `583b2a7` wrote +(`admitted_envelope` over `rmp_serde::to_vec`), printed by a throwaway test that +is not kept. The first is the v2 `generation-with-receipts` record lifted by that +revision's own `LegacyAdmittedGeneration::into_current`, the second is the +generation `route_proof::committed_generation` admits through a real route-proof +deployment. Both are synthetic. + +| File | Schema | What it is | +|---|---|---| +| `generation-v3-odin` | `idunn.admitted_generation.v3` | routed, Odin-correlated generation | +| `generation-v3-route-proof` | `idunn.admitted_generation.v3` | routed, route-proof generation, no Odin receipts | + +`control_plane.rs` decodes them through `LegacyAdmittedGenerationV3`; the +fixtures die with that struct. diff --git a/tests/fixtures/idunn-control-legacy/generation-v3-odin.hex b/tests/fixtures/idunn-control-legacy/generation-v3-odin.hex new file mode 100644 index 0000000..23e87fa --- /dev/null +++ b/tests/fixtures/idunn-control-legacy/generation-v3-odin.hex @@ -0,0 +1,2 @@ +idunn.admitted_generation.v3 +dc0015bc6964756e6e2e61646d69747465645f67656e65726174696f6e2e7633a773657276696365d93267656e65726174696f6e2d74782d37636236616537392d323635302d346261322d623664392d643137333338643566393031af636f6e74696e756974792d74657374d92774782d37636236616537392d323635302d346261322d623664392d643137333338643566393031cf0000018bcfe6eea099d9216964756e6e2e636f6d70696c65645f6465706c6f796d656e745f706c616e2e7632d9477368613235362d64396364646465626366636362326366316261396164366239323566336666616237646561636332366661653631646138663236353861336139343039656232b5736572766963652d696e6361726e6174696f6e2d316e9bbf6964756e6e2e736f757263655f73656c656374696f6e5f66616374732e7631d92768747470733a2f2f6769746875622e636f6d2f47616d6543756c742f536572766963652e676974af726566732f68656164732f6d61696ed92832323232323232323232323232323232323232323232323232323232323232323232323232323232d92832323232323232323232323232323232323232323232323232323232323232323232323232323232d92833333333333333333333333333333333333333333333333333333333333333333333333333333333bc6465706c6f796d656e742f6964756e6e2f7265636970652e746f6d6cd9477368613235362d353962636334323766373939343035636134613531326232386138303637343435366661333134393732333631323830656130626332316363356532663635338091a87265662d6865616464dc058e0a736368656d61203d202267616d6563756c742e6964756e6e2e7461726765745f6465636c61726174696f6e2e7631220a746172676574203d202273657276696365220a736f757263655f7374616d705f656e7669726f6e6d656e74203d2022534552564943455f4255494c445f434f4d4d4954220a0a5b5b73746570735d5d0a6964203d20226275696c64220a7068617365203d20226275696c64220a72756e6e6572203d202272757374220a61726776203d205b22636172676f222c20226275696c64222c20222d2d6c6f636b6564225d0a0a5b5b65787465726e616c5f696e707574735d5d0a6964203d2022746f6f6c636861696e2d696e646578220a75726c203d202268747470733a2f2f6578616d706c652e696e76616c69642f746f6f6c636861696e2d696e646578220a736861323536203d202264646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464220a72756e6e6572203d202272757374220a64657374696e6174696f6e203d2022696e707574732f746f6f6c636861696e2d696e646578220a0a5b5b6172746966616374735d5d0a6964203d20226461656d6f6e220a736f757263655f6b696e64203d202272756e6e65722d6f7574707574220a72756e6e6572203d202272757374220a736f75726365203d20227461726765742f72656c656173652f73657276696365220a64657374696e6174696f6e203d202273657276696365220a65787065637465645f736861323536203d202263636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363220a65786563757461626c65203d20747275650a0a5b736572766963655d0a65786563757461626c655f6172746966616374203d20226461656d6f6e220a617267756d656e7473203d205b0a20207b206b696e64203d202262696e64696e67222c206e616d65203d202273746174655f726f6f7422207d2c0a5d0a7472616e73706f7274203d202268747470220a726f7574655f7265717569726564203d20747275650a72657175697265645f656e7669726f6e6d656e74203d205b2247414d4543554c545f4944554e4e5f43414e4449444154455f42494e44222c202247414d4543554c545f4944554e4e5f52554e54494d455f42554e444c45225d0a0a5b736572766963652e6865616c74685d0a636f6e7472616374203d2022736572766963652e6865616c7468220a0a5b73746174655d0a736368656d615f67656e65726174696f6e203d20227631220a0a5b5b73746174652e736c6f74735d5d0a6964203d202272756e74696d652d6361636865220a72656c61746976655f70617468203d202272756e74696d652d63616368652e6363220a6b696e64203d202263756c7463616368652d66696c65220a736368656d61203d2022736572766963652e72756e74696d652d63616368652e7631220a777269746572203d20226e6f6e65220a7265636f76657279203d202272656275696c6461626c65220a73746172747570203d20226f70656e2d61742d7374617274220a0a5b5b70726f76696465735d5d0a6361706162696c697479203d2022736572766963652e72756e74696d65220a736368656d61203d2022736572766963652e72756e74696d652e7631220a636f6d7061746962696c697479203d20227631220a0a5b5b646570656e64656e636965735d5d0a6b696e64203d20227368617265642d696e667261737472756374757265220a6361706162696c697479203d20226f64696e2e76657273652d72656e64657a766f7573220a736368656d61203d20226f64696e2e76657273652d746f706f6c6f67792e7631220a636f6d7061746962696c697479203d20227631220adc076e0a736368656d61203d202267616d6563756c742e6964756e6e2e6f70657261746f725f62696e64696e672e7632220a746172676574203d202273657276696365220a0a5b7265706f7369746f72795d0a6f726967696e203d202268747470733a2f2f6769746875622e636f6d2f47616d6543756c742f536572766963652e676974220a61646d69747465645f726566203d2022726566732f68656164732f6d61696e220a6d696e696d756d5f7265766973696f6e203d202231313131313131313131313131313131313131313131313131313131313131313131313131313131220a73656c656374696f6e203d20227265662d68656164220a636865636b6f7574203d20222f7372762f6275696c642f53657276696365220a7265636970655f70617468203d20226465706c6f796d656e742f6964756e6e2f7265636970652e746f6d6c220a0a5b72756e6e6572732e727573745d0a647269766572203d2022646f636b6572220a696d616765203d202272757374407368613235363a61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161220a75736572203d2022313030303a31303030220a6166666f7264616e636573203d205b22736f757263652d72656164222c202261727469666163742d7772697465225d0a616c6c6f7765645f70726f6772616d73203d205b22636172676f225d0a6e6574776f726b5f70726f66696c65203d20226275696c642d646570656e64656e63792d656772657373220a6d656d6f72795f6d6562696279746573203d20323034380a6370755f71756f74615f70657263656e74203d203230300a706964735f6c696d6974203d203531320a746d7066735f6d6562696279746573203d203531320a0a5b776f726b6c6f61645d0a647269766572203d202273797374656d642d7472616e7369656e74220a73746174655f67726f7570203d202273657276696365220a756e69745f707265666978203d20226964756e6e2d73657276696365220a72656c656173655f726f6f74203d20222f7372762f736572766963652f72656c6561736573220a73746174655f726f6f74203d20222f7661722f6c69622f67616d6563756c742f73657276696365220a72756e74696d655f726f6f74203d20222f72756e2f67616d6563756c742f6964756e6e2f73657276696365220a6e6574776f726b203d2022686f73742d70726976617465220a68617264656e696e67203d2022737472696374220a6d656d6f72795f6d6562696279746573203d20313032340a6370755f71756f74615f70657263656e74203d203130300a0a5b776f726b6c6f61642e617267756d656e745f62696e64696e67735d0a73746174655f726f6f74203d20222f7661722f6c69622f67616d6563756c742f73657276696365220a0a5b776f726b6c6f61642e7365637265745f66696c65735d0a47414d4543554c545f52554e54494d455f50524553454e43455f4944454e54495459203d20222f6574632f67616d6563756c742f736572766963652f72756e74696d652d70726573656e63652d6964656e746974792e6363220a0a5b72756e74696d655f6964656e746974795d0a72756e74696d655f6964203d2022736572766963652d79676764726173696c220a65787065637465645f7369676e65725f6964656e746974795f6964203d2022736572766963652d72756e74696d652d7369676e6572220a74727573745f616e63686f725f73746f7265203d20222f6574632f67616d6563756c742f74727573742f736572766963652e6363220a0a5b726f7574655d0a647269766572203d20226e67696e782d73747265616d2d746370220a726f7574655f6964203d202273657276696365220a737461626c655f656e64706f696e74203d2022687474703a2f2f3132372e302e302e313a3137393939220a707269766174655f686f7374203d20223132372e302e302e31220a707269766174655f706f72745f7374617274203d2031383030300a707269766174655f706f72745f656e64203d2031383030390a636f6e6669675f70617468203d20222f6574632f6e67696e782f6964756e6e2d73747265616d2d726f757465732f736572766963652e636f6e66220a72656c6f61645f756e6974203d20226e67696e782e73657276696365220a0a5b6272616b65735d0a6465706c6f796d656e745f73746f7265203d20222f7661722f6c69622f67616d6563756c742f6964756e6e2f736572766963652d6465706c6f796d656e742d6272616b652e6363220a6c6966656379636c655f73746f7265203d20222f7661722f6c69622f67616d6563756c742f6964756e6e2f736572766963652d6c6966656379636c652d6272616b652e6363220a0a5b726f6c6c6f75745d0a7374726174656779203d202263616e6469646174652d7468656e2d70726f6d6f7465220a647261696e5f7365636f6e6473203d2033300a72657461696e5f72656c6561736573203d20320a0a5b706c6163656d656e745d0a646573697265645f7265706c69636173203d20310a6e6f646573203d205b2279676764726173696c225d0a919296b57368617265642d696e667261737472756374757265b56f64696e2e76657273652d72656e64657a766f7573b66f64696e2e76657273652d746f706f6c6f67792e7631a2763101b06265666f72652d70726f6d6f74696f6e97a46f64696e98ad6d616e616765642d7265616479a46f64696eb26f64696e2d696e6361726e6174696f6e2d31d9477368613235362d61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161d9477368613235362d65656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565d9477368613235362d63343263333331373863366539666534633435313162623563663962626461376431633138356536646330373064323234653463653431303538626338633238d9477368613235362d6137313730323730313265383236396235666636363961313135386466636236313764346463636230663963333633656631373738656561633230353332633907b56f64696e2e76657273652d72656e64657a766f7573b66f64696e2e76657273652d746f706f6c6f67792e7631a2763101b57463703a2f2f31302e37372e302e313a3137383731cd465196b76964756e6e2e7365616c65645f72656c656173652e7631d9477368613235362d35663738353165373634333366383261376562666431626232646234323065366263333263646263346536666538373563356633326461363434643535656237d9477368613235362d643963646464656263666363623263663162613961643662393235663366666162376465616363323666616536316461386632363538613361393430396562329195a66461656d6f6ea773657276696365d9477368613235362d636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363632ac39196af746f6f6c636861696e2d696e646578d92768747470733a2f2f6578616d706c652e696e76616c69642f746f6f6c636861696e2d696e646578d9477368613235362d64646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464a472757374b6696e707574732f746f6f6c636861696e2d696e646578117892d9477368613235362d35663738353165373634333366383261376562666431626232646234323065366263333263646263346536666538373563356633326461363434643535656237bd2f7372762f736572766963652f72656c65617365732f63757272656e74dc0012bd6964756e6e2e65787065637465645f696e6361726e6174696f6e2e7632a773657276696365d9477368613235362d64396364646465626366636362326366316261396164366239323566336666616237646561636332366661653631646138663236353861336139343039656232b5736572766963652d696e6361726e6174696f6e2d31d9477368613235362d35663738353165373634333366383261376562666431626232646234323065366263333263646263346536666538373563356633326461363434643535656237bb6769746875622e636f6d2f47616d6543756c742f53657276696365d92832323232323232323232323232323232323232323232323232323232323232323232323232323232d9477368613235362d35396263633432376637393934303563613461353132623238613830363734343536666133313439373233363132383065613062633231636335653266363533b1736572766963652d79676764726173696cb6736572766963652d72756e74696d652d7369676e6572ae736572766963652e6865616c7468d9477368613235362d63636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363a27631d9477368613235362d32663837303134316366353638316566656332653234316364636435663731303831616539326162316535633534633839636438393666386335656236323731c294a773657276696365a468747470b6687474703a2f2f3132372e302e302e313a3137393939b6687474703a2f2f3132372e302e302e313a31383030319194af736572766963652e72756e74696d65b2736572766963652e72756e74696d652e7631a2763101919ab57368617265642d696e667261737472756374757265b56f64696e2e76657273652d72656e64657a766f7573b66f64696e2e76657273652d746f706f6c6f67792e7631a2763101b06265666f72652d70726f6d6f74696f6ea46f64696eb36d616e616765642d696e6361726e6174696f6ed9477368613235362d63343263333331373863366539666534633435313162623563663962626461376431633138356536646330373064323234653463653431303538626338633238b57463703a2f2f31302e37372e302e313a31373837319abb6964756e6e2e72756e74696d655f61637469766174696f6e2e7632d9477368613235362d39613662663530353466616430306232343363323331386330333436303931333230343665643436356231356534636561666532613736623966663231313039b1736572766963652d79676764726173696cd9477368613235362d37316361633366396164336534346139333861343762623035653738306566366264396365303266396139636631333464643765363865396162323563663262d94032363535323862353235386435353563356434646533383931303830363164383965663439616336306631373337626464656337316535333335643163623935c420c06afef8f66e6ec88c96c81ad6d74dbd9933738032f4d8069a46412380e2272dcf0000018bcfe567ecd94066346536393735326334333432363965323262306130346137623430613065336235386364343561383238376364303936383263313864326565366539376239a765643235353139c44046048cf77cc9f524da966e16a131edb34180f4ea8420f5f24a64fd53178e44988bef994d6df103a5f117470283d533e9d7e8245c4f9965f9a4c0a0114ed1c201dc0033b26964756e6e2d343234322e73657276696365af4964756e6e20746573742034323432af696e766f636174696f6e2d3432343201a465786563a6616c77617973ad636f6e74726f6c2d67726f7570c3a57534323432a57534323432a0a0a0c3c3a9696e76697369626c65a3706964c3a430303737a0a0cd10920194cd1092cd1092cd1092cd109294cd1092cd1092cd1092cd109291cd10920000000000c391010807b52f6f70742f746573742f62696e2f736572766963650101d9477368613235362d34626635313232663334343535346335336264653265626238636432623765336431363030616436333163333835613564376363653233633737383534353961d9477368613235362d37316361633366396164336534346139333861343762623035653738306566366264396365303266396139636631333464643765363865396162323563663262a92f6f70742f74657374a92f72756e2f74657374d9477368613235362d6462633162346339303066666534386435373562356461356336333830343031323566363564623066653365323434393462373665613938363435376439383690d9477368613235362d30383466656430386239373861663464376431393661373434366138366235383030396536333662363131646231363231316236356139616164666632396335d9202f73797374656d2e736c6963652f6964756e6e2d343234322e73657276696365c090aa61637469766174696f6edc002001010101010101010101010101010101010101010101010101010101010101019091b1736b69707065642d73746174656c65737392af6f64696e2d636f7272656c61746564959140d9477368613235362d63333634316638353434643763303266333538306230376330663938383766306336613237666635616231643461336532396361663139376366633239396165ab6f64696e2d7369676e65722864959141d9477368613235362d35353961656164303832363464353739356433393039373138636464303561626434393537326538346665353535393065656633316138386130386664666664ab6f64696e2d7369676e6572296493a870726f6d6f74656495a773657276696365d9477368613235362d37316361633366396164336534346139333861343762623035653738306566366264396365303266396139636631333464643765363865396162323563663262d9477368613235362d62663563663539653335363635323235333236386336303463626638646638636664623033613461306433326232376164313538653538313730396338306534d9477368613235362d34313266303131626462396566646138616137323036636364336333613438633637613131386164646161363266616436303463326236346664396163643063cf0000018bcfe6ee96cf0000018bcfe6ee9692d94038663736666430326265623133353233373733373632306361666530383234636138663937326438623332643134373762346336333430656139666634643762dc002048ccd7cc85cc8c14ccb8384dccbccc8fcc83ccbacca50f25ccb072ccc126cc9fccee0cccd660316f5bcccf5e6f172529c095c000c0c092000093c000c0 diff --git a/tests/fixtures/idunn-control-legacy/generation-v3-route-proof.hex b/tests/fixtures/idunn-control-legacy/generation-v3-route-proof.hex new file mode 100644 index 0000000..fbcc390 --- /dev/null +++ b/tests/fixtures/idunn-control-legacy/generation-v3-route-proof.hex @@ -0,0 +1,2 @@ +idunn.admitted_generation.v3 +dc0015bc6964756e6e2e61646d69747465645f67656e65726174696f6e2e7633a773657276696365d93267656e65726174696f6e2d74782d35653364386238382d646632352d346465662d386237632d633736636636376564643933b2636f6e74696e756974792d73657276696365d92774782d35653364386238382d646632352d346465662d386237632d633736636636376564643933cf000001a0ef449fec9ad9216964756e6e2e636f6d70696c65645f6465706c6f796d656e745f706c616e2e7633d9477368613235362d65373533396236376136393534613039373834363234653763643832623335653365386663363833306664646565656330636438646366343262633436333066b5736572766963652d696e6361726e6174696f6e2d316e9bbf6964756e6e2e736f757263655f73656c656374696f6e5f66616374732e7631d92768747470733a2f2f6769746875622e636f6d2f47616d6543756c742f536572766963652e676974af726566732f68656164732f6d61696ed92832323232323232323232323232323232323232323232323232323232323232323232323232323232d92832323232323232323232323232323232323232323232323232323232323232323232323232323232d92833333333333333333333333333333333333333333333333333333333333333333333333333333333bc6465706c6f796d656e742f6964756e6e2f7265636970652e746f6d6cd9477368613235362d393135366563643230356266343439336166636532363961623565613631336133663535663935303766393161666537633633303162613566333735623063378091a87265662d6865616464dc050f0a736368656d61203d202267616d6563756c742e6964756e6e2e7461726765745f6465636c61726174696f6e2e7631220a746172676574203d202273657276696365220a736f757263655f7374616d705f656e7669726f6e6d656e74203d2022534552564943455f4255494c445f434f4d4d4954220a0a5b5b73746570735d5d0a6964203d20226275696c64220a7068617365203d20226275696c64220a72756e6e6572203d202272757374220a61726776203d205b22636172676f222c20226275696c64222c20222d2d6c6f636b6564225d0a0a5b5b65787465726e616c5f696e707574735d5d0a6964203d2022746f6f6c636861696e2d696e646578220a75726c203d202268747470733a2f2f6578616d706c652e696e76616c69642f746f6f6c636861696e2d696e646578220a736861323536203d202264646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464220a72756e6e6572203d202272757374220a64657374696e6174696f6e203d2022696e707574732f746f6f6c636861696e2d696e646578220a0a5b5b6172746966616374735d5d0a6964203d20226461656d6f6e220a736f757263655f6b696e64203d202272756e6e65722d6f7574707574220a72756e6e6572203d202272757374220a736f75726365203d20227461726765742f72656c656173652f73657276696365220a64657374696e6174696f6e203d202273657276696365220a65787065637465645f736861323536203d202263636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363220a65786563757461626c65203d20747275650a0a5b736572766963655d0a65786563757461626c655f6172746966616374203d20226461656d6f6e220a617267756d656e7473203d205b0a20207b206b696e64203d202262696e64696e67222c206e616d65203d202273746174655f726f6f7422207d2c0a5d0a7472616e73706f7274203d202268747470220a726f7574655f7265717569726564203d20747275650a72657175697265645f656e7669726f6e6d656e74203d205b2247414d4543554c545f4944554e4e5f43414e4449444154455f42494e44222c202247414d4543554c545f4944554e4e5f52554e54494d455f42554e444c45225d0a0a5b736572766963652e6865616c74685d0a636f6e7472616374203d2022736572766963652e6865616c7468220a0a5b73746174655d0a736368656d615f67656e65726174696f6e203d20227631220a0a5b5b73746174652e736c6f74735d5d0a6964203d202272756e74696d652d6361636865220a72656c61746976655f70617468203d202272756e74696d652d63616368652e6363220a6b696e64203d202263756c7463616368652d66696c65220a736368656d61203d2022736572766963652e72756e74696d652d63616368652e7631220a777269746572203d20226e6f6e65220a7265636f76657279203d202272656275696c6461626c65220a73746172747570203d20226f70656e2d61742d7374617274220a0a5b5b70726f76696465735d5d0a6361706162696c697479203d2022736572766963652e72756e74696d65220a6361706163697479203d20310a736368656d61203d2022736572766963652e72756e74696d652e7631220a636f6d7061746962696c697479203d20227631220a0adc07980a736368656d61203d202267616d6563756c742e6964756e6e2e6f70657261746f725f62696e64696e672e7632220a746172676574203d202273657276696365220a0a5b7265706f7369746f72795d0a6f726967696e203d202268747470733a2f2f6769746875622e636f6d2f47616d6543756c742f536572766963652e676974220a61646d69747465645f726566203d2022726566732f68656164732f6d61696e220a6d696e696d756d5f7265766973696f6e203d202231313131313131313131313131313131313131313131313131313131313131313131313131313131220a73656c656374696f6e203d20227265662d68656164220a636865636b6f7574203d20222f7372762f6275696c642f53657276696365220a7265636970655f70617468203d20226465706c6f796d656e742f6964756e6e2f7265636970652e746f6d6c220a0a5b72756e6e6572732e727573745d0a647269766572203d2022646f636b6572220a696d616765203d202272757374407368613235363a61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161220a75736572203d2022313030303a31303030220a6166666f7264616e636573203d205b22736f757263652d72656164222c202261727469666163742d7772697465225d0a616c6c6f7765645f70726f6772616d73203d205b22636172676f225d0a6e6574776f726b5f70726f66696c65203d20226275696c642d646570656e64656e63792d656772657373220a6d656d6f72795f6d6562696279746573203d20323034380a6370755f71756f74615f70657263656e74203d203230300a706964735f6c696d6974203d203531320a746d7066735f6d6562696279746573203d203531320a0a5b776f726b6c6f61645d0a647269766572203d202273797374656d642d7472616e7369656e74220a73746174655f67726f7570203d202273657276696365220a756e69745f707265666978203d20226964756e6e2d73657276696365220a72656c656173655f726f6f74203d20222f7372762f736572766963652f72656c6561736573220a73746174655f726f6f74203d20222f7661722f6c69622f67616d6563756c742f73657276696365220a72756e74696d655f726f6f74203d20222f72756e2f67616d6563756c742f6964756e6e2f73657276696365220a6e6574776f726b203d2022686f73742d70726976617465220a68617264656e696e67203d2022737472696374220a6d656d6f72795f6d6562696279746573203d20313032340a6370755f71756f74615f70657263656e74203d203130300a0a5b776f726b6c6f61642e617267756d656e745f62696e64696e67735d0a73746174655f726f6f74203d20222f7661722f6c69622f67616d6563756c742f73657276696365220a0a5b776f726b6c6f61642e7365637265745f66696c65735d0a47414d4543554c545f52554e54494d455f50524553454e43455f4944454e54495459203d20222f6574632f67616d6563756c742f736572766963652f72756e74696d652d70726573656e63652d6964656e746974792e6363220a0a5b72756e74696d655f6964656e746974795d0a72756e74696d655f6964203d2022736572766963652d79676764726173696c220a65787065637465645f7369676e65725f6964656e746974795f6964203d202235353064336235303862343832366564623532356666386339313636393530306230396563306134303639373133626666633137363465663463636363336637220a74727573745f616e63686f725f73746f7265203d20222f746d702f2e746d706f4d424135692f6964656e7469746965732f70726f76696465722d616e63686f722e6363220a0a5b726f7574655d0a647269766572203d20226e67696e782d73747265616d2d746370220a726f7574655f6964203d202273657276696365220a737461626c655f656e64706f696e74203d2022687474703a2f2f3132372e302e302e313a3337373433220a707269766174655f686f7374203d20223132372e302e302e31220a707269766174655f706f72745f7374617274203d2034303537370a707269766174655f706f72745f656e64203d2034303537380a636f6e6669675f70617468203d20222f746d702f2e746d706f4d424135692f736572766963652e636f6e66220a72656c6f61645f756e6974203d20226e67696e782e73657276696365220a0a5b6272616b65735d0a6465706c6f796d656e745f73746f7265203d20222f7661722f6c69622f67616d6563756c742f6964756e6e2f736572766963652d6465706c6f796d656e742d6272616b652e6363220a6c6966656379636c655f73746f7265203d20222f7661722f6c69622f67616d6563756c742f6964756e6e2f736572766963652d6c6966656379636c652d6272616b652e6363220a0a5b726f6c6c6f75745d0a7374726174656779203d202263616e6469646174652d7468656e2d70726f6d6f7465220a647261696e5f7365636f6e6473203d2033300a72657461696e5f72656c6561736573203d20320a0a5b706c6163656d656e745d0a646573697265645f7265706c69636173203d20310a6e6f646573203d205b2279676764726173696c225d0a90cd9e81957878cd012c787896b76964756e6e2e7365616c65645f72656c656173652e7631d9477368613235362d34613533323635653265393464656434663131623936383434366566346137306265613363666635313634383238333736663739623063356436666330653539d9477368613235362d653735333962363761363935346130393738343632346537636438326233356533653866633638333066646465656563306364386463663432626334363330669195a66461656d6f6ea773657276696365d9477368613235362d636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363632ac39196af746f6f6c636861696e2d696e646578d92768747470733a2f2f6578616d706c652e696e76616c69642f746f6f6c636861696e2d696e646578d9477368613235362d64646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464646464a472757374b6696e707574732f746f6f6c636861696e2d696e646578117892d9477368613235362d34613533323635653265393464656434663131623936383434366566346137306265613363666635313634383238333736663739623063356436666330653539ba2f7372762f736572766963652f72656c65617365732f74657374dc0012bd6964756e6e2e65787065637465645f696e6361726e6174696f6e2e7632a773657276696365d9477368613235362d65373533396236376136393534613039373834363234653763643832623335653365386663363833306664646565656330636438646366343262633436333066b5736572766963652d696e6361726e6174696f6e2d31d9477368613235362d34613533323635653265393464656434663131623936383434366566346137306265613363666635313634383238333736663739623063356436666330653539bb6769746875622e636f6d2f47616d6543756c742f53657276696365d92832323232323232323232323232323232323232323232323232323232323232323232323232323232d9477368613235362d39313536656364323035626634343933616663653236396162356561363133613366353566393530376639316166653763363330316261356633373562306337b1736572766963652d79676764726173696cd94035353064336235303862343832366564623532356666386339313636393530306230396563306134303639373133626666633137363465663463636363336637ae736572766963652e6865616c7468d9477368613235362d63636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363a27631d9477368613235362d32663837303134316366353638316566656332653234316364636435663731303831616539326162316535633534633839636438393666386335656236323731c294a773657276696365a468747470b6687474703a2f2f3132372e302e302e313a3337373433b6687474703a2f2f3132372e302e302e313a34303537379194af736572766963652e72756e74696d65b2736572766963652e72756e74696d652e7631a2763101909abb6964756e6e2e72756e74696d655f61637469766174696f6e2e7632d9477368613235362d38646437343836646535373263333732316234383965663531373232633931353334396366376365383632366662313737636261316639386137613061663063b1736572766963652d79676764726173696cd9477368613235362d30636363383737383930313339323639383066383865373661653537663236666238363131643735373435346631656661366133653536313762366636643032d94031653534396337363539396133323564343435626561343135393233613563656638303833353439353262396266383237646533663134366466306465666530c4202f287f2191a013ed6c10ba58e6b52a20be204c0590b415164efae071c5aca54fcf000001a0ef449ce9d94033373962393163363738363561346139626266366334333662653633363965333433646464643235613437316538656638613635316539663863353362323864a765643235353139c4406267597c3a042ebaa5a89355da21e37f59bf72fa816db670bc749c25c6a6150d98ac21475c598f943477402028e3b17bb09f7a98353294e3e7c6ee538b2a1206dc0033b26964756e6e2d343234322e73657276696365af4964756e6e20746573742034323432af696e766f636174696f6e2d3432343201a465786563a6616c77617973ad636f6e74726f6c2d67726f7570c3a57534323432a57534323432a0a0a0c3c3a9696e76697369626c65a3706964c3a430303737a0a0cd10920194cd1092cd1092cd1092cd109294cd1092cd1092cd1092cd109291cd10920000000000c391010807b52f6f70742f746573742f62696e2f736572766963650101d9477368613235362d63636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363d9477368613235362d30636363383737383930313339323639383066383865373661653537663236666238363131643735373435346631656661366133653536313762366636643032a92f6f70742f74657374a92f72756e2f74657374d9477368613235362d6462633162346339303066666534386435373562356461356336333830343031323566363564623066653365323434393462373665613938363435376439383690d9477368613235362d30383466656430386239373861663464376431393661373434366138366235383030396536333662363131646231363231316236356139616164666632396335d9202f73797374656d2e736c6963652f6964756e6e2d343234322e73657276696365c090aa61637469766174696f6edc002001010101010101010101010101010101010101010101010101010101010101019091b1736b69707065642d73746174656c65737392ab726f7574652d70726f6f6695dc03c0ccdc0018ccd92367616d6563756c742e72756e74696d655f70726573656e63655f6865616c74682e7632cca773657276696365ccd9477368613235362d38646437343836646535373263333732316234383965663531373232633931353334396366376365383632366662313737636261316639386137613061663063ccd9477368613235362d65373533396236376136393534613039373834363234653763643832623335653365386663363833306664646565656330636438646366343262633436333066ccb5736572766963652d696e6361726e6174696f6e2d31ccd9477368613235362d34613533323635653265393464656434663131623936383434366566346137306265613363666635313634383238333736663739623063356436666330653539ccd9477368613235362d66663136383733633362666530333730613962336332386466353632663361663464356265313736343630333738633931653032396566396634396266323938cca27631ccd9477368613235362d32663837303134316366353638316566656332653234316364636435663731303831616539326162316535633534633839636438393666386335656236323731ccb1736572766963652d79676764726173696cccd9477368613235362d30636363383737383930313339323639383066383865373661653537663236666238363131643735373435346631656661366133653536313762366636643032ccb6687474703a2f2f3132372e302e302e313a3430353737cc91cc94ccaf736572766963652e72756e74696d65ccb2736572766963652e72756e74696d652e7631cca2763101ccae736572766963652e6865616c7468cca6616374697665ccd93c726f7574652d6f62736572766174696f6e3a63616e6469646174652d3934373439373738356531333466633639306133316461616430626631363136ccc0ccd9403535306433623530386234383236656462353235666638633931363639353030623039656330613430363937313362666663313736346566346363636333663702cccf000001cca0ccef44cc9e5acca765643235353139ccc440ccaa3009610eccf72acccccc8a643633ccf92d3bcc80ccde2dccefccc0ccd911cc8ccca1ccd3ccb053704d041027ccb5ccfdccf01fccd5ccb4cce628ccadcc91cca9ccb6651e27cca778cc912930ccbdcc9e1c1f12cc9724ccedccf4ccd33803ccd94031653534396337363539396133323564343435626561343135393233613563656638303833353439353262396266383237646533663134366466306465666530ccc4405c6dccdecc86cc83cc8f38ccac6b357ccce5ccc2ccd80ecc8709cca822ccbf5106ccc1cce701ccf77bcce211686a744b592ccc93491bcca415cc9f4c75cccbcc860fccf23bcc9acc84ccd6ccf867cce8cc9bcc92ccf6ccf4ccd5ccb84ecc9dcc9c0bd9477368613235362d64626664333862636466343133343862363335653630663865313835356339323836363637653639303534656632353063623530653935303935346363353131d92a63616e6469646174652d3934373439373738356531333466633639306133316461616430626631363136cf000001a0ef449e4fcf000001a0ef449e5bc093a870726f6d6f74656495a773657276696365d9477368613235362d30636363383737383930313339323639383066383865373661653537663236666238363131643735373435346631656661366133653536313762366636643032d9477368613235362d33396133356465613735303130363633646162386133613436366338366530343630383538363036306531666366363662656331366163633837366630366335d9477368613235362d35366536323264306634633834383138303738356162613366356262356533353333336233303661666135656664633539643739373030393563663430353965cf000001a0ef449f3acf000001a0ef449f3ac000c095c000c0c092000093c000c0