diff options
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/held.rs | 64 |
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"); + } + } + } } } } |
