diff options
Diffstat (limited to 'src/state')
| -rw-r--r-- | src/state/mod.rs | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/state/mod.rs b/src/state/mod.rs index 68eafec..a812b91 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -35,7 +35,7 @@ use std::time::Duration; use anyhow::{Result, bail}; use tokio::sync::{Mutex, RwLock, broadcast, mpsc, watch}; use tokio::time::Instant; -use tracing::{debug, error, warn}; +use tracing::{debug, error, info, warn}; use crate::document::lock::DocumentLock; use crate::document::replica::{Epoch, Stamp}; @@ -94,6 +94,11 @@ pub(crate) struct AppState { pub(crate) change_tx: watch::Sender<u64>, pub(crate) initial_sync_done: Arc<AtomicBool>, + /// True while this node, writing on as its writer of before (`--keep-actor`), has + /// neither synced once with a ready neighbour nor waited `KEPT_WRITER_WAIT`: its + /// writes wait (`mutate_doc_as`), so that peers holding later changes of that writer, + /// made by another copy of its files, hand them over first (`hold_writes`). + pub(crate) writes_held: Arc<watch::Sender<bool>>, /// True once the node shuts down (a signal, a failed join, a transport gone): the /// HTTP API drains and its long-lived responses end, and keepalives say the node /// isn't ready and is leaving (`peer::farewell`). @@ -310,7 +315,41 @@ fn roll_back( Ok(()) } +/// How long a node writing on as its writer of before waits for a ready neighbour to sync +/// with before it writes anyway (`AppState::hold_writes`). +pub(crate) const KEPT_WRITER_WAIT: Duration = Duration::from_secs(10); + impl AppState { + /// Holds this node's writes, as it writes on as its writer of before (`--keep-actor`), + /// until it synced once with a ready neighbour (`release_writes`, `swarm::sync`), or + /// `KEPT_WRITER_WAIT` passed: so that a neighbour holding later changes of that writer, + /// which another copy of its files made (a backup restored, a disk rolled back), hands + /// them over first, and the node takes a fresh writer then (`access::screen`). + pub(crate) fn hold_writes(&self) { + self.writes_held.send_replace(true); + let held = Arc::clone(&self.writes_held); + tokio::spawn(async move { + tokio::time::sleep(KEPT_WRITER_WAIT).await; + if held.send_replace(false) { + info!( + wait = ?KEPT_WRITER_WAIT, + "Writing on as the actor of before: no neighbour synced with within the wait" + ); + } + }); + } + + /// Lets this node's writes go, held while it writes on as its writer of before: it + /// synced once with `peer`, a ready neighbour. + pub(crate) fn release_writes(&self, peer: &str) { + if self.writes_held.send_replace(false) { + info!( + peer, + "Synced with a neighbour: writing on as the actor of before" + ); + } + } + /// Asks the sync worker to look at `peer` (`SyncEvent::PeerNeedsSync`): whether to /// send it a sync message now. Nothing if the worker is gone (the node stops). pub(crate) fn request_sync(&self, peer: &str) { @@ -583,7 +622,8 @@ impl AppState { } /// [`mutate_doc_for`](Self::mutate_doc_for); with `registering_first`, after this - /// node's registration if the policy needs it first. + /// node's registration if the policy needs it first. While writes are held + /// (`writes_held`), after they no longer are. async fn mutate_doc_as<T, E>( &self, user: Option<String>, @@ -595,6 +635,7 @@ impl AppState { T: Send + 'static, E: From<StoreError> + Send + 'static, { + let _ = self.writes_held.subscribe().wait_for(|held| !held).await; let state = self.clone(); tokio::task::spawn_blocking(move || { |
