server_description.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.compareTopologyVersion = exports.parseServerType = exports.ServerDescription = void 0;
  4. const bson_1 = require("../bson");
  5. const error_1 = require("../error");
  6. const utils_1 = require("../utils");
  7. const common_1 = require("./common");
  8. const WRITABLE_SERVER_TYPES = new Set([
  9. common_1.ServerType.RSPrimary,
  10. common_1.ServerType.Standalone,
  11. common_1.ServerType.Mongos,
  12. common_1.ServerType.LoadBalancer
  13. ]);
  14. const DATA_BEARING_SERVER_TYPES = new Set([
  15. common_1.ServerType.RSPrimary,
  16. common_1.ServerType.RSSecondary,
  17. common_1.ServerType.Mongos,
  18. common_1.ServerType.Standalone,
  19. common_1.ServerType.LoadBalancer
  20. ]);
  21. /**
  22. * The client's view of a single server, based on the most recent hello outcome.
  23. *
  24. * Internal type, not meant to be directly instantiated
  25. * @public
  26. */
  27. class ServerDescription {
  28. /**
  29. * Create a ServerDescription
  30. * @internal
  31. *
  32. * @param address - The address of the server
  33. * @param hello - An optional hello response for this server
  34. */
  35. constructor(address, hello, options = {}) {
  36. if (address == null || address === '') {
  37. throw new error_1.MongoRuntimeError('ServerDescription must be provided with a non-empty address');
  38. }
  39. this.address =
  40. typeof address === 'string'
  41. ? utils_1.HostAddress.fromString(address).toString() // Use HostAddress to normalize
  42. : address.toString();
  43. this.type = parseServerType(hello, options);
  44. this.hosts = hello?.hosts?.map((host) => host.toLowerCase()) ?? [];
  45. this.passives = hello?.passives?.map((host) => host.toLowerCase()) ?? [];
  46. this.arbiters = hello?.arbiters?.map((host) => host.toLowerCase()) ?? [];
  47. this.tags = hello?.tags ?? {};
  48. this.minWireVersion = hello?.minWireVersion ?? 0;
  49. this.maxWireVersion = hello?.maxWireVersion ?? 0;
  50. this.roundTripTime = options?.roundTripTime ?? -1;
  51. this.lastUpdateTime = (0, utils_1.now)();
  52. this.lastWriteDate = hello?.lastWrite?.lastWriteDate ?? 0;
  53. this.error = options.error ?? null;
  54. // TODO(NODE-2674): Preserve int64 sent from MongoDB
  55. this.topologyVersion = this.error?.topologyVersion ?? hello?.topologyVersion ?? null;
  56. this.setName = hello?.setName ?? null;
  57. this.setVersion = hello?.setVersion ?? null;
  58. this.electionId = hello?.electionId ?? null;
  59. this.logicalSessionTimeoutMinutes = hello?.logicalSessionTimeoutMinutes ?? null;
  60. this.primary = hello?.primary ?? null;
  61. this.me = hello?.me?.toLowerCase() ?? null;
  62. this.$clusterTime = hello?.$clusterTime ?? null;
  63. }
  64. get hostAddress() {
  65. return utils_1.HostAddress.fromString(this.address);
  66. }
  67. get allHosts() {
  68. return this.hosts.concat(this.arbiters).concat(this.passives);
  69. }
  70. /** Is this server available for reads*/
  71. get isReadable() {
  72. return this.type === common_1.ServerType.RSSecondary || this.isWritable;
  73. }
  74. /** Is this server data bearing */
  75. get isDataBearing() {
  76. return DATA_BEARING_SERVER_TYPES.has(this.type);
  77. }
  78. /** Is this server available for writes */
  79. get isWritable() {
  80. return WRITABLE_SERVER_TYPES.has(this.type);
  81. }
  82. get host() {
  83. const chopLength = `:${this.port}`.length;
  84. return this.address.slice(0, -chopLength);
  85. }
  86. get port() {
  87. const port = this.address.split(':').pop();
  88. return port ? Number.parseInt(port, 10) : 27017;
  89. }
  90. /**
  91. * Determines if another `ServerDescription` is equal to this one per the rules defined
  92. * in the {@link https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#serverdescription|SDAM spec}
  93. */
  94. equals(other) {
  95. // Despite using the comparator that would determine a nullish topologyVersion as greater than
  96. // for equality we should only always perform direct equality comparison
  97. const topologyVersionsEqual = this.topologyVersion === other?.topologyVersion ||
  98. compareTopologyVersion(this.topologyVersion, other?.topologyVersion) === 0;
  99. const electionIdsEqual = this.electionId != null && other?.electionId != null
  100. ? (0, utils_1.compareObjectId)(this.electionId, other.electionId) === 0
  101. : this.electionId === other?.electionId;
  102. return (other != null &&
  103. (0, utils_1.errorStrictEqual)(this.error, other.error) &&
  104. this.type === other.type &&
  105. this.minWireVersion === other.minWireVersion &&
  106. (0, utils_1.arrayStrictEqual)(this.hosts, other.hosts) &&
  107. tagsStrictEqual(this.tags, other.tags) &&
  108. this.setName === other.setName &&
  109. this.setVersion === other.setVersion &&
  110. electionIdsEqual &&
  111. this.primary === other.primary &&
  112. this.logicalSessionTimeoutMinutes === other.logicalSessionTimeoutMinutes &&
  113. topologyVersionsEqual);
  114. }
  115. }
  116. exports.ServerDescription = ServerDescription;
  117. // Parses a `hello` message and determines the server type
  118. function parseServerType(hello, options) {
  119. if (options?.loadBalanced) {
  120. return common_1.ServerType.LoadBalancer;
  121. }
  122. if (!hello || !hello.ok) {
  123. return common_1.ServerType.Unknown;
  124. }
  125. if (hello.isreplicaset) {
  126. return common_1.ServerType.RSGhost;
  127. }
  128. if (hello.msg && hello.msg === 'isdbgrid') {
  129. return common_1.ServerType.Mongos;
  130. }
  131. if (hello.setName) {
  132. if (hello.hidden) {
  133. return common_1.ServerType.RSOther;
  134. }
  135. else if (hello.isWritablePrimary) {
  136. return common_1.ServerType.RSPrimary;
  137. }
  138. else if (hello.secondary) {
  139. return common_1.ServerType.RSSecondary;
  140. }
  141. else if (hello.arbiterOnly) {
  142. return common_1.ServerType.RSArbiter;
  143. }
  144. else {
  145. return common_1.ServerType.RSOther;
  146. }
  147. }
  148. return common_1.ServerType.Standalone;
  149. }
  150. exports.parseServerType = parseServerType;
  151. function tagsStrictEqual(tags, tags2) {
  152. const tagsKeys = Object.keys(tags);
  153. const tags2Keys = Object.keys(tags2);
  154. return (tagsKeys.length === tags2Keys.length &&
  155. tagsKeys.every((key) => tags2[key] === tags[key]));
  156. }
  157. /**
  158. * Compares two topology versions.
  159. *
  160. * 1. If the response topologyVersion is unset or the ServerDescription's
  161. * topologyVersion is null, the client MUST assume the response is more recent.
  162. * 1. If the response's topologyVersion.processId is not equal to the
  163. * ServerDescription's, the client MUST assume the response is more recent.
  164. * 1. If the response's topologyVersion.processId is equal to the
  165. * ServerDescription's, the client MUST use the counter field to determine
  166. * which topologyVersion is more recent.
  167. *
  168. * ```ts
  169. * currentTv < newTv === -1
  170. * currentTv === newTv === 0
  171. * currentTv > newTv === 1
  172. * ```
  173. */
  174. function compareTopologyVersion(currentTv, newTv) {
  175. if (currentTv == null || newTv == null) {
  176. return -1;
  177. }
  178. if (!currentTv.processId.equals(newTv.processId)) {
  179. return -1;
  180. }
  181. // TODO(NODE-2674): Preserve int64 sent from MongoDB
  182. const currentCounter = bson_1.Long.isLong(currentTv.counter)
  183. ? currentTv.counter
  184. : bson_1.Long.fromNumber(currentTv.counter);
  185. const newCounter = bson_1.Long.isLong(newTv.counter) ? newTv.counter : bson_1.Long.fromNumber(newTv.counter);
  186. return currentCounter.compare(newCounter);
  187. }
  188. exports.compareTopologyVersion = compareTopologyVersion;
  189. //# sourceMappingURL=server_description.js.map