Skip to content

Commit

Permalink
Use types to help track different sizes
Browse files Browse the repository at this point in the history
Assign types to the four summary sizes from BlockDevMgr

Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Aug 29, 2024
1 parent de10442 commit 62e1ceb
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 90 deletions.
10 changes: 5 additions & 5 deletions src/engine/strat_engine/backstore/backstore/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
backstore::{
backstore::InternalBackstore,
blockdev::{v1::StratBlockDev, InternalBlockDev},
blockdevmgr::BlockDevMgr,
blockdevmgr::{BlockDevMgr, BlockDevMgrMetaSize, BlockDevMgrSize},
cache_tier::CacheTier,
data_tier::DataTier,
devices::UnownedDevices,
Expand Down Expand Up @@ -125,11 +125,11 @@ impl InternalBackstore for Backstore {
}

fn datatier_usable_size(&self) -> Sectors {
self.data_tier.usable_size()
self.data_tier.usable_size().sectors()
}

fn available_in_backstore(&self) -> Sectors {
self.data_tier.usable_size() - self.next
self.data_tier.usable_size().sectors() - self.next
}

fn alloc(
Expand Down Expand Up @@ -513,7 +513,7 @@ impl Backstore {
}

/// The current size of all the blockdevs in the data tier.
pub fn datatier_size(&self) -> Sectors {
pub fn datatier_size(&self) -> BlockDevMgrSize {
self.data_tier.size()
}

Expand Down Expand Up @@ -605,7 +605,7 @@ impl Backstore {

/// The number of sectors in the backstore given up to Stratis metadata
/// on devices in the data tier.
pub fn datatier_metadata_size(&self) -> Sectors {
pub fn datatier_metadata_size(&self) -> BlockDevMgrMetaSize {
self.data_tier.metadata_size()
}

Expand Down
16 changes: 10 additions & 6 deletions src/engine/strat_engine/backstore/backstore/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ use crate::{
engine::{
strat_engine::{
backstore::{
backstore::InternalBackstore, blockdev::v2::StratBlockDev,
blockdevmgr::BlockDevMgr, cache_tier::CacheTier, data_tier::DataTier,
devices::UnownedDevices, shared::BlockSizeSummary,
backstore::InternalBackstore,
blockdev::v2::StratBlockDev,
blockdevmgr::{BlockDevMgr, BlockDevMgrSize},
cache_tier::CacheTier,
data_tier::DataTier,
devices::UnownedDevices,
shared::BlockSizeSummary,
},
crypt::{crypt_metadata_size, handle::v2::CryptHandle, interpret_clevis_config},
dm::{get_dm, list_of_backstore_devices, remove_optional_devices, DEVICEMAPPER_PATH},
Expand Down Expand Up @@ -170,7 +174,7 @@ impl InternalBackstore for Backstore {
}

fn datatier_usable_size(&self) -> Sectors {
self.datatier_size() - self.datatier_metadata_size()
self.datatier_size().sectors() - self.datatier_metadata_size()
}

fn available_in_backstore(&self) -> Sectors {
Expand Down Expand Up @@ -750,7 +754,7 @@ impl Backstore {
}

/// The current size of all the blockdevs in the data tier.
pub fn datatier_size(&self) -> Sectors {
pub fn datatier_size(&self) -> BlockDevMgrSize {
self.data_tier.size()
}

Expand Down Expand Up @@ -857,7 +861,7 @@ impl Backstore {

/// Metadata size on the data tier, including crypt metadata space.
pub fn datatier_metadata_size(&self) -> Sectors {
self.datatier_crypt_meta_size() + self.data_tier.metadata_size()
self.datatier_crypt_meta_size() + self.data_tier.metadata_size().sectors()
}

/// Write the given data to the data tier's devices.
Expand Down
155 changes: 127 additions & 28 deletions src/engine/strat_engine/backstore/blockdevmgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,87 @@ impl TimeStamp {
}
}

/// Type for the block dev mgr size.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrSize(pub Sectors);

impl BlockDevMgrSize {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrSize
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrSize(blockdevs.iter().map(|b| b.total_size().sectors()).sum())
}
}

/// All metadata allocated to individual block devs in the backstore.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrMetaSize(pub Sectors);

impl BlockDevMgrMetaSize {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrMetaSize
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrMetaSize(blockdevs.iter().map(|bd| bd.metadata_size()).sum())
}
}

/// All space that has not been allocated for any purpose.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrAvailSpace(pub Sectors);

impl BlockDevMgrAvailSpace {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrAvailSpace
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrAvailSpace(blockdevs.iter().map(|bd| bd.available()).sum())
}
}

/// All space that has not been allocated to metadata on the devices.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BlockDevMgrUsableSpace(pub Sectors);

impl BlockDevMgrUsableSpace {
pub fn sectors(self) -> Sectors {
self.0
}
}

impl<B> From<&[B]> for BlockDevMgrUsableSpace
where
B: InternalBlockDev,
{
fn from(blockdevs: &[B]) -> Self {
BlockDevMgrUsableSpace(
blockdevs
.iter()
.map(|b| b.total_size().sectors() - b.metadata_size())
.sum(),
)
}
}

#[derive(Debug)]
pub struct BlockDevMgr<B> {
/// All the block devices that belong to this block dev manager.
Expand All @@ -77,6 +158,10 @@ pub struct BlockDevMgr<B> {
last_update_time: TimeStamp,
}

pub struct Sizer<'a, B> {
block_devs: &'a [B],
}

impl BlockDevMgr<v1::StratBlockDev> {
/// Initialize a new StratBlockDevMgr with specified pool and devices.
pub fn initialize(
Expand Down Expand Up @@ -249,6 +334,27 @@ impl BlockDevMgr<v2::StratBlockDev> {
}
}

impl<B> Sizer<'_, B>
where
B: InternalBlockDev,
{
pub fn avail_space(&self) -> BlockDevMgrAvailSpace {
self.block_devs.into()
}

pub fn size(&self) -> BlockDevMgrSize {
self.block_devs.into()
}

pub fn metadata_size(&self) -> BlockDevMgrMetaSize {
self.block_devs.into()
}

pub fn usable_size(&self) -> BlockDevMgrUsableSpace {
self.block_devs.into()
}
}

impl<B> BlockDevMgr<B>
where
B: InternalBlockDev,
Expand Down Expand Up @@ -346,7 +452,7 @@ where
/// nothing.
pub fn alloc(&mut self, sizes: &[Sectors]) -> Option<Vec<Vec<BlkDevSegment>>> {
let total_needed: Sectors = sizes.iter().cloned().sum();
if self.avail_space() < total_needed {
if self.sizer().avail_space().sectors() < total_needed {
return None;
}

Expand Down Expand Up @@ -444,27 +550,10 @@ where
})
}

// SIZE methods

/// The number of sectors not allocated for any purpose.
pub fn avail_space(&self) -> Sectors {
self.block_devs.iter().map(|bd| bd.available()).sum()
}

/// The current size of all the blockdevs.
/// self.size() > self.avail_space() because some sectors are certainly
/// allocated for Stratis metadata
pub fn size(&self) -> Sectors {
self.block_devs
.iter()
.map(|b| b.total_size().sectors())
.sum()
}

/// The number of sectors given over to Stratis metadata
/// self.allocated_size() - self.metadata_size() >= self.avail_space()
pub fn metadata_size(&self) -> Sectors {
self.block_devs.iter().map(|bd| bd.metadata_size()).sum()
pub fn sizer(&self) -> Sizer<'_, B> {
Sizer {
block_devs: &self.block_devs,
}
}

pub fn grow(&mut self, dev: DevUuid) -> StratisResult<bool> {
Expand Down Expand Up @@ -546,13 +635,18 @@ mod tests {
None,
)
.unwrap();
assert_eq!(mgr.avail_space() + mgr.metadata_size(), mgr.size());
assert_eq!(
mgr.sizer().avail_space().sectors() + mgr.sizer().metadata_size().sectors(),
mgr.sizer().size().sectors()
);

let allocated = Sectors(2);
mgr.alloc(&[allocated]).unwrap();
assert_eq!(
mgr.avail_space() + allocated + mgr.metadata_size(),
mgr.size()
mgr.sizer().avail_space().sectors()
+ allocated
+ mgr.sizer().metadata_size().sectors(),
mgr.sizer().size().sectors()
);
}

Expand Down Expand Up @@ -764,13 +858,18 @@ mod tests {
MDADataSize::default(),
)
.unwrap();
assert_eq!(mgr.avail_space() + mgr.metadata_size(), mgr.size());
assert_eq!(
mgr.sizer().avail_space().sectors() + mgr.sizer().metadata_size().sectors(),
mgr.sizer().size().sectors()
);

let allocated = Sectors(2);
mgr.alloc(&[allocated]).unwrap();
assert_eq!(
mgr.avail_space() + allocated + mgr.metadata_size(),
mgr.size()
mgr.sizer().avail_space().sectors()
+ allocated
+ mgr.sizer().metadata_size().sectors(),
mgr.sizer().size().sectors()
);
}

Expand Down
Loading

0 comments on commit 62e1ceb

Please sign in to comment.