From 11c5d1d0454984a71aa2116b22d5a1f0f7d8498d Mon Sep 17 00:00:00 2001 From: MalyshevDmitry Date: Sat, 21 Feb 2015 17:47:47 +0300 Subject: [PATCH 1/3] PoolCluster node remove config #1006 --- Readme.md | 18 ++++++++++++++---- lib/PoolCluster.js | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 3deaf64dc..66e1b90e9 100644 --- a/Readme.md +++ b/Readme.md @@ -442,6 +442,20 @@ poolCluster.add('MASTER', masterConfig); poolCluster.add('SLAVE1', slave1Config); poolCluster.add('SLAVE2', slave2Config); +// remove manually +poolCluster.remove('SLAVE2'); // By nodeId +poolCluster.remove('SLAVE*'); // By target group : SLAVE1-2 + +// event on manually removing (for each node) +poolCluster.on('removeManually', function (nodeId) { + console.log('REMOVED MANUALLY NODE: ' + nodeId); // nodeId = SLAVE1 +}); + +// event on auto removing (i.e. when connection was lost) +poolCluster.on('remove', function (nodeId) { + console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1 +}); + // Target Group : ALL(anonymous, MASTER, SLAVE1-2), Selector : round-robin(default) poolCluster.getConnection(function (err, connection) {}); @@ -450,10 +464,6 @@ poolCluster.getConnection('MASTER', function (err, connection) {}); // Target Group : SLAVE1-2, Selector : order // If can't connect to SLAVE1, return SLAVE2. (remove SLAVE1 in the cluster) -poolCluster.on('remove', function (nodeId) { - console.log('REMOVED NODE : ' + nodeId); // nodeId = SLAVE1 -}); - poolCluster.getConnection('SLAVE*', 'ORDER', function (err, connection) {}); // of namespace : of(pattern, selector) diff --git a/lib/PoolCluster.js b/lib/PoolCluster.js index e2af2399b..e410d1244 100644 --- a/lib/PoolCluster.js +++ b/lib/PoolCluster.js @@ -63,6 +63,29 @@ PoolCluster.prototype.add = function(id, config) { } }; +PoolCluster.prototype.remove = function(pattern) { + + var foundNodeIds = this._findNodeIds(pattern); + + for (var i = 0, len = foundNodeIds.length; i < len; i++) { + + var node = this._getNode(foundNodeIds[i]); + + if (node !== null) { + var index = this._serviceableNodeIds.indexOf(node.id); + if (index !== -1) { + this._serviceableNodeIds.splice(index, 1); + delete this._nodes[ node.id ]; + this._clearFindCaches(); + node.pool.end(); + this.emit('removeManually', node.id); + } + } + + } + +}; + PoolCluster.prototype.getConnection = function(pattern, selector, cb) { var namespace; if (typeof pattern === 'function') { From ecb8e175daf38cd60b804524f27d9fdd6e1127db Mon Sep 17 00:00:00 2001 From: MalyshevDmitry Date: Mon, 23 Feb 2015 16:02:37 +0300 Subject: [PATCH 2/3] PoolCluster node remove method unit tests --- .../test-connection-remove-by-name.js | 30 ++++++++++++++ .../test-connection-remove-by-pattern.js | 40 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 test/unit/pool-cluster/test-connection-remove-by-name.js create mode 100644 test/unit/pool-cluster/test-connection-remove-by-pattern.js diff --git a/test/unit/pool-cluster/test-connection-remove-by-name.js b/test/unit/pool-cluster/test-connection-remove-by-name.js new file mode 100644 index 000000000..23c506ea9 --- /dev/null +++ b/test/unit/pool-cluster/test-connection-remove-by-name.js @@ -0,0 +1,30 @@ +var assert = require('assert'); +var common = require('../../common'); +var cluster = common.createPoolCluster({defaultSelector: 'ORDER'}); +var server = common.createFakeServer(); + +var connCount = 0; +var poolConfig = common.getTestConfig({port: common.fakeServerPort}); +cluster.add('SLAVE1', poolConfig); + +server.listen(common.fakeServerPort, function(err) { + assert.ifError(err); + + cluster.getConnection('SLAVE1', function (err, connection) { + assert.ifError(err); + assert.strictEqual(connection._clusterId, 'SLAVE1'); + + cluster.remove('SLAVE1'); + + }); + + cluster.on('removeManually', function(nodeId){ + assert.strictEqual(nodeId, 'SLAVE1'); + cluster.getConnection('SLAVE1', function (err, connection) { + assert.ok(err); + assert.equal(err.message, 'Pool does not exist.'); + server.destroy(); + }); + }); + +}); diff --git a/test/unit/pool-cluster/test-connection-remove-by-pattern.js b/test/unit/pool-cluster/test-connection-remove-by-pattern.js new file mode 100644 index 000000000..f1e2403ea --- /dev/null +++ b/test/unit/pool-cluster/test-connection-remove-by-pattern.js @@ -0,0 +1,40 @@ +var assert = require('assert'); +var common = require('../../common'); +var cluster = common.createPoolCluster({defaultSelector: 'ORDER'}); +var server = common.createFakeServer(); + +var connCount = 0; +var poolConfig = common.getTestConfig({port: common.fakeServerPort}); +cluster.add('SLAVE1', poolConfig); +cluster.add('SLAVE2', poolConfig); + +server.listen(common.fakeServerPort, function(err) { + assert.ifError(err); + + // added nodes + assert.deepEqual(cluster._serviceableNodeIds, ['SLAVE1', 'SLAVE2']); + + // Test SLAVE1 + cluster.getConnection('SLAVE1', function (err, connection) { + assert.strictEqual(connection._clusterId, 'SLAVE1'); + cluster.remove('SLAVE1'); + }); + + // Test SLAVE2 + cluster.getConnection('SLAVE2', function (err, connection) { + assert.strictEqual(connection._clusterId, 'SLAVE2'); + cluster.remove('SLAVE2'); + }); + + cluster.on('removeManually', function(nodeId){ + cluster.getConnection(nodeId, function (err, connection) { + assert.ok(err); + assert.strictEqual(err.message, 'Pool does not exist.'); + }); + }); + +}); + +setTimeout(function serverDestroy(){ + server.destroy(); +}, 2000); From c671c648e5bfa9cccacc6fc1551e84114f464df4 Mon Sep 17 00:00:00 2001 From: MalyshevDmitry Date: Mon, 23 Feb 2015 17:09:34 +0300 Subject: [PATCH 3/3] =?UTF-8?q?PoolCluster=20node=20remove=20method=20unit?= =?UTF-8?q?=20tests=20(attempt=20=E2=84=962)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unit/pool-cluster/test-connection-remove-by-name.js | 5 ++++- test/unit/pool-cluster/test-connection-remove-by-pattern.js | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/unit/pool-cluster/test-connection-remove-by-name.js b/test/unit/pool-cluster/test-connection-remove-by-name.js index 23c506ea9..c15a391ad 100644 --- a/test/unit/pool-cluster/test-connection-remove-by-name.js +++ b/test/unit/pool-cluster/test-connection-remove-by-name.js @@ -23,8 +23,11 @@ server.listen(common.fakeServerPort, function(err) { cluster.getConnection('SLAVE1', function (err, connection) { assert.ok(err); assert.equal(err.message, 'Pool does not exist.'); - server.destroy(); }); }); }); + +setTimeout(function serverDestroy(){ + server.destroy(); +}, 10000); \ No newline at end of file diff --git a/test/unit/pool-cluster/test-connection-remove-by-pattern.js b/test/unit/pool-cluster/test-connection-remove-by-pattern.js index f1e2403ea..5dfb99917 100644 --- a/test/unit/pool-cluster/test-connection-remove-by-pattern.js +++ b/test/unit/pool-cluster/test-connection-remove-by-pattern.js @@ -3,7 +3,7 @@ var common = require('../../common'); var cluster = common.createPoolCluster({defaultSelector: 'ORDER'}); var server = common.createFakeServer(); -var connCount = 0; +var connCount = 2; var poolConfig = common.getTestConfig({port: common.fakeServerPort}); cluster.add('SLAVE1', poolConfig); cluster.add('SLAVE2', poolConfig); @@ -37,4 +37,4 @@ server.listen(common.fakeServerPort, function(err) { setTimeout(function serverDestroy(){ server.destroy(); -}, 2000); +}, 10000);