Skip to content

Commit 3204052

Browse files
committed
fix: increment ID only append succeeded
Otherwise it leads to gaps in IDs and the rollback log is rather sensitive to those when it comes to recovery.
1 parent 03838c0 commit 3204052

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

nomt/src/seglog/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub struct SegmentedLog {
176176
}
177177

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

190-
let record_id = self.gen_record_id();
190+
let record_id = self.end_live.next();
191191

192192
// If the head segment is full or doesn't exist, create a new one.
193193
let root_dir_fsync = match self.head_segment_writer {
@@ -210,6 +210,9 @@ impl SegmentedLog {
210210
writer.write_header(data.len() as u32, record_id)?;
211211
writer.write_payload(data)?;
212212
writer.fsync()?;
213+
214+
// Once the write succeeded, update the live range.
215+
self.end_live = record_id;
213216
if self.start_live.is_nil() {
214217
self.start_live = record_id;
215218
}
@@ -255,13 +258,6 @@ impl SegmentedLog {
255258
last_segment_id.wrapping_add(1)
256259
}
257260

258-
/// Generate a new record ID and update the live end.
259-
fn gen_record_id(&mut self) -> RecordId {
260-
let id = self.end_live.next();
261-
self.end_live = id;
262-
id
263-
}
264-
265261
/// Prunes the items from the back of the log (oldest records).
266262
///
267263
/// Prunes segments that lie outside of the new live range, i.e. deletes the old segments.

0 commit comments

Comments
 (0)