diff --git a/src/deployment.rs b/src/deployment.rs index 23ecac3..a67b4bc 100644 --- a/src/deployment.rs +++ b/src/deployment.rs @@ -275,6 +275,10 @@ pub struct OperatorBinding { pub process_write_lease: Option, pub brakes: BrakeBinding, pub rollout: RolloutBinding, + /// Optional per-phase deadline overrides; absent fields take Idunn's defaults + /// when the plan is compiled. + #[serde(default)] + pub deadlines: Option, pub placement: PlacementBinding, #[serde(default)] pub external_capabilities: Vec, @@ -709,6 +713,44 @@ pub struct RolloutBinding { pub retain_releases: u32, } +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct DeadlineBinding { + #[serde(default)] + pub fencing_seconds: Option, + #[serde(default)] + pub leasing_seconds: Option, + #[serde(default)] + pub awaiting_ready_seconds: Option, + #[serde(default)] + pub routing_seconds: Option, + #[serde(default)] + pub committing_seconds: Option, +} + +/// Longest deadline a binding may declare: one day. +pub const MAXIMUM_DEADLINE_SECONDS: u32 = 86_400; + +impl DeadlineBinding { + fn validate(&self) -> Result<()> { + for (name, seconds) in [ + ("fencing", self.fencing_seconds), + ("leasing", self.leasing_seconds), + ("awaiting-ready", self.awaiting_ready_seconds), + ("routing", self.routing_seconds), + ("committing", self.committing_seconds), + ] { + if let Some(seconds) = seconds { + ensure!( + (1..=MAXIMUM_DEADLINE_SECONDS).contains(&seconds), + "{name} deadline must be between 1 and {MAXIMUM_DEADLINE_SECONDS} seconds" + ); + } + } + Ok(()) + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum RolloutStrategy { @@ -1343,6 +1385,9 @@ impl OperatorBinding { /// The part of validation that does not depend on where the workload runs. fn validate_shared_tail(&self) -> Result<()> { + if let Some(deadlines) = &self.deadlines { + deadlines.validate()?; + } ensure!( self.rollout.retain_releases >= 2, "candidate rollout must retain at least current and prior releases" diff --git a/src/deployment_plan.rs b/src/deployment_plan.rs index c768657..e3d690b 100644 --- a/src/deployment_plan.rs +++ b/src/deployment_plan.rs @@ -12,13 +12,16 @@ use sha2::{Digest, Sha256}; use crate::control_plane::SequenceAdmittedReady; use crate::deployment::{ - CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding, + CapabilityDependency, DeadlineBinding, DependencyKind, ExternalCapabilityBinding, OperatorBinding, ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, TargetDeclaration, capability_compatible, }; pub const SOURCE_SELECTION_FACTS_SCHEMA: &str = "idunn.source_selection_facts.v1"; -pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v2"; +pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v3"; +/// Plans sealed before phase deadlines existed. They keep their bytes, and so +/// their plan id; their deadlines are Idunn's defaults at read time. +pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA_V2: &str = "idunn.compiled_deployment_plan.v2"; pub const SEALED_RELEASE_SCHEMA: &str = "idunn.sealed_release.v1"; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -460,6 +463,86 @@ pub fn select_dependencies( .collect() } +/// Idunn's default duration for each post-fencing phase, in seconds. A binding +/// overrides any of them; the resolved values are frozen into the plan. +pub const DEFAULT_FENCING_DEADLINE_SECONDS: u32 = 120; +pub const DEFAULT_LEASING_DEADLINE_SECONDS: u32 = 120; +pub const DEFAULT_AWAITING_READY_DEADLINE_SECONDS: u32 = 300; +pub const DEFAULT_ROUTING_DEADLINE_SECONDS: u32 = 120; +pub const DEFAULT_COMMITTING_DEADLINE_SECONDS: u32 = 120; + +/// The resolved per-phase deadline durations a plan carries. Nothing reads +/// them at the cut that introduced them. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PhaseDeadlines { + pub fencing_seconds: u32, + pub leasing_seconds: u32, + pub awaiting_ready_seconds: u32, + pub routing_seconds: u32, + pub committing_seconds: u32, +} + +impl PhaseDeadlines { + pub const IDUNN_DEFAULTS: Self = Self { + fencing_seconds: DEFAULT_FENCING_DEADLINE_SECONDS, + leasing_seconds: DEFAULT_LEASING_DEADLINE_SECONDS, + awaiting_ready_seconds: DEFAULT_AWAITING_READY_DEADLINE_SECONDS, + routing_seconds: DEFAULT_ROUTING_DEADLINE_SECONDS, + committing_seconds: DEFAULT_COMMITTING_DEADLINE_SECONDS, + }; + + /// Binding overrides over Idunn's defaults. + pub fn resolve(binding: Option<&DeadlineBinding>) -> Self { + let defaults = Self::IDUNN_DEFAULTS; + let Some(binding) = binding else { + return defaults; + }; + Self { + fencing_seconds: binding.fencing_seconds.unwrap_or(defaults.fencing_seconds), + leasing_seconds: binding.leasing_seconds.unwrap_or(defaults.leasing_seconds), + awaiting_ready_seconds: binding + .awaiting_ready_seconds + .unwrap_or(defaults.awaiting_ready_seconds), + routing_seconds: binding.routing_seconds.unwrap_or(defaults.routing_seconds), + committing_seconds: binding + .committing_seconds + .unwrap_or(defaults.committing_seconds), + } + } + + fn validate_against(&self, binding: &OperatorBinding) -> Result<()> { + let declared = binding.deadlines.clone().unwrap_or_default(); + for (name, frozen, declared) in [ + ("fencing", self.fencing_seconds, declared.fencing_seconds), + ("leasing", self.leasing_seconds, declared.leasing_seconds), + ( + "awaiting-ready", + self.awaiting_ready_seconds, + declared.awaiting_ready_seconds, + ), + ("routing", self.routing_seconds, declared.routing_seconds), + ( + "committing", + self.committing_seconds, + declared.committing_seconds, + ), + ] { + ensure!( + (1..=crate::deployment::MAXIMUM_DEADLINE_SECONDS).contains(&frozen), + "frozen {name} deadline is out of range" + ); + if let Some(declared) = declared { + ensure!( + frozen == declared, + "frozen {name} deadline differs from the binding" + ); + } + } + Ok(()) + } +} + /// Private Idunn control-plane state. This contains host binding details and is /// never an Odin/CultMesh projection. The shared CultNet Expected incarnation /// is the sanitized topology projection derived only after a release validates @@ -476,12 +559,17 @@ pub struct CompiledDeploymentPlan { pub binding_blob: Vec, pub dependencies: Vec, pub candidate_port: Option, + /// Present exactly on v3 plans. Skipped when absent so a v2 plan re-encodes + /// to its original bytes and keeps its plan id. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub deadlines: Option, } impl CompiledDeploymentPlan { pub fn validate(&self) -> Result<()> { ensure!( - self.schema == COMPILED_DEPLOYMENT_PLAN_SCHEMA, + self.schema == COMPILED_DEPLOYMENT_PLAN_SCHEMA + || self.schema == COMPILED_DEPLOYMENT_PLAN_SCHEMA_V2, "unsupported deployment-plan schema" ); require_sha256(&self.plan_id, "plan id")?; @@ -492,6 +580,11 @@ impl CompiledDeploymentPlan { ); let (declaration, binding) = self.parsed_inputs()?; self.source.validate_against(&binding)?; + match (&self.schema[..], &self.deadlines) { + (COMPILED_DEPLOYMENT_PLAN_SCHEMA, Some(deadlines)) => deadlines.validate_against(&binding)?, + (COMPILED_DEPLOYMENT_PLAN_SCHEMA_V2, None) => {} + _ => bail!("deployment plan deadlines differ from its schema"), + } ensure!( sha256_id(&self.recipe_blob) == self.source.recipe_blob_sha256, "selected recipe blob differs from the exact plan recipe bytes" @@ -527,6 +620,12 @@ impl CompiledDeploymentPlan { Ok(()) } + /// The deadline durations this plan runs under: frozen for v3, Idunn's + /// defaults for a v2 plan that predates them. + pub fn phase_deadlines(&self) -> PhaseDeadlines { + self.deadlines.unwrap_or(PhaseDeadlines::IDUNN_DEFAULTS) + } + pub(crate) fn parsed_inputs(&self) -> Result<(TargetDeclaration, OperatorBinding)> { let recipe_text = std::str::from_utf8(&self.recipe_blob) .context("stored deployment recipe is not UTF-8")?; @@ -589,6 +688,7 @@ pub(crate) fn compile_deployment_plan( binding_blob: binding_bytes.to_vec(), dependencies, candidate_port, + deadlines: Some(PhaseDeadlines::resolve(binding.deadlines.as_ref())), }; plan.plan_id = plan.recomputed_plan_id()?; plan.validate()?; @@ -1594,4 +1694,92 @@ nodes = ["yggdrasil"] changed.external_inputs[0].size_bytes = 18; assert!(changed.validate_against(&plan).is_err()); } + + fn binding_with_deadlines(table: &str) -> String { + BINDING.replace("[placement]", &format!("{table}\n[placement]")) + } + + fn plan_under(binding: &str) -> Result { + let providers = [ready_odin_provider("odin", "odin-yggdrasil", 1)]; + compile_deployment_plan( + RECIPE.as_bytes(), + binding.as_bytes(), + source(RECIPE), + "service-incarnation-1", + Some(18001), + 110, + &providers, + ) + } + + #[test] + fn a_compiled_plan_freezes_idunn_default_deadlines() { + let plan = plan(); + assert_eq!(plan.schema, COMPILED_DEPLOYMENT_PLAN_SCHEMA); + assert_eq!(plan.deadlines, Some(PhaseDeadlines::IDUNN_DEFAULTS)); + assert_eq!(plan.phase_deadlines(), PhaseDeadlines::IDUNN_DEFAULTS); + plan.validate().unwrap(); + } + + #[test] + fn a_binding_overrides_only_the_deadlines_it_names() { + let binding = binding_with_deadlines("[deadlines]\nawaiting_ready_seconds = 45\n"); + let plan = plan_under(&binding).unwrap(); + let frozen = plan.phase_deadlines(); + assert_eq!(frozen.awaiting_ready_seconds, 45); + assert_eq!( + PhaseDeadlines { + awaiting_ready_seconds: DEFAULT_AWAITING_READY_DEADLINE_SECONDS, + ..frozen + }, + PhaseDeadlines::IDUNN_DEFAULTS + ); + } + + #[test] + fn deadline_bounds_and_unknown_phases_are_refused() { + for table in [ + "[deadlines]\nrouting_seconds = 0\n", + "[deadlines]\nrouting_seconds = 86401\n", + "[deadlines]\nwarming_seconds = 5\n", + ] { + assert!( + OperatorBinding::parse(&binding_with_deadlines(table)).is_err(), + "{table}" + ); + } + } + + #[test] + fn frozen_deadlines_must_agree_with_the_binding_and_the_plan_schema() { + let binding = binding_with_deadlines("[deadlines]\nrouting_seconds = 45\n"); + let sealed = |mut plan: CompiledDeploymentPlan| { + plan.plan_id = plan.recomputed_plan_id().unwrap(); + plan + }; + let honest = plan_under(&binding).unwrap(); + + let mut drifted = honest.clone(); + drifted.deadlines.as_mut().unwrap().routing_seconds = 46; + assert!(sealed(drifted).validate().is_err()); + + let mut absent = honest.clone(); + absent.deadlines = None; + assert!(sealed(absent).validate().is_err()); + + let mut v2_with_deadlines = honest.clone(); + v2_with_deadlines.schema = COMPILED_DEPLOYMENT_PLAN_SCHEMA_V2.into(); + assert!(sealed(v2_with_deadlines).validate().is_err()); + + // A plan sealed before deadlines existed keeps its nine-field encoding, + // so its plan id still holds, and it runs on Idunn's defaults. + let mut v2 = plan(); + v2.schema = COMPILED_DEPLOYMENT_PLAN_SCHEMA_V2.into(); + v2.deadlines = None; + let v2 = sealed(v2); + v2.validate().unwrap(); + assert_eq!(v2.phase_deadlines(), PhaseDeadlines::IDUNN_DEFAULTS); + assert_eq!(rmp_serde::to_vec(&v2).unwrap()[0], 0x99); + assert_eq!(rmp_serde::to_vec(&honest).unwrap()[0], 0x9a); + } }