Skip to content

Commit 8dad7a1

Browse files
committed
Add restoreNodeTimeout option to PoolCluster
closes #880 closes #906
1 parent e48e45d commit 8dad7a1

File tree

6 files changed

+226
-44
lines changed

6 files changed

+226
-44
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ you spot any mistakes.
88

99
* Add `poolCluster.remove` to remove pools from the cluster #1006 #1007
1010
* Add optional callback to `poolCluster.end`
11+
* Add `restoreNodeTimeout` option to `PoolCluster` #880 #906
1112
* Fix `poolCluster.add` to throw if `PoolCluster` has been closed
1213
* Fix `poolCluster.add` to throw if `id` already defined
1314
* Fix un-catchable error from `PoolCluster` when MySQL server offline #1033

Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ poolCluster.end(function (err) {
480480
* `canRetry`: If `true`, `PoolCluster` will attempt to reconnect when connection fails. (Default: `true`)
481481
* `removeNodeErrorCount`: If connection fails, node's `errorCount` increases.
482482
When `errorCount` is greater than `removeNodeErrorCount`, remove a node in the `PoolCluster`. (Default: `5`)
483+
* `restoreNodeTimeout`: If connection fails, specifies the number of milliseconds
484+
before another connection attempt will be made. If set to `0`, then node will bd
485+
removed instead and never re-used. (Default: `0`)
483486
* `defaultSelector`: The default selector. (Default: `RR`)
484487
* `RR`: Select one alternately. (Round-Robin)
485488
* `RANDOM`: Select the node by random function.

lib/PoolCluster.js

Lines changed: 91 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function PoolCluster(config) {
1717
this._canRetry = typeof config.canRetry === 'undefined' ? true : config.canRetry;
1818
this._defaultSelector = config.defaultSelector || 'RR';
1919
this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
20+
this._restoreNodeTimeout = config.restoreNodeTimeout || 0;
2021

2122
this._closed = false;
2223
this._findCaches = Object.create(null);
@@ -45,9 +46,10 @@ PoolCluster.prototype.add = function add(id, config) {
4546
: new PoolConfig(id);
4647

4748
this._nodes[nodeId] = {
48-
id: nodeId,
49-
errorCount: 0,
50-
pool: new Pool({config: poolConfig})
49+
id : nodeId,
50+
errorCount : 0,
51+
pool : new Pool({config: poolConfig}),
52+
_offlineUntil : 0
5153
};
5254

5355
this._clearFindCaches();
@@ -114,17 +116,13 @@ PoolCluster.prototype.of = function(pattern, selector) {
114116
};
115117

116118
PoolCluster.prototype.remove = function remove(pattern) {
117-
var foundNodeIds = this._findNodeIds(pattern);
119+
var foundNodeIds = this._findNodeIds(pattern, true);
118120

119121
for (var i = 0; i < foundNodeIds.length; i++) {
120122
var node = this._getNode(foundNodeIds[i]);
121123

122124
if (node) {
123-
delete this._nodes[node.id];
124-
125-
this._clearFindCaches();
126-
127-
node.pool.end(_noop);
125+
this._removeNode(node);
128126
}
129127
}
130128
};
@@ -150,56 +148,86 @@ PoolCluster.prototype._clearFindCaches = function _clearFindCaches() {
150148
this._findCaches = Object.create(null);
151149
};
152150

153-
PoolCluster.prototype._findNodeIds = function(pattern) {
154-
if (this._findCaches[pattern] !== undefined) {
155-
return this._findCaches[pattern];
151+
PoolCluster.prototype._decreaseErrorCount = function _decreaseErrorCount(node) {
152+
var errorCount = node.errorCount;
153+
154+
if (errorCount > this._removeNodeErrorCount) {
155+
errorCount = this._removeNodeErrorCount;
156+
}
157+
158+
if (errorCount < 1) {
159+
errorCount = 1;
156160
}
157161

158-
var foundNodeIds;
159-
var nodeIds = Object.keys(this._nodes);
162+
node.errorCount = errorCount - 1;
160163

161-
if (pattern === '*') {
162-
// all
163-
foundNodeIds = nodeIds;
164-
} else if (nodeIds.indexOf(pattern) != -1) {
165-
// one
166-
foundNodeIds = [pattern];
167-
} else if (pattern[pattern.length - 1] === '*') {
168-
// wild-card matching
169-
var keyword = pattern.substring(pattern.length - 1, 0);
164+
if (node._offlineUntil) {
165+
node._offlineUntil = 0;
166+
this.emit('online', node.id);
167+
}
168+
};
170169

171-
foundNodeIds = nodeIds.filter(function (id) {
172-
return id.indexOf(keyword) === 0;
173-
});
174-
} else {
175-
foundNodeIds = [];
170+
PoolCluster.prototype._findNodeIds = function _findNodeIds(pattern, includeOffline) {
171+
var currentTime = 0;
172+
var foundNodeIds = this._findCaches[pattern];
173+
174+
if (foundNodeIds === undefined) {
175+
var nodeIds = Object.keys(this._nodes);
176+
var wildcard = pattern.substr(-1) === '*';
177+
var keyword = wildcard
178+
? pattern.substr(0, pattern.length - 1)
179+
: pattern;
180+
181+
if (wildcard) {
182+
foundNodeIds = keyword.length !== 0
183+
? nodeIds.filter(function (id) { return id.substr(0, keyword.length) === keyword; })
184+
: nodeIds;
185+
} else {
186+
var index = nodeIds.indexOf(keyword);
187+
foundNodeIds = nodeIds.slice(index, index + 1);
188+
}
189+
190+
this._findCaches[pattern] = foundNodeIds;
191+
}
192+
193+
if (includeOffline) {
194+
return foundNodeIds;
176195
}
177196

178-
this._findCaches[pattern] = foundNodeIds;
197+
return foundNodeIds.filter(function (nodeId) {
198+
var node = this._getNode(nodeId);
199+
200+
if (!node._offlineUntil) {
201+
return true;
202+
}
203+
204+
if (!currentTime) {
205+
currentTime = getMonotonicMilliseconds();
206+
}
179207

180-
return foundNodeIds;
208+
return node._offlineUntil <= currentTime;
209+
}, this);
181210
};
182211

183-
PoolCluster.prototype._getNode = function(id) {
212+
PoolCluster.prototype._getNode = function _getNode(id) {
184213
return this._nodes[id] || null;
185214
};
186215

187216
PoolCluster.prototype._increaseErrorCount = function _increaseErrorCount(node) {
188-
if (++node.errorCount >= this._removeNodeErrorCount) {
189-
delete this._nodes[node.id];
190-
191-
this._clearFindCaches();
217+
var errorCount = ++node.errorCount;
192218

193-
node.pool.end(_noop);
194-
195-
this.emit('remove', node.id);
219+
if (this._removeNodeErrorCount > errorCount) {
220+
return;
196221
}
197-
};
198222

199-
PoolCluster.prototype._decreaseErrorCount = function(node) {
200-
if (node.errorCount > 0) {
201-
--node.errorCount;
223+
if (this._restoreNodeTimeout > 0) {
224+
node._offlineUntil = getMonotonicMilliseconds() + this._restoreNodeTimeout;
225+
this.emit('offline', node.id);
226+
return;
202227
}
228+
229+
this._removeNode(node);
230+
this.emit('remove', node.id);
203231
};
204232

205233
PoolCluster.prototype._getConnection = function(node, cb) {
@@ -220,6 +248,27 @@ PoolCluster.prototype._getConnection = function(node, cb) {
220248
});
221249
};
222250

251+
PoolCluster.prototype._removeNode = function _removeNode(node) {
252+
delete this._nodes[node.id];
253+
254+
this._clearFindCaches();
255+
256+
node.pool.end(_noop);
257+
};
258+
259+
function getMonotonicMilliseconds() {
260+
var ms;
261+
262+
if (typeof process.hrtime === 'function') {
263+
ms = process.hrtime();
264+
ms = ms[0] * 1e3 + ms[1] * 1e-6;
265+
} else {
266+
ms = process.uptime() * 1000;
267+
}
268+
269+
return Math.floor(ms);
270+
}
271+
223272
function _cb(err) {
224273
if (err) {
225274
throw err;

lib/PoolNamespace.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ PoolNamespace.prototype.getConnection = function(cb) {
1717
var namespace = this;
1818

1919
if (clusterNode === null) {
20-
var err = new Error('Pool does not exist.')
21-
err.code = 'POOL_NOEXIST';
20+
var err = null;
21+
22+
if (this._cluster._findNodeIds(this._pattern, true).length !== 0) {
23+
err = new Error('Pool does not have online node.');
24+
err.code = 'POOL_NONEONLINE';
25+
} else {
26+
err = new Error('Pool does not exist.');
27+
err.code = 'POOL_NOEXIST';
28+
}
29+
2230
return cb(err);
2331
}
2432

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster({
4+
canRetry : true,
5+
removeNodeErrorCount : 2,
6+
restoreNodeTimeout : 100
7+
});
8+
var server = common.createFakeServer();
9+
10+
var connCount = 0;
11+
var offline = true;
12+
var offlineEvents = 0;
13+
var onlineEvents = 0;
14+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
15+
cluster.add('MASTER', poolConfig);
16+
17+
server.listen(common.fakeServerPort, function (err) {
18+
assert.ifError(err);
19+
20+
cluster.on('offline', function (id) {
21+
assert.equal(++offlineEvents, 1);
22+
assert.equal(id, 'MASTER');
23+
assert.equal(connCount, 2);
24+
25+
cluster.getConnection('MASTER', function (err) {
26+
assert.ok(err);
27+
assert.equal(err.code, 'POOL_NONEONLINE');
28+
29+
offline = false;
30+
});
31+
32+
setTimeout(function () {
33+
cluster.getConnection('MASTER', function (err, conn) {
34+
assert.ifError(err);
35+
conn.release();
36+
});
37+
}, 200);
38+
});
39+
40+
cluster.on('online', function (id) {
41+
assert.equal(++onlineEvents, 1);
42+
assert.equal(id, 'MASTER');
43+
assert.equal(connCount, 3);
44+
45+
cluster.end(function (err) {
46+
assert.ifError(err);
47+
server.destroy();
48+
});
49+
});
50+
51+
cluster.getConnection('MASTER', function (err) {
52+
assert.ok(err);
53+
assert.equal(err.code, 'PROTOCOL_CONNECTION_LOST');
54+
assert.equal(err.fatal, true);
55+
assert.equal(connCount, 2);
56+
});
57+
});
58+
59+
server.on('connection', function (conn) {
60+
connCount += 1;
61+
62+
if (offline) {
63+
conn.destroy();
64+
} else {
65+
conn.handshake();
66+
}
67+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var assert = require('assert');
2+
var common = require('../../common');
3+
var cluster = common.createPoolCluster({
4+
canRetry : true,
5+
removeNodeErrorCount : 2,
6+
restoreNodeTimeout : 100
7+
});
8+
var server = common.createFakeServer();
9+
10+
var connCount = 0;
11+
var offline = true;
12+
var poolConfig = common.getTestConfig({port: common.fakeServerPort});
13+
cluster.add('MASTER', poolConfig);
14+
15+
server.listen(common.fakeServerPort, function (err) {
16+
assert.ifError(err);
17+
18+
cluster.getConnection('MASTER', function (err) {
19+
assert.ok(err);
20+
assert.equal(err.code, 'PROTOCOL_CONNECTION_LOST');
21+
assert.equal(err.fatal, true);
22+
assert.equal(connCount, 2);
23+
24+
cluster.getConnection('MASTER', function (err) {
25+
assert.ok(err);
26+
assert.equal(err.code, 'POOL_NONEONLINE');
27+
28+
offline = false;
29+
});
30+
31+
setTimeout(function () {
32+
cluster.getConnection('MASTER', function (err, conn) {
33+
assert.ifError(err);
34+
35+
conn.release();
36+
37+
cluster.end(function (err) {
38+
assert.ifError(err);
39+
server.destroy();
40+
});
41+
});
42+
}, 200);
43+
});
44+
});
45+
46+
server.on('connection', function (conn) {
47+
connCount += 1;
48+
49+
if (offline) {
50+
conn.destroy();
51+
} else {
52+
conn.handshake();
53+
}
54+
});

0 commit comments

Comments
 (0)