summaryrefslogtreecommitdiff
path: root/jstests/sharding/addshard2.js
blob: 7af23a4ab5bd2a51d1e89de20d6ae492ac3929e1 (plain)
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
(function() {

    // Don't start any shards, yet
    var s =
        new ShardingTest({name: "add_shard2", shards: 1, mongos: 1, other: {useHostname: true}});

    // Start two new instances, which will be used for shards
    var conn1 = MongoRunner.runMongod({useHostname: true});
    var conn2 = MongoRunner.runMongod({useHostname: true});

    var rs1 = new ReplSetTest({"name": "add_shard2_rs1", nodes: 3});
    rs1.startSet();
    rs1.initiate();
    var master1 = rs1.getPrimary();

    var rs2 = new ReplSetTest({"name": "add_shard2_rs2", nodes: 3});
    rs2.startSet();
    rs2.initiate();
    var master2 = rs2.getPrimary();

    // replica set with set name = 'config'
    var rs3 = new ReplSetTest({'name': 'config', nodes: 3});
    rs3.startSet();
    rs3.initiate();

    // replica set with set name = 'admin'
    var rs4 = new ReplSetTest({'name': 'admin', nodes: 3});
    rs4.startSet();
    rs4.initiate();

    // replica set with configsvr: true should *not* be allowed to be added as a shard
    var rs5 = new ReplSetTest({
        name: 'csrs',
        nodes: 3,
        nodeOptions: {configsvr: "", journal: "", storageEngine: "wiredTiger"}
    });
    rs5.startSet();
    var conf = rs5.getReplSetConfig();
    conf.configsvr = true;
    rs5.initiate(conf);

    // step 1. name given. maxSize zero means no limit. Make sure it is allowed.
    assert.commandWorked(
        s.admin.runCommand({addshard: getHostName() + ":" + conn1.port, name: "bar", maxSize: 0}));
    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() + ":" + master1.port}).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() + ":" + master2.port,
        "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
    var portWithoutHostRunning = allocatePort();
    assert(!s.admin.runCommand(
                        {addshard: "add_shard2_rs2/NonExistingHost:" + portWithoutHostRunning}).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() + ":" + master2.port + ",foo:" +
            portWithoutHostRunning
    }).ok,
           "accepted bad hostname in step 6");

    // Cannot add invalid stand alone host.
    assert.commandFailed(s.admin.runCommand({addshard: 'dummy:12345'}));

    //
    // SERVER-17231 Adding replica set w/ set name = 'config'
    //
    var configReplURI = 'config/' + getHostName() + ':' + rs3.getPrimary().port;

    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() + ':' + rs4.getPrimary().port}).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();

})();