Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(cache): Respect the max_removed_entry configuration #286

Merged
merged 2 commits into from
Jan 5, 2022
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
2 changes: 1 addition & 1 deletion docs/ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ use.
a matching request is processed.
- `max_removed_entries` - While running the cleanup task, at most this many
entries will be removed before cancelling the task. This should be used to
limit the maximum amount of time the cleanup task takes.
limit the maximum amount of time the cleanup task takes. Defaults to 100_000.
- `default_lock_timeout_sec` - The amount of time a cache entry can be locked
for writing.
- `inner` - Another provider configuration to generate suggestions with.
Expand Down
7 changes: 4 additions & 3 deletions merino-cache/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl Suggester {
let metrics = metrics_client.clone();
let task_items = items.clone();
let task_interval = config.cleanup_interval;
let max_removals = config.max_removed_entries;
tokio::spawn(async move {
let mut timer = tokio::time::interval(task_interval);
// The timer fires immediately, but we don't want to run the
Expand All @@ -120,7 +121,7 @@ impl Suggester {
timer.tick().await;
loop {
timer.tick().await;
Self::remove_expired_entries(&task_items, &metrics);
Self::remove_expired_entries(&task_items, max_removals, &metrics);
}
});
}
Expand All @@ -140,14 +141,14 @@ impl Suggester {
#[tracing::instrument(level = "debug", skip(items))]
fn remove_expired_entries<K: Eq + Hash + Debug>(
items: &Arc<DedupedMap<K, Instant, Vec<Suggestion>>>,
max_removals: usize,
metrics_client: &StatsdClient,
) {
let start = Instant::now();
let count_before_storage = items.len_storage();
let count_before_pointers = items.len_pointers();

// Retain all cache entries that have not yet expired.
let max_removals = 10_000;
let mut num_removals = 0;
items.retain(|_key, expiration, _suggestions| {
if num_removals > max_removals {
Expand Down Expand Up @@ -315,7 +316,7 @@ mod tests {
let (rx, sink) = SpyMetricSink::new();
let metrics_client = StatsdClient::from_sink("merino-test", sink);

Suggester::remove_expired_entries(&cache, &metrics_client);
Suggester::remove_expired_entries(&cache, 100, &metrics_client);

assert_eq!(cache.len_storage(), 1);
assert_eq!(cache.len_pointers(), 1);
Expand Down