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: support parallel script execution in pipelines #1304

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,15 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {

const script = this._shaToScript[item.args[0]];

if (!script || this.redis._addedScriptHashes[script.sha]) {
if (
!script ||
this.redis._addedScriptHashes[script.sha] ||
scripts.includes(script)
Copy link
Collaborator Author

@marcbachmann marcbachmann Mar 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to replace the .includes with an object lookup or set.
A Set would automatically deduplicate it.

Maybe also use const scripts = {} and then Object.values(scripts) further down.
But the performance overhead is negligible as this operation only occurs every time the scripts cache is cleared. I'd rather deduplicate the execution on first load. With the current code the script gets loaded multiple times per for every pipeline created in parallel.

We could also change the behavior by assigning a promise and then reuse that in other requests.
Also doable in another PR if desired.

this.redis._addedScriptHashes[script.sha] = loadPromise

The whole logic could also be moved into the custom command functions invoked in redis.sendCommand.

) {
continue;
}

scripts.push(script);
this.redis._addedScriptHashes[script.sha] = true;
}

const _this = this;
Expand All @@ -330,7 +333,12 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
if (this.isCluster) {
return pMap(scripts, (script) => _this.redis.script("load", script.lua), {
concurrency: 10,
}).then(execPipeline);
}).then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});
}

return this.redis
Expand All @@ -352,7 +360,12 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
})
);
})
.then(execPipeline);
.then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});

function execPipeline() {
let data = "";
Expand Down
22 changes: 22 additions & 0 deletions test/functional/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,28 @@ describe("pipeline", function () {
});
});
});

it("should support parallel script execution", function (done) {
const random = `${Math.random()}`;
const redis = new Redis();
redis.defineCommand("something", {
numberOfKeys: 0,
lua: `return "${random}"`,
});
Promise.all([
redis.multi([["something"]]).exec(),
redis.multi([["something"]]).exec(),
])
.then(([[first], [second]]) => {
expect(first[0]).to.equal(null);
expect(first[1]).to.equal(random);
expect(second[0]).to.equal(null);
expect(second[1]).to.equal(random);
redis.disconnect();
done();
})
.catch(done);
});
});

describe("#length", function () {
Expand Down