@depup/mongodb 7.1.0-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +201 -0
- package/README.md +33 -0
- package/etc/prepare.js +12 -0
- package/lib/admin.js +136 -0
- package/lib/admin.js.map +1 -0
- package/lib/bson.js +85 -0
- package/lib/bson.js.map +1 -0
- package/lib/bulk/common.js +835 -0
- package/lib/bulk/common.js.map +1 -0
- package/lib/bulk/ordered.js +67 -0
- package/lib/bulk/ordered.js.map +1 -0
- package/lib/bulk/unordered.js +92 -0
- package/lib/bulk/unordered.js.map +1 -0
- package/lib/change_stream.js +513 -0
- package/lib/change_stream.js.map +1 -0
- package/lib/client-side-encryption/auto_encrypter.js +272 -0
- package/lib/client-side-encryption/auto_encrypter.js.map +1 -0
- package/lib/client-side-encryption/client_encryption.js +609 -0
- package/lib/client-side-encryption/client_encryption.js.map +1 -0
- package/lib/client-side-encryption/errors.js +138 -0
- package/lib/client-side-encryption/errors.js.map +1 -0
- package/lib/client-side-encryption/mongocryptd_manager.js +85 -0
- package/lib/client-side-encryption/mongocryptd_manager.js.map +1 -0
- package/lib/client-side-encryption/providers/aws.js +23 -0
- package/lib/client-side-encryption/providers/aws.js.map +1 -0
- package/lib/client-side-encryption/providers/azure.js +132 -0
- package/lib/client-side-encryption/providers/azure.js.map +1 -0
- package/lib/client-side-encryption/providers/gcp.js +16 -0
- package/lib/client-side-encryption/providers/gcp.js.map +1 -0
- package/lib/client-side-encryption/providers/index.js +43 -0
- package/lib/client-side-encryption/providers/index.js.map +1 -0
- package/lib/client-side-encryption/state_machine.js +427 -0
- package/lib/client-side-encryption/state_machine.js.map +1 -0
- package/lib/cmap/auth/auth_provider.js +51 -0
- package/lib/cmap/auth/auth_provider.js.map +1 -0
- package/lib/cmap/auth/aws4.js +161 -0
- package/lib/cmap/auth/aws4.js.map +1 -0
- package/lib/cmap/auth/aws_temporary_credentials.js +103 -0
- package/lib/cmap/auth/aws_temporary_credentials.js.map +1 -0
- package/lib/cmap/auth/gssapi.js +155 -0
- package/lib/cmap/auth/gssapi.js.map +1 -0
- package/lib/cmap/auth/mongo_credentials.js +169 -0
- package/lib/cmap/auth/mongo_credentials.js.map +1 -0
- package/lib/cmap/auth/mongodb_aws.js +128 -0
- package/lib/cmap/auth/mongodb_aws.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js +84 -0
- package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js +62 -0
- package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/callback_workflow.js +141 -0
- package/lib/cmap/auth/mongodb_oidc/callback_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/command_builders.js +44 -0
- package/lib/cmap/auth/mongodb_oidc/command_builders.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js +39 -0
- package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/human_callback_workflow.js +122 -0
- package/lib/cmap/auth/mongodb_oidc/human_callback_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js +32 -0
- package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/token_cache.js +52 -0
- package/lib/cmap/auth/mongodb_oidc/token_cache.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js +23 -0
- package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js.map +1 -0
- package/lib/cmap/auth/mongodb_oidc.js +73 -0
- package/lib/cmap/auth/mongodb_oidc.js.map +1 -0
- package/lib/cmap/auth/plain.js +26 -0
- package/lib/cmap/auth/plain.js.map +1 -0
- package/lib/cmap/auth/providers.js +22 -0
- package/lib/cmap/auth/providers.js.map +1 -0
- package/lib/cmap/auth/scram.js +254 -0
- package/lib/cmap/auth/scram.js.map +1 -0
- package/lib/cmap/auth/x509.js +36 -0
- package/lib/cmap/auth/x509.js.map +1 -0
- package/lib/cmap/command_monitoring_events.js +223 -0
- package/lib/cmap/command_monitoring_events.js.map +1 -0
- package/lib/cmap/commands.js +535 -0
- package/lib/cmap/commands.js.map +1 -0
- package/lib/cmap/connect.js +381 -0
- package/lib/cmap/connect.js.map +1 -0
- package/lib/cmap/connection.js +571 -0
- package/lib/cmap/connection.js.map +1 -0
- package/lib/cmap/connection_pool.js +558 -0
- package/lib/cmap/connection_pool.js.map +1 -0
- package/lib/cmap/connection_pool_events.js +190 -0
- package/lib/cmap/connection_pool_events.js.map +1 -0
- package/lib/cmap/errors.js +108 -0
- package/lib/cmap/errors.js.map +1 -0
- package/lib/cmap/handshake/client_metadata.js +242 -0
- package/lib/cmap/handshake/client_metadata.js.map +1 -0
- package/lib/cmap/metrics.js +62 -0
- package/lib/cmap/metrics.js.map +1 -0
- package/lib/cmap/stream_description.js +70 -0
- package/lib/cmap/stream_description.js.map +1 -0
- package/lib/cmap/wire_protocol/compression.js +178 -0
- package/lib/cmap/wire_protocol/compression.js.map +1 -0
- package/lib/cmap/wire_protocol/constants.js +21 -0
- package/lib/cmap/wire_protocol/constants.js.map +1 -0
- package/lib/cmap/wire_protocol/on_data.js +111 -0
- package/lib/cmap/wire_protocol/on_data.js.map +1 -0
- package/lib/cmap/wire_protocol/on_demand/document.js +222 -0
- package/lib/cmap/wire_protocol/on_demand/document.js.map +1 -0
- package/lib/cmap/wire_protocol/responses.js +315 -0
- package/lib/cmap/wire_protocol/responses.js.map +1 -0
- package/lib/cmap/wire_protocol/shared.js +35 -0
- package/lib/cmap/wire_protocol/shared.js.map +1 -0
- package/lib/collection.js +751 -0
- package/lib/collection.js.map +1 -0
- package/lib/connection_string.js +1090 -0
- package/lib/connection_string.js.map +1 -0
- package/lib/constants.js +170 -0
- package/lib/constants.js.map +1 -0
- package/lib/cursor/abstract_cursor.js +924 -0
- package/lib/cursor/abstract_cursor.js.map +1 -0
- package/lib/cursor/aggregation_cursor.js +164 -0
- package/lib/cursor/aggregation_cursor.js.map +1 -0
- package/lib/cursor/change_stream_cursor.js +104 -0
- package/lib/cursor/change_stream_cursor.js.map +1 -0
- package/lib/cursor/client_bulk_write_cursor.js +52 -0
- package/lib/cursor/client_bulk_write_cursor.js.map +1 -0
- package/lib/cursor/explainable_cursor.js +36 -0
- package/lib/cursor/explainable_cursor.js.map +1 -0
- package/lib/cursor/find_cursor.js +399 -0
- package/lib/cursor/find_cursor.js.map +1 -0
- package/lib/cursor/list_collections_cursor.js +34 -0
- package/lib/cursor/list_collections_cursor.js.map +1 -0
- package/lib/cursor/list_indexes_cursor.js +32 -0
- package/lib/cursor/list_indexes_cursor.js.map +1 -0
- package/lib/cursor/list_search_indexes_cursor.js +14 -0
- package/lib/cursor/list_search_indexes_cursor.js.map +1 -0
- package/lib/cursor/run_command_cursor.js +94 -0
- package/lib/cursor/run_command_cursor.js.map +1 -0
- package/lib/db.js +419 -0
- package/lib/db.js.map +1 -0
- package/lib/deps.js +112 -0
- package/lib/deps.js.map +1 -0
- package/lib/encrypter.js +106 -0
- package/lib/encrypter.js.map +1 -0
- package/lib/error.js +1380 -0
- package/lib/error.js.map +1 -0
- package/lib/explain.js +59 -0
- package/lib/explain.js.map +1 -0
- package/lib/gridfs/download.js +305 -0
- package/lib/gridfs/download.js.map +1 -0
- package/lib/gridfs/index.js +164 -0
- package/lib/gridfs/index.js.map +1 -0
- package/lib/gridfs/upload.js +358 -0
- package/lib/gridfs/upload.js.map +1 -0
- package/lib/index.js +193 -0
- package/lib/index.js.map +1 -0
- package/lib/mongo_client.js +557 -0
- package/lib/mongo_client.js.map +1 -0
- package/lib/mongo_client_auth_providers.js +80 -0
- package/lib/mongo_client_auth_providers.js.map +1 -0
- package/lib/mongo_logger.js +655 -0
- package/lib/mongo_logger.js.map +1 -0
- package/lib/mongo_types.js +56 -0
- package/lib/mongo_types.js.map +1 -0
- package/lib/operations/aggregate.js +90 -0
- package/lib/operations/aggregate.js.map +1 -0
- package/lib/operations/client_bulk_write/client_bulk_write.js +51 -0
- package/lib/operations/client_bulk_write/client_bulk_write.js.map +1 -0
- package/lib/operations/client_bulk_write/command_builder.js +340 -0
- package/lib/operations/client_bulk_write/command_builder.js.map +1 -0
- package/lib/operations/client_bulk_write/common.js +3 -0
- package/lib/operations/client_bulk_write/common.js.map +1 -0
- package/lib/operations/client_bulk_write/executor.js +120 -0
- package/lib/operations/client_bulk_write/executor.js.map +1 -0
- package/lib/operations/client_bulk_write/results_merger.js +204 -0
- package/lib/operations/client_bulk_write/results_merger.js.map +1 -0
- package/lib/operations/command.js +83 -0
- package/lib/operations/command.js.map +1 -0
- package/lib/operations/count.js +45 -0
- package/lib/operations/count.js.map +1 -0
- package/lib/operations/create_collection.js +109 -0
- package/lib/operations/create_collection.js.map +1 -0
- package/lib/operations/delete.js +125 -0
- package/lib/operations/delete.js.map +1 -0
- package/lib/operations/distinct.js +61 -0
- package/lib/operations/distinct.js.map +1 -0
- package/lib/operations/drop.js +93 -0
- package/lib/operations/drop.js.map +1 -0
- package/lib/operations/end_sessions.js +34 -0
- package/lib/operations/end_sessions.js.map +1 -0
- package/lib/operations/estimated_document_count.js +41 -0
- package/lib/operations/estimated_document_count.js.map +1 -0
- package/lib/operations/execute_operation.js +239 -0
- package/lib/operations/execute_operation.js.map +1 -0
- package/lib/operations/find.js +148 -0
- package/lib/operations/find.js.map +1 -0
- package/lib/operations/find_and_modify.js +158 -0
- package/lib/operations/find_and_modify.js.map +1 -0
- package/lib/operations/get_more.js +62 -0
- package/lib/operations/get_more.js.map +1 -0
- package/lib/operations/indexes.js +186 -0
- package/lib/operations/indexes.js.map +1 -0
- package/lib/operations/insert.js +70 -0
- package/lib/operations/insert.js.map +1 -0
- package/lib/operations/kill_cursors.js +43 -0
- package/lib/operations/kill_cursors.js.map +1 -0
- package/lib/operations/list_collections.js +53 -0
- package/lib/operations/list_collections.js.map +1 -0
- package/lib/operations/list_databases.js +40 -0
- package/lib/operations/list_databases.js.map +1 -0
- package/lib/operations/operation.js +102 -0
- package/lib/operations/operation.js.map +1 -0
- package/lib/operations/profiling_level.js +43 -0
- package/lib/operations/profiling_level.js.map +1 -0
- package/lib/operations/remove_user.js +27 -0
- package/lib/operations/remove_user.js.map +1 -0
- package/lib/operations/rename.js +38 -0
- package/lib/operations/rename.js.map +1 -0
- package/lib/operations/run_command.js +47 -0
- package/lib/operations/run_command.js.map +1 -0
- package/lib/operations/search_indexes/create.js +33 -0
- package/lib/operations/search_indexes/create.js.map +1 -0
- package/lib/operations/search_indexes/drop.js +43 -0
- package/lib/operations/search_indexes/drop.js.map +1 -0
- package/lib/operations/search_indexes/update.js +35 -0
- package/lib/operations/search_indexes/update.js.map +1 -0
- package/lib/operations/set_profiling_level.js +53 -0
- package/lib/operations/set_profiling_level.js.map +1 -0
- package/lib/operations/stats.js +27 -0
- package/lib/operations/stats.js.map +1 -0
- package/lib/operations/update.js +188 -0
- package/lib/operations/update.js.map +1 -0
- package/lib/operations/validate_collection.js +37 -0
- package/lib/operations/validate_collection.js.map +1 -0
- package/lib/read_concern.js +73 -0
- package/lib/read_concern.js.map +1 -0
- package/lib/read_preference.js +191 -0
- package/lib/read_preference.js.map +1 -0
- package/lib/sdam/common.js +49 -0
- package/lib/sdam/common.js.map +1 -0
- package/lib/sdam/events.js +146 -0
- package/lib/sdam/events.js.map +1 -0
- package/lib/sdam/monitor.js +544 -0
- package/lib/sdam/monitor.js.map +1 -0
- package/lib/sdam/server.js +404 -0
- package/lib/sdam/server.js.map +1 -0
- package/lib/sdam/server_description.js +204 -0
- package/lib/sdam/server_description.js.map +1 -0
- package/lib/sdam/server_selection.js +294 -0
- package/lib/sdam/server_selection.js.map +1 -0
- package/lib/sdam/server_selection_events.js +85 -0
- package/lib/sdam/server_selection_events.js.map +1 -0
- package/lib/sdam/srv_polling.js +108 -0
- package/lib/sdam/srv_polling.js.map +1 -0
- package/lib/sdam/topology.js +652 -0
- package/lib/sdam/topology.js.map +1 -0
- package/lib/sdam/topology_description.js +383 -0
- package/lib/sdam/topology_description.js.map +1 -0
- package/lib/sessions.js +892 -0
- package/lib/sessions.js.map +1 -0
- package/lib/sort.js +103 -0
- package/lib/sort.js.map +1 -0
- package/lib/timeout.js +296 -0
- package/lib/timeout.js.map +1 -0
- package/lib/transactions.js +135 -0
- package/lib/transactions.js.map +1 -0
- package/lib/utils.js +1163 -0
- package/lib/utils.js.map +1 -0
- package/lib/write_concern.js +100 -0
- package/lib/write_concern.js.map +1 -0
- package/mongodb.d.ts +9018 -0
- package/package.json +197 -0
- package/src/admin.ts +173 -0
- package/src/bson.ts +149 -0
- package/src/bulk/common.ts +1251 -0
- package/src/bulk/ordered.ts +83 -0
- package/src/bulk/unordered.ts +115 -0
- package/src/change_stream.ts +1160 -0
- package/src/client-side-encryption/auto_encrypter.ts +469 -0
- package/src/client-side-encryption/client_encryption.ts +1150 -0
- package/src/client-side-encryption/errors.ts +144 -0
- package/src/client-side-encryption/mongocryptd_manager.ts +100 -0
- package/src/client-side-encryption/providers/aws.ts +33 -0
- package/src/client-side-encryption/providers/azure.ts +181 -0
- package/src/client-side-encryption/providers/gcp.ts +16 -0
- package/src/client-side-encryption/providers/index.ts +207 -0
- package/src/client-side-encryption/state_machine.ts +651 -0
- package/src/cmap/auth/auth_provider.ts +77 -0
- package/src/cmap/auth/aws4.ts +207 -0
- package/src/cmap/auth/aws_temporary_credentials.ts +129 -0
- package/src/cmap/auth/gssapi.ts +203 -0
- package/src/cmap/auth/mongo_credentials.ts +292 -0
- package/src/cmap/auth/mongodb_aws.ts +179 -0
- package/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts +88 -0
- package/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts +73 -0
- package/src/cmap/auth/mongodb_oidc/callback_workflow.ts +188 -0
- package/src/cmap/auth/mongodb_oidc/command_builders.ts +53 -0
- package/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts +46 -0
- package/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts +141 -0
- package/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts +31 -0
- package/src/cmap/auth/mongodb_oidc/token_cache.ts +62 -0
- package/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts +22 -0
- package/src/cmap/auth/mongodb_oidc.ts +185 -0
- package/src/cmap/auth/plain.ts +25 -0
- package/src/cmap/auth/providers.ts +22 -0
- package/src/cmap/auth/scram.ts +344 -0
- package/src/cmap/auth/x509.ts +43 -0
- package/src/cmap/command_monitoring_events.ts +316 -0
- package/src/cmap/commands.ts +788 -0
- package/src/cmap/connect.ts +518 -0
- package/src/cmap/connection.ts +931 -0
- package/src/cmap/connection_pool.ts +831 -0
- package/src/cmap/connection_pool_events.ts +300 -0
- package/src/cmap/errors.ts +119 -0
- package/src/cmap/handshake/client_metadata.ts +353 -0
- package/src/cmap/metrics.ts +58 -0
- package/src/cmap/stream_description.ts +96 -0
- package/src/cmap/wire_protocol/compression.ts +211 -0
- package/src/cmap/wire_protocol/constants.ts +17 -0
- package/src/cmap/wire_protocol/on_data.ts +139 -0
- package/src/cmap/wire_protocol/on_demand/document.ts +358 -0
- package/src/cmap/wire_protocol/responses.ts +393 -0
- package/src/cmap/wire_protocol/shared.ts +48 -0
- package/src/collection.ts +1314 -0
- package/src/connection_string.ts +1297 -0
- package/src/constants.ts +176 -0
- package/src/cursor/abstract_cursor.ts +1247 -0
- package/src/cursor/aggregation_cursor.ts +245 -0
- package/src/cursor/change_stream_cursor.ts +171 -0
- package/src/cursor/client_bulk_write_cursor.ts +83 -0
- package/src/cursor/explainable_cursor.ts +51 -0
- package/src/cursor/find_cursor.ts +497 -0
- package/src/cursor/list_collections_cursor.ts +50 -0
- package/src/cursor/list_indexes_cursor.ts +37 -0
- package/src/cursor/list_search_indexes_cursor.ts +20 -0
- package/src/cursor/run_command_cursor.ts +178 -0
- package/src/db.ts +621 -0
- package/src/deps.ts +227 -0
- package/src/encrypter.ts +127 -0
- package/src/error.ts +1567 -0
- package/src/explain.ts +124 -0
- package/src/gridfs/download.ts +483 -0
- package/src/gridfs/index.ts +264 -0
- package/src/gridfs/upload.ts +537 -0
- package/src/index.ts +633 -0
- package/src/mongo_client.ts +1155 -0
- package/src/mongo_client_auth_providers.ts +87 -0
- package/src/mongo_logger.ts +1081 -0
- package/src/mongo_types.ts +678 -0
- package/src/operations/aggregate.ts +153 -0
- package/src/operations/client_bulk_write/client_bulk_write.ts +72 -0
- package/src/operations/client_bulk_write/command_builder.ts +487 -0
- package/src/operations/client_bulk_write/common.ts +276 -0
- package/src/operations/client_bulk_write/executor.ts +149 -0
- package/src/operations/client_bulk_write/results_merger.ts +260 -0
- package/src/operations/command.ts +171 -0
- package/src/operations/count.ts +74 -0
- package/src/operations/create_collection.ts +218 -0
- package/src/operations/delete.ts +183 -0
- package/src/operations/distinct.ts +92 -0
- package/src/operations/drop.ts +130 -0
- package/src/operations/end_sessions.ts +44 -0
- package/src/operations/estimated_document_count.ts +61 -0
- package/src/operations/execute_operation.ts +319 -0
- package/src/operations/find.ts +254 -0
- package/src/operations/find_and_modify.ts +319 -0
- package/src/operations/get_more.ts +108 -0
- package/src/operations/indexes.ts +418 -0
- package/src/operations/insert.ts +108 -0
- package/src/operations/kill_cursors.ts +64 -0
- package/src/operations/list_collections.ts +108 -0
- package/src/operations/list_databases.ts +68 -0
- package/src/operations/operation.ts +181 -0
- package/src/operations/profiling_level.ts +46 -0
- package/src/operations/remove_user.ts +36 -0
- package/src/operations/rename.ts +60 -0
- package/src/operations/run_command.ts +79 -0
- package/src/operations/search_indexes/create.ts +56 -0
- package/src/operations/search_indexes/drop.ts +58 -0
- package/src/operations/search_indexes/update.ts +45 -0
- package/src/operations/set_profiling_level.ts +74 -0
- package/src/operations/stats.ts +37 -0
- package/src/operations/update.ts +318 -0
- package/src/operations/validate_collection.ts +50 -0
- package/src/read_concern.ts +88 -0
- package/src/read_preference.ts +256 -0
- package/src/sdam/common.ts +74 -0
- package/src/sdam/events.ts +219 -0
- package/src/sdam/monitor.ts +771 -0
- package/src/sdam/server.ts +595 -0
- package/src/sdam/server_description.ts +297 -0
- package/src/sdam/server_selection.ts +415 -0
- package/src/sdam/server_selection_events.ts +142 -0
- package/src/sdam/srv_polling.ts +146 -0
- package/src/sdam/topology.ts +1100 -0
- package/src/sdam/topology_description.ts +548 -0
- package/src/sessions.ts +1244 -0
- package/src/sort.ts +141 -0
- package/src/timeout.ts +405 -0
- package/src/transactions.ts +181 -0
- package/src/utils.ts +1434 -0
- package/src/write_concern.ts +183 -0
- package/tsconfig.json +46 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
import { EJSON, type ObjectId } from '../bson';
|
|
2
|
+
import * as WIRE_CONSTANTS from '../cmap/wire_protocol/constants';
|
|
3
|
+
import { type MongoError, MongoRuntimeError, MongoStalePrimaryError } from '../error';
|
|
4
|
+
import { compareObjectId, shuffle } from '../utils';
|
|
5
|
+
import { ServerType, TopologyType } from './common';
|
|
6
|
+
import { ServerDescription } from './server_description';
|
|
7
|
+
import type { SrvPollingEvent } from './srv_polling';
|
|
8
|
+
|
|
9
|
+
// constants related to compatibility checks
|
|
10
|
+
const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
|
|
11
|
+
const MAX_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_SERVER_VERSION;
|
|
12
|
+
const MIN_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_WIRE_VERSION;
|
|
13
|
+
const MAX_SUPPORTED_WIRE_VERSION = WIRE_CONSTANTS.MAX_SUPPORTED_WIRE_VERSION;
|
|
14
|
+
|
|
15
|
+
const MONGOS_OR_UNKNOWN = new Set<ServerType>([ServerType.Mongos, ServerType.Unknown]);
|
|
16
|
+
const MONGOS_OR_STANDALONE = new Set<ServerType>([ServerType.Mongos, ServerType.Standalone]);
|
|
17
|
+
const NON_PRIMARY_RS_MEMBERS = new Set<ServerType>([
|
|
18
|
+
ServerType.RSSecondary,
|
|
19
|
+
ServerType.RSArbiter,
|
|
20
|
+
ServerType.RSOther
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
/** @public */
|
|
24
|
+
export interface TopologyDescriptionOptions {
|
|
25
|
+
heartbeatFrequencyMS?: number;
|
|
26
|
+
localThresholdMS?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Representation of a deployment of servers
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export class TopologyDescription {
|
|
34
|
+
type: TopologyType;
|
|
35
|
+
setName: string | null;
|
|
36
|
+
maxSetVersion: number | null;
|
|
37
|
+
maxElectionId: ObjectId | null;
|
|
38
|
+
servers: Map<string, ServerDescription>;
|
|
39
|
+
stale: boolean;
|
|
40
|
+
compatible: boolean;
|
|
41
|
+
compatibilityError?: string;
|
|
42
|
+
logicalSessionTimeoutMinutes: number | null;
|
|
43
|
+
heartbeatFrequencyMS: number;
|
|
44
|
+
localThresholdMS: number;
|
|
45
|
+
commonWireVersion: number;
|
|
46
|
+
/**
|
|
47
|
+
* Create a TopologyDescription
|
|
48
|
+
*/
|
|
49
|
+
constructor(
|
|
50
|
+
topologyType: TopologyType,
|
|
51
|
+
serverDescriptions: Map<string, ServerDescription> | null = null,
|
|
52
|
+
setName: string | null = null,
|
|
53
|
+
maxSetVersion: number | null = null,
|
|
54
|
+
maxElectionId: ObjectId | null = null,
|
|
55
|
+
commonWireVersion: number | null = null,
|
|
56
|
+
options: TopologyDescriptionOptions | null = null
|
|
57
|
+
) {
|
|
58
|
+
options = options ?? {};
|
|
59
|
+
|
|
60
|
+
this.type = topologyType ?? TopologyType.Unknown;
|
|
61
|
+
this.servers = serverDescriptions ?? new Map();
|
|
62
|
+
this.stale = false;
|
|
63
|
+
this.compatible = true;
|
|
64
|
+
this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 0;
|
|
65
|
+
this.localThresholdMS = options.localThresholdMS ?? 15;
|
|
66
|
+
this.setName = setName ?? null;
|
|
67
|
+
this.maxElectionId = maxElectionId ?? null;
|
|
68
|
+
this.maxSetVersion = maxSetVersion ?? null;
|
|
69
|
+
this.commonWireVersion = commonWireVersion ?? 0;
|
|
70
|
+
|
|
71
|
+
// determine server compatibility
|
|
72
|
+
for (const serverDescription of this.servers.values()) {
|
|
73
|
+
// Load balancer mode is always compatible.
|
|
74
|
+
if (
|
|
75
|
+
serverDescription.type === ServerType.Unknown ||
|
|
76
|
+
serverDescription.type === ServerType.LoadBalancer
|
|
77
|
+
) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (serverDescription.minWireVersion > MAX_SUPPORTED_WIRE_VERSION) {
|
|
82
|
+
this.compatible = false;
|
|
83
|
+
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})`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (serverDescription.maxWireVersion < MIN_SUPPORTED_WIRE_VERSION) {
|
|
87
|
+
this.compatible = false;
|
|
88
|
+
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}).`;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Whenever a client updates the TopologyDescription from a hello response, it MUST set
|
|
94
|
+
// TopologyDescription.logicalSessionTimeoutMinutes to the smallest logicalSessionTimeoutMinutes
|
|
95
|
+
// value among ServerDescriptions of all data-bearing server types. If any have a null
|
|
96
|
+
// logicalSessionTimeoutMinutes, then TopologyDescription.logicalSessionTimeoutMinutes MUST be
|
|
97
|
+
// set to null.
|
|
98
|
+
this.logicalSessionTimeoutMinutes = null;
|
|
99
|
+
for (const [, server] of this.servers) {
|
|
100
|
+
if (server.isReadable) {
|
|
101
|
+
if (server.logicalSessionTimeoutMinutes == null) {
|
|
102
|
+
// If any of the servers have a null logicalSessionsTimeout, then the whole topology does
|
|
103
|
+
this.logicalSessionTimeoutMinutes = null;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (this.logicalSessionTimeoutMinutes == null) {
|
|
108
|
+
// First server with a non null logicalSessionsTimeout
|
|
109
|
+
this.logicalSessionTimeoutMinutes = server.logicalSessionTimeoutMinutes;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Always select the smaller of the:
|
|
114
|
+
// current server logicalSessionsTimeout and the topologies logicalSessionsTimeout
|
|
115
|
+
this.logicalSessionTimeoutMinutes = Math.min(
|
|
116
|
+
this.logicalSessionTimeoutMinutes,
|
|
117
|
+
server.logicalSessionTimeoutMinutes
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns a new TopologyDescription based on the SrvPollingEvent
|
|
125
|
+
* @internal
|
|
126
|
+
*/
|
|
127
|
+
updateFromSrvPollingEvent(ev: SrvPollingEvent, srvMaxHosts = 0): TopologyDescription {
|
|
128
|
+
/** The SRV addresses defines the set of addresses we should be using */
|
|
129
|
+
const incomingHostnames = ev.hostnames();
|
|
130
|
+
const currentHostnames = new Set(this.servers.keys());
|
|
131
|
+
|
|
132
|
+
const hostnamesToAdd = new Set<string>(incomingHostnames);
|
|
133
|
+
const hostnamesToRemove = new Set<string>();
|
|
134
|
+
for (const hostname of currentHostnames) {
|
|
135
|
+
// filter hostnamesToAdd (made from incomingHostnames) down to what is *not* present in currentHostnames
|
|
136
|
+
hostnamesToAdd.delete(hostname);
|
|
137
|
+
if (!incomingHostnames.has(hostname)) {
|
|
138
|
+
// If the SRV Records no longer include this hostname
|
|
139
|
+
// we have to stop using it
|
|
140
|
+
hostnamesToRemove.add(hostname);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (hostnamesToAdd.size === 0 && hostnamesToRemove.size === 0) {
|
|
145
|
+
// No new hosts to add and none to remove
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const serverDescriptions = new Map(this.servers);
|
|
150
|
+
for (const removedHost of hostnamesToRemove) {
|
|
151
|
+
serverDescriptions.delete(removedHost);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (hostnamesToAdd.size > 0) {
|
|
155
|
+
if (srvMaxHosts === 0) {
|
|
156
|
+
// Add all!
|
|
157
|
+
for (const hostToAdd of hostnamesToAdd) {
|
|
158
|
+
serverDescriptions.set(hostToAdd, new ServerDescription(hostToAdd));
|
|
159
|
+
}
|
|
160
|
+
} else if (serverDescriptions.size < srvMaxHosts) {
|
|
161
|
+
// Add only the amount needed to get us back to srvMaxHosts
|
|
162
|
+
const selectedHosts = shuffle(hostnamesToAdd, srvMaxHosts - serverDescriptions.size);
|
|
163
|
+
for (const selectedHostToAdd of selectedHosts) {
|
|
164
|
+
serverDescriptions.set(selectedHostToAdd, new ServerDescription(selectedHostToAdd));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return new TopologyDescription(
|
|
170
|
+
this.type,
|
|
171
|
+
serverDescriptions,
|
|
172
|
+
this.setName,
|
|
173
|
+
this.maxSetVersion,
|
|
174
|
+
this.maxElectionId,
|
|
175
|
+
this.commonWireVersion,
|
|
176
|
+
{ heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS }
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns a copy of this description updated with a given ServerDescription
|
|
182
|
+
* @internal
|
|
183
|
+
*/
|
|
184
|
+
update(serverDescription: ServerDescription): TopologyDescription {
|
|
185
|
+
const address = serverDescription.address;
|
|
186
|
+
|
|
187
|
+
// potentially mutated values
|
|
188
|
+
let { type: topologyType, setName, maxSetVersion, maxElectionId, commonWireVersion } = this;
|
|
189
|
+
|
|
190
|
+
const serverType = serverDescription.type;
|
|
191
|
+
const serverDescriptions = new Map(this.servers);
|
|
192
|
+
|
|
193
|
+
// update common wire version
|
|
194
|
+
if (serverDescription.maxWireVersion !== 0) {
|
|
195
|
+
if (commonWireVersion === 0) {
|
|
196
|
+
commonWireVersion = serverDescription.maxWireVersion;
|
|
197
|
+
} else {
|
|
198
|
+
commonWireVersion = Math.min(commonWireVersion, serverDescription.maxWireVersion);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (
|
|
203
|
+
typeof serverDescription.setName === 'string' &&
|
|
204
|
+
typeof setName === 'string' &&
|
|
205
|
+
serverDescription.setName !== setName
|
|
206
|
+
) {
|
|
207
|
+
if (topologyType === TopologyType.Single) {
|
|
208
|
+
// "Single" Topology with setName mismatch is direct connection usage, mark unknown do not remove
|
|
209
|
+
serverDescription = new ServerDescription(address);
|
|
210
|
+
} else {
|
|
211
|
+
serverDescriptions.delete(address);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// update the actual server description
|
|
216
|
+
serverDescriptions.set(address, serverDescription);
|
|
217
|
+
|
|
218
|
+
if (topologyType === TopologyType.Single) {
|
|
219
|
+
// once we are defined as single, that never changes
|
|
220
|
+
return new TopologyDescription(
|
|
221
|
+
TopologyType.Single,
|
|
222
|
+
serverDescriptions,
|
|
223
|
+
setName,
|
|
224
|
+
maxSetVersion,
|
|
225
|
+
maxElectionId,
|
|
226
|
+
commonWireVersion,
|
|
227
|
+
{ heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS }
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (topologyType === TopologyType.Unknown) {
|
|
232
|
+
if (serverType === ServerType.Standalone && this.servers.size !== 1) {
|
|
233
|
+
serverDescriptions.delete(address);
|
|
234
|
+
} else {
|
|
235
|
+
topologyType = topologyTypeForServerType(serverType);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (topologyType === TopologyType.Sharded) {
|
|
240
|
+
if (!MONGOS_OR_UNKNOWN.has(serverType)) {
|
|
241
|
+
serverDescriptions.delete(address);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (topologyType === TopologyType.ReplicaSetNoPrimary) {
|
|
246
|
+
if (MONGOS_OR_STANDALONE.has(serverType)) {
|
|
247
|
+
serverDescriptions.delete(address);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (serverType === ServerType.RSPrimary) {
|
|
251
|
+
const result = updateRsFromPrimary(
|
|
252
|
+
serverDescriptions,
|
|
253
|
+
serverDescription,
|
|
254
|
+
setName,
|
|
255
|
+
maxSetVersion,
|
|
256
|
+
maxElectionId
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
topologyType = result[0];
|
|
260
|
+
setName = result[1];
|
|
261
|
+
maxSetVersion = result[2];
|
|
262
|
+
maxElectionId = result[3];
|
|
263
|
+
} else if (NON_PRIMARY_RS_MEMBERS.has(serverType)) {
|
|
264
|
+
const result = updateRsNoPrimaryFromMember(serverDescriptions, serverDescription, setName);
|
|
265
|
+
topologyType = result[0];
|
|
266
|
+
setName = result[1];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (topologyType === TopologyType.ReplicaSetWithPrimary) {
|
|
271
|
+
if (MONGOS_OR_STANDALONE.has(serverType)) {
|
|
272
|
+
serverDescriptions.delete(address);
|
|
273
|
+
topologyType = checkHasPrimary(serverDescriptions);
|
|
274
|
+
} else if (serverType === ServerType.RSPrimary) {
|
|
275
|
+
const result = updateRsFromPrimary(
|
|
276
|
+
serverDescriptions,
|
|
277
|
+
serverDescription,
|
|
278
|
+
setName,
|
|
279
|
+
maxSetVersion,
|
|
280
|
+
maxElectionId
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
topologyType = result[0];
|
|
284
|
+
setName = result[1];
|
|
285
|
+
maxSetVersion = result[2];
|
|
286
|
+
maxElectionId = result[3];
|
|
287
|
+
} else if (NON_PRIMARY_RS_MEMBERS.has(serverType)) {
|
|
288
|
+
topologyType = updateRsWithPrimaryFromMember(
|
|
289
|
+
serverDescriptions,
|
|
290
|
+
serverDescription,
|
|
291
|
+
setName
|
|
292
|
+
);
|
|
293
|
+
} else {
|
|
294
|
+
topologyType = checkHasPrimary(serverDescriptions);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return new TopologyDescription(
|
|
299
|
+
topologyType,
|
|
300
|
+
serverDescriptions,
|
|
301
|
+
setName,
|
|
302
|
+
maxSetVersion,
|
|
303
|
+
maxElectionId,
|
|
304
|
+
commonWireVersion,
|
|
305
|
+
{ heartbeatFrequencyMS: this.heartbeatFrequencyMS, localThresholdMS: this.localThresholdMS }
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
get error(): MongoError | null {
|
|
310
|
+
const descriptionsWithError = Array.from(this.servers.values()).filter(
|
|
311
|
+
(sd: ServerDescription) => sd.error
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
if (descriptionsWithError.length > 0) {
|
|
315
|
+
return descriptionsWithError[0].error;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Determines if the topology description has any known servers
|
|
323
|
+
*/
|
|
324
|
+
get hasKnownServers(): boolean {
|
|
325
|
+
return Array.from(this.servers.values()).some(
|
|
326
|
+
(sd: ServerDescription) => sd.type !== ServerType.Unknown
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Determines if this topology description has a data-bearing server available.
|
|
332
|
+
*/
|
|
333
|
+
get hasDataBearingServers(): boolean {
|
|
334
|
+
return Array.from(this.servers.values()).some((sd: ServerDescription) => sd.isDataBearing);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Determines if the topology has a definition for the provided address
|
|
339
|
+
* @internal
|
|
340
|
+
*/
|
|
341
|
+
hasServer(address: string): boolean {
|
|
342
|
+
return this.servers.has(address);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Returns a JSON-serializable representation of the TopologyDescription. This is primarily
|
|
347
|
+
* intended for use with JSON.stringify().
|
|
348
|
+
*
|
|
349
|
+
* This method will not throw.
|
|
350
|
+
*/
|
|
351
|
+
toJSON() {
|
|
352
|
+
return EJSON.serialize(this);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function topologyTypeForServerType(serverType: ServerType): TopologyType {
|
|
357
|
+
switch (serverType) {
|
|
358
|
+
case ServerType.Standalone:
|
|
359
|
+
return TopologyType.Single;
|
|
360
|
+
case ServerType.Mongos:
|
|
361
|
+
return TopologyType.Sharded;
|
|
362
|
+
case ServerType.RSPrimary:
|
|
363
|
+
return TopologyType.ReplicaSetWithPrimary;
|
|
364
|
+
case ServerType.RSOther:
|
|
365
|
+
case ServerType.RSSecondary:
|
|
366
|
+
return TopologyType.ReplicaSetNoPrimary;
|
|
367
|
+
default:
|
|
368
|
+
return TopologyType.Unknown;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function updateRsFromPrimary(
|
|
373
|
+
serverDescriptions: Map<string, ServerDescription>,
|
|
374
|
+
serverDescription: ServerDescription,
|
|
375
|
+
setName: string | null = null,
|
|
376
|
+
maxSetVersion: number | null = null,
|
|
377
|
+
maxElectionId: ObjectId | null = null
|
|
378
|
+
): [TopologyType, string | null, number | null, ObjectId | null] {
|
|
379
|
+
const setVersionElectionIdMismatch = (
|
|
380
|
+
serverDescription: ServerDescription,
|
|
381
|
+
maxSetVersion: number | null,
|
|
382
|
+
maxElectionId: ObjectId | null
|
|
383
|
+
) => {
|
|
384
|
+
return (
|
|
385
|
+
`primary marked stale due to electionId/setVersion mismatch:` +
|
|
386
|
+
` server setVersion: ${serverDescription.setVersion},` +
|
|
387
|
+
` server electionId: ${serverDescription.electionId},` +
|
|
388
|
+
` topology setVersion: ${maxSetVersion},` +
|
|
389
|
+
` topology electionId: ${maxElectionId}`
|
|
390
|
+
);
|
|
391
|
+
};
|
|
392
|
+
setName = setName || serverDescription.setName;
|
|
393
|
+
if (setName !== serverDescription.setName) {
|
|
394
|
+
serverDescriptions.delete(serverDescription.address);
|
|
395
|
+
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (serverDescription.maxWireVersion >= 17) {
|
|
399
|
+
const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId);
|
|
400
|
+
const maxElectionIdIsEqual = electionIdComparison === 0;
|
|
401
|
+
const maxElectionIdIsLess = electionIdComparison === -1;
|
|
402
|
+
const maxSetVersionIsLessOrEqual =
|
|
403
|
+
(maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1);
|
|
404
|
+
|
|
405
|
+
if (maxElectionIdIsLess || (maxElectionIdIsEqual && maxSetVersionIsLessOrEqual)) {
|
|
406
|
+
// The reported electionId was greater
|
|
407
|
+
// or the electionId was equal and reported setVersion was greater
|
|
408
|
+
// Always update both values, they are a tuple
|
|
409
|
+
maxElectionId = serverDescription.electionId;
|
|
410
|
+
maxSetVersion = serverDescription.setVersion;
|
|
411
|
+
} else {
|
|
412
|
+
// Stale primary
|
|
413
|
+
// replace serverDescription with a default ServerDescription of type "Unknown"
|
|
414
|
+
serverDescriptions.set(
|
|
415
|
+
serverDescription.address,
|
|
416
|
+
new ServerDescription(serverDescription.address, undefined, {
|
|
417
|
+
error: new MongoStalePrimaryError(
|
|
418
|
+
setVersionElectionIdMismatch(serverDescription, maxSetVersion, maxElectionId)
|
|
419
|
+
)
|
|
420
|
+
})
|
|
421
|
+
);
|
|
422
|
+
|
|
423
|
+
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
|
|
424
|
+
}
|
|
425
|
+
} else {
|
|
426
|
+
const electionId = serverDescription.electionId ? serverDescription.electionId : null;
|
|
427
|
+
if (serverDescription.setVersion && electionId) {
|
|
428
|
+
if (maxSetVersion && maxElectionId) {
|
|
429
|
+
if (
|
|
430
|
+
maxSetVersion > serverDescription.setVersion ||
|
|
431
|
+
compareObjectId(maxElectionId, electionId) > 0
|
|
432
|
+
) {
|
|
433
|
+
// this primary is stale, we must remove it
|
|
434
|
+
serverDescriptions.set(
|
|
435
|
+
serverDescription.address,
|
|
436
|
+
new ServerDescription(serverDescription.address, undefined, {
|
|
437
|
+
error: new MongoStalePrimaryError(
|
|
438
|
+
setVersionElectionIdMismatch(serverDescription, maxSetVersion, maxElectionId)
|
|
439
|
+
)
|
|
440
|
+
})
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
maxElectionId = serverDescription.electionId;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (
|
|
451
|
+
serverDescription.setVersion != null &&
|
|
452
|
+
(maxSetVersion == null || serverDescription.setVersion > maxSetVersion)
|
|
453
|
+
) {
|
|
454
|
+
maxSetVersion = serverDescription.setVersion;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// We've heard from the primary. Is it the same primary as before?
|
|
459
|
+
for (const [address, server] of serverDescriptions) {
|
|
460
|
+
if (server.type === ServerType.RSPrimary && server.address !== serverDescription.address) {
|
|
461
|
+
// Reset old primary's type to Unknown.
|
|
462
|
+
serverDescriptions.set(
|
|
463
|
+
address,
|
|
464
|
+
new ServerDescription(server.address, undefined, {
|
|
465
|
+
error: new MongoStalePrimaryError(
|
|
466
|
+
'primary marked stale due to discovery of newer primary'
|
|
467
|
+
)
|
|
468
|
+
})
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
// There can only be one primary
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// Discover new hosts from this primary's response.
|
|
477
|
+
serverDescription.allHosts.forEach((address: string) => {
|
|
478
|
+
if (!serverDescriptions.has(address)) {
|
|
479
|
+
serverDescriptions.set(address, new ServerDescription(address));
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
// Remove hosts not in the response.
|
|
484
|
+
const currentAddresses = Array.from(serverDescriptions.keys());
|
|
485
|
+
const responseAddresses = serverDescription.allHosts;
|
|
486
|
+
currentAddresses
|
|
487
|
+
.filter((addr: string) => responseAddresses.indexOf(addr) === -1)
|
|
488
|
+
.forEach((address: string) => {
|
|
489
|
+
serverDescriptions.delete(address);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function updateRsWithPrimaryFromMember(
|
|
496
|
+
serverDescriptions: Map<string, ServerDescription>,
|
|
497
|
+
serverDescription: ServerDescription,
|
|
498
|
+
setName: string | null = null
|
|
499
|
+
): TopologyType {
|
|
500
|
+
if (setName == null) {
|
|
501
|
+
// TODO(NODE-3483): should be an appropriate runtime error
|
|
502
|
+
throw new MongoRuntimeError('Argument "setName" is required if connected to a replica set');
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (
|
|
506
|
+
setName !== serverDescription.setName ||
|
|
507
|
+
(serverDescription.me && serverDescription.address !== serverDescription.me)
|
|
508
|
+
) {
|
|
509
|
+
serverDescriptions.delete(serverDescription.address);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
return checkHasPrimary(serverDescriptions);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function updateRsNoPrimaryFromMember(
|
|
516
|
+
serverDescriptions: Map<string, ServerDescription>,
|
|
517
|
+
serverDescription: ServerDescription,
|
|
518
|
+
setName: string | null = null
|
|
519
|
+
): [TopologyType, string | null] {
|
|
520
|
+
const topologyType = TopologyType.ReplicaSetNoPrimary;
|
|
521
|
+
setName = setName ?? serverDescription.setName;
|
|
522
|
+
if (setName !== serverDescription.setName) {
|
|
523
|
+
serverDescriptions.delete(serverDescription.address);
|
|
524
|
+
return [topologyType, setName];
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
serverDescription.allHosts.forEach((address: string) => {
|
|
528
|
+
if (!serverDescriptions.has(address)) {
|
|
529
|
+
serverDescriptions.set(address, new ServerDescription(address));
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
if (serverDescription.me && serverDescription.address !== serverDescription.me) {
|
|
534
|
+
serverDescriptions.delete(serverDescription.address);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return [topologyType, setName];
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function checkHasPrimary(serverDescriptions: Map<string, ServerDescription>): TopologyType {
|
|
541
|
+
for (const serverDescription of serverDescriptions.values()) {
|
|
542
|
+
if (serverDescription.type === ServerType.RSPrimary) {
|
|
543
|
+
return TopologyType.ReplicaSetWithPrimary;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return TopologyType.ReplicaSetNoPrimary;
|
|
548
|
+
}
|