summaryrefslogtreecommitdiff
path: root/jstests/sharding/coll_epoch_test1.js
blob: 5e243b8fff4a790bd7aece5134cb2be66717df4d (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
// Tests various cases of dropping and recreating collections in the same namespace with multiple
// mongoses
(function() {
    'use strict';

    var st = new ShardingTest({shards: 3, mongos: 3, causallyConsistent: true});

    var config = st.s0.getDB("config");
    var admin = st.s0.getDB("admin");
    var coll = st.s0.getCollection("foo.bar");

    // Use separate mongoses for admin, inserting data, and validating results, so no single-mongos
    // tricks will work
    var staleMongos = st.s1;
    var insertMongos = st.s2;

    var shards = [st.shard0, st.shard1, st.shard2];

    //
    // Test that inserts and queries go to the correct shard even when the collection has been
    // sharded from another mongos
    //

    jsTest.log("Enabling sharding for the first time...");

    assert.commandWorked(admin.runCommand({enableSharding: coll.getDB() + ""}));
    // TODO(PM-85): Make sure we *always* move the primary after collection lifecyle project is
    // complete
    st.ensurePrimaryShard(coll.getDB().getName(), st.shard1.shardName);
    assert.commandWorked(admin.runCommand({shardCollection: coll + "", key: {_id: 1}}));
    st.configRS.awaitLastOpCommitted();  // TODO: Remove after collection lifecyle project (PM-85)

    var bulk = insertMongos.getCollection(coll + "").initializeUnorderedBulkOp();
    for (var i = 0; i < 100; i++) {
        bulk.insert({_id: i, test: "a"});
    }
    assert.writeOK(bulk.execute());
    assert.eq(100, staleMongos.getCollection(coll + "").find({test: "a"}).itcount());

    assert(coll.drop());
    st.configRS.awaitLastOpCommitted();

    //
    // Test that inserts and queries go to the correct shard even when the collection has been
    // resharded from another mongos, with a different key
    //

    jsTest.log("Re-enabling sharding with a different key...");

    st.ensurePrimaryShard(coll.getDB().getName(), st.shard1.shardName);
    assert.commandWorked(coll.ensureIndex({notId: 1}));
    assert.commandWorked(admin.runCommand({shardCollection: coll + "", key: {notId: 1}}));
    st.configRS.awaitLastOpCommitted();

    bulk = insertMongos.getCollection(coll + "").initializeUnorderedBulkOp();
    for (var i = 0; i < 100; i++) {
        bulk.insert({notId: i, test: "b"});
    }
    assert.writeOK(bulk.execute());
    assert.eq(100, staleMongos.getCollection(coll + "").find({test: "b"}).itcount());
    assert.eq(0, staleMongos.getCollection(coll + "").find({test: {$in: ["a"]}}).itcount());

    assert(coll.drop());
    st.configRS.awaitLastOpCommitted();

    //
    // Test that inserts and queries go to the correct shard even when the collection has been
    // unsharded from another mongos
    //

    jsTest.log("Re-creating unsharded collection from a sharded collection...");

    bulk = insertMongos.getCollection(coll + "").initializeUnorderedBulkOp();
    for (var i = 0; i < 100; i++) {
        bulk.insert({test: "c"});
    }
    assert.writeOK(bulk.execute());

    assert.eq(100, staleMongos.getCollection(coll + "").find({test: "c"}).itcount());
    assert.eq(0, staleMongos.getCollection(coll + "").find({test: {$in: ["a", "b"]}}).itcount());

    st.stop();
})();