From 63ef6eb66bfde84361a41532ceb4e2c092123798 Mon Sep 17 00:00:00 2001 From: Kim Altintop Date: Tue, 17 Oct 2023 13:13:27 +0200 Subject: [PATCH] db: Swap order of fsync and provide error context --- crates/core/src/db/commit_log.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/core/src/db/commit_log.rs b/crates/core/src/db/commit_log.rs index db5e416923..209ae93304 100644 --- a/crates/core/src/db/commit_log.rs +++ b/crates/core/src/db/commit_log.rs @@ -15,6 +15,7 @@ use crate::{ error::DBError, }; +use anyhow::Context; use spacetimedb_lib::{ hash::{hash_bytes, Hash}, DataKey, @@ -69,9 +70,15 @@ impl CommitLog { let mut mlog = mlog.lock().unwrap(); mlog.append(commit)?; if self.fsync { - mlog.sync_all()?; + let offset = mlog.open_segment_max_offset; + // Sync the odb first, as the mlog depends on its data. This is + // not an atomicity guarantee, but the error context may help + // with forensics. let mut odb = self.odb.lock().unwrap(); - odb.sync_all()?; + odb.sync_all() + .with_context(|| format!("Error syncing odb to disk. Log offset: {offset}"))?; + mlog.sync_all() + .with_context(|| format!("Error syncing mlog to disk. Log offset: {offset}"))?; log::trace!("DATABASE: FSYNC"); } else { mlog.flush()?;