-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: abort incomplete pipelines upon reconnect (#1084)
Elasticache severs the connection immediately after it returns a READONLY error. This can sometimes leave queued up pipelined commands in an inconsistent state when the connection is reestablished. For example, if a pipeline has 6 commands and the second one generates a READONLY error, Elasticache will only return results for the first two before severing the connection. Upon reconnect, the pipeline still thinks it has 6 commands to send but the commandQueue has only 4. This fix will detect any pipeline command sets that only had a partial response before connection loss, and abort them. This Elasticache behavior also affects transactions. If reconnectOnError returns 2, some transaction fragments may end up in the offlineQueue. This fix will check the offlineQueue for any such transaction fragments and abort them, so that we don't send mismatched multi/exec to redis upon reconnection. - Introduced piplineIndex property on pipelined commands to allow for later cleanup - Added a routine to event_handler that aborts any pipelined commands inside commandQueue and offlineQueue that were interrupted in the middle of the pipeline - Added a routine to event_handler that removes any transaction fragments from the offline queue - Introduced inTransaction property on commands to simplify pipeline logic - Added a flags param to mock_server to allow the Elasticache disconnect behavior to be simulated - Added a reconnect_on_error test case for transactions - Added some test cases testing for correct handling of this unique elasticache behavior - Added unit tests to validate inTransaction and pipelineIndex setting Fixes #965
- Loading branch information
Showing
8 changed files
with
344 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,153 @@ | ||
import Redis from "../../lib/redis"; | ||
import { expect } from "chai"; | ||
import MockServer from "../helpers/mock_server"; | ||
|
||
// AWS Elasticache closes the connection immediately when it encounters a READONLY error | ||
function simulateElasticache(options: { | ||
reconnectOnErrorValue: boolean | number; | ||
}) { | ||
let inTransaction = false; | ||
const mockServer = new MockServer(30000, (argv, socket, flags) => { | ||
switch (argv[0]) { | ||
case "multi": | ||
inTransaction = true; | ||
return MockServer.raw("+OK\r\n"); | ||
case "del": | ||
flags.disconnect = true; | ||
return new Error( | ||
"READONLY You can't write against a read only replica." | ||
); | ||
case "get": | ||
return inTransaction ? MockServer.raw("+QUEUED\r\n") : argv[1]; | ||
case "exec": | ||
inTransaction = false; | ||
return []; | ||
} | ||
}); | ||
|
||
return new Redis({ | ||
port: 30000, | ||
reconnectOnError(err: Error): boolean | number { | ||
// bring the mock server back up | ||
mockServer.connect(); | ||
return options.reconnectOnErrorValue; | ||
} | ||
}); | ||
} | ||
|
||
function expectReplyError(err) { | ||
expect(err).to.exist; | ||
expect(err.name).to.eql("ReplyError"); | ||
} | ||
|
||
function expectAbortError(err) { | ||
expect(err).to.exist; | ||
expect(err.name).to.eql("AbortError"); | ||
expect(err.message).to.eql("Command aborted due to connection close"); | ||
} | ||
|
||
describe("elasticache", function() { | ||
it("should abort a failed transaction when connection is lost", function(done) { | ||
const redis = simulateElasticache({ reconnectOnErrorValue: true }); | ||
|
||
redis | ||
.multi() | ||
.del("foo") | ||
.del("bar") | ||
.exec(err => { | ||
expectAbortError(err); | ||
expect(err.command).to.eql({ | ||
name: "exec", | ||
args: [] | ||
}); | ||
expect(err.previousErrors).to.have.lengthOf(2); | ||
expectReplyError(err.previousErrors[0]); | ||
expect(err.previousErrors[0].command).to.eql({ | ||
name: "del", | ||
args: ["foo"] | ||
}); | ||
expectAbortError(err.previousErrors[1]); | ||
expect(err.previousErrors[1].command).to.eql({ | ||
name: "del", | ||
args: ["bar"] | ||
}); | ||
|
||
// ensure we've recovered into a healthy state | ||
redis.get("foo", (err, res) => { | ||
expect(res).to.eql("foo"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it("should not resend failed transaction commands", function(done) { | ||
const redis = simulateElasticache({ reconnectOnErrorValue: 2 }); | ||
redis | ||
.multi() | ||
.del("foo") | ||
.get("bar") | ||
.exec(err => { | ||
expectAbortError(err); | ||
expect(err.command).to.eql({ | ||
name: "exec", | ||
args: [] | ||
}); | ||
expect(err.previousErrors).to.have.lengthOf(2); | ||
expectAbortError(err.previousErrors[0]); | ||
expect(err.previousErrors[0].command).to.eql({ | ||
name: "del", | ||
args: ["foo"] | ||
}); | ||
expectAbortError(err.previousErrors[1]); | ||
expect(err.previousErrors[1].command).to.eql({ | ||
name: "get", | ||
args: ["bar"] | ||
}); | ||
|
||
// ensure we've recovered into a healthy state | ||
redis.get("foo", (err, res) => { | ||
expect(res).to.eql("foo"); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it("should resend intact pipelines", function(done) { | ||
const redis = simulateElasticache({ reconnectOnErrorValue: true }); | ||
|
||
let p1Result; | ||
redis | ||
.pipeline() | ||
.del("foo") | ||
.get("bar") | ||
.exec((err, result) => (p1Result = result)); | ||
|
||
redis | ||
.pipeline() | ||
.get("baz") | ||
.get("qux") | ||
.exec((err, p2Result) => { | ||
// First pipeline should have been aborted | ||
expect(p1Result).to.have.lengthOf(2); | ||
expect(p1Result[0]).to.have.lengthOf(1); | ||
expect(p1Result[1]).to.have.lengthOf(1); | ||
expectReplyError(p1Result[0][0]); | ||
expect(p1Result[0][0].command).to.eql({ | ||
name: "del", | ||
args: ["foo"] | ||
}); | ||
expectAbortError(p1Result[1][0]); | ||
expect(p1Result[1][0].command).to.eql({ | ||
name: "get", | ||
args: ["bar"] | ||
}); | ||
|
||
// Second pipeline was intact and should have been retried successfully | ||
expect(p2Result).to.have.lengthOf(2); | ||
expect(p2Result[0]).to.eql([null, "baz"]); | ||
expect(p2Result[1]).to.eql([null, "qux"]); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |
This file contains 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 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.