From 726a5f5e37bf5d6c25194a114f36b7f8756a1278 Mon Sep 17 00:00:00 2001 From: Croxx Date: Wed, 9 Oct 2024 00:04:03 +0800 Subject: [PATCH] refactor: refine runtime and tracing options (#756) * refactor: refine runtime and tracing options Signed-off-by: MrCroxx * refactor: rename API Signed-off-by: MrCroxx * refactor: tiny refines Signed-off-by: MrCroxx * chore: pass ffmt check Signed-off-by: MrCroxx --------- Signed-off-by: MrCroxx --- examples/hybrid_full.rs | 8 +- examples/tail_based_tracing.rs | 6 +- foyer-bench/Cargo.toml | 1 + foyer-bench/src/main.rs | 48 +++++------ foyer-common/src/tracing.rs | 151 ++++++++++++++++++++++----------- foyer-storage/src/prelude.rs | 4 +- foyer-storage/src/store.rs | 32 +++---- foyer/src/hybrid/builder.rs | 54 ++++++------ foyer/src/hybrid/cache.rs | 17 ++-- foyer/src/prelude.rs | 47 +++++----- 10 files changed, 209 insertions(+), 159 deletions(-) diff --git a/examples/hybrid_full.rs b/examples/hybrid_full.rs index aca38a03..aba9510f 100644 --- a/examples/hybrid_full.rs +++ b/examples/hybrid_full.rs @@ -18,7 +18,7 @@ use anyhow::Result; use chrono::Datelike; use foyer::{ DirectFsDeviceOptions, Engine, FifoPicker, HybridCache, HybridCacheBuilder, LargeEngineOptions, LruConfig, - RateLimitPicker, RecoverMode, RuntimeConfig, SmallEngineOptions, TokioRuntimeConfig, TombstoneLogConfigBuilder, + RateLimitPicker, RecoverMode, RuntimeOptions, SmallEngineOptions, TokioRuntimeOptions, TombstoneLogConfigBuilder, }; use tempfile::tempdir; @@ -45,12 +45,12 @@ async fn main() -> Result<()> { .with_recover_mode(RecoverMode::Quiet) .with_admission_picker(Arc::new(RateLimitPicker::new(100 * 1024 * 1024))) .with_compression(foyer::Compression::Lz4) - .with_runtime_config(RuntimeConfig::Separated { - read_runtime_config: TokioRuntimeConfig { + .with_runtime_options(RuntimeOptions::Separated { + read_runtime_options: TokioRuntimeOptions { worker_threads: 4, max_blocking_threads: 8, }, - write_runtime_config: TokioRuntimeConfig { + write_runtime_options: TokioRuntimeOptions { worker_threads: 4, max_blocking_threads: 8, }, diff --git a/examples/tail_based_tracing.rs b/examples/tail_based_tracing.rs index 0d515091..4c20e91f 100644 --- a/examples/tail_based_tracing.rs +++ b/examples/tail_based_tracing.rs @@ -14,7 +14,7 @@ use std::time::Duration; -use foyer::{DirectFsDeviceOptions, Engine, HybridCache, HybridCacheBuilder}; +use foyer::{DirectFsDeviceOptions, Engine, HybridCache, HybridCacheBuilder, TracingOptions}; #[cfg(feature = "jaeger")] fn init_jaeger_exporter() { @@ -76,9 +76,7 @@ async fn main() -> anyhow::Result<()> { .await?; hybrid.enable_tracing(); - hybrid - .tracing_config() - .set_record_hybrid_get_threshold(Duration::from_millis(10)); + hybrid.update_tracing_options(TracingOptions::new().with_record_hybrid_get_threshold(Duration::from_millis(10))); hybrid.insert(42, "The answer to life, the universe, and everything.".to_string()); assert_eq!( diff --git a/foyer-bench/Cargo.toml b/foyer-bench/Cargo.toml index d942e473..966c2e91 100644 --- a/foyer-bench/Cargo.toml +++ b/foyer-bench/Cargo.toml @@ -22,6 +22,7 @@ fastrace-jaeger = { workspace = true, optional = true } foyer = { workspace = true } futures = "0.3" hdrhistogram = "7" +humantime = "2" itertools = { workspace = true } metrics = { workspace = true } metrics-exporter-prometheus = "0.15" diff --git a/foyer-bench/src/main.rs b/foyer-bench/src/main.rs index bbe52904..bd3541b9 100644 --- a/foyer-bench/src/main.rs +++ b/foyer-bench/src/main.rs @@ -36,7 +36,7 @@ use clap::{builder::PossibleValuesParser, ArgGroup, Parser}; use foyer::{ Compression, DirectFileDeviceOptions, DirectFsDeviceOptions, Engine, FifoConfig, FifoPicker, HybridCache, HybridCacheBuilder, InvalidRatioPicker, LargeEngineOptions, LfuConfig, LruConfig, RateLimitPicker, RecoverMode, - RuntimeConfig, S3FifoConfig, SmallEngineOptions, TokioRuntimeConfig, TracingConfig, + RuntimeOptions, S3FifoConfig, SmallEngineOptions, TokioRuntimeOptions, TracingOptions, }; use futures::future::join_all; use itertools::Itertools; @@ -245,20 +245,20 @@ pub struct Args { set_cache_capacity: usize, /// Record insert trace threshold. Only effective with "tracing" feature. - #[arg(long, default_value_t = 1000 * 1000)] - trace_insert_us: usize, + #[arg(long, default_value = "1s")] + trace_insert: humantime::Duration, /// Record get trace threshold. Only effective with "tracing" feature. - #[arg(long, default_value_t = 1000 * 1000)] - trace_get_us: usize, + #[arg(long, default_value = "1s")] + trace_get: humantime::Duration, /// Record obtain trace threshold. Only effective with "tracing" feature. - #[arg(long, default_value_t = 1000 * 1000)] - trace_obtain_us: usize, + #[arg(long, default_value = "1s")] + trace_obtain: humantime::Duration, /// Record remove trace threshold. Only effective with "tracing" feature. - #[arg(long, default_value_t = 1000 * 1000)] - trace_remove_us: usize, + #[arg(long, default_value = "1s")] + trace_remove: humantime::Duration, /// Record fetch trace threshold. Only effective with "tracing" feature. - #[arg(long, default_value_t = 1000 * 1000)] - trace_fetch_us: usize, + #[arg(long, default_value = "1s")] + trace_fetch: humantime::Duration, } #[derive(Debug)] @@ -433,15 +433,15 @@ async fn benchmark(args: Args) { .unwrap(); } - let tracing_config = TracingConfig::default(); - tracing_config.set_record_hybrid_insert_threshold(Duration::from_micros(args.trace_insert_us as _)); - tracing_config.set_record_hybrid_get_threshold(Duration::from_micros(args.trace_get_us as _)); - tracing_config.set_record_hybrid_obtain_threshold(Duration::from_micros(args.trace_obtain_us as _)); - tracing_config.set_record_hybrid_remove_threshold(Duration::from_micros(args.trace_remove_us as _)); - tracing_config.set_record_hybrid_fetch_threshold(Duration::from_micros(args.trace_fetch_us as _)); + let tracing_options = TracingOptions::new() + .with_record_hybrid_insert_threshold(args.trace_insert.into()) + .with_record_hybrid_get_threshold(args.trace_get.into()) + .with_record_hybrid_obtain_threshold(args.trace_obtain.into()) + .with_record_hybrid_remove_threshold(args.trace_remove.into()) + .with_record_hybrid_fetch_threshold(args.trace_fetch.into()); let builder = HybridCacheBuilder::new() - .with_tracing_config(tracing_config) + .with_tracing_options(tracing_options) .memory(args.mem.as_u64() as _) .with_shards(args.shards); @@ -479,18 +479,18 @@ async fn benchmark(args: Args) { .with_flush(args.flush) .with_recover_mode(args.recover_mode) .with_compression(args.compression) - .with_runtime_config(match args.runtime.as_str() { - "disabled" => RuntimeConfig::Disabled, - "unified" => RuntimeConfig::Unified(TokioRuntimeConfig { + .with_runtime_options(match args.runtime.as_str() { + "disabled" => RuntimeOptions::Disabled, + "unified" => RuntimeOptions::Unified(TokioRuntimeOptions { worker_threads: args.runtime_worker_threads, max_blocking_threads: args.runtime_max_blocking_threads, }), - "separated" => RuntimeConfig::Separated { - read_runtime_config: TokioRuntimeConfig { + "separated" => RuntimeOptions::Separated { + read_runtime_options: TokioRuntimeOptions { worker_threads: args.read_runtime_worker_threads, max_blocking_threads: args.read_runtime_max_blocking_threads, }, - write_runtime_config: TokioRuntimeConfig { + write_runtime_options: TokioRuntimeOptions { worker_threads: args.write_runtime_worker_threads, max_blocking_threads: args.write_runtime_max_blocking_threads, }, diff --git a/foyer-common/src/tracing.rs b/foyer-common/src/tracing.rs index 057e9c50..1c3e9f20 100644 --- a/foyer-common/src/tracing.rs +++ b/foyer-common/src/tracing.rs @@ -15,7 +15,7 @@ use std::{ ops::Deref, pin::Pin, - sync::atomic::{AtomicUsize, Ordering}, + sync::atomic::{AtomicU64, Ordering}, task::{Context, Poll}, time::Duration, }; @@ -26,87 +26,136 @@ use pin_project::pin_project; use serde::{Deserialize, Serialize}; /// Configurations for tracing. -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Default)] pub struct TracingConfig { /// Threshold for recording the hybrid cache `insert` and `insert_with_context` operation in us. - record_hybrid_insert_threshold_us: AtomicUsize, + record_hybrid_insert_threshold_us: AtomicU64, /// Threshold for recording the hybrid cache `get` operation in us. - record_hybrid_get_threshold_us: AtomicUsize, + record_hybrid_get_threshold_us: AtomicU64, /// Threshold for recording the hybrid cache `obtain` operation in us. - record_hybrid_obtain_threshold_us: AtomicUsize, + record_hybrid_obtain_threshold_us: AtomicU64, /// Threshold for recording the hybrid cache `remove` operation in us. - record_hybrid_remove_threshold_us: AtomicUsize, + record_hybrid_remove_threshold_us: AtomicU64, /// Threshold for recording the hybrid cache `fetch` operation in us. - record_hybrid_fetch_threshold_us: AtomicUsize, + record_hybrid_fetch_threshold_us: AtomicU64, } -impl Default for TracingConfig { - /// All thresholds are set to `1s`. - fn default() -> Self { - Self { - record_hybrid_insert_threshold_us: AtomicUsize::from(1000 * 1000), - record_hybrid_get_threshold_us: AtomicUsize::from(1000 * 1000), - record_hybrid_obtain_threshold_us: AtomicUsize::from(1000 * 1000), - record_hybrid_remove_threshold_us: AtomicUsize::from(1000 * 1000), - record_hybrid_fetch_threshold_us: AtomicUsize::from(1000 * 1000), +impl TracingConfig { + /// Update tracing config with options. + pub fn update(&self, options: TracingOptions) { + if let Some(threshold) = options.record_hybrid_insert_threshold { + self.record_hybrid_insert_threshold_us + .store(threshold.as_micros() as _, Ordering::Relaxed); } - } -} -impl TracingConfig { - /// Set the threshold for recording the hybrid cache `insert` and `insert_with_context` operation. - pub fn set_record_hybrid_insert_threshold(&self, threshold: Duration) { - self.record_hybrid_insert_threshold_us - .store(threshold.as_micros() as _, Ordering::Relaxed); + if let Some(threshold) = options.record_hybrid_get_threshold { + self.record_hybrid_get_threshold_us + .store(threshold.as_micros() as _, Ordering::Relaxed); + } + + if let Some(threshold) = options.record_hybrid_obtain_threshold { + self.record_hybrid_obtain_threshold_us + .store(threshold.as_micros() as _, Ordering::Relaxed); + } + + if let Some(threshold) = options.record_hybrid_remove_threshold { + self.record_hybrid_remove_threshold_us + .store(threshold.as_micros() as _, Ordering::Relaxed); + } + + if let Some(threshold) = options.record_hybrid_fetch_threshold { + self.record_hybrid_fetch_threshold_us + .store(threshold.as_micros() as _, Ordering::Relaxed); + } } /// Threshold for recording the hybrid cache `insert` and `insert_with_context` operation. pub fn record_hybrid_insert_threshold(&self) -> Duration { - Duration::from_micros(self.record_hybrid_insert_threshold_us.load(Ordering::Relaxed) as _) - } - - /// Set the threshold for recording the hybrid cache `get` operation. - pub fn set_record_hybrid_get_threshold(&self, threshold: Duration) { - self.record_hybrid_get_threshold_us - .store(threshold.as_micros() as _, Ordering::Relaxed); + Duration::from_micros(self.record_hybrid_insert_threshold_us.load(Ordering::Relaxed)) } /// Threshold for recording the hybrid cache `get` operation. pub fn record_hybrid_get_threshold(&self) -> Duration { - Duration::from_micros(self.record_hybrid_get_threshold_us.load(Ordering::Relaxed) as _) - } - - /// Set the threshold for recording the hybrid cache `obtain` operation. - pub fn set_record_hybrid_obtain_threshold(&self, threshold: Duration) { - self.record_hybrid_obtain_threshold_us - .store(threshold.as_micros() as _, Ordering::Relaxed); + Duration::from_micros(self.record_hybrid_get_threshold_us.load(Ordering::Relaxed)) } /// Threshold for recording the hybrid cache `obtain` operation. pub fn record_hybrid_obtain_threshold(&self) -> Duration { - Duration::from_micros(self.record_hybrid_obtain_threshold_us.load(Ordering::Relaxed) as _) - } - - /// Set the threshold for recording the hybrid cache `remove` operation. - pub fn set_record_hybrid_remove_threshold(&self, threshold: Duration) { - self.record_hybrid_remove_threshold_us - .store(threshold.as_micros() as _, Ordering::Relaxed); + Duration::from_micros(self.record_hybrid_obtain_threshold_us.load(Ordering::Relaxed)) } /// Threshold for recording the hybrid cache `remove` operation. pub fn record_hybrid_remove_threshold(&self) -> Duration { - Duration::from_micros(self.record_hybrid_remove_threshold_us.load(Ordering::Relaxed) as _) + Duration::from_micros(self.record_hybrid_remove_threshold_us.load(Ordering::Relaxed)) } - /// Set the threshold for recording the hybrid cache `fetch` operation. - pub fn set_record_hybrid_fetch_threshold(&self, threshold: Duration) { - self.record_hybrid_fetch_threshold_us - .store(threshold.as_micros() as _, Ordering::Relaxed); + /// Threshold for recording the hybrid cache `fetch` operation. + pub fn record_hybrid_fetch_threshold(&self) -> Duration { + Duration::from_micros(self.record_hybrid_fetch_threshold_us.load(Ordering::Relaxed)) } +} +/// Options for tracing. +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct TracingOptions { + /// Threshold for recording the hybrid cache `insert` and `insert_with_context` operation. + record_hybrid_insert_threshold: Option, + /// Threshold for recording the hybrid cache `get` operation. + record_hybrid_get_threshold: Option, + /// Threshold for recording the hybrid cache `obtain` operation. + record_hybrid_obtain_threshold: Option, + /// Threshold for recording the hybrid cache `remove` operation. + record_hybrid_remove_threshold: Option, /// Threshold for recording the hybrid cache `fetch` operation. - pub fn record_hybrid_fetch_threshold(&self) -> Duration { - Duration::from_micros(self.record_hybrid_fetch_threshold_us.load(Ordering::Relaxed) as _) + record_hybrid_fetch_threshold: Option, +} + +impl Default for TracingOptions { + fn default() -> Self { + Self::new() + } +} + +impl TracingOptions { + /// Create an empty tracing options. + pub fn new() -> Self { + Self { + record_hybrid_insert_threshold: None, + record_hybrid_get_threshold: None, + record_hybrid_obtain_threshold: None, + record_hybrid_remove_threshold: None, + record_hybrid_fetch_threshold: None, + } + } + + /// Set the threshold for recording the hybrid cache `insert` and `insert_with_context` operation. + pub fn with_record_hybrid_insert_threshold(mut self, threshold: Duration) -> Self { + self.record_hybrid_insert_threshold = Some(threshold); + self + } + + /// Set the threshold for recording the hybrid cache `get` operation. + pub fn with_record_hybrid_get_threshold(mut self, threshold: Duration) -> Self { + self.record_hybrid_get_threshold = Some(threshold); + self + } + + /// Set the threshold for recording the hybrid cache `obtain` operation. + pub fn with_record_hybrid_obtain_threshold(mut self, threshold: Duration) -> Self { + self.record_hybrid_obtain_threshold = Some(threshold); + self + } + + /// Set the threshold for recording the hybrid cache `remove` operation. + pub fn with_record_hybrid_remove_threshold(mut self, threshold: Duration) -> Self { + self.record_hybrid_remove_threshold = Some(threshold); + self + } + + /// Set the threshold for recording the hybrid cache `fetch` operation. + pub fn with_record_hybrid_fetch_threshold(mut self, threshold: Duration) -> Self { + self.record_hybrid_fetch_threshold = Some(threshold); + self } } diff --git a/foyer-storage/src/prelude.rs b/foyer-storage/src/prelude.rs index 1cd942b4..75954c60 100644 --- a/foyer-storage/src/prelude.rs +++ b/foyer-storage/src/prelude.rs @@ -34,7 +34,7 @@ pub use crate::{ statistics::Statistics, storage::{either::Order, Storage}, store::{ - DeviceOptions, Engine, LargeEngineOptions, RuntimeConfig, SmallEngineOptions, Store, StoreBuilder, - TokioRuntimeConfig, + DeviceOptions, Engine, LargeEngineOptions, RuntimeOptions, SmallEngineOptions, Store, StoreBuilder, + TokioRuntimeOptions, }, }; diff --git a/foyer-storage/src/store.rs b/foyer-storage/src/store.rs index 741dd9c6..39428ae2 100644 --- a/foyer-storage/src/store.rs +++ b/foyer-storage/src/store.rs @@ -304,7 +304,7 @@ impl FromStr for Engine { /// Tokio runtime configuration. #[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct TokioRuntimeConfig { +pub struct TokioRuntimeOptions { /// Dedicated runtime worker threads. /// /// If the value is set to `0`, the dedicated will use the default worker threads of tokio. @@ -321,19 +321,19 @@ pub struct TokioRuntimeConfig { pub max_blocking_threads: usize, } -/// Configuration for the dedicated runtime. +/// Options for the dedicated runtime. #[derive(Debug, Clone, Serialize, Deserialize)] -pub enum RuntimeConfig { +pub enum RuntimeOptions { /// Disable dedicated runtime. The runtime which foyer is built on will be used. Disabled, /// Use unified dedicated runtime for both reads and writes. - Unified(TokioRuntimeConfig), + Unified(TokioRuntimeOptions), /// Use separated dedicated runtime for reads or writes. Separated { /// Dedicated runtime for reads. - read_runtime_config: TokioRuntimeConfig, + read_runtime_options: TokioRuntimeOptions, /// Dedicated runtime for both foreground and background writes - write_runtime_config: TokioRuntimeConfig, + write_runtime_options: TokioRuntimeOptions, }, } @@ -349,7 +349,7 @@ where device_options: DeviceOptions, engine: Engine, - runtime_config: RuntimeConfig, + runtime_config: RuntimeOptions, admission_picker: Arc>, compression: Compression, @@ -378,7 +378,7 @@ where device_options: DeviceOptions::None, engine, - runtime_config: RuntimeConfig::Disabled, + runtime_config: RuntimeOptions::Disabled, admission_picker: Arc::>::default(), compression: Compression::None, @@ -443,8 +443,8 @@ where } /// Configure the dedicated runtime for the disk cache store. - pub fn with_runtime_config(mut self, runtime_config: RuntimeConfig) -> Self { - self.runtime_config = runtime_config; + pub fn with_runtime_options(mut self, runtime_options: RuntimeOptions) -> Self { + self.runtime_config = runtime_options; self } @@ -480,7 +480,7 @@ where let compression = self.compression; - let build_runtime = |config: &TokioRuntimeConfig, suffix: &str| { + let build_runtime = |config: &TokioRuntimeOptions, suffix: &str| { let mut builder = tokio::runtime::Builder::new_multi_thread(); #[cfg(not(madsim))] if config.worker_threads != 0 { @@ -498,17 +498,17 @@ where let user_runtime_handle = Handle::current(); let (read_runtime, write_runtime) = match self.runtime_config { - RuntimeConfig::Disabled => { + RuntimeOptions::Disabled => { tracing::warn!("[store]: Dedicated runtime is disabled"); (None, None) } - RuntimeConfig::Unified(runtime_config) => { + RuntimeOptions::Unified(runtime_config) => { let runtime = build_runtime(&runtime_config, "unified")?; (Some(runtime.clone()), Some(runtime.clone())) } - RuntimeConfig::Separated { - read_runtime_config, - write_runtime_config, + RuntimeOptions::Separated { + read_runtime_options: read_runtime_config, + write_runtime_options: write_runtime_config, } => { let read_runtime = build_runtime(&read_runtime_config, "read")?; let write_runtime = build_runtime(&write_runtime_config, "write")?; diff --git a/foyer/src/hybrid/builder.rs b/foyer/src/hybrid/builder.rs index f5c3683e..826c091a 100644 --- a/foyer/src/hybrid/builder.rs +++ b/foyer/src/hybrid/builder.rs @@ -18,11 +18,11 @@ use ahash::RandomState; use foyer_common::{ code::{HashBuilder, StorageKey, StorageValue}, event::EventListener, - tracing::TracingConfig, + tracing::TracingOptions, }; use foyer_memory::{Cache, CacheBuilder, EvictionConfig, Weighter}; use foyer_storage::{ - AdmissionPicker, Compression, DeviceOptions, Engine, LargeEngineOptions, RecoverMode, RuntimeConfig, + AdmissionPicker, Compression, DeviceOptions, Engine, LargeEngineOptions, RecoverMode, RuntimeOptions, SmallEngineOptions, StoreBuilder, }; @@ -32,7 +32,7 @@ use crate::HybridCache; pub struct HybridCacheBuilder { name: String, event_listener: Option>>, - tracing_config: TracingConfig, + tracing_options: TracingOptions, } impl Default for HybridCacheBuilder { @@ -47,7 +47,7 @@ impl HybridCacheBuilder { Self { name: "foyer".to_string(), event_listener: None, - tracing_config: TracingConfig::default(), + tracing_options: TracingOptions::default(), } } @@ -69,11 +69,11 @@ impl HybridCacheBuilder { self } - /// Set tracing config. + /// Set tracing options. /// /// Default: Only operations over 1s will be recorded. - pub fn with_tracing_config(mut self, tracing_config: TracingConfig) -> Self { - self.tracing_config = tracing_config; + pub fn with_tracing_options(mut self, tracing_options: TracingOptions) -> Self { + self.tracing_options = tracing_options; self } @@ -90,7 +90,7 @@ impl HybridCacheBuilder { HybridCacheBuilderPhaseMemory { builder, name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, } } } @@ -103,7 +103,7 @@ where S: HashBuilder + Debug, { name: String, - tracing_config: TracingConfig, + tracing_options: TracingOptions, builder: CacheBuilder, } @@ -119,7 +119,7 @@ where let builder = self.builder.with_shards(shards); HybridCacheBuilderPhaseMemory { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, builder, } } @@ -131,7 +131,7 @@ where let builder = self.builder.with_eviction_config(eviction_config.into()); HybridCacheBuilderPhaseMemory { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, builder, } } @@ -145,7 +145,7 @@ where let builder = self.builder.with_object_pool_capacity(object_pool_capacity); HybridCacheBuilderPhaseMemory { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, builder, } } @@ -158,7 +158,7 @@ where let builder = self.builder.with_hash_builder(hash_builder); HybridCacheBuilderPhaseMemory { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, builder, } } @@ -168,7 +168,7 @@ where let builder = self.builder.with_weighter(weighter); HybridCacheBuilderPhaseMemory { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, builder, } } @@ -179,7 +179,7 @@ where HybridCacheBuilderPhaseStorage { builder: StoreBuilder::new(memory.clone(), engine).with_name(&self.name), name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory, } } @@ -193,7 +193,7 @@ where S: HashBuilder + Debug, { name: String, - tracing_config: TracingConfig, + tracing_options: TracingOptions, memory: Cache, builder: StoreBuilder, } @@ -209,7 +209,7 @@ where let builder = self.builder.with_device_options(device_options); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } @@ -222,7 +222,7 @@ where let builder = self.builder.with_flush(flush); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } @@ -237,7 +237,7 @@ where let builder = self.builder.with_recover_mode(recover_mode); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } @@ -252,7 +252,7 @@ where let builder = self.builder.with_admission_picker(admission_picker); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } @@ -265,18 +265,18 @@ where let builder = self.builder.with_compression(compression); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } } /// Configure the dedicated runtime for the disk cache store. - pub fn with_runtime_config(self, runtime_config: RuntimeConfig) -> Self { - let builder = self.builder.with_runtime_config(runtime_config); + pub fn with_runtime_options(self, runtime_options: RuntimeOptions) -> Self { + let builder = self.builder.with_runtime_options(runtime_options); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } @@ -289,7 +289,7 @@ where let builder = self.builder.with_large_object_disk_cache_options(options); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } @@ -302,7 +302,7 @@ where let builder = self.builder.with_small_object_disk_cache_options(options); Self { name: self.name, - tracing_config: self.tracing_config, + tracing_options: self.tracing_options, memory: self.memory, builder, } @@ -311,6 +311,6 @@ where /// Build and open the hybrid cache with the given configurations. pub async fn build(self) -> anyhow::Result> { let storage = self.builder.build().await?; - Ok(HybridCache::new(self.name, self.memory, storage, self.tracing_config)) + Ok(HybridCache::new(self.name, self.memory, storage, self.tracing_options)) } } diff --git a/foyer/src/hybrid/cache.rs b/foyer/src/hybrid/cache.rs index 5a728018..6c57a0ef 100644 --- a/foyer/src/hybrid/cache.rs +++ b/foyer/src/hybrid/cache.rs @@ -33,7 +33,7 @@ use foyer_common::{ code::{HashBuilder, StorageKey, StorageValue}, future::Diversion, metrics::Metrics, - tracing::{InRootSpan, TracingConfig}, + tracing::{InRootSpan, TracingConfig, TracingOptions}, }; use foyer_memory::{Cache, CacheContext, CacheEntry, Fetch, FetchMark, FetchState}; use foyer_storage::{DeviceStats, Store}; @@ -130,23 +130,24 @@ where name: String, memory: Cache, storage: Store, - tracing_config: TracingConfig, + tracing_options: TracingOptions, ) -> Self { let metrics = Arc::new(Metrics::new(&name)); - let tracing_config = Arc::new(tracing_config); - let trace = Arc::new(AtomicBool::new(false)); + let tracing_config = Arc::::default(); + tracing_config.update(tracing_options); + let tracing = Arc::new(AtomicBool::new(false)); Self { memory, storage, metrics, tracing_config, - tracing: trace, + tracing, } } - /// Access the trace config. - pub fn tracing_config(&self) -> &TracingConfig { - &self.tracing_config + /// Access the trace config with options. + pub fn update_tracing_options(&self, options: TracingOptions) { + self.tracing_config.update(options); } /// Access the in-memory cache. diff --git a/foyer/src/prelude.rs b/foyer/src/prelude.rs index 937c1f5f..9ca7aec4 100644 --- a/foyer/src/prelude.rs +++ b/foyer/src/prelude.rs @@ -12,27 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use common::{ - buf::{BufExt, BufMutExt}, - code::{Key, StorageKey, StorageValue, Value}, - event::EventListener, - range::RangeBoundsExt, - tracing::TracingConfig, +pub use crate::{ + common::{ + buf::{BufExt, BufMutExt}, + code::{Key, StorageKey, StorageValue, Value}, + event::EventListener, + range::RangeBoundsExt, + tracing::TracingOptions, + }, + hybrid::{ + builder::{HybridCacheBuilder, HybridCacheBuilderPhaseMemory, HybridCacheBuilderPhaseStorage}, + cache::{HybridCache, HybridCacheEntry, HybridFetch, HybridFetchInner}, + writer::{HybridCacheStorageWriter, HybridCacheWriter}, + }, + memory::{ + Cache, CacheBuilder, CacheContext, CacheEntry, EvictionConfig, FetchState, FifoConfig, LfuConfig, LruConfig, + S3FifoConfig, Weighter, + }, + storage::{ + AdmissionPicker, AdmitAllPicker, Compression, Dev, DevConfig, DevExt, DeviceStats, DirectFileDevice, + DirectFileDeviceOptions, DirectFsDevice, DirectFsDeviceOptions, Engine, EvictionPicker, FifoPicker, + InvalidRatioPicker, LargeEngineOptions, RateLimitPicker, RecoverMode, ReinsertionPicker, RejectAllPicker, + Runtime, RuntimeOptions, SmallEngineOptions, Storage, Store, StoreBuilder, TokioRuntimeOptions, + TombstoneLogConfigBuilder, + }, }; -pub use memory::{ - Cache, CacheBuilder, CacheContext, CacheEntry, EvictionConfig, FetchState, FifoConfig, LfuConfig, LruConfig, - S3FifoConfig, Weighter, -}; -pub use storage::{ - AdmissionPicker, AdmitAllPicker, Compression, Dev, DevConfig, DevExt, DeviceStats, DirectFileDevice, - DirectFileDeviceOptions, DirectFsDevice, DirectFsDeviceOptions, Engine, EvictionPicker, FifoPicker, - InvalidRatioPicker, LargeEngineOptions, RateLimitPicker, RecoverMode, ReinsertionPicker, RejectAllPicker, Runtime, - RuntimeConfig, SmallEngineOptions, Storage, Store, StoreBuilder, TokioRuntimeConfig, TombstoneLogConfigBuilder, -}; - -pub use crate::hybrid::{ - builder::{HybridCacheBuilder, HybridCacheBuilderPhaseMemory, HybridCacheBuilderPhaseStorage}, - cache::{HybridCache, HybridCacheEntry, HybridFetch, HybridFetchInner}, - writer::{HybridCacheStorageWriter, HybridCacheWriter}, -}; -use crate::{common, memory, storage};