//! Imperative edits: rewrite the editable layer, dry run, reconcile, roll //! back, and persist through the pending queue. use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::Arc; use rutis::host::ErrorKind; use rutis::{Ctx, Error}; use crate::edit::{apply_edit, Edit}; use crate::error::Failure; use crate::error::{PersistError, Unavailable}; use crate::patch::Layer; use crate::resolver::Build; use crate::LoaderError; use super::desired::{Desired, Row}; use super::{Failing, Inner, LoaderChanged, PendingEditDropped, Restore, Slot, State}; /// Where one copy would run: its instance (`None` outside instances), its /// context without its own scope, and the instances enclosing it. type Target = (Option, Option, Build); /// Whether `row` is inside the group `top`. fn below(desired: &Desired, row: &Row, top: &str) -> bool { let mut parent = row.parent.as_deref(); while let Some(id) = parent { if id == top { return true; } parent = desired.row(id).and_then(|r| r.parent.as_deref()); } false } /// The instances a row's copies run in under `desired`: `None` outside /// instanced groups, else each instance of its instanced group. fn copies(state: &State, desired: &Desired, row: &Row) -> Vec> { let Some(group) = desired.instanced_group(row) else { return vec![None]; }; let mut numbers: Vec = state .instances .iter() .filter(|(_, instance)| instance.group == group.id) .map(|(n, _)| *n) .collect(); numbers.sort_unstable(); numbers.into_iter().map(Some).collect() } /// The context the copy of `leaf` in `scope` would run in, without its own /// scope, when `top` (the leaf or a group above it) is applied afresh: the /// running context above `top`, with the scope of `top` and of each group /// between them applied. fn dry_context( state: &State, desired: &Desired, leaf: &Row, top: &str, scope: Option, root: Option<&Ctx>, ) -> Result, LoaderError> { // Each group between, with the instance its copy is in. let mut groups: Vec<(&Row, Option)> = Vec::new(); let mut current = scope; let mut row = leaf; let anchor = loop { let parent = if row.instanced { match current.and_then(|n| state.instances.get(&n)) { Some(instance) => instance.parent.clone(), None => return Ok(None), } } else { row.parent.clone().map(|id| Slot { row: id, scope: current, }) }; if row.id == top { break parent; } let Some(parent) = parent else { break None; }; let Some(next) = desired.row(&parent.row) else { return Ok(None); }; current = parent.scope; row = next; groups.push((row, current)); }; let base = match state.groups.get(&anchor) { Some(group) => group.ctx.clone(), None if anchor.as_ref().is_none_or(|a| a.scope.is_none()) => match root { Some(root) => root.clone(), None => return Ok(None), }, None => return Ok(None), }; let mut ctx = base; for (group, copy) in groups.into_iter().rev() { let build = Inner::build_for(state, copy); ctx = group .scope .as_ref() .map_err(Clone::clone)? .bind(&build)? .context(&ctx, copy); } Ok(Some(ctx)) } /// How many times a version conflict is resolved by replaying the pending /// queue before giving up with [`LoaderError::Conflict`]. const CONFLICT_RETRIES: usize = 3; impl Inner { /// Check a row would start: resolve, validate the config, build and /// validate the instance, for every copy (each instance it runs in). /// For a group, check every copy of every plugin below it the same way, /// in the contexts the group would give them; an error that copy (same /// row and instance) already fails with is left to reconcile. Nothing /// is spawned. With `resolved`, check that module /// instead of resolving the name. pub(super) async fn dry_run( &self, layers: &[Layer], id: &str, resolved: Option>, ) -> Result<(), LoaderError> { self.dry_run_checking(layers, id, resolved, None).await } /// [`Inner::dry_run`] for `check`, which also validates the configs of /// rows of other runtimes and collects their extra keys into /// `warnings` (config schema design ยง7). pub(super) async fn dry_run_checking( &self, layers: &[Layer], id: &str, resolved: Option>, mut warnings: Option<&mut Vec>, ) -> Result<(), LoaderError> { // The plugins to check, and where each copy would run: its context // without the plugin's own scope, and the instances enclosing it. let (desired, checks, failing) = { let state = self.state.lock().unwrap(); let root = state.groups.get(&None).map(|g| g.ctx.clone()); let desired = self.build_desired(layers, root.as_ref()); let Some(row) = desired.row(id) else { return Ok(()); }; if let Some(invalid) = &row.invalid { return Err(invalid.clone()); } if let Err(e) = &row.disabled { return Err(e.clone()); } if row.group { row.scope.as_ref().map_err(Clone::clone)?; } if !desired.wanted(row) { return Ok(()); } let failing: Vec<(Option, Failure)> = Self::failures(&state) .into_iter() .map(|f| (f.scope, f.failure)) .collect(); let mut checks: Vec<(usize, Vec)> = Vec::new(); for (index, leaf) in desired.rows.iter().enumerate() { if leaf.group || !desired.wanted(leaf) || (leaf.id != id && !below(&desired, leaf, id)) { continue; } let mut targets = Vec::new(); for scope in copies(&state, &desired, leaf) { let base = dry_context(&state, &desired, leaf, id, scope, root.as_ref())?; targets.push((scope, base, Self::build_for(&state, scope))); } checks.push((index, targets)); } (desired, checks, failing) }; for (index, targets) in checks { let row = &desired.rows[index]; // The edited row itself must start; a copy of a plugin below a // group only must not fail in a new way. let tolerated = |scope: Option, error: &LoaderError| { let failure = Failure { id: row.id.clone(), error: error.to_string(), }; row.id != id && failing.contains(&(scope, failure)) }; let name = row.name.clone().unwrap_or_default(); let module = match (&resolved, row.id == id) { (Some(resolved), true) => Ok(resolved.clone()), (_, true) => self.resolver.resolve(&name).await, (_, false) => { let cached = self.state.lock().unwrap().resolved.get(&name).cloned(); match cached { Some(result) => result, None => self.resolver.resolve(&name).await, } } }; match module { Ok(module) => { self.dry_run_copies(row, &module, targets, &tolerated, warnings.as_deref_mut())? } Err(error) if targets.iter().all(|(scope, ..)| tolerated(*scope, &error)) => {} Err(error) => return Err(error), } } Ok(()) } /// Check each copy of the plugin row `row` would start in its context. /// A copy failing with an error `tolerated` accepts for it does not /// count. fn dry_run_copies( &self, row: &Row, resolved: &crate::resolver::Resolved, targets: Vec, tolerated: &dyn Fn(Option, &LoaderError) -> bool, mut warnings: Option<&mut Vec>, ) -> Result<(), LoaderError> { let rejected = |error: Error| LoaderError::Rejected { id: row.id.clone(), error: Arc::new(error.into_kind()), }; for (copy, base, build) in targets { // A copy unavailable in its instance fails there alone. let check = |result: Result<(), LoaderError>| match result { Err(error) if !tolerated(copy, &error) && !Unavailable::is(&error) => Err(error), _ => Ok(()), }; // Evaluate where the plugin would run: its group, with its // isolates. let scope = if resolved.foreign_scope { if let Some(misplaced) = &row.misplaced { check(Err(misplaced.clone()))?; continue; } match row.foreign_scope(&self.catalog, &build) { Ok(scope) => scope, Err(error) => { check(Err(error))?; continue; } } } else { match row .scope .as_ref() .map_err(Clone::clone) .and_then(|s| s.bind(&build)) { Ok(scope) => scope, Err(error) => { check(Err(error))?; continue; } } }; let ctx = base.map(|ctx| scope.context(&ctx, copy)); let config = match self.eval().value_in(&row.config, ctx.as_ref(), &build) { Ok(config) => config, Err(error) => { check(Err(error))?; continue; } }; // The filled copy, for the rows of every runtime. let purpose = match warnings.as_deref_mut() { Some(warnings) => super::config_step::Purpose::Check { warnings }, None => super::config_step::Purpose::Run { row: &row.id, instance: None, }, }; let subject = super::config_step::Subject::of(resolved); let config = match self.config_step(&subject, &config, purpose) { Ok(config) => config, Err(error) => { check(Err(super::config_step::rejected(&row.id, error)))?; continue; } }; let checked = catch_unwind(AssertUnwindSafe(|| { let factory = match &resolved.scoped { Some(scoped) => scoped(&build.clone().for_row(&row.id))?, None => resolved.factory.clone(), }; factory.validate_config(&config)?; super::reconcile::optional_conflict(factory.as_ref(), &row.raw_scope.inject)?; factory.build(&config)?.validate() })); let result = match checked { Ok(Ok(())) => Ok(()), Ok(Err(error)) => Err(rejected(error)), Err(_) => Err(rejected(rutis::Error::from(ErrorKind::PluginFailed( "panicked during the dry run".into(), )))), }; check(result)?; } Ok(()) } /// Apply one edit in memory: rewrite the editable layer, dry-run, /// reconcile, and roll back if rows newly fail. Nothing is persisted. /// /// The layer change and the edit's place in the pending queue are set in /// one critical section, before the reconcile is awaited: a caller that /// drops the future at the reconcile leaves the edit queued, and a later /// `flush` writes it (#1229). Returns the edit's index in the queue. pub(super) async fn commit(self: &Arc, edit: &Edit) -> Result { let (layers, editable, before) = { let state = self.state.lock().unwrap(); let editable = state.editable.ok_or(LoaderError::NoEditableLayer)?; (state.layers.clone(), editable, Self::failures(&state)) }; let patches = apply_edit(&layers, editable, edit)?; let mut next = layers.clone(); next[editable].patches = patches; if !matches!( edit, Edit::Remove { .. } | Edit::SetDisabled { disabled: true, .. } ) { if let Some(id) = edit.id() { self.dry_run(&next, id, None).await?; } } let index = { let mut state = self.state.lock().unwrap(); // A rename changes the module: resolve it afresh. if let Edit::Rename { name, .. } = edit { state.resolved.remove(name); } // A change to a row starts its stopped copies again. if let Some(id) = edit.id() { state.stopped.retain(|slot, _| slot.row != id); } state.layers = next; state.pending.push(edit.clone()); state.pending.len() - 1 }; let report = self.reconcile_inner().await; if report.new_failures.is_empty() { return Ok(index); } { let mut state = self.state.lock().unwrap(); state.layers = layers; // Undo the queue entry with the layer change: the edit failed to // apply, so no caller sees it queued. state.pending.remove(index); } self.reconcile_inner().await; // Compare with the state before the edit, not before the rollback. let rollback: Vec = { let state = self.state.lock().unwrap(); Failing::public( Self::failures(&state) .iter() .filter(|f| !before.contains(f)), ) }; if rollback.is_empty() { Err(LoaderError::ApplyFailed { failures: report.new_failures, }) } else { Err(LoaderError::RollbackFailed { apply: report.new_failures, rollback, }) } } /// Persist the pending queue; on a version conflict, reload the layer, /// replay the queue on it and try again. `current` is the queue index /// of the edit the caller is waiting for. pub(super) async fn persist_queue( self: &Arc, mut current: Option, ) -> Result<(), LoaderError> { let mut current_error: Option = None; let finish = |error: Option| error.map_or(Ok(()), Err); for attempt in 0..=CONFLICT_RETRIES { let (layer, version, edits, patches) = { let state = self.state.lock().unwrap(); let Some(editable) = state.editable else { return finish(current_error); }; if state.pending.is_empty() { return finish(current_error); } ( state.layers[editable].name.clone(), state.version.clone(), state.pending.clone(), state.layers[editable].patches.clone(), ) }; match self.persist.save(&layer, &version, &edits, &patches).await { Ok(version) => { let mut state = self.state.lock().unwrap(); state.version = version; state.pending.clear(); return finish(current_error); } Err(PersistError::Failed(message)) => { return Err(current_error.unwrap_or(LoaderError::PersistFailed(message))); } Err(PersistError::Conflict) if attempt == CONFLICT_RETRIES => { return Err(current_error.unwrap_or(LoaderError::Conflict)); } Err(PersistError::Conflict) => { let (latest, version) = self .persist .load(&layer) .await .map_err(|e| LoaderError::PersistFailed(e.to_string()))?; // `edits` is the queue as it was: the replay rebuilds it // on the latest content, and the state below puts the old // layer, version and queue back if this future is dropped // before the replay finishes (#1229). let mut restore = { let mut state = self.state.lock().unwrap(); let restore = Restore::new(self, &state); let editable = state.editable.unwrap(); state.layers[editable].patches = latest; state.version = version; state.pending.clear(); restore }; self.reconcile_inner().await; let mut replaced = None; // Reported once the replay is kept: a future dropped // before that leaves the queue, and the edits, as they // were. let mut dropped = Vec::new(); for (index, edit) in edits.into_iter().enumerate() { match self.commit(&edit).await { Ok(queued) => { if current == Some(index) { replaced = Some(queued); } } Err(error) if current == Some(index) => current_error = Some(error), Err(error) => dropped.push((edit, error)), } } restore.disarm(); for (edit, error) in dropped { self.emit(PendingEditDropped { edit, error }); } current = replaced; } } } finish(current_error) } pub(super) async fn edit(self: &Arc, edit: Edit) -> Result<(), LoaderError> { let _op = self.op.lock().await; self.check_open()?; let index = self.commit(&edit).await?; let result = self.persist_queue(Some(index)).await; self.emit(LoaderChanged::Edited(edit)); result } }