From 2b4e99e1ed273da0f921eaec3e52300480acdf0a Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 03:00:19 +0000 Subject: document: what a dropped change takes with it is the document's to say document::dropping: a change a document holds and all that builds on it (Tainted), and the document left without them, with what this node's own writes among them touched and the others' changes (Dropped). The walk of the history, the fork and the fresh actor were the node's, in AppState::drop_taken, which keeps what is its own: whether a drop waits for a second member's word, the quarantine, the refused. --- src/document/dropping.rs | 143 +++++++++++++++++++++++++++++++++++++++++++++++ src/document/mod.rs | 1 + src/state/host.rs | 106 ++++++++++------------------------- 3 files changed, 174 insertions(+), 76 deletions(-) create mode 100644 src/document/dropping.rs diff --git a/src/document/dropping.rs b/src/document/dropping.rs new file mode 100644 index 0000000..56775fd --- /dev/null +++ b/src/document/dropping.rs @@ -0,0 +1,143 @@ +//! A document without a change it holds and every change built on it: what goes +//! (`Tainted`), and the document that is left (`Tainted::dropped`), forked where none of +//! them is, written by a fresh actor, as the old one's later changes are among those +//! dropped. + +use std::collections::HashSet; + +use super::{ActorId, Change, ChangeHash, Document, ExpandedChange}; + +/// Why a change isn't dropped. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Kept { + /// It is the epoch's root: what the document is. + Root, + /// Nothing would be left. + Nothing, +} + +/// A change a document holds and all that builds on it. +pub(crate) struct Tainted { + root: ChangeHash, + changes: Vec, + tainted: HashSet, +} + +/// The document left once what a `Tainted` names is dropped, and what went. +pub(crate) struct Dropped { + pub(crate) doc: Document, + /// The fresh actor `doc` is written by. + pub(crate) actor: ActorId, + /// What this node's own writes among those dropped touched. + pub(crate) own_paths: Vec>, + /// The others' changes dropped. + pub(crate) theirs: Vec, +} + +/// Another node's change dropped: who made it, for whom, and whether it is the one +/// dropped or builds on it. +pub(crate) struct Theirs { + pub(crate) change: Change, + pub(crate) committer: Option, + pub(crate) user: Option, + pub(crate) root: bool, +} + +impl Tainted { + /// `root` and what builds on it in `doc`; none if `doc` doesn't hold it; why it is + /// kept, where it is. + pub(crate) fn of(doc: &Document, root: ChangeHash) -> Option> { + let changes: Vec = doc.get_changes(&[]).into_iter().collect(); + let dropped = changes.iter().find(|change| change.hash() == root)?; + if dropped.deps().is_empty() { + return Some(Err(Kept::Root)); + } + // In causal order: a change after those it builds on. + let mut tainted: HashSet = [root].into(); + for change in &changes { + if change.deps().iter().any(|dep| tainted.contains(dep)) { + tainted.insert(change.hash()); + } + } + if tainted.len() == changes.len() { + return Some(Err(Kept::Nothing)); + } + Some(Ok(Tainted { + root, + changes, + tainted, + })) + } + + /// How many changes go. + pub(crate) fn len(&self) -> usize { + self.tainted.len() + } + + /// How many changes the document holds, those that go among them. + pub(crate) fn of_all(&self) -> usize { + self.changes.len() + } + + /// `doc` without them: this node's own writes among them by the actors it wrote + /// under (`own`, and the one `doc` writes with). + pub(crate) fn dropped(self, doc: &Document, own: Vec) -> anyhow::Result { + let kept = || { + self.changes + .iter() + .filter(|change| !self.tainted.contains(&change.hash())) + }; + let built_on: HashSet = kept() + .flat_map(|change| change.deps().iter().copied()) + .collect(); + let heads: Vec = kept() + .map(|change| change.hash()) + .filter(|hash| !built_on.contains(hash)) + .collect(); + let mut actors = own; + actors.push(doc.get_actor().clone()); + let gone = || { + self.changes + .iter() + .filter(|change| self.tainted.contains(&change.hash())) + }; + let ours: Vec = gone() + .filter(|change| actors.contains(change.actor_id())) + .map(|change| change.decode()) + .collect(); + let mut own_paths: Vec> = super::inspect::Inspector::new(doc) + .ops(&ours) + .into_iter() + .flatten() + .map(|op| op.path) + .collect(); + own_paths.sort(); + own_paths.dedup(); + let actor = ActorId::random(); + let fresh = doc.fork_at(&heads)?.with_actor(actor.clone()); + let theirs = gone() + .filter(|change| !actors.contains(change.actor_id())) + .map(|change| Theirs { + change: change.clone(), + committer: doc + .get_author_for_actor(change.actor_id()) + .and_then(|author| { + std::str::from_utf8(author.as_bytes()) + .ok() + .map(str::to_owned) + }), + user: change + .message() + .and_then(super::replica::user_of) + .map(str::to_owned), + root: change.hash() == self.root, + }) + .collect(); + Ok(Dropped { + doc: fresh, + actor, + own_paths, + theirs, + }) + } +} diff --git a/src/document/mod.rs b/src/document/mod.rs index fd44331..1c39de3 100644 --- a/src/document/mod.rs +++ b/src/document/mod.rs @@ -57,6 +57,7 @@ pub(crate) mod at; #[cfg(test)] pub(crate) mod bench; pub(crate) mod changes; +pub(crate) mod dropping; #[cfg(feature = "federation")] pub(crate) mod events; pub(crate) mod file; diff --git a/src/state/host.rs b/src/state/host.rs index 34ed7ed..2fac94b 100644 --- a/src/state/host.rs +++ b/src/state/host.rs @@ -124,30 +124,21 @@ impl AppState { if !replica.writable { return None; } - let changes = replica.doc.get_changes(&[]); - let dropped = changes.iter().find(|change| change.hash() == root)?; - if dropped.deps().is_empty() { - error!(change = %root, "Asked to drop the epoch's root; kept it"); - return None; - } - // In causal order: a change after those it builds on. - let mut tainted: std::collections::HashSet = [root].into(); - for change in &changes { - if change.deps().iter().any(|dep| tainted.contains(dep)) { - tainted.insert(change.hash()); + use crate::document::dropping::{Kept, Tainted}; + let tainted = match Tainted::of(&replica.doc, root)? { + Ok(tainted) => tainted, + Err(Kept::Root) => { + error!(change = %root, "Asked to drop the epoch's root; kept it"); + return None; } - } - let kept: Vec<_> = changes - .iter() - .filter(|change| !tainted.contains(&change.hash())) - .collect(); - if kept.is_empty() { - error!(change = %root, "Dropping a change would leave nothing; kept it"); - return None; - } + Err(Kept::Nothing) => { + error!(change = %root, "Dropping a change would leave nothing; kept it"); + return None; + } + }; + let count = tainted.len(); if another - && (tainted.len() > BIG_DROP - || (tainted.len() > SMALL_DROP && tainted.len() * 10 > changes.len())) + && (count > BIG_DROP || (count > SMALL_DROP && count * 10 > tainted.of_all())) { use crate::locks::MutexExt; let mut screening = state.screening.lock_or_recover(); @@ -155,75 +146,38 @@ impl AppState { .of(replica.epoch_seq) .hold_drop(root, &peer); if said < 2 { - error!(change = %root, peer = %peer, changes = tainted.len(), why = %why, "A peer says we hold a change the swarm's rules refuse, and ours do too, but dropping it takes much: waiting for another member to say so"); + error!(change = %root, peer = %peer, changes = count, why = %why, "A peer says we hold a change the swarm's rules refuse, and ours do too, but dropping it takes much: waiting for another member to say so"); return None; } } - let built_on: std::collections::HashSet = kept - .iter() - .flat_map(|change| change.deps().iter().copied()) - .collect(); - let heads: Vec = kept - .iter() - .map(|change| change.hash()) - .filter(|hash| !built_on.contains(hash)) - .collect(); - // This node's own writes among those dropped: the paths they touched. - let mut actors = own; - actors.push(replica.doc.get_actor().clone()); - let ours: Vec = changes - .iter() - .filter(|change| tainted.contains(&change.hash())) - .filter(|change| actors.contains(change.actor_id())) - .map(|change| change.decode()) - .collect(); - let mut paths: Vec> = - crate::document::inspect::Inspector::new(&replica.doc) - .ops(&ours) - .into_iter() - .flatten() - .map(|op| op.path) - .collect(); - paths.sort(); - paths.dedup(); - let actor = crate::document::ActorId::random(); - let fresh = match replica.doc.fork_at(&heads) { - Ok(fresh) => fresh.with_actor(actor.clone()), + // This node's own writes among those dropped go to the quarantine; the + // others' changes, for operators, to the refused. + let dropped = match tainted.dropped(&replica.doc, own) { + Ok(dropped) => dropped, Err(error) => { error!(error = %error, "Couldn't drop a change taken unjudged"); return None; } }; - // The others' changes dropped, for operators (own: to the quarantine). - let theirs: Vec = changes - .iter() - .filter(|change| tainted.contains(&change.hash())) - .filter(|change| !actors.contains(change.actor_id())) - .map(|change| crate::access::screen::Refused { - change: (*change).clone(), - committer: replica - .doc - .get_author_for_actor(change.actor_id()) - .and_then(|author| { - std::str::from_utf8(author.as_bytes()) - .ok() - .map(str::to_owned) - }), - user: change - .message() - .and_then(crate::document::replica::user_of) - .map(str::to_owned), + let paths = dropped.own_paths; + let theirs: Vec = dropped + .theirs + .into_iter() + .map(|theirs| crate::access::screen::Refused { + change: theirs.change, + committer: theirs.committer, + user: theirs.user, paths: Default::default(), - why: if change.hash() == root { + why: if theirs.root { format!("taken unjudged, dropped: {why}") } else { format!("taken unjudged, dropped: it builds on {root}") }, }) .collect(); - state.own_writers.add(actor); + state.own_writers.add(dropped.actor); let epoch = replica.epoch; - let old = state.swap_document(&mut replica, epoch, fresh); + let old = state.swap_document(&mut replica, epoch, dropped.doc); { use crate::locks::MutexExt; let mut screening = state.screening.lock_or_recover(); @@ -233,7 +187,7 @@ impl AppState { // Before the document is let go: whoever reads it without the writes finds // them quarantined. crate::remake::quarantine_taken(&state, epoch, &old, paths); - Some((own_paths, tainted.len())) + Some((own_paths, count)) }) .await .ok() -- cgit v1.3.1