summaryrefslogtreecommitdiff
path: root/jstests/sharding/chunk_operations_invalidate_single_shard.js
blob: e660cec230571a6078be248a912fab5d572c1b90 (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
/**
 * Test that chunk operations don't cause the mongos to refresh unless an affected chunk is
 * targeted.
 */

(function() {
'use strict';

load("jstests/sharding/libs/shard_versioning_util.js");
load('jstests/sharding/libs/sharded_transactions_helpers.js');

let st = new ShardingTest({
    mongos: 1,
    shards: 3,
    other: {mongosOptions: {setParameter: {enableFinerGrainedCatalogCacheRefresh: true}}}
});
const dbName = "test";
const collName = "foo";
const ns = dbName + "." + collName;
let testDB = st.s.getDB(dbName);
let testColl = testDB.foo;

let getMongosCollVersion = (ns) => {
    return st.s.adminCommand({getShardVersion: ns}).version;
};

let setUp = () => {
    /**
     * Sets up a test by moving chunks to such that one chunk is on each
     * shard, with the following distribution:
     *     shard0: [-inf, -10)
     *     shard1: [-10, 10)
     *     shard2: [10, inf)
     */
    assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
    assert.commandWorked(st.s.adminCommand({movePrimary: dbName, to: st.shard0.shardName}));
    assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {x: 1}}));
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {x: -10}}));
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {x: 10}}));

    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {x: -100}, to: st.shard0.shardName}));
    assert.commandWorked(st.s.adminCommand({moveChunk: ns, find: {x: 0}, to: st.shard1.shardName}));
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {x: 1000}, to: st.shard2.shardName}));
    flushRoutersAndRefreshShardMetadata(st, {ns});
};

// Verify that a split doesn't update the mongos' catalog cache unless an affected chunk is
// targeted.
let testSplit = () => {
    const mongosCollectionVersion = getMongosCollVersion(ns);

    assert.commandWorked(st.s.adminCommand({split: ns, middle: {x: -500}}));

    testColl.findOne({x: 0});
    testColl.findOne({x: 1000});
    assert.eq(mongosCollectionVersion, getMongosCollVersion(ns));

    testColl.findOne({x: -1000});
    assert.lt(mongosCollectionVersion, getMongosCollVersion(ns));
};

// Verify that a merge doesn't update the mongos' catalog cache unless an affected chunk is
// targeted.
let testMerge = () => {
    const mongosCollectionVersion = getMongosCollVersion(ns);

    assert.commandWorked(st.s.adminCommand({mergeChunks: ns, bounds: [{x: MinKey}, {x: -10}]}));
    testColl.findOne({x: 0});
    testColl.findOne({x: 1000});
    assert.eq(mongosCollectionVersion, getMongosCollVersion(ns));

    testColl.findOne({x: -1000});
    assert.lt(mongosCollectionVersion, getMongosCollVersion(ns));
};

// Verify that a chunk move doesn't update the mongos' catalog cache unless an affected chunk is
// targeted.
let testMoveChunk = () => {
    // Contact the donor shard to trigger update.
    let mongosCollectionVersion = getMongosCollVersion(ns);

    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {x: -100}, to: st.shard1.shardName}));
    testColl.findOne({x: 1000});
    assert.eq(mongosCollectionVersion, getMongosCollVersion(ns));

    testColl.findOne({x: -1000});
    assert.lt(mongosCollectionVersion, getMongosCollVersion(ns));

    // Contact the recipient shard to trigger update.
    mongosCollectionVersion = getMongosCollVersion(ns);

    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {x: -100}, to: st.shard0.shardName}));
    testColl.findOne({x: 1000});
    assert.eq(mongosCollectionVersion, getMongosCollVersion(ns));

    testColl.findOne({x: -1000});
    assert.lt(mongosCollectionVersion, getMongosCollVersion(ns));
};

setUp();
testSplit();
testMerge();
testMoveChunk();

st.stop();
})();