Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support disabled, unified, and separated runtime #652

Merged
merged 1 commit into from
Aug 19, 2024
Merged
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
14 changes: 10 additions & 4 deletions examples/hybrid_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use anyhow::Result;
use chrono::Datelike;
use foyer::{
DirectFsDeviceOptionsBuilder, FifoPicker, HybridCache, HybridCacheBuilder, LruConfig, RateLimitPicker, RecoverMode,
RuntimeConfig, TombstoneLogConfigBuilder,
RuntimeConfig, TokioRuntimeConfig, TombstoneLogConfigBuilder,
};
use tempfile::tempdir;

Expand Down Expand Up @@ -59,9 +59,15 @@ async fn main() -> Result<()> {
.with_flush(true)
.build(),
)
.with_runtime_config(RuntimeConfig {
worker_threads: 4,
max_blocking_threads: 8,
.with_runtime_config(RuntimeConfig::Separated {
read_runtime_config: TokioRuntimeConfig {
worker_threads: 4,
max_blocking_threads: 8,
},
write_runtime_config: TokioRuntimeConfig {
worker_threads: 4,
max_blocking_threads: 8,
},
})
.build()
.await?;
Expand Down
69 changes: 58 additions & 11 deletions foyer-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use foyer::{
Compression, DirectFileDeviceOptionsBuilder, DirectFsDeviceOptionsBuilder, FifoConfig, FifoPicker, HybridCache,
HybridCacheBuilder, InvalidRatioPicker, LfuConfig, LruConfig, RateLimitPicker, RecoverMode, RuntimeConfig,
S3FifoConfig, TracingConfig,
S3FifoConfig, TokioRuntimeConfig, TracingConfig,
};
use metrics_exporter_prometheus::PrometheusBuilder;

Expand All @@ -37,7 +37,7 @@
};

use analyze::{analyze, monitor, Metrics};
use clap::{ArgGroup, Parser};
use clap::{builder::PossibleValuesParser, ArgGroup, Parser};

use futures::future::join_all;
use itertools::Itertools;
Expand Down Expand Up @@ -156,16 +156,49 @@
#[arg(long, default_value_t = false)]
metrics: bool,

/// Benchmark user runtime worker threads.
#[arg(long, default_value_t = 0)]
benchmark_runtime_worker_threads: usize,
user_runtime_worker_threads: usize,

Check warning on line 161 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L161

Added line #L161 was not covered by tests

/// dedicated runtime worker threads
/// Dedicated runtime type.
#[arg(long, value_parser = PossibleValuesParser::new(["disabled", "unified", "separated"]), default_value = "disabled")]
runtime: String,

Check warning on line 165 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L165

Added line #L165 was not covered by tests

/// Dedicated runtime worker threads.
///
/// Only valid when using unified dedicated runtime.
#[arg(long, default_value_t = 0)]
runtime_worker_threads: usize,

/// max threads for blocking io
/// Max threads for blocking io.
///
/// Only valid when using unified dedicated runtime.
#[arg(long, default_value_t = 0)]
runtime_max_blocking_threads: usize,

Check warning on line 177 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L177

Added line #L177 was not covered by tests

/// Dedicated runtime for writes worker threads.
///
/// Only valid when using separated dedicated runtime.
#[arg(long, default_value_t = 0)]
write_runtime_worker_threads: usize,

Check warning on line 183 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L183

Added line #L183 was not covered by tests

/// Dedicated runtime for writes Max threads for blocking io.
///
/// Only valid when using separated dedicated runtime.
#[arg(long, default_value_t = 0)]
write_runtime_max_blocking_threads: usize,

Check warning on line 189 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L189

Added line #L189 was not covered by tests

/// Dedicated runtime for reads worker threads.
///
/// Only valid when using separated dedicated runtime.
#[arg(long, default_value_t = 0)]
read_runtime_worker_threads: usize,

Check warning on line 195 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L195

Added line #L195 was not covered by tests

/// Dedicated runtime for writes max threads for blocking io.
///
/// Only valid when using separated dedicated runtime.
#[arg(long, default_value_t = 0)]
max_blocking_threads: usize,
read_runtime_max_blocking_threads: usize,

Check warning on line 201 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L201

Added line #L201 was not covered by tests

/// compression algorithm
#[arg(long, value_enum, default_value_t = Compression::None)]
Expand Down Expand Up @@ -379,8 +412,8 @@
println!("{:#?}", args);

let mut builder = tokio::runtime::Builder::new_multi_thread();
if args.benchmark_runtime_worker_threads != 0 {
builder.worker_threads(args.benchmark_runtime_worker_threads);
if args.user_runtime_worker_threads != 0 {
builder.worker_threads(args.user_runtime_worker_threads);

Check warning on line 416 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L416

Added line #L416 was not covered by tests
}
builder.thread_name("foyer-bench");
let runtime = builder.enable_all().build().unwrap();
Expand Down Expand Up @@ -484,9 +517,23 @@
Box::<FifoPicker>::default(),
])
.with_compression(args.compression)
.with_runtime_config(RuntimeConfig {
worker_threads: args.runtime_worker_threads,
max_blocking_threads: args.max_blocking_threads,
.with_runtime_config(match args.runtime.as_str() {
"disabled" => RuntimeConfig::Disabled,
"unified" => RuntimeConfig::Unified(TokioRuntimeConfig {
worker_threads: args.runtime_worker_threads,
max_blocking_threads: args.runtime_max_blocking_threads,
}),
"separated" => RuntimeConfig::Separated {
read_runtime_config: TokioRuntimeConfig {
worker_threads: args.read_runtime_worker_threads,
max_blocking_threads: args.read_runtime_max_blocking_threads,
},
write_runtime_config: TokioRuntimeConfig {
worker_threads: args.write_runtime_worker_threads,
max_blocking_threads: args.write_runtime_max_blocking_threads,
},
},
_ => unreachable!(),

Check warning on line 536 in foyer-bench/src/main.rs

View check run for this annotation

Codecov / codecov/patch

foyer-bench/src/main.rs#L522-L536

Added lines #L522 - L536 were not covered by tests
});

if args.admission_rate_limit > 0 {
Expand Down
9 changes: 0 additions & 9 deletions foyer-storage/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,4 @@ where
Engine::Combined(storage) => StoreFuture::Combined(storage.wait()),
}
}

fn runtime(&self) -> &tokio::runtime::Handle {
match self {
Engine::Noop(storage) => storage.runtime(),
Engine::Large(storage) => storage.runtime(),
Engine::Small(storage) => storage.runtime(),
Engine::Combined(storage) => storage.runtime(),
}
}
}
37 changes: 21 additions & 16 deletions foyer-storage/src/large/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
pub reinsertion_picker: Arc<dyn ReinsertionPicker<Key = K>>,
pub tombstone_log_config: Option<TombstoneLogConfig>,
pub statistics: Arc<Statistics>,
pub read_runtime_handle: Handle,
pub write_runtime_handle: Handle,
pub user_runtime_handle: Handle,
pub marker: PhantomData<(V, S)>,
}

Expand All @@ -109,6 +112,8 @@
.field("reinsertion_pickers", &self.reinsertion_picker)
.field("tombstone_log_config", &self.tombstone_log_config)
.field("statistics", &self.statistics)
.field("read_runtime_handle", &self.read_runtime_handle)
.field("write_runtime_handle", &self.write_runtime_handle)

Check warning on line 116 in foyer-storage/src/large/generic.rs

View check run for this annotation

Codecov / codecov/patch

foyer-storage/src/large/generic.rs#L115-L116

Added lines #L115 - L116 were not covered by tests
.finish()
}
}
Expand Down Expand Up @@ -152,7 +157,9 @@

sequence: AtomicSequence,

runtime: Handle,
_read_runtime_handle: Handle,
write_runtime_handle: Handle,
_user_runtime_handle: Handle,

active: AtomicBool,

Expand All @@ -179,8 +186,6 @@
S: HashBuilder + Debug,
{
async fn open(mut config: GenericLargeStorageConfig<K, V, S>) -> Result<Self> {
let runtime = Handle::current();

let stats = config.statistics.clone();

let device = config.device.clone();
Expand Down Expand Up @@ -223,7 +228,7 @@
&indexer,
&region_manager,
&tombstones,
runtime.clone(),
config.user_runtime_handle.clone(),
)
.await?;

Expand All @@ -236,7 +241,7 @@
tombstone_log.clone(),
stats.clone(),
metrics.clone(),
runtime.clone(),
config.write_runtime_handle.clone(),
)
.await
}))
Expand All @@ -251,7 +256,7 @@
flushers.clone(),
stats.clone(),
config.flush,
runtime.clone(),
config.write_runtime_handle.clone(),
)
.await
}))
Expand All @@ -267,7 +272,9 @@
statistics: stats,
flush: config.flush,
sequence,
runtime,
_read_runtime_handle: config.read_runtime_handle,
write_runtime_handle: config.write_runtime_handle,
_user_runtime_handle: config.user_runtime_handle,
active: AtomicBool::new(true),
metrics,
}),
Expand Down Expand Up @@ -375,7 +382,7 @@
});

let this = self.clone();
self.inner.runtime.spawn(async move {
self.inner.write_runtime_handle.spawn(async move {
let sequence = this.inner.sequence.fetch_add(1, Ordering::Relaxed);
this.inner.flushers[sequence as usize % this.inner.flushers.len()].submit(Submission::Tombstone {
tombstone: Tombstone { hash, sequence },
Expand Down Expand Up @@ -424,10 +431,6 @@

Ok(())
}

fn runtime(&self) -> &Handle {
&self.inner.runtime
}
}

impl<K, V, S> Storage for GenericLargeStorage<K, V, S>
Expand Down Expand Up @@ -476,10 +479,6 @@
fn wait(&self) -> impl Future<Output = ()> + Send + 'static {
self.wait()
}

fn runtime(&self) -> &Handle {
self.runtime()
}
}

#[cfg(test)]
Expand Down Expand Up @@ -554,6 +553,9 @@
tombstone_log_config: None,
buffer_threshold: 16 * 1024 * 1024,
statistics: Arc::<Statistics>::default(),
read_runtime_handle: Handle::current(),
write_runtime_handle: Handle::current(),
user_runtime_handle: Handle::current(),
marker: PhantomData,
};
GenericLargeStorage::open(config).await.unwrap()
Expand Down Expand Up @@ -582,6 +584,9 @@
tombstone_log_config: Some(TombstoneLogConfigBuilder::new(path).with_flush(true).build()),
buffer_threshold: 16 * 1024 * 1024,
statistics: Arc::<Statistics>::default(),
read_runtime_handle: Handle::current(),
write_runtime_handle: Handle::current(),
user_runtime_handle: Handle::current(),
marker: PhantomData,
};
GenericLargeStorage::open(config).await.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion foyer-storage/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ pub use crate::{
},
statistics::Statistics,
storage::{either::Order, Storage},
store::{CombinedConfig, DeviceConfig, RuntimeConfig, Store, StoreBuilder},
store::{CombinedConfig, DeviceConfig, RuntimeConfig, RuntimeHandles, Store, StoreBuilder, TokioRuntimeConfig},
};
4 changes: 0 additions & 4 deletions foyer-storage/src/small/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,4 @@ where
fn wait(&self) -> impl Future<Output = ()> + Send + 'static {
async { todo!() }
}

fn runtime(&self) -> &tokio::runtime::Handle {
todo!()
}
}
12 changes: 0 additions & 12 deletions foyer-storage/src/storage/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use futures::{
future::{join, ready, select, try_join, Either as EitherFuture},
pin_mut, Future, FutureExt,
};
use tokio::runtime::Handle;

use crate::{error::Result, serde::KvInfo, storage::Storage, DeviceStats, IoBytes};

Expand Down Expand Up @@ -290,15 +289,4 @@ where
fn wait(&self) -> impl Future<Output = ()> + Send + 'static {
join(self.left.wait(), self.right.wait()).map(|_| ())
}

fn runtime(&self) -> &Handle {
if cfg!(debug_assertions) {
let hleft = self.left.runtime();
let hright = self.right.runtime();
assert_eq!(hleft.id(), hright.id());
hleft
} else {
self.left.runtime()
}
}
}
6 changes: 0 additions & 6 deletions foyer-storage/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::{fmt::Debug, future::Future, sync::Arc};

use foyer_common::code::{HashBuilder, StorageKey, StorageValue};
use foyer_memory::CacheEntry;
use tokio::runtime::Handle;

use crate::{device::monitor::DeviceStats, error::Result, serde::KvInfo, IoBytes};

Expand Down Expand Up @@ -74,9 +73,4 @@ pub trait Storage: Send + Sync + 'static + Clone + Debug {
/// Wait for the ongoing flush and reclaim tasks to finish.
#[must_use]
fn wait(&self) -> impl Future<Output = ()> + Send + 'static;

/// Get disk cache runtime handle.
///
/// The runtime is determined during the opening phase.
fn runtime(&self) -> &Handle;
}
16 changes: 2 additions & 14 deletions foyer-storage/src/storage/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use foyer_memory::CacheEntry;

use futures::future::ready;
use tokio::runtime::Handle;

use crate::device::monitor::DeviceStats;
use crate::serde::KvInfo;
Expand All @@ -34,7 +33,6 @@
V: StorageValue,
S: HashBuilder + Debug,
{
runtime: Handle,
_marker: PhantomData<(K, V, S)>,
}

Expand All @@ -56,10 +54,7 @@
S: HashBuilder + Debug,
{
fn clone(&self) -> Self {
Self {
runtime: self.runtime.clone(),
_marker: PhantomData,
}
Self { _marker: PhantomData }

Check warning on line 57 in foyer-storage/src/storage/noop.rs

View check run for this annotation

Codecov / codecov/patch

foyer-storage/src/storage/noop.rs#L57

Added line #L57 was not covered by tests
}
}

Expand All @@ -75,10 +70,7 @@
type Config = ();

async fn open(_: Self::Config) -> Result<Self> {
Ok(Self {
runtime: Handle::current(),
_marker: PhantomData,
})
Ok(Self { _marker: PhantomData })
}

async fn close(&self) -> Result<()> {
Expand Down Expand Up @@ -108,10 +100,6 @@
fn wait(&self) -> impl Future<Output = ()> + Send + 'static {
ready(())
}

fn runtime(&self) -> &Handle {
&self.runtime
}
}

#[cfg(test)]
Expand Down
Loading
Loading