aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/held.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-30 14:55:03 +0000
committertv <tv@krebsco.de>2026-11-30 14:55:03 +0000
commit8017b0e69f0350c2a35629ae71d511ddf7a3aa9c (patch)
tree494d282a79985a19d2194717fa7a7e7a4bba52e1 /src/federation/held.rs
parent8e97bcfa65e92da21df912cc5e19cf18027b82b8 (diff)
A node of a swarm holding a store, on a set of events (--store-document)
With store-root or store-id, store-document names the file of the store's own document, a set of events: the node holds its store there and nothing in the Automerge document, syncs with nodes holding a set and no others, and goes on from the file when started again. Without a file yet it begins from the root (StoreStart::set); joining by the ID alone needs a file. What a checkpoint forgot is kept for store-keeps-forgotten (30 days). The file stands for store-file, and no checkpoint is pinned. The set is written to its file where it changed, every five seconds with the store's worker and as the node stops (Holding::save), what a checkpoint forgot longer ago let go of first. Simulations: a node behind catches up; a node goes on from its file.
Diffstat (limited to 'src/federation/held.rs')
-rw-r--r--src/federation/held.rs64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index cd53727..fd43062 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -111,6 +111,10 @@ pub(crate) struct Holding {
members: RwLock<Option<BTreeSet<(String, String)>>>,
/// What it votes for its party with, if it votes by itself.
voter: Option<Voter>,
+ /// Where the set of events carrying the store is kept (`--store-document`), if it
+ /// is; and how it stood as it was last written there.
+ document: Option<(PathBuf, Arc<Files>)>,
+ written: Mutex<Option<super::store::passing::Stamp>>,
/// Whether a set of events carries the store (`carrying`), from the node's start on.
of_a_set: bool,
// A std mutex: never held while waiting for the document.
@@ -264,6 +268,8 @@ impl Holding {
members: RwLock::default(),
voter: None,
of_a_set: false,
+ document: None,
+ written: Mutex::default(),
inner: Mutex::new(Inner {
kept,
..Inner::default()
@@ -1034,6 +1040,51 @@ impl Holding {
self
}
+ /// Itself, its store carried by `set` and kept in the file at `path`, where the node
+ /// is given a set.
+ pub(crate) fn carrying_if(
+ self,
+ set: Option<Set<Federation>>,
+ path: Option<PathBuf>,
+ files: Arc<Files>,
+ ) -> Self {
+ match (set, path) {
+ (Some(set), Some(path)) => self.carrying(set).kept_in(path, files),
+ (Some(set), None) => self.carrying(set),
+ (None, _) => self,
+ }
+ }
+
+ /// Itself, keeping the set of events that carries its store in the file at `path`.
+ pub(crate) fn kept_in(mut self, path: PathBuf, files: Arc<Files>) -> Self {
+ self.document = Some((path, files));
+ self
+ }
+
+ /// Lets go of what a checkpoint forgot longer ago than the set keeps it, and
+ /// writes the set to its file where it changed since it was last written.
+ /// Whether it wrote.
+ pub(crate) fn save(&self) -> std::io::Result<bool> {
+ let Some((path, files)) = &self.document else {
+ return Ok(false);
+ };
+ let now = Instant::now();
+ self.change_set(|set| set.expire(now));
+ let mut written = self.written.lock_or_recover();
+ let Some((stands, bytes)) = self
+ .with_set(|set| {
+ let stands = super::store::passing::stamp(set);
+ (Some(stands) != *written).then(|| (stands, set.save(now)))
+ })
+ .flatten()
+ else {
+ return Ok(false);
+ };
+ files.write_atomic(FileKind::Store, path, &bytes)?;
+ *written = Some(stands);
+ Ok(true)
+ }
+
/// The kind of the document that carries the store: a set of events, or the
/// document the node holds, which the store is read from.
pub(crate) fn kind(&self) -> &'static str {
@@ -1314,7 +1365,18 @@ pub(crate) async fn run_worker(state: crate::state::AppState) {
}
}
}
- _ = looks.tick() => look = true,
+ _ = looks.tick() => {
+ look = true;
+ // A set of events to its file, where it changed (`Holding::save`).
+ if state.of_a_set() {
+ let holding = Arc::clone(&state.holding);
+ if let Ok(Err(error)) =
+ tokio::task::spawn_blocking(move || holding.save()).await
+ {
+ error!(error = %error, "Couldn't write the store's document: a restart goes on from what the file holds");
+ }
+ }
+ }
}
}
}