-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Improve RedisCluster
slot caching and invalidation.
#2655
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
Draft
michael-grunder
wants to merge
6
commits into
develop
Choose a base branch
from
fix/slot-cache-invalidation
base: develop
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.
Conversation
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
When a replica node is taken offline, it will briefly respond to commands with some form of `CLUSTERDOWN` error. Presently while this did cause PhpRedis to invalidate the slot cache, we did not immediately remap the topology. What this meant, is that for an already connected cluster, we might keep sending readonly commands to this now decomissioned cluster replica for the duration of the request. Additionally, if the request was long lived enough we may still have it mapped if the replica was brought back online at which point the client might see dozens of `LOADING` exceptions. This commit just changes our behavior when we detect any `CLUSTERDOWN` error such that we flush the slot cache, disconnect the particular node, and also attempt a remap of the topology.
This commit adds a new `ini` setting `redis.clusters.slot_cache_expiry` which specifies in seconds how long a cached slot map should live before being refreshed. The setting is disabled by default to avoid breaking backward compatibility.
b609b64
to
86391ec
Compare
86391ec
to
2ecdd3f
Compare
This commit adds a mechanism to invalidate every `RedisCluster` slot cache across all forked workers with one call to any of the workers. The invalidation itslef works by using a `uint64_t` generation counter which is allocated in shared memory on `MINIT` which is stored in every slot cache when it is created. When a user wants to invalidate any of the slot caches they can call `RedisCluster::invalidateSlotCaches()` which simply does an atomic increment on this global genertion value. When a slot cache is retreived from the persistent list, the internal generation is compared to the global shared memory generation and if they don't match, the cache is not used, which is the same as invalidting it as RedisCluster will write a new slot cache on object destruction. The feature is only compiled if `config.m4` determines that using `mmap` to allocate shared memory is available and works. Additionally, the feature must be explicitly enabled by setting the `redis.clusters.shared_slot_cache_invalidation`.
2ecdd3f
to
fead6c7
Compare
yatsukhnenko
approved these changes
May 1, 2025
yatsukhnenko
reviewed
May 1, 2025
dc6e530
to
2e38b5c
Compare
`time(NULL)` isn't monotonic and can run backwards so switch to `zend_hrtime` which uses `clock_gettime(CLOCK_MONOTONIC)` on Linux and `QueryPerformanceCounter`on Windows.
2e38b5c
to
f01f805
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR contains a few changes to our slot caching mechanism and cluster remapping.
CLUSTERDOWN
to an event that triggers a remapping of the keyspace. This can occur for example right after a node is removed from the cluster viaCLUSTER FORGET
. In addition, we now immediately remap the keyspace in this situation.ini
setting to tellRedisCluster
that the slot cache should expire after some number of seconds. This ensures that the keyspace is remapped even after non-destructive changes are made to the cluster such as a new replica being brought online. new replicas would never causeMOVED
orASKING
responses so PhpRedis wouldn't know they were avaialble.