Skip to content

Commit

Permalink
core: Replay database prefix
Browse files Browse the repository at this point in the history
If a commit cannot be decoded from the log, stop replaying and truncate
the log to the known-good prefix.
  • Loading branch information
kim committed Nov 6, 2023
1 parent 29da610 commit 7f0bd54
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crates/core/src/db/relational_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use spacetimedb_primitives::{ColId, IndexId, SequenceId, TableId};
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};
use std::borrow::Cow;
use std::fs::{create_dir_all, File};
use std::io;
use std::ops::RangeBounds;
use std::path::Path;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -95,10 +96,18 @@ impl RelationalDB {
let mut last_commit_offset = None;
let mut last_hash: Option<Hash> = None;
if let Some(message_log) = &message_log {
let message_log = message_log.lock().unwrap();
let mut message_log = message_log.lock().unwrap();
let max_offset = message_log.open_segment_max_offset;
for commit in commit_log::Iter::from(message_log.segments()) {
let commit = commit?;
'commits: for commit in commit_log::Iter::from(message_log.segments()) {
let commit = match commit {
Ok(commit) => commit,
Err(e) if e.kind() == io::ErrorKind::InvalidData => {
log::warn!("Corrupt commit after offset {last_commit_offset:?}");
message_log.reset_to(last_commit_offset.unwrap_or(0))?;
break 'commits;
}
Err(e) => return Err(e.into()),
};

segment_index += 1;
last_hash = commit.parent_commit_hash;
Expand Down

0 comments on commit 7f0bd54

Please sign in to comment.