Skip to content
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

Fix bug that scripts may not be loaded correctly in pipeline #1107

Merged
merged 1 commit into from Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/pipeline.ts
Expand Up @@ -253,6 +253,7 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {

// Check whether scripts exists
const scripts = [];
const addedScriptHashes = {};
for (let i = 0; i < this._queue.length; ++i) {
const item = this._queue[i];
if (this.isCluster && item.isCustomCommand) {
Expand All @@ -267,10 +268,11 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
continue;
}
const script = this._shaToScript[item.args[0]];
if (!script) {
if (!script || addedScriptHashes[script.sha]) {
continue;
}
scripts.push(script);
addedScriptHashes[script.sha] = true;
}

const _this = this;
Expand All @@ -279,7 +281,10 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
}

return this.redis
.script("exists", Array.from(new Set(scripts.map(({ sha }) => sha))))
.script(
"exists",
scripts.map(({ sha }) => sha)
)
.then(function (results) {
const pending = [];
for (let i = 0; i < results.length; ++i) {
Expand Down
49 changes: 32 additions & 17 deletions test/functional/pipeline.ts
Expand Up @@ -267,41 +267,56 @@ describe("pipeline", function () {
it("should check and load uniq scripts only", function (done) {
const redis = new Redis();
redis.defineCommand("test", {
numberOfKeys: 1,
lua: "return {unpack(KEYS),unpack(ARGV)}",
numberOfKeys: 2,
lua: "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}",
});
redis.defineCommand("echo", {
numberOfKeys: 1,
lua: "return {KEYS[1],ARGV[1]}",
});

redis.once("ready", function () {
const expectedComands = [
"script",
"script",
"script",
"evalsha",
"evalsha",
"evalsha",
"evalsha",
const expectedCommands = [
["script", "exists"],
["script", "load", "return {KEYS[1],ARGV[1]}"],
["script", "load", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}"],
["evalsha"],
["evalsha"],
["evalsha"],
["evalsha"],
["evalsha"],
["evalsha"],
];
const expectedResults = [
[null, ["a", "1"]],
[null, ["b", "2"]],
[null, ["k1", "k2", "v1", "v2"]],
[null, ["k3", "k4", "v3", "v4"]],
[null, ["c", "3"]],
[null, ["k5", "k6", "v5", "v6"]],
];
redis.monitor(function (err, monitor) {
monitor.on("monitor", function (_, command) {
const name = expectedComands.shift();
expect(name).to.eql(command[0]);
if (!expectedComands.length) {
const expectedCommand = expectedCommands.shift();
expectedCommand.forEach((arg, i) => expect(arg).to.eql(command[i]));
if (!expectedCommands.length) {
monitor.disconnect();
redis.disconnect();
done();
}
});
const pipe = redis.pipeline();
pipe
.echo("f", "0")
.test("a", "1")
.echo("a", "1")
.echo("b", "2")
.test("b", "3")
.exec();
.test("k1", "k2", "v1", "v2")
.test("k3", "k4", "v3", "v4")
.echo("c", "3")
.test("k5", "k6", "v5", "v6")
.exec(function (err, results) {
expect(err).to.eql(null);
expect(results).to.eql(expectedResults);
});
});
});
});
Expand Down