Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iroh-docs): Add flush_store and use it to make sure the default author is persisted #2471

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions iroh-docs/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ enum Action {
#[debug("reply")]
reply: oneshot::Sender<Result<ContentHashesIterator>>,
},
#[display("FlushStore")]
FlushStore {
#[debug("reply")]
reply: oneshot::Sender<Result<()>>,
},
#[display("Replica({}, {})", _0.fmt_short(), _1)]
Replica(NamespaceId, ReplicaAction),
#[display("Shutdown")]
Expand Down Expand Up @@ -542,6 +547,21 @@ impl SyncHandle {
rx.await?
}

/// Makes sure that all pending database operations are persisted to disk.
///
/// Otherwise, database operations are batched into bigger transactions for speed.
/// Use this if you need to make sure something is written to the database
/// before another operation, e.g. to make sure sudden process exits don't corrupt
/// your application state.
///
/// It's not necessary to call this function before shutdown, as `shutdown` will
/// trigger a flush on its own.
pub async fn flush_store(&self) -> Result<()> {
let (reply, rx) = oneshot::channel();
self.send(Action::FlushStore { reply }).await?;
rx.await?
}

async fn send(&self, action: Action) -> Result<()> {
self.tx
.send_async(action)
Expand Down Expand Up @@ -675,6 +695,7 @@ impl Actor {
Action::ContentHashes { reply } => {
send_reply_with(reply, self, |this| this.store.content_hashes())
}
Action::FlushStore { reply } => send_reply(reply, self.store.flush()),
Action::Replica(namespace, action) => self.on_replica_action(namespace, action),
}
}
Expand Down
4 changes: 4 additions & 0 deletions iroh-docs/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ impl DefaultAuthorStorage {
let author = Author::new(&mut rand::thread_rng());
let author_id = author.id();
docs_store.import_author(author).await?;
// Make sure to write the default author to the store
// *before* we write the default author ID file.
// Otherwise the default author ID file is effectively a dangling reference.
docs_store.flush_store().await?;
self.persist(author_id).await?;
Ok(author_id)
}
Expand Down
Loading