Skip to content

Commit

Permalink
fix(cluster): fix not connecting to the unknown nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Feb 7, 2016
1 parent a5b64ad commit 0dcb768
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/cluster/connection_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ConnectionPool.prototype.findOrCreate = function (node, readOnly) {
node.port = node.port || 6379;
node.host = node.host || '127.0.0.1';
node.key = node.key || node.host + ':' + node.port;
readOnly = Boolean(readOnly);

if (this.specifiedOptions[node.key]) {
_.assign(node, this.specifiedOptions[node.key]);
Expand Down
5 changes: 3 additions & 2 deletions lib/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ function Cluster(startupNodes, options) {

var _this = this;
this.connectionPool.on('-node', function (redis) {
_this.emit('-node');
if (_this.subscriber === redis) {
_this.selectSubscriber();
}
_this.emit('-node');
});
this.connectionPool.on('+node', function (redis) {
_this.emit('+node', redis);
Expand All @@ -84,7 +84,6 @@ function Cluster(startupNodes, options) {
});

this.slots = [];

this.retryAttempts = 0;

this.resetOfflineQueue();
Expand Down Expand Up @@ -430,6 +429,8 @@ Cluster.prototype.sendCommand = function (command, stream, node) {
} else {
_this.slots[slot] = [key];
}
var splitKey = key.split(':');
_this.connectionPool.findOrCreate({ host: splitKey[0], port: Number(splitKey[1]) });
tryConnection();
_this.refreshSlotsCache();
},
Expand Down
30 changes: 30 additions & 0 deletions test/functional/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,36 @@ describe('cluster', function () {
});
});

it('should be able to redirect a command to a unknown node', function (done) {
var slotTable = [
[0, 16383, ['127.0.0.1', 30001]]
];
var node1 = new MockServer(30001, function (argv) {
if (argv[0] === 'cluster' && argv[1] === 'slots') {
return slotTable;
}
if (argv[0] === 'get' && argv[1] === 'foo') {
return new Error('MOVED ' + utils.calcSlot('foo') + ' 127.0.0.1:30002');
}
});
var node2 = new MockServer(30002, function (argv) {
if (argv[0] === 'cluster' && argv[1] === 'slots') {
return slotTable;
}
if (argv[0] === 'get' && argv[1] === 'foo') {
return 'bar';
}
});
var cluster = new Redis.Cluster([
{ host: '127.0.0.1', port: '30001' }
], { lazyConnect: false });
cluster.get('foo', function (err, res) {
expect(res).to.eql('bar');
cluster.disconnect();
disconnect([node1, node2], done);
});
});

it('should auto redirect the command within a pipeline', function (done) {
var moved = false;
var times = 0;
Expand Down

0 comments on commit 0dcb768

Please sign in to comment.