summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard2.js
blob: 527fad07be557d2fce57aec5ecae2b494f4be461 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
(function() {
    'use strict';

    function placeCheck(num) {
        print("shard2 step: " + num);
    }

    function printAll() {
        print("****************");
        db.foo.find().forEach(printjsononeline);
        print("++++++++++++++++++");
        primary.foo.find().forEach(printjsononeline);
        print("++++++++++++++++++");
        secondary.foo.find().forEach(printjsononeline);
        print("---------------------");
    }

    var s = new ShardingTest({shards: 2});
    var db = s.getDB("test");

    assert.commandWorked(s.s0.adminCommand({enablesharding: "test"}));
    s.ensurePrimaryShard('test', s.shard1.shardName);
    assert.commandWorked(s.s0.adminCommand({shardcollection: "test.foo", key: {num: 1}}));
    assert.eq(1, s.config.chunks.count({"ns": "test.foo"}), "sanity check 1");

    assert.commandWorked(s.s0.adminCommand({split: "test.foo", middle: {num: 0}}));
    assert.eq(2, s.config.chunks.count({"ns": "test.foo"}), "should be 2 shards");
    var chunks = s.config.chunks.find({"ns": "test.foo"}).toArray();
    assert.eq(chunks[0].shard, chunks[1].shard, "server should be the same after a split");

    assert.writeOK(db.foo.save({num: 1, name: "eliot"}));
    assert.writeOK(db.foo.save({num: 2, name: "sara"}));
    assert.writeOK(db.foo.save({num: -1, name: "joe"}));

    assert.eq(3,
              s.getPrimaryShard("test").getDB("test").foo.find().length(),
              "not right directly to db A");
    assert.eq(3, db.foo.find().length(), "not right on shard");

    var primary = s.getPrimaryShard("test").getDB("test");
    var secondary = s.getOther(primary).getDB("test");

    assert.eq(3, primary.foo.find().length(), "primary wrong B");
    assert.eq(0, secondary.foo.find().length(), "secondary wrong C");
    assert.eq(3, db.foo.find().sort({num: 1}).length());

    placeCheck(2);

    // Test move shard to unexisting shard
    assert.commandFailedWithCode(
        s.s0.adminCommand(
            {movechunk: "test.foo", find: {num: 1}, to: "adasd", _waitForDelete: true}),
        ErrorCodes.ShardNotFound);

    assert.commandWorked(s.s0.adminCommand({
        movechunk: "test.foo",
        find: {num: 1},
        to: secondary.getMongo().name,
        _waitForDelete: true
    }));
    assert.eq(2, secondary.foo.find().length(), "secondary should have 2 after move shard");
    assert.eq(1, primary.foo.find().length(), "primary should only have 1 after move shard");

    assert.eq(2,
              s.config.chunks.count({"ns": "test.foo"}),
              "still should have 2 shards after move not:" + s.getChunksString());
    var chunks = s.config.chunks.find({"ns": "test.foo"}).toArray();
    assert.neq(chunks[0].shard, chunks[1].shard, "servers should NOT be the same after the move");

    placeCheck(3);

    // Test inserts go to right server/shard
    assert.writeOK(db.foo.save({num: 3, name: "bob"}));
    assert.eq(1, primary.foo.find().length(), "after move insert go wrong place?");
    assert.eq(3, secondary.foo.find().length(), "after move insert go wrong place?");

    assert.writeOK(db.foo.save({num: -2, name: "funny man"}));
    assert.eq(2, primary.foo.find().length(), "after move insert go wrong place?");
    assert.eq(3, secondary.foo.find().length(), "after move insert go wrong place?");

    assert.writeOK(db.foo.save({num: 0, name: "funny guy"}));
    assert.eq(2, primary.foo.find().length(), "boundary A");
    assert.eq(4, secondary.foo.find().length(), "boundary B");

    placeCheck(4);

    // findOne
    assert.eq("eliot", db.foo.findOne({num: 1}).name);
    assert.eq("funny man", db.foo.findOne({num: -2}).name);

    // getAll
    function sumQuery(c) {
        var sum = 0;
        c.toArray().forEach(function(z) {
            sum += z.num;
        });
        return sum;
    }
    assert.eq(6, db.foo.find().length(), "sharded query 1");
    assert.eq(3, sumQuery(db.foo.find()), "sharded query 2");

    placeCheck(5);

    // sort by num

    assert.eq(3, sumQuery(db.foo.find().sort({num: 1})), "sharding query w/sort 1");
    assert.eq(3, sumQuery(db.foo.find().sort({num: -1})), "sharding query w/sort 2");

    assert.eq(
        "funny man", db.foo.find().sort({num: 1})[0].name, "sharding query w/sort 3 order wrong");
    assert.eq(-2, db.foo.find().sort({num: 1})[0].num, "sharding query w/sort 4 order wrong");

    assert.eq("bob", db.foo.find().sort({num: -1})[0].name, "sharding query w/sort 5 order wrong");
    assert.eq(3, db.foo.find().sort({num: -1})[0].num, "sharding query w/sort 6 order wrong");

    placeCheck(6);

    // Sort by name
    function getNames(c) {
        return c.toArray().map(function(z) {
            return z.name;
        });
    }
    var correct = getNames(db.foo.find()).sort();
    assert.eq(correct, getNames(db.foo.find().sort({name: 1})));
    correct = correct.reverse();
    assert.eq(correct, getNames(db.foo.find().sort({name: -1})));

    assert.eq(3, sumQuery(db.foo.find().sort({name: 1})), "sharding query w/non-shard sort 1");
    assert.eq(3, sumQuery(db.foo.find().sort({name: -1})), "sharding query w/non-shard sort 2");

    // sort by num multiple shards per server
    assert.commandWorked(s.s0.adminCommand({split: "test.foo", middle: {num: 2}}));
    assert.eq("funny man",
              db.foo.find().sort({num: 1})[0].name,
              "sharding query w/sort and another split 1 order wrong");
    assert.eq("bob",
              db.foo.find().sort({num: -1})[0].name,
              "sharding query w/sort and another split 2 order wrong");
    assert.eq("funny man",
              db.foo.find({num: {$lt: 100}}).sort({num: 1}).arrayAccess(0).name,
              "sharding query w/sort and another split 3 order wrong");

    placeCheck(7);

    db.foo.find().sort({_id: 1}).forEach(function(z) {
        print(z._id);
    });

    var zzz = db.foo.find().explain("executionStats").executionStats;
    assert.eq(0, zzz.totalKeysExamined, "EX1a");
    assert.eq(6, zzz.nReturned, "EX1b");
    assert.eq(6, zzz.totalDocsExamined, "EX1c");

    zzz = db.foo.find().hint({_id: 1}).sort({_id: 1}).explain("executionStats").executionStats;
    assert.eq(6, zzz.totalKeysExamined, "EX2a");
    assert.eq(6, zzz.nReturned, "EX2b");
    assert.eq(6, zzz.totalDocsExamined, "EX2c");

    // getMore
    assert.eq(4, db.foo.find().limit(-4).toArray().length, "getMore 1");
    function countCursor(c) {
        var num = 0;
        while (c.hasNext()) {
            c.next();
            num++;
        }
        return num;
    }
    assert.eq(6, countCursor(db.foo.find()._exec()), "getMore 2");
    assert.eq(6, countCursor(db.foo.find().batchSize(1)._exec()), "getMore 3");

    // find by non-shard-key
    db.foo.find().forEach(function(z) {
        var y = db.foo.findOne({_id: z._id});
        assert(y, "_id check 1 : " + tojson(z));
        assert.eq(z.num, y.num, "_id check 2 : " + tojson(z));
    });

    // update
    var person = db.foo.findOne({num: 3});
    assert.eq("bob", person.name, "update setup 1");
    person.name = "bob is gone";
    db.foo.update({num: 3}, person);
    person = db.foo.findOne({num: 3});
    assert.eq("bob is gone", person.name, "update test B");

    // remove
    assert(db.foo.findOne({num: 3}) != null, "remove test A");
    db.foo.remove({num: 3});
    assert.isnull(db.foo.findOne({num: 3}), "remove test B");

    db.foo.save({num: 3, name: "eliot2"});
    person = db.foo.findOne({num: 3});
    assert(person, "remove test C");
    assert.eq(person.name, "eliot2");

    db.foo.remove({_id: person._id});
    assert.isnull(db.foo.findOne({num: 3}), "remove test E");

    placeCheck(8);

    // more update stuff

    printAll();
    var total = db.foo.find().count();
    var res = assert.writeOK(db.foo.update({}, {$inc: {x: 1}}, false, true));
    printAll();
    assert.eq(total, res.nModified, res.toString());

    res = db.foo.update({num: -1}, {$inc: {x: 1}}, false, true);
    assert.eq(1, res.nModified, res.toString());

    // ---- move all to the secondary

    assert.eq(2, s.onNumShards("foo"), "on 2 shards");

    secondary.foo.insert({num: -3});

    assert.commandWorked(s.s0.adminCommand({
        movechunk: "test.foo",
        find: {num: -2},
        to: secondary.getMongo().name,
        _waitForDelete: true
    }));
    assert.eq(1, s.onNumShards("foo"), "on 1 shards");

    assert.commandWorked(s.s0.adminCommand({
        movechunk: "test.foo",
        find: {num: -2},
        to: primary.getMongo().name,
        _waitForDelete: true
    }));
    assert.eq(2, s.onNumShards("foo"), "on 2 shards again");
    assert.eq(3, s.config.chunks.count({"ns": "test.foo"}), "only 3 chunks");

    print("YO : " + tojson(db.runCommand("serverStatus")));

    s.stop();
})();