From dfc364e4f4be97496986e586927bf539f63baff5 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Fri, 11 Apr 2025 11:54:12 +0200 Subject: [PATCH] remove non-absolute-position write APIs from VirtualFile --- pageserver/src/virtual_file.rs | 91 ++-------------------------------- 1 file changed, 3 insertions(+), 88 deletions(-) diff --git a/pageserver/src/virtual_file.rs b/pageserver/src/virtual_file.rs index a738732a79..fc69b702d3 100644 --- a/pageserver/src/virtual_file.rs +++ b/pageserver/src/virtual_file.rs @@ -223,14 +223,6 @@ impl VirtualFile { self.inner.write_all_at(buf, offset, ctx).await } - pub async fn write_all( - &mut self, - buf: FullSlice, - ctx: &RequestContext, - ) -> (FullSlice, Result) { - self.inner.write_all(buf, ctx).await - } - async fn read_to_end(&mut self, buf: &mut Vec, ctx: &RequestContext) -> Result<(), Error> { self.inner.read_to_end(buf, ctx).await } @@ -847,58 +839,6 @@ impl VirtualFileInner { (restore(buf), Ok(())) } - /// Writes `buf` to the file at the current offset. - /// - /// Panics if there is an uninitialized range in `buf`, as that is most likely a bug in the caller. - pub async fn write_all( - &mut self, - buf: FullSlice, - ctx: &RequestContext, - ) -> (FullSlice, Result) { - let buf = buf.into_raw_slice(); - let bounds = buf.bounds(); - let restore = - |buf: Slice<_>| FullSlice::must_new(Slice::from_buf_bounds(buf.into_inner(), bounds)); - let nbytes = buf.len(); - let mut buf = buf; - while !buf.is_empty() { - let (tmp, res) = self.write(FullSlice::must_new(buf), ctx).await; - buf = tmp.into_raw_slice(); - match res { - Ok(0) => { - return ( - restore(buf), - Err(Error::new( - std::io::ErrorKind::WriteZero, - "failed to write whole buffer", - )), - ); - } - Ok(n) => { - buf = buf.slice(n..); - } - Err(ref e) if e.kind() == std::io::ErrorKind::Interrupted => {} - Err(e) => return (restore(buf), Err(e)), - } - } - (restore(buf), Ok(nbytes)) - } - - async fn write( - &mut self, - buf: FullSlice, - ctx: &RequestContext, - ) -> (FullSlice, Result) { - let pos = self.pos; - let (buf, res) = self.write_at(buf, pos, ctx).await; - let n = match res { - Ok(n) => n, - Err(e) => return (buf, Err(e)), - }; - self.pos += n as u64; - (buf, Ok(n)) - } - pub(crate) async fn read_at( &self, buf: tokio_epoll_uring::Slice, @@ -927,23 +867,11 @@ impl VirtualFileInner { }) } - /// The function aborts the process if the error is fatal. async fn write_at( &self, buf: FullSlice, offset: u64, ctx: &RequestContext, - ) -> (FullSlice, Result) { - let (slice, result) = self.write_at_inner(buf, offset, ctx).await; - let result = result.maybe_fatal_err("write_at"); - (slice, result) - } - - async fn write_at_inner( - &self, - buf: FullSlice, - offset: u64, - ctx: &RequestContext, ) -> (FullSlice, Result) { let file_guard = match self.lock_file().await { Ok(file_guard) => file_guard, @@ -952,6 +880,7 @@ impl VirtualFileInner { observe_duration!(StorageIoOperation::Write, { let ((_file_guard, buf), result) = io_engine::get().write_at(file_guard, offset, buf).await; + let result = result.maybe_fatal_err("write_at"); if let Ok(size) = result { ctx.io_size_metrics().write.add(size.into_u64()); } @@ -1370,7 +1299,6 @@ static SYNC_MODE: AtomicU8 = AtomicU8::new(SyncMode::Sync as u8); #[cfg(test)] mod tests { - use std::io::Write; use std::os::unix::fs::FileExt; use std::sync::Arc; @@ -1429,19 +1357,6 @@ mod tests { MaybeVirtualFile::File(file) => file.seek(pos), } } - async fn write_all( - &mut self, - buf: FullSlice, - ctx: &RequestContext, - ) -> Result<(), Error> { - match self { - MaybeVirtualFile::VirtualFile(file) => { - let (_buf, res) = file.write_all(buf, ctx).await; - res.map(|_| ()) - } - MaybeVirtualFile::File(file) => file.write_all(&buf[..]), - } - } // Helper function to slurp contents of a file, starting at the current position, // into a string @@ -1555,7 +1470,7 @@ mod tests { .await?; file_a - .write_all(b"foobar".to_vec().slice_len(), &ctx) + .write_all_at(IoBuffer::from(b"foobar").slice_len(), 0, &ctx) .await?; // cannot read from a file opened in write-only mode @@ -1566,7 +1481,7 @@ mod tests { // cannot write to a file opened in read-only mode let _ = file_a - .write_all(b"bar".to_vec().slice_len(), &ctx) + .write_all_at(IoBuffer::from(b"bar").slice_len(), 0, &ctx) .await .unwrap_err();