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 211302 : fn block_cursor(&self) -> BlockCursor<'_> {
32 211302 : (*self).block_cursor()
33 211302 : }
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 2740435 : fn from(value: PageReadGuard<'static>) -> BlockLease<'static> {
49 2740435 : BlockLease::PageReadGuard(value)
50 2740435 : }
51 : }
52 :
53 : #[cfg(test)]
54 : impl<'a> From<std::sync::Arc<[u8; PAGE_SZ]>> for BlockLease<'a> {
55 1016806 : fn from(value: std::sync::Arc<[u8; PAGE_SZ]>) -> Self {
56 1016806 : BlockLease::Arc(value)
57 1016806 : }
58 : }
59 :
60 : impl<'a> Deref for BlockLease<'a> {
61 : type Target = [u8; PAGE_SZ];
62 :
63 15854189 : fn deref(&self) -> &Self::Target {
64 15854189 : match self {
65 13499751 : BlockLease::PageReadGuard(v) => v.deref(),
66 1302368 : BlockLease::EphemeralFileMutableTail(v) => v,
67 0 : BlockLease::Slice(v) => v,
68 : #[cfg(test)]
69 1016806 : BlockLease::Arc(v) => v.deref(),
70 : #[cfg(test)]
71 35264 : BlockLease::Vec(v) => {
72 35264 : TryFrom::try_from(&v[..]).expect("caller must ensure that v has PAGE_SZ")
73 : }
74 : }
75 15854189 : }
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 8732888 : async fn read_blk(
96 8732888 : &self,
97 8732888 : blknum: u32,
98 8732888 : ctx: &RequestContext,
99 8732888 : ) -> Result<BlockLease, std::io::Error> {
100 8732888 : use BlockReaderRef::*;
101 8732888 : match self {
102 656343 : FileBlockReader(r) => r.read_blk(blknum, ctx).await,
103 4955837 : 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 1016806 : TestDisk(r) => r.read_blk(blknum),
108 : #[cfg(test)]
109 20900 : VirtualFile(r) => r.read_blk(blknum, ctx).await,
110 : }
111 8732888 : }
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 : reader: BlockReaderRef<'a>,
153 : }
154 :
155 : impl<'a> BlockCursor<'a> {
156 3570041 : pub(crate) fn new(reader: BlockReaderRef<'a>) -> Self {
157 3570041 : BlockCursor { reader }
158 3570041 : }
159 : // Needed by cli
160 0 : pub fn new_fileblockreader(reader: &'a FileBlockReader) -> Self {
161 0 : BlockCursor {
162 0 : reader: BlockReaderRef::FileBlockReader(reader),
163 0 : }
164 0 : }
165 :
166 : /// Read a block.
167 : ///
168 : /// Returns a "lease" object that can be used to
169 : /// access to the contents of the page. (For the page cache, the
170 : /// lease object represents a lock on the buffer.)
171 : #[inline(always)]
172 8732888 : pub async fn read_blk(
173 8732888 : &self,
174 8732888 : blknum: u32,
175 8732888 : ctx: &RequestContext,
176 8732888 : ) -> Result<BlockLease, std::io::Error> {
177 8732888 : self.reader.read_blk(blknum, ctx).await
178 8732888 : }
179 : }
180 :
181 : /// An adapter for reading a (virtual) file using the page cache.
182 : ///
183 : /// The file is assumed to be immutable. This doesn't provide any functions
184 : /// for modifying the file, nor for invalidating the cache if it is modified.
185 : #[derive(Clone)]
186 : pub struct FileBlockReader<'a> {
187 : pub file: &'a VirtualFile,
188 :
189 : /// Unique ID of this file, used as key in the page cache.
190 : file_id: page_cache::FileId,
191 : }
192 :
193 : impl<'a> FileBlockReader<'a> {
194 2296386 : pub fn new(file: &'a VirtualFile, file_id: FileId) -> Self {
195 2296386 : FileBlockReader { file_id, file }
196 2296386 : }
197 :
198 : /// Read a page from the underlying file into given buffer.
199 60030 : async fn fill_buffer(
200 60030 : &self,
201 60030 : buf: PageWriteGuard<'static>,
202 60030 : blkno: u32,
203 60030 : ctx: &RequestContext,
204 60030 : ) -> Result<PageWriteGuard<'static>, std::io::Error> {
205 60030 : assert!(buf.len() == PAGE_SZ);
206 60030 : self.file
207 60030 : .read_exact_at_page(buf, blkno as u64 * PAGE_SZ as u64, ctx)
208 36199 : .await
209 60030 : }
210 : /// Read a block.
211 : ///
212 : /// Returns a "lease" object that can be used to
213 : /// access to the contents of the page. (For the page cache, the
214 : /// lease object represents a lock on the buffer.)
215 2740435 : pub async fn read_blk<'b>(
216 2740435 : &self,
217 2740435 : blknum: u32,
218 2740435 : ctx: &RequestContext,
219 2740435 : ) -> Result<BlockLease<'b>, std::io::Error> {
220 2740435 : let cache = page_cache::get();
221 2740435 : match cache
222 2740435 : .read_immutable_buf(self.file_id, blknum, ctx)
223 33975 : .await
224 2740435 : .map_err(|e| {
225 0 : std::io::Error::new(
226 0 : std::io::ErrorKind::Other,
227 0 : format!("Failed to read immutable buf: {e:#}"),
228 0 : )
229 2740435 : })? {
230 2680405 : ReadBufResult::Found(guard) => Ok(guard.into()),
231 60030 : ReadBufResult::NotFound(write_guard) => {
232 : // Read the page from disk into the buffer
233 60030 : let write_guard = self.fill_buffer(write_guard, blknum, ctx).await?;
234 60030 : Ok(write_guard.mark_valid().into())
235 : }
236 : }
237 2740435 : }
238 : }
239 :
240 : impl BlockReader for FileBlockReader<'_> {
241 487473 : fn block_cursor(&self) -> BlockCursor<'_> {
242 487473 : BlockCursor::new(BlockReaderRef::FileBlockReader(self))
243 487473 : }
244 : }
245 :
246 : ///
247 : /// Trait for block-oriented output
248 : ///
249 : pub trait BlockWriter {
250 : ///
251 : /// Write a page to the underlying storage.
252 : ///
253 : /// 'buf' must be of size PAGE_SZ. Returns the block number the page was
254 : /// written to.
255 : ///
256 : fn write_blk(&mut self, buf: Bytes) -> Result<u32, std::io::Error>;
257 : }
258 :
259 : ///
260 : /// A simple in-memory buffer of blocks.
261 : ///
262 : pub struct BlockBuf {
263 : pub blocks: Vec<Bytes>,
264 : }
265 : impl BlockWriter for BlockBuf {
266 14227 : fn write_blk(&mut self, buf: Bytes) -> Result<u32, std::io::Error> {
267 14227 : assert!(buf.len() == PAGE_SZ);
268 14227 : let blknum = self.blocks.len();
269 14227 : self.blocks.push(buf);
270 14227 : Ok(blknum as u32)
271 14227 : }
272 : }
273 :
274 : impl BlockBuf {
275 1572 : pub fn new() -> Self {
276 1572 : BlockBuf { blocks: Vec::new() }
277 1572 : }
278 :
279 2023972 : pub fn size(&self) -> u64 {
280 2023972 : (self.blocks.len() * PAGE_SZ) as u64
281 2023972 : }
282 : }
283 : impl Default for BlockBuf {
284 0 : fn default() -> Self {
285 0 : Self::new()
286 0 : }
287 : }
|