From 89b62296ba2024d5c5d92730a4ab3ca24aafd244 Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 28 Jun 2016 15:22:49 +0100 Subject: [PATCH] Ensure node exists before being redirected via an ASK --- lib/cluster/index.js | 2 ++ test/functional/cluster.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/cluster/index.js b/lib/cluster/index.js index e1bd887f..3e7e0515 100644 --- a/lib/cluster/index.js +++ b/lib/cluster/index.js @@ -401,6 +401,8 @@ Cluster.prototype.sendCommand = function (command, stream, node) { }, ask: function (slot, key) { debug('command %s is required to ask %s:%s', command.name, key); + var splitKey = key.split(':'); + _this.connectionPool.findOrCreate({ host: splitKey[0], port: Number(splitKey[1]) }); tryConnection(false, key); }, tryagain: partialTry, diff --git a/test/functional/cluster.js b/test/functional/cluster.js index 1cb353ac..a12a9bc0 100644 --- a/test/functional/cluster.js +++ b/test/functional/cluster.js @@ -452,6 +452,38 @@ describe('cluster', function () { cluster.get('foo'); }); }); + + it('should be able to redirect a command to a unknown node', function (done) { + var asked = false; + var slotTable = [ + [0, 16383, ['127.0.0.1', 30002]] + ]; + var node1 = new MockServer(30001, function (argv) { + if (argv[0] === 'get' && argv[1] === 'foo') { + expect(asked).to.eql(true); + return "bar" + } else if (argv[0] === 'asking') { + asked = true; + } + }); + var node2 = new MockServer(30002, function (argv) { + if (argv[0] === 'cluster' && argv[1] === 'slots') { + return slotTable; + } + if (argv[0] === 'get' && argv[1] === 'foo') { + return new Error('ASK ' + calculateSlot('foo') + ' 127.0.0.1:30001'); + } + }); + + var cluster = new Redis.Cluster([ + { host: '127.0.0.1', port: '30002' } + ]); + cluster.get('foo', function (err, res) { + expect(res).to.eql('bar'); + cluster.disconnect(); + disconnect([node1, node2], done); + }); + }); }); describe('TRYAGAIN', function () {