/
addshard2.js
102 lines (83 loc) · 4.03 KB
/
addshard2.js
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
// don't start any shards, yet
s = new ShardingTest( "add_shard2", 1, 0, 1, {useHostname : true} );
var conn1 = MongoRunner.runMongod({useHostname: true});
var conn2 = MongoRunner.runMongod({useHostname: true});
var rs1 = new ReplSetTest( { "name" : "add_shard2_rs1", nodes : 3 , startPort : 31200 } );
rs1.startSet();
rs1.initiate();
var master1 = rs1.getMaster();
var rs2 = new ReplSetTest( { "name" : "add_shard2_rs2", nodes : 3 , startPort : 31203 } );
rs2.startSet();
rs2.initiate();
var master2 = rs2.getMaster();
// replica set with set name = 'config'
var rs3 = new ReplSetTest({ 'name': 'config', nodes: 3, startPort: 31206 });
rs3.startSet();
rs3.initiate();
// replica set with set name = 'admin'
var rs4 = new ReplSetTest({ 'name': 'admin', nodes: 3, startPort: 31209 });
rs4.startSet();
rs4.initiate();
// replica set with configServer: true should *not* be allowed to be added as a shard
var rs5 = new ReplSetTest({ 'name': 'csrs', nodes: 3, startPort: 31212 });
rs5.startSet();
var conf = rs5.getReplSetConfig();
conf.configServer = true;
conf.settings = {protocolVersion: 1};
rs5.initiate(conf);
// step 1. name given
assert(s.admin.runCommand({"addshard" : getHostName()+":" + conn1.port, "name" : "bar"}).ok,
"failed to add shard in step 1");
var shard = s.getDB("config").shards.findOne({"_id" : {"$nin" : ["shard0000"]}});
assert(shard, "shard wasn't found");
assert.eq("bar", shard._id, "shard has incorrect name");
// step 2. replica set
assert(s.admin.runCommand({"addshard" : "add_shard2_rs1/"+getHostName()+":31200"}).ok, "failed to add shard in step 2");
shard = s.getDB("config").shards.findOne({"_id" : {"$nin" : ["shard0000", "bar"]}});
assert(shard, "shard wasn't found");
assert.eq("add_shard2_rs1", shard._id, "t2 name");
// step 3. replica set w/ name given
assert(s.admin.runCommand({"addshard" : "add_shard2_rs2/"+getHostName()+":31203", "name" : "myshard"}).ok,
"failed to add shard in step 4");
shard = s.getDB("config").shards.findOne({"_id" : {"$nin" : ["shard0000", "bar", "add_shard2_rs1"]}});
assert(shard, "shard wasn't found");
assert.eq("myshard", shard._id, "t3 name");
// step 4. no name given
assert(s.admin.runCommand({"addshard" : getHostName()+":" + conn2.port}).ok,
"failed to add shard in step 4");
shard = s.getDB("config").shards.findOne({"_id" : {"$nin" : ["shard0000", "bar", "add_shard2_rs1", "myshard"]}});
assert(shard, "shard wasn't found");
assert.eq("shard0001", shard._id, "t4 name");
assert.eq(s.getDB("config").shards.count(), 5, "unexpected number of shards");
// step 5. replica set w/ a wrong host
assert(!s.admin.runCommand({"addshard" : "add_shard2_rs2/NonExistingHost:31203"}).ok, "accepted bad hostname in step 5");
// step 6. replica set w/ mixed wrong/right hosts
assert(!s.admin.runCommand({"addshard" : "add_shard2_rs2/"+getHostName()+":31203,foo:9999"}).ok,
"accepted bad hostname in step 6");
//
// SERVER-17231 Adding replica set w/ set name = 'config'
//
var configReplURI = 'config/' + getHostName() + ':31206';
assert(!s.admin.runCommand({ 'addshard': configReplURI }).ok,
'accepted replica set shard with set name "config"');
// but we should be allowed to add that replica set using a different shard name
assert(s.admin.runCommand({ 'addshard': configReplURI, name: 'not_config' }).ok,
'unable to add replica set using valid replica set name');
shard = s.getDB('config').shards.findOne({ '_id': 'not_config' });
assert(shard, 'shard with name "not_config" not found');
//
// SERVER-17232 Try inserting into shard with name 'admin'
//
assert(s.admin.runCommand({ 'addshard': 'admin/' + getHostName() + ':31209' }).ok,
'adding replica set with name "admin" should work');
var wRes = s.getDB('test').foo.insert({ x: 1 });
assert(!wRes.hasWriteError() && wRes.nInserted === 1,
'failed to insert document into "test.foo" unsharded collection');
// SERVER-19545 Should not be able to add config server replsets as shards.
assert.commandFailed(s.admin.runCommand({addshard: rs5.getURL()}));
s.stop();
rs1.stopSet();
rs2.stopSet();
rs3.stopSet();
rs4.stopSet();
rs5.stopSet();