Skip to content

Commit

Permalink
fix(cluster): ensure node exists before being redirected via an ASK (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
James Allen authored and luin committed Jun 28, 2016
1 parent 68c4ccc commit 5d9d0d3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,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,
Expand Down
32 changes: 32 additions & 0 deletions test/functional/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 5d9d0d3

Please sign in to comment.