-
Notifications
You must be signed in to change notification settings - Fork 19
fix(ttl_map): initialize time to 1 for correct wrapping sub #224
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
Merged
gabotechs
merged 1 commit into
datafusion-contrib:main
from
ruchirK:fix-time-wrapping-sub
Nov 19, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,7 +184,8 @@ where | |
| Self { | ||
| buckets, | ||
| data: stage_targets, | ||
| time: Arc::new(AtomicU64::new(0)), | ||
| // Explicitly initialize `time` to 1 to avoid underflow issues with circular buffer. | ||
| time: Arc::new(AtomicU64::new(1)), | ||
| gc_scheduler_task: None, | ||
| config, | ||
| #[cfg(test)] | ||
|
|
@@ -446,6 +447,34 @@ mod tests { | |
| assert!(final_time < 100); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_initial_time() { | ||
| // Create a map with 7 buckets. 7 is chosen specifically as it | ||
| // has the property that (2^64 - 1) % 7 = 1, whereas (0 - 1) % 7 = 6. | ||
| let ttl_map = TTLMap::<String, i32>::_new(TTLMapConfig { | ||
| ttl: Duration::from_millis(70), | ||
| tick: Duration::from_millis(10), | ||
| }); | ||
|
|
||
| ttl_map.get_or_init("test_key".to_string(), || 999); | ||
|
|
||
| // Advance GC 3 times, which shouldn't free the first key. | ||
| for _ in 0..3 { | ||
| TTLMap::<String, i32>::gc(ttl_map.time.clone(), &ttl_map.buckets); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think we can just truncate everything after this. |
||
|
|
||
| tokio::time::sleep(Duration::from_millis(10)).await; | ||
| // Check that we still have our key. Have to wait before asserting to avoid the assertion | ||
| // being spuriously true. | ||
| assert_eq!(ttl_map.data.len(), 1); | ||
|
|
||
| // Run GC for 4 more steps, at which point the first key should be removed. | ||
| for _ in 0..4 { | ||
| TTLMap::<String, i32>::gc(ttl_map.time.clone(), &ttl_map.buckets); | ||
| } | ||
| assert_eventually(|| ttl_map.data.is_empty(), Duration::from_millis(100)).await; | ||
| } | ||
|
|
||
| // Run with `cargo test bench_lock_contention --release -- --nocapture` to see output. | ||
| #[tokio::test(flavor = "multi_thread", worker_threads = 16)] | ||
| async fn bench_lock_contention() { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 nice. Any chance to reproduce this in a test that fails in
mainand succeeds in this branch?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that's a good q let me think on that for a minute
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! PTAL