-
Notifications
You must be signed in to change notification settings - Fork 231
fix: validate connection health and handle missing slots in cluster refresh #944
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
billshen99
wants to merge
1
commit into
redis:main
Choose a base branch
from
billshen99:conn_reuse_and_slot_missing_fix_clean
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.
+185
−2
Open
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 |
|---|---|---|
|
|
@@ -241,10 +241,18 @@ func (c *clusterClient) _refresh() (err error) { | |
| var removes []conn | ||
|
|
||
| c.mu.RLock() | ||
| // Preserve old slot mappings to use as fallback for incomplete topology | ||
| oldWslots := c.wslots | ||
| oldRslots := c.rslots | ||
| for addr, cc := range c.conns { | ||
| if fresh, ok := conns[addr]; ok { | ||
| fresh.conn = cc.conn | ||
| conns[addr] = fresh | ||
| // Validate connection health before reusing | ||
| // If connection has error, it will be replaced with a new one | ||
| if cc.conn.Error() == nil { | ||
| fresh.conn = cc.conn | ||
| conns[addr] = fresh | ||
| } | ||
| // else: let the new connection be created (conns[addr] already has fresh conn from connFn) | ||
| } else { | ||
| removes = append(removes, cc.conn) | ||
| } | ||
|
|
@@ -321,6 +329,19 @@ func (c *clusterClient) _refresh() (err error) { | |
| } | ||
| } | ||
|
|
||
| // Preserve old slot mappings for any slots not covered by the new topology | ||
| // This prevents nil connections when CLUSTER SLOTS returns incomplete data | ||
| for i := 0; i < 16384; i++ { | ||
|
Comment on lines
+332
to
+334
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. We should not do this. |
||
| if wslots[i] == nil && oldWslots[i] != nil { | ||
| wslots[i] = oldWslots[i] | ||
| } | ||
| if c.rOpt != nil && len(rslots) > 0 { | ||
| if len(rslots[i]) == 0 && len(oldRslots) > i && len(oldRslots[i]) > 0 { | ||
| rslots[i] = oldRslots[i] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| c.mu.Lock() | ||
| c.wslots = wslots | ||
| c.rslots = rslots | ||
|
|
@@ -469,6 +490,8 @@ func (c *clusterClient) _pick(slot uint16, toReplica bool) (p conn) { | |
| } | ||
| } | ||
| p = nodes[rIndex].conn | ||
| } else { | ||
| p = c.wslots[slot] // fallback to master when replica slots are missing | ||
| } | ||
| } else { | ||
| p = c.wslots[slot] | ||
|
|
@@ -628,6 +651,8 @@ func (c *clusterClient) _pickMulti(multi []Completed) (retries *connretry, init | |
| } | ||
| } | ||
| cc = nodes[rIndex].conn | ||
| } else { | ||
| cc = c.wslots[slot] // fallback to master when replica slots are missing | ||
| } | ||
| } else { | ||
| cc = c.wslots[slot] | ||
|
|
@@ -1117,6 +1142,8 @@ func (c *clusterClient) _pickMultiCache(multi []CacheableTTL) *connretrycache { | |
| } | ||
| } | ||
| p = nodes[rIndex].conn | ||
| } else { | ||
| p = c.wslots[slot] // fallback to master when replica slots are missing | ||
| } | ||
| } else { | ||
| p = c.wslots[slot] | ||
|
|
||
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
Oops, something went wrong.
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.
muxhandles reconnections internally, so no Error() check at the cluster client is okay.