Skip to content

Commit 87243c4

Browse files
committed
disklayer_sqlite: Commit the cache flush transaction
Commit the WAL transaction with synchronous set to FULL. The transaction was previously rolled back on drop, so sync_cache could return success without syncing cached writes. Restore the original synchronous mode even when the flush fails so that flushing does not change the durability of later writes. Verify that the WAL commit is visible and runs with synchronous set to FULL. Check that a rejected commit reports an error without changing the mode or preventing a later flush. Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
1 parent b662d30 commit 87243c4

3 files changed

Lines changed: 113 additions & 4 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,7 @@ dependencies = [
18691869
"serde_json",
18701870
"storage_tests",
18711871
"tempfile",
1872+
"test_with_tracing",
18721873
"tracing",
18731874
"vm_resource",
18741875
]

‎vm/devices/storage/disklayer_sqlite/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ tracing.workspace = true
3737

3838
[dev-dependencies]
3939
pal_async.workspace = true
40+
rusqlite = { workspace = true, features = ["hooks"] }
4041
storage_tests.workspace = true
42+
test_with_tracing.workspace = true
4143

4244
# Building SQLite from source needs a C toolchain for the target, which the
4345
# macOS cross-compile lint job does not have. That job only lints and never

‎vm/devices/storage/disklayer_sqlite/src/lib.rs‎

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,17 @@ impl LayerIo for SqliteDiskLayer {
377377
unblock({
378378
let mut conn = self.conn.clone().lock_owned().await;
379379
move || -> rusqlite::Result<()> {
380+
let previous_mode: u32 =
381+
conn.pragma_query_value(None, "synchronous", |row| row.get(0))?;
380382
// https://sqlite-users.sqlite.narkive.com/LX75NOma/forcing-a-manual-fsync-in-wal-normal-mode
381383
conn.pragma_update(None, "synchronous", "FULL")?;
382-
{
384+
let flush_result = (|| {
383385
let tx = conn.transaction()?;
384386
tx.pragma_update(None, "user_version", "0")?;
385-
}
386-
conn.pragma_update(None, "synchronous", "NORMAL")?;
387-
Ok(())
387+
tx.commit()
388+
})();
389+
let restore_result = conn.pragma_update(None, "synchronous", previous_mode);
390+
flush_result.and(restore_result)
388391
}
389392
})
390393
.await
@@ -585,10 +588,22 @@ mod tests {
585588
use crate::FormatParams;
586589
use crate::SqliteDiskLayer;
587590
use disk_backend::Disk;
591+
use disk_backend::DiskError;
588592
use disk_layered::DiskLayer;
589593
use disk_layered::LayerConfiguration;
594+
use disk_layered::LayerIo;
590595
use disk_layered::LayeredDisk;
596+
use guestmem::GuestMemory;
591597
use pal_async::async_test;
598+
use rusqlite::Connection;
599+
use rusqlite::hooks::AuthAction;
600+
use rusqlite::hooks::AuthContext;
601+
use rusqlite::hooks::Authorization;
602+
use scsi_buffers::OwnedRequestBuffers;
603+
use std::sync::Arc;
604+
use std::sync::atomic::AtomicBool;
605+
use std::sync::atomic::Ordering;
606+
use test_with_tracing::test;
592607

593608
const SECTOR_SIZE: u32 = 512;
594609
const DISK_SIZE: u64 = 1024 * 1024;
@@ -632,4 +647,95 @@ mod tests {
632647
let dir = tempfile::tempdir().unwrap();
633648
storage_tests::sector_range::test_layer_sector_range(&new_layer(&dir)).await;
634649
}
650+
651+
#[async_test]
652+
async fn sync_cache_commits_wal_transaction() {
653+
let dir = tempfile::tempdir().unwrap();
654+
let layer = new_layer(&dir);
655+
let synchronous_full = Arc::new(AtomicBool::new(false));
656+
{
657+
let conn = layer.conn.lock().await;
658+
conn.pragma_update(None, "synchronous", "NORMAL").unwrap();
659+
let synchronous_full = synchronous_full.clone();
660+
// The commit hook cannot query this connection. Track mode changes
661+
// through the authorizer as SQLite prepares each PRAGMA assignment.
662+
conn.authorizer(Some(move |ctx: AuthContext<'_>| {
663+
if let AuthAction::Pragma {
664+
pragma_name,
665+
pragma_value: Some(value),
666+
} = ctx.action
667+
&& pragma_name.eq_ignore_ascii_case("synchronous")
668+
{
669+
synchronous_full.store(
670+
value.eq_ignore_ascii_case("FULL") || value == "2",
671+
Ordering::Relaxed,
672+
);
673+
}
674+
Authorization::Allow
675+
}));
676+
}
677+
let observer = Connection::open(dir.path().join("test.dbhd")).unwrap();
678+
let mem = GuestMemory::allocate(SECTOR_SIZE as usize);
679+
let buffers = OwnedRequestBuffers::linear(0, SECTOR_SIZE as usize, false);
680+
681+
for pattern in [0x5a, 0xa5] {
682+
mem.write_at(0, &[pattern; SECTOR_SIZE as usize]).unwrap();
683+
layer.write(&buffers.buffer(&mem), 7, false).await.unwrap();
684+
let before: u32 = observer
685+
.pragma_query_value(None, "data_version", |row| row.get(0))
686+
.unwrap();
687+
688+
let synchronous_full = synchronous_full.clone();
689+
layer
690+
.conn
691+
.lock()
692+
.await
693+
.commit_hook(Some(move || !synchronous_full.load(Ordering::Relaxed)));
694+
layer
695+
.sync_cache()
696+
.await
697+
.expect("cache flush must commit with synchronous=FULL");
698+
layer.conn.lock().await.commit_hook(None::<fn() -> bool>);
699+
700+
let after: u32 = observer
701+
.pragma_query_value(None, "data_version", |row| row.get(0))
702+
.unwrap();
703+
assert_ne!(
704+
before, after,
705+
"cache flush did not commit a WAL transaction"
706+
);
707+
}
708+
}
709+
710+
#[async_test]
711+
async fn sync_cache_reports_commit_error() {
712+
let dir = tempfile::tempdir().unwrap();
713+
let layer = new_layer(&dir);
714+
for (mode, expected) in [("NORMAL", 1), ("FULL", 2)] {
715+
{
716+
let conn = layer.conn.lock().await;
717+
conn.pragma_update(None, "synchronous", mode).unwrap();
718+
conn.commit_hook(Some(|| true));
719+
}
720+
721+
assert!(matches!(layer.sync_cache().await, Err(DiskError::Io(_))));
722+
723+
layer.conn.lock().await.commit_hook(None::<fn() -> bool>);
724+
for flush_succeeded in [false, true] {
725+
if flush_succeeded {
726+
layer.sync_cache().await.unwrap();
727+
}
728+
let synchronous: u32 = layer
729+
.conn
730+
.lock()
731+
.await
732+
.pragma_query_value(None, "synchronous", |row| row.get(0))
733+
.unwrap();
734+
assert_eq!(
735+
synchronous, expected,
736+
"expected synchronous={mode} after flush (success={flush_succeeded})"
737+
);
738+
}
739+
}
740+
}
635741
}

0 commit comments

Comments
 (0)