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

Pipeline-based script loading and retries #1499

Merged
merged 4 commits into from
Feb 20, 2022
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
42 changes: 10 additions & 32 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ class Redis extends Commander {
private _autoPipelines = new Map();
private _runningAutoPipelines = new Set();

// Prepare a cache of scripts and setup a interval which regularly clears it
private _addedScriptHashes: { [sha: string]: true } = {};
private _addedScriptHashesCleanInterval?: ReturnType<
typeof setInterval
> | null = null;

constructor(port: number, host: string, options: RedisOptions);
constructor(path: string, options: RedisOptions);
constructor(port: number, options: RedisOptions);
Expand Down Expand Up @@ -187,13 +181,6 @@ class Redis extends Commander {
process.nextTick(this.emit.bind(this, status, arg));
}

private clearAddedScriptHashesCleanInterval() {
if (this._addedScriptHashesCleanInterval) {
clearInterval(this._addedScriptHashesCleanInterval);
this._addedScriptHashesCleanInterval = null;
}
}

/**
* Create a connection to Redis.
* This method will be invoked automatically when creating a new Redis instance
Expand All @@ -213,18 +200,6 @@ class Redis extends Commander {
return;
}

// Make sure only one timer is active at a time
this.clearAddedScriptHashesCleanInterval();

// Scripts need to get reset on reconnect as redis
// might have been restarted or some failover happened
this._addedScriptHashes = {};

// Start the script cache cleaning
this._addedScriptHashesCleanInterval = setInterval(() => {
this._addedScriptHashes = {};
}, this.options.maxScriptsCachingTime);

this.connectionEpoch += 1;
this.setStatus("connecting");

Expand Down Expand Up @@ -343,8 +318,6 @@ class Redis extends Commander {
* If you want to wait for the pending replies, use Redis#quit instead.
*/
disconnect(reconnect = false) {
this.clearAddedScriptHashesCleanInterval();

if (!reconnect) {
this.manuallyClosing = true;
}
Expand Down Expand Up @@ -628,10 +601,6 @@ class Redis extends Commander {
command.setTimeout(this.options.commandTimeout);
}

if (command.name === "quit") {
this.clearAddedScriptHashesCleanInterval();
}

let writable =
this.status === "ready" ||
(!stream &&
Expand Down Expand Up @@ -679,7 +648,16 @@ class Redis extends Commander {
command.args
);
}
(stream || this.stream).write(command.toWritable());

if (stream) {
if (stream.isPipeline) {
stream.write(command.toWritable(stream.destination.redis.stream));
} else {
stream.write(command.toWritable(stream));
}
} else {
this.stream.write(command.toWritable(this.stream));
}

this.commandQueue.push({
command: command,
Expand Down
23 changes: 0 additions & 23 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class Cluster extends Commander {
_groupsBySlot: number[] = Array(16384);
private _runningAutoPipelines: Set<string> = new Set();
private _readyDelayedCallbacks: CallbackFunction[] = [];
public _addedScriptHashes: { [key: string]: any } = {};
public _addedScriptHashesCleanInterval: NodeJS.Timeout;

/**
* Every time Cluster#connect() is called, this value will be
Expand Down Expand Up @@ -153,13 +151,6 @@ class Cluster extends Commander {
}
}

clearAddedScriptHashesCleanInterval() {
if (this._addedScriptHashesCleanInterval) {
clearInterval(this._addedScriptHashesCleanInterval);
this._addedScriptHashesCleanInterval = null;
}
}

resetNodesRefreshInterval() {
if (this.slotsTimer) {
return;
Expand Down Expand Up @@ -195,14 +186,6 @@ class Cluster extends Commander {
return;
}

// Make sure only one timer is active at a time
this.clearAddedScriptHashesCleanInterval();

// Start the script cache cleaning
this._addedScriptHashesCleanInterval = setInterval(() => {
this._addedScriptHashes = {};
}, this.options.maxScriptsCachingTime);

const epoch = ++this.connectionEpoch;
this.setStatus("connecting");

Expand Down Expand Up @@ -306,8 +289,6 @@ class Cluster extends Commander {
debug("closed because %s", reason);
}

this.clearAddedScriptHashesCleanInterval();

let retryDelay;
if (
!this.manuallyClosing &&
Expand Down Expand Up @@ -347,8 +328,6 @@ class Cluster extends Commander {
const status = this.status;
this.setStatus("disconnecting");

this.clearAddedScriptHashesCleanInterval();

if (!reconnect) {
this.manuallyClosing = true;
}
Expand Down Expand Up @@ -379,8 +358,6 @@ class Cluster extends Commander {
const status = this.status;
this.setStatus("disconnecting");

this.clearAddedScriptHashesCleanInterval();

this.manuallyClosing = true;

if (this.reconnectTimeout) {
Expand Down
4 changes: 2 additions & 2 deletions lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
convertObjectToArray,
} from "./utils";
import { flatten } from "./utils/lodash";
import { CallbackFunction, ICommand, CommandParameter } from "./types";
import { CallbackFunction, ICommand, CommandParameter, NetStream } from "./types";

interface ICommandOptions {
/**
Expand Down Expand Up @@ -269,7 +269,7 @@ export default class Command implements ICommand {
* @see {@link Redis#sendCommand}
* @public
*/
public toWritable(): string | Buffer {
public toWritable(socket: NetStream): string | Buffer {
let bufferMode = false;
for (const arg of this.args) {
if (arg instanceof Buffer) {
Expand Down
75 changes: 6 additions & 69 deletions lib/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as calculateSlot from "cluster-key-slot";
import * as pMap from "p-map";
import { exists, hasFlag } from "redis-commands";
import asCallback from "standard-as-callback";
import { deprecate } from "util";
Expand Down Expand Up @@ -334,71 +333,9 @@ Pipeline.prototype.exec = function (
}
}

// Check whether scripts exists
const scripts = [];
for (let i = 0; i < this._queue.length; ++i) {
const item = this._queue[i];

if (item.name !== "evalsha") {
continue;
}

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

if (
!script ||
this.redis._addedScriptHashes[script.sha] ||
scripts.includes(script)
) {
continue;
}

scripts.push(script);
}

const _this = this;
if (!scripts.length) {
return execPipeline();
}

// In cluster mode, always load scripts before running the pipeline
if (this.isCluster) {
pMap(scripts, (script) => _this.redis.script("load", script.lua), {
concurrency: 10,
})
.then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
})
.then(execPipeline, this.reject);
return this.promise;
}
execPipeline();

this.redis
.script(
"exists",
scripts.map(({ sha }) => sha)
)
.then(function (results) {
const pending = [];
for (let i = 0; i < results.length; ++i) {
if (!results[i]) {
pending.push(scripts[i]);
}
}
return Promise.all(
pending.map(function (script) {
return _this.redis.script("load", script.lua);
})
);
})
.then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
})
.then(execPipeline, this.reject);
return this.promise;

function execPipeline() {
Expand All @@ -414,7 +351,10 @@ Pipeline.prototype.exec = function (
};
}
let bufferMode = false;

const stream = {
isPipeline: true,
destination: _this.isCluster ? node : { redis: _this.redis },
write: function (writable) {
if (writable instanceof Buffer) {
bufferMode = true;
Expand Down Expand Up @@ -442,11 +382,8 @@ Pipeline.prototype.exec = function (
} else {
sendData = data;
}
if (_this.isCluster) {
node.redis.stream.write(sendData);
} else {
_this.redis.stream.write(sendData);
}

stream.destination.redis.stream.write(sendData);

// Reset writePending for resending
writePending = _this._queue.length;
Expand Down
2 changes: 0 additions & 2 deletions lib/redis/event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ export function closeHandler(self) {
abortTransactionFragments(self.offlineQueue);
}

self.clearAddedScriptHashesCleanInterval();

if (self.manuallyClosing) {
self.manuallyClosing = false;
debug("skip reconnecting since the connection is manually closed.");
Expand Down
77 changes: 49 additions & 28 deletions lib/script.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { createHash } from "crypto";
import Command from "./command";
import asCallback from "standard-as-callback";
import { CallbackFunction } from "./types";

function isPromise(obj: any): boolean {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function"
);
}

import { CallbackFunction, NetStream } from "./types";
export default class Script {
private sha: string;
private Command;

constructor(
private lua: string,
Expand All @@ -21,6 +13,31 @@ export default class Script {
private readOnly: boolean = false
) {
this.sha = createHash("sha1").update(lua).digest("hex");

const sha = this.sha;
const socketHasScriptLoaded = new WeakSet();
this.Command = class CustomScriptCommand extends Command {
public toWritable(socket: NetStream): string | Buffer {
const origReject = this.reject;
this.reject = (err) => {
if (err.toString().indexOf("NOSCRIPT") !== -1) {
socketHasScriptLoaded.delete(socket);
}
this.reject = origReject;
this.reject(err);
};

if (!socketHasScriptLoaded.has(socket)) {
socketHasScriptLoaded.add(socket);
this.name = "eval";
this.args[0] = lua;
} else if (this.name === "eval") {
this.name = "evalsha";
this.args[0] = sha;
}
return super.toWritable(socket);
}
};
}

execute(
Expand All @@ -39,28 +56,32 @@ export default class Script {
options.readOnly = true;
}

const evalsha = new Command("evalsha", [this.sha].concat(args), options);
const evalsha = new this.Command(
"evalsha",
[this.sha].concat(args),
options
);
evalsha.isCustomCommand = true;

const result = container.sendCommand(evalsha);
if (isPromise(result)) {
return asCallback(
result.catch((err: Error) => {
if (err.toString().indexOf("NOSCRIPT") === -1) {
throw err;
}
return container.sendCommand(
new Command("eval", [this.lua].concat(args), options)
);
}),
callback
evalsha.promise = evalsha.promise.catch((err: Error) => {
if (err.toString().indexOf("NOSCRIPT") === -1) {
throw err;
}

// Resend the same custom evalsha command that gets transformed to an eval
// in case it's not loaded yet on the connectionDo an eval as fallback, redis will hash and load it
const resend = new this.Command(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could remove this fallback if order of delivery needs to be guaranteed.
It will still clear the cache for the next command that gets sent to redis.
https://github.com/luin/ioredis/pull/1499/files#diff-3618aaced6e025d978d2cdde354e756687916385e9024868d0ad3fff887c55c6R24

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm thinking to include this breaking change in the next major version #1504 so we can just retry custom commands in pipeline as well.

"evalsha",
[this.sha].concat(args),
options
);
}
resend.isCustomCommand = true;

// result is not a Promise--probably returned from a pipeline chain; however,
// we still need the callback to fire when the script is evaluated
asCallback(evalsha.promise, callback);
const client = container.isPipeline ? container.redis : container;
return client.sendCommand(resend);
});

return result;
asCallback(evalsha.promise, callback);
return container.sendCommand(evalsha);
}
}
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading