diff options
Diffstat (limited to 'jstests/libs/discover_topology.js')
-rw-r--r-- | jstests/libs/discover_topology.js | 118 |
1 files changed, 72 insertions, 46 deletions
diff --git a/jstests/libs/discover_topology.js b/jstests/libs/discover_topology.js index 7fb1c79c427..7eebd6cd8ca 100644 --- a/jstests/libs/discover_topology.js +++ b/jstests/libs/discover_topology.js @@ -80,52 +80,78 @@ var DiscoverTopology = (function() { }; } - 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, - * primary: <primary-conn-string>, - * 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, - * primary: <primary-conn-string>, - * nodes: [...]}, - * ... - * }, - * mongos: { - * type: Topology.kRouter, - * 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); - } + /** + * 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, + * primary: <primary-conn-string>, + * 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, + * primary: <primary-conn-string>, + * nodes: [...]}, + * ... + * }, + * mongos: { + * type: Topology.kRouter, + * nodes: [...], + * } + * } + * is returned, where the description for each shard depends on whether it is a stand-alone + * shard or a replica set shard. + */ + function findConnectedNodes(conn, options = {connectFn: kDefaultConnectFn}) { + const isMongod = conn.adminCommand({isMaster: 1}).msg !== 'isdbgrid'; + + if (isMongod) { + return getDataMemberConnectionStrings(conn); + } - return findConnectedNodesViaMongos(conn, options); + return findConnectedNodesViaMongos(conn, options); + } + + function addNonConfigNodesToList(topology, hostList) { + if (topology.type === Topology.kStandalone) { + hostList.push(topology.mongod); + } else if (topology.type === Topology.kReplicaSet) { + hostList.push(...topology.nodes); + } else if (topology.type === Topology.kShardedCluster) { + for (let shardName of Object.keys(topology.shards)) { + const shardTopology = topology.shards[shardName]; + addNonConfigNodesToList(shardTopology, hostList); + } + hostList.push(...topology.mongos.nodes); + } else { + throw new Error('Unrecognized topology format: ' + tojson(topology)); } - }; + } + + /** + * Return list of nodes in the cluster given a connection NOT including config servers (if + * there are any). + */ + function findNonConfigNodes(conn) { + const topology = findConnectedNodes(conn); + let hostList = []; + addNonConfigNodesToList(topology, hostList); + return hostList; + } + + return {findConnectedNodes: findConnectedNodes, findNonConfigNodes: findNonConfigNodes}; })(); |