something is working
This commit is contained in:
+56
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports._advanceClusterTime = exports.drainTimerQueue = exports.ServerType = exports.TopologyType = exports.STATE_CONNECTED = exports.STATE_CONNECTING = exports.STATE_CLOSED = exports.STATE_CLOSING = void 0;
|
||||
const timers_1 = require("timers");
|
||||
// shared state names
|
||||
exports.STATE_CLOSING = 'closing';
|
||||
exports.STATE_CLOSED = 'closed';
|
||||
exports.STATE_CONNECTING = 'connecting';
|
||||
exports.STATE_CONNECTED = 'connected';
|
||||
/**
|
||||
* An enumeration of topology types we know about
|
||||
* @public
|
||||
*/
|
||||
exports.TopologyType = Object.freeze({
|
||||
Single: 'Single',
|
||||
ReplicaSetNoPrimary: 'ReplicaSetNoPrimary',
|
||||
ReplicaSetWithPrimary: 'ReplicaSetWithPrimary',
|
||||
Sharded: 'Sharded',
|
||||
Unknown: 'Unknown',
|
||||
LoadBalanced: 'LoadBalanced'
|
||||
});
|
||||
/**
|
||||
* An enumeration of server types we know about
|
||||
* @public
|
||||
*/
|
||||
exports.ServerType = Object.freeze({
|
||||
Standalone: 'Standalone',
|
||||
Mongos: 'Mongos',
|
||||
PossiblePrimary: 'PossiblePrimary',
|
||||
RSPrimary: 'RSPrimary',
|
||||
RSSecondary: 'RSSecondary',
|
||||
RSArbiter: 'RSArbiter',
|
||||
RSOther: 'RSOther',
|
||||
RSGhost: 'RSGhost',
|
||||
Unknown: 'Unknown',
|
||||
LoadBalancer: 'LoadBalancer'
|
||||
});
|
||||
/** @internal */
|
||||
function drainTimerQueue(queue) {
|
||||
queue.forEach(timers_1.clearTimeout);
|
||||
queue.clear();
|
||||
}
|
||||
exports.drainTimerQueue = drainTimerQueue;
|
||||
/** Shared function to determine clusterTime for a given topology or session */
|
||||
function _advanceClusterTime(entity, $clusterTime) {
|
||||
if (entity.clusterTime == null) {
|
||||
entity.clusterTime = $clusterTime;
|
||||
}
|
||||
else {
|
||||
if ($clusterTime.clusterTime.greaterThan(entity.clusterTime.clusterTime)) {
|
||||
entity.clusterTime = $clusterTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
exports._advanceClusterTime = _advanceClusterTime;
|
||||
//# sourceMappingURL=common.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/sdam/common.ts"],"names":[],"mappings":";;;AAAA,mCAAsC;AAMtC,qBAAqB;AACR,QAAA,aAAa,GAAG,SAAS,CAAC;AAC1B,QAAA,YAAY,GAAG,QAAQ,CAAC;AACxB,QAAA,gBAAgB,GAAG,YAAY,CAAC;AAChC,QAAA,eAAe,GAAG,WAAW,CAAC;AAE3C;;;GAGG;AACU,QAAA,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC;IACxC,MAAM,EAAE,QAAQ;IAChB,mBAAmB,EAAE,qBAAqB;IAC1C,qBAAqB,EAAE,uBAAuB;IAC9C,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,YAAY,EAAE,cAAc;CACpB,CAAC,CAAC;AAKZ;;;GAGG;AACU,QAAA,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC;IACtC,UAAU,EAAE,YAAY;IACxB,MAAM,EAAE,QAAQ;IAChB,eAAe,EAAE,iBAAiB;IAClC,SAAS,EAAE,WAAW;IACtB,WAAW,EAAE,aAAa;IAC1B,SAAS,EAAE,WAAW;IACtB,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,SAAS;IAClB,YAAY,EAAE,cAAc;CACpB,CAAC,CAAC;AAQZ,gBAAgB;AAChB,SAAgB,eAAe,CAAC,KAAiB;IAC/C,KAAK,CAAC,OAAO,CAAC,qBAAY,CAAC,CAAC;IAC5B,KAAK,CAAC,KAAK,EAAE,CAAC;AAChB,CAAC;AAHD,0CAGC;AAWD,+EAA+E;AAC/E,SAAgB,mBAAmB,CACjC,MAAgC,EAChC,YAAyB;IAEzB,IAAI,MAAM,CAAC,WAAW,IAAI,IAAI,EAAE;QAC9B,MAAM,CAAC,WAAW,GAAG,YAAY,CAAC;KACnC;SAAM;QACL,IAAI,YAAY,CAAC,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE;YACxE,MAAM,CAAC,WAAW,GAAG,YAAY,CAAC;SACnC;KACF;AACH,CAAC;AAXD,kDAWC"}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ServerHeartbeatFailedEvent = exports.ServerHeartbeatSucceededEvent = exports.ServerHeartbeatStartedEvent = exports.TopologyClosedEvent = exports.TopologyOpeningEvent = exports.TopologyDescriptionChangedEvent = exports.ServerClosedEvent = exports.ServerOpeningEvent = exports.ServerDescriptionChangedEvent = void 0;
|
||||
/**
|
||||
* Emitted when server description changes, but does NOT include changes to the RTT.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class ServerDescriptionChangedEvent {
|
||||
/** @internal */
|
||||
constructor(topologyId, address, previousDescription, newDescription) {
|
||||
this.topologyId = topologyId;
|
||||
this.address = address;
|
||||
this.previousDescription = previousDescription;
|
||||
this.newDescription = newDescription;
|
||||
}
|
||||
}
|
||||
exports.ServerDescriptionChangedEvent = ServerDescriptionChangedEvent;
|
||||
/**
|
||||
* Emitted when server is initialized.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class ServerOpeningEvent {
|
||||
/** @internal */
|
||||
constructor(topologyId, address) {
|
||||
this.topologyId = topologyId;
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
exports.ServerOpeningEvent = ServerOpeningEvent;
|
||||
/**
|
||||
* Emitted when server is closed.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class ServerClosedEvent {
|
||||
/** @internal */
|
||||
constructor(topologyId, address) {
|
||||
this.topologyId = topologyId;
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
exports.ServerClosedEvent = ServerClosedEvent;
|
||||
/**
|
||||
* Emitted when topology description changes.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class TopologyDescriptionChangedEvent {
|
||||
/** @internal */
|
||||
constructor(topologyId, previousDescription, newDescription) {
|
||||
this.topologyId = topologyId;
|
||||
this.previousDescription = previousDescription;
|
||||
this.newDescription = newDescription;
|
||||
}
|
||||
}
|
||||
exports.TopologyDescriptionChangedEvent = TopologyDescriptionChangedEvent;
|
||||
/**
|
||||
* Emitted when topology is initialized.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class TopologyOpeningEvent {
|
||||
/** @internal */
|
||||
constructor(topologyId) {
|
||||
this.topologyId = topologyId;
|
||||
}
|
||||
}
|
||||
exports.TopologyOpeningEvent = TopologyOpeningEvent;
|
||||
/**
|
||||
* Emitted when topology is closed.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class TopologyClosedEvent {
|
||||
/** @internal */
|
||||
constructor(topologyId) {
|
||||
this.topologyId = topologyId;
|
||||
}
|
||||
}
|
||||
exports.TopologyClosedEvent = TopologyClosedEvent;
|
||||
/**
|
||||
* Emitted when the server monitor’s hello command is started - immediately before
|
||||
* the hello command is serialized into raw BSON and written to the socket.
|
||||
*
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class ServerHeartbeatStartedEvent {
|
||||
/** @internal */
|
||||
constructor(connectionId) {
|
||||
this.connectionId = connectionId;
|
||||
}
|
||||
}
|
||||
exports.ServerHeartbeatStartedEvent = ServerHeartbeatStartedEvent;
|
||||
/**
|
||||
* Emitted when the server monitor’s hello succeeds.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class ServerHeartbeatSucceededEvent {
|
||||
/** @internal */
|
||||
constructor(connectionId, duration, reply) {
|
||||
this.connectionId = connectionId;
|
||||
this.duration = duration;
|
||||
this.reply = reply !== null && reply !== void 0 ? reply : {};
|
||||
}
|
||||
}
|
||||
exports.ServerHeartbeatSucceededEvent = ServerHeartbeatSucceededEvent;
|
||||
/**
|
||||
* Emitted when the server monitor’s hello fails, either with an “ok: 0” or a socket exception.
|
||||
* @public
|
||||
* @category Event
|
||||
*/
|
||||
class ServerHeartbeatFailedEvent {
|
||||
/** @internal */
|
||||
constructor(connectionId, duration, failure) {
|
||||
this.connectionId = connectionId;
|
||||
this.duration = duration;
|
||||
this.failure = failure;
|
||||
}
|
||||
}
|
||||
exports.ServerHeartbeatFailedEvent = ServerHeartbeatFailedEvent;
|
||||
//# sourceMappingURL=events.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/sdam/events.ts"],"names":[],"mappings":";;;AAIA;;;;GAIG;AACH,MAAa,6BAA6B;IAUxC,gBAAgB;IAChB,YACE,UAAkB,EAClB,OAAe,EACf,mBAAsC,EACtC,cAAiC;QAEjC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,mBAAmB,GAAG,mBAAmB,CAAC;QAC/C,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;CACF;AAtBD,sEAsBC;AAED;;;;GAIG;AACH,MAAa,kBAAkB;IAM7B,gBAAgB;IAChB,YAAY,UAAkB,EAAE,OAAe;QAC7C,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAXD,gDAWC;AAED;;;;GAIG;AACH,MAAa,iBAAiB;IAM5B,gBAAgB;IAChB,YAAY,UAAkB,EAAE,OAAe;QAC7C,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAXD,8CAWC;AAED;;;;GAIG;AACH,MAAa,+BAA+B;IAQ1C,gBAAgB;IAChB,YACE,UAAkB,EAClB,mBAAwC,EACxC,cAAmC;QAEnC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,mBAAmB,GAAG,mBAAmB,CAAC;QAC/C,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;CACF;AAlBD,0EAkBC;AAED;;;;GAIG;AACH,MAAa,oBAAoB;IAI/B,gBAAgB;IAChB,YAAY,UAAkB;QAC5B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;AARD,oDAQC;AAED;;;;GAIG;AACH,MAAa,mBAAmB;IAI9B,gBAAgB;IAChB,YAAY,UAAkB;QAC5B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;CACF;AARD,kDAQC;AAED;;;;;;GAMG;AACH,MAAa,2BAA2B;IAItC,gBAAgB;IAChB,YAAY,YAAoB;QAC9B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;CACF;AARD,kEAQC;AAED;;;;GAIG;AACH,MAAa,6BAA6B;IAQxC,gBAAgB;IAChB,YAAY,YAAoB,EAAE,QAAgB,EAAE,KAAsB;QACxE,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE,CAAC;IAC3B,CAAC;CACF;AAdD,sEAcC;AAED;;;;GAIG;AACH,MAAa,0BAA0B;IAQrC,gBAAgB;IAChB,YAAY,YAAoB,EAAE,QAAgB,EAAE,OAAc;QAChE,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;CACF;AAdD,gEAcC"}
|
||||
+327
@@ -0,0 +1,327 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.RTTPinger = exports.Monitor = void 0;
|
||||
const timers_1 = require("timers");
|
||||
const bson_1 = require("../bson");
|
||||
const connect_1 = require("../cmap/connect");
|
||||
const connection_1 = require("../cmap/connection");
|
||||
const constants_1 = require("../constants");
|
||||
const error_1 = require("../error");
|
||||
const mongo_types_1 = require("../mongo_types");
|
||||
const utils_1 = require("../utils");
|
||||
const common_1 = require("./common");
|
||||
const events_1 = require("./events");
|
||||
const server_1 = require("./server");
|
||||
/** @internal */
|
||||
const kServer = Symbol('server');
|
||||
/** @internal */
|
||||
const kMonitorId = Symbol('monitorId');
|
||||
/** @internal */
|
||||
const kConnection = Symbol('connection');
|
||||
/** @internal */
|
||||
const kCancellationToken = Symbol('cancellationToken');
|
||||
/** @internal */
|
||||
const kRTTPinger = Symbol('rttPinger');
|
||||
/** @internal */
|
||||
const kRoundTripTime = Symbol('roundTripTime');
|
||||
const STATE_IDLE = 'idle';
|
||||
const STATE_MONITORING = 'monitoring';
|
||||
const stateTransition = (0, utils_1.makeStateMachine)({
|
||||
[common_1.STATE_CLOSING]: [common_1.STATE_CLOSING, STATE_IDLE, common_1.STATE_CLOSED],
|
||||
[common_1.STATE_CLOSED]: [common_1.STATE_CLOSED, STATE_MONITORING],
|
||||
[STATE_IDLE]: [STATE_IDLE, STATE_MONITORING, common_1.STATE_CLOSING],
|
||||
[STATE_MONITORING]: [STATE_MONITORING, STATE_IDLE, common_1.STATE_CLOSING]
|
||||
});
|
||||
const INVALID_REQUEST_CHECK_STATES = new Set([common_1.STATE_CLOSING, common_1.STATE_CLOSED, STATE_MONITORING]);
|
||||
function isInCloseState(monitor) {
|
||||
return monitor.s.state === common_1.STATE_CLOSED || monitor.s.state === common_1.STATE_CLOSING;
|
||||
}
|
||||
/** @internal */
|
||||
class Monitor extends mongo_types_1.TypedEventEmitter {
|
||||
constructor(server, options) {
|
||||
var _a, _b, _c;
|
||||
super();
|
||||
this[kServer] = server;
|
||||
this[kConnection] = undefined;
|
||||
this[kCancellationToken] = new mongo_types_1.CancellationToken();
|
||||
this[kCancellationToken].setMaxListeners(Infinity);
|
||||
this[kMonitorId] = undefined;
|
||||
this.s = {
|
||||
state: common_1.STATE_CLOSED
|
||||
};
|
||||
this.address = server.description.address;
|
||||
this.options = Object.freeze({
|
||||
connectTimeoutMS: (_a = options.connectTimeoutMS) !== null && _a !== void 0 ? _a : 10000,
|
||||
heartbeatFrequencyMS: (_b = options.heartbeatFrequencyMS) !== null && _b !== void 0 ? _b : 10000,
|
||||
minHeartbeatFrequencyMS: (_c = options.minHeartbeatFrequencyMS) !== null && _c !== void 0 ? _c : 500
|
||||
});
|
||||
const cancellationToken = this[kCancellationToken];
|
||||
// TODO: refactor this to pull it directly from the pool, requires new ConnectionPool integration
|
||||
const connectOptions = Object.assign({
|
||||
id: '<monitor>',
|
||||
generation: server.s.pool.generation,
|
||||
connectionType: connection_1.Connection,
|
||||
cancellationToken,
|
||||
hostAddress: server.description.hostAddress
|
||||
}, options,
|
||||
// force BSON serialization options
|
||||
{
|
||||
raw: false,
|
||||
promoteLongs: true,
|
||||
promoteValues: true,
|
||||
promoteBuffers: true
|
||||
});
|
||||
// ensure no authentication is used for monitoring
|
||||
delete connectOptions.credentials;
|
||||
if (connectOptions.autoEncrypter) {
|
||||
delete connectOptions.autoEncrypter;
|
||||
}
|
||||
this.connectOptions = Object.freeze(connectOptions);
|
||||
}
|
||||
get connection() {
|
||||
return this[kConnection];
|
||||
}
|
||||
connect() {
|
||||
if (this.s.state !== common_1.STATE_CLOSED) {
|
||||
return;
|
||||
}
|
||||
// start
|
||||
const heartbeatFrequencyMS = this.options.heartbeatFrequencyMS;
|
||||
const minHeartbeatFrequencyMS = this.options.minHeartbeatFrequencyMS;
|
||||
this[kMonitorId] = (0, utils_1.makeInterruptibleAsyncInterval)(monitorServer(this), {
|
||||
interval: heartbeatFrequencyMS,
|
||||
minInterval: minHeartbeatFrequencyMS,
|
||||
immediate: true
|
||||
});
|
||||
}
|
||||
requestCheck() {
|
||||
var _a;
|
||||
if (INVALID_REQUEST_CHECK_STATES.has(this.s.state)) {
|
||||
return;
|
||||
}
|
||||
(_a = this[kMonitorId]) === null || _a === void 0 ? void 0 : _a.wake();
|
||||
}
|
||||
reset() {
|
||||
const topologyVersion = this[kServer].description.topologyVersion;
|
||||
if (isInCloseState(this) || topologyVersion == null) {
|
||||
return;
|
||||
}
|
||||
stateTransition(this, common_1.STATE_CLOSING);
|
||||
resetMonitorState(this);
|
||||
// restart monitor
|
||||
stateTransition(this, STATE_IDLE);
|
||||
// restart monitoring
|
||||
const heartbeatFrequencyMS = this.options.heartbeatFrequencyMS;
|
||||
const minHeartbeatFrequencyMS = this.options.minHeartbeatFrequencyMS;
|
||||
this[kMonitorId] = (0, utils_1.makeInterruptibleAsyncInterval)(monitorServer(this), {
|
||||
interval: heartbeatFrequencyMS,
|
||||
minInterval: minHeartbeatFrequencyMS
|
||||
});
|
||||
}
|
||||
close() {
|
||||
if (isInCloseState(this)) {
|
||||
return;
|
||||
}
|
||||
stateTransition(this, common_1.STATE_CLOSING);
|
||||
resetMonitorState(this);
|
||||
// close monitor
|
||||
this.emit('close');
|
||||
stateTransition(this, common_1.STATE_CLOSED);
|
||||
}
|
||||
}
|
||||
exports.Monitor = Monitor;
|
||||
function resetMonitorState(monitor) {
|
||||
var _a, _b, _c;
|
||||
(_a = monitor[kMonitorId]) === null || _a === void 0 ? void 0 : _a.stop();
|
||||
monitor[kMonitorId] = undefined;
|
||||
(_b = monitor[kRTTPinger]) === null || _b === void 0 ? void 0 : _b.close();
|
||||
monitor[kRTTPinger] = undefined;
|
||||
monitor[kCancellationToken].emit('cancel');
|
||||
(_c = monitor[kConnection]) === null || _c === void 0 ? void 0 : _c.destroy({ force: true });
|
||||
monitor[kConnection] = undefined;
|
||||
}
|
||||
function checkServer(monitor, callback) {
|
||||
let start = (0, utils_1.now)();
|
||||
monitor.emit(server_1.Server.SERVER_HEARTBEAT_STARTED, new events_1.ServerHeartbeatStartedEvent(monitor.address));
|
||||
function failureHandler(err) {
|
||||
var _a;
|
||||
(_a = monitor[kConnection]) === null || _a === void 0 ? void 0 : _a.destroy({ force: true });
|
||||
monitor[kConnection] = undefined;
|
||||
monitor.emit(server_1.Server.SERVER_HEARTBEAT_FAILED, new events_1.ServerHeartbeatFailedEvent(monitor.address, (0, utils_1.calculateDurationInMs)(start), err));
|
||||
monitor.emit('resetServer', err);
|
||||
monitor.emit('resetConnectionPool');
|
||||
callback(err);
|
||||
}
|
||||
const connection = monitor[kConnection];
|
||||
if (connection && !connection.closed) {
|
||||
const { serverApi, helloOk } = connection;
|
||||
const connectTimeoutMS = monitor.options.connectTimeoutMS;
|
||||
const maxAwaitTimeMS = monitor.options.heartbeatFrequencyMS;
|
||||
const topologyVersion = monitor[kServer].description.topologyVersion;
|
||||
const isAwaitable = topologyVersion != null;
|
||||
const cmd = {
|
||||
[(serverApi === null || serverApi === void 0 ? void 0 : serverApi.version) || helloOk ? 'hello' : constants_1.LEGACY_HELLO_COMMAND]: true,
|
||||
...(isAwaitable && topologyVersion
|
||||
? { maxAwaitTimeMS, topologyVersion: makeTopologyVersion(topologyVersion) }
|
||||
: {})
|
||||
};
|
||||
const options = isAwaitable
|
||||
? {
|
||||
socketTimeoutMS: connectTimeoutMS ? connectTimeoutMS + maxAwaitTimeMS : 0,
|
||||
exhaustAllowed: true
|
||||
}
|
||||
: { socketTimeoutMS: connectTimeoutMS };
|
||||
if (isAwaitable && monitor[kRTTPinger] == null) {
|
||||
monitor[kRTTPinger] = new RTTPinger(monitor[kCancellationToken], Object.assign({ heartbeatFrequencyMS: monitor.options.heartbeatFrequencyMS }, monitor.connectOptions));
|
||||
}
|
||||
connection.command((0, utils_1.ns)('admin.$cmd'), cmd, options, (err, hello) => {
|
||||
var _a;
|
||||
if (err) {
|
||||
return failureHandler(err);
|
||||
}
|
||||
if (!('isWritablePrimary' in hello)) {
|
||||
// Provide hello-style response document.
|
||||
hello.isWritablePrimary = hello[constants_1.LEGACY_HELLO_COMMAND];
|
||||
}
|
||||
const rttPinger = monitor[kRTTPinger];
|
||||
const duration = isAwaitable && rttPinger ? rttPinger.roundTripTime : (0, utils_1.calculateDurationInMs)(start);
|
||||
monitor.emit(server_1.Server.SERVER_HEARTBEAT_SUCCEEDED, new events_1.ServerHeartbeatSucceededEvent(monitor.address, duration, hello));
|
||||
// if we are using the streaming protocol then we immediately issue another `started`
|
||||
// event, otherwise the "check" is complete and return to the main monitor loop
|
||||
if (isAwaitable && hello.topologyVersion) {
|
||||
monitor.emit(server_1.Server.SERVER_HEARTBEAT_STARTED, new events_1.ServerHeartbeatStartedEvent(monitor.address));
|
||||
start = (0, utils_1.now)();
|
||||
}
|
||||
else {
|
||||
(_a = monitor[kRTTPinger]) === null || _a === void 0 ? void 0 : _a.close();
|
||||
monitor[kRTTPinger] = undefined;
|
||||
callback(undefined, hello);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
// connecting does an implicit `hello`
|
||||
(0, connect_1.connect)(monitor.connectOptions, (err, conn) => {
|
||||
if (err) {
|
||||
monitor[kConnection] = undefined;
|
||||
// we already reset the connection pool on network errors in all cases
|
||||
if (!(err instanceof error_1.MongoNetworkError)) {
|
||||
monitor.emit('resetConnectionPool');
|
||||
}
|
||||
failureHandler(err);
|
||||
return;
|
||||
}
|
||||
if (conn) {
|
||||
// Tell the connection that we are using the streaming protocol so that the
|
||||
// connection's message stream will only read the last hello on the buffer.
|
||||
conn.isMonitoringConnection = true;
|
||||
if (isInCloseState(monitor)) {
|
||||
conn.destroy({ force: true });
|
||||
return;
|
||||
}
|
||||
monitor[kConnection] = conn;
|
||||
monitor.emit(server_1.Server.SERVER_HEARTBEAT_SUCCEEDED, new events_1.ServerHeartbeatSucceededEvent(monitor.address, (0, utils_1.calculateDurationInMs)(start), conn.hello));
|
||||
callback(undefined, conn.hello);
|
||||
}
|
||||
});
|
||||
}
|
||||
function monitorServer(monitor) {
|
||||
return (callback) => {
|
||||
stateTransition(monitor, STATE_MONITORING);
|
||||
function done() {
|
||||
if (!isInCloseState(monitor)) {
|
||||
stateTransition(monitor, STATE_IDLE);
|
||||
}
|
||||
callback();
|
||||
}
|
||||
checkServer(monitor, (err, hello) => {
|
||||
if (err) {
|
||||
// otherwise an error occurred on initial discovery, also bail
|
||||
if (monitor[kServer].description.type === common_1.ServerType.Unknown) {
|
||||
monitor.emit('resetServer', err);
|
||||
return done();
|
||||
}
|
||||
}
|
||||
// if the check indicates streaming is supported, immediately reschedule monitoring
|
||||
if (hello && hello.topologyVersion) {
|
||||
(0, timers_1.setTimeout)(() => {
|
||||
var _a;
|
||||
if (!isInCloseState(monitor)) {
|
||||
(_a = monitor[kMonitorId]) === null || _a === void 0 ? void 0 : _a.wake();
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
done();
|
||||
});
|
||||
};
|
||||
}
|
||||
function makeTopologyVersion(tv) {
|
||||
return {
|
||||
processId: tv.processId,
|
||||
// tests mock counter as just number, but in a real situation counter should always be a Long
|
||||
// TODO(NODE-2674): Preserve int64 sent from MongoDB
|
||||
counter: bson_1.Long.isLong(tv.counter) ? tv.counter : bson_1.Long.fromNumber(tv.counter)
|
||||
};
|
||||
}
|
||||
/** @internal */
|
||||
class RTTPinger {
|
||||
constructor(cancellationToken, options) {
|
||||
this[kConnection] = undefined;
|
||||
this[kCancellationToken] = cancellationToken;
|
||||
this[kRoundTripTime] = 0;
|
||||
this.closed = false;
|
||||
const heartbeatFrequencyMS = options.heartbeatFrequencyMS;
|
||||
this[kMonitorId] = (0, timers_1.setTimeout)(() => measureRoundTripTime(this, options), heartbeatFrequencyMS);
|
||||
}
|
||||
get roundTripTime() {
|
||||
return this[kRoundTripTime];
|
||||
}
|
||||
close() {
|
||||
var _a;
|
||||
this.closed = true;
|
||||
(0, timers_1.clearTimeout)(this[kMonitorId]);
|
||||
(_a = this[kConnection]) === null || _a === void 0 ? void 0 : _a.destroy({ force: true });
|
||||
this[kConnection] = undefined;
|
||||
}
|
||||
}
|
||||
exports.RTTPinger = RTTPinger;
|
||||
function measureRoundTripTime(rttPinger, options) {
|
||||
const start = (0, utils_1.now)();
|
||||
options.cancellationToken = rttPinger[kCancellationToken];
|
||||
const heartbeatFrequencyMS = options.heartbeatFrequencyMS;
|
||||
if (rttPinger.closed) {
|
||||
return;
|
||||
}
|
||||
function measureAndReschedule(conn) {
|
||||
if (rttPinger.closed) {
|
||||
conn === null || conn === void 0 ? void 0 : conn.destroy({ force: true });
|
||||
return;
|
||||
}
|
||||
if (rttPinger[kConnection] == null) {
|
||||
rttPinger[kConnection] = conn;
|
||||
}
|
||||
rttPinger[kRoundTripTime] = (0, utils_1.calculateDurationInMs)(start);
|
||||
rttPinger[kMonitorId] = (0, timers_1.setTimeout)(() => measureRoundTripTime(rttPinger, options), heartbeatFrequencyMS);
|
||||
}
|
||||
const connection = rttPinger[kConnection];
|
||||
if (connection == null) {
|
||||
(0, connect_1.connect)(options, (err, conn) => {
|
||||
if (err) {
|
||||
rttPinger[kConnection] = undefined;
|
||||
rttPinger[kRoundTripTime] = 0;
|
||||
return;
|
||||
}
|
||||
measureAndReschedule(conn);
|
||||
});
|
||||
return;
|
||||
}
|
||||
connection.command((0, utils_1.ns)('admin.$cmd'), { [constants_1.LEGACY_HELLO_COMMAND]: 1 }, undefined, err => {
|
||||
if (err) {
|
||||
rttPinger[kConnection] = undefined;
|
||||
rttPinger[kRoundTripTime] = 0;
|
||||
return;
|
||||
}
|
||||
measureAndReschedule();
|
||||
});
|
||||
}
|
||||
//# sourceMappingURL=monitor.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+345
@@ -0,0 +1,345 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Server = void 0;
|
||||
const connection_1 = require("../cmap/connection");
|
||||
const connection_pool_1 = require("../cmap/connection_pool");
|
||||
const constants_1 = require("../constants");
|
||||
const error_1 = require("../error");
|
||||
const logger_1 = require("../logger");
|
||||
const mongo_types_1 = require("../mongo_types");
|
||||
const transactions_1 = require("../transactions");
|
||||
const utils_1 = require("../utils");
|
||||
const common_1 = require("./common");
|
||||
const monitor_1 = require("./monitor");
|
||||
const server_description_1 = require("./server_description");
|
||||
const stateTransition = (0, utils_1.makeStateMachine)({
|
||||
[common_1.STATE_CLOSED]: [common_1.STATE_CLOSED, common_1.STATE_CONNECTING],
|
||||
[common_1.STATE_CONNECTING]: [common_1.STATE_CONNECTING, common_1.STATE_CLOSING, common_1.STATE_CONNECTED, common_1.STATE_CLOSED],
|
||||
[common_1.STATE_CONNECTED]: [common_1.STATE_CONNECTED, common_1.STATE_CLOSING, common_1.STATE_CLOSED],
|
||||
[common_1.STATE_CLOSING]: [common_1.STATE_CLOSING, common_1.STATE_CLOSED]
|
||||
});
|
||||
/** @internal */
|
||||
const kMonitor = Symbol('monitor');
|
||||
/** @internal */
|
||||
class Server extends mongo_types_1.TypedEventEmitter {
|
||||
/**
|
||||
* Create a server
|
||||
*/
|
||||
constructor(topology, description, options) {
|
||||
super();
|
||||
this.serverApi = options.serverApi;
|
||||
const poolOptions = { hostAddress: description.hostAddress, ...options };
|
||||
this.s = {
|
||||
description,
|
||||
options,
|
||||
logger: new logger_1.Logger('Server'),
|
||||
state: common_1.STATE_CLOSED,
|
||||
topology,
|
||||
pool: new connection_pool_1.ConnectionPool(poolOptions),
|
||||
operationCount: 0
|
||||
};
|
||||
for (const event of [...constants_1.CMAP_EVENTS, ...constants_1.APM_EVENTS]) {
|
||||
this.s.pool.on(event, (e) => this.emit(event, e));
|
||||
}
|
||||
this.s.pool.on(connection_1.Connection.CLUSTER_TIME_RECEIVED, (clusterTime) => {
|
||||
this.clusterTime = clusterTime;
|
||||
});
|
||||
if (this.loadBalanced) {
|
||||
this[kMonitor] = null;
|
||||
// monitoring is disabled in load balancing mode
|
||||
return;
|
||||
}
|
||||
// create the monitor
|
||||
// TODO(NODE-4144): Remove new variable for type narrowing
|
||||
const monitor = new monitor_1.Monitor(this, this.s.options);
|
||||
this[kMonitor] = monitor;
|
||||
for (const event of constants_1.HEARTBEAT_EVENTS) {
|
||||
monitor.on(event, (e) => this.emit(event, e));
|
||||
}
|
||||
monitor.on('resetConnectionPool', () => {
|
||||
this.s.pool.clear();
|
||||
});
|
||||
monitor.on('resetServer', (error) => markServerUnknown(this, error));
|
||||
monitor.on(Server.SERVER_HEARTBEAT_SUCCEEDED, (event) => {
|
||||
this.emit(Server.DESCRIPTION_RECEIVED, new server_description_1.ServerDescription(this.description.hostAddress, event.reply, {
|
||||
roundTripTime: calculateRoundTripTime(this.description.roundTripTime, event.duration)
|
||||
}));
|
||||
if (this.s.state === common_1.STATE_CONNECTING) {
|
||||
stateTransition(this, common_1.STATE_CONNECTED);
|
||||
this.emit(Server.CONNECT, this);
|
||||
}
|
||||
});
|
||||
}
|
||||
get clusterTime() {
|
||||
return this.s.topology.clusterTime;
|
||||
}
|
||||
set clusterTime(clusterTime) {
|
||||
this.s.topology.clusterTime = clusterTime;
|
||||
}
|
||||
get description() {
|
||||
return this.s.description;
|
||||
}
|
||||
get name() {
|
||||
return this.s.description.address;
|
||||
}
|
||||
get autoEncrypter() {
|
||||
if (this.s.options && this.s.options.autoEncrypter) {
|
||||
return this.s.options.autoEncrypter;
|
||||
}
|
||||
return;
|
||||
}
|
||||
get loadBalanced() {
|
||||
return this.s.topology.description.type === common_1.TopologyType.LoadBalanced;
|
||||
}
|
||||
/**
|
||||
* Initiate server connect
|
||||
*/
|
||||
connect() {
|
||||
var _a;
|
||||
if (this.s.state !== common_1.STATE_CLOSED) {
|
||||
return;
|
||||
}
|
||||
stateTransition(this, common_1.STATE_CONNECTING);
|
||||
// If in load balancer mode we automatically set the server to
|
||||
// a load balancer. It never transitions out of this state and
|
||||
// has no monitor.
|
||||
if (!this.loadBalanced) {
|
||||
(_a = this[kMonitor]) === null || _a === void 0 ? void 0 : _a.connect();
|
||||
}
|
||||
else {
|
||||
stateTransition(this, common_1.STATE_CONNECTED);
|
||||
this.emit(Server.CONNECT, this);
|
||||
}
|
||||
}
|
||||
/** Destroy the server connection */
|
||||
destroy(options, callback) {
|
||||
var _a;
|
||||
if (typeof options === 'function')
|
||||
(callback = options), (options = {});
|
||||
options = Object.assign({}, { force: false }, options);
|
||||
if (this.s.state === common_1.STATE_CLOSED) {
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
return;
|
||||
}
|
||||
stateTransition(this, common_1.STATE_CLOSING);
|
||||
if (!this.loadBalanced) {
|
||||
(_a = this[kMonitor]) === null || _a === void 0 ? void 0 : _a.close();
|
||||
}
|
||||
this.s.pool.close(options, err => {
|
||||
stateTransition(this, common_1.STATE_CLOSED);
|
||||
this.emit('closed');
|
||||
if (typeof callback === 'function') {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Immediately schedule monitoring of this server. If there already an attempt being made
|
||||
* this will be a no-op.
|
||||
*/
|
||||
requestCheck() {
|
||||
var _a;
|
||||
if (!this.loadBalanced) {
|
||||
(_a = this[kMonitor]) === null || _a === void 0 ? void 0 : _a.requestCheck();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Execute a command
|
||||
* @internal
|
||||
*/
|
||||
command(ns, cmd, options, callback) {
|
||||
if (callback == null) {
|
||||
throw new error_1.MongoInvalidArgumentError('Callback must be provided');
|
||||
}
|
||||
if (ns.db == null || typeof ns === 'string') {
|
||||
throw new error_1.MongoInvalidArgumentError('Namespace must not be a string');
|
||||
}
|
||||
if (this.s.state === common_1.STATE_CLOSING || this.s.state === common_1.STATE_CLOSED) {
|
||||
callback(new error_1.MongoServerClosedError());
|
||||
return;
|
||||
}
|
||||
// Clone the options
|
||||
const finalOptions = Object.assign({}, options, { wireProtocolCommand: false });
|
||||
// There are cases where we need to flag the read preference not to get sent in
|
||||
// the command, such as pre-5.0 servers attempting to perform an aggregate write
|
||||
// with a non-primary read preference. In this case the effective read preference
|
||||
// (primary) is not the same as the provided and must be removed completely.
|
||||
if (finalOptions.omitReadPreference) {
|
||||
delete finalOptions.readPreference;
|
||||
}
|
||||
// error if collation not supported
|
||||
if ((0, utils_1.collationNotSupported)(this, cmd)) {
|
||||
callback(new error_1.MongoCompatibilityError(`Server ${this.name} does not support collation`));
|
||||
return;
|
||||
}
|
||||
const session = finalOptions.session;
|
||||
const conn = session === null || session === void 0 ? void 0 : session.pinnedConnection;
|
||||
// NOTE: This is a hack! We can't retrieve the connections used for executing an operation
|
||||
// (and prevent them from being checked back in) at the point of operation execution.
|
||||
// This should be considered as part of the work for NODE-2882
|
||||
// NOTE:
|
||||
// When incrementing operation count, it's important that we increment it before we
|
||||
// attempt to check out a connection from the pool. This ensures that operations that
|
||||
// are waiting for a connection are included in the operation count. Load balanced
|
||||
// mode will only ever have a single server, so the operation count doesn't matter.
|
||||
// Incrementing the operation count above the logic to handle load balanced mode would
|
||||
// require special logic to decrement it again, or would double increment (the load
|
||||
// balanced code makes a recursive call). Instead, we increment the count after this
|
||||
// check.
|
||||
if (this.loadBalanced && session && conn == null && isPinnableCommand(cmd, session)) {
|
||||
this.s.pool.checkOut((err, checkedOut) => {
|
||||
if (err || checkedOut == null) {
|
||||
if (callback)
|
||||
return callback(err);
|
||||
return;
|
||||
}
|
||||
session.pin(checkedOut);
|
||||
this.command(ns, cmd, finalOptions, callback);
|
||||
});
|
||||
return;
|
||||
}
|
||||
this.s.operationCount += 1;
|
||||
this.s.pool.withConnection(conn, (err, conn, cb) => {
|
||||
if (err || !conn) {
|
||||
this.s.operationCount -= 1;
|
||||
markServerUnknown(this, err);
|
||||
return cb(err);
|
||||
}
|
||||
conn.command(ns, cmd, finalOptions, makeOperationHandler(this, conn, cmd, finalOptions, (error, response) => {
|
||||
this.s.operationCount -= 1;
|
||||
cb(error, response);
|
||||
}));
|
||||
}, callback);
|
||||
}
|
||||
}
|
||||
exports.Server = Server;
|
||||
/** @event */
|
||||
Server.SERVER_HEARTBEAT_STARTED = constants_1.SERVER_HEARTBEAT_STARTED;
|
||||
/** @event */
|
||||
Server.SERVER_HEARTBEAT_SUCCEEDED = constants_1.SERVER_HEARTBEAT_SUCCEEDED;
|
||||
/** @event */
|
||||
Server.SERVER_HEARTBEAT_FAILED = constants_1.SERVER_HEARTBEAT_FAILED;
|
||||
/** @event */
|
||||
Server.CONNECT = constants_1.CONNECT;
|
||||
/** @event */
|
||||
Server.DESCRIPTION_RECEIVED = constants_1.DESCRIPTION_RECEIVED;
|
||||
/** @event */
|
||||
Server.CLOSED = constants_1.CLOSED;
|
||||
/** @event */
|
||||
Server.ENDED = constants_1.ENDED;
|
||||
function calculateRoundTripTime(oldRtt, duration) {
|
||||
if (oldRtt === -1) {
|
||||
return duration;
|
||||
}
|
||||
const alpha = 0.2;
|
||||
return alpha * duration + (1 - alpha) * oldRtt;
|
||||
}
|
||||
function markServerUnknown(server, error) {
|
||||
var _a;
|
||||
// Load balancer servers can never be marked unknown.
|
||||
if (server.loadBalanced) {
|
||||
return;
|
||||
}
|
||||
if (error instanceof error_1.MongoNetworkError && !(error instanceof error_1.MongoNetworkTimeoutError)) {
|
||||
(_a = server[kMonitor]) === null || _a === void 0 ? void 0 : _a.reset();
|
||||
}
|
||||
server.emit(Server.DESCRIPTION_RECEIVED, new server_description_1.ServerDescription(server.description.hostAddress, undefined, { error }));
|
||||
}
|
||||
function isPinnableCommand(cmd, session) {
|
||||
if (session) {
|
||||
return (session.inTransaction() ||
|
||||
'aggregate' in cmd ||
|
||||
'find' in cmd ||
|
||||
'getMore' in cmd ||
|
||||
'listCollections' in cmd ||
|
||||
'listIndexes' in cmd);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function connectionIsStale(pool, connection) {
|
||||
if (connection.serviceId) {
|
||||
return (connection.generation !== pool.serviceGenerations.get(connection.serviceId.toHexString()));
|
||||
}
|
||||
return connection.generation !== pool.generation;
|
||||
}
|
||||
function shouldHandleStateChangeError(server, err) {
|
||||
const etv = err.topologyVersion;
|
||||
const stv = server.description.topologyVersion;
|
||||
return (0, server_description_1.compareTopologyVersion)(stv, etv) < 0;
|
||||
}
|
||||
function inActiveTransaction(session, cmd) {
|
||||
return session && session.inTransaction() && !(0, transactions_1.isTransactionCommand)(cmd);
|
||||
}
|
||||
/** this checks the retryWrites option passed down from the client options, it
|
||||
* does not check if the server supports retryable writes */
|
||||
function isRetryableWritesEnabled(topology) {
|
||||
return topology.s.options.retryWrites !== false;
|
||||
}
|
||||
function makeOperationHandler(server, connection, cmd, options, callback) {
|
||||
const session = options === null || options === void 0 ? void 0 : options.session;
|
||||
return function handleOperationResult(error, result) {
|
||||
if (result != null) {
|
||||
return callback(undefined, result);
|
||||
}
|
||||
if (!error) {
|
||||
return callback(new error_1.MongoUnexpectedServerResponseError('Empty response with no error'));
|
||||
}
|
||||
if (!(error instanceof error_1.MongoError)) {
|
||||
// Node.js or some other error we have not special handling for
|
||||
return callback(error);
|
||||
}
|
||||
if (connectionIsStale(server.s.pool, connection)) {
|
||||
return callback(error);
|
||||
}
|
||||
if (error instanceof error_1.MongoNetworkError) {
|
||||
if (session && !session.hasEnded && session.serverSession) {
|
||||
session.serverSession.isDirty = true;
|
||||
}
|
||||
// inActiveTransaction check handles commit and abort.
|
||||
if (inActiveTransaction(session, cmd) &&
|
||||
!error.hasErrorLabel(error_1.MongoErrorLabel.TransientTransactionError)) {
|
||||
error.addErrorLabel(error_1.MongoErrorLabel.TransientTransactionError);
|
||||
}
|
||||
if ((isRetryableWritesEnabled(server.s.topology) || (0, transactions_1.isTransactionCommand)(cmd)) &&
|
||||
(0, utils_1.supportsRetryableWrites)(server) &&
|
||||
!inActiveTransaction(session, cmd)) {
|
||||
error.addErrorLabel(error_1.MongoErrorLabel.RetryableWriteError);
|
||||
}
|
||||
if (!(error instanceof error_1.MongoNetworkTimeoutError) || (0, error_1.isNetworkErrorBeforeHandshake)(error)) {
|
||||
// In load balanced mode we never mark the server as unknown and always
|
||||
// clear for the specific service id.
|
||||
server.s.pool.clear(connection.serviceId);
|
||||
if (!server.loadBalanced) {
|
||||
markServerUnknown(server, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ((isRetryableWritesEnabled(server.s.topology) || (0, transactions_1.isTransactionCommand)(cmd)) &&
|
||||
(0, error_1.needsRetryableWriteLabel)(error, (0, utils_1.maxWireVersion)(server)) &&
|
||||
!inActiveTransaction(session, cmd)) {
|
||||
error.addErrorLabel(error_1.MongoErrorLabel.RetryableWriteError);
|
||||
}
|
||||
if ((0, error_1.isSDAMUnrecoverableError)(error)) {
|
||||
if (shouldHandleStateChangeError(server, error)) {
|
||||
if ((0, utils_1.maxWireVersion)(server) <= 7 || (0, error_1.isNodeShuttingDownError)(error)) {
|
||||
server.s.pool.clear(connection.serviceId);
|
||||
}
|
||||
if (!server.loadBalanced) {
|
||||
markServerUnknown(server, error);
|
||||
process.nextTick(() => server.requestCheck());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (session &&
|
||||
session.isPinned &&
|
||||
error.hasErrorLabel(error_1.MongoErrorLabel.TransientTransactionError)) {
|
||||
session.unpin({ force: true });
|
||||
}
|
||||
return callback(error);
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=server.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+190
@@ -0,0 +1,190 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.compareTopologyVersion = exports.parseServerType = exports.ServerDescription = void 0;
|
||||
const bson_1 = require("../bson");
|
||||
const error_1 = require("../error");
|
||||
const utils_1 = require("../utils");
|
||||
const common_1 = require("./common");
|
||||
const WRITABLE_SERVER_TYPES = new Set([
|
||||
common_1.ServerType.RSPrimary,
|
||||
common_1.ServerType.Standalone,
|
||||
common_1.ServerType.Mongos,
|
||||
common_1.ServerType.LoadBalancer
|
||||
]);
|
||||
const DATA_BEARING_SERVER_TYPES = new Set([
|
||||
common_1.ServerType.RSPrimary,
|
||||
common_1.ServerType.RSSecondary,
|
||||
common_1.ServerType.Mongos,
|
||||
common_1.ServerType.Standalone,
|
||||
common_1.ServerType.LoadBalancer
|
||||
]);
|
||||
/**
|
||||
* The client's view of a single server, based on the most recent hello outcome.
|
||||
*
|
||||
* Internal type, not meant to be directly instantiated
|
||||
* @public
|
||||
*/
|
||||
class ServerDescription {
|
||||
/**
|
||||
* Create a ServerDescription
|
||||
* @internal
|
||||
*
|
||||
* @param address - The address of the server
|
||||
* @param hello - An optional hello response for this server
|
||||
*/
|
||||
constructor(address, hello, options = {}) {
|
||||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
|
||||
if (address == null || address === '') {
|
||||
throw new error_1.MongoRuntimeError('ServerDescription must be provided with a non-empty address');
|
||||
}
|
||||
this.address =
|
||||
typeof address === 'string'
|
||||
? utils_1.HostAddress.fromString(address).toString(false) // Use HostAddress to normalize
|
||||
: address.toString(false);
|
||||
this.type = parseServerType(hello, options);
|
||||
this.hosts = (_b = (_a = hello === null || hello === void 0 ? void 0 : hello.hosts) === null || _a === void 0 ? void 0 : _a.map((host) => host.toLowerCase())) !== null && _b !== void 0 ? _b : [];
|
||||
this.passives = (_d = (_c = hello === null || hello === void 0 ? void 0 : hello.passives) === null || _c === void 0 ? void 0 : _c.map((host) => host.toLowerCase())) !== null && _d !== void 0 ? _d : [];
|
||||
this.arbiters = (_f = (_e = hello === null || hello === void 0 ? void 0 : hello.arbiters) === null || _e === void 0 ? void 0 : _e.map((host) => host.toLowerCase())) !== null && _f !== void 0 ? _f : [];
|
||||
this.tags = (_g = hello === null || hello === void 0 ? void 0 : hello.tags) !== null && _g !== void 0 ? _g : {};
|
||||
this.minWireVersion = (_h = hello === null || hello === void 0 ? void 0 : hello.minWireVersion) !== null && _h !== void 0 ? _h : 0;
|
||||
this.maxWireVersion = (_j = hello === null || hello === void 0 ? void 0 : hello.maxWireVersion) !== null && _j !== void 0 ? _j : 0;
|
||||
this.roundTripTime = (_k = options === null || options === void 0 ? void 0 : options.roundTripTime) !== null && _k !== void 0 ? _k : -1;
|
||||
this.lastUpdateTime = (0, utils_1.now)();
|
||||
this.lastWriteDate = (_m = (_l = hello === null || hello === void 0 ? void 0 : hello.lastWrite) === null || _l === void 0 ? void 0 : _l.lastWriteDate) !== null && _m !== void 0 ? _m : 0;
|
||||
this.error = (_o = options.error) !== null && _o !== void 0 ? _o : null;
|
||||
// TODO(NODE-2674): Preserve int64 sent from MongoDB
|
||||
this.topologyVersion = (_r = (_q = (_p = this.error) === null || _p === void 0 ? void 0 : _p.topologyVersion) !== null && _q !== void 0 ? _q : hello === null || hello === void 0 ? void 0 : hello.topologyVersion) !== null && _r !== void 0 ? _r : null;
|
||||
this.setName = (_s = hello === null || hello === void 0 ? void 0 : hello.setName) !== null && _s !== void 0 ? _s : null;
|
||||
this.setVersion = (_t = hello === null || hello === void 0 ? void 0 : hello.setVersion) !== null && _t !== void 0 ? _t : null;
|
||||
this.electionId = (_u = hello === null || hello === void 0 ? void 0 : hello.electionId) !== null && _u !== void 0 ? _u : null;
|
||||
this.logicalSessionTimeoutMinutes = (_v = hello === null || hello === void 0 ? void 0 : hello.logicalSessionTimeoutMinutes) !== null && _v !== void 0 ? _v : null;
|
||||
this.primary = (_w = hello === null || hello === void 0 ? void 0 : hello.primary) !== null && _w !== void 0 ? _w : null;
|
||||
this.me = (_y = (_x = hello === null || hello === void 0 ? void 0 : hello.me) === null || _x === void 0 ? void 0 : _x.toLowerCase()) !== null && _y !== void 0 ? _y : null;
|
||||
this.$clusterTime = (_z = hello === null || hello === void 0 ? void 0 : hello.$clusterTime) !== null && _z !== void 0 ? _z : null;
|
||||
}
|
||||
get hostAddress() {
|
||||
return utils_1.HostAddress.fromString(this.address);
|
||||
}
|
||||
get allHosts() {
|
||||
return this.hosts.concat(this.arbiters).concat(this.passives);
|
||||
}
|
||||
/** Is this server available for reads*/
|
||||
get isReadable() {
|
||||
return this.type === common_1.ServerType.RSSecondary || this.isWritable;
|
||||
}
|
||||
/** Is this server data bearing */
|
||||
get isDataBearing() {
|
||||
return DATA_BEARING_SERVER_TYPES.has(this.type);
|
||||
}
|
||||
/** Is this server available for writes */
|
||||
get isWritable() {
|
||||
return WRITABLE_SERVER_TYPES.has(this.type);
|
||||
}
|
||||
get host() {
|
||||
const chopLength = `:${this.port}`.length;
|
||||
return this.address.slice(0, -chopLength);
|
||||
}
|
||||
get port() {
|
||||
const port = this.address.split(':').pop();
|
||||
return port ? Number.parseInt(port, 10) : 27017;
|
||||
}
|
||||
/**
|
||||
* Determines if another `ServerDescription` is equal to this one per the rules defined
|
||||
* in the {@link https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#serverdescription|SDAM spec}
|
||||
*/
|
||||
equals(other) {
|
||||
// Despite using the comparator that would determine a nullish topologyVersion as greater than
|
||||
// for equality we should only always perform direct equality comparison
|
||||
const topologyVersionsEqual = this.topologyVersion === (other === null || other === void 0 ? void 0 : other.topologyVersion) ||
|
||||
compareTopologyVersion(this.topologyVersion, other === null || other === void 0 ? void 0 : other.topologyVersion) === 0;
|
||||
const electionIdsEqual = this.electionId != null && (other === null || other === void 0 ? void 0 : other.electionId) != null
|
||||
? (0, utils_1.compareObjectId)(this.electionId, other.electionId) === 0
|
||||
: this.electionId === (other === null || other === void 0 ? void 0 : other.electionId);
|
||||
return (other != null &&
|
||||
(0, utils_1.errorStrictEqual)(this.error, other.error) &&
|
||||
this.type === other.type &&
|
||||
this.minWireVersion === other.minWireVersion &&
|
||||
(0, utils_1.arrayStrictEqual)(this.hosts, other.hosts) &&
|
||||
tagsStrictEqual(this.tags, other.tags) &&
|
||||
this.setName === other.setName &&
|
||||
this.setVersion === other.setVersion &&
|
||||
electionIdsEqual &&
|
||||
this.primary === other.primary &&
|
||||
this.logicalSessionTimeoutMinutes === other.logicalSessionTimeoutMinutes &&
|
||||
topologyVersionsEqual);
|
||||
}
|
||||
}
|
||||
exports.ServerDescription = ServerDescription;
|
||||
// Parses a `hello` message and determines the server type
|
||||
function parseServerType(hello, options) {
|
||||
if (options === null || options === void 0 ? void 0 : options.loadBalanced) {
|
||||
return common_1.ServerType.LoadBalancer;
|
||||
}
|
||||
if (!hello || !hello.ok) {
|
||||
return common_1.ServerType.Unknown;
|
||||
}
|
||||
if (hello.isreplicaset) {
|
||||
return common_1.ServerType.RSGhost;
|
||||
}
|
||||
if (hello.msg && hello.msg === 'isdbgrid') {
|
||||
return common_1.ServerType.Mongos;
|
||||
}
|
||||
if (hello.setName) {
|
||||
if (hello.hidden) {
|
||||
return common_1.ServerType.RSOther;
|
||||
}
|
||||
else if (hello.isWritablePrimary) {
|
||||
return common_1.ServerType.RSPrimary;
|
||||
}
|
||||
else if (hello.secondary) {
|
||||
return common_1.ServerType.RSSecondary;
|
||||
}
|
||||
else if (hello.arbiterOnly) {
|
||||
return common_1.ServerType.RSArbiter;
|
||||
}
|
||||
else {
|
||||
return common_1.ServerType.RSOther;
|
||||
}
|
||||
}
|
||||
return common_1.ServerType.Standalone;
|
||||
}
|
||||
exports.parseServerType = parseServerType;
|
||||
function tagsStrictEqual(tags, tags2) {
|
||||
const tagsKeys = Object.keys(tags);
|
||||
const tags2Keys = Object.keys(tags2);
|
||||
return (tagsKeys.length === tags2Keys.length &&
|
||||
tagsKeys.every((key) => tags2[key] === tags[key]));
|
||||
}
|
||||
/**
|
||||
* Compares two topology versions.
|
||||
*
|
||||
* 1. If the response topologyVersion is unset or the ServerDescription's
|
||||
* topologyVersion is null, the client MUST assume the response is more recent.
|
||||
* 1. If the response's topologyVersion.processId is not equal to the
|
||||
* ServerDescription's, the client MUST assume the response is more recent.
|
||||
* 1. If the response's topologyVersion.processId is equal to the
|
||||
* ServerDescription's, the client MUST use the counter field to determine
|
||||
* which topologyVersion is more recent.
|
||||
*
|
||||
* ```ts
|
||||
* currentTv < newTv === -1
|
||||
* currentTv === newTv === 0
|
||||
* currentTv > newTv === 1
|
||||
* ```
|
||||
*/
|
||||
function compareTopologyVersion(currentTv, newTv) {
|
||||
if (currentTv == null || newTv == null) {
|
||||
return -1;
|
||||
}
|
||||
if (!currentTv.processId.equals(newTv.processId)) {
|
||||
return -1;
|
||||
}
|
||||
// TODO(NODE-2674): Preserve int64 sent from MongoDB
|
||||
const currentCounter = bson_1.Long.isLong(currentTv.counter)
|
||||
? currentTv.counter
|
||||
: bson_1.Long.fromNumber(currentTv.counter);
|
||||
const newCounter = bson_1.Long.isLong(newTv.counter) ? newTv.counter : bson_1.Long.fromNumber(newTv.counter);
|
||||
return currentCounter.compare(newCounter);
|
||||
}
|
||||
exports.compareTopologyVersion = compareTopologyVersion;
|
||||
//# sourceMappingURL=server_description.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+228
@@ -0,0 +1,228 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.readPreferenceServerSelector = exports.secondaryWritableServerSelector = exports.sameServerSelector = exports.writableServerSelector = exports.MIN_SECONDARY_WRITE_WIRE_VERSION = void 0;
|
||||
const error_1 = require("../error");
|
||||
const read_preference_1 = require("../read_preference");
|
||||
const common_1 = require("./common");
|
||||
// max staleness constants
|
||||
const IDLE_WRITE_PERIOD = 10000;
|
||||
const SMALLEST_MAX_STALENESS_SECONDS = 90;
|
||||
// Minimum version to try writes on secondaries.
|
||||
exports.MIN_SECONDARY_WRITE_WIRE_VERSION = 13;
|
||||
/**
|
||||
* Returns a server selector that selects for writable servers
|
||||
*/
|
||||
function writableServerSelector() {
|
||||
return (topologyDescription, servers) => latencyWindowReducer(topologyDescription, servers.filter((s) => s.isWritable));
|
||||
}
|
||||
exports.writableServerSelector = writableServerSelector;
|
||||
/**
|
||||
* The purpose of this selector is to select the same server, only
|
||||
* if it is in a state that it can have commands sent to it.
|
||||
*/
|
||||
function sameServerSelector(description) {
|
||||
return (topologyDescription, servers) => {
|
||||
if (!description)
|
||||
return [];
|
||||
// Filter the servers to match the provided description only if
|
||||
// the type is not unknown.
|
||||
return servers.filter(sd => {
|
||||
return sd.address === description.address && sd.type !== common_1.ServerType.Unknown;
|
||||
});
|
||||
};
|
||||
}
|
||||
exports.sameServerSelector = sameServerSelector;
|
||||
/**
|
||||
* Returns a server selector that uses a read preference to select a
|
||||
* server potentially for a write on a secondary.
|
||||
*/
|
||||
function secondaryWritableServerSelector(wireVersion, readPreference) {
|
||||
// If server version < 5.0, read preference always primary.
|
||||
// If server version >= 5.0...
|
||||
// - If read preference is supplied, use that.
|
||||
// - If no read preference is supplied, use primary.
|
||||
if (!readPreference ||
|
||||
!wireVersion ||
|
||||
(wireVersion && wireVersion < exports.MIN_SECONDARY_WRITE_WIRE_VERSION)) {
|
||||
return readPreferenceServerSelector(read_preference_1.ReadPreference.primary);
|
||||
}
|
||||
return readPreferenceServerSelector(readPreference);
|
||||
}
|
||||
exports.secondaryWritableServerSelector = secondaryWritableServerSelector;
|
||||
/**
|
||||
* Reduces the passed in array of servers by the rules of the "Max Staleness" specification
|
||||
* found here: https://github.com/mongodb/specifications/blob/master/source/max-staleness/max-staleness.rst
|
||||
*
|
||||
* @param readPreference - The read preference providing max staleness guidance
|
||||
* @param topologyDescription - The topology description
|
||||
* @param servers - The list of server descriptions to be reduced
|
||||
* @returns The list of servers that satisfy the requirements of max staleness
|
||||
*/
|
||||
function maxStalenessReducer(readPreference, topologyDescription, servers) {
|
||||
if (readPreference.maxStalenessSeconds == null || readPreference.maxStalenessSeconds < 0) {
|
||||
return servers;
|
||||
}
|
||||
const maxStaleness = readPreference.maxStalenessSeconds;
|
||||
const maxStalenessVariance = (topologyDescription.heartbeatFrequencyMS + IDLE_WRITE_PERIOD) / 1000;
|
||||
if (maxStaleness < maxStalenessVariance) {
|
||||
throw new error_1.MongoInvalidArgumentError(`Option "maxStalenessSeconds" must be at least ${maxStalenessVariance} seconds`);
|
||||
}
|
||||
if (maxStaleness < SMALLEST_MAX_STALENESS_SECONDS) {
|
||||
throw new error_1.MongoInvalidArgumentError(`Option "maxStalenessSeconds" must be at least ${SMALLEST_MAX_STALENESS_SECONDS} seconds`);
|
||||
}
|
||||
if (topologyDescription.type === common_1.TopologyType.ReplicaSetWithPrimary) {
|
||||
const primary = Array.from(topologyDescription.servers.values()).filter(primaryFilter)[0];
|
||||
return servers.reduce((result, server) => {
|
||||
var _a;
|
||||
const stalenessMS = server.lastUpdateTime -
|
||||
server.lastWriteDate -
|
||||
(primary.lastUpdateTime - primary.lastWriteDate) +
|
||||
topologyDescription.heartbeatFrequencyMS;
|
||||
const staleness = stalenessMS / 1000;
|
||||
const maxStalenessSeconds = (_a = readPreference.maxStalenessSeconds) !== null && _a !== void 0 ? _a : 0;
|
||||
if (staleness <= maxStalenessSeconds) {
|
||||
result.push(server);
|
||||
}
|
||||
return result;
|
||||
}, []);
|
||||
}
|
||||
if (topologyDescription.type === common_1.TopologyType.ReplicaSetNoPrimary) {
|
||||
if (servers.length === 0) {
|
||||
return servers;
|
||||
}
|
||||
const sMax = servers.reduce((max, s) => s.lastWriteDate > max.lastWriteDate ? s : max);
|
||||
return servers.reduce((result, server) => {
|
||||
var _a;
|
||||
const stalenessMS = sMax.lastWriteDate - server.lastWriteDate + topologyDescription.heartbeatFrequencyMS;
|
||||
const staleness = stalenessMS / 1000;
|
||||
const maxStalenessSeconds = (_a = readPreference.maxStalenessSeconds) !== null && _a !== void 0 ? _a : 0;
|
||||
if (staleness <= maxStalenessSeconds) {
|
||||
result.push(server);
|
||||
}
|
||||
return result;
|
||||
}, []);
|
||||
}
|
||||
return servers;
|
||||
}
|
||||
/**
|
||||
* Determines whether a server's tags match a given set of tags
|
||||
*
|
||||
* @param tagSet - The requested tag set to match
|
||||
* @param serverTags - The server's tags
|
||||
*/
|
||||
function tagSetMatch(tagSet, serverTags) {
|
||||
const keys = Object.keys(tagSet);
|
||||
const serverTagKeys = Object.keys(serverTags);
|
||||
for (let i = 0; i < keys.length; ++i) {
|
||||
const key = keys[i];
|
||||
if (serverTagKeys.indexOf(key) === -1 || serverTags[key] !== tagSet[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Reduces a set of server descriptions based on tags requested by the read preference
|
||||
*
|
||||
* @param readPreference - The read preference providing the requested tags
|
||||
* @param servers - The list of server descriptions to reduce
|
||||
* @returns The list of servers matching the requested tags
|
||||
*/
|
||||
function tagSetReducer(readPreference, servers) {
|
||||
if (readPreference.tags == null ||
|
||||
(Array.isArray(readPreference.tags) && readPreference.tags.length === 0)) {
|
||||
return servers;
|
||||
}
|
||||
for (let i = 0; i < readPreference.tags.length; ++i) {
|
||||
const tagSet = readPreference.tags[i];
|
||||
const serversMatchingTagset = servers.reduce((matched, server) => {
|
||||
if (tagSetMatch(tagSet, server.tags))
|
||||
matched.push(server);
|
||||
return matched;
|
||||
}, []);
|
||||
if (serversMatchingTagset.length) {
|
||||
return serversMatchingTagset;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
/**
|
||||
* Reduces a list of servers to ensure they fall within an acceptable latency window. This is
|
||||
* further specified in the "Server Selection" specification, found here:
|
||||
* https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst
|
||||
*
|
||||
* @param topologyDescription - The topology description
|
||||
* @param servers - The list of servers to reduce
|
||||
* @returns The servers which fall within an acceptable latency window
|
||||
*/
|
||||
function latencyWindowReducer(topologyDescription, servers) {
|
||||
const low = servers.reduce((min, server) => min === -1 ? server.roundTripTime : Math.min(server.roundTripTime, min), -1);
|
||||
const high = low + topologyDescription.localThresholdMS;
|
||||
return servers.reduce((result, server) => {
|
||||
if (server.roundTripTime <= high && server.roundTripTime >= low)
|
||||
result.push(server);
|
||||
return result;
|
||||
}, []);
|
||||
}
|
||||
// filters
|
||||
function primaryFilter(server) {
|
||||
return server.type === common_1.ServerType.RSPrimary;
|
||||
}
|
||||
function secondaryFilter(server) {
|
||||
return server.type === common_1.ServerType.RSSecondary;
|
||||
}
|
||||
function nearestFilter(server) {
|
||||
return server.type === common_1.ServerType.RSSecondary || server.type === common_1.ServerType.RSPrimary;
|
||||
}
|
||||
function knownFilter(server) {
|
||||
return server.type !== common_1.ServerType.Unknown;
|
||||
}
|
||||
function loadBalancerFilter(server) {
|
||||
return server.type === common_1.ServerType.LoadBalancer;
|
||||
}
|
||||
/**
|
||||
* Returns a function which selects servers based on a provided read preference
|
||||
*
|
||||
* @param readPreference - The read preference to select with
|
||||
*/
|
||||
function readPreferenceServerSelector(readPreference) {
|
||||
if (!readPreference.isValid()) {
|
||||
throw new error_1.MongoInvalidArgumentError('Invalid read preference specified');
|
||||
}
|
||||
return (topologyDescription, servers) => {
|
||||
const commonWireVersion = topologyDescription.commonWireVersion;
|
||||
if (commonWireVersion &&
|
||||
readPreference.minWireVersion &&
|
||||
readPreference.minWireVersion > commonWireVersion) {
|
||||
throw new error_1.MongoCompatibilityError(`Minimum wire version '${readPreference.minWireVersion}' required, but found '${commonWireVersion}'`);
|
||||
}
|
||||
if (topologyDescription.type === common_1.TopologyType.LoadBalanced) {
|
||||
return servers.filter(loadBalancerFilter);
|
||||
}
|
||||
if (topologyDescription.type === common_1.TopologyType.Unknown) {
|
||||
return [];
|
||||
}
|
||||
if (topologyDescription.type === common_1.TopologyType.Single ||
|
||||
topologyDescription.type === common_1.TopologyType.Sharded) {
|
||||
return latencyWindowReducer(topologyDescription, servers.filter(knownFilter));
|
||||
}
|
||||
const mode = readPreference.mode;
|
||||
if (mode === read_preference_1.ReadPreference.PRIMARY) {
|
||||
return servers.filter(primaryFilter);
|
||||
}
|
||||
if (mode === read_preference_1.ReadPreference.PRIMARY_PREFERRED) {
|
||||
const result = servers.filter(primaryFilter);
|
||||
if (result.length) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
const filter = mode === read_preference_1.ReadPreference.NEAREST ? nearestFilter : secondaryFilter;
|
||||
const selectedServers = latencyWindowReducer(topologyDescription, tagSetReducer(readPreference, maxStalenessReducer(readPreference, topologyDescription, servers.filter(filter))));
|
||||
if (mode === read_preference_1.ReadPreference.SECONDARY_PREFERRED && selectedServers.length === 0) {
|
||||
return servers.filter(primaryFilter);
|
||||
}
|
||||
return selectedServers;
|
||||
};
|
||||
}
|
||||
exports.readPreferenceServerSelector = readPreferenceServerSelector;
|
||||
//# sourceMappingURL=server_selection.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+122
@@ -0,0 +1,122 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.SrvPoller = exports.SrvPollingEvent = void 0;
|
||||
const dns = require("dns");
|
||||
const timers_1 = require("timers");
|
||||
const error_1 = require("../error");
|
||||
const logger_1 = require("../logger");
|
||||
const mongo_types_1 = require("../mongo_types");
|
||||
const utils_1 = require("../utils");
|
||||
/**
|
||||
* Determines whether a provided address matches the provided parent domain in order
|
||||
* to avoid certain attack vectors.
|
||||
*
|
||||
* @param srvAddress - The address to check against a domain
|
||||
* @param parentDomain - The domain to check the provided address against
|
||||
* @returns Whether the provided address matches the parent domain
|
||||
*/
|
||||
function matchesParentDomain(srvAddress, parentDomain) {
|
||||
const regex = /^.*?\./;
|
||||
const srv = `.${srvAddress.replace(regex, '')}`;
|
||||
const parent = `.${parentDomain.replace(regex, '')}`;
|
||||
return srv.endsWith(parent);
|
||||
}
|
||||
/**
|
||||
* @internal
|
||||
* @category Event
|
||||
*/
|
||||
class SrvPollingEvent {
|
||||
constructor(srvRecords) {
|
||||
this.srvRecords = srvRecords;
|
||||
}
|
||||
hostnames() {
|
||||
return new Set(this.srvRecords.map(r => utils_1.HostAddress.fromSrvRecord(r).toString()));
|
||||
}
|
||||
}
|
||||
exports.SrvPollingEvent = SrvPollingEvent;
|
||||
/** @internal */
|
||||
class SrvPoller extends mongo_types_1.TypedEventEmitter {
|
||||
constructor(options) {
|
||||
var _a, _b, _c;
|
||||
super();
|
||||
if (!options || !options.srvHost) {
|
||||
throw new error_1.MongoRuntimeError('Options for SrvPoller must exist and include srvHost');
|
||||
}
|
||||
this.srvHost = options.srvHost;
|
||||
this.srvMaxHosts = (_a = options.srvMaxHosts) !== null && _a !== void 0 ? _a : 0;
|
||||
this.srvServiceName = (_b = options.srvServiceName) !== null && _b !== void 0 ? _b : 'mongodb';
|
||||
this.rescanSrvIntervalMS = 60000;
|
||||
this.heartbeatFrequencyMS = (_c = options.heartbeatFrequencyMS) !== null && _c !== void 0 ? _c : 10000;
|
||||
this.logger = new logger_1.Logger('srvPoller', options);
|
||||
this.haMode = false;
|
||||
this.generation = 0;
|
||||
this._timeout = undefined;
|
||||
}
|
||||
get srvAddress() {
|
||||
return `_${this.srvServiceName}._tcp.${this.srvHost}`;
|
||||
}
|
||||
get intervalMS() {
|
||||
return this.haMode ? this.heartbeatFrequencyMS : this.rescanSrvIntervalMS;
|
||||
}
|
||||
start() {
|
||||
if (!this._timeout) {
|
||||
this.schedule();
|
||||
}
|
||||
}
|
||||
stop() {
|
||||
if (this._timeout) {
|
||||
(0, timers_1.clearTimeout)(this._timeout);
|
||||
this.generation += 1;
|
||||
this._timeout = undefined;
|
||||
}
|
||||
}
|
||||
schedule() {
|
||||
if (this._timeout) {
|
||||
(0, timers_1.clearTimeout)(this._timeout);
|
||||
}
|
||||
this._timeout = (0, timers_1.setTimeout)(() => this._poll(), this.intervalMS);
|
||||
}
|
||||
success(srvRecords) {
|
||||
this.haMode = false;
|
||||
this.schedule();
|
||||
this.emit(SrvPoller.SRV_RECORD_DISCOVERY, new SrvPollingEvent(srvRecords));
|
||||
}
|
||||
failure(message, obj) {
|
||||
this.logger.warn(message, obj);
|
||||
this.haMode = true;
|
||||
this.schedule();
|
||||
}
|
||||
parentDomainMismatch(srvRecord) {
|
||||
this.logger.warn(`parent domain mismatch on SRV record (${srvRecord.name}:${srvRecord.port})`, srvRecord);
|
||||
}
|
||||
_poll() {
|
||||
const generation = this.generation;
|
||||
dns.resolveSrv(this.srvAddress, (err, srvRecords) => {
|
||||
if (generation !== this.generation) {
|
||||
return;
|
||||
}
|
||||
if (err) {
|
||||
this.failure('DNS error', err);
|
||||
return;
|
||||
}
|
||||
const finalAddresses = [];
|
||||
for (const record of srvRecords) {
|
||||
if (matchesParentDomain(record.name, this.srvHost)) {
|
||||
finalAddresses.push(record);
|
||||
}
|
||||
else {
|
||||
this.parentDomainMismatch(record);
|
||||
}
|
||||
}
|
||||
if (!finalAddresses.length) {
|
||||
this.failure('No valid addresses found at host');
|
||||
return;
|
||||
}
|
||||
this.success(finalAddresses);
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.SrvPoller = SrvPoller;
|
||||
/** @event */
|
||||
SrvPoller.SRV_RECORD_DISCOVERY = 'srvRecordDiscovery';
|
||||
//# sourceMappingURL=srv_polling.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"srv_polling.js","sourceRoot":"","sources":["../../src/sdam/srv_polling.ts"],"names":[],"mappings":";;;AAAA,2BAA2B;AAC3B,mCAAkD;AAElD,oCAA6C;AAC7C,sCAAkD;AAClD,gDAAmD;AACnD,oCAAuC;AAEvC;;;;;;;GAOG;AACH,SAAS,mBAAmB,CAAC,UAAkB,EAAE,YAAoB;IACnE,MAAM,KAAK,GAAG,QAAQ,CAAC;IACvB,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC;IAChD,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC;IACrD,OAAO,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AAC9B,CAAC;AAED;;;GAGG;AACH,MAAa,eAAe;IAE1B,YAAY,UAA2B;QACrC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAED,SAAS;QACP,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,mBAAW,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;IACpF,CAAC;CACF;AATD,0CASC;AAeD,gBAAgB;AAChB,MAAa,SAAU,SAAQ,+BAAkC;IAc/D,YAAY,OAAyB;;QACnC,KAAK,EAAE,CAAC;QAER,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;YAChC,MAAM,IAAI,yBAAiB,CAAC,sDAAsD,CAAC,CAAC;SACrF;QAED,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,WAAW,GAAG,MAAA,OAAO,CAAC,WAAW,mCAAI,CAAC,CAAC;QAC5C,IAAI,CAAC,cAAc,GAAG,MAAA,OAAO,CAAC,cAAc,mCAAI,SAAS,CAAC;QAC1D,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC;QACjC,IAAI,CAAC,oBAAoB,GAAG,MAAA,OAAO,CAAC,oBAAoB,mCAAI,KAAK,CAAC;QAClE,IAAI,CAAC,MAAM,GAAG,IAAI,eAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QAE/C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC;QAEpB,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC5B,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,IAAI,CAAC,cAAc,SAAS,IAAI,CAAC,OAAO,EAAE,CAAC;IACxD,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC;IAC5E,CAAC;IAED,KAAK;QACH,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE;YAClB,IAAI,CAAC,QAAQ,EAAE,CAAC;SACjB;IACH,CAAC;IAED,IAAI;QACF,IAAI,IAAI,CAAC,QAAQ,EAAE;YACjB,IAAA,qBAAY,EAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC5B,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC;YACrB,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;SAC3B;IACH,CAAC;IAED,QAAQ;QACN,IAAI,IAAI,CAAC,QAAQ,EAAE;YACjB,IAAA,qBAAY,EAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;SAC7B;QAED,IAAI,CAAC,QAAQ,GAAG,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IAClE,CAAC;IAED,OAAO,CAAC,UAA2B;QACjC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChB,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,oBAAoB,EAAE,IAAI,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO,CAAC,OAAe,EAAE,GAA2B;QAClD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QACnB,IAAI,CAAC,QAAQ,EAAE,CAAC;IAClB,CAAC;IAED,oBAAoB,CAAC,SAAwB;QAC3C,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,yCAAyC,SAAS,CAAC,IAAI,IAAI,SAAS,CAAC,IAAI,GAAG,EAC5E,SAAS,CACV,CAAC;IACJ,CAAC;IAED,KAAK;QACH,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;QACnC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,GAAG,EAAE,UAAU,EAAE,EAAE;YAClD,IAAI,UAAU,KAAK,IAAI,CAAC,UAAU,EAAE;gBAClC,OAAO;aACR;YAED,IAAI,GAAG,EAAE;gBACP,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;gBAC/B,OAAO;aACR;YAED,MAAM,cAAc,GAAoB,EAAE,CAAC;YAC3C,KAAK,MAAM,MAAM,IAAI,UAAU,EAAE;gBAC/B,IAAI,mBAAmB,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE;oBAClD,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;iBAC7B;qBAAM;oBACL,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;iBACnC;aACF;YAED,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE;gBAC1B,IAAI,CAAC,OAAO,CAAC,kCAAkC,CAAC,CAAC;gBACjD,OAAO;aACR;YAED,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;;AA/GH,8BAgHC;AArGC,aAAa;AACG,8BAAoB,GAAG,oBAA6B,CAAC"}
|
||||
+645
@@ -0,0 +1,645 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ServerCapabilities = exports.Topology = void 0;
|
||||
const Denque = require("denque");
|
||||
const timers_1 = require("timers");
|
||||
const util_1 = require("util");
|
||||
const bson_1 = require("../bson");
|
||||
const connection_string_1 = require("../connection_string");
|
||||
const constants_1 = require("../constants");
|
||||
const error_1 = require("../error");
|
||||
const mongo_types_1 = require("../mongo_types");
|
||||
const read_preference_1 = require("../read_preference");
|
||||
const utils_1 = require("../utils");
|
||||
const common_1 = require("./common");
|
||||
const events_1 = require("./events");
|
||||
const server_1 = require("./server");
|
||||
const server_description_1 = require("./server_description");
|
||||
const server_selection_1 = require("./server_selection");
|
||||
const srv_polling_1 = require("./srv_polling");
|
||||
const topology_description_1 = require("./topology_description");
|
||||
// Global state
|
||||
let globalTopologyCounter = 0;
|
||||
const stateTransition = (0, utils_1.makeStateMachine)({
|
||||
[common_1.STATE_CLOSED]: [common_1.STATE_CLOSED, common_1.STATE_CONNECTING],
|
||||
[common_1.STATE_CONNECTING]: [common_1.STATE_CONNECTING, common_1.STATE_CLOSING, common_1.STATE_CONNECTED, common_1.STATE_CLOSED],
|
||||
[common_1.STATE_CONNECTED]: [common_1.STATE_CONNECTED, common_1.STATE_CLOSING, common_1.STATE_CLOSED],
|
||||
[common_1.STATE_CLOSING]: [common_1.STATE_CLOSING, common_1.STATE_CLOSED]
|
||||
});
|
||||
/** @internal */
|
||||
const kCancelled = Symbol('cancelled');
|
||||
/** @internal */
|
||||
const kWaitQueue = Symbol('waitQueue');
|
||||
/**
|
||||
* A container of server instances representing a connection to a MongoDB topology.
|
||||
* @internal
|
||||
*/
|
||||
class Topology extends mongo_types_1.TypedEventEmitter {
|
||||
/**
|
||||
* @param seedlist - a list of HostAddress instances to connect to
|
||||
*/
|
||||
constructor(seeds, options) {
|
||||
var _a;
|
||||
super();
|
||||
// Legacy CSFLE support
|
||||
this.bson = Object.create(null);
|
||||
this.bson.serialize = bson_1.serialize;
|
||||
this.bson.deserialize = bson_1.deserialize;
|
||||
// Options should only be undefined in tests, MongoClient will always have defined options
|
||||
options = options !== null && options !== void 0 ? options : {
|
||||
hosts: [utils_1.HostAddress.fromString('localhost:27017')],
|
||||
...Object.fromEntries(connection_string_1.DEFAULT_OPTIONS.entries()),
|
||||
...Object.fromEntries(connection_string_1.FEATURE_FLAGS.entries())
|
||||
};
|
||||
if (typeof seeds === 'string') {
|
||||
seeds = [utils_1.HostAddress.fromString(seeds)];
|
||||
}
|
||||
else if (!Array.isArray(seeds)) {
|
||||
seeds = [seeds];
|
||||
}
|
||||
const seedlist = [];
|
||||
for (const seed of seeds) {
|
||||
if (typeof seed === 'string') {
|
||||
seedlist.push(utils_1.HostAddress.fromString(seed));
|
||||
}
|
||||
else if (seed instanceof utils_1.HostAddress) {
|
||||
seedlist.push(seed);
|
||||
}
|
||||
else {
|
||||
// FIXME(NODE-3483): May need to be a MongoParseError
|
||||
throw new error_1.MongoRuntimeError(`Topology cannot be constructed from ${JSON.stringify(seed)}`);
|
||||
}
|
||||
}
|
||||
const topologyType = topologyTypeFromOptions(options);
|
||||
const topologyId = globalTopologyCounter++;
|
||||
const selectedHosts = options.srvMaxHosts == null ||
|
||||
options.srvMaxHosts === 0 ||
|
||||
options.srvMaxHosts >= seedlist.length
|
||||
? seedlist
|
||||
: (0, utils_1.shuffle)(seedlist, options.srvMaxHosts);
|
||||
const serverDescriptions = new Map();
|
||||
for (const hostAddress of selectedHosts) {
|
||||
serverDescriptions.set(hostAddress.toString(), new server_description_1.ServerDescription(hostAddress));
|
||||
}
|
||||
this[kWaitQueue] = new Denque();
|
||||
this.s = {
|
||||
// the id of this topology
|
||||
id: topologyId,
|
||||
// passed in options
|
||||
options,
|
||||
// initial seedlist of servers to connect to
|
||||
seedlist,
|
||||
// initial state
|
||||
state: common_1.STATE_CLOSED,
|
||||
// the topology description
|
||||
description: new topology_description_1.TopologyDescription(topologyType, serverDescriptions, options.replicaSet, undefined, undefined, undefined, options),
|
||||
serverSelectionTimeoutMS: options.serverSelectionTimeoutMS,
|
||||
heartbeatFrequencyMS: options.heartbeatFrequencyMS,
|
||||
minHeartbeatFrequencyMS: options.minHeartbeatFrequencyMS,
|
||||
// a map of server instances to normalized addresses
|
||||
servers: new Map(),
|
||||
credentials: options === null || options === void 0 ? void 0 : options.credentials,
|
||||
clusterTime: undefined,
|
||||
// timer management
|
||||
connectionTimers: new Set(),
|
||||
detectShardedTopology: ev => this.detectShardedTopology(ev),
|
||||
detectSrvRecords: ev => this.detectSrvRecords(ev)
|
||||
};
|
||||
if (options.srvHost && !options.loadBalanced) {
|
||||
this.s.srvPoller =
|
||||
(_a = options.srvPoller) !== null && _a !== void 0 ? _a : new srv_polling_1.SrvPoller({
|
||||
heartbeatFrequencyMS: this.s.heartbeatFrequencyMS,
|
||||
srvHost: options.srvHost,
|
||||
srvMaxHosts: options.srvMaxHosts,
|
||||
srvServiceName: options.srvServiceName
|
||||
});
|
||||
this.on(Topology.TOPOLOGY_DESCRIPTION_CHANGED, this.s.detectShardedTopology);
|
||||
}
|
||||
}
|
||||
detectShardedTopology(event) {
|
||||
var _a, _b, _c;
|
||||
const previousType = event.previousDescription.type;
|
||||
const newType = event.newDescription.type;
|
||||
const transitionToSharded = previousType !== common_1.TopologyType.Sharded && newType === common_1.TopologyType.Sharded;
|
||||
const srvListeners = (_a = this.s.srvPoller) === null || _a === void 0 ? void 0 : _a.listeners(srv_polling_1.SrvPoller.SRV_RECORD_DISCOVERY);
|
||||
const listeningToSrvPolling = !!(srvListeners === null || srvListeners === void 0 ? void 0 : srvListeners.includes(this.s.detectSrvRecords));
|
||||
if (transitionToSharded && !listeningToSrvPolling) {
|
||||
(_b = this.s.srvPoller) === null || _b === void 0 ? void 0 : _b.on(srv_polling_1.SrvPoller.SRV_RECORD_DISCOVERY, this.s.detectSrvRecords);
|
||||
(_c = this.s.srvPoller) === null || _c === void 0 ? void 0 : _c.start();
|
||||
}
|
||||
}
|
||||
detectSrvRecords(ev) {
|
||||
const previousTopologyDescription = this.s.description;
|
||||
this.s.description = this.s.description.updateFromSrvPollingEvent(ev, this.s.options.srvMaxHosts);
|
||||
if (this.s.description === previousTopologyDescription) {
|
||||
// Nothing changed, so return
|
||||
return;
|
||||
}
|
||||
updateServers(this);
|
||||
this.emit(Topology.TOPOLOGY_DESCRIPTION_CHANGED, new events_1.TopologyDescriptionChangedEvent(this.s.id, previousTopologyDescription, this.s.description));
|
||||
}
|
||||
/**
|
||||
* @returns A `TopologyDescription` for this topology
|
||||
*/
|
||||
get description() {
|
||||
return this.s.description;
|
||||
}
|
||||
get loadBalanced() {
|
||||
return this.s.options.loadBalanced;
|
||||
}
|
||||
get capabilities() {
|
||||
return new ServerCapabilities(this.lastHello());
|
||||
}
|
||||
/** Initiate server connect */
|
||||
connect(options, callback) {
|
||||
var _a;
|
||||
if (typeof options === 'function')
|
||||
(callback = options), (options = {});
|
||||
options = options !== null && options !== void 0 ? options : {};
|
||||
if (this.s.state === common_1.STATE_CONNECTED) {
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
return;
|
||||
}
|
||||
stateTransition(this, common_1.STATE_CONNECTING);
|
||||
// emit SDAM monitoring events
|
||||
this.emit(Topology.TOPOLOGY_OPENING, new events_1.TopologyOpeningEvent(this.s.id));
|
||||
// emit an event for the topology change
|
||||
this.emit(Topology.TOPOLOGY_DESCRIPTION_CHANGED, new events_1.TopologyDescriptionChangedEvent(this.s.id, new topology_description_1.TopologyDescription(common_1.TopologyType.Unknown), // initial is always Unknown
|
||||
this.s.description));
|
||||
// connect all known servers, then attempt server selection to connect
|
||||
const serverDescriptions = Array.from(this.s.description.servers.values());
|
||||
this.s.servers = new Map(serverDescriptions.map(serverDescription => [
|
||||
serverDescription.address,
|
||||
createAndConnectServer(this, serverDescription)
|
||||
]));
|
||||
// In load balancer mode we need to fake a server description getting
|
||||
// emitted from the monitor, since the monitor doesn't exist.
|
||||
if (this.s.options.loadBalanced) {
|
||||
for (const description of serverDescriptions) {
|
||||
const newDescription = new server_description_1.ServerDescription(description.hostAddress, undefined, {
|
||||
loadBalanced: this.s.options.loadBalanced
|
||||
});
|
||||
this.serverUpdateHandler(newDescription);
|
||||
}
|
||||
}
|
||||
const exitWithError = (error) => callback ? callback(error) : this.emit(Topology.ERROR, error);
|
||||
const readPreference = (_a = options.readPreference) !== null && _a !== void 0 ? _a : read_preference_1.ReadPreference.primary;
|
||||
this.selectServer((0, server_selection_1.readPreferenceServerSelector)(readPreference), options, (err, server) => {
|
||||
if (err) {
|
||||
return this.close({ force: false }, () => exitWithError(err));
|
||||
}
|
||||
// TODO: NODE-2471
|
||||
const skipPingOnConnect = this.s.options[Symbol.for('@@mdb.skipPingOnConnect')] === true;
|
||||
if (!skipPingOnConnect && server && this.s.credentials) {
|
||||
server.command((0, utils_1.ns)('admin.$cmd'), { ping: 1 }, {}, err => {
|
||||
if (err) {
|
||||
return exitWithError(err);
|
||||
}
|
||||
stateTransition(this, common_1.STATE_CONNECTED);
|
||||
this.emit(Topology.OPEN, this);
|
||||
this.emit(Topology.CONNECT, this);
|
||||
callback === null || callback === void 0 ? void 0 : callback(undefined, this);
|
||||
});
|
||||
return;
|
||||
}
|
||||
stateTransition(this, common_1.STATE_CONNECTED);
|
||||
this.emit(Topology.OPEN, this);
|
||||
this.emit(Topology.CONNECT, this);
|
||||
callback === null || callback === void 0 ? void 0 : callback(undefined, this);
|
||||
});
|
||||
}
|
||||
/** Close this topology */
|
||||
close(options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
if (typeof options === 'boolean') {
|
||||
options = { force: options };
|
||||
}
|
||||
options = options !== null && options !== void 0 ? options : {};
|
||||
if (this.s.state === common_1.STATE_CLOSED || this.s.state === common_1.STATE_CLOSING) {
|
||||
return callback === null || callback === void 0 ? void 0 : callback();
|
||||
}
|
||||
const destroyedServers = Array.from(this.s.servers.values(), server => {
|
||||
return (0, util_1.promisify)(destroyServer)(server, this, options);
|
||||
});
|
||||
Promise.all(destroyedServers)
|
||||
.then(() => {
|
||||
this.s.servers.clear();
|
||||
stateTransition(this, common_1.STATE_CLOSING);
|
||||
drainWaitQueue(this[kWaitQueue], new error_1.MongoTopologyClosedError());
|
||||
(0, common_1.drainTimerQueue)(this.s.connectionTimers);
|
||||
if (this.s.srvPoller) {
|
||||
this.s.srvPoller.stop();
|
||||
this.s.srvPoller.removeListener(srv_polling_1.SrvPoller.SRV_RECORD_DISCOVERY, this.s.detectSrvRecords);
|
||||
}
|
||||
this.removeListener(Topology.TOPOLOGY_DESCRIPTION_CHANGED, this.s.detectShardedTopology);
|
||||
stateTransition(this, common_1.STATE_CLOSED);
|
||||
// emit an event for close
|
||||
this.emit(Topology.TOPOLOGY_CLOSED, new events_1.TopologyClosedEvent(this.s.id));
|
||||
})
|
||||
.finally(() => callback === null || callback === void 0 ? void 0 : callback());
|
||||
}
|
||||
/**
|
||||
* Selects a server according to the selection predicate provided
|
||||
*
|
||||
* @param selector - An optional selector to select servers by, defaults to a random selection within a latency window
|
||||
* @param options - Optional settings related to server selection
|
||||
* @param callback - The callback used to indicate success or failure
|
||||
* @returns An instance of a `Server` meeting the criteria of the predicate provided
|
||||
*/
|
||||
selectServer(selector, options, callback) {
|
||||
let serverSelector;
|
||||
if (typeof selector !== 'function') {
|
||||
if (typeof selector === 'string') {
|
||||
serverSelector = (0, server_selection_1.readPreferenceServerSelector)(read_preference_1.ReadPreference.fromString(selector));
|
||||
}
|
||||
else {
|
||||
let readPreference;
|
||||
if (selector instanceof read_preference_1.ReadPreference) {
|
||||
readPreference = selector;
|
||||
}
|
||||
else {
|
||||
read_preference_1.ReadPreference.translate(options);
|
||||
readPreference = options.readPreference || read_preference_1.ReadPreference.primary;
|
||||
}
|
||||
serverSelector = (0, server_selection_1.readPreferenceServerSelector)(readPreference);
|
||||
}
|
||||
}
|
||||
else {
|
||||
serverSelector = selector;
|
||||
}
|
||||
options = Object.assign({}, { serverSelectionTimeoutMS: this.s.serverSelectionTimeoutMS }, options);
|
||||
const isSharded = this.description.type === common_1.TopologyType.Sharded;
|
||||
const session = options.session;
|
||||
const transaction = session && session.transaction;
|
||||
if (isSharded && transaction && transaction.server) {
|
||||
callback(undefined, transaction.server);
|
||||
return;
|
||||
}
|
||||
const waitQueueMember = {
|
||||
serverSelector,
|
||||
transaction,
|
||||
callback
|
||||
};
|
||||
const serverSelectionTimeoutMS = options.serverSelectionTimeoutMS;
|
||||
if (serverSelectionTimeoutMS) {
|
||||
waitQueueMember.timer = (0, timers_1.setTimeout)(() => {
|
||||
waitQueueMember[kCancelled] = true;
|
||||
waitQueueMember.timer = undefined;
|
||||
const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
|
||||
waitQueueMember.callback(timeoutError);
|
||||
}, serverSelectionTimeoutMS);
|
||||
}
|
||||
this[kWaitQueue].push(waitQueueMember);
|
||||
processWaitQueue(this);
|
||||
}
|
||||
// Sessions related methods
|
||||
/**
|
||||
* @returns Whether the topology should initiate selection to determine session support
|
||||
*/
|
||||
shouldCheckForSessionSupport() {
|
||||
if (this.description.type === common_1.TopologyType.Single) {
|
||||
return !this.description.hasKnownServers;
|
||||
}
|
||||
return !this.description.hasDataBearingServers;
|
||||
}
|
||||
/**
|
||||
* @returns Whether sessions are supported on the current topology
|
||||
*/
|
||||
hasSessionSupport() {
|
||||
return this.loadBalanced || this.description.logicalSessionTimeoutMinutes != null;
|
||||
}
|
||||
/**
|
||||
* Update the internal TopologyDescription with a ServerDescription
|
||||
*
|
||||
* @param serverDescription - The server to update in the internal list of server descriptions
|
||||
*/
|
||||
serverUpdateHandler(serverDescription) {
|
||||
if (!this.s.description.hasServer(serverDescription.address)) {
|
||||
return;
|
||||
}
|
||||
// ignore this server update if its from an outdated topologyVersion
|
||||
if (isStaleServerDescription(this.s.description, serverDescription)) {
|
||||
return;
|
||||
}
|
||||
// these will be used for monitoring events later
|
||||
const previousTopologyDescription = this.s.description;
|
||||
const previousServerDescription = this.s.description.servers.get(serverDescription.address);
|
||||
if (!previousServerDescription) {
|
||||
return;
|
||||
}
|
||||
// Driver Sessions Spec: "Whenever a driver receives a cluster time from
|
||||
// a server it MUST compare it to the current highest seen cluster time
|
||||
// for the deployment. If the new cluster time is higher than the
|
||||
// highest seen cluster time it MUST become the new highest seen cluster
|
||||
// time. Two cluster times are compared using only the BsonTimestamp
|
||||
// value of the clusterTime embedded field."
|
||||
const clusterTime = serverDescription.$clusterTime;
|
||||
if (clusterTime) {
|
||||
(0, common_1._advanceClusterTime)(this, clusterTime);
|
||||
}
|
||||
// If we already know all the information contained in this updated description, then
|
||||
// we don't need to emit SDAM events, but still need to update the description, in order
|
||||
// to keep client-tracked attributes like last update time and round trip time up to date
|
||||
const equalDescriptions = previousServerDescription && previousServerDescription.equals(serverDescription);
|
||||
// first update the TopologyDescription
|
||||
this.s.description = this.s.description.update(serverDescription);
|
||||
if (this.s.description.compatibilityError) {
|
||||
this.emit(Topology.ERROR, new error_1.MongoCompatibilityError(this.s.description.compatibilityError));
|
||||
return;
|
||||
}
|
||||
// emit monitoring events for this change
|
||||
if (!equalDescriptions) {
|
||||
const newDescription = this.s.description.servers.get(serverDescription.address);
|
||||
if (newDescription) {
|
||||
this.emit(Topology.SERVER_DESCRIPTION_CHANGED, new events_1.ServerDescriptionChangedEvent(this.s.id, serverDescription.address, previousServerDescription, newDescription));
|
||||
}
|
||||
}
|
||||
// update server list from updated descriptions
|
||||
updateServers(this, serverDescription);
|
||||
// attempt to resolve any outstanding server selection attempts
|
||||
if (this[kWaitQueue].length > 0) {
|
||||
processWaitQueue(this);
|
||||
}
|
||||
if (!equalDescriptions) {
|
||||
this.emit(Topology.TOPOLOGY_DESCRIPTION_CHANGED, new events_1.TopologyDescriptionChangedEvent(this.s.id, previousTopologyDescription, this.s.description));
|
||||
}
|
||||
}
|
||||
auth(credentials, callback) {
|
||||
if (typeof credentials === 'function')
|
||||
(callback = credentials), (credentials = undefined);
|
||||
if (typeof callback === 'function')
|
||||
callback(undefined, true);
|
||||
}
|
||||
get clientMetadata() {
|
||||
return this.s.options.metadata;
|
||||
}
|
||||
isConnected() {
|
||||
return this.s.state === common_1.STATE_CONNECTED;
|
||||
}
|
||||
isDestroyed() {
|
||||
return this.s.state === common_1.STATE_CLOSED;
|
||||
}
|
||||
/**
|
||||
* @deprecated This function is deprecated and will be removed in the next major version.
|
||||
*/
|
||||
unref() {
|
||||
(0, utils_1.emitWarning)('`unref` is a noop and will be removed in the next major version');
|
||||
}
|
||||
// NOTE: There are many places in code where we explicitly check the last hello
|
||||
// to do feature support detection. This should be done any other way, but for
|
||||
// now we will just return the first hello seen, which should suffice.
|
||||
lastHello() {
|
||||
const serverDescriptions = Array.from(this.description.servers.values());
|
||||
if (serverDescriptions.length === 0)
|
||||
return {};
|
||||
const sd = serverDescriptions.filter((sd) => sd.type !== common_1.ServerType.Unknown)[0];
|
||||
const result = sd || { maxWireVersion: this.description.commonWireVersion };
|
||||
return result;
|
||||
}
|
||||
get commonWireVersion() {
|
||||
return this.description.commonWireVersion;
|
||||
}
|
||||
get logicalSessionTimeoutMinutes() {
|
||||
return this.description.logicalSessionTimeoutMinutes;
|
||||
}
|
||||
get clusterTime() {
|
||||
return this.s.clusterTime;
|
||||
}
|
||||
set clusterTime(clusterTime) {
|
||||
this.s.clusterTime = clusterTime;
|
||||
}
|
||||
}
|
||||
exports.Topology = Topology;
|
||||
/** @event */
|
||||
Topology.SERVER_OPENING = constants_1.SERVER_OPENING;
|
||||
/** @event */
|
||||
Topology.SERVER_CLOSED = constants_1.SERVER_CLOSED;
|
||||
/** @event */
|
||||
Topology.SERVER_DESCRIPTION_CHANGED = constants_1.SERVER_DESCRIPTION_CHANGED;
|
||||
/** @event */
|
||||
Topology.TOPOLOGY_OPENING = constants_1.TOPOLOGY_OPENING;
|
||||
/** @event */
|
||||
Topology.TOPOLOGY_CLOSED = constants_1.TOPOLOGY_CLOSED;
|
||||
/** @event */
|
||||
Topology.TOPOLOGY_DESCRIPTION_CHANGED = constants_1.TOPOLOGY_DESCRIPTION_CHANGED;
|
||||
/** @event */
|
||||
Topology.ERROR = constants_1.ERROR;
|
||||
/** @event */
|
||||
Topology.OPEN = constants_1.OPEN;
|
||||
/** @event */
|
||||
Topology.CONNECT = constants_1.CONNECT;
|
||||
/** @event */
|
||||
Topology.CLOSE = constants_1.CLOSE;
|
||||
/** @event */
|
||||
Topology.TIMEOUT = constants_1.TIMEOUT;
|
||||
/** Destroys a server, and removes all event listeners from the instance */
|
||||
function destroyServer(server, topology, options, callback) {
|
||||
options = options !== null && options !== void 0 ? options : {};
|
||||
for (const event of constants_1.LOCAL_SERVER_EVENTS) {
|
||||
server.removeAllListeners(event);
|
||||
}
|
||||
server.destroy(options, () => {
|
||||
topology.emit(Topology.SERVER_CLOSED, new events_1.ServerClosedEvent(topology.s.id, server.description.address));
|
||||
for (const event of constants_1.SERVER_RELAY_EVENTS) {
|
||||
server.removeAllListeners(event);
|
||||
}
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
/** Predicts the TopologyType from options */
|
||||
function topologyTypeFromOptions(options) {
|
||||
if (options === null || options === void 0 ? void 0 : options.directConnection) {
|
||||
return common_1.TopologyType.Single;
|
||||
}
|
||||
if (options === null || options === void 0 ? void 0 : options.replicaSet) {
|
||||
return common_1.TopologyType.ReplicaSetNoPrimary;
|
||||
}
|
||||
if (options === null || options === void 0 ? void 0 : options.loadBalanced) {
|
||||
return common_1.TopologyType.LoadBalanced;
|
||||
}
|
||||
return common_1.TopologyType.Unknown;
|
||||
}
|
||||
/**
|
||||
* Creates new server instances and attempts to connect them
|
||||
*
|
||||
* @param topology - The topology that this server belongs to
|
||||
* @param serverDescription - The description for the server to initialize and connect to
|
||||
*/
|
||||
function createAndConnectServer(topology, serverDescription) {
|
||||
topology.emit(Topology.SERVER_OPENING, new events_1.ServerOpeningEvent(topology.s.id, serverDescription.address));
|
||||
const server = new server_1.Server(topology, serverDescription, topology.s.options);
|
||||
for (const event of constants_1.SERVER_RELAY_EVENTS) {
|
||||
server.on(event, (e) => topology.emit(event, e));
|
||||
}
|
||||
server.on(server_1.Server.DESCRIPTION_RECEIVED, description => topology.serverUpdateHandler(description));
|
||||
server.connect();
|
||||
return server;
|
||||
}
|
||||
/**
|
||||
* @param topology - Topology to update.
|
||||
* @param incomingServerDescription - New server description.
|
||||
*/
|
||||
function updateServers(topology, incomingServerDescription) {
|
||||
// update the internal server's description
|
||||
if (incomingServerDescription && topology.s.servers.has(incomingServerDescription.address)) {
|
||||
const server = topology.s.servers.get(incomingServerDescription.address);
|
||||
if (server) {
|
||||
server.s.description = incomingServerDescription;
|
||||
}
|
||||
}
|
||||
// add new servers for all descriptions we currently don't know about locally
|
||||
for (const serverDescription of topology.description.servers.values()) {
|
||||
if (!topology.s.servers.has(serverDescription.address)) {
|
||||
const server = createAndConnectServer(topology, serverDescription);
|
||||
topology.s.servers.set(serverDescription.address, server);
|
||||
}
|
||||
}
|
||||
// for all servers no longer known, remove their descriptions and destroy their instances
|
||||
for (const entry of topology.s.servers) {
|
||||
const serverAddress = entry[0];
|
||||
if (topology.description.hasServer(serverAddress)) {
|
||||
continue;
|
||||
}
|
||||
if (!topology.s.servers.has(serverAddress)) {
|
||||
continue;
|
||||
}
|
||||
const server = topology.s.servers.get(serverAddress);
|
||||
topology.s.servers.delete(serverAddress);
|
||||
// prepare server for garbage collection
|
||||
if (server) {
|
||||
destroyServer(server, topology);
|
||||
}
|
||||
}
|
||||
}
|
||||
function drainWaitQueue(queue, err) {
|
||||
while (queue.length) {
|
||||
const waitQueueMember = queue.shift();
|
||||
if (!waitQueueMember) {
|
||||
continue;
|
||||
}
|
||||
if (waitQueueMember.timer) {
|
||||
(0, timers_1.clearTimeout)(waitQueueMember.timer);
|
||||
}
|
||||
if (!waitQueueMember[kCancelled]) {
|
||||
waitQueueMember.callback(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
function processWaitQueue(topology) {
|
||||
if (topology.s.state === common_1.STATE_CLOSED) {
|
||||
drainWaitQueue(topology[kWaitQueue], new error_1.MongoTopologyClosedError());
|
||||
return;
|
||||
}
|
||||
const isSharded = topology.description.type === common_1.TopologyType.Sharded;
|
||||
const serverDescriptions = Array.from(topology.description.servers.values());
|
||||
const membersToProcess = topology[kWaitQueue].length;
|
||||
for (let i = 0; i < membersToProcess; ++i) {
|
||||
const waitQueueMember = topology[kWaitQueue].shift();
|
||||
if (!waitQueueMember) {
|
||||
continue;
|
||||
}
|
||||
if (waitQueueMember[kCancelled]) {
|
||||
continue;
|
||||
}
|
||||
let selectedDescriptions;
|
||||
try {
|
||||
const serverSelector = waitQueueMember.serverSelector;
|
||||
selectedDescriptions = serverSelector
|
||||
? serverSelector(topology.description, serverDescriptions)
|
||||
: serverDescriptions;
|
||||
}
|
||||
catch (e) {
|
||||
if (waitQueueMember.timer) {
|
||||
(0, timers_1.clearTimeout)(waitQueueMember.timer);
|
||||
}
|
||||
waitQueueMember.callback(e);
|
||||
continue;
|
||||
}
|
||||
let selectedServer;
|
||||
if (selectedDescriptions.length === 0) {
|
||||
topology[kWaitQueue].push(waitQueueMember);
|
||||
continue;
|
||||
}
|
||||
else if (selectedDescriptions.length === 1) {
|
||||
selectedServer = topology.s.servers.get(selectedDescriptions[0].address);
|
||||
}
|
||||
else {
|
||||
// don't shuffle the array if there are only two elements
|
||||
const descriptions = selectedDescriptions.length === 2 ? selectedDescriptions : (0, utils_1.shuffle)(selectedDescriptions, 2);
|
||||
const server1 = topology.s.servers.get(descriptions[0].address);
|
||||
const server2 = topology.s.servers.get(descriptions[1].address);
|
||||
selectedServer =
|
||||
server1 && server2 && server1.s.operationCount < server2.s.operationCount
|
||||
? server1
|
||||
: server2;
|
||||
}
|
||||
if (!selectedServer) {
|
||||
waitQueueMember.callback(new error_1.MongoServerSelectionError('server selection returned a server description but the server was not found in the topology', topology.description));
|
||||
return;
|
||||
}
|
||||
const transaction = waitQueueMember.transaction;
|
||||
if (isSharded && transaction && transaction.isActive && selectedServer) {
|
||||
transaction.pinServer(selectedServer);
|
||||
}
|
||||
if (waitQueueMember.timer) {
|
||||
(0, timers_1.clearTimeout)(waitQueueMember.timer);
|
||||
}
|
||||
waitQueueMember.callback(undefined, selectedServer);
|
||||
}
|
||||
if (topology[kWaitQueue].length > 0) {
|
||||
// ensure all server monitors attempt monitoring soon
|
||||
for (const [, server] of topology.s.servers) {
|
||||
process.nextTick(function scheduleServerCheck() {
|
||||
return server.requestCheck();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
function isStaleServerDescription(topologyDescription, incomingServerDescription) {
|
||||
const currentServerDescription = topologyDescription.servers.get(incomingServerDescription.address);
|
||||
const currentTopologyVersion = currentServerDescription === null || currentServerDescription === void 0 ? void 0 : currentServerDescription.topologyVersion;
|
||||
return ((0, server_description_1.compareTopologyVersion)(currentTopologyVersion, incomingServerDescription.topologyVersion) > 0);
|
||||
}
|
||||
/** @public */
|
||||
class ServerCapabilities {
|
||||
constructor(hello) {
|
||||
this.minWireVersion = hello.minWireVersion || 0;
|
||||
this.maxWireVersion = hello.maxWireVersion || 0;
|
||||
}
|
||||
get hasAggregationCursor() {
|
||||
return this.maxWireVersion >= 1;
|
||||
}
|
||||
get hasWriteCommands() {
|
||||
return this.maxWireVersion >= 2;
|
||||
}
|
||||
get hasTextSearch() {
|
||||
return this.minWireVersion >= 0;
|
||||
}
|
||||
get hasAuthCommands() {
|
||||
return this.maxWireVersion >= 1;
|
||||
}
|
||||
get hasListCollectionsCommand() {
|
||||
return this.maxWireVersion >= 3;
|
||||
}
|
||||
get hasListIndexesCommand() {
|
||||
return this.maxWireVersion >= 3;
|
||||
}
|
||||
get supportsSnapshotReads() {
|
||||
return this.maxWireVersion >= 13;
|
||||
}
|
||||
get commandsTakeWriteConcern() {
|
||||
return this.maxWireVersion >= 5;
|
||||
}
|
||||
get commandsTakeCollation() {
|
||||
return this.maxWireVersion >= 5;
|
||||
}
|
||||
}
|
||||
exports.ServerCapabilities = ServerCapabilities;
|
||||
//# sourceMappingURL=topology.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+340
@@ -0,0 +1,340 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.TopologyDescription = void 0;
|
||||
const WIRE_CONSTANTS = require("../cmap/wire_protocol/constants");
|
||||
const error_1 = require("../error");
|
||||
const utils_1 = require("../utils");
|
||||
const common_1 = require("./common");
|
||||
const server_description_1 = require("./server_description");
|
||||
// constants related to compatibility checks
|
||||
const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
|
||||
const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION;
|
||||
const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION;
|
||||
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
|
||||
const MONGOS_OR_UNKNOWN = new Set([common_1.ServerType.Mongos, common_1.ServerType.Unknown]);
|
||||
const MONGOS_OR_STANDALONE = new Set([common_1.ServerType.Mongos, common_1.ServerType.Standalone]);
|
||||
const NON_PRIMARY_RS_MEMBERS = new Set([
|
||||
common_1.ServerType.RSSecondary,
|
||||
common_1.ServerType.RSArbiter,
|
||||
common_1.ServerType.RSOther
|
||||
]);
|
||||
/**
|
||||
* Representation of a deployment of servers
|
||||
* @public
|
||||
*/
|
||||
class TopologyDescription {
|
||||
/**
|
||||
* Create a TopologyDescription
|
||||
*/
|
||||
constructor(topologyType, serverDescriptions = null, setName = null, maxSetVersion = null, maxElectionId = null, commonWireVersion = null, options = null) {
|
||||
var _a, _b;
|
||||
options = options !== null && options !== void 0 ? options : {};
|
||||
this.type = topologyType !== null && topologyType !== void 0 ? topologyType : common_1.TopologyType.Unknown;
|
||||
this.servers = serverDescriptions !== null && serverDescriptions !== void 0 ? serverDescriptions : new Map();
|
||||
this.stale = false;
|
||||
this.compatible = true;
|
||||
this.heartbeatFrequencyMS = (_a = options.heartbeatFrequencyMS) !== null && _a !== void 0 ? _a : 0;
|
||||
this.localThresholdMS = (_b = options.localThresholdMS) !== null && _b !== void 0 ? _b : 15;
|
||||
this.setName = setName !== null && setName !== void 0 ? setName : null;
|
||||
this.maxElectionId = maxElectionId !== null && maxElectionId !== void 0 ? maxElectionId : null;
|
||||
this.maxSetVersion = maxSetVersion !== null && maxSetVersion !== void 0 ? maxSetVersion : null;
|
||||
this.commonWireVersion = commonWireVersion !== null && commonWireVersion !== void 0 ? commonWireVersion : 0;
|
||||
// determine server compatibility
|
||||
for (const serverDescription of this.servers.values()) {
|
||||
// Load balancer mode is always compatible.
|
||||
if (serverDescription.type === common_1.ServerType.Unknown ||
|
||||
serverDescription.type === common_1.ServerType.LoadBalancer) {
|
||||
continue;
|
||||
}
|
||||
if (serverDescription.minWireVersion > MAX_SUPPORTED_WIRE_VERSION) {
|
||||
this.compatible = false;
|
||||
this.compatibilityError = `Server at ${serverDescription.address} requires wire version ${serverDescription.minWireVersion}, but this version of the driver only supports up to ${MAX_SUPPORTED_WIRE_VERSION} (MongoDB ${MAX_SUPPORTED_SERVER_VERSION})`;
|
||||
}
|
||||
if (serverDescription.maxWireVersion < MIN_SUPPORTED_WIRE_VERSION) {
|
||||
this.compatible = false;
|
||||
this.compatibilityError = `Server at ${serverDescription.address} reports wire version ${serverDescription.maxWireVersion}, but this version of the driver requires at least ${MIN_SUPPORTED_WIRE_VERSION} (MongoDB ${MIN_SUPPORTED_SERVER_VERSION}).`;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Whenever a client updates the TopologyDescription from a hello response, it MUST set
|
||||
// TopologyDescription.logicalSessionTimeoutMinutes to the smallest logicalSessionTimeoutMinutes
|
||||
// value among ServerDescriptions of all data-bearing server types. If any have a null
|
||||
// logicalSessionTimeoutMinutes, then TopologyDescription.logicalSessionTimeoutMinutes MUST be
|
||||
// set to null.
|
||||
this.logicalSessionTimeoutMinutes = null;
|
||||
for (const [, server] of this.servers) {
|
||||
if (server.isReadable) {
|
||||
if (server.logicalSessionTimeoutMinutes == null) {
|
||||
// If any of the servers have a null logicalSessionsTimeout, then the whole topology does
|
||||
this.logicalSessionTimeoutMinutes = null;
|
||||
break;
|
||||
}
|
||||
if (this.logicalSessionTimeoutMinutes == null) {
|
||||
// First server with a non null logicalSessionsTimeout
|
||||
this.logicalSessionTimeoutMinutes = server.logicalSessionTimeoutMinutes;
|
||||
continue;
|
||||
}
|
||||
// Always select the smaller of the:
|
||||
// current server logicalSessionsTimeout and the topologies logicalSessionsTimeout
|
||||
this.logicalSessionTimeoutMinutes = Math.min(this.logicalSessionTimeoutMinutes, server.logicalSessionTimeoutMinutes);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Returns a new TopologyDescription based on the SrvPollingEvent
|
||||
* @internal
|
||||
*/
|
||||
updateFromSrvPollingEvent(ev, srvMaxHosts = 0) {
|
||||
/** The SRV addresses defines the set of addresses we should be using */
|
||||
const incomingHostnames = ev.hostnames();
|
||||
const currentHostnames = new Set(this.servers.keys());
|
||||
const hostnamesToAdd = new Set(incomingHostnames);
|
||||
const hostnamesToRemove = new Set();
|
||||
for (const hostname of currentHostnames) {
|
||||
// filter hostnamesToAdd (made from incomingHostnames) down to what is *not* present in currentHostnames
|
||||
hostnamesToAdd.delete(hostname);
|
||||
if (!incomingHostnames.has(hostname)) {
|
||||
// If the SRV Records no longer include this hostname
|
||||
// we have to stop using it
|
||||
hostnamesToRemove.add(hostname);
|
||||
}
|
||||
}
|
||||
if (hostnamesToAdd.size === 0 && hostnamesToRemove.size === 0) {
|
||||
// No new hosts to add and none to remove
|
||||
return this;
|
||||
}
|
||||
const serverDescriptions = new Map(this.servers);
|
||||
for (const removedHost of hostnamesToRemove) {
|
||||
serverDescriptions.delete(removedHost);
|
||||
}
|
||||
if (hostnamesToAdd.size > 0) {
|
||||
if (srvMaxHosts === 0) {
|
||||
// Add all!
|
||||
for (const hostToAdd of hostnamesToAdd) {
|
||||
serverDescriptions.set(hostToAdd, new server_description_1.ServerDescription(hostToAdd));
|
||||
}
|
||||
}
|
||||
else if (serverDescriptions.size < srvMaxHosts) {
|
||||
// Add only the amount needed to get us back to srvMaxHosts
|
||||
const selectedHosts = (0, utils_1.shuffle)(hostnamesToAdd, srvMaxHosts - serverDescriptions.size);
|
||||
for (const selectedHostToAdd of selectedHosts) {
|
||||
serverDescriptions.set(selectedHostToAdd, new server_description_1.ServerDescription(selectedHostToAdd));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TopologyDescription(this.type, serverDescriptions, this.setName, this.maxSetVersion, this.maxElectionId, this.commonWireVersion, { heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS });
|
||||
}
|
||||
/**
|
||||
* Returns a copy of this description updated with a given ServerDescription
|
||||
* @internal
|
||||
*/
|
||||
update(serverDescription) {
|
||||
const address = serverDescription.address;
|
||||
// potentially mutated values
|
||||
let { type: topologyType, setName, maxSetVersion, maxElectionId, commonWireVersion } = this;
|
||||
const serverType = serverDescription.type;
|
||||
const serverDescriptions = new Map(this.servers);
|
||||
// update common wire version
|
||||
if (serverDescription.maxWireVersion !== 0) {
|
||||
if (commonWireVersion == null) {
|
||||
commonWireVersion = serverDescription.maxWireVersion;
|
||||
}
|
||||
else {
|
||||
commonWireVersion = Math.min(commonWireVersion, serverDescription.maxWireVersion);
|
||||
}
|
||||
}
|
||||
if (typeof serverDescription.setName === 'string' &&
|
||||
typeof setName === 'string' &&
|
||||
serverDescription.setName !== setName) {
|
||||
if (topologyType === common_1.TopologyType.Single) {
|
||||
// "Single" Topology with setName mismatch is direct connection usage, mark unknown do not remove
|
||||
serverDescription = new server_description_1.ServerDescription(address);
|
||||
}
|
||||
else {
|
||||
serverDescriptions.delete(address);
|
||||
}
|
||||
}
|
||||
// update the actual server description
|
||||
serverDescriptions.set(address, serverDescription);
|
||||
if (topologyType === common_1.TopologyType.Single) {
|
||||
// once we are defined as single, that never changes
|
||||
return new TopologyDescription(common_1.TopologyType.Single, serverDescriptions, setName, maxSetVersion, maxElectionId, commonWireVersion, { heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS });
|
||||
}
|
||||
if (topologyType === common_1.TopologyType.Unknown) {
|
||||
if (serverType === common_1.ServerType.Standalone && this.servers.size !== 1) {
|
||||
serverDescriptions.delete(address);
|
||||
}
|
||||
else {
|
||||
topologyType = topologyTypeForServerType(serverType);
|
||||
}
|
||||
}
|
||||
if (topologyType === common_1.TopologyType.Sharded) {
|
||||
if (!MONGOS_OR_UNKNOWN.has(serverType)) {
|
||||
serverDescriptions.delete(address);
|
||||
}
|
||||
}
|
||||
if (topologyType === common_1.TopologyType.ReplicaSetNoPrimary) {
|
||||
if (MONGOS_OR_STANDALONE.has(serverType)) {
|
||||
serverDescriptions.delete(address);
|
||||
}
|
||||
if (serverType === common_1.ServerType.RSPrimary) {
|
||||
const result = updateRsFromPrimary(serverDescriptions, serverDescription, setName, maxSetVersion, maxElectionId);
|
||||
topologyType = result[0];
|
||||
setName = result[1];
|
||||
maxSetVersion = result[2];
|
||||
maxElectionId = result[3];
|
||||
}
|
||||
else if (NON_PRIMARY_RS_MEMBERS.has(serverType)) {
|
||||
const result = updateRsNoPrimaryFromMember(serverDescriptions, serverDescription, setName);
|
||||
topologyType = result[0];
|
||||
setName = result[1];
|
||||
}
|
||||
}
|
||||
if (topologyType === common_1.TopologyType.ReplicaSetWithPrimary) {
|
||||
if (MONGOS_OR_STANDALONE.has(serverType)) {
|
||||
serverDescriptions.delete(address);
|
||||
topologyType = checkHasPrimary(serverDescriptions);
|
||||
}
|
||||
else if (serverType === common_1.ServerType.RSPrimary) {
|
||||
const result = updateRsFromPrimary(serverDescriptions, serverDescription, setName, maxSetVersion, maxElectionId);
|
||||
topologyType = result[0];
|
||||
setName = result[1];
|
||||
maxSetVersion = result[2];
|
||||
maxElectionId = result[3];
|
||||
}
|
||||
else if (NON_PRIMARY_RS_MEMBERS.has(serverType)) {
|
||||
topologyType = updateRsWithPrimaryFromMember(serverDescriptions, serverDescription, setName);
|
||||
}
|
||||
else {
|
||||
topologyType = checkHasPrimary(serverDescriptions);
|
||||
}
|
||||
}
|
||||
return new TopologyDescription(topologyType, serverDescriptions, setName, maxSetVersion, maxElectionId, commonWireVersion, { heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS });
|
||||
}
|
||||
get error() {
|
||||
const descriptionsWithError = Array.from(this.servers.values()).filter((sd) => sd.error);
|
||||
if (descriptionsWithError.length > 0) {
|
||||
return descriptionsWithError[0].error;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Determines if the topology description has any known servers
|
||||
*/
|
||||
get hasKnownServers() {
|
||||
return Array.from(this.servers.values()).some((sd) => sd.type !== common_1.ServerType.Unknown);
|
||||
}
|
||||
/**
|
||||
* Determines if this topology description has a data-bearing server available.
|
||||
*/
|
||||
get hasDataBearingServers() {
|
||||
return Array.from(this.servers.values()).some((sd) => sd.isDataBearing);
|
||||
}
|
||||
/**
|
||||
* Determines if the topology has a definition for the provided address
|
||||
* @internal
|
||||
*/
|
||||
hasServer(address) {
|
||||
return this.servers.has(address);
|
||||
}
|
||||
}
|
||||
exports.TopologyDescription = TopologyDescription;
|
||||
function topologyTypeForServerType(serverType) {
|
||||
switch (serverType) {
|
||||
case common_1.ServerType.Standalone:
|
||||
return common_1.TopologyType.Single;
|
||||
case common_1.ServerType.Mongos:
|
||||
return common_1.TopologyType.Sharded;
|
||||
case common_1.ServerType.RSPrimary:
|
||||
return common_1.TopologyType.ReplicaSetWithPrimary;
|
||||
case common_1.ServerType.RSOther:
|
||||
case common_1.ServerType.RSSecondary:
|
||||
return common_1.TopologyType.ReplicaSetNoPrimary;
|
||||
default:
|
||||
return common_1.TopologyType.Unknown;
|
||||
}
|
||||
}
|
||||
function updateRsFromPrimary(serverDescriptions, serverDescription, setName = null, maxSetVersion = null, maxElectionId = null) {
|
||||
setName = setName || serverDescription.setName;
|
||||
if (setName !== serverDescription.setName) {
|
||||
serverDescriptions.delete(serverDescription.address);
|
||||
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
|
||||
}
|
||||
const electionId = serverDescription.electionId ? serverDescription.electionId : null;
|
||||
if (serverDescription.setVersion && electionId) {
|
||||
if (maxSetVersion && maxElectionId) {
|
||||
if (maxSetVersion > serverDescription.setVersion ||
|
||||
(0, utils_1.compareObjectId)(maxElectionId, electionId) > 0) {
|
||||
// this primary is stale, we must remove it
|
||||
serverDescriptions.set(serverDescription.address, new server_description_1.ServerDescription(serverDescription.address));
|
||||
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
|
||||
}
|
||||
}
|
||||
maxElectionId = serverDescription.electionId;
|
||||
}
|
||||
if (serverDescription.setVersion != null &&
|
||||
(maxSetVersion == null || serverDescription.setVersion > maxSetVersion)) {
|
||||
maxSetVersion = serverDescription.setVersion;
|
||||
}
|
||||
// We've heard from the primary. Is it the same primary as before?
|
||||
for (const [address, server] of serverDescriptions) {
|
||||
if (server.type === common_1.ServerType.RSPrimary && server.address !== serverDescription.address) {
|
||||
// Reset old primary's type to Unknown.
|
||||
serverDescriptions.set(address, new server_description_1.ServerDescription(server.address));
|
||||
// There can only be one primary
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Discover new hosts from this primary's response.
|
||||
serverDescription.allHosts.forEach((address) => {
|
||||
if (!serverDescriptions.has(address)) {
|
||||
serverDescriptions.set(address, new server_description_1.ServerDescription(address));
|
||||
}
|
||||
});
|
||||
// Remove hosts not in the response.
|
||||
const currentAddresses = Array.from(serverDescriptions.keys());
|
||||
const responseAddresses = serverDescription.allHosts;
|
||||
currentAddresses
|
||||
.filter((addr) => responseAddresses.indexOf(addr) === -1)
|
||||
.forEach((address) => {
|
||||
serverDescriptions.delete(address);
|
||||
});
|
||||
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
|
||||
}
|
||||
function updateRsWithPrimaryFromMember(serverDescriptions, serverDescription, setName = null) {
|
||||
if (setName == null) {
|
||||
// TODO(NODE-3483): should be an appropriate runtime error
|
||||
throw new error_1.MongoRuntimeError('Argument "setName" is required if connected to a replica set');
|
||||
}
|
||||
if (setName !== serverDescription.setName ||
|
||||
(serverDescription.me && serverDescription.address !== serverDescription.me)) {
|
||||
serverDescriptions.delete(serverDescription.address);
|
||||
}
|
||||
return checkHasPrimary(serverDescriptions);
|
||||
}
|
||||
function updateRsNoPrimaryFromMember(serverDescriptions, serverDescription, setName = null) {
|
||||
const topologyType = common_1.TopologyType.ReplicaSetNoPrimary;
|
||||
setName = setName !== null && setName !== void 0 ? setName : serverDescription.setName;
|
||||
if (setName !== serverDescription.setName) {
|
||||
serverDescriptions.delete(serverDescription.address);
|
||||
return [topologyType, setName];
|
||||
}
|
||||
serverDescription.allHosts.forEach((address) => {
|
||||
if (!serverDescriptions.has(address)) {
|
||||
serverDescriptions.set(address, new server_description_1.ServerDescription(address));
|
||||
}
|
||||
});
|
||||
if (serverDescription.me && serverDescription.address !== serverDescription.me) {
|
||||
serverDescriptions.delete(serverDescription.address);
|
||||
}
|
||||
return [topologyType, setName];
|
||||
}
|
||||
function checkHasPrimary(serverDescriptions) {
|
||||
for (const serverDescription of serverDescriptions.values()) {
|
||||
if (serverDescription.type === common_1.ServerType.RSPrimary) {
|
||||
return common_1.TopologyType.ReplicaSetWithPrimary;
|
||||
}
|
||||
}
|
||||
return common_1.TopologyType.ReplicaSetNoPrimary;
|
||||
}
|
||||
//# sourceMappingURL=topology_description.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user