Skip to content

Commit 063c1b4

Browse files
committed
fix(ttl_map): initialize time to 1 for correct wrapping sub
Fixes #221. Previously, we encountered a bug at the initial gc tick on ttl_map where `time` (denoting the number of ticks since map initialization) starts at 0, but the logic to compute the time to remove the inserted value is ``` free_time = (time - 1) // (number of ticks to ttl) ``` In other words, we maintain a circular buffer of inserts, with a single slot per tick, and we will delete the item once `buffer.len()` ticks have elapsed (ttl = tick * buffer.len()). This is all fine, except we implement the logic as: ``` free_time = time.wrapping_sub(1) / buffer.len() ``` which is computing for `u64` `time`: ``` free_time = ((time - 1) mod 2^64) mod buffer.len() ``` when we really just want `free_time = (time - 1) mod buffer.len()`. Luckily this equality holds as long as ` 0 < time < 2^64`, as for those times `time - 1 mod 2&64 = time`. This commit changes our behavior to initialize `time` at 1 instead of 0. We don't need to worry about the overflow case because even if the tick duration was a nanosecond, it would take ~584 years to overflow 64 bits at which point we would surely have other problems besides the momentarily incorrect ttl. For the underflow case, this should primarily help with unit tests above anything else, as the bug only happened at `time = 0`.
1 parent 731ad2a commit 063c1b4

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/common/ttl_map.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ where
184184
Self {
185185
buckets,
186186
data: stage_targets,
187-
time: Arc::new(AtomicU64::new(0)),
187+
// Explicitly initialize `time` to 1 to avoid underflow issues with circular buffer.
188+
time: Arc::new(AtomicU64::new(1)),
188189
gc_scheduler_task: None,
189190
config,
190191
#[cfg(test)]
@@ -446,6 +447,34 @@ mod tests {
446447
assert!(final_time < 100);
447448
}
448449

450+
#[tokio::test]
451+
async fn test_initial_time() {
452+
// Create a map with 7 buckets. 7 is chosen specifically as it
453+
// has the property that (2^64 - 1) % 7 = 1, whereas (0 - 1) % 7 = 6.
454+
let ttl_map = TTLMap::<String, i32>::_new(TTLMapConfig {
455+
ttl: Duration::from_millis(70),
456+
tick: Duration::from_millis(10),
457+
});
458+
459+
ttl_map.get_or_init("test_key".to_string(), || 999);
460+
461+
// Advance GC 3 times, which shouldn't free the first key.
462+
for _ in 0..3 {
463+
TTLMap::<String, i32>::gc(ttl_map.time.clone(), &ttl_map.buckets);
464+
}
465+
466+
tokio::time::sleep(Duration::from_millis(10)).await;
467+
// Check that we still have our key. Have to wait before asserting to avoid the assertion
468+
// being spuriously true.
469+
assert_eq!(ttl_map.data.len(), 1);
470+
471+
// Run GC for 4 more steps, at which point the first key should be removed.
472+
for _ in 0..4 {
473+
TTLMap::<String, i32>::gc(ttl_map.time.clone(), &ttl_map.buckets);
474+
}
475+
assert_eventually(|| ttl_map.data.len() == 0, Duration::from_millis(100)).await;
476+
}
477+
449478
// Run with `cargo test bench_lock_contention --release -- --nocapture` to see output.
450479
#[tokio::test(flavor = "multi_thread", worker_threads = 16)]
451480
async fn bench_lock_contention() {

0 commit comments

Comments
 (0)