Skip to content

Commit

Permalink
fix: increment ID only append succeeded
Browse files Browse the repository at this point in the history
Otherwise it leads to gaps in IDs and the rollback log is rather
sensitive to those when it comes to recovery.
  • Loading branch information
pepyakin committed Feb 3, 2025
1 parent c3d28ba commit 1e842f7
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions nomt/src/seglog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub struct SegmentedLog {
}

impl SegmentedLog {
/// Append a record to the log.
/// Append a record to the log and return its ID.
///
/// After this function returned, the data is guaranteed to be persisted.
pub fn append(&mut self, data: &[u8]) -> Result<RecordId> {
Expand All @@ -187,7 +187,7 @@ impl SegmentedLog {
));
}

let record_id = self.gen_record_id();
let record_id = self.end_live.next();

// If the head segment is full or doesn't exist, create a new one.
let root_dir_fsync = match self.head_segment_writer {
Expand All @@ -210,6 +210,9 @@ impl SegmentedLog {
writer.write_header(data.len() as u32, record_id)?;
writer.write_payload(data)?;
writer.fsync()?;

// Once the write succeeded, update the live range.
self.end_live = record_id;
if self.start_live.is_nil() {
self.start_live = record_id;
}
Expand Down Expand Up @@ -255,13 +258,6 @@ impl SegmentedLog {
last_segment_id.wrapping_add(1)
}

/// Generate a new record ID and update the live end.
fn gen_record_id(&mut self) -> RecordId {
let id = self.end_live.next();
self.end_live = id;
id
}

/// Prunes the items from the back of the log (oldest records).
///
/// Prunes segments that lie outside of the new live range, i.e. deletes the old segments.
Expand Down

0 comments on commit 1e842f7

Please sign in to comment.