Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mongos.js 46KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. 'use strict';
  2. const inherits = require('util').inherits;
  3. const f = require('util').format;
  4. const EventEmitter = require('events').EventEmitter;
  5. const BasicCursor = require('../cursor');
  6. const Logger = require('../connection/logger');
  7. const retrieveBSON = require('../connection/utils').retrieveBSON;
  8. const MongoError = require('../error').MongoError;
  9. const Server = require('./server');
  10. const clone = require('./shared').clone;
  11. const diff = require('./shared').diff;
  12. const cloneOptions = require('./shared').cloneOptions;
  13. const createClientInfo = require('./shared').createClientInfo;
  14. const SessionMixins = require('./shared').SessionMixins;
  15. const isRetryableWritesSupported = require('./shared').isRetryableWritesSupported;
  16. const relayEvents = require('../utils').relayEvents;
  17. const isRetryableError = require('../error').isRetryableError;
  18. const BSON = retrieveBSON();
  19. /**
  20. * @fileOverview The **Mongos** class is a class that represents a Mongos Proxy topology and is
  21. * used to construct connections.
  22. *
  23. * @example
  24. * var Mongos = require('mongodb-core').Mongos
  25. * , ReadPreference = require('mongodb-core').ReadPreference
  26. * , assert = require('assert');
  27. *
  28. * var server = new Mongos([{host: 'localhost', port: 30000}]);
  29. * // Wait for the connection event
  30. * server.on('connect', function(server) {
  31. * server.destroy();
  32. * });
  33. *
  34. * // Start connecting
  35. * server.connect();
  36. */
  37. const defaultAuthProviders = require('../auth/defaultAuthProviders').defaultAuthProviders;
  38. //
  39. // States
  40. var DISCONNECTED = 'disconnected';
  41. var CONNECTING = 'connecting';
  42. var CONNECTED = 'connected';
  43. var UNREFERENCED = 'unreferenced';
  44. var DESTROYED = 'destroyed';
  45. function stateTransition(self, newState) {
  46. var legalTransitions = {
  47. disconnected: [CONNECTING, DESTROYED, DISCONNECTED],
  48. connecting: [CONNECTING, DESTROYED, CONNECTED, DISCONNECTED],
  49. connected: [CONNECTED, DISCONNECTED, DESTROYED, UNREFERENCED],
  50. unreferenced: [UNREFERENCED, DESTROYED],
  51. destroyed: [DESTROYED]
  52. };
  53. // Get current state
  54. var legalStates = legalTransitions[self.state];
  55. if (legalStates && legalStates.indexOf(newState) !== -1) {
  56. self.state = newState;
  57. } else {
  58. self.logger.error(
  59. f(
  60. 'Pool with id [%s] failed attempted illegal state transition from [%s] to [%s] only following state allowed [%s]',
  61. self.id,
  62. self.state,
  63. newState,
  64. legalStates
  65. )
  66. );
  67. }
  68. }
  69. //
  70. // ReplSet instance id
  71. var id = 1;
  72. var handlers = ['connect', 'close', 'error', 'timeout', 'parseError'];
  73. /**
  74. * Creates a new Mongos instance
  75. * @class
  76. * @param {array} seedlist A list of seeds for the replicaset
  77. * @param {number} [options.haInterval=5000] The High availability period for replicaset inquiry
  78. * @param {Cursor} [options.cursorFactory=Cursor] The cursor factory class used for all query cursors
  79. * @param {number} [options.size=5] Server connection pool size
  80. * @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
  81. * @param {number} [options.keepAliveInitialDelay=0] Initial delay before TCP keep alive enabled
  82. * @param {number} [options.localThresholdMS=15] Cutoff latency point in MS for MongoS proxy selection
  83. * @param {boolean} [options.noDelay=true] TCP Connection no delay
  84. * @param {number} [options.connectionTimeout=1000] TCP Connection timeout setting
  85. * @param {number} [options.socketTimeout=0] TCP Socket timeout setting
  86. * @param {boolean} [options.singleBufferSerializtion=true] Serialize into single buffer, trade of peak memory for serialization speed
  87. * @param {boolean} [options.ssl=false] Use SSL for connection
  88. * @param {boolean|function} [options.checkServerIdentity=true] Ensure we check server identify during SSL, set to false to disable checking. Only works for Node 0.12.x or higher. You can pass in a boolean or your own checkServerIdentity override function.
  89. * @param {Buffer} [options.ca] SSL Certificate store binary buffer
  90. * @param {Buffer} [options.crl] SSL Certificate revocation store binary buffer
  91. * @param {Buffer} [options.cert] SSL Certificate binary buffer
  92. * @param {Buffer} [options.key] SSL Key file binary buffer
  93. * @param {string} [options.passphrase] SSL Certificate pass phrase
  94. * @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
  95. * @param {boolean} [options.rejectUnauthorized=true] Reject unauthorized server certificates
  96. * @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
  97. * @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
  98. * @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
  99. * @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
  100. * @param {boolean} [options.monitorCommands=false] Enable command monitoring for this topology
  101. * @return {Mongos} A cursor instance
  102. * @fires Mongos#connect
  103. * @fires Mongos#reconnect
  104. * @fires Mongos#joined
  105. * @fires Mongos#left
  106. * @fires Mongos#failed
  107. * @fires Mongos#fullsetup
  108. * @fires Mongos#all
  109. * @fires Mongos#serverHeartbeatStarted
  110. * @fires Mongos#serverHeartbeatSucceeded
  111. * @fires Mongos#serverHeartbeatFailed
  112. * @fires Mongos#topologyOpening
  113. * @fires Mongos#topologyClosed
  114. * @fires Mongos#topologyDescriptionChanged
  115. * @property {string} type the topology type.
  116. * @property {string} parserType the parser type used (c++ or js).
  117. */
  118. var Mongos = function(seedlist, options) {
  119. options = options || {};
  120. // Get replSet Id
  121. this.id = id++;
  122. // Internal state
  123. this.s = {
  124. options: Object.assign({}, options),
  125. // BSON instance
  126. bson:
  127. options.bson ||
  128. new BSON([
  129. BSON.Binary,
  130. BSON.Code,
  131. BSON.DBRef,
  132. BSON.Decimal128,
  133. BSON.Double,
  134. BSON.Int32,
  135. BSON.Long,
  136. BSON.Map,
  137. BSON.MaxKey,
  138. BSON.MinKey,
  139. BSON.ObjectId,
  140. BSON.BSONRegExp,
  141. BSON.Symbol,
  142. BSON.Timestamp
  143. ]),
  144. // Factory overrides
  145. Cursor: options.cursorFactory || BasicCursor,
  146. // Logger instance
  147. logger: Logger('Mongos', options),
  148. // Seedlist
  149. seedlist: seedlist,
  150. // Ha interval
  151. haInterval: options.haInterval ? options.haInterval : 10000,
  152. // Disconnect handler
  153. disconnectHandler: options.disconnectHandler,
  154. // Server selection index
  155. index: 0,
  156. // Connect function options passed in
  157. connectOptions: {},
  158. // Are we running in debug mode
  159. debug: typeof options.debug === 'boolean' ? options.debug : false,
  160. // localThresholdMS
  161. localThresholdMS: options.localThresholdMS || 15,
  162. // Client info
  163. clientInfo: createClientInfo(options),
  164. // Authentication context
  165. authenticationContexts: []
  166. };
  167. // Set the client info
  168. this.s.options.clientInfo = createClientInfo(options);
  169. // Log info warning if the socketTimeout < haInterval as it will cause
  170. // a lot of recycled connections to happen.
  171. if (
  172. this.s.logger.isWarn() &&
  173. this.s.options.socketTimeout !== 0 &&
  174. this.s.options.socketTimeout < this.s.haInterval
  175. ) {
  176. this.s.logger.warn(
  177. f(
  178. 'warning socketTimeout %s is less than haInterval %s. This might cause unnecessary server reconnections due to socket timeouts',
  179. this.s.options.socketTimeout,
  180. this.s.haInterval
  181. )
  182. );
  183. }
  184. // All the authProviders
  185. this.authProviders = options.authProviders || defaultAuthProviders(this.s.bson);
  186. // Disconnected state
  187. this.state = DISCONNECTED;
  188. // Current proxies we are connecting to
  189. this.connectingProxies = [];
  190. // Currently connected proxies
  191. this.connectedProxies = [];
  192. // Disconnected proxies
  193. this.disconnectedProxies = [];
  194. // Are we authenticating
  195. this.authenticating = false;
  196. // Index of proxy to run operations against
  197. this.index = 0;
  198. // High availability timeout id
  199. this.haTimeoutId = null;
  200. // Last ismaster
  201. this.ismaster = null;
  202. // Description of the Replicaset
  203. this.topologyDescription = {
  204. topologyType: 'Unknown',
  205. servers: []
  206. };
  207. // Highest clusterTime seen in responses from the current deployment
  208. this.clusterTime = null;
  209. // Add event listener
  210. EventEmitter.call(this);
  211. };
  212. inherits(Mongos, EventEmitter);
  213. Object.assign(Mongos.prototype, SessionMixins);
  214. Object.defineProperty(Mongos.prototype, 'type', {
  215. enumerable: true,
  216. get: function() {
  217. return 'mongos';
  218. }
  219. });
  220. Object.defineProperty(Mongos.prototype, 'parserType', {
  221. enumerable: true,
  222. get: function() {
  223. return BSON.native ? 'c++' : 'js';
  224. }
  225. });
  226. Object.defineProperty(Mongos.prototype, 'logicalSessionTimeoutMinutes', {
  227. enumerable: true,
  228. get: function() {
  229. if (!this.ismaster) return null;
  230. return this.ismaster.logicalSessionTimeoutMinutes || null;
  231. }
  232. });
  233. /**
  234. * Emit event if it exists
  235. * @method
  236. */
  237. function emitSDAMEvent(self, event, description) {
  238. if (self.listeners(event).length > 0) {
  239. self.emit(event, description);
  240. }
  241. }
  242. const SERVER_EVENTS = ['serverDescriptionChanged', 'error', 'close', 'timeout', 'parseError'];
  243. function destroyServer(server, options) {
  244. options = options || {};
  245. SERVER_EVENTS.forEach(event => server.removeAllListeners(event));
  246. server.destroy(options);
  247. }
  248. /**
  249. * Initiate server connect
  250. * @method
  251. * @param {array} [options.auth=null] Array of auth options to apply on connect
  252. */
  253. Mongos.prototype.connect = function(options) {
  254. var self = this;
  255. // Add any connect level options to the internal state
  256. this.s.connectOptions = options || {};
  257. // Set connecting state
  258. stateTransition(this, CONNECTING);
  259. // Create server instances
  260. var servers = this.s.seedlist.map(function(x) {
  261. const server = new Server(
  262. Object.assign({}, self.s.options, x, options, {
  263. authProviders: self.authProviders,
  264. reconnect: false,
  265. monitoring: false,
  266. parent: self,
  267. clientInfo: clone(self.s.clientInfo)
  268. })
  269. );
  270. relayEvents(server, self, ['serverDescriptionChanged']);
  271. return server;
  272. });
  273. // Emit the topology opening event
  274. emitSDAMEvent(this, 'topologyOpening', { topologyId: this.id });
  275. // Start all server connections
  276. connectProxies(self, servers);
  277. };
  278. function handleEvent(self) {
  279. return function() {
  280. if (self.state === DESTROYED) return;
  281. // Move to list of disconnectedProxies
  282. moveServerFrom(self.connectedProxies, self.disconnectedProxies, this);
  283. // Emit the initial topology
  284. emitTopologyDescriptionChanged(self);
  285. // Emit the left signal
  286. self.emit('left', 'mongos', this);
  287. // Emit the sdam event
  288. self.emit('serverClosed', {
  289. topologyId: self.id,
  290. address: this.name
  291. });
  292. };
  293. }
  294. function handleInitialConnectEvent(self, event) {
  295. return function() {
  296. var _this = this;
  297. // Destroy the instance
  298. if (self.state === DESTROYED) {
  299. // Emit the initial topology
  300. emitTopologyDescriptionChanged(self);
  301. // Move from connectingProxies
  302. moveServerFrom(self.connectingProxies, self.disconnectedProxies, this);
  303. return this.destroy();
  304. }
  305. // Check the type of server
  306. if (event === 'connect') {
  307. // Do we have authentication contexts that need to be applied
  308. applyAuthenticationContexts(self, _this, function() {
  309. // Get last known ismaster
  310. self.ismaster = _this.lastIsMaster();
  311. // Is this not a proxy, remove t
  312. if (self.ismaster.msg === 'isdbgrid') {
  313. // Add to the connectd list
  314. for (var i = 0; i < self.connectedProxies.length; i++) {
  315. if (self.connectedProxies[i].name === _this.name) {
  316. // Move from connectingProxies
  317. moveServerFrom(self.connectingProxies, self.disconnectedProxies, _this);
  318. // Emit the initial topology
  319. emitTopologyDescriptionChanged(self);
  320. _this.destroy();
  321. return self.emit('failed', _this);
  322. }
  323. }
  324. // Remove the handlers
  325. for (i = 0; i < handlers.length; i++) {
  326. _this.removeAllListeners(handlers[i]);
  327. }
  328. // Add stable state handlers
  329. _this.on('error', handleEvent(self, 'error'));
  330. _this.on('close', handleEvent(self, 'close'));
  331. _this.on('timeout', handleEvent(self, 'timeout'));
  332. _this.on('parseError', handleEvent(self, 'parseError'));
  333. // Move from connecting proxies connected
  334. moveServerFrom(self.connectingProxies, self.connectedProxies, _this);
  335. // Emit the joined event
  336. self.emit('joined', 'mongos', _this);
  337. } else {
  338. // Print warning if we did not find a mongos proxy
  339. if (self.s.logger.isWarn()) {
  340. var message = 'expected mongos proxy, but found replicaset member mongod for server %s';
  341. // We have a standalone server
  342. if (!self.ismaster.hosts) {
  343. message = 'expected mongos proxy, but found standalone mongod for server %s';
  344. }
  345. self.s.logger.warn(f(message, _this.name));
  346. }
  347. // This is not a mongos proxy, remove it completely
  348. removeProxyFrom(self.connectingProxies, _this);
  349. // Emit the left event
  350. self.emit('left', 'server', _this);
  351. // Emit failed event
  352. self.emit('failed', _this);
  353. }
  354. });
  355. } else {
  356. moveServerFrom(self.connectingProxies, self.disconnectedProxies, this);
  357. // Emit the left event
  358. self.emit('left', 'mongos', this);
  359. // Emit failed event
  360. self.emit('failed', this);
  361. }
  362. // Emit the initial topology
  363. emitTopologyDescriptionChanged(self);
  364. // Trigger topologyMonitor
  365. if (self.connectingProxies.length === 0) {
  366. // Emit connected if we are connected
  367. if (self.connectedProxies.length > 0 && self.state === CONNECTING) {
  368. // Set the state to connected
  369. stateTransition(self, CONNECTED);
  370. // Emit the connect event
  371. self.emit('connect', self);
  372. self.emit('fullsetup', self);
  373. self.emit('all', self);
  374. } else if (self.disconnectedProxies.length === 0) {
  375. // Print warning if we did not find a mongos proxy
  376. if (self.s.logger.isWarn()) {
  377. self.s.logger.warn(
  378. f('no mongos proxies found in seed list, did you mean to connect to a replicaset')
  379. );
  380. }
  381. // Emit the error that no proxies were found
  382. return self.emit('error', new MongoError('no mongos proxies found in seed list'));
  383. }
  384. // Topology monitor
  385. topologyMonitor(self, { firstConnect: true });
  386. }
  387. };
  388. }
  389. function connectProxies(self, servers) {
  390. // Update connectingProxies
  391. self.connectingProxies = self.connectingProxies.concat(servers);
  392. // Index used to interleaf the server connects, avoiding
  393. // runtime issues on io constrained vm's
  394. var timeoutInterval = 0;
  395. function connect(server, timeoutInterval) {
  396. setTimeout(function() {
  397. // Emit opening server event
  398. self.emit('serverOpening', {
  399. topologyId: self.id,
  400. address: server.name
  401. });
  402. // Emit the initial topology
  403. emitTopologyDescriptionChanged(self);
  404. // Add event handlers
  405. server.once('close', handleInitialConnectEvent(self, 'close'));
  406. server.once('timeout', handleInitialConnectEvent(self, 'timeout'));
  407. server.once('parseError', handleInitialConnectEvent(self, 'parseError'));
  408. server.once('error', handleInitialConnectEvent(self, 'error'));
  409. server.once('connect', handleInitialConnectEvent(self, 'connect'));
  410. // Command Monitoring events
  411. relayEvents(server, self, ['commandStarted', 'commandSucceeded', 'commandFailed']);
  412. // Start connection
  413. server.connect(self.s.connectOptions);
  414. }, timeoutInterval);
  415. }
  416. // Start all the servers
  417. while (servers.length > 0) {
  418. connect(servers.shift(), timeoutInterval++);
  419. }
  420. }
  421. function pickProxy(self) {
  422. // Get the currently connected Proxies
  423. var connectedProxies = self.connectedProxies.slice(0);
  424. // Set lower bound
  425. var lowerBoundLatency = Number.MAX_VALUE;
  426. // Determine the lower bound for the Proxies
  427. for (var i = 0; i < connectedProxies.length; i++) {
  428. if (connectedProxies[i].lastIsMasterMS < lowerBoundLatency) {
  429. lowerBoundLatency = connectedProxies[i].lastIsMasterMS;
  430. }
  431. }
  432. // Filter out the possible servers
  433. connectedProxies = connectedProxies.filter(function(server) {
  434. if (
  435. server.lastIsMasterMS <= lowerBoundLatency + self.s.localThresholdMS &&
  436. server.isConnected()
  437. ) {
  438. return true;
  439. }
  440. });
  441. // We have no connectedProxies pick first of the connected ones
  442. if (connectedProxies.length === 0) {
  443. return self.connectedProxies[0];
  444. }
  445. // Get proxy
  446. var proxy = connectedProxies[self.index % connectedProxies.length];
  447. // Update the index
  448. self.index = (self.index + 1) % connectedProxies.length;
  449. // Return the proxy
  450. return proxy;
  451. }
  452. function moveServerFrom(from, to, proxy) {
  453. for (var i = 0; i < from.length; i++) {
  454. if (from[i].name === proxy.name) {
  455. from.splice(i, 1);
  456. }
  457. }
  458. for (i = 0; i < to.length; i++) {
  459. if (to[i].name === proxy.name) {
  460. to.splice(i, 1);
  461. }
  462. }
  463. to.push(proxy);
  464. }
  465. function removeProxyFrom(from, proxy) {
  466. for (var i = 0; i < from.length; i++) {
  467. if (from[i].name === proxy.name) {
  468. from.splice(i, 1);
  469. }
  470. }
  471. }
  472. function reconnectProxies(self, proxies, callback) {
  473. // Count lefts
  474. var count = proxies.length;
  475. // Handle events
  476. var _handleEvent = function(self, event) {
  477. return function() {
  478. var _self = this;
  479. count = count - 1;
  480. // Destroyed
  481. if (self.state === DESTROYED || self.state === UNREFERENCED) {
  482. moveServerFrom(self.connectingProxies, self.disconnectedProxies, _self);
  483. return this.destroy();
  484. }
  485. if (event === 'connect' && !self.authenticating) {
  486. // Do we have authentication contexts that need to be applied
  487. applyAuthenticationContexts(self, _self, function() {
  488. // Destroyed
  489. if (self.state === DESTROYED || self.state === UNREFERENCED) {
  490. moveServerFrom(self.connectingProxies, self.disconnectedProxies, _self);
  491. return _self.destroy();
  492. }
  493. // Remove the handlers
  494. for (var i = 0; i < handlers.length; i++) {
  495. _self.removeAllListeners(handlers[i]);
  496. }
  497. // Add stable state handlers
  498. _self.on('error', handleEvent(self, 'error'));
  499. _self.on('close', handleEvent(self, 'close'));
  500. _self.on('timeout', handleEvent(self, 'timeout'));
  501. _self.on('parseError', handleEvent(self, 'parseError'));
  502. // Move to the connected servers
  503. moveServerFrom(self.connectingProxies, self.connectedProxies, _self);
  504. // Emit topology Change
  505. emitTopologyDescriptionChanged(self);
  506. // Emit joined event
  507. self.emit('joined', 'mongos', _self);
  508. });
  509. } else {
  510. // Move from connectingProxies
  511. moveServerFrom(self.connectingProxies, self.disconnectedProxies, _self);
  512. this.destroy();
  513. }
  514. // Are we done finish up callback
  515. if (count === 0) {
  516. callback();
  517. }
  518. };
  519. };
  520. // No new servers
  521. if (count === 0) {
  522. return callback();
  523. }
  524. // Execute method
  525. function execute(_server, i) {
  526. setTimeout(function() {
  527. // Destroyed
  528. if (self.state === DESTROYED || self.state === UNREFERENCED) {
  529. return;
  530. }
  531. // Create a new server instance
  532. var server = new Server(
  533. Object.assign({}, self.s.options, {
  534. host: _server.name.split(':')[0],
  535. port: parseInt(_server.name.split(':')[1], 10),
  536. authProviders: self.authProviders,
  537. reconnect: false,
  538. monitoring: false,
  539. parent: self,
  540. clientInfo: clone(self.s.clientInfo)
  541. })
  542. );
  543. destroyServer(_server);
  544. removeProxyFrom(self.disconnectedProxies, _server);
  545. // Relay the server description change
  546. relayEvents(server, self, ['serverDescriptionChanged']);
  547. // Emit opening server event
  548. self.emit('serverOpening', {
  549. topologyId: server.s.topologyId !== -1 ? server.s.topologyId : self.id,
  550. address: server.name
  551. });
  552. // Add temp handlers
  553. server.once('connect', _handleEvent(self, 'connect'));
  554. server.once('close', _handleEvent(self, 'close'));
  555. server.once('timeout', _handleEvent(self, 'timeout'));
  556. server.once('error', _handleEvent(self, 'error'));
  557. server.once('parseError', _handleEvent(self, 'parseError'));
  558. // Command Monitoring events
  559. relayEvents(server, self, ['commandStarted', 'commandSucceeded', 'commandFailed']);
  560. // Connect to proxy
  561. self.connectingProxies.push(server);
  562. server.connect(self.s.connectOptions);
  563. }, i);
  564. }
  565. // Create new instances
  566. for (var i = 0; i < proxies.length; i++) {
  567. execute(proxies[i], i);
  568. }
  569. }
  570. function applyAuthenticationContexts(self, server, callback) {
  571. if (self.s.authenticationContexts.length === 0) {
  572. return callback();
  573. }
  574. // Copy contexts to ensure no modificiation in the middle of
  575. // auth process.
  576. var authContexts = self.s.authenticationContexts.slice(0);
  577. // Apply one of the contexts
  578. function applyAuth(authContexts, server, callback) {
  579. if (authContexts.length === 0) return callback();
  580. // Get the first auth context
  581. var authContext = authContexts.shift();
  582. // Copy the params
  583. var customAuthContext = authContext.slice(0);
  584. // Push our callback handler
  585. customAuthContext.push(function(/* err */) {
  586. applyAuth(authContexts, server, callback);
  587. });
  588. // Attempt authentication
  589. server.auth.apply(server, customAuthContext);
  590. }
  591. // Apply all auth contexts
  592. applyAuth(authContexts, server, callback);
  593. }
  594. function topologyMonitor(self, options) {
  595. options = options || {};
  596. // Set momitoring timeout
  597. self.haTimeoutId = setTimeout(function() {
  598. if (self.state === DESTROYED || self.state === UNREFERENCED) return;
  599. // If we have a primary and a disconnect handler, execute
  600. // buffered operations
  601. if (self.isConnected() && self.s.disconnectHandler) {
  602. self.s.disconnectHandler.execute();
  603. }
  604. // Get the connectingServers
  605. var proxies = self.connectedProxies.slice(0);
  606. // Get the count
  607. var count = proxies.length;
  608. // If the count is zero schedule a new fast
  609. function pingServer(_self, _server, cb) {
  610. // Measure running time
  611. var start = new Date().getTime();
  612. // Emit the server heartbeat start
  613. emitSDAMEvent(self, 'serverHeartbeatStarted', { connectionId: _server.name });
  614. // Execute ismaster
  615. _server.command(
  616. 'admin.$cmd',
  617. {
  618. ismaster: true
  619. },
  620. {
  621. monitoring: true,
  622. socketTimeout: self.s.options.connectionTimeout || 2000
  623. },
  624. function(err, r) {
  625. if (self.state === DESTROYED || self.state === UNREFERENCED) {
  626. // Move from connectingProxies
  627. moveServerFrom(self.connectedProxies, self.disconnectedProxies, _server);
  628. _server.destroy();
  629. return cb(err, r);
  630. }
  631. // Calculate latency
  632. var latencyMS = new Date().getTime() - start;
  633. // We had an error, remove it from the state
  634. if (err) {
  635. // Emit the server heartbeat failure
  636. emitSDAMEvent(self, 'serverHeartbeatFailed', {
  637. durationMS: latencyMS,
  638. failure: err,
  639. connectionId: _server.name
  640. });
  641. // Move from connected proxies to disconnected proxies
  642. moveServerFrom(self.connectedProxies, self.disconnectedProxies, _server);
  643. } else {
  644. // Update the server ismaster
  645. _server.ismaster = r.result;
  646. _server.lastIsMasterMS = latencyMS;
  647. // Server heart beat event
  648. emitSDAMEvent(self, 'serverHeartbeatSucceeded', {
  649. durationMS: latencyMS,
  650. reply: r.result,
  651. connectionId: _server.name
  652. });
  653. }
  654. cb(err, r);
  655. }
  656. );
  657. }
  658. // No proxies initiate monitor again
  659. if (proxies.length === 0) {
  660. // Emit close event if any listeners registered
  661. if (self.listeners('close').length > 0 && self.state === CONNECTING) {
  662. self.emit('error', new MongoError('no mongos proxy available'));
  663. } else {
  664. self.emit('close', self);
  665. }
  666. // Attempt to connect to any unknown servers
  667. return reconnectProxies(self, self.disconnectedProxies, function() {
  668. if (self.state === DESTROYED || self.state === UNREFERENCED) return;
  669. // Are we connected ? emit connect event
  670. if (self.state === CONNECTING && options.firstConnect) {
  671. self.emit('connect', self);
  672. self.emit('fullsetup', self);
  673. self.emit('all', self);
  674. } else if (self.isConnected()) {
  675. self.emit('reconnect', self);
  676. } else if (!self.isConnected() && self.listeners('close').length > 0) {
  677. self.emit('close', self);
  678. }
  679. // Perform topology monitor
  680. topologyMonitor(self);
  681. });
  682. }
  683. // Ping all servers
  684. for (var i = 0; i < proxies.length; i++) {
  685. pingServer(self, proxies[i], function() {
  686. count = count - 1;
  687. if (count === 0) {
  688. if (self.state === DESTROYED || self.state === UNREFERENCED) return;
  689. // Attempt to connect to any unknown servers
  690. reconnectProxies(self, self.disconnectedProxies, function() {
  691. if (self.state === DESTROYED || self.state === UNREFERENCED) return;
  692. // Perform topology monitor
  693. topologyMonitor(self);
  694. });
  695. }
  696. });
  697. }
  698. }, self.s.haInterval);
  699. }
  700. /**
  701. * Returns the last known ismaster document for this server
  702. * @method
  703. * @return {object}
  704. */
  705. Mongos.prototype.lastIsMaster = function() {
  706. return this.ismaster;
  707. };
  708. /**
  709. * Unref all connections belong to this server
  710. * @method
  711. */
  712. Mongos.prototype.unref = function() {
  713. // Transition state
  714. stateTransition(this, UNREFERENCED);
  715. // Get all proxies
  716. var proxies = this.connectedProxies.concat(this.connectingProxies);
  717. proxies.forEach(function(x) {
  718. x.unref();
  719. });
  720. clearTimeout(this.haTimeoutId);
  721. };
  722. /**
  723. * Destroy the server connection
  724. * @param {boolean} [options.force=false] Force destroy the pool
  725. * @method
  726. */
  727. Mongos.prototype.destroy = function(options) {
  728. var self = this;
  729. // Transition state
  730. stateTransition(this, DESTROYED);
  731. // Get all proxies
  732. var proxies = this.connectedProxies.concat(this.connectingProxies);
  733. // Clear out any monitoring process
  734. if (this.haTimeoutId) clearTimeout(this.haTimeoutId);
  735. // Clear out authentication contexts
  736. this.s.authenticationContexts = [];
  737. // Destroy all connecting servers
  738. proxies.forEach(function(server) {
  739. // Emit the sdam event
  740. self.emit('serverClosed', {
  741. topologyId: self.id,
  742. address: server.name
  743. });
  744. destroyServer(server, options);
  745. // Move to list of disconnectedProxies
  746. moveServerFrom(self.connectedProxies, self.disconnectedProxies, server);
  747. });
  748. // Emit the final topology change
  749. emitTopologyDescriptionChanged(self);
  750. // Emit toplogy closing event
  751. emitSDAMEvent(this, 'topologyClosed', { topologyId: this.id });
  752. };
  753. /**
  754. * Figure out if the server is connected
  755. * @method
  756. * @return {boolean}
  757. */
  758. Mongos.prototype.isConnected = function() {
  759. return this.connectedProxies.length > 0;
  760. };
  761. /**
  762. * Figure out if the server instance was destroyed by calling destroy
  763. * @method
  764. * @return {boolean}
  765. */
  766. Mongos.prototype.isDestroyed = function() {
  767. return this.state === DESTROYED;
  768. };
  769. //
  770. // Operations
  771. //
  772. // Execute write operation
  773. var executeWriteOperation = function(self, op, ns, ops, options, callback) {
  774. if (typeof options === 'function') (callback = options), (options = {});
  775. options = options || {};
  776. // Pick a server
  777. let server = pickProxy(self);
  778. // No server found error out
  779. if (!server) return callback(new MongoError('no mongos proxy available'));
  780. if (!options.retryWrites || !options.session || !isRetryableWritesSupported(self)) {
  781. // Execute the command
  782. return server[op](ns, ops, options, callback);
  783. }
  784. // increment and assign txnNumber
  785. options.willRetryWrite = true;
  786. options.session.incrementTransactionNumber();
  787. server[op](ns, ops, options, (err, result) => {
  788. if (!err) return callback(null, result);
  789. if (!isRetryableError(err)) {
  790. return callback(err);
  791. }
  792. // Pick another server
  793. server = pickProxy(self);
  794. // No server found error out with original error
  795. if (!server || !isRetryableWritesSupported(server)) {
  796. return callback(err);
  797. }
  798. // rerun the operation
  799. server[op](ns, ops, options, callback);
  800. });
  801. };
  802. /**
  803. * Insert one or more documents
  804. * @method
  805. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  806. * @param {array} ops An array of documents to insert
  807. * @param {boolean} [options.ordered=true] Execute in order or out of order
  808. * @param {object} [options.writeConcern={}] Write concern for the operation
  809. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  810. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  811. * @param {ClientSession} [options.session=null] Session to use for the operation
  812. * @param {boolean} [options.retryWrites] Enable retryable writes for this operation
  813. * @param {opResultCallback} callback A callback function
  814. */
  815. Mongos.prototype.insert = function(ns, ops, options, callback) {
  816. if (typeof options === 'function') {
  817. (callback = options), (options = {}), (options = options || {});
  818. }
  819. if (this.state === DESTROYED) return callback(new MongoError(f('topology was destroyed')));
  820. // Not connected but we have a disconnecthandler
  821. if (!this.isConnected() && this.s.disconnectHandler != null) {
  822. return this.s.disconnectHandler.add('insert', ns, ops, options, callback);
  823. }
  824. // No mongos proxy available
  825. if (!this.isConnected()) {
  826. return callback(new MongoError('no mongos proxy available'));
  827. }
  828. // Execute write operation
  829. executeWriteOperation(this, 'insert', ns, ops, options, callback);
  830. };
  831. /**
  832. * Perform one or more update operations
  833. * @method
  834. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  835. * @param {array} ops An array of updates
  836. * @param {boolean} [options.ordered=true] Execute in order or out of order
  837. * @param {object} [options.writeConcern={}] Write concern for the operation
  838. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  839. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  840. * @param {ClientSession} [options.session=null] Session to use for the operation
  841. * @param {boolean} [options.retryWrites] Enable retryable writes for this operation
  842. * @param {opResultCallback} callback A callback function
  843. */
  844. Mongos.prototype.update = function(ns, ops, options, callback) {
  845. if (typeof options === 'function') {
  846. (callback = options), (options = {}), (options = options || {});
  847. }
  848. if (this.state === DESTROYED) return callback(new MongoError(f('topology was destroyed')));
  849. // Not connected but we have a disconnecthandler
  850. if (!this.isConnected() && this.s.disconnectHandler != null) {
  851. return this.s.disconnectHandler.add('update', ns, ops, options, callback);
  852. }
  853. // No mongos proxy available
  854. if (!this.isConnected()) {
  855. return callback(new MongoError('no mongos proxy available'));
  856. }
  857. // Execute write operation
  858. executeWriteOperation(this, 'update', ns, ops, options, callback);
  859. };
  860. /**
  861. * Perform one or more remove operations
  862. * @method
  863. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  864. * @param {array} ops An array of removes
  865. * @param {boolean} [options.ordered=true] Execute in order or out of order
  866. * @param {object} [options.writeConcern={}] Write concern for the operation
  867. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  868. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  869. * @param {ClientSession} [options.session=null] Session to use for the operation
  870. * @param {boolean} [options.retryWrites] Enable retryable writes for this operation
  871. * @param {opResultCallback} callback A callback function
  872. */
  873. Mongos.prototype.remove = function(ns, ops, options, callback) {
  874. if (typeof options === 'function') {
  875. (callback = options), (options = {}), (options = options || {});
  876. }
  877. if (this.state === DESTROYED) return callback(new MongoError(f('topology was destroyed')));
  878. // Not connected but we have a disconnecthandler
  879. if (!this.isConnected() && this.s.disconnectHandler != null) {
  880. return this.s.disconnectHandler.add('remove', ns, ops, options, callback);
  881. }
  882. // No mongos proxy available
  883. if (!this.isConnected()) {
  884. return callback(new MongoError('no mongos proxy available'));
  885. }
  886. // Execute write operation
  887. executeWriteOperation(this, 'remove', ns, ops, options, callback);
  888. };
  889. const RETRYABLE_WRITE_OPERATIONS = ['findAndModify', 'insert', 'update', 'delete'];
  890. function isWriteCommand(command) {
  891. return RETRYABLE_WRITE_OPERATIONS.some(op => command[op]);
  892. }
  893. /**
  894. * Execute a command
  895. * @method
  896. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  897. * @param {object} cmd The command hash
  898. * @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
  899. * @param {Connection} [options.connection] Specify connection object to execute command against
  900. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  901. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  902. * @param {ClientSession} [options.session=null] Session to use for the operation
  903. * @param {opResultCallback} callback A callback function
  904. */
  905. Mongos.prototype.command = function(ns, cmd, options, callback) {
  906. if (typeof options === 'function') {
  907. (callback = options), (options = {}), (options = options || {});
  908. }
  909. if (this.state === DESTROYED) return callback(new MongoError(f('topology was destroyed')));
  910. var self = this;
  911. // Pick a proxy
  912. var server = pickProxy(self);
  913. // Topology is not connected, save the call in the provided store to be
  914. // Executed at some point when the handler deems it's reconnected
  915. if ((server == null || !server.isConnected()) && this.s.disconnectHandler != null) {
  916. return this.s.disconnectHandler.add('command', ns, cmd, options, callback);
  917. }
  918. // No server returned we had an error
  919. if (server == null) {
  920. return callback(new MongoError('no mongos proxy available'));
  921. }
  922. // Cloned options
  923. var clonedOptions = cloneOptions(options);
  924. clonedOptions.topology = self;
  925. const willRetryWrite =
  926. !options.retrying &&
  927. options.retryWrites &&
  928. options.session &&
  929. isRetryableWritesSupported(self) &&
  930. !options.session.inTransaction() &&
  931. isWriteCommand(cmd);
  932. const cb = (err, result) => {
  933. if (!err) return callback(null, result);
  934. if (!isRetryableError(err)) {
  935. return callback(err);
  936. }
  937. if (willRetryWrite) {
  938. const newOptions = Object.assign({}, clonedOptions, { retrying: true });
  939. return this.command(ns, cmd, newOptions, callback);
  940. }
  941. return callback(err);
  942. };
  943. // increment and assign txnNumber
  944. if (willRetryWrite) {
  945. options.session.incrementTransactionNumber();
  946. options.willRetryWrite = willRetryWrite;
  947. }
  948. // Execute the command
  949. server.command(ns, cmd, clonedOptions, cb);
  950. };
  951. /**
  952. * Get a new cursor
  953. * @method
  954. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  955. * @param {object|Long} cmd Can be either a command returning a cursor or a cursorId
  956. * @param {object} [options] Options for the cursor
  957. * @param {object} [options.batchSize=0] Batchsize for the operation
  958. * @param {array} [options.documents=[]] Initial documents list for cursor
  959. * @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
  960. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  961. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  962. * @param {ClientSession} [options.session=null] Session to use for the operation
  963. * @param {object} [options.topology] The internal topology of the created cursor
  964. * @returns {Cursor}
  965. */
  966. Mongos.prototype.cursor = function(ns, cmd, options) {
  967. options = options || {};
  968. const topology = options.topology || this;
  969. // Set up final cursor type
  970. var FinalCursor = options.cursorFactory || this.s.Cursor;
  971. // Return the cursor
  972. return new FinalCursor(this.s.bson, ns, cmd, options, topology, this.s.options);
  973. };
  974. /**
  975. * Authenticate using a specified mechanism
  976. * @method
  977. * @param {string} mechanism The Auth mechanism we are invoking
  978. * @param {string} db The db we are invoking the mechanism against
  979. * @param {...object} param Parameters for the specific mechanism
  980. * @param {authResultCallback} callback A callback function
  981. */
  982. Mongos.prototype.auth = function(mechanism, db) {
  983. var allArgs = Array.prototype.slice.call(arguments, 0).slice(0);
  984. var self = this;
  985. var args = Array.prototype.slice.call(arguments, 2);
  986. var callback = args.pop();
  987. var currentContextIndex = 0;
  988. // If we don't have the mechanism fail
  989. if (this.authProviders[mechanism] == null && mechanism !== 'default') {
  990. return callback(new MongoError(f('auth provider %s does not exist', mechanism)));
  991. }
  992. // Are we already authenticating, throw
  993. if (this.authenticating) {
  994. return callback(new MongoError('authentication or logout allready in process'));
  995. }
  996. // Topology is not connected, save the call in the provided store to be
  997. // Executed at some point when the handler deems it's reconnected
  998. if (!self.isConnected() && self.s.disconnectHandler != null) {
  999. return self.s.disconnectHandler.add('auth', db, allArgs, {}, callback);
  1000. }
  1001. // Set to authenticating
  1002. this.authenticating = true;
  1003. // All errors
  1004. var errors = [];
  1005. // Get all the servers
  1006. var servers = this.connectedProxies.slice(0);
  1007. // No servers return
  1008. if (servers.length === 0) {
  1009. this.authenticating = false;
  1010. callback(null, true);
  1011. }
  1012. // Authenticate
  1013. function auth(server) {
  1014. // Arguments without a callback
  1015. var argsWithoutCallback = [mechanism, db].concat(args.slice(0));
  1016. // Create arguments
  1017. var finalArguments = argsWithoutCallback.concat([
  1018. function(err) {
  1019. count = count - 1;
  1020. // Save all the errors
  1021. if (err) errors.push({ name: server.name, err: err });
  1022. // We are done
  1023. if (count === 0) {
  1024. // Auth is done
  1025. self.authenticating = false;
  1026. // Return the auth error
  1027. if (errors.length) {
  1028. // Remove the entry from the stored authentication contexts
  1029. self.s.authenticationContexts.splice(currentContextIndex, 0);
  1030. // Return error
  1031. return callback(
  1032. new MongoError({
  1033. message: 'authentication fail',
  1034. errors: errors
  1035. }),
  1036. false
  1037. );
  1038. }
  1039. // Successfully authenticated session
  1040. callback(null, self);
  1041. }
  1042. }
  1043. ]);
  1044. // Execute the auth only against non arbiter servers
  1045. if (!server.lastIsMaster().arbiterOnly) {
  1046. server.auth.apply(server, finalArguments);
  1047. }
  1048. }
  1049. // Save current context index
  1050. currentContextIndex = this.s.authenticationContexts.length;
  1051. // Store the auth context and return the last index
  1052. this.s.authenticationContexts.push([mechanism, db].concat(args.slice(0)));
  1053. // Get total count
  1054. var count = servers.length;
  1055. // Authenticate against all servers
  1056. while (servers.length > 0) {
  1057. auth(servers.shift());
  1058. }
  1059. };
  1060. /**
  1061. * Logout from a database
  1062. * @method
  1063. * @param {string} db The db we are logging out from
  1064. * @param {authResultCallback} callback A callback function
  1065. */
  1066. Mongos.prototype.logout = function(dbName, callback) {
  1067. var self = this;
  1068. // Are we authenticating or logging out, throw
  1069. if (this.authenticating) {
  1070. throw new MongoError('authentication or logout allready in process');
  1071. }
  1072. // Ensure no new members are processed while logging out
  1073. this.authenticating = true;
  1074. // Remove from all auth providers (avoid any reaplication of the auth details)
  1075. var providers = Object.keys(this.authProviders);
  1076. for (var i = 0; i < providers.length; i++) {
  1077. this.authProviders[providers[i]].logout(dbName);
  1078. }
  1079. // Now logout all the servers
  1080. var servers = this.connectedProxies.slice(0);
  1081. var count = servers.length;
  1082. if (count === 0) return callback();
  1083. var errors = [];
  1084. function logoutServer(_server, cb) {
  1085. _server.logout(dbName, function(err) {
  1086. if (err) errors.push({ name: _server.name, err: err });
  1087. cb();
  1088. });
  1089. }
  1090. // Execute logout on all server instances
  1091. for (i = 0; i < servers.length; i++) {
  1092. logoutServer(servers[i], function() {
  1093. count = count - 1;
  1094. if (count === 0) {
  1095. // Do not block new operations
  1096. self.authenticating = false;
  1097. // If we have one or more errors
  1098. if (errors.length)
  1099. return callback(
  1100. new MongoError({
  1101. message: f('logout failed against db %s', dbName),
  1102. errors: errors
  1103. }),
  1104. false
  1105. );
  1106. // No errors
  1107. callback();
  1108. }
  1109. });
  1110. }
  1111. };
  1112. /**
  1113. * Selects a server
  1114. *
  1115. * @method
  1116. * @param {function} selector Unused
  1117. * @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
  1118. * @param {function} callback
  1119. */
  1120. Mongos.prototype.selectServer = function(selector, options, callback) {
  1121. if (typeof selector === 'function' && typeof callback === 'undefined')
  1122. (callback = selector), (selector = undefined), (options = {});
  1123. if (typeof options === 'function')
  1124. (callback = options), (options = selector), (selector = undefined);
  1125. options = options || {};
  1126. const server = pickProxy(this);
  1127. if (this.s.debug) this.emit('pickedServer', null, server);
  1128. callback(null, server);
  1129. };
  1130. /**
  1131. * All raw connections
  1132. * @method
  1133. * @return {Connection[]}
  1134. */
  1135. Mongos.prototype.connections = function() {
  1136. var connections = [];
  1137. for (var i = 0; i < this.connectedProxies.length; i++) {
  1138. connections = connections.concat(this.connectedProxies[i].connections());
  1139. }
  1140. return connections;
  1141. };
  1142. function emitTopologyDescriptionChanged(self) {
  1143. if (self.listeners('topologyDescriptionChanged').length > 0) {
  1144. var topology = 'Unknown';
  1145. if (self.connectedProxies.length > 0) {
  1146. topology = 'Sharded';
  1147. }
  1148. // Generate description
  1149. var description = {
  1150. topologyType: topology,
  1151. servers: []
  1152. };
  1153. // All proxies
  1154. var proxies = self.disconnectedProxies.concat(self.connectingProxies);
  1155. // Add all the disconnected proxies
  1156. description.servers = description.servers.concat(
  1157. proxies.map(function(x) {
  1158. var description = x.getDescription();
  1159. description.type = 'Unknown';
  1160. return description;
  1161. })
  1162. );
  1163. // Add all the connected proxies
  1164. description.servers = description.servers.concat(
  1165. self.connectedProxies.map(function(x) {
  1166. var description = x.getDescription();
  1167. description.type = 'Mongos';
  1168. return description;
  1169. })
  1170. );
  1171. // Get the diff
  1172. var diffResult = diff(self.topologyDescription, description);
  1173. // Create the result
  1174. var result = {
  1175. topologyId: self.id,
  1176. previousDescription: self.topologyDescription,
  1177. newDescription: description,
  1178. diff: diffResult
  1179. };
  1180. // Emit the topologyDescription change
  1181. if (diffResult.servers.length > 0) {
  1182. self.emit('topologyDescriptionChanged', result);
  1183. }
  1184. // Set the new description
  1185. self.topologyDescription = description;
  1186. }
  1187. }
  1188. /**
  1189. * A mongos connect event, used to verify that the connection is up and running
  1190. *
  1191. * @event Mongos#connect
  1192. * @type {Mongos}
  1193. */
  1194. /**
  1195. * A mongos reconnect event, used to verify that the mongos topology has reconnected
  1196. *
  1197. * @event Mongos#reconnect
  1198. * @type {Mongos}
  1199. */
  1200. /**
  1201. * A mongos fullsetup event, used to signal that all topology members have been contacted.
  1202. *
  1203. * @event Mongos#fullsetup
  1204. * @type {Mongos}
  1205. */
  1206. /**
  1207. * A mongos all event, used to signal that all topology members have been contacted.
  1208. *
  1209. * @event Mongos#all
  1210. * @type {Mongos}
  1211. */
  1212. /**
  1213. * A server member left the mongos list
  1214. *
  1215. * @event Mongos#left
  1216. * @type {Mongos}
  1217. * @param {string} type The type of member that left (mongos)
  1218. * @param {Server} server The server object that left
  1219. */
  1220. /**
  1221. * A server member joined the mongos list
  1222. *
  1223. * @event Mongos#joined
  1224. * @type {Mongos}
  1225. * @param {string} type The type of member that left (mongos)
  1226. * @param {Server} server The server object that joined
  1227. */
  1228. /**
  1229. * A server opening SDAM monitoring event
  1230. *
  1231. * @event Mongos#serverOpening
  1232. * @type {object}
  1233. */
  1234. /**
  1235. * A server closed SDAM monitoring event
  1236. *
  1237. * @event Mongos#serverClosed
  1238. * @type {object}
  1239. */
  1240. /**
  1241. * A server description SDAM change monitoring event
  1242. *
  1243. * @event Mongos#serverDescriptionChanged
  1244. * @type {object}
  1245. */
  1246. /**
  1247. * A topology open SDAM event
  1248. *
  1249. * @event Mongos#topologyOpening
  1250. * @type {object}
  1251. */
  1252. /**
  1253. * A topology closed SDAM event
  1254. *
  1255. * @event Mongos#topologyClosed
  1256. * @type {object}
  1257. */
  1258. /**
  1259. * A topology structure SDAM change event
  1260. *
  1261. * @event Mongos#topologyDescriptionChanged
  1262. * @type {object}
  1263. */
  1264. /**
  1265. * A topology serverHeartbeatStarted SDAM event
  1266. *
  1267. * @event Mongos#serverHeartbeatStarted
  1268. * @type {object}
  1269. */
  1270. /**
  1271. * A topology serverHeartbeatFailed SDAM event
  1272. *
  1273. * @event Mongos#serverHeartbeatFailed
  1274. * @type {object}
  1275. */
  1276. /**
  1277. * A topology serverHeartbeatSucceeded SDAM change event
  1278. *
  1279. * @event Mongos#serverHeartbeatSucceeded
  1280. * @type {object}
  1281. */
  1282. /**
  1283. * An event emitted indicating a command was started, if command monitoring is enabled
  1284. *
  1285. * @event Mongos#commandStarted
  1286. * @type {object}
  1287. */
  1288. /**
  1289. * An event emitted indicating a command succeeded, if command monitoring is enabled
  1290. *
  1291. * @event Mongos#commandSucceeded
  1292. * @type {object}
  1293. */
  1294. /**
  1295. * An event emitted indicating a command failed, if command monitoring is enabled
  1296. *
  1297. * @event Mongos#commandFailed
  1298. * @type {object}
  1299. */
  1300. module.exports = Mongos;