From d8b8a203a413e5ec3bde93340f1d9320501d6695 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Tue, 29 Aug 2023 11:14:07 +0200 Subject: [PATCH] WIP: async with_file & read_at_async, problem is that FileSlotGuard is not Send --- pageserver/src/virtual_file.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pageserver/src/virtual_file.rs b/pageserver/src/virtual_file.rs index e997988c68..8337a8bf79 100644 --- a/pageserver/src/virtual_file.rs +++ b/pageserver/src/virtual_file.rs @@ -279,6 +279,22 @@ impl VirtualFile { .observe_closure_duration(|| func(&guard))); } + /// Like with_file, but, takes a func that returns a future. + async fn with_file_async(&self, op: &str, mut factory: F) -> Result + where + F: FnOnce(FileSlotGuard) -> Fut, + Fut: std::future::Future, + { + let guard = self.get_file_guard()?; + + let start = std::time::Instant::now(); + let res = factory(guard).await; + STORAGE_IO_TIME + .with_label_values(&[op]) + .observe(start.elapsed().as_secs_f64()); + Ok(res) + } + fn get_file_guard(&self) -> Result { let open_files = get_open_files(); @@ -483,8 +499,12 @@ impl VirtualFile { } async fn read_at_async(&self, buf: &mut [u8], offset: u64) -> Result { - // TODO: don't block here - let result = self.with_file("read", |file| file.read_at(buf, offset))?; + let result = self + .with_file_async("read", |file| async move { + // todo: use async IO here + file.read_at(buf, offset) + }) + .await?; if let Ok(size) = result { STORAGE_IO_SIZE .with_label_values(&["read", &self.tenant_id, &self.timeline_id])