Repository navigation
Fix/cluster stuck in close #2217
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
+168
−1
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ac39fb2
Specify issue details in ISSUE.md
Duwab bb2c124
Test failing reconnect after node end
Duwab e284166
fix(cluster): finish closing when connect() is aborted during startup…
Duwab d2ba788
Update ISSUE details
Duwab 141501c
Document where the issue comes from
Duwab 789865b
Clarify testing logic
Duwab ea1445b
Remove issue report
Duwab 3b3c509
fix(cluster): settle ready-delayed callbacks when connect() is aborted
Duwab 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,109 @@ | ||
| import MockServer from "../../helpers/mock_server"; | ||
| import { expect } from "chai"; | ||
| import { Cluster } from "../../../lib"; | ||
|
|
||
| const DISCONNECT_TIMEOUT = 200; | ||
|
|
||
| /** | ||
| * Resolves on the first `event`, or rejects after `ms` with the cluster state. | ||
| */ | ||
| function waitFor(cluster: Cluster, event: string, ms: number) { | ||
| return new Promise<void>((resolve, reject) => { | ||
| const timer = setTimeout(() => { | ||
| cluster.removeListener(event, onEvent); | ||
| reject( | ||
| new Error( | ||
| `no "${event}" within ${ms}ms: status=${cluster.status}, ` + | ||
| // @ts-expect-error | ||
| `reconnectTimeout=${Boolean(cluster.reconnectTimeout)}, ` + | ||
| // @ts-expect-error | ||
| `offlineQueue=${cluster.offlineQueue.length}` | ||
| ) | ||
| ); | ||
| }, ms); | ||
| const onEvent = () => { | ||
| clearTimeout(timer); | ||
| resolve(); | ||
| }; | ||
| cluster.once(event, onEvent); | ||
| }); | ||
| } | ||
|
|
||
| describe("cluster:late node end", () => { | ||
| const PORT_NODE_1 = 30001; | ||
| const PORT_NODE_2 = 30002; | ||
| const slotTable = [ | ||
| [0, 8000, ["127.0.0.1", PORT_NODE_1]], | ||
| [8001, 16383, ["127.0.0.1", PORT_NODE_2]], | ||
| ]; | ||
|
|
||
| it("should reconnect when a node ends while resolving the startup nodes", async () => { | ||
| // [Setup] Configure Redis servers (2 mock nodes) | ||
| // The first ready check fails, and the server keeps that socket half-open | ||
| let readyChecks = 0; | ||
| let slowPort: number | undefined; | ||
| const redisServerHandler = (argv, c) => { | ||
| if (argv[0] === "cluster" && argv[1].toLowerCase() === "info") { | ||
| readyChecks++; | ||
| // First ready check → failure | ||
| if (readyChecks === 1) { | ||
| slowPort = c.localPort; | ||
| c.allowHalfOpen = true; | ||
| return "cluster_state:fail"; | ||
| } | ||
| // Other ready checks → success | ||
| return "cluster_state:ok"; | ||
| } | ||
| if (argv[0] === "get") { | ||
| return "bar"; | ||
| } | ||
| }; | ||
| new MockServer(PORT_NODE_1, redisServerHandler, slotTable); | ||
| new MockServer(PORT_NODE_2, redisServerHandler, slotTable); | ||
|
|
||
| // [Setup] Configure DNS lookup | ||
| // The lookup made by the reconnection only resolves once the slow node has ended. | ||
| let lookups = 0; | ||
| let slowNodeEndedDuringLookup = false; | ||
| const cluster = new Cluster( | ||
| [ | ||
| { | ||
| host: "redis.test", // Use a hostname instead of an IP to force one dnsLookup() per connect() | ||
| port: PORT_NODE_1, | ||
| }, | ||
| ], | ||
| { | ||
| redisOptions: { disconnectTimeout: DISCONNECT_TIMEOUT }, | ||
| dnsLookup(hostname, callback) { | ||
| lookups++; | ||
|
|
||
| // Only the 2nd lookup resolves late (after the slow node has ended) | ||
| if (lookups === 2) { | ||
| const onNodeEnd = (redis) => { | ||
| if (redis.options.port !== slowPort) return; | ||
| cluster.removeListener("-node", onNodeEnd); | ||
| slowNodeEndedDuringLookup = true; | ||
| setImmediate(() => callback(null, "127.0.0.1")); | ||
| }; | ||
| cluster.on("-node", onNodeEnd); | ||
| } else { | ||
| callback(null, "127.0.0.1"); | ||
| } | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| // Sent during the incident: queued until the cluster is ready. | ||
| const pending = cluster.get("foo"); | ||
|
|
||
| try { | ||
| await waitFor(cluster, "ready", 5 * DISCONNECT_TIMEOUT); | ||
|
|
||
| expect(slowNodeEndedDuringLookup).to.eql(true); | ||
| expect(readyChecks).to.eql(2); | ||
| expect(await pending).to.eql("bar"); | ||
| } finally { | ||
| cluster.disconnect(); | ||
| } | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.