Skip to content

Commit

Permalink
bugfix: make sure to flush the wal before flushing dirty pages to dou…
Browse files Browse the repository at this point in the history
…ble buffer
  • Loading branch information
jauhararifin committed Dec 25, 2024
1 parent 3b26411 commit 5694c02
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
10 changes: 6 additions & 4 deletions src/pager/file_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ impl<R: Runtime> FileManager<R> {
}

pub(crate) fn sync(&mut self, wal: &impl WalSync) -> anyhow::Result<()> {
if let Some(max_lsn) = (0..self.pgids.len()).map(|i| self.lsns[i]).max() {
wal.sync(max_lsn)?;
}

// WARNING: it is important to write the double buffer after the wal is flushed to the
// point where all the page that will be flushed have their logs written to the wal.
self.double_buff.truncate(0)?;
self.double_buff.seek(SeekFrom::Start(0))?;
self.double_buff.write_all(&self.pages)?;
Expand All @@ -134,10 +140,6 @@ impl<R: Runtime> FileManager<R> {
.double_buff_bytes_written
.fetch_add(self.pages.len() as u64);

if let Some(max_lsn) = (0..self.pgids.len()).map(|i| self.lsns[i]).max() {
wal.sync(max_lsn)?;
}

for (i, pgid) in self.pgids.iter().enumerate() {
// TODO: maybe we can use vectorized write to write them all in one single syscall
let page_size = self.page_size as u64;
Expand Down
6 changes: 3 additions & 3 deletions tests/basic_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ fn test_os_concurrent_checkpoint_and_rollback() {

#[test]
fn test_db_crashing() {
println!("running with seed 0");
common_tests::simulate_db_crashing(0);

println!("running with seed 8083966471238347500");
common_tests::simulate_db_crashing(8083966471238347500);

Expand All @@ -266,9 +269,6 @@ fn test_db_crashing() {

println!("running with seed 1417552117827942665");
common_tests::simulate_db_crashing(1417552117827942665);

println!("running with seed 0");
common_tests::simulate_db_crashing(0);
}

#[test]
Expand Down
12 changes: 8 additions & 4 deletions tests/common_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ pub fn simulate_db_crashing(seed: u64) {
let p = 100usize;
let mut runtime = SimulatedRuntime::new(seed);

let rng = Arc::new(parking_lot::Mutex::new(rand::rngs::StdRng::seed_from_u64(
seed,
)));

for i in 0..100 {
log::info!(i; "running program");

let rng = rng.clone();
runtime.run(move || {
let path = PathBuf::from("/");
let db = Db::<SimulatedRuntime>::open(
Expand All @@ -37,21 +42,20 @@ pub fn simulate_db_crashing(seed: u64) {
let mut handles = vec![];
for _ in 0..20 {
let db = db.clone();
let rng = rng.clone();
let handle = SimulatedRuntime::spawn("worker", move || loop {
let mut rng = rand::rngs::StdRng::seed_from_u64(seed);

let mut tx = db.update().expect("cannot create write tx");
let mut bucket = tx.bucket("table1").unwrap();

let x = rng.gen_range(0..n);
let x = rng.lock().gen_range(0..n);
for i in 0..p {
let x = x + i * n;
let key = format!("key{x:05}");
let val = format!("val{x:05}");
bucket.put(key.as_bytes(), val.as_bytes()).unwrap();
}

if rng.gen_bool(0.5) {
if rng.lock().gen_bool(0.5) {
tx.commit().unwrap()
} else {
tx.rollback().unwrap()
Expand Down

0 comments on commit 5694c02

Please sign in to comment.