-
-
Notifications
You must be signed in to change notification settings - Fork 369
Mitigate stale cache across processes (related to #735) #746
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
Open
snaka
wants to merge
2
commits into
splitrb:main
Choose a base branch
from
snaka:add-cache-invalidation-ttl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Split | ||
| # Manages global cache invalidation across multiple processes using Redis timestamp. | ||
| # The mechanism is opt-in: it activates only when | ||
| # Split.configuration.cache_global_ts_check_interval is set. | ||
| class CacheInvalidator | ||
| class << self | ||
| # Check global timestamp and clear cache if it has been updated. | ||
| # No-op when cache_global_ts_check_interval is not configured. | ||
| # @param cache [Split::Cache] the cache instance to potentially clear | ||
| def check_and_clear_if_needed(cache) | ||
| return unless check_interval | ||
|
|
||
| now = Time.now | ||
|
|
||
| # Skip if within check interval | ||
| return if within_check_interval?(now) | ||
|
|
||
| # Get global timestamp from Redis | ||
| current_global_ts = fetch_global_timestamp | ||
|
|
||
| # Clear cache if timestamp was updated | ||
| cache.clear if timestamp_updated?(current_global_ts) | ||
|
|
||
| # Update local timestamp and check time | ||
| update_local_state(current_global_ts, now) | ||
| end | ||
|
|
||
| # Invalidate cache globally by updating the global timestamp. | ||
| # No-op when cache_global_ts_check_interval is not configured. | ||
| def invalidate | ||
| return unless check_interval | ||
|
|
||
| Split.redis.set(global_timestamp_key, Time.now.to_f) | ||
| end | ||
|
|
||
| # Reset local state (used when cache is cleared) | ||
| def reset | ||
| @global_cache_ts = nil | ||
| @last_global_ts_check = nil | ||
| end | ||
|
|
||
| private | ||
| def within_check_interval?(now) | ||
| # Snapshot the ivar so a concurrent `reset` cannot nil it between reads | ||
| last_check = @last_global_ts_check | ||
| last_check && (now.to_f - last_check.to_f) < check_interval | ||
| end | ||
|
|
||
| def fetch_global_timestamp | ||
| Split.redis.get(global_timestamp_key).to_f | ||
| end | ||
|
|
||
| def timestamp_updated?(current_global_ts) | ||
| # Snapshot the ivar so a concurrent `reset` cannot nil it between reads | ||
| known_ts = @global_cache_ts | ||
| known_ts && current_global_ts > known_ts | ||
| end | ||
|
|
||
| def update_local_state(current_global_ts, now) | ||
| @global_cache_ts = current_global_ts | ||
| @last_global_ts_check = now | ||
| end | ||
|
|
||
| def global_timestamp_key | ||
| "split:cache:global_ts" | ||
| end | ||
|
|
||
| def check_interval | ||
| Split.configuration.cache_global_ts_check_interval | ||
| end | ||
| end | ||
| end | ||
| end |
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
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
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
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
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.
Building on top of "broken"
Split::Cachebehavior:clearis not thread-safe, can causeundefined method '[]' for nil (NoMethodError)to be raised fromfetchexecuting in a different thread (e.g. ifclearsets@cache = nilin thread A afterfetchperformed@cache ||= {}but before@cache[namespace][key]in thread B).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.
Fixed in 144b194.
fetchandclear_keynow take a local snapshot of@cacheup front, so a concurrentclearcan no longer cause theNoMethodError— at worst a racing thread writes into a detached hash, which is just one extra cache miss. Applied the same pattern to the ivars read twice inCacheInvalidator's predicates. Added a deterministic regression spec (clear landing mid-fetch), which raisesNoMethodErroron the previous code.