Line data Source code
1 : //!
2 : //! Low-level Block-oriented I/O functions
3 : //!
4 :
5 : use super::ephemeral_file::EphemeralFile;
6 : use super::storage_layer::delta_layer::{Adapter, DeltaLayerInner};
7 : use crate::context::RequestContext;
8 : use crate::page_cache::{self, FileId, PageReadGuard, PageWriteGuard, ReadBufResult, PAGE_SZ};
9 : use crate::virtual_file::VirtualFile;
10 : use bytes::Bytes;
11 : use std::ops::Deref;
12 :
13 : /// This is implemented by anything that can read 8 kB (PAGE_SZ)
14 : /// blocks, using the page cache
15 : ///
16 : /// There are currently two implementations: EphemeralFile, and FileBlockReader
17 : /// below.
18 : pub trait BlockReader {
19 : ///
20 : /// Create a new "cursor" for reading from this reader.
21 : ///
22 : /// A cursor caches the last accessed page, allowing for faster
23 : /// access if the same block is accessed repeatedly.
24 : fn block_cursor(&self) -> BlockCursor<'_>;
25 : }
26 :
27 : impl<B> BlockReader for &B
28 : where
29 : B: BlockReader,
30 : {
31 211522 : fn block_cursor(&self) -> BlockCursor<'_> {
32 211522 : (*self).block_cursor()
33 211522 : }
34 : }
35 :
36 : /// Reference to an in-memory copy of an immutable on-disk block.
37 : pub enum BlockLease<'a> {
38 : PageReadGuard(PageReadGuard<'static>),
39 : EphemeralFileMutableTail(&'a [u8; PAGE_SZ]),
40 : Slice(&'a [u8; PAGE_SZ]),
41 : #[cfg(test)]
42 : Arc(std::sync::Arc<[u8; PAGE_SZ]>),
43 : #[cfg(test)]
44 : Vec(Vec<u8>),
45 : }
46 :
47 : impl From<PageReadGuard<'static>> for BlockLease<'static> {
48 2741305 : fn from(value: PageReadGuard<'static>) -> BlockLease<'static> {
49 2741305 : BlockLease::PageReadGuard(value)
50 2741305 : }
51 : }
52 :
53 : #[cfg(test)]
54 : impl<'a> From<std::sync::Arc<[u8; PAGE_SZ]>> for BlockLease<'a> {
55 1016862 : fn from(value: std::sync::Arc<[u8; PAGE_SZ]>) -> Self {
56 1016862 : BlockLease::Arc(value)
57 1016862 : }
58 : }
59 :
60 : impl<'a> Deref for BlockLease<'a> {
61 : type Target = [u8; PAGE_SZ];
62 :
63 15855194 : fn deref(&self) -> &Self::Target {
64 15855194 : match self {
65 13500432 : BlockLease::PageReadGuard(v) => v.deref(),
66 1302608 : BlockLease::EphemeralFileMutableTail(v) => v,
67 0 : BlockLease::Slice(v) => v,
68 : #[cfg(test)]
69 1016862 : BlockLease::Arc(v) => v.deref(),
70 : #[cfg(test)]
71 35292 : BlockLease::Vec(v) => {
72 35292 : TryFrom::try_from(&v[..]).expect("caller must ensure that v has PAGE_SZ")
73 : }
74 : }
75 15855194 : }
76 : }
77 :
78 : /// Provides the ability to read blocks from different sources,
79 : /// similar to using traits for this purpose.
80 : ///
81 : /// Unlike traits, we also support the read function to be async though.
82 : pub(crate) enum BlockReaderRef<'a> {
83 : FileBlockReader(&'a FileBlockReader<'a>),
84 : EphemeralFile(&'a EphemeralFile),
85 : Adapter(Adapter<&'a DeltaLayerInner>),
86 : Slice(&'a [u8]),
87 : #[cfg(test)]
88 : TestDisk(&'a super::disk_btree::tests::TestDisk),
89 : #[cfg(test)]
90 : VirtualFile(&'a VirtualFile),
91 : }
92 :
93 : impl<'a> BlockReaderRef<'a> {
94 : #[inline(always)]
95 8733865 : async fn read_blk(
96 8733865 : &self,
97 8733865 : blknum: u32,
98 8733865 : ctx: &RequestContext,
99 8733865 : ) -> Result<BlockLease, std::io::Error> {
100 8733865 : use BlockReaderRef::*;
101 8733865 : match self {
102 657195 : FileBlockReader(r) => r.read_blk(blknum, ctx).await,
103 4955890 : EphemeralFile(r) => r.read_blk(blknum, ctx).await,
104 2083002 : Adapter(r) => r.read_blk(blknum, ctx).await,
105 0 : Slice(s) => Self::read_blk_slice(s, blknum),
106 : #[cfg(test)]
107 1016862 : TestDisk(r) => r.read_blk(blknum),
108 : #[cfg(test)]
109 20916 : VirtualFile(r) => r.read_blk(blknum, ctx).await,
110 : }
111 8733865 : }
112 : }
113 :
114 : impl<'a> BlockReaderRef<'a> {
115 0 : fn read_blk_slice(slice: &[u8], blknum: u32) -> std::io::Result<BlockLease> {
116 0 : let start = (blknum as usize).checked_mul(PAGE_SZ).unwrap();
117 0 : let end = start.checked_add(PAGE_SZ).unwrap();
118 0 : if end > slice.len() {
119 0 : return Err(std::io::Error::new(
120 0 : std::io::ErrorKind::UnexpectedEof,
121 0 : format!("slice too short, len={} end={}", slice.len(), end),
122 0 : ));
123 0 : }
124 0 : let slice = &slice[start..end];
125 0 : let page_sized: &[u8; PAGE_SZ] = slice
126 0 : .try_into()
127 0 : .expect("we add PAGE_SZ to start, so the slice must have PAGE_SZ");
128 0 : Ok(BlockLease::Slice(page_sized))
129 0 : }
130 : }
131 :
132 : ///
133 : /// A "cursor" for efficiently reading multiple pages from a BlockReader
134 : ///
135 : /// You can access the last page with `*cursor`. 'read_blk' returns 'self', so
136 : /// that in many cases you can use a BlockCursor as a drop-in replacement for
137 : /// the underlying BlockReader. For example:
138 : ///
139 : /// ```no_run
140 : /// # use pageserver::tenant::block_io::{BlockReader, FileBlockReader};
141 : /// # use pageserver::context::RequestContext;
142 : /// # let reader: FileBlockReader = unimplemented!("stub");
143 : /// # let ctx: RequestContext = unimplemented!("stub");
144 : /// let cursor = reader.block_cursor();
145 : /// let buf = cursor.read_blk(1, &ctx);
146 : /// // do stuff with 'buf'
147 : /// let buf = cursor.read_blk(2, &ctx);
148 : /// // do stuff with 'buf'
149 : /// ```
150 : ///
151 : pub struct BlockCursor<'a> {
152 : pub(super) read_compressed: bool,
153 : reader: BlockReaderRef<'a>,
154 : }
155 :
156 : impl<'a> BlockCursor<'a> {
157 3082647 : pub(crate) fn new(reader: BlockReaderRef<'a>) -> Self {
158 3082647 : Self::new_with_compression(reader, false)
159 3082647 : }
160 3570781 : pub(crate) fn new_with_compression(reader: BlockReaderRef<'a>, read_compressed: bool) -> Self {
161 3570781 : BlockCursor {
162 3570781 : read_compressed,
163 3570781 : reader,
164 3570781 : }
165 3570781 : }
166 : // Needed by cli
167 0 : pub fn new_fileblockreader(reader: &'a FileBlockReader) -> Self {
168 0 : BlockCursor {
169 0 : read_compressed: false,
170 0 : reader: BlockReaderRef::FileBlockReader(reader),
171 0 : }
172 0 : }
173 :
174 : /// Read a block.
175 : ///
176 : /// Returns a "lease" object that can be used to
177 : /// access to the contents of the page. (For the page cache, the
178 : /// lease object represents a lock on the buffer.)
179 : #[inline(always)]
180 8733865 : pub async fn read_blk(
181 8733865 : &self,
182 8733865 : blknum: u32,
183 8733865 : ctx: &RequestContext,
184 8733865 : ) -> Result<BlockLease, std::io::Error> {
185 8733865 : self.reader.read_blk(blknum, ctx).await
186 8733865 : }
187 : }
188 :
189 : /// An adapter for reading a (virtual) file using the page cache.
190 : ///
191 : /// The file is assumed to be immutable. This doesn't provide any functions
192 : /// for modifying the file, nor for invalidating the cache if it is modified.
193 : #[derive(Clone)]
194 : pub struct FileBlockReader<'a> {
195 : pub file: &'a VirtualFile,
196 :
197 : /// Unique ID of this file, used as key in the page cache.
198 : file_id: page_cache::FileId,
199 :
200 : compressed_reads: bool,
201 : }
202 :
203 : impl<'a> FileBlockReader<'a> {
204 2296640 : pub fn new(file: &'a VirtualFile, file_id: FileId) -> Self {
205 2296640 : FileBlockReader {
206 2296640 : file_id,
207 2296640 : file,
208 2296640 : compressed_reads: true,
209 2296640 : }
210 2296640 : }
211 :
212 : /// Read a page from the underlying file into given buffer.
213 60065 : async fn fill_buffer(
214 60065 : &self,
215 60065 : buf: PageWriteGuard<'static>,
216 60065 : blkno: u32,
217 60065 : ctx: &RequestContext,
218 60065 : ) -> Result<PageWriteGuard<'static>, std::io::Error> {
219 60065 : assert!(buf.len() == PAGE_SZ);
220 60065 : self.file
221 60065 : .read_exact_at_page(buf, blkno as u64 * PAGE_SZ as u64, ctx)
222 36456 : .await
223 60065 : }
224 : /// Read a block.
225 : ///
226 : /// Returns a "lease" object that can be used to
227 : /// access to the contents of the page. (For the page cache, the
228 : /// lease object represents a lock on the buffer.)
229 2741305 : pub async fn read_blk<'b>(
230 2741305 : &self,
231 2741305 : blknum: u32,
232 2741305 : ctx: &RequestContext,
233 2741305 : ) -> Result<BlockLease<'b>, std::io::Error> {
234 2741305 : let cache = page_cache::get();
235 2741305 : match cache
236 2741305 : .read_immutable_buf(self.file_id, blknum, ctx)
237 34012 : .await
238 2741305 : .map_err(|e| {
239 0 : std::io::Error::new(
240 0 : std::io::ErrorKind::Other,
241 0 : format!("Failed to read immutable buf: {e:#}"),
242 0 : )
243 2741305 : })? {
244 2681240 : ReadBufResult::Found(guard) => Ok(guard.into()),
245 60065 : ReadBufResult::NotFound(write_guard) => {
246 : // Read the page from disk into the buffer
247 60065 : let write_guard = self.fill_buffer(write_guard, blknum, ctx).await?;
248 60065 : Ok(write_guard.mark_valid().into())
249 : }
250 : }
251 2741305 : }
252 : }
253 :
254 : impl BlockReader for FileBlockReader<'_> {
255 488102 : fn block_cursor(&self) -> BlockCursor<'_> {
256 488102 : BlockCursor::new_with_compression(
257 488102 : BlockReaderRef::FileBlockReader(self),
258 488102 : self.compressed_reads,
259 488102 : )
260 488102 : }
261 : }
262 :
263 : ///
264 : /// Trait for block-oriented output
265 : ///
266 : pub trait BlockWriter {
267 : ///
268 : /// Write a page to the underlying storage.
269 : ///
270 : /// 'buf' must be of size PAGE_SZ. Returns the block number the page was
271 : /// written to.
272 : ///
273 : fn write_blk(&mut self, buf: Bytes) -> Result<u32, std::io::Error>;
274 : }
275 :
276 : ///
277 : /// A simple in-memory buffer of blocks.
278 : ///
279 : pub struct BlockBuf {
280 : pub blocks: Vec<Bytes>,
281 : }
282 : impl BlockWriter for BlockBuf {
283 14258 : fn write_blk(&mut self, buf: Bytes) -> Result<u32, std::io::Error> {
284 14258 : assert!(buf.len() == PAGE_SZ);
285 14258 : let blknum = self.blocks.len();
286 14258 : self.blocks.push(buf);
287 14258 : Ok(blknum as u32)
288 14258 : }
289 : }
290 :
291 : impl BlockBuf {
292 1596 : pub fn new() -> Self {
293 1596 : BlockBuf { blocks: Vec::new() }
294 1596 : }
295 :
296 2023972 : pub fn size(&self) -> u64 {
297 2023972 : (self.blocks.len() * PAGE_SZ) as u64
298 2023972 : }
299 : }
300 : impl Default for BlockBuf {
301 0 : fn default() -> Self {
302 0 : Self::new()
303 0 : }
304 : }
|