GeezerVison is showing github.com — Refresh slot cache when slot connection acquisition fails by agarwalbharat · Pull Request #4544 · redis/jedis
Conversation
agarwalbharat
commented
May 28, 2026
•
edited by cursor
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
getConnectionFromSlot() previously refreshed the cluster slot cache only when no pool was mapped for the requested slot. If a slot did have a mapped pool but borrowing a connection from that pool failed with a JedisConnectionException, the exception was propagated immediately.
This made getConnectionFromSlot() behave differently from normal command execution, where connection failures trigger retry handling and slot cache renewal.
Implemented the fallback in getConnectionFromSlot() for JedisConnectionException to renew the slot cache after a connection failure from the mapped slot pool, then resolve the slot again and retry connection acquisition. Preserve the existing fallback to a random reachable node when the slot is still unmapped after renewal.
Note Medium Risk
Changes cluster slot connection selection used by command routing and direct slot APIs; behavior is more resilient but alters failure/retry timing during topology or node outages.
Overview
ClusterConnectionProvider.getConnectionFromSlot() now treats a failed borrow from the mapped slot pool like other cluster paths: on JedisConnectionException, it renews the slot cache, looks up the slot again, and retries (or falls back to a random reachable node if the slot is still unmapped). Renewal and retry logic live in a new getConnectionFromSlotAfterRenewing() helper, with the original connection failure attached as a suppressed exception when retries still fail.
A unit test (ClusterConnectionProviderTest) verifies cache renewal and a successful second borrow after a stale pool throws. The test class is added to the formatter plugin includes in pom.xml.
Reviewed by Cursor Bugbot for commit 32fcf36. Bugbot is set up for automated code reviews on this repo. Configure here.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.
In case there are security findings, they will be communicated to you as a comment inside the PR.
Hope you’ll enjoy using Jit.
Questions? Comments? Want to learn more? Get in touch with us.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Pull request overview
Makes getConnectionFromSlot() resilient to a stale slot→node mapping: if borrowing a connection from the mapped pool throws JedisConnectionException, the slot cache is now renewed and the lookup is retried (falling back to a random reachable node if the slot remains unmapped), matching the behavior of normal command-path retry handling. Logic shared between the previously-unmapped and newly-failing cases is extracted into a getConnectionFromSlotAfterRenewing helper, with the original exception attached as suppressed when a follow-up attempt also fails.
Changes:
- Extract slot-cache renewal + retry logic into
getConnectionFromSlotAfterRenewing, preserving the original exception via addSuppressed.
- Wrap the initial
connectionPool.getResource() call in getConnectionFromSlot with a JedisConnectionException catch that invokes the renewal helper.
- Add a unit test verifying that a failing first
getResource() triggers renewClusterSlots and a successful connection from the refreshed pool.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File |
Description |
| src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java |
Adds renewal-and-retry fallback on JedisConnectionException in getConnectionFromSlot; refactors shared logic into helper. |
| src/test/java/redis/clients/jedis/providers/ClusterConnectionProviderTest.java |
New unit test validating renewal and retry behavior when the mapped slot pool fails. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Comment on lines
+218
to
+220
|
|
try{
|
|
|
return connectionPool.getResource();
|
|
|
} else {
|
|
|
// no choice, fallback to new connection to random node
|
|
|
return getConnection();
|
|
|
}catch(JedisConnectionException e){
|
|
@agarwalbharat
Thanks for spending time on this issue! As far as I see, this PR addresses #4154.
To get the Java Format Check green, the newly added test file needs to be opted into the formatter. Please add it to the <includes> block of the formatter-maven-plugin configuration in
pom.xml (https://github.com/redis/jedis/blob/master/pom.xml#L527):
<include>src/test/java/redis/clients/jedis/providers/ClusterConnectionProviderTest.java</include>
then to auto format it you can use
mvn formatter:format.
I'll do a closer review pass shortly — thanks again!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Implemented the fallback in getConnectionFromSlot()
Update getConnectionFromSlot() to renew the slot cache after a connection failure from the mapped slot pool, then resolve the slot again and retry connection acquisition. Preserve the existing fallback to a random reachable node when the slot is still unmapped after renewal.
To get the Java Format Check green, the newly added test file needs to be opted into the formatter. Please add it to the <includes> block of the formatter-maven-plugin configuration in
Thank you for the suggestion. I’ve added the file to the formatter includes and applied the formatting changes.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Not merging this PR — superseded by #4555. Recording the rationale here for traceability.
Why we're not taking this approach:
getConnectionFromSlot() is reached via the command path (doExecuteCommand → resolver.resolve → getConnection(args) → getConnectionFromSlot). ClusterCommandExecutor.handleConnectionProblem deliberately throttles renewal — only after 2 consecutive failures + backoff — specifically to avoid renewal storms.
This PR makes the first failed getResource() trigger renewSlotCache() immediately, with no backoff and no gate. JedisClusterInfoCache.renewClusterSlots() relies on rediscoverLock.tryLock(), which dedups concurrent renewals but not serial ones — so under concurrency a brief network hiccup can fan out into back-to-back renewals on the command path.
Agreed direction (see issue discussion :
- Short term — expose a manual
renewSlotCache() for direct getConnectionFromSlot() callers (which bypass ClusterCommandExecutor and so don't get handleConnectionProblem today). Expose renewSlotCache on JedisCluster as public API #4555
- Longer term — redesign topology refresh per the TODO at
ClusterCommandExecutor.java:338: per-node success/failure tracking with a configurable grace period. Tracked separately as its own effort.
Thanks @agarwalbharat for the analysis that led here — closing this in favor of #4555. Will follow up on the topology-refresh redesign in a dedicated issue.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
ggivo
Awaiting requested review from ggivo
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|