Skip to content

Commit

Permalink
Uses consistent error types in tiered storage (#34110)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored Nov 16, 2023
1 parent f598870 commit 7e3b09d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
12 changes: 6 additions & 6 deletions accounts-db/src/tiered_storage/byte_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use {
crate::tiered_storage::{footer::AccountBlockFormat, meta::AccountMetaOptionalFields},
std::{
io::{Cursor, Read, Write},
io::{Cursor, Read, Result as IoResult, Write},
mem,
},
};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl ByteBlockWriter {

/// Write the specified typed instance to the internal buffer of
/// the ByteBlockWriter instance.
pub fn write_type<T>(&mut self, value: &T) -> std::io::Result<usize> {
pub fn write_type<T>(&mut self, value: &T) -> IoResult<usize> {
let size = mem::size_of::<T>();
let ptr = value as *const _ as *const u8;
let slice = unsafe { std::slice::from_raw_parts(ptr, size) };
Expand All @@ -70,7 +70,7 @@ impl ByteBlockWriter {
pub fn write_optional_fields(
&mut self,
opt_fields: &AccountMetaOptionalFields,
) -> std::io::Result<usize> {
) -> IoResult<usize> {
let mut size = 0;
if let Some(rent_epoch) = opt_fields.rent_epoch {
size += self.write_type(&rent_epoch)?;
Expand All @@ -86,7 +86,7 @@ impl ByteBlockWriter {

/// Write the specified typed bytes to the internal buffer of the
/// ByteBlockWriter instance.
pub fn write(&mut self, buf: &[u8]) -> std::io::Result<()> {
pub fn write(&mut self, buf: &[u8]) -> IoResult<()> {
match &mut self.encoder {
ByteBlockEncoder::Raw(cursor) => cursor.write_all(buf)?,
ByteBlockEncoder::Lz4(lz4_encoder) => lz4_encoder.write_all(buf)?,
Expand All @@ -97,7 +97,7 @@ impl ByteBlockWriter {

/// Flush the internal byte buffer that collects all the previous writes
/// into an encoded byte array.
pub fn finish(self) -> std::io::Result<Vec<u8>> {
pub fn finish(self) -> IoResult<Vec<u8>> {
match self.encoder {
ByteBlockEncoder::Raw(cursor) => Ok(cursor.into_inner()),
ByteBlockEncoder::Lz4(lz4_encoder) => {
Expand Down Expand Up @@ -134,7 +134,7 @@ impl ByteBlockReader {
///
/// Note that calling this function with AccountBlockFormat::AlignedRaw encoding
/// will result in panic as the input is already decoded.
pub fn decode(encoding: AccountBlockFormat, input: &[u8]) -> std::io::Result<Vec<u8>> {
pub fn decode(encoding: AccountBlockFormat, input: &[u8]) -> IoResult<Vec<u8>> {
match encoding {
AccountBlockFormat::Lz4 => {
let mut decoder = lz4::Decoder::new(input).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions accounts-db/src/tiered_storage/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fs::{File, OpenOptions},
io::{Read, Seek, SeekFrom, Write},
io::{Read, Result as IoResult, Seek, SeekFrom, Write},
mem,
path::Path,
};
Expand All @@ -25,7 +25,7 @@ impl TieredStorageFile {
)
}

pub fn new_writable(file_path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
pub fn new_writable(file_path: impl AsRef<Path>) -> IoResult<Self> {
Ok(Self(
OpenOptions::new()
.create_new(true)
Expand All @@ -34,37 +34,37 @@ impl TieredStorageFile {
))
}

pub fn write_type<T>(&self, value: &T) -> Result<usize, std::io::Error> {
pub fn write_type<T>(&self, value: &T) -> IoResult<usize> {
let ptr = value as *const _ as *const u8;
let slice = unsafe { std::slice::from_raw_parts(ptr, mem::size_of::<T>()) };
(&self.0).write_all(slice)?;

Ok(std::mem::size_of::<T>())
}

pub fn read_type<T>(&self, value: &mut T) -> Result<(), std::io::Error> {
pub fn read_type<T>(&self, value: &mut T) -> IoResult<()> {
let ptr = value as *mut _ as *mut u8;
let slice = unsafe { std::slice::from_raw_parts_mut(ptr, mem::size_of::<T>()) };
(&self.0).read_exact(slice)?;

Ok(())
}

pub fn seek(&self, offset: u64) -> Result<u64, std::io::Error> {
pub fn seek(&self, offset: u64) -> IoResult<u64> {
(&self.0).seek(SeekFrom::Start(offset))
}

pub fn seek_from_end(&self, offset: i64) -> Result<u64, std::io::Error> {
pub fn seek_from_end(&self, offset: i64) -> IoResult<u64> {
(&self.0).seek(SeekFrom::End(offset))
}

pub fn write_bytes(&self, bytes: &[u8]) -> Result<usize, std::io::Error> {
pub fn write_bytes(&self, bytes: &[u8]) -> IoResult<usize> {
(&self.0).write_all(bytes)?;

Ok(bytes.len())
}

pub fn read_bytes(&self, buffer: &mut [u8]) -> Result<(), std::io::Error> {
pub fn read_bytes(&self, buffer: &mut [u8]) -> IoResult<()> {
(&self.0).read_exact(buffer)?;

Ok(())
Expand Down
10 changes: 5 additions & 5 deletions accounts-db/src/tiered_storage/footer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::tiered_storage::{
error::TieredStorageError, file::TieredStorageFile, index::IndexBlockFormat,
mmap_utils::get_type, TieredStorageResult as TsResult,
mmap_utils::get_type, TieredStorageResult,
},
memmap2::Mmap,
solana_sdk::{hash::Hash, pubkey::Pubkey},
Expand Down Expand Up @@ -168,19 +168,19 @@ impl Default for TieredStorageFooter {
}

impl TieredStorageFooter {
pub fn new_from_path(path: impl AsRef<Path>) -> TsResult<Self> {
pub fn new_from_path(path: impl AsRef<Path>) -> TieredStorageResult<Self> {
let file = TieredStorageFile::new_readonly(path);
Self::new_from_footer_block(&file)
}

pub fn write_footer_block(&self, file: &TieredStorageFile) -> TsResult<()> {
pub fn write_footer_block(&self, file: &TieredStorageFile) -> TieredStorageResult<()> {
file.write_type(self)?;
file.write_type(&TieredStorageMagicNumber::default())?;

Ok(())
}

pub fn new_from_footer_block(file: &TieredStorageFile) -> TsResult<Self> {
pub fn new_from_footer_block(file: &TieredStorageFile) -> TieredStorageResult<Self> {
let mut footer_size: u64 = 0;
let mut footer_version: u64 = 0;
let mut magic_number = TieredStorageMagicNumber(0);
Expand All @@ -204,7 +204,7 @@ impl TieredStorageFooter {
Ok(footer)
}

pub fn new_from_mmap(map: &Mmap) -> TsResult<&TieredStorageFooter> {
pub fn new_from_mmap(map: &Mmap) -> TieredStorageResult<&TieredStorageFooter> {
let offset = map.len().saturating_sub(FOOTER_TAIL_SIZE);
let (footer_size, offset) = get_type::<u64>(map, offset)?;
let (_footer_version, offset) = get_type::<u64>(map, offset)?;
Expand Down
5 changes: 3 additions & 2 deletions accounts-db/src/tiered_storage/mmap_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use {
crate::{accounts_file::ALIGN_BOUNDARY_OFFSET, u64_align},
log::*,
memmap2::Mmap,
std::io::Result as IoResult,
};

pub fn get_type<T>(map: &Mmap, offset: usize) -> std::io::Result<(&T, usize)> {
pub fn get_type<T>(map: &Mmap, offset: usize) -> IoResult<(&T, usize)> {
let (data, next) = get_slice(map, offset, std::mem::size_of::<T>())?;
let ptr = data.as_ptr() as *const T;
debug_assert!(ptr as usize % std::mem::align_of::<T>() == 0);
Expand All @@ -15,7 +16,7 @@ pub fn get_type<T>(map: &Mmap, offset: usize) -> std::io::Result<(&T, usize)> {
/// doesn't overrun the internal buffer. Otherwise return an Error.
/// Also return the offset of the first byte after the requested data that
/// falls on a 64-byte boundary.
pub fn get_slice(map: &Mmap, offset: usize, size: usize) -> std::io::Result<(&[u8], usize)> {
pub fn get_slice(map: &Mmap, offset: usize, size: usize) -> IoResult<(&[u8], usize)> {
let (next, overflow) = offset.overflowing_add(size);
if overflow || next > map.len() {
error!(
Expand Down

0 comments on commit 7e3b09d

Please sign in to comment.