aboutsummaryrefslogtreecommitdiffstats
path: root/src/state
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-20 04:16:12 +0000
committertv <tv@krebsco.de>2026-11-20 05:29:48 +0100
commit7926a9764626878f5cc244286cce9eb502358929 (patch)
tree7ba6082872afd464490feb296c4f54afbc5c80a9 /src/state
parent00cac6466057c2a8a92dae9e4cd46369e72bcad9 (diff)
keep-actor by default: the writer kept across a clean stop, writes held till a sync
A node that stopped cleanly writes on as the writer of before, as `--keep-actor` had it, now by default: an epoch gathers a writer per node and per crash, drop or rejoin, not per start. `keep-actor = false`, in a configuration file, has a node write as a fresh writer each start; a node without a document file has nothing to keep, which is no longer an error. Writing on as the writer of before, a node holds its writes (`mutate_doc` and the API's through it) until it synced once with a ready neighbour, level with it, or 10 seconds passed (`AppState::hold_writes`, `KEPT_WRITER_WAIT`): a neighbour holding later changes of that writer, which a copy of the node's files made (a backup restored, a disk rolled back), hands them over first, and the node takes a fresh writer then, as it did. A node that reaches no neighbour within the wait writes on.
Diffstat (limited to 'src/state')
-rw-r--r--src/state/mod.rs45
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 || {