Skip to content

Commit 28a278c

Browse files
authored
fix(ttl_map): initialize time to 1 for correct wrapping sub (#224)
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 8a2428b commit 28a278c

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
@@ -189,7 +189,8 @@ where
189189
Self {
190190
buckets,
191191
data: stage_targets,
192-
time: Arc::new(AtomicU64::new(0)),
192+
// Explicitly initialize `time` to 1 to avoid underflow issues with circular buffer.
193+
time: Arc::new(AtomicU64::new(1)),
193194
gc_scheduler_task: None,
194195
config,
195196
#[cfg(test)]
@@ -474,6 +475,34 @@ mod tests {
474475
assert!(final_time < 100);
475476
}
476477

478+
#[tokio::test]
479+
async fn test_initial_time() {
480+
// Create a map with 7 buckets. 7 is chosen specifically as it
481+
// has the property that (2^64 - 1) % 7 = 1, whereas (0 - 1) % 7 = 6.
482+
let ttl_map = TTLMap::<String, i32>::_new(TTLMapConfig {
483+
ttl: Duration::from_millis(70),
484+
tick: Duration::from_millis(10),
485+
});
486+
487+
ttl_map.get_or_init("test_key".to_string(), || 999);
488+
489+
// Advance GC 3 times, which shouldn't free the first key.
490+
for _ in 0..3 {
491+
TTLMap::<String, i32>::gc(ttl_map.time.clone(), &ttl_map.buckets);
492+
}
493+
494+
tokio::time::sleep(Duration::from_millis(10)).await;
495+
// Check that we still have our key. Have to wait before asserting to avoid the assertion
496+
// being spuriously true.
497+
assert_eq!(ttl_map.data.len(), 1);
498+
499+
// Run GC for 4 more steps, at which point the first key should be removed.
500+
for _ in 0..4 {
501+
TTLMap::<String, i32>::gc(ttl_map.time.clone(), &ttl_map.buckets);
502+
}
503+
assert_eventually(|| ttl_map.data.is_empty(), Duration::from_millis(100)).await;
504+
}
505+
477506
// Run with `cargo test bench_lock_contention --release -- --nocapture` to see output.
478507
#[tokio::test(flavor = "multi_thread", worker_threads = 16)]
479508
async fn bench_lock_contention() {

0 commit comments

Comments
 (0)