summaryrefslogtreecommitdiff
path: root/jstests/sharding/move_primary_with_drop_collection.js
blob: 9f5eb9aafada33cf658d27b4d8e0ba4622c35220 (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
(function() {
"use strict";

var st = new ShardingTest({shards: 2});
var configDB = st.s.getDB('config');

/*
 * Test that moving database primary works after dropping a recreating the same sharded collection,
 * the new primary never owned a chunk of the sharded collection.
 */
var testDB = st.s.getDB(jsTest.name() + "_db1");
var coll = testDB['coll'];

assert.commandWorked(st.s.adminCommand({enableSharding: testDB.getName()}));

jsTest.log("Create sharded collection with on chunk on shad 0");
st.ensurePrimaryShard(testDB.getName(), st.shard0.shardName);
st.shardColl(coll, {skey: 1}, false, false);

jsTest.log("Move database primary back and forth shard 1");
st.ensurePrimaryShard(testDB.getName(), st.shard1.shardName);
st.ensurePrimaryShard(testDB.getName(), st.shard0.shardName);

jsTest.log("Drop sharded collection");
coll.drop();

jsTest.log("Re-Create sharded collection on shard 0");
st.shardColl(coll, {skey: 1}, false, false);

jsTest.log("Move database primary to shard 1");
st.ensurePrimaryShard(testDB.getName(), st.shard1.shardName);

jsTest.log("Drop sharded collection");
coll.drop();

/*
 * Test that moving database primary works after dropping a recreating the same sharded collection,
 * the new primary previously owned a chunk of the original collection.
 */
var testDB = st.s.getDB(jsTest.name() + "_db2");
var coll = testDB['coll'];

assert.commandWorked(st.s.adminCommand({enableSharding: testDB.getName()}));

jsTest.log("Create sharded collection with two chunks on each shard");
st.ensurePrimaryShard(testDB.getName(), st.shard0.shardName);
st.shardColl(coll, {skey: 1}, {skey: 0}, {skey: 0});

assert.eq(1, configDB.chunks.count({ns: coll.getFullName(), shard: st.shard0.shardName}));
assert.eq(1, configDB.chunks.count({ns: coll.getFullName(), shard: st.shard1.shardName}));
jsTest.log("Move all chunks to shard 0");
assert.commandWorked(st.s.adminCommand({
    moveChunk: coll.getFullName(),
    find: {skey: 10},
    to: st.shard0.shardName,
    _waitForDelete: true
}));
assert.eq(2, configDB.chunks.count({ns: coll.getFullName(), shard: st.shard0.shardName}));
assert.eq(0, configDB.chunks.count({ns: coll.getFullName(), shard: st.shard1.shardName}));

jsTest.log("Drop sharded collection");
coll.drop();

jsTest.log("Re-Create sharded collection with one chunk on shard 0");
st.shardColl(coll, {skey: 1}, false, false);
assert.eq(1, configDB.chunks.count({ns: coll.getFullName(), shard: st.shard0.shardName}));

jsTest.log("Move primary of DB to shard 1");
st.ensurePrimaryShard(testDB.getName(), st.shard1.shardName);

jsTest.log("Drop sharded collection");
coll.drop();

st.stop();
})();