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

fix: fix insert ephemeral #786

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
204 changes: 125 additions & 79 deletions foyer-memory/src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,18 @@
Operator::Immutable => shard.read().with(|shard| shard.release_immutable(&self.record)),
Operator::Mutable => shard.write().with(|mut shard| shard.release_mutable(&self.record)),
}

if self.record.is_ephemeral() {
shard
.write()
.with(|mut shard| shard.remove(hash, self.key()))
.inspect(|record| {
// Deallocate data out of the lock critical section.
if let Some(listener) = self.inner.event_listener.as_ref() {
listener.on_leave(Event::Remove, record.key(), record.value());
}
});
}
}
}
}
Expand Down Expand Up @@ -921,6 +933,18 @@

fn is_send_sync_static<T: Send + Sync + 'static>() {}

fn fifo_cache_for_test() -> RawCache<Fifo<u64, u64>> {
RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: FifoConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
})
}

#[test]
fn test_send_sync_static() {
is_send_sync_static::<RawCache<Fifo<(), ()>>>();
Expand All @@ -929,92 +953,114 @@
is_send_sync_static::<RawCache<Lru<(), ()>>>();
}

fn fuzzy<E>(cache: RawCache<E>, hints: Vec<E::Hint>)
where
E: Eviction<Key = u64, Value = u64>,
{
let handles = (0..8)
.map(|i| {
let c = cache.clone();
let hints = hints.clone();
std::thread::spawn(move || {
let mut rng = SmallRng::seed_from_u64(i);
for _ in 0..100000 {
let key = rng.next_u64();
if let Some(entry) = c.get(&key) {
assert_eq!(key, *entry);
drop(entry);
continue;
#[test]
fn test_insert_ephemeral() {
let fifo = fifo_cache_for_test();

let e1 = fifo.insert_ephemeral(1, 1);
assert_eq!(fifo.usage(), 1);
drop(e1);
assert_eq!(fifo.usage(), 0);

let e2a = fifo.insert_ephemeral(2, 2);
assert_eq!(fifo.usage(), 1);
let e2b = fifo.get(&2).expect("entry 2 should exist");
drop(e2a);
assert_eq!(fifo.usage(), 1);
drop(e2b);
assert_eq!(fifo.usage(), 1);
}

mod fuzzy {
use super::*;

fn fuzzy<E>(cache: RawCache<E>, hints: Vec<E::Hint>)
where
E: Eviction<Key = u64, Value = u64>,
{
let handles = (0..8)
.map(|i| {
let c = cache.clone();
let hints = hints.clone();
std::thread::spawn(move || {
let mut rng = SmallRng::seed_from_u64(i);
for _ in 0..100000 {
let key = rng.next_u64();
if let Some(entry) = c.get(&key) {
assert_eq!(key, *entry);
drop(entry);
continue;

Check warning on line 992 in foyer-memory/src/raw.rs

View check run for this annotation

Codecov / codecov/patch

foyer-memory/src/raw.rs#L990-L992

Added lines #L990 - L992 were not covered by tests
}
let hint = hints.choose(&mut rng).cloned().unwrap();
c.insert_with_hint(key, key, hint);
}
let hint = hints.choose(&mut rng).cloned().unwrap();
c.insert_with_hint(key, key, hint);
}
})
})
})
.collect_vec();
.collect_vec();

handles.into_iter().for_each(|handle| handle.join().unwrap());
handles.into_iter().for_each(|handle| handle.join().unwrap());

assert_eq!(cache.usage(), cache.capacity());
}
assert_eq!(cache.usage(), cache.capacity());
}

#[test_log::test]
fn test_fifo_cache_fuzzy() {
let cache: RawCache<Fifo<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: FifoConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![FifoHint];
fuzzy(cache, hints);
}
#[test_log::test]
fn test_fifo_cache_fuzzy() {
let cache: RawCache<Fifo<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: FifoConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![FifoHint];
fuzzy(cache, hints);
}

#[test_log::test]
fn test_s3fifo_cache_fuzzy() {
let cache: RawCache<S3Fifo<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: S3FifoConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![S3FifoHint];
fuzzy(cache, hints);
}
#[test_log::test]
fn test_s3fifo_cache_fuzzy() {
let cache: RawCache<S3Fifo<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: S3FifoConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![S3FifoHint];
fuzzy(cache, hints);
}

#[test_log::test]
fn test_lru_cache_fuzzy() {
let cache: RawCache<Lru<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: LruConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![LruHint::HighPriority, LruHint::LowPriority];
fuzzy(cache, hints);
}
#[test_log::test]
fn test_lru_cache_fuzzy() {
let cache: RawCache<Lru<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: LruConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![LruHint::HighPriority, LruHint::LowPriority];
fuzzy(cache, hints);
}

#[test_log::test]
fn test_lfu_cache_fuzzy() {
let cache: RawCache<Lfu<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: LfuConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![LfuHint];
fuzzy(cache, hints);
#[test_log::test]
fn test_lfu_cache_fuzzy() {
let cache: RawCache<Lfu<u64, u64>> = RawCache::new(RawCacheConfig {
name: "test".to_string(),
capacity: 256,
shards: 4,
eviction_config: LfuConfig::default(),
hash_builder: Default::default(),
weighter: Arc::new(|_, _| 1),
event_listener: None,
});
let hints = vec![LfuHint];
fuzzy(cache, hints);
}
}
}
Loading