diff options
author | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2018-02-02 21:54:43 -0500 |
---|---|---|
committer | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2018-02-02 21:54:43 -0500 |
commit | 64bed8173387fbafcfcf39bfb9aa6cecadf25822 (patch) | |
tree | 0d7c39adb7a8329072cc132b9b3d1cc4291a313d /jstests/libs | |
parent | 331d67a0d872d2f5452c0bed6fe6fb3658b69dd9 (diff) | |
download | mongo-64bed8173387fbafcfcf39bfb9aa6cecadf25822.tar.gz |
SERVER-33068 Fix run_check_repl_dbhash.js hook to actually run dbhash.
Consolidates the logic in the run_check_repl_dbhash.js and
run_validate_collections.js for discovering all of the mongod processes
in a MongoDB deployment into a new discover_topology.js library.
Also adds a test that relies on mongod logging to verify that the
run_check_repl_dbhash.js and run_validate_collections.js hooks execute
on all of the expected servers.
Diffstat (limited to 'jstests/libs')
-rw-r--r-- | jstests/libs/discover_topology.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/jstests/libs/discover_topology.js b/jstests/libs/discover_topology.js new file mode 100644 index 00000000000..95b072894b0 --- /dev/null +++ b/jstests/libs/discover_topology.js @@ -0,0 +1,102 @@ +'use strict'; + +// The tojson() function that is commonly used to build up assertion messages doesn't support the +// Symbol type, so we just use unique string values instead. +var Topology = { + kStandalone: 'stand-alone', + kReplicaSet: 'replica set', + kShardedCluster: 'sharded cluster', +}; + +var DiscoverTopology = (function() { + const kDefaultConnectFn = (host) => new Mongo(host); + + function getDataMemberConnectionStrings(conn) { + const res = conn.adminCommand({isMaster: 1}); + + if (!res.hasOwnProperty('setName')) { + // 'conn' represents a connection to a stand-alone mongod. + return {type: Topology.kStandalone, mongod: conn.host}; + } + + // The "passives" field contains the list of unelectable (priority=0) secondaries + // and is omitted from the server's response when there are none. + res.passives = res.passives || []; + return {type: Topology.kReplicaSet, nodes: [...res.hosts, ...res.passives]}; + } + + function findConnectedNodesViaMongos(conn, options) { + function getConfigServerConnectionString() { + const shardMap = conn.adminCommand({getShardMap: 1}); + + if (!shardMap.hasOwnProperty('map')) { + throw new Error( + 'Expected "getShardMap" command to return an object with a "map" field: ' + + tojson(shardMap)); + } + + if (!shardMap.map.hasOwnProperty('config')) { + throw new Error( + 'Expected "getShardMap" command to return an object with a "map.config"' + + ' field: ' + tojson(shardMap)); + } + + return shardMap.map.config; + } + + const connectFn = + options.hasOwnProperty('connectFn') ? options.connectFn : kDefaultConnectFn; + + const configsvrConn = connectFn(getConfigServerConnectionString()); + const configsvrHosts = getDataMemberConnectionStrings(configsvrConn); + + const shards = assert.commandWorked(conn.adminCommand({listShards: 1})).shards; + const shardHosts = {}; + + for (let shardInfo of shards) { + const shardConn = connectFn(shardInfo.host); + shardHosts[shardInfo._id] = getDataMemberConnectionStrings(shardConn); + } + + return {type: Topology.kShardedCluster, configsvr: configsvrHosts, shards: shardHosts}; + } + + return { + /** + * Returns an object describing the topology of the mongod processes reachable from 'conn'. + * The "connectFn" property can be optionally specified to support custom retry logic when + * making connection attempts without overriding the Mongo constructor itself. + * + * For a stand-alone mongod, an object of the form + * {type: Topology.kStandalone, mongod: <conn-string>} + * is returned. + * + * For a replica set, an object of the form + * {type: Topology.kReplicaSet, nodes: [<conn-string1>, <conn-string2>, ...]} + * is returned. + * + * For a sharded cluster, an object of the form + * { + * type: Topology.kShardedCluster, + * configsvr: {nodes: [...]}, + * shards: { + * <shard-name1>: {type: Topology.kStandalone, mongod: ...}, + * <shard-name2>: {type: Topology.kReplicaSet, nodes: [...]}, + * ... + * } + * } + * is returned, where the description for each shard depends on whether it is a stand-alone + * shard or a replica set shard. + */ + findConnectedNodes: function findConnectedNodes(conn, + options = {connectFn: kDefaultConnectFn}) { + const isMongod = conn.adminCommand({isMaster: 1}).msg !== 'isdbgrid'; + + if (isMongod) { + return getDataMemberConnectionStrings(conn); + } + + return findConnectedNodesViaMongos(conn, options); + }, + }; +})(); |