Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
problame committed Jul 29, 2024
1 parent f45613b commit 59f5025
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pageserver/src/tenant/ephemeral_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl EphemeralFile {
})
}

pub(crate) fn len(&self) -> u64 {
pub(crate) fn len(&self) -> u32 {
self.rw.bytes_written()
}

Expand All @@ -83,7 +83,7 @@ impl EphemeralFile {
&mut self,
srcbuf: &[u8],
ctx: &RequestContext,
) -> Result<u64, io::Error> {
) -> Result<u32, io::Error> {
let pos = self.rw.bytes_written();

// Write the length field
Expand Down
2 changes: 1 addition & 1 deletion pageserver/src/tenant/ephemeral_file/page_caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl RW {
self.rw.write_all_borrowed(srcbuf, ctx).await
}

pub(crate) fn bytes_written(&self) -> u64 {
pub(crate) fn bytes_written(&self) -> u32 {
self.rw.bytes_written()
}

Expand Down
25 changes: 13 additions & 12 deletions pageserver/src/tenant/ephemeral_file/zero_padded_read_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ where
self.buffered_writer.write_buffered_borrowed(buf, ctx).await
}

pub fn bytes_written(&self) -> u64 {
pub fn bytes_written(&self) -> u32 {
let flushed_offset = self.buffered_writer.as_inner().bytes_written();
let buffer: &zero_padded::Buffer<TAIL_SZ> = self.buffered_writer.inspect_buffer();
flushed_offset + u64::try_from(buffer.pending()).unwrap()
flushed_offset + u32::try_from(buffer.pending()).unwrap()
}

/// Get a slice of all blocks that [`Self::read_blk`] would return as [`ReadResult::ServedFromZeroPaddedMutableTail`].
Expand All @@ -91,10 +91,11 @@ where
}

pub(crate) async fn read_blk(&self, blknum: u32) -> Result<ReadResult<'_, W>, std::io::Error> {
let flushed_offset = self.buffered_writer.as_inner().bytes_written();
let flushed_offset = u32::try_from(self.buffered_writer.as_inner().bytes_written()).expect("");
let buffer: &zero_padded::Buffer<TAIL_SZ> = self.buffered_writer.inspect_buffer();
let buffered_offset = flushed_offset + u64::try_from(buffer.pending()).unwrap();
let read_offset = (blknum as u64) * (PAGE_SZ as u64);
let buffered_offset = flushed_offset + u32::try_from(buffer.pending()).unwrap();
let page_sz = u32::try_from(PAGE_SZ).unwrap();
let read_offset = blknum.checked_mul(page_sz).unwrap();

// The trailing page ("block") might only be partially filled,
// yet the blob_io code relies on us to return a full PAGE_SZed slice anyway.
Expand All @@ -103,28 +104,28 @@ where
// DeltaLayer probably has the same issue, not sure why it needs no special treatment.
// => check here that the read doesn't go beyond this potentially trailing
// => the zero-padding is done in the `else` branch below
let blocks_written = if buffered_offset % (PAGE_SZ as u64) == 0 {
buffered_offset / (PAGE_SZ as u64)
let blocks_written = if buffered_offset % page_sz == 0 {
buffered_offset / page_sz
} else {
(buffered_offset / (PAGE_SZ as u64)) + 1
(buffered_offset / page_sz) + 1
};
if (blknum as u64) >= blocks_written {
if blknum >= blocks_written {
return Err(std::io::Error::new(std::io::ErrorKind::Other, anyhow::anyhow!("read past end of ephemeral_file: read=0x{read_offset:x} buffered=0x{buffered_offset:x} flushed=0x{flushed_offset}")));
}

// assertions for the `if-else` below
assert_eq!(
flushed_offset % (TAIL_SZ as u64), 0,
flushed_offset % (u32::try_from(TAIL_SZ).unwrap()), 0,
"we only use write_buffered_borrowed to write to the buffered writer, so it's guaranteed that flushes happen buffer.cap()-sized chunks"
);
assert_eq!(
flushed_offset % (PAGE_SZ as u64),
flushed_offset % page_sz,
0,
"the logic below can't handle if the page is spread across the flushed part and the buffer"
);

if read_offset < flushed_offset {
assert!(read_offset + (PAGE_SZ as u64) <= flushed_offset);
assert!(read_offset + page_sz <= flushed_offset);
Ok(ReadResult::NeedsReadFromWriter {
writer: self.as_writer(),
})
Expand Down

0 comments on commit 59f5025

Please sign in to comment.