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.

server.js 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. 'use strict';
  2. var inherits = require('util').inherits,
  3. f = require('util').format,
  4. EventEmitter = require('events').EventEmitter,
  5. ReadPreference = require('./read_preference'),
  6. Logger = require('../connection/logger'),
  7. debugOptions = require('../connection/utils').debugOptions,
  8. retrieveBSON = require('../connection/utils').retrieveBSON,
  9. Pool = require('../connection/pool'),
  10. Query = require('../connection/commands').Query,
  11. MongoError = require('../error').MongoError,
  12. MongoNetworkError = require('../error').MongoNetworkError,
  13. TwoSixWireProtocolSupport = require('../wireprotocol/2_6_support'),
  14. ThreeTwoWireProtocolSupport = require('../wireprotocol/3_2_support'),
  15. BasicCursor = require('../cursor'),
  16. sdam = require('./shared'),
  17. createClientInfo = require('./shared').createClientInfo,
  18. createCompressionInfo = require('./shared').createCompressionInfo,
  19. resolveClusterTime = require('./shared').resolveClusterTime,
  20. SessionMixins = require('./shared').SessionMixins,
  21. relayEvents = require('../utils').relayEvents;
  22. const collationNotSupported = require('../utils').collationNotSupported;
  23. function getSaslSupportedMechs(options) {
  24. if (!options) {
  25. return {};
  26. }
  27. const authArray = options.auth || [];
  28. const authMechanism = authArray[0] || options.authMechanism;
  29. const authSource = authArray[1] || options.authSource || options.dbName || 'admin';
  30. const user = authArray[2] || options.user;
  31. if (typeof authMechanism === 'string' && authMechanism.toUpperCase() !== 'DEFAULT') {
  32. return {};
  33. }
  34. if (!user) {
  35. return {};
  36. }
  37. return { saslSupportedMechs: `${authSource}.${user}` };
  38. }
  39. function getDefaultAuthMechanism(ismaster) {
  40. if (ismaster) {
  41. // If ismaster contains saslSupportedMechs, use scram-sha-256
  42. // if it is available, else scram-sha-1
  43. if (Array.isArray(ismaster.saslSupportedMechs)) {
  44. return ismaster.saslSupportedMechs.indexOf('SCRAM-SHA-256') >= 0
  45. ? 'scram-sha-256'
  46. : 'scram-sha-1';
  47. }
  48. // Fallback to legacy selection method. If wire version >= 3, use scram-sha-1
  49. if (ismaster.maxWireVersion >= 3) {
  50. return 'scram-sha-1';
  51. }
  52. }
  53. // Default for wireprotocol < 3
  54. return 'mongocr';
  55. }
  56. function extractIsMasterError(err, result) {
  57. if (err) {
  58. return err;
  59. }
  60. if (result && result.result && result.result.ok === 0) {
  61. return new MongoError(result.result);
  62. }
  63. }
  64. // Used for filtering out fields for loggin
  65. var debugFields = [
  66. 'reconnect',
  67. 'reconnectTries',
  68. 'reconnectInterval',
  69. 'emitError',
  70. 'cursorFactory',
  71. 'host',
  72. 'port',
  73. 'size',
  74. 'keepAlive',
  75. 'keepAliveInitialDelay',
  76. 'noDelay',
  77. 'connectionTimeout',
  78. 'checkServerIdentity',
  79. 'socketTimeout',
  80. 'singleBufferSerializtion',
  81. 'ssl',
  82. 'ca',
  83. 'crl',
  84. 'cert',
  85. 'key',
  86. 'rejectUnauthorized',
  87. 'promoteLongs',
  88. 'promoteValues',
  89. 'promoteBuffers',
  90. 'servername'
  91. ];
  92. // Server instance id
  93. var id = 0;
  94. var serverAccounting = false;
  95. var servers = {};
  96. var BSON = retrieveBSON();
  97. /**
  98. * Creates a new Server instance
  99. * @class
  100. * @param {boolean} [options.reconnect=true] Server will attempt to reconnect on loss of connection
  101. * @param {number} [options.reconnectTries=30] Server attempt to reconnect #times
  102. * @param {number} [options.reconnectInterval=1000] Server will wait # milliseconds between retries
  103. * @param {number} [options.monitoring=true] Enable the server state monitoring (calling ismaster at monitoringInterval)
  104. * @param {number} [options.monitoringInterval=5000] The interval of calling ismaster when monitoring is enabled.
  105. * @param {Cursor} [options.cursorFactory=Cursor] The cursor factory class used for all query cursors
  106. * @param {string} options.host The server host
  107. * @param {number} options.port The server port
  108. * @param {number} [options.size=5] Server connection pool size
  109. * @param {boolean} [options.keepAlive=true] TCP Connection keep alive enabled
  110. * @param {number} [options.keepAliveInitialDelay=300000] Initial delay before TCP keep alive enabled
  111. * @param {boolean} [options.noDelay=true] TCP Connection no delay
  112. * @param {number} [options.connectionTimeout=30000] TCP Connection timeout setting
  113. * @param {number} [options.socketTimeout=360000] TCP Socket timeout setting
  114. * @param {boolean} [options.ssl=false] Use SSL for connection
  115. * @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.
  116. * @param {Buffer} [options.ca] SSL Certificate store binary buffer
  117. * @param {Buffer} [options.crl] SSL Certificate revocation store binary buffer
  118. * @param {Buffer} [options.cert] SSL Certificate binary buffer
  119. * @param {Buffer} [options.key] SSL Key file binary buffer
  120. * @param {string} [options.passphrase] SSL Certificate pass phrase
  121. * @param {boolean} [options.rejectUnauthorized=true] Reject unauthorized server certificates
  122. * @param {string} [options.servername=null] String containing the server name requested via TLS SNI.
  123. * @param {boolean} [options.promoteLongs=true] Convert Long values from the db into Numbers if they fit into 53 bits
  124. * @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
  125. * @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
  126. * @param {string} [options.appname=null] Application name, passed in on ismaster call and logged in mongod server logs. Maximum size 128 bytes.
  127. * @param {boolean} [options.domainsEnabled=false] Enable the wrapping of the callback in the current domain, disabled by default to avoid perf hit.
  128. * @param {boolean} [options.monitorCommands=false] Enable command monitoring for this topology
  129. * @return {Server} A cursor instance
  130. * @fires Server#connect
  131. * @fires Server#close
  132. * @fires Server#error
  133. * @fires Server#timeout
  134. * @fires Server#parseError
  135. * @fires Server#reconnect
  136. * @fires Server#reconnectFailed
  137. * @fires Server#serverHeartbeatStarted
  138. * @fires Server#serverHeartbeatSucceeded
  139. * @fires Server#serverHeartbeatFailed
  140. * @fires Server#topologyOpening
  141. * @fires Server#topologyClosed
  142. * @fires Server#topologyDescriptionChanged
  143. * @property {string} type the topology type.
  144. * @property {string} parserType the parser type used (c++ or js).
  145. */
  146. var Server = function(options) {
  147. options = options || {};
  148. // Add event listener
  149. EventEmitter.call(this);
  150. // Server instance id
  151. this.id = id++;
  152. // Internal state
  153. this.s = {
  154. // Options
  155. options: options,
  156. // Logger
  157. logger: Logger('Server', options),
  158. // Factory overrides
  159. Cursor: options.cursorFactory || BasicCursor,
  160. // BSON instance
  161. bson:
  162. options.bson ||
  163. new BSON([
  164. BSON.Binary,
  165. BSON.Code,
  166. BSON.DBRef,
  167. BSON.Decimal128,
  168. BSON.Double,
  169. BSON.Int32,
  170. BSON.Long,
  171. BSON.Map,
  172. BSON.MaxKey,
  173. BSON.MinKey,
  174. BSON.ObjectId,
  175. BSON.BSONRegExp,
  176. BSON.Symbol,
  177. BSON.Timestamp
  178. ]),
  179. // Pool
  180. pool: null,
  181. // Disconnect handler
  182. disconnectHandler: options.disconnectHandler,
  183. // Monitor thread (keeps the connection alive)
  184. monitoring: typeof options.monitoring === 'boolean' ? options.monitoring : true,
  185. // Is the server in a topology
  186. inTopology: !!options.parent,
  187. // Monitoring timeout
  188. monitoringInterval:
  189. typeof options.monitoringInterval === 'number' ? options.monitoringInterval : 5000,
  190. // Topology id
  191. topologyId: -1,
  192. compression: { compressors: createCompressionInfo(options) },
  193. // Optional parent topology
  194. parent: options.parent
  195. };
  196. // If this is a single deployment we need to track the clusterTime here
  197. if (!this.s.parent) {
  198. this.s.clusterTime = null;
  199. }
  200. // Curent ismaster
  201. this.ismaster = null;
  202. // Current ping time
  203. this.lastIsMasterMS = -1;
  204. // The monitoringProcessId
  205. this.monitoringProcessId = null;
  206. // Initial connection
  207. this.initialConnect = true;
  208. // Wire protocol handler, default to oldest known protocol handler
  209. // this gets changed when the first ismaster is called.
  210. this.wireProtocolHandler = new TwoSixWireProtocolSupport();
  211. // Default type
  212. this._type = 'server';
  213. // Set the client info
  214. this.clientInfo = createClientInfo(options);
  215. // Max Stalleness values
  216. // last time we updated the ismaster state
  217. this.lastUpdateTime = 0;
  218. // Last write time
  219. this.lastWriteDate = 0;
  220. // Stalleness
  221. this.staleness = 0;
  222. };
  223. inherits(Server, EventEmitter);
  224. Object.assign(Server.prototype, SessionMixins);
  225. Object.defineProperty(Server.prototype, 'type', {
  226. enumerable: true,
  227. get: function() {
  228. return this._type;
  229. }
  230. });
  231. Object.defineProperty(Server.prototype, 'parserType', {
  232. enumerable: true,
  233. get: function() {
  234. return BSON.native ? 'c++' : 'js';
  235. }
  236. });
  237. Object.defineProperty(Server.prototype, 'logicalSessionTimeoutMinutes', {
  238. enumerable: true,
  239. get: function() {
  240. if (!this.ismaster) return null;
  241. return this.ismaster.logicalSessionTimeoutMinutes || null;
  242. }
  243. });
  244. // In single server deployments we track the clusterTime directly on the topology, however
  245. // in Mongos and ReplSet deployments we instead need to delegate the clusterTime up to the
  246. // tracking objects so we can ensure we are gossiping the maximum time received from the
  247. // server.
  248. Object.defineProperty(Server.prototype, 'clusterTime', {
  249. enumerable: true,
  250. set: function(clusterTime) {
  251. const settings = this.s.parent ? this.s.parent : this.s;
  252. resolveClusterTime(settings, clusterTime);
  253. },
  254. get: function() {
  255. const settings = this.s.parent ? this.s.parent : this.s;
  256. return settings.clusterTime || null;
  257. }
  258. });
  259. Server.enableServerAccounting = function() {
  260. serverAccounting = true;
  261. servers = {};
  262. };
  263. Server.disableServerAccounting = function() {
  264. serverAccounting = false;
  265. };
  266. Server.servers = function() {
  267. return servers;
  268. };
  269. Object.defineProperty(Server.prototype, 'name', {
  270. enumerable: true,
  271. get: function() {
  272. return this.s.options.host + ':' + this.s.options.port;
  273. }
  274. });
  275. function isSupportedServer(response) {
  276. return response && typeof response.maxWireVersion === 'number' && response.maxWireVersion >= 2;
  277. }
  278. function configureWireProtocolHandler(self, ismaster) {
  279. // 3.2 wire protocol handler
  280. if (ismaster.maxWireVersion >= 4) {
  281. return new ThreeTwoWireProtocolSupport();
  282. }
  283. // default to 2.6 wire protocol handler
  284. return new TwoSixWireProtocolSupport();
  285. }
  286. function disconnectHandler(self, type, ns, cmd, options, callback) {
  287. // Topology is not connected, save the call in the provided store to be
  288. // Executed at some point when the handler deems it's reconnected
  289. if (
  290. !self.s.pool.isConnected() &&
  291. self.s.options.reconnect &&
  292. self.s.disconnectHandler != null &&
  293. !options.monitoring
  294. ) {
  295. self.s.disconnectHandler.add(type, ns, cmd, options, callback);
  296. return true;
  297. }
  298. // If we have no connection error
  299. if (!self.s.pool.isConnected()) {
  300. callback(new MongoError(f('no connection available to server %s', self.name)));
  301. return true;
  302. }
  303. }
  304. function monitoringProcess(self) {
  305. return function() {
  306. // Pool was destroyed do not continue process
  307. if (self.s.pool.isDestroyed()) return;
  308. // Emit monitoring Process event
  309. self.emit('monitoring', self);
  310. // Perform ismaster call
  311. // Query options
  312. var queryOptions = { numberToSkip: 0, numberToReturn: -1, checkKeys: false, slaveOk: true };
  313. // Create a query instance
  314. var query = new Query(self.s.bson, 'admin.$cmd', { ismaster: true }, queryOptions);
  315. // Get start time
  316. var start = new Date().getTime();
  317. // Execute the ismaster query
  318. self.s.pool.write(
  319. query,
  320. {
  321. socketTimeout:
  322. typeof self.s.options.connectionTimeout !== 'number'
  323. ? 2000
  324. : self.s.options.connectionTimeout,
  325. monitoring: true
  326. },
  327. function(err, result) {
  328. // Set initial lastIsMasterMS
  329. self.lastIsMasterMS = new Date().getTime() - start;
  330. if (self.s.pool.isDestroyed()) return;
  331. // Update the ismaster view if we have a result
  332. if (result) {
  333. self.ismaster = result.result;
  334. }
  335. // Re-schedule the monitoring process
  336. self.monitoringProcessId = setTimeout(monitoringProcess(self), self.s.monitoringInterval);
  337. }
  338. );
  339. };
  340. }
  341. var eventHandler = function(self, event) {
  342. return function(err) {
  343. // Log information of received information if in info mode
  344. if (self.s.logger.isInfo()) {
  345. var object = err instanceof MongoError ? JSON.stringify(err) : {};
  346. self.s.logger.info(
  347. f('server %s fired event %s out with message %s', self.name, event, object)
  348. );
  349. }
  350. // Handle connect event
  351. if (event === 'connect') {
  352. // Issue an ismaster command at connect
  353. // Query options
  354. var queryOptions = { numberToSkip: 0, numberToReturn: -1, checkKeys: false, slaveOk: true };
  355. // Create a query instance
  356. var compressors =
  357. self.s.compression && self.s.compression.compressors ? self.s.compression.compressors : [];
  358. var query = new Query(
  359. self.s.bson,
  360. 'admin.$cmd',
  361. Object.assign(
  362. { ismaster: true, client: self.clientInfo, compression: compressors },
  363. getSaslSupportedMechs(self.s.options)
  364. ),
  365. queryOptions
  366. );
  367. // Get start time
  368. var start = new Date().getTime();
  369. // Execute the ismaster query
  370. self.s.pool.write(
  371. query,
  372. {
  373. socketTimeout: self.s.options.connectionTimeout || 2000
  374. },
  375. function(err, result) {
  376. // Set initial lastIsMasterMS
  377. self.lastIsMasterMS = new Date().getTime() - start;
  378. const serverError = extractIsMasterError(err, result);
  379. if (serverError) {
  380. self.destroy();
  381. return self.emit('error', serverError);
  382. }
  383. if (!isSupportedServer(result.result)) {
  384. self.destroy();
  385. const latestSupportedVersion = '2.6';
  386. const message =
  387. 'Server at ' +
  388. self.s.options.host +
  389. ':' +
  390. self.s.options.port +
  391. ' reports wire version ' +
  392. (result.result.maxWireVersion || 0) +
  393. ', but this version of Node.js Driver requires at least 2 (MongoDB' +
  394. latestSupportedVersion +
  395. ').';
  396. return self.emit('error', new MongoError(message), self);
  397. }
  398. // Determine whether the server is instructing us to use a compressor
  399. if (result.result && result.result.compression) {
  400. for (var i = 0; i < self.s.compression.compressors.length; i++) {
  401. if (result.result.compression.indexOf(self.s.compression.compressors[i]) > -1) {
  402. self.s.pool.options.agreedCompressor = self.s.compression.compressors[i];
  403. break;
  404. }
  405. }
  406. if (self.s.compression.zlibCompressionLevel) {
  407. self.s.pool.options.zlibCompressionLevel = self.s.compression.zlibCompressionLevel;
  408. }
  409. }
  410. // Ensure no error emitted after initial connect when reconnecting
  411. self.initialConnect = false;
  412. // Save the ismaster
  413. self.ismaster = result.result;
  414. // It's a proxy change the type so
  415. // the wireprotocol will send $readPreference
  416. if (self.ismaster.msg === 'isdbgrid') {
  417. self._type = 'mongos';
  418. }
  419. // Add the correct wire protocol handler
  420. self.wireProtocolHandler = configureWireProtocolHandler(self, self.ismaster);
  421. // Have we defined self monitoring
  422. if (self.s.monitoring) {
  423. self.monitoringProcessId = setTimeout(
  424. monitoringProcess(self),
  425. self.s.monitoringInterval
  426. );
  427. }
  428. // Emit server description changed if something listening
  429. sdam.emitServerDescriptionChanged(self, {
  430. address: self.name,
  431. arbiters: [],
  432. hosts: [],
  433. passives: [],
  434. type: sdam.getTopologyType(self)
  435. });
  436. if (!self.s.inTopology) {
  437. // Emit topology description changed if something listening
  438. sdam.emitTopologyDescriptionChanged(self, {
  439. topologyType: 'Single',
  440. servers: [
  441. {
  442. address: self.name,
  443. arbiters: [],
  444. hosts: [],
  445. passives: [],
  446. type: sdam.getTopologyType(self)
  447. }
  448. ]
  449. });
  450. }
  451. // Log the ismaster if available
  452. if (self.s.logger.isInfo()) {
  453. self.s.logger.info(
  454. f('server %s connected with ismaster [%s]', self.name, JSON.stringify(self.ismaster))
  455. );
  456. }
  457. // Emit connect
  458. self.emit('connect', self);
  459. }
  460. );
  461. } else if (
  462. event === 'error' ||
  463. event === 'parseError' ||
  464. event === 'close' ||
  465. event === 'timeout' ||
  466. event === 'reconnect' ||
  467. event === 'attemptReconnect' ||
  468. 'reconnectFailed'
  469. ) {
  470. // Remove server instance from accounting
  471. if (
  472. serverAccounting &&
  473. ['close', 'timeout', 'error', 'parseError', 'reconnectFailed'].indexOf(event) !== -1
  474. ) {
  475. // Emit toplogy opening event if not in topology
  476. if (!self.s.inTopology) {
  477. self.emit('topologyOpening', { topologyId: self.id });
  478. }
  479. delete servers[self.id];
  480. }
  481. if (event === 'close') {
  482. // Closing emits a server description changed event going to unknown.
  483. sdam.emitServerDescriptionChanged(self, {
  484. address: self.name,
  485. arbiters: [],
  486. hosts: [],
  487. passives: [],
  488. type: 'Unknown'
  489. });
  490. }
  491. // Reconnect failed return error
  492. if (event === 'reconnectFailed') {
  493. self.emit('reconnectFailed', err);
  494. // Emit error if any listeners
  495. if (self.listeners('error').length > 0) {
  496. self.emit('error', err);
  497. }
  498. // Terminate
  499. return;
  500. }
  501. // On first connect fail
  502. if (
  503. self.s.pool.state === 'disconnected' &&
  504. self.initialConnect &&
  505. ['close', 'timeout', 'error', 'parseError'].indexOf(event) !== -1
  506. ) {
  507. self.initialConnect = false;
  508. return self.emit(
  509. 'error',
  510. new MongoNetworkError(
  511. f('failed to connect to server [%s] on first connect [%s]', self.name, err)
  512. )
  513. );
  514. }
  515. // Reconnect event, emit the server
  516. if (event === 'reconnect') {
  517. // Reconnecting emits a server description changed event going from unknown to the
  518. // current server type.
  519. sdam.emitServerDescriptionChanged(self, {
  520. address: self.name,
  521. arbiters: [],
  522. hosts: [],
  523. passives: [],
  524. type: sdam.getTopologyType(self)
  525. });
  526. return self.emit(event, self);
  527. }
  528. // Emit the event
  529. self.emit(event, err);
  530. }
  531. };
  532. };
  533. /**
  534. * Initiate server connect
  535. * @method
  536. * @param {array} [options.auth=null] Array of auth options to apply on connect
  537. */
  538. Server.prototype.connect = function(options) {
  539. var self = this;
  540. options = options || {};
  541. // Set the connections
  542. if (serverAccounting) servers[this.id] = this;
  543. // Do not allow connect to be called on anything that's not disconnected
  544. if (self.s.pool && !self.s.pool.isDisconnected() && !self.s.pool.isDestroyed()) {
  545. throw new MongoError(f('server instance in invalid state %s', self.s.pool.state));
  546. }
  547. // Create a pool
  548. self.s.pool = new Pool(this, Object.assign(self.s.options, options, { bson: this.s.bson }));
  549. // Set up listeners
  550. self.s.pool.on('close', eventHandler(self, 'close'));
  551. self.s.pool.on('error', eventHandler(self, 'error'));
  552. self.s.pool.on('timeout', eventHandler(self, 'timeout'));
  553. self.s.pool.on('parseError', eventHandler(self, 'parseError'));
  554. self.s.pool.on('connect', eventHandler(self, 'connect'));
  555. self.s.pool.on('reconnect', eventHandler(self, 'reconnect'));
  556. self.s.pool.on('reconnectFailed', eventHandler(self, 'reconnectFailed'));
  557. // Set up listeners for command monitoring
  558. relayEvents(self.s.pool, self, ['commandStarted', 'commandSucceeded', 'commandFailed']);
  559. // Emit toplogy opening event if not in topology
  560. if (!self.s.inTopology) {
  561. this.emit('topologyOpening', { topologyId: self.id });
  562. }
  563. // Emit opening server event
  564. self.emit('serverOpening', {
  565. topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id,
  566. address: self.name
  567. });
  568. // Connect with optional auth settings
  569. if (options.auth) {
  570. self.s.pool.connect.apply(self.s.pool, options.auth);
  571. } else {
  572. self.s.pool.connect();
  573. }
  574. };
  575. /**
  576. * Get the server description
  577. * @method
  578. * @return {object}
  579. */
  580. Server.prototype.getDescription = function() {
  581. var ismaster = this.ismaster || {};
  582. var description = {
  583. type: sdam.getTopologyType(this),
  584. address: this.name
  585. };
  586. // Add fields if available
  587. if (ismaster.hosts) description.hosts = ismaster.hosts;
  588. if (ismaster.arbiters) description.arbiters = ismaster.arbiters;
  589. if (ismaster.passives) description.passives = ismaster.passives;
  590. if (ismaster.setName) description.setName = ismaster.setName;
  591. return description;
  592. };
  593. /**
  594. * Returns the last known ismaster document for this server
  595. * @method
  596. * @return {object}
  597. */
  598. Server.prototype.lastIsMaster = function() {
  599. return this.ismaster;
  600. };
  601. /**
  602. * Unref all connections belong to this server
  603. * @method
  604. */
  605. Server.prototype.unref = function() {
  606. this.s.pool.unref();
  607. };
  608. /**
  609. * Figure out if the server is connected
  610. * @method
  611. * @return {boolean}
  612. */
  613. Server.prototype.isConnected = function() {
  614. if (!this.s.pool) return false;
  615. return this.s.pool.isConnected();
  616. };
  617. /**
  618. * Figure out if the server instance was destroyed by calling destroy
  619. * @method
  620. * @return {boolean}
  621. */
  622. Server.prototype.isDestroyed = function() {
  623. if (!this.s.pool) return false;
  624. return this.s.pool.isDestroyed();
  625. };
  626. function basicWriteValidations(self) {
  627. if (!self.s.pool) return new MongoError('server instance is not connected');
  628. if (self.s.pool.isDestroyed()) return new MongoError('server instance pool was destroyed');
  629. }
  630. function basicReadValidations(self, options) {
  631. basicWriteValidations(self, options);
  632. if (options.readPreference && !(options.readPreference instanceof ReadPreference)) {
  633. throw new Error('readPreference must be an instance of ReadPreference');
  634. }
  635. }
  636. /**
  637. * Execute a command
  638. * @method
  639. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  640. * @param {object} cmd The command hash
  641. * @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
  642. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  643. * @param {Boolean} [options.checkKeys=false] Specify if the bson parser should validate keys.
  644. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  645. * @param {Boolean} [options.fullResult=false] Return the full envelope instead of just the result document.
  646. * @param {ClientSession} [options.session=null] Session to use for the operation
  647. * @param {opResultCallback} callback A callback function
  648. */
  649. Server.prototype.command = function(ns, cmd, options, callback) {
  650. var self = this;
  651. if (typeof options === 'function') {
  652. (callback = options), (options = {}), (options = options || {});
  653. }
  654. var result = basicReadValidations(self, options);
  655. if (result) return callback(result);
  656. // Clone the options
  657. options = Object.assign({}, options, { wireProtocolCommand: false });
  658. // Debug log
  659. if (self.s.logger.isDebug())
  660. self.s.logger.debug(
  661. f(
  662. 'executing command [%s] against %s',
  663. JSON.stringify({
  664. ns: ns,
  665. cmd: cmd,
  666. options: debugOptions(debugFields, options)
  667. }),
  668. self.name
  669. )
  670. );
  671. // If we are not connected or have a disconnectHandler specified
  672. if (disconnectHandler(self, 'command', ns, cmd, options, callback)) return;
  673. // error if collation not supported
  674. if (collationNotSupported(this, cmd)) {
  675. return callback(new MongoError(`server ${this.name} does not support collation`));
  676. }
  677. self.wireProtocolHandler.command(self, ns, cmd, options, callback);
  678. };
  679. /**
  680. * Insert one or more documents
  681. * @method
  682. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  683. * @param {array} ops An array of documents to insert
  684. * @param {boolean} [options.ordered=true] Execute in order or out of order
  685. * @param {object} [options.writeConcern={}] Write concern for the operation
  686. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  687. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  688. * @param {ClientSession} [options.session=null] Session to use for the operation
  689. * @param {opResultCallback} callback A callback function
  690. */
  691. Server.prototype.insert = function(ns, ops, options, callback) {
  692. var self = this;
  693. if (typeof options === 'function') {
  694. (callback = options), (options = {}), (options = options || {});
  695. }
  696. var result = basicWriteValidations(self, options);
  697. if (result) return callback(result);
  698. // If we are not connected or have a disconnectHandler specified
  699. if (disconnectHandler(self, 'insert', ns, ops, options, callback)) return;
  700. // Setup the docs as an array
  701. ops = Array.isArray(ops) ? ops : [ops];
  702. // Execute write
  703. return self.wireProtocolHandler.insert(self, ns, ops, options, callback);
  704. };
  705. /**
  706. * Perform one or more update operations
  707. * @method
  708. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  709. * @param {array} ops An array of updates
  710. * @param {boolean} [options.ordered=true] Execute in order or out of order
  711. * @param {object} [options.writeConcern={}] Write concern for the operation
  712. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  713. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  714. * @param {ClientSession} [options.session=null] Session to use for the operation
  715. * @param {opResultCallback} callback A callback function
  716. */
  717. Server.prototype.update = function(ns, ops, options, callback) {
  718. var self = this;
  719. if (typeof options === 'function') {
  720. (callback = options), (options = {}), (options = options || {});
  721. }
  722. var result = basicWriteValidations(self, options);
  723. if (result) return callback(result);
  724. // If we are not connected or have a disconnectHandler specified
  725. if (disconnectHandler(self, 'update', ns, ops, options, callback)) return;
  726. // error if collation not supported
  727. if (collationNotSupported(this, options)) {
  728. return callback(new MongoError(`server ${this.name} does not support collation`));
  729. }
  730. // Setup the docs as an array
  731. ops = Array.isArray(ops) ? ops : [ops];
  732. // Execute write
  733. return self.wireProtocolHandler.update(self, ns, ops, options, callback);
  734. };
  735. /**
  736. * Perform one or more remove operations
  737. * @method
  738. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  739. * @param {array} ops An array of removes
  740. * @param {boolean} [options.ordered=true] Execute in order or out of order
  741. * @param {object} [options.writeConcern={}] Write concern for the operation
  742. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  743. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  744. * @param {ClientSession} [options.session=null] Session to use for the operation
  745. * @param {opResultCallback} callback A callback function
  746. */
  747. Server.prototype.remove = function(ns, ops, options, callback) {
  748. var self = this;
  749. if (typeof options === 'function') {
  750. (callback = options), (options = {}), (options = options || {});
  751. }
  752. var result = basicWriteValidations(self, options);
  753. if (result) return callback(result);
  754. // If we are not connected or have a disconnectHandler specified
  755. if (disconnectHandler(self, 'remove', ns, ops, options, callback)) return;
  756. // error if collation not supported
  757. if (collationNotSupported(this, options)) {
  758. return callback(new MongoError(`server ${this.name} does not support collation`));
  759. }
  760. // Setup the docs as an array
  761. ops = Array.isArray(ops) ? ops : [ops];
  762. // Execute write
  763. return self.wireProtocolHandler.remove(self, ns, ops, options, callback);
  764. };
  765. /**
  766. * Get a new cursor
  767. * @method
  768. * @param {string} ns The MongoDB fully qualified namespace (ex: db1.collection1)
  769. * @param {object|Long} cmd Can be either a command returning a cursor or a cursorId
  770. * @param {object} [options] Options for the cursor
  771. * @param {object} [options.batchSize=0] Batchsize for the operation
  772. * @param {array} [options.documents=[]] Initial documents list for cursor
  773. * @param {ReadPreference} [options.readPreference] Specify read preference if command supports it
  774. * @param {Boolean} [options.serializeFunctions=false] Specify if functions on an object should be serialized.
  775. * @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
  776. * @param {ClientSession} [options.session=null] Session to use for the operation
  777. * @param {object} [options.topology] The internal topology of the created cursor
  778. * @returns {Cursor}
  779. */
  780. Server.prototype.cursor = function(ns, cmd, options) {
  781. options = options || {};
  782. const topology = options.topology || this;
  783. // Set up final cursor type
  784. var FinalCursor = options.cursorFactory || this.s.Cursor;
  785. // Return the cursor
  786. return new FinalCursor(this.s.bson, ns, cmd, options, topology, this.s.options);
  787. };
  788. /**
  789. * Logout from a database
  790. * @method
  791. * @param {string} db The db we are logging out from
  792. * @param {authResultCallback} callback A callback function
  793. */
  794. Server.prototype.logout = function(dbName, callback) {
  795. this.s.pool.logout(dbName, callback);
  796. };
  797. /**
  798. * Authenticate using a specified mechanism
  799. * @method
  800. * @param {string} mechanism The Auth mechanism we are invoking
  801. * @param {string} db The db we are invoking the mechanism against
  802. * @param {...object} param Parameters for the specific mechanism
  803. * @param {authResultCallback} callback A callback function
  804. */
  805. Server.prototype.auth = function(mechanism, db) {
  806. var self = this;
  807. if (mechanism === 'default') {
  808. mechanism = getDefaultAuthMechanism(self.ismaster);
  809. }
  810. // Slice all the arguments off
  811. var args = Array.prototype.slice.call(arguments, 0);
  812. // Set the mechanism
  813. args[0] = mechanism;
  814. // Get the callback
  815. var callback = args[args.length - 1];
  816. // If we are not connected or have a disconnectHandler specified
  817. if (disconnectHandler(self, 'auth', db, args, {}, callback)) {
  818. return;
  819. }
  820. // Do not authenticate if we are an arbiter
  821. if (this.lastIsMaster() && this.lastIsMaster().arbiterOnly) {
  822. return callback(null, true);
  823. }
  824. // Apply the arguments to the pool
  825. self.s.pool.auth.apply(self.s.pool, args);
  826. };
  827. /**
  828. * Compare two server instances
  829. * @method
  830. * @param {Server} server Server to compare equality against
  831. * @return {boolean}
  832. */
  833. Server.prototype.equals = function(server) {
  834. if (typeof server === 'string') return this.name.toLowerCase() === server.toLowerCase();
  835. if (server.name) return this.name.toLowerCase() === server.name.toLowerCase();
  836. return false;
  837. };
  838. /**
  839. * All raw connections
  840. * @method
  841. * @return {Connection[]}
  842. */
  843. Server.prototype.connections = function() {
  844. return this.s.pool.allConnections();
  845. };
  846. /**
  847. * Selects a server
  848. * @return {Server}
  849. */
  850. Server.prototype.selectServer = function(selector, options, callback) {
  851. if (typeof selector === 'function' && typeof callback === 'undefined')
  852. (callback = selector), (selector = undefined), (options = {});
  853. if (typeof options === 'function')
  854. (callback = options), (options = selector), (selector = undefined);
  855. callback(null, this);
  856. };
  857. var listeners = ['close', 'error', 'timeout', 'parseError', 'connect'];
  858. /**
  859. * Destroy the server connection
  860. * @method
  861. * @param {boolean} [options.emitClose=false] Emit close event on destroy
  862. * @param {boolean} [options.emitDestroy=false] Emit destroy event on destroy
  863. * @param {boolean} [options.force=false] Force destroy the pool
  864. */
  865. Server.prototype.destroy = function(options) {
  866. if (this._destroyed) return;
  867. options = options || {};
  868. var self = this;
  869. // Set the connections
  870. if (serverAccounting) delete servers[this.id];
  871. // Destroy the monitoring process if any
  872. if (this.monitoringProcessId) {
  873. clearTimeout(this.monitoringProcessId);
  874. }
  875. // No pool, return
  876. if (!self.s.pool) {
  877. this._destroyed = true;
  878. return;
  879. }
  880. // Emit close event
  881. if (options.emitClose) {
  882. self.emit('close', self);
  883. }
  884. // Emit destroy event
  885. if (options.emitDestroy) {
  886. self.emit('destroy', self);
  887. }
  888. // Remove all listeners
  889. listeners.forEach(function(event) {
  890. self.s.pool.removeAllListeners(event);
  891. });
  892. // Emit opening server event
  893. if (self.listeners('serverClosed').length > 0)
  894. self.emit('serverClosed', {
  895. topologyId: self.s.topologyId !== -1 ? self.s.topologyId : self.id,
  896. address: self.name
  897. });
  898. // Emit toplogy opening event if not in topology
  899. if (self.listeners('topologyClosed').length > 0 && !self.s.inTopology) {
  900. self.emit('topologyClosed', { topologyId: self.id });
  901. }
  902. if (self.s.logger.isDebug()) {
  903. self.s.logger.debug(f('destroy called on server %s', self.name));
  904. }
  905. // Destroy the pool
  906. this.s.pool.destroy(options.force);
  907. this._destroyed = true;
  908. };
  909. /**
  910. * A server connect event, used to verify that the connection is up and running
  911. *
  912. * @event Server#connect
  913. * @type {Server}
  914. */
  915. /**
  916. * A server reconnect event, used to verify that the server topology has reconnected
  917. *
  918. * @event Server#reconnect
  919. * @type {Server}
  920. */
  921. /**
  922. * A server opening SDAM monitoring event
  923. *
  924. * @event Server#serverOpening
  925. * @type {object}
  926. */
  927. /**
  928. * A server closed SDAM monitoring event
  929. *
  930. * @event Server#serverClosed
  931. * @type {object}
  932. */
  933. /**
  934. * A server description SDAM change monitoring event
  935. *
  936. * @event Server#serverDescriptionChanged
  937. * @type {object}
  938. */
  939. /**
  940. * A topology open SDAM event
  941. *
  942. * @event Server#topologyOpening
  943. * @type {object}
  944. */
  945. /**
  946. * A topology closed SDAM event
  947. *
  948. * @event Server#topologyClosed
  949. * @type {object}
  950. */
  951. /**
  952. * A topology structure SDAM change event
  953. *
  954. * @event Server#topologyDescriptionChanged
  955. * @type {object}
  956. */
  957. /**
  958. * Server reconnect failed
  959. *
  960. * @event Server#reconnectFailed
  961. * @type {Error}
  962. */
  963. /**
  964. * Server connection pool closed
  965. *
  966. * @event Server#close
  967. * @type {object}
  968. */
  969. /**
  970. * Server connection pool caused an error
  971. *
  972. * @event Server#error
  973. * @type {Error}
  974. */
  975. /**
  976. * Server destroyed was called
  977. *
  978. * @event Server#destroy
  979. * @type {Server}
  980. */
  981. module.exports = Server;