Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ use foyer::{
};
use tempfile::tempdir;

fn nonzero(value: usize) -> NonZeroUsize {
match NonZeroUsize::new(value) {
Some(value) => value,
None => unreachable!("example constants are nonzero"),
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let dir = tempdir()?;
Expand All @@ -142,7 +149,7 @@ async fn main() -> anyhow::Result<()> {
.with_write_iops(2000)
.with_write_throughput(100 * 1024 * 1024)
.with_read_throughput(800 * 1024 * 1024)
.with_iops_counter(IopsCounter::PerIoSize(NonZeroUsize::new(128 * 1024).unwrap())),
.with_iops_counter(IopsCounter::PerIoSize(nonzero(128 * 1024))),
)
.build()?;

Expand All @@ -162,10 +169,10 @@ async fn main() -> anyhow::Result<()> {
.with_engine_config(
BlockEngineConfig::new(device)
.with_block_size(16 * 1024 * 1024)
.with_indexer_shards(64)
.with_recover_concurrency(8)
.with_flushers(2)
.with_reclaimers(2)
.with_indexer_shards(nonzero(64))
.with_recover_concurrency(nonzero(8))
.with_flushers(nonzero(2))
.with_reclaimers(nonzero(2))
.with_buffer_pool_size(256 * 1024 * 1024)
.with_clean_block_threshold(4)
.with_eviction_pickers(vec![Box::<FifoPicker>::default()])
Expand Down
17 changes: 12 additions & 5 deletions examples/hybrid_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ use foyer::{
};
use tempfile::tempdir;

fn nonzero(value: usize) -> NonZeroUsize {
match NonZeroUsize::new(value) {
Some(value) => value,
None => unreachable!("example constants are nonzero"),
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let dir = tempdir()?;
Expand All @@ -32,7 +39,7 @@ async fn main() -> anyhow::Result<()> {
.with_write_iops(2000)
.with_write_throughput(100 * 1024 * 1024)
.with_read_throughput(800 * 1024 * 1024)
.with_iops_counter(IopsCounter::PerIoSize(NonZeroUsize::new(128 * 1024).unwrap())),
.with_iops_counter(IopsCounter::PerIoSize(nonzero(128 * 1024))),
)
.build()?;

Expand All @@ -52,10 +59,10 @@ async fn main() -> anyhow::Result<()> {
.with_engine_config(
BlockEngineConfig::new(device)
.with_block_size(16 * 1024 * 1024)
.with_indexer_shards(64)
.with_recover_concurrency(8)
.with_flushers(2)
.with_reclaimers(2)
.with_indexer_shards(nonzero(64))
.with_recover_concurrency(nonzero(8))
.with_flushers(nonzero(2))
.with_reclaimers(nonzero(2))
.with_buffer_pool_size(256 * 1024 * 1024)
.with_clean_block_threshold(4)
.with_eviction_pickers(vec![Box::<FifoPicker>::default()])
Expand Down
29 changes: 15 additions & 14 deletions foyer-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
collections::BTreeMap,
fs::create_dir_all,
net::SocketAddr,
num::NonZeroUsize,
ops::{Deref, Range},
sync::{
Arc,
Expand Down Expand Up @@ -127,12 +128,12 @@ struct Args {
block_size: ByteSize,

/// Flusher count.
#[arg(long, default_value_t = 4)]
flushers: usize,
#[arg(long, default_value = "4")]
flushers: NonZeroUsize,

/// Reclaimer count.
#[arg(long, default_value_t = 4)]
reclaimers: usize,
#[arg(long, default_value = "4")]
reclaimers: NonZeroUsize,

/// Writer count.
#[arg(long, default_value_t = 16)]
Expand All @@ -146,8 +147,8 @@ struct Args {
recover_mode: RecoverMode,

/// Recover concurrency.
#[arg(long, default_value_t = 16)]
recover_concurrency: usize,
#[arg(long, default_value = "16")]
recover_concurrency: NonZeroUsize,

/// Disk write iops throttle.
#[arg(long, default_value_t = 0)]
Expand All @@ -170,8 +171,8 @@ struct Args {
clean_block_threshold: usize,

/// Shards of both in-memory cache and disk cache indexer.
#[arg(long, default_value_t = 64)]
shards: usize,
#[arg(long, default_value = "64")]
shards: NonZeroUsize,

/// weigher to enable metrics exporter
#[arg(long, default_value_t = false)]
Expand Down Expand Up @@ -277,14 +278,14 @@ struct Args {
#[arg(long, default_value_t = false)]
direct: bool,

#[arg(long, default_value_t = 1)]
io_uring_threads: usize,
#[arg(long, default_value = "1")]
io_uring_threads: NonZeroUsize,

#[arg(long, required = false)]
io_uring_cpus: Vec<u32>,

#[arg(long, default_value_t = 64)]
io_uring_iodepth: usize,
#[arg(long, default_value = "64")]
io_uring_iodepth: NonZeroUsize,

#[arg(long, default_value_t = false)]
io_uring_sqpoll: bool,
Expand Down Expand Up @@ -516,9 +517,9 @@ async fn benchmark(args: Args) {
builder
.with_metrics_registry(Box::new(PrometheusMetricsRegistry::new(registry)))
.memory(args.mem.as_u64() as _)
.with_shards(args.shards)
.with_shards(args.shards.get())
} else {
builder.memory(args.mem.as_u64() as _).with_shards(args.shards)
builder.memory(args.mem.as_u64() as _).with_shards(args.shards.get())
};

let builder = match args.eviction.as_str() {
Expand Down
65 changes: 37 additions & 28 deletions foyer-storage/src/engine/block/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
fmt::Debug,
future::Future,
marker::PhantomData,
num::NonZeroUsize,
sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
Expand Down Expand Up @@ -84,10 +85,10 @@ where
device: Arc<dyn Device>,
block_size: usize,
compression: Compression,
indexer_shards: usize,
recover_concurrency: usize,
flushers: usize,
reclaimers: usize,
indexer_shards: NonZeroUsize,
recover_concurrency: NonZeroUsize,
flushers: NonZeroUsize,
reclaimers: NonZeroUsize,
buffer_pool_size: usize,
blob_index_size: usize,
submit_queue_size_threshold: usize,
Expand All @@ -103,6 +104,13 @@ where
marker: PhantomData<(K, V, P)>,
}

fn nonzero(value: usize) -> NonZeroUsize {
match NonZeroUsize::new(value) {
Some(value) => value,
None => unreachable!("default block engine counts are nonzero"),
}
}

impl<K, V, P> Debug for BlockEngineConfig<K, V, P>
where
K: StorageKey,
Expand Down Expand Up @@ -142,10 +150,10 @@ where
device,
block_size: 16 * 1024 * 1024, // 16 MiB
compression: Compression::default(),
indexer_shards: 64,
recover_concurrency: 8,
flushers: 1,
reclaimers: 1,
indexer_shards: nonzero(64),
recover_concurrency: nonzero(8),
flushers: nonzero(1),
reclaimers: nonzero(1),
buffer_pool_size: 16 * 1024 * 1024, // 16 MiB
blob_index_size: 4 * 1024, // 4 KiB
submit_queue_size_threshold: 16 * 1024 * 1024, // 16 MiB
Expand Down Expand Up @@ -178,15 +186,15 @@ where
/// Set the shard num of the indexer. Each shard has its own lock.
///
/// Default: `64`.
pub fn with_indexer_shards(mut self, indexer_shards: usize) -> Self {
pub fn with_indexer_shards(mut self, indexer_shards: NonZeroUsize) -> Self {
self.indexer_shards = indexer_shards;
self
}

/// Set the recover concurrency for the disk cache store.
///
/// Default: `8`.
pub fn with_recover_concurrency(mut self, recover_concurrency: usize) -> Self {
pub fn with_recover_concurrency(mut self, recover_concurrency: NonZeroUsize) -> Self {
self.recover_concurrency = recover_concurrency;
self
}
Expand All @@ -196,7 +204,7 @@ where
/// The flusher count limits how many blocks can be concurrently written.
///
/// Default: `1`.
pub fn with_flushers(mut self, flushers: usize) -> Self {
pub fn with_flushers(mut self, flushers: NonZeroUsize) -> Self {
self.flushers = flushers;
self
}
Expand All @@ -216,7 +224,7 @@ where
/// The reclaimer count limits how many blocks can be concurrently reclaimed.
///
/// Default: `1`.
pub fn with_reclaimers(mut self, reclaimers: usize) -> Self {
pub fn with_reclaimers(mut self, reclaimers: NonZeroUsize) -> Self {
self.reclaimers = reclaimers;
self
}
Expand Down Expand Up @@ -355,13 +363,14 @@ where
None
};

let indexer = Indexer::new(self.indexer_shards);
let indexer = Indexer::new(self.indexer_shards.get());
let submit_queue_size = Arc::<AtomicUsize>::default();

#[expect(clippy::type_complexity)]
let (flushers, rxs): (Vec<Flusher<K, V, P>>, Vec<UnboundedReceiver<Submission<K, V, P>>>) = (0..self.flushers)
.map(|id| Flusher::<K, V, P>::new(id, submit_queue_size.clone(), metrics.clone()))
.unzip();
let (flushers, rxs): (Vec<Flusher<K, V, P>>, Vec<UnboundedReceiver<Submission<K, V, P>>>) =
(0..self.flushers.get())
.map(|id| Flusher::<K, V, P>::new(id, submit_queue_size.clone(), metrics.clone()))
.unzip();

let reclaimer = Reclaimer::new(
indexer.clone(),
Expand All @@ -379,14 +388,14 @@ where
block_size,
self.eviction_pickers,
reclaimer,
self.reclaimers,
self.reclaimers.get(),
self.clean_block_threshold,
metrics.clone(),
runtime.clone(),
)?;
let blocks = block_manager.blocks();

if self.flushers + self.clean_block_threshold > blocks / 2 {
if self.flushers.get() + self.clean_block_threshold > blocks / 2 {
tracing::warn!(
"[block engine]: block-based object disk cache stable blocks count is too small, flusher [{flushers}] + clean block threshold [{clean_block_threshold}] (default = reclaimers) is supposed to be much larger than the block count [{blocks}]",
flushers = self.flushers,
Expand All @@ -397,7 +406,7 @@ where
let sequence = AtomicSequence::default();

RecoverRunner::run(
self.recover_concurrency,
self.recover_concurrency.get(),
recover_mode,
self.blob_index_size,
(0..blocks as BlockId).collect_vec(),
Expand All @@ -410,7 +419,7 @@ where
)
.await?;

let io_buffer_size = self.buffer_pool_size / self.flushers;
let io_buffer_size = self.buffer_pool_size / self.flushers.get();
for (flusher, rx) in flushers.iter().zip(rxs) {
flusher.run(
rx,
Expand Down Expand Up @@ -905,10 +914,10 @@ mod tests {
device,
block_size: 16 * 1024,
compression: Compression::None,
indexer_shards: 4,
recover_concurrency: 2,
flushers: 1,
reclaimers: 1,
indexer_shards: nonzero(4),
recover_concurrency: nonzero(2),
flushers: nonzero(1),
reclaimers: nonzero(1),
clean_block_threshold: 1,
admission_filter: StorageFilter::new(),
eviction_pickers: vec![Box::<FifoPicker>::default()],
Expand Down Expand Up @@ -948,10 +957,10 @@ mod tests {
device,
block_size: 16 * 1024,
compression: Compression::None,
indexer_shards: 4,
recover_concurrency: 2,
flushers: 1,
reclaimers: 1,
indexer_shards: nonzero(4),
recover_concurrency: nonzero(2),
flushers: nonzero(1),
reclaimers: nonzero(1),
clean_block_threshold: 1,
eviction_pickers: vec![Box::<FifoPicker>::default()],
admission_filter: StorageFilter::new(),
Expand Down
Loading