pool_cluster.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. 'use strict';
  2. const process = require('process');
  3. const Pool = require('./pool.js');
  4. const PoolConfig = require('./pool_config.js');
  5. const Connection = require('./connection.js');
  6. const EventEmitter = require('events').EventEmitter;
  7. /**
  8. * Selector
  9. */
  10. const makeSelector = {
  11. RR() {
  12. let index = 0;
  13. return (clusterIds) => clusterIds[index++ % clusterIds.length];
  14. },
  15. RANDOM() {
  16. return (clusterIds) =>
  17. clusterIds[Math.floor(Math.random() * clusterIds.length)];
  18. },
  19. ORDER() {
  20. return (clusterIds) => clusterIds[0];
  21. },
  22. };
  23. const getMonotonicMilliseconds = function () {
  24. let ms;
  25. if (typeof process.hrtime === 'function') {
  26. ms = process.hrtime();
  27. ms = ms[0] * 1e3 + ms[1] * 1e-6;
  28. } else {
  29. ms = process.uptime() * 1000;
  30. }
  31. return Math.floor(ms);
  32. };
  33. const patternRegExp = function (pattern) {
  34. if (pattern instanceof RegExp) {
  35. return pattern;
  36. }
  37. const source = pattern
  38. .replace(/([.+?^=!:${}()|[\]/\\])/g, '\\$1')
  39. .replace(/\*/g, '.*');
  40. return new RegExp(`^${source}$`);
  41. };
  42. class PoolNamespace {
  43. constructor(cluster, pattern, selector) {
  44. this._cluster = cluster;
  45. this._pattern = pattern;
  46. this._selector = makeSelector[selector]();
  47. }
  48. getConnection(cb) {
  49. const clusterNode = this._getClusterNode();
  50. if (clusterNode === null) {
  51. let err = new Error('Pool does Not exist.');
  52. err.code = 'POOL_NOEXIST';
  53. if (this._cluster._findNodeIds(this._pattern, true).length !== 0) {
  54. err = new Error('Pool does Not have online node.');
  55. err.code = 'POOL_NONEONLINE';
  56. }
  57. return cb(err);
  58. }
  59. return this._cluster._getConnection(clusterNode, (err, connection) => {
  60. if (err) {
  61. if (
  62. this._cluster._canRetry &&
  63. this._cluster._findNodeIds(this._pattern).length !== 0
  64. ) {
  65. this._cluster.emit('warn', err);
  66. return this.getConnection(cb);
  67. }
  68. return cb(err);
  69. }
  70. return cb(null, connection);
  71. });
  72. }
  73. /**
  74. * pool cluster query
  75. * @param {*} sql
  76. * @param {*} values
  77. * @param {*} cb
  78. * @returns query
  79. */
  80. query(sql, values, cb) {
  81. const query = Connection.createQuery(sql, values, cb, {});
  82. this.getConnection((err, conn) => {
  83. if (err) {
  84. if (typeof query.onResult === 'function') {
  85. query.onResult(err);
  86. } else {
  87. query.emit('error', err);
  88. }
  89. return;
  90. }
  91. try {
  92. conn.query(query).once('end', () => {
  93. conn.release();
  94. });
  95. } catch (e) {
  96. conn.release();
  97. throw e;
  98. }
  99. });
  100. return query;
  101. }
  102. /**
  103. * pool cluster execute
  104. * @param {*} sql
  105. * @param {*} values
  106. * @param {*} cb
  107. */
  108. execute(sql, values, cb) {
  109. if (typeof values === 'function') {
  110. cb = values;
  111. values = [];
  112. }
  113. this.getConnection((err, conn) => {
  114. if (err) {
  115. return cb(err);
  116. }
  117. try {
  118. conn.execute(sql, values, cb).once('end', () => {
  119. conn.release();
  120. });
  121. } catch (e) {
  122. conn.release();
  123. throw e;
  124. }
  125. });
  126. }
  127. _getClusterNode() {
  128. const foundNodeIds = this._cluster._findNodeIds(this._pattern);
  129. if (foundNodeIds.length === 0) {
  130. return null;
  131. }
  132. const nodeId =
  133. foundNodeIds.length === 1
  134. ? foundNodeIds[0]
  135. : this._selector(foundNodeIds);
  136. return this._cluster._getNode(nodeId);
  137. }
  138. }
  139. class PoolCluster extends EventEmitter {
  140. constructor(config) {
  141. super();
  142. config = config || {};
  143. this._canRetry =
  144. typeof config.canRetry === 'undefined' ? true : config.canRetry;
  145. this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
  146. this._restoreNodeTimeout = config.restoreNodeTimeout || 0;
  147. this._defaultSelector = config.defaultSelector || 'RR';
  148. this._closed = false;
  149. this._lastId = 0;
  150. this._nodes = {};
  151. this._serviceableNodeIds = [];
  152. this._namespaces = {};
  153. this._findCaches = {};
  154. }
  155. of(pattern, selector) {
  156. pattern = pattern || '*';
  157. selector = selector || this._defaultSelector;
  158. selector = selector.toUpperCase();
  159. if (!makeSelector[selector] === 'undefined') {
  160. selector = this._defaultSelector;
  161. }
  162. const key = pattern + selector;
  163. if (typeof this._namespaces[key] === 'undefined') {
  164. this._namespaces[key] = new PoolNamespace(this, pattern, selector);
  165. }
  166. return this._namespaces[key];
  167. }
  168. add(id, config) {
  169. if (typeof id === 'object') {
  170. config = id;
  171. id = `CLUSTER::${++this._lastId}`;
  172. }
  173. if (typeof this._nodes[id] === 'undefined') {
  174. this._nodes[id] = {
  175. id: id,
  176. errorCount: 0,
  177. pool: new Pool({ config: new PoolConfig(config) }),
  178. _offlineUntil: 0,
  179. };
  180. this._serviceableNodeIds.push(id);
  181. this._clearFindCaches();
  182. }
  183. }
  184. remove(pattern) {
  185. const foundNodeIds = this._findNodeIds(pattern, true);
  186. for (let i = 0; i < foundNodeIds.length; i++) {
  187. const node = this._getNode(foundNodeIds[i]);
  188. if (node) {
  189. this._removeNode(node);
  190. }
  191. }
  192. }
  193. getConnection(pattern, selector, cb) {
  194. let namespace;
  195. if (typeof pattern === 'function') {
  196. cb = pattern;
  197. namespace = this.of();
  198. } else {
  199. if (typeof selector === 'function') {
  200. cb = selector;
  201. selector = this._defaultSelector;
  202. }
  203. namespace = this.of(pattern, selector);
  204. }
  205. namespace.getConnection(cb);
  206. }
  207. end(callback) {
  208. const cb =
  209. callback !== undefined
  210. ? callback
  211. : (err) => {
  212. if (err) {
  213. throw err;
  214. }
  215. };
  216. if (this._closed) {
  217. process.nextTick(cb);
  218. return;
  219. }
  220. this._closed = true;
  221. let calledBack = false;
  222. let waitingClose = 0;
  223. const onEnd = (err) => {
  224. if (!calledBack && (err || --waitingClose <= 0)) {
  225. calledBack = true;
  226. return cb(err);
  227. }
  228. };
  229. for (const id in this._nodes) {
  230. waitingClose++;
  231. this._nodes[id].pool.end(onEnd);
  232. }
  233. if (waitingClose === 0) {
  234. process.nextTick(onEnd);
  235. }
  236. }
  237. _findNodeIds(pattern, includeOffline) {
  238. let currentTime = 0;
  239. let foundNodeIds = this._findCaches[pattern];
  240. if (foundNodeIds === undefined) {
  241. const expression = patternRegExp(pattern);
  242. foundNodeIds = this._serviceableNodeIds.filter((id) =>
  243. id.match(expression)
  244. );
  245. }
  246. this._findCaches[pattern] = foundNodeIds;
  247. if (includeOffline) {
  248. return foundNodeIds;
  249. }
  250. return foundNodeIds.filter((nodeId) => {
  251. const node = this._getNode(nodeId);
  252. if (!node._offlineUntil) {
  253. return true;
  254. }
  255. if (!currentTime) {
  256. currentTime = getMonotonicMilliseconds();
  257. }
  258. return node._offlineUntil <= currentTime;
  259. });
  260. }
  261. _getNode(id) {
  262. return this._nodes[id] || null;
  263. }
  264. _increaseErrorCount(node) {
  265. const errorCount = ++node.errorCount;
  266. if (this._removeNodeErrorCount > errorCount) {
  267. return;
  268. }
  269. if (this._restoreNodeTimeout > 0) {
  270. node._offlineUntil =
  271. getMonotonicMilliseconds() + this._restoreNodeTimeout;
  272. this.emit('offline', node.id);
  273. return;
  274. }
  275. this._removeNode(node);
  276. this.emit('remove', node.id);
  277. }
  278. _decreaseErrorCount(node) {
  279. let errorCount = node.errorCount;
  280. if (errorCount > this._removeNodeErrorCount) {
  281. errorCount = this._removeNodeErrorCount;
  282. }
  283. if (errorCount < 1) {
  284. errorCount = 1;
  285. }
  286. node.errorCount = errorCount - 1;
  287. if (node._offlineUntil) {
  288. node._offlineUntil = 0;
  289. this.emit('online', node.id);
  290. }
  291. }
  292. _getConnection(node, cb) {
  293. node.pool.getConnection((err, connection) => {
  294. if (err) {
  295. this._increaseErrorCount(node);
  296. return cb(err);
  297. }
  298. this._decreaseErrorCount(node);
  299. connection._clusterId = node.id;
  300. return cb(null, connection);
  301. });
  302. }
  303. _removeNode(node) {
  304. const index = this._serviceableNodeIds.indexOf(node.id);
  305. if (index !== -1) {
  306. this._serviceableNodeIds.splice(index, 1);
  307. delete this._nodes[node.id];
  308. this._clearFindCaches();
  309. node.pool.end();
  310. }
  311. }
  312. _clearFindCaches() {
  313. this._findCaches = {};
  314. }
  315. }
  316. module.exports = PoolCluster;