1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
load('jstests/libs/discover_topology.js');
var FSMShardingTest = class {
constructor(connStr) {
/**
* `topology` has the following format:
{
"type" : "sharded cluster",
"configsvr" : {
"type" : "replica set",
"nodes" : [
"robert-macbook-pro.local:20001",
"robert-macbook-pro.local:20002",
"robert-macbook-pro.local:20003"
]
},
"shards" : {
"__unknown_name__-rs0" : {
"type" : "replica set",
"nodes" : [
"robert-macbook-pro.local:20000"
]
}
},
"mongos" : {
"type" : "mongos router",
"nodes" : [
"robert-macbook-pro.local:20004"
]
}
}
*/
const conn = new Mongo(connStr);
const topology = DiscoverTopology.findConnectedNodes(conn);
assert.eq(topology.type, Topology.kShardedCluster, 'Topology must be a sharded cluster');
this._mongoses = [];
for (let connStr of topology.mongos.nodes) {
this._mongoses.push(new Mongo(connStr));
}
for (let mongos of this._mongoses) {
mongos.name = mongos.host;
}
this._configsvr = new ReplSetTest(topology.configsvr.nodes[0]);
for (let node of this._configsvr.nodes) {
node.name = node.host;
}
// connections to replsets or mongods.
this._shard_connections = [];
// ReplSetTest objects for replsets shards.
this._shard_rsts = [];
for (let shardName of Object.keys(topology.shards)) {
let shardTopology = topology.shards[shardName];
let shard;
if (shardTopology.type === Topology.kReplicaSet) {
const shard_rst = new ReplSetTest(shardTopology.nodes[0]);
this._shard_rsts.push(shard_rst);
shard = new Mongo(shard_rst.getURL());
shard.name = shard_rst.getURL();
} else {
shard = new Mongo(shardTopology.mongod);
shard.name = shard.host;
}
shard.shardName = shardName;
this._shard_connections.push(shard);
}
}
/*
* Setters and Getters.
*/
s(n = 0) {
return this._mongoses[n];
}
d(n = 0) {
// Only return for non-replset shards.
if (this._shard_rsts[n] === undefined) {
return this._shard_connections[n];
}
return undefined;
}
/**
* This function returns the ReplSetTest instance for a shard, whereas shard() returns
* the connection to the shard primary.
*/
rs(n = 0) {
return this._shard_rsts[n];
}
c(n = 0) {
return this._configsvr.nodes[n];
}
shard(n = 0) {
return this._shard_connections[n];
}
get _configServers() {
return this._configsvr.nodes;
}
/*
* Public Functions.
*/
shardColl(coll, shardKey) {
assert.commandWorked(this.s(0).adminCommand({
enableSharding: coll.getDB().toString(),
}));
assert.commandWorked(this.s(0).adminCommand({
shardCollection: coll.toString(),
key: shardKey,
}));
}
/*
* Internal Functions.
*/
};
|