Skip to content

Commit 5531a56

Browse files
committed
disklayer_sqlite: Commit the cache flush transaction
Commit the FULL synchronous WAL transaction before switching back to NORMAL mode. The transaction was previously rolled back on drop, so sync_cache could return success without syncing cached writes. Add tests for WAL commit visibility and commit error reporting. Check that a later flush can complete after a rejected commit. Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
1 parent b662d30 commit 5531a56

3 files changed

Lines changed: 55 additions & 0 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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ impl LayerIo for SqliteDiskLayer {
382382
{
383383
let tx = conn.transaction()?;
384384
tx.pragma_update(None, "user_version", "0")?;
385+
tx.commit()?;
385386
}
386387
conn.pragma_update(None, "synchronous", "NORMAL")?;
387388
Ok(())
@@ -585,10 +586,16 @@ mod tests {
585586
use crate::FormatParams;
586587
use crate::SqliteDiskLayer;
587588
use disk_backend::Disk;
589+
use disk_backend::DiskError;
588590
use disk_layered::DiskLayer;
589591
use disk_layered::LayerConfiguration;
592+
use disk_layered::LayerIo;
590593
use disk_layered::LayeredDisk;
594+
use guestmem::GuestMemory;
591595
use pal_async::async_test;
596+
use rusqlite::Connection;
597+
use scsi_buffers::OwnedRequestBuffers;
598+
use test_with_tracing::test;
592599

593600
const SECTOR_SIZE: u32 = 512;
594601
const DISK_SIZE: u64 = 1024 * 1024;
@@ -632,4 +639,49 @@ mod tests {
632639
let dir = tempfile::tempdir().unwrap();
633640
storage_tests::sector_range::test_layer_sector_range(&new_layer(&dir)).await;
634641
}
642+
643+
#[async_test]
644+
async fn sync_cache_commits_wal_transaction() {
645+
let dir = tempfile::tempdir().unwrap();
646+
let layer = new_layer(&dir);
647+
layer
648+
.conn
649+
.lock()
650+
.await
651+
.pragma_update(None, "synchronous", "NORMAL")
652+
.unwrap();
653+
let observer = Connection::open(dir.path().join("test.dbhd")).unwrap();
654+
let mem = GuestMemory::allocate(SECTOR_SIZE as usize);
655+
let buffers = OwnedRequestBuffers::linear(0, SECTOR_SIZE as usize, false);
656+
657+
for pattern in [0x5a, 0xa5] {
658+
mem.write_at(0, &[pattern; SECTOR_SIZE as usize]).unwrap();
659+
layer.write(&buffers.buffer(&mem), 7, false).await.unwrap();
660+
let before: u32 = observer
661+
.pragma_query_value(None, "data_version", |row| row.get(0))
662+
.unwrap();
663+
664+
layer.sync_cache().await.unwrap();
665+
666+
let after: u32 = observer
667+
.pragma_query_value(None, "data_version", |row| row.get(0))
668+
.unwrap();
669+
assert_ne!(
670+
before, after,
671+
"cache flush did not commit a WAL transaction"
672+
);
673+
}
674+
}
675+
676+
#[async_test]
677+
async fn sync_cache_reports_commit_error() {
678+
let dir = tempfile::tempdir().unwrap();
679+
let layer = new_layer(&dir);
680+
layer.conn.lock().await.commit_hook(Some(|| true));
681+
682+
assert!(matches!(layer.sync_cache().await, Err(DiskError::Io(_))));
683+
684+
layer.conn.lock().await.commit_hook(None::<fn() -> bool>);
685+
layer.sync_cache().await.unwrap();
686+
}
635687
}

0 commit comments

Comments
 (0)