Skip to content

Commit

Permalink
feat: impl Default, serde for eviction configs, debug for hybrid (#441)
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <mrcroxx@outlook.com>
  • Loading branch information
MrCroxx committed Apr 28, 2024
1 parent 002e488 commit 294fd16
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -22,6 +22,7 @@ tokio = { package = "madsim-tokio", version = "0.2", features = [
"signal",
"fs",
] }
serde = { version = "1", features = ["derive", "rc"] }

[profile.release]
debug = true
2 changes: 1 addition & 1 deletion foyer-common/Cargo.toml
Expand Up @@ -19,7 +19,7 @@ hashbrown = "0.14"
itertools = "0.12"
nix = { version = "0.28", features = ["fs"] }
parking_lot = { version = "0.12", features = ["arc_lock"] }
serde = "1"
serde = { workspace = true }
tokio = { workspace = true }

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions foyer-memory/Cargo.toml
Expand Up @@ -21,6 +21,7 @@ hashbrown = "0.14"
itertools = "0.12"
libc = "0.2"
parking_lot = "0.12"
serde = { workspace = true }
tokio = { workspace = true }

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion foyer-memory/src/cache.rs
Expand Up @@ -22,6 +22,7 @@ use std::{

use ahash::RandomState;
use futures::{Future, FutureExt};
use serde::{Deserialize, Serialize};
use tokio::sync::oneshot;

use foyer_common::code::{Key, Value};
Expand Down Expand Up @@ -219,7 +220,7 @@ where
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EvictionConfig {
Fifo(FifoConfig),
Lru(LruConfig),
Expand Down
3 changes: 2 additions & 1 deletion foyer-memory/src/eviction/fifo.rs
Expand Up @@ -18,6 +18,7 @@ use foyer_intrusive::{
dlist::{Dlist, DlistLink},
intrusive_adapter,
};
use serde::{Deserialize, Serialize};

use crate::{
eviction::Eviction,
Expand Down Expand Up @@ -87,7 +88,7 @@ where
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FifoConfig {}

pub struct Fifo<T>
Expand Down
15 changes: 14 additions & 1 deletion foyer-memory/src/eviction/lfu.rs
Expand Up @@ -20,14 +20,15 @@ use foyer_intrusive::{
dlist::{Dlist, DlistLink},
intrusive_adapter,
};
use serde::{Deserialize, Serialize};

use crate::{
eviction::Eviction,
handle::{BaseHandle, Handle},
CacheContext,
};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LfuConfig {
/// `window` capacity ratio of the total cache capacity.
///
Expand All @@ -45,6 +46,18 @@ pub struct LfuConfig {
pub cmsketch_eps: f64,
pub cmsketch_confidence: f64,
}

impl Default for LfuConfig {
fn default() -> Self {
Self {
window_capacity_ratio: 0.1,
protected_capacity_ratio: 0.8,
cmsketch_eps: 0.001,
cmsketch_confidence: 0.9,
}
}
}

#[derive(Debug, Clone)]
pub struct LfuContext;

Expand Down
11 changes: 10 additions & 1 deletion foyer-memory/src/eviction/lru.rs
Expand Up @@ -19,14 +19,15 @@ use foyer_intrusive::{
dlist::{Dlist, DlistLink},
intrusive_adapter,
};
use serde::{Deserialize, Serialize};

use crate::{
eviction::Eviction,
handle::{BaseHandle, Handle},
CacheContext,
};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LruConfig {
/// The ratio of the high priority pool occupied.
///
Expand All @@ -39,6 +40,14 @@ pub struct LruConfig {
pub high_priority_pool_ratio: f64,
}

impl Default for LruConfig {
fn default() -> Self {
Self {
high_priority_pool_ratio: 0.0,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LruContext {
HighPriority,
Expand Down
7 changes: 6 additions & 1 deletion foyer-memory/src/eviction/mod.rs
Expand Up @@ -14,12 +14,17 @@

use std::ptr::NonNull;

use serde::{de::DeserializeOwned, Serialize};

pub trait EvictionConfig: Send + Sync + 'static + Clone + Serialize + DeserializeOwned + Default {}
impl<T> EvictionConfig for T where T: Send + Sync + 'static + Clone + Serialize + DeserializeOwned + Default {}

/// The lifetime of `handle: Self::H` is managed by [`Indexer`].
///
/// Each `handle`'s lifetime in [`Indexer`] must outlive the raw pointer in [`Eviction`].
pub trait Eviction: Send + Sync + 'static {
type Handle;
type Config;
type Config: EvictionConfig;

/// Create a new empty eviction container.
///
Expand Down
13 changes: 12 additions & 1 deletion foyer-memory/src/eviction/s3fifo.rs
Expand Up @@ -22,6 +22,7 @@ use foyer_intrusive::{
dlist::{Dlist, DlistLink},
intrusive_adapter,
};
use serde::{Deserialize, Serialize};

use crate::{
eviction::Eviction,
Expand Down Expand Up @@ -119,13 +120,23 @@ where
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct S3FifoConfig {
pub small_queue_capacity_ratio: f64,
pub ghost_queue_capacity_ratio: f64,
pub small_to_main_freq_threshold: u8,
}

impl Default for S3FifoConfig {
fn default() -> Self {
Self {
small_queue_capacity_ratio: 0.1,
ghost_queue_capacity_ratio: 1.0,
small_to_main_freq_threshold: 1,
}
}
}

pub struct S3Fifo<T>
where
T: Send + Sync + 'static,
Expand Down
2 changes: 1 addition & 1 deletion foyer-storage-bench/Cargo.toml
Expand Up @@ -42,7 +42,7 @@ opentelemetry_sdk = { version = "0.22", features = [
parking_lot = "0.12"
prometheus = "0.13"
rand = "0.8.5"
serde ={ version = "1", features = ["derive", "rc"] }
serde = { workspace = true }
serde_bytes = "0.11.14"
tokio = { workspace = true }
tracing = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion foyer-storage/Cargo.toml
Expand Up @@ -32,7 +32,7 @@ parking_lot = { version = "0.12", features = ["arc_lock"] }
paste = "1.0"
prometheus = "0.13"
rand = "0.8.5"
serde = { version = "1", features = ["rc"] }
serde = { workspace = true }
thiserror = "1"
tokio = { workspace = true }
tracing = "0.1"
Expand Down
15 changes: 15 additions & 0 deletions foyer/src/hybrid.rs
Expand Up @@ -14,6 +14,7 @@

use std::{
borrow::Borrow,
fmt::Debug,
future::Future,
hash::{BuildHasher, Hash},
sync::{Arc, OnceLock},
Expand Down Expand Up @@ -379,6 +380,20 @@ where
store: Store<K, V>,
}

impl<K, V, S> Debug for HybridCache<K, V, S>
where
K: StorageKey,
V: StorageValue,
S: BuildHasher + Send + Sync + 'static,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HybridCache")
.field("cache", &self.cache)
.field("store", &self.store)
.finish()
}
}

impl<K, V, S> Clone for HybridCache<K, V, S>
where
K: StorageKey,
Expand Down

0 comments on commit 294fd16

Please sign in to comment.