pool_cluster.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. class PoolNamespace {
  24. constructor(cluster, pattern, selector) {
  25. this._cluster = cluster;
  26. this._pattern = pattern;
  27. this._selector = makeSelector[selector]();
  28. }
  29. getConnection(cb) {
  30. const clusterNode = this._getClusterNode();
  31. if (clusterNode === null) {
  32. return cb(new Error('Pool does Not exists.'));
  33. }
  34. return this._cluster._getConnection(clusterNode, (err, connection) => {
  35. if (err) {
  36. return cb(err);
  37. }
  38. if (connection === 'retry') {
  39. return this.getConnection(cb);
  40. }
  41. return cb(null, connection);
  42. });
  43. }
  44. /**
  45. * pool cluster query
  46. * @param {*} sql
  47. * @param {*} values
  48. * @param {*} cb
  49. * @returns query
  50. */
  51. query(sql, values, cb) {
  52. const query = Connection.createQuery(sql, values, cb, {});
  53. this.getConnection((err, conn) => {
  54. if (err) {
  55. if (typeof query.onResult === 'function') {
  56. query.onResult(err);
  57. } else {
  58. query.emit('error', err);
  59. }
  60. return;
  61. }
  62. try {
  63. conn.query(query).once('end', () => {
  64. conn.release();
  65. });
  66. } catch (e) {
  67. conn.release();
  68. throw e;
  69. }
  70. });
  71. return query;
  72. }
  73. /**
  74. * pool cluster execute
  75. * @param {*} sql
  76. * @param {*} values
  77. * @param {*} cb
  78. */
  79. execute(sql, values, cb) {
  80. if (typeof values === 'function') {
  81. cb = values;
  82. values = [];
  83. }
  84. this.getConnection((err, conn) => {
  85. if (err) {
  86. return cb(err);
  87. }
  88. try {
  89. conn.execute(sql, values, cb).once('end', () => {
  90. conn.release();
  91. });
  92. } catch (e) {
  93. conn.release();
  94. throw e;
  95. }
  96. });
  97. }
  98. _getClusterNode() {
  99. const foundNodeIds = this._cluster._findNodeIds(this._pattern);
  100. if (foundNodeIds.length === 0) {
  101. return null;
  102. }
  103. const nodeId =
  104. foundNodeIds.length === 1
  105. ? foundNodeIds[0]
  106. : this._selector(foundNodeIds);
  107. return this._cluster._getNode(nodeId);
  108. }
  109. }
  110. class PoolCluster extends EventEmitter {
  111. constructor(config) {
  112. super();
  113. config = config || {};
  114. this._canRetry =
  115. typeof config.canRetry === 'undefined' ? true : config.canRetry;
  116. this._removeNodeErrorCount = config.removeNodeErrorCount || 5;
  117. this._defaultSelector = config.defaultSelector || 'RR';
  118. this._closed = false;
  119. this._lastId = 0;
  120. this._nodes = {};
  121. this._serviceableNodeIds = [];
  122. this._namespaces = {};
  123. this._findCaches = {};
  124. }
  125. of(pattern, selector) {
  126. pattern = pattern || '*';
  127. selector = selector || this._defaultSelector;
  128. selector = selector.toUpperCase();
  129. if (!makeSelector[selector] === 'undefined') {
  130. selector = this._defaultSelector;
  131. }
  132. const key = pattern + selector;
  133. if (typeof this._namespaces[key] === 'undefined') {
  134. this._namespaces[key] = new PoolNamespace(this, pattern, selector);
  135. }
  136. return this._namespaces[key];
  137. }
  138. add(id, config) {
  139. if (typeof id === 'object') {
  140. config = id;
  141. id = `CLUSTER::${++this._lastId}`;
  142. }
  143. if (typeof this._nodes[id] === 'undefined') {
  144. this._nodes[id] = {
  145. id: id,
  146. errorCount: 0,
  147. pool: new Pool({ config: new PoolConfig(config) })
  148. };
  149. this._serviceableNodeIds.push(id);
  150. this._clearFindCaches();
  151. }
  152. }
  153. getConnection(pattern, selector, cb) {
  154. let namespace;
  155. if (typeof pattern === 'function') {
  156. cb = pattern;
  157. namespace = this.of();
  158. } else {
  159. if (typeof selector === 'function') {
  160. cb = selector;
  161. selector = this._defaultSelector;
  162. }
  163. namespace = this.of(pattern, selector);
  164. }
  165. namespace.getConnection(cb);
  166. }
  167. end(callback) {
  168. const cb =
  169. callback !== undefined
  170. ? callback
  171. : err => {
  172. if (err) {
  173. throw err;
  174. }
  175. };
  176. if (this._closed) {
  177. process.nextTick(cb);
  178. return;
  179. }
  180. this._closed = true;
  181. let calledBack = false;
  182. let waitingClose = 0;
  183. const onEnd = err => {
  184. if (!calledBack && (err || --waitingClose <= 0)) {
  185. calledBack = true;
  186. return cb(err);
  187. }
  188. };
  189. for (const id in this._nodes) {
  190. waitingClose++;
  191. this._nodes[id].pool.end(onEnd);
  192. }
  193. if (waitingClose === 0) {
  194. process.nextTick(onEnd);
  195. }
  196. }
  197. _findNodeIds(pattern) {
  198. if (typeof this._findCaches[pattern] !== 'undefined') {
  199. return this._findCaches[pattern];
  200. }
  201. let foundNodeIds;
  202. if (pattern === '*') {
  203. // all
  204. foundNodeIds = this._serviceableNodeIds;
  205. } else if (this._serviceableNodeIds.indexOf(pattern) !== -1) {
  206. // one
  207. foundNodeIds = [pattern];
  208. } else {
  209. // wild matching
  210. const keyword = pattern.substring(pattern.length - 1, 0);
  211. foundNodeIds = this._serviceableNodeIds.filter(id =>
  212. id.startsWith(keyword)
  213. );
  214. }
  215. this._findCaches[pattern] = foundNodeIds;
  216. return foundNodeIds;
  217. }
  218. _getNode(id) {
  219. return this._nodes[id] || null;
  220. }
  221. _increaseErrorCount(node) {
  222. if (++node.errorCount >= this._removeNodeErrorCount) {
  223. const index = this._serviceableNodeIds.indexOf(node.id);
  224. if (index !== -1) {
  225. this._serviceableNodeIds.splice(index, 1);
  226. delete this._nodes[node.id];
  227. this._clearFindCaches();
  228. node.pool.end();
  229. this.emit('remove', node.id);
  230. }
  231. }
  232. }
  233. _decreaseErrorCount(node) {
  234. if (node.errorCount > 0) {
  235. --node.errorCount;
  236. }
  237. }
  238. _getConnection(node, cb) {
  239. node.pool.getConnection((err, connection) => {
  240. if (err) {
  241. this._increaseErrorCount(node);
  242. if (this._canRetry) {
  243. // REVIEW: this seems wrong?
  244. this.emit('warn', err);
  245. // eslint-disable-next-line no-console
  246. console.warn(`[Error] PoolCluster : ${err}`);
  247. return cb(null, 'retry');
  248. }
  249. return cb(err);
  250. }
  251. this._decreaseErrorCount(node);
  252. connection._clusterId = node.id;
  253. return cb(null, connection);
  254. });
  255. }
  256. _clearFindCaches() {
  257. this._findCaches = {};
  258. }
  259. }
  260. module.exports = PoolCluster;