Skip to content

Commit

Permalink
awesome
Browse files Browse the repository at this point in the history
  • Loading branch information
just-in-chang committed Jun 26, 2024
1 parent 07952ba commit 8dcf743
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion crates/aptos-in-memory-cache/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<C: SizedCache<NotATransaction> + 'static> Cache<usize, NotATransaction> for
let size_in_bytes = value.get_size();
self.cache
.insert_with_size(key, Arc::new(value), size_in_bytes);
if self.cache.total_size() > self.metadata.eviction_trigger_size_in_bytes {
if self.cache.total_size() >= self.metadata.eviction_trigger_size_in_bytes {
self.eviction_start.store(key, Ordering::Relaxed);
self.insert_notify.notify_one();
}
Expand All @@ -112,6 +112,7 @@ fn spawn_eviction_task<C: SizedCache<NotATransaction> + 'static>(
tokio::spawn(async move {
loop {
insert_notify.notified().await;
println!("awesome");
let watermark_value = highest_key.load(Ordering::Relaxed);
let mut eviction_index = (watermark_value + 1) % metadata.capacity;

Expand Down
10 changes: 8 additions & 2 deletions crates/aptos-in-memory-cache/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ mod tests {
// Wait for eviction to occur
tokio::time::sleep(Duration::from_micros(1)).await;

// assert_eq!(cache.total_size(), 6 * std::mem::size_of_val(&NotATransaction::new(0)));
assert_eq!(
cache.total_size(),
6 * std::mem::size_of_val(&NotATransaction::new(0))
);

// Further inserts to ensure eviction continues correctly
for i in 7..10 {
Expand Down Expand Up @@ -154,7 +157,10 @@ mod tests {
// Wait for eviction to occur
tokio::time::sleep(Duration::from_micros(1)).await;

// assert_eq!(cache.total_size(), 10 * std::mem::size_of_val(&NotATransaction::new(0)));
assert_eq!(
cache.total_size(),
10 * std::mem::size_of_val(&NotATransaction::new(0))
);

tokio::time::sleep(Duration::from_micros(1)).await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Cache<u64, Transaction> for InMemoryCache {
.insert_with_size(key, Arc::new(value), size_in_bytes);
self.eviction_start.store(key, Ordering::Relaxed);
if self.cache.total_size() > self.metadata.size_config.cache_eviction_trigger_size_bytes {
self.insert_notify.notify_waiters();
self.insert_notify.notify_one();
}
}

Expand Down Expand Up @@ -251,6 +251,10 @@ fn spawn_eviction_task<C: SizedCache<Transaction> + 'static>(
// If we accidentally evict a newer transaction, we insert it back.
if value.key > watermark_value {
cache.insert_with_size(value.key, value.value.clone(), value.size_in_bytes);
highest_key.store(value.key, Ordering::Relaxed);
// Notifying here will trigger another check from this function to see if we need to evict more.
insert_notify.notify_one();
break;
}
}
eviction_index = (eviction_index + 1) % metadata.size_config.cache_capacity;
Expand Down

0 comments on commit 8dcf743

Please sign in to comment.