summaryrefslogtreecommitdiff
path: root/jstests/sharding/unowned_doc_filtering.js
blob: 2c12ef4b0cf6c1137bd2ef99fda945921bff9f5f (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
/**
 * Tests that the mongod chunk filtering stage properly filters out unowned documents even after
 * the shards are restarted.
 *
 * This test involves restarting a standalone shard, so cannot be run on ephemeral storage engines.
 * A restarted standalone will lose all data when using an ephemeral storage engine.
 * @tags: [requires_persistence]
 */
(function() {
    "use strict";

    var st = new ShardingTest({shards: 2});

    var testDB = st.s.getDB('test');

    assert.commandWorked(testDB.adminCommand({enableSharding: 'test'}));
    st.ensurePrimaryShard('test', 'shard0000');
    assert.commandWorked(testDB.adminCommand({shardCollection: 'test.foo', key: {x: 1}}));

    var inserts = [];
    for (var i = 0; i < 100; i++) {
        inserts.push({x: i});
    }
    assert.writeOK(testDB.foo.insert(inserts));

    assert.commandWorked(testDB.adminCommand({split: 'test.foo', find: {x: 50}}));
    assert.commandWorked(
        testDB.adminCommand({moveChunk: 'test.foo', find: {x: 100}, to: 'shard0001'}));

    // Insert some documents directly into the shards into chunks not owned by that shard.
    st.d0.getDB('test').foo.insert({x: 100});
    st.d1.getDB('test').foo.insert({x: 0});

    st.restartMongod(0);
    st.restartMongod(1);

    var fooCount;
    for (var retries = 0; retries <= 2; retries++) {
        try {
            fooCount = testDB.foo.find().itcount();
            break;
        } catch (e) {
            // expected for reestablishing connections broken by the mongod restart.
            assert.eq(ErrorCodes.HostUnreachable, e.code, tojson(e));
        }
    }
    assert.eq(100, fooCount);

    st.stop();
}());