Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit f3e838c

Browse files
authored
lib: modify storage monitor to return more up-to-date information (#380)
Instead of checking the file size, it now locally queries the dbstat table and aggregates the result. A new connection is established on each iteration on purpose: it's not a heavy operation, and at the same time it makes the fiber resilient to any issues with connections that broke. The update is also performed once per minute now, instead of previous 15 minutes.
1 parent dfa1c6f commit f3e838c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

sqld/src/lib.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,21 @@ async fn start_primary(
402402
// TODO: Once we have a separate fiber that does WAL checkpoints, running this routine
403403
// right after checkpointing is exactly where it should be done.
404404
async fn run_storage_monitor(mut db_path: PathBuf, stats: Stats) -> anyhow::Result<()> {
405-
let duration = tokio::time::Duration::from_secs(60 * 15);
405+
let duration = tokio::time::Duration::from_secs(60);
406406
db_path.push("data");
407407
loop {
408-
let attr = tokio::fs::metadata(&db_path).await;
409-
stats.set_storage_bytes_used(attr.map_or(0, |stats| stats.len()));
408+
if let Ok(conn) = rusqlite::Connection::open_with_flags(
409+
&db_path,
410+
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
411+
) {
412+
if let Ok(storage_bytes_used) =
413+
conn.query_row("select sum(pgsize) from dbstat;", [], |row| {
414+
row.get::<usize, u64>(0)
415+
})
416+
{
417+
stats.set_storage_bytes_used(storage_bytes_used);
418+
}
419+
}
410420
tokio::time::sleep(duration).await;
411421
}
412422
}

0 commit comments

Comments
 (0)