-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
pipeline.ts
449 lines (405 loc) · 11.9 KB
/
pipeline.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import Command from "./command";
import { deprecate } from "util";
import asCallback from "standard-as-callback";
import { exists, hasFlag } from "redis-commands";
import * as calculateSlot from "cluster-key-slot";
import * as pMap from "p-map";
import * as PromiseContainer from "./promiseContainer";
import { CallbackFunction } from "./types";
import Commander from "./commander";
import { noop } from "./utils";
/*
This function derives from the cluster-key-slot implementation.
Instead of checking that all keys have the same slot, it checks that all slots are served by the same set of nodes.
If this is satisfied, it returns the first key's slot.
*/
function generateMultiWithNodes(redis, keys) {
const slot = calculateSlot(keys[0]);
const target = redis._groupsBySlot[slot];
for (let i = 1; i < keys.length; i++) {
if (redis._groupsBySlot[calculateSlot(keys[i])] !== target) {
return -1;
}
}
return slot;
}
export default function Pipeline(redis) {
Commander.call(this);
this.redis = redis;
this.isCluster =
this.redis.constructor.name === "Cluster" || this.redis.isCluster;
this.isPipeline = true;
this.options = redis.options;
this._queue = [];
this._result = [];
this._transactions = 0;
this._shaToScript = {};
Object.keys(redis.scriptsSet).forEach((name) => {
const script = redis.scriptsSet[name];
this._shaToScript[script.sha] = script;
this[name] = redis[name];
this[name + "Buffer"] = redis[name + "Buffer"];
});
redis.addedBuiltinSet.forEach((name) => {
this[name] = redis[name];
this[name + "Buffer"] = redis[name + "Buffer"];
});
const Promise = PromiseContainer.get();
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
const _this = this;
Object.defineProperty(this, "length", {
get: function () {
return _this._queue.length;
},
});
}
Object.assign(Pipeline.prototype, Commander.prototype);
Pipeline.prototype.fillResult = function (value, position) {
if (this._queue[position].name === "exec" && Array.isArray(value[1])) {
const execLength = value[1].length;
for (let i = 0; i < execLength; i++) {
if (value[1][i] instanceof Error) {
continue;
}
const cmd = this._queue[position - (execLength - i)];
try {
value[1][i] = cmd.transformReply(value[1][i]);
} catch (err) {
value[1][i] = err;
}
}
}
this._result[position] = value;
if (--this.replyPending) {
return;
}
if (this.isCluster) {
let retriable = true;
let commonError: { name: string; message: string };
for (let i = 0; i < this._result.length; ++i) {
const error = this._result[i][0];
const command = this._queue[i];
if (error) {
if (
command.name === "exec" &&
error.message ===
"EXECABORT Transaction discarded because of previous errors."
) {
continue;
}
if (!commonError) {
commonError = {
name: error.name,
message: error.message,
};
} else if (
commonError.name !== error.name ||
commonError.message !== error.message
) {
retriable = false;
break;
}
} else if (!command.inTransaction) {
const isReadOnly =
exists(command.name) && hasFlag(command.name, "readonly");
if (!isReadOnly) {
retriable = false;
break;
}
}
}
if (commonError && retriable) {
const _this = this;
const errv = commonError.message.split(" ");
const queue = this._queue;
let inTransaction = false;
this._queue = [];
for (let i = 0; i < queue.length; ++i) {
if (
errv[0] === "ASK" &&
!inTransaction &&
queue[i].name !== "asking" &&
(!queue[i - 1] || queue[i - 1].name !== "asking")
) {
const asking = new Command("asking");
asking.ignore = true;
this.sendCommand(asking);
}
queue[i].initPromise();
this.sendCommand(queue[i]);
inTransaction = queue[i].inTransaction;
}
let matched = true;
if (typeof this.leftRedirections === "undefined") {
this.leftRedirections = {};
}
const exec = function () {
_this.exec();
};
this.redis.handleError(commonError, this.leftRedirections, {
moved: function (slot, key) {
_this.preferKey = key;
_this.redis.slots[errv[1]] = [key];
_this.redis._groupsBySlot[errv[1]] =
_this.redis._groupsIds[_this.redis.slots[errv[1]].join(";")];
_this.redis.refreshSlotsCache();
_this.exec();
},
ask: function (slot, key) {
_this.preferKey = key;
_this.exec();
},
tryagain: exec,
clusterDown: exec,
connectionClosed: exec,
maxRedirections: () => {
matched = false;
},
defaults: () => {
matched = false;
},
});
if (matched) {
return;
}
}
}
let ignoredCount = 0;
for (let i = 0; i < this._queue.length - ignoredCount; ++i) {
if (this._queue[i + ignoredCount].ignore) {
ignoredCount += 1;
}
this._result[i] = this._result[i + ignoredCount];
}
this.resolve(this._result.slice(0, this._result.length - ignoredCount));
};
Pipeline.prototype.sendCommand = function (command) {
if (this._transactions > 0) {
command.inTransaction = true;
}
const position = this._queue.length;
command.pipelineIndex = position;
command.promise
.then((result) => {
this.fillResult([null, result], position);
})
.catch((error) => {
this.fillResult([error], position);
});
this._queue.push(command);
return this;
};
Pipeline.prototype.addBatch = function (commands) {
let command, commandName, args;
for (let i = 0; i < commands.length; ++i) {
command = commands[i];
commandName = command[0];
args = command.slice(1);
this[commandName].apply(this, args);
}
return this;
};
const multi = Pipeline.prototype.multi;
Pipeline.prototype.multi = function () {
this._transactions += 1;
return multi.apply(this, arguments);
};
const execBuffer = Pipeline.prototype.execBuffer;
const exec = Pipeline.prototype.exec;
Pipeline.prototype.execBuffer = deprecate(function () {
if (this._transactions > 0) {
this._transactions -= 1;
}
return execBuffer.apply(this, arguments);
}, "Pipeline#execBuffer: Use Pipeline#exec instead");
// NOTE: To avoid an unhandled promise rejection, this will unconditionally always return this.promise,
// which always has the rejection handled by standard-as-callback
// adding the provided rejection callback.
//
// If a different promise instance were returned, that promise would cause its own unhandled promise rejection
// errors, even if that promise unconditionally resolved to **the resolved value of** this.promise.
Pipeline.prototype.exec = function (
callback: CallbackFunction
): Promise<Array<any>> {
// Wait for the cluster to be connected, since we need nodes information before continuing
if (this.isCluster && !this.redis.slots.length) {
if (this.redis.status === "wait") this.redis.connect().catch(noop);
this.redis.delayUntilReady((err) => {
if (err) {
callback(err);
return;
}
this.exec(callback);
});
return this.promise;
}
if (this._transactions > 0) {
this._transactions -= 1;
return (this.options.dropBufferSupport ? exec : execBuffer).apply(
this,
arguments
);
}
if (!this.nodeifiedPromise) {
this.nodeifiedPromise = true;
asCallback(this.promise, callback);
}
if (!this._queue.length) {
this.resolve([]);
}
let pipelineSlot: number;
if (this.isCluster) {
// List of the first key for each command
const sampleKeys: string[] = [];
for (let i = 0; i < this._queue.length; i++) {
const keys = this._queue[i].getKeys();
if (keys.length) {
sampleKeys.push(keys[0]);
}
// For each command, check that the keys belong to the same slot
if (keys.length && calculateSlot.generateMulti(keys) < 0) {
this.reject(
new Error(
"All the keys in a pipeline command should belong to the same slot"
)
);
return this.promise;
}
}
if (sampleKeys.length) {
pipelineSlot = generateMultiWithNodes(this.redis, sampleKeys);
if (pipelineSlot < 0) {
this.reject(
new Error(
"All keys in the pipeline should belong to the same slots allocation group"
)
);
return this.promise;
}
} else {
// Send the pipeline to a random node
pipelineSlot = (Math.random() * 16384) | 0;
}
}
// 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;
}
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]);
}
}
const Promise = PromiseContainer.get();
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() {
let data = "";
let buffers: Buffer[];
let writePending: number = (_this.replyPending = _this._queue.length);
let node;
if (_this.isCluster) {
node = {
slot: pipelineSlot,
redis: _this.redis.connectionPool.nodes.all[_this.preferKey],
};
}
let bufferMode = false;
const stream = {
write: function (writable) {
if (writable instanceof Buffer) {
bufferMode = true;
}
if (bufferMode) {
if (!buffers) {
buffers = [];
}
if (typeof data === "string") {
buffers.push(Buffer.from(data, "utf8"));
data = undefined;
}
buffers.push(
typeof writable === "string"
? Buffer.from(writable, "utf8")
: writable
);
} else {
data += writable;
}
if (!--writePending) {
let sendData: Buffer | string;
if (buffers) {
sendData = Buffer.concat(buffers);
} else {
sendData = data;
}
if (_this.isCluster) {
node.redis.stream.write(sendData);
} else {
_this.redis.stream.write(sendData);
}
// Reset writePending for resending
writePending = _this._queue.length;
data = "";
buffers = undefined;
bufferMode = false;
}
},
};
for (let i = 0; i < _this._queue.length; ++i) {
_this.redis.sendCommand(_this._queue[i], stream, node);
}
return _this.promise;
}
};