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_description.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. // An enumeration of server types we know about
  3. const ServerType = {
  4. Standalone: 'Standalone',
  5. Mongos: 'Mongos',
  6. PossiblePrimary: 'PossiblePrimary',
  7. RSPrimary: 'RSPrimary',
  8. RSSecondary: 'RSSecondary',
  9. RSArbiter: 'RSArbiter',
  10. RSOther: 'RSOther',
  11. RSGhost: 'RSGhost',
  12. Unknown: 'Unknown'
  13. };
  14. const WRITABLE_SERVER_TYPES = new Set([
  15. ServerType.RSPrimary,
  16. ServerType.Standalone,
  17. ServerType.Mongos
  18. ]);
  19. const ISMASTER_FIELDS = [
  20. 'minWireVersion',
  21. 'maxWireVersion',
  22. 'me',
  23. 'hosts',
  24. 'passives',
  25. 'arbiters',
  26. 'tags',
  27. 'setName',
  28. 'setVersion',
  29. 'electionId',
  30. 'primary',
  31. 'logicalSessionTimeoutMinutes'
  32. ];
  33. /**
  34. * The client's view of a single server, based on the most recent ismaster outcome.
  35. *
  36. * Internal type, not meant to be directly instantiated
  37. */
  38. class ServerDescription {
  39. /**
  40. * Create a ServerDescription
  41. * @param {String} address The address of the server
  42. * @param {Object} [ismaster] An optional ismaster response for this server
  43. * @param {Object} [options] Optional settings
  44. * @param {Number} [options.roundTripTime] The round trip time to ping this server (in ms)
  45. */
  46. constructor(address, ismaster, options) {
  47. options = options || {};
  48. ismaster = Object.assign(
  49. {
  50. minWireVersion: 0,
  51. maxWireVersion: 0,
  52. hosts: [],
  53. passives: [],
  54. arbiters: [],
  55. tags: []
  56. },
  57. ismaster
  58. );
  59. this.address = address;
  60. this.error = null;
  61. this.roundTripTime = options.roundTripTime || 0;
  62. this.lastUpdateTime = Date.now();
  63. this.lastWriteDate = ismaster.lastWrite ? ismaster.lastWrite.lastWriteDate : null;
  64. this.opTime = ismaster.lastWrite ? ismaster.lastWrite.opTime : null;
  65. this.type = parseServerType(ismaster);
  66. // direct mappings
  67. ISMASTER_FIELDS.forEach(field => {
  68. if (typeof ismaster[field] !== 'undefined') this[field] = ismaster[field];
  69. });
  70. // normalize case for hosts
  71. this.hosts = this.hosts.map(host => host.toLowerCase());
  72. this.passives = this.passives.map(host => host.toLowerCase());
  73. this.arbiters = this.arbiters.map(host => host.toLowerCase());
  74. }
  75. get allHosts() {
  76. return this.hosts.concat(this.arbiters).concat(this.passives);
  77. }
  78. /**
  79. * @return {Boolean} Is this server available for reads
  80. */
  81. get isReadable() {
  82. return this.type === ServerType.RSSecondary || this.isWritable;
  83. }
  84. /**
  85. * @return {Boolean} Is this server available for writes
  86. */
  87. get isWritable() {
  88. return WRITABLE_SERVER_TYPES.has(this.type);
  89. }
  90. }
  91. /**
  92. * Parses an `ismaster` message and determines the server type
  93. *
  94. * @param {Object} ismaster The `ismaster` message to parse
  95. * @return {ServerType}
  96. */
  97. function parseServerType(ismaster) {
  98. if (!ismaster || !ismaster.ok) {
  99. return ServerType.Unknown;
  100. }
  101. if (ismaster.isreplicaset) {
  102. return ServerType.RSGhost;
  103. }
  104. if (ismaster.msg && ismaster.msg === 'isdbgrid') {
  105. return ServerType.Mongos;
  106. }
  107. if (ismaster.setName) {
  108. if (ismaster.hidden) {
  109. return ServerType.RSOther;
  110. } else if (ismaster.ismaster) {
  111. return ServerType.RSPrimary;
  112. } else if (ismaster.secondary) {
  113. return ServerType.RSSecondary;
  114. } else if (ismaster.arbiterOnly) {
  115. return ServerType.RSArbiter;
  116. } else {
  117. return ServerType.RSOther;
  118. }
  119. }
  120. return ServerType.Standalone;
  121. }
  122. module.exports = {
  123. ServerDescription,
  124. ServerType
  125. };