summaryrefslogtreecommitdiff
path: root/jstests/sharding/secondary_shard_versioning.js
blob: 94e49c09a5d17c30a5a7a4770c8585fe1104e777 (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
/**
 * Tests that secondaries participate in the shard versioning protocol.
 */
(function() {
"use strict";

load('jstests/libs/profiler.js');  // for profilerHasSingleMatchingEntryOrThrow()

// Set the secondaries to priority 0 and votes 0 to prevent the primaries from stepping down.
let rsOpts = {nodes: [{}, {rsConfig: {priority: 0}}]};
let st = new ShardingTest({mongos: 2, shards: {rs0: rsOpts, rs1: rsOpts}});

assert.commandWorked(st.s0.adminCommand({enableSharding: 'test'}));
st.ensurePrimaryShard('test', st.shard0.shardName);

assert.commandWorked(st.s0.adminCommand({shardCollection: 'test.foo', key: {x: 1}}));
assert.commandWorked(st.s0.adminCommand({split: 'test.foo', middle: {x: 0}}));

let freshMongos = st.s0;
let staleMongos = st.s1;

jsTest.log("do insert from stale mongos to make it load the routing table before the move");
assert.writeOK(staleMongos.getDB('test').foo.insert({x: 1}));

jsTest.log("do moveChunk from fresh mongos");
assert.commandWorked(freshMongos.adminCommand({
    moveChunk: 'test.foo',
    find: {x: 0},
    to: st.shard1.shardName,
}));

// Turn on system profiler on secondaries to collect data on all future operations on the db.
let donorShardSecondary = st.rs0.getSecondary();
let recipientShardSecondary = st.rs1.getSecondary();
assert.commandWorked(donorShardSecondary.getDB('test').setProfilingLevel(2));
assert.commandWorked(recipientShardSecondary.getDB('test').setProfilingLevel(2));

// Use the mongos with the stale routing table to send read requests to the secondaries. 'local'
// read concern level must be specified in the request because secondaries default to
// 'available', which doesn't participate in the version protocol. Check that the donor shard
// returns a stale shardVersion error, which provokes mongos to refresh its routing table and
// re-target; that the recipient shard secondary refreshes its routing table on hearing the
// fresh version from mongos; and that the recipient shard secondary returns the results.

jsTest.log("do secondary read from stale mongos");
let res = staleMongos.getDB('test').runCommand({
    count: 'foo',
    query: {x: 1},
    $readPreference: {mode: "secondary"},
    readConcern: {"level": "local"}
});
assert(res.ok);
assert.eq(1, res.n, tojson(res));

// Check that the donor shard secondary returned stale shardVersion.
profilerHasSingleMatchingEntryOrThrow({
    profileDB: donorShardSecondary.getDB('test'),
    filter: {
        "ns": "test.foo",
        "command.count": "foo",
        "command.query": {x: 1},
        "command.shardVersion": {"$exists": true},
        "command.$readPreference": {"mode": "secondary"},
        "command.readConcern": {"level": "local"},
        "errCode": ErrorCodes.StaleConfig
    }
});

// The recipient shard secondary will also return stale shardVersion once, even though the
// mongos is fresh, because the recipient shard secondary was stale.
profilerHasSingleMatchingEntryOrThrow({
    profileDB: donorShardSecondary.getDB('test'),
    filter: {
        "ns": "test.foo",
        "command.count": "foo",
        "command.query": {x: 1},
        "command.shardVersion": {"$exists": true},
        "command.$readPreference": {"mode": "secondary"},
        "command.readConcern": {"level": "local"},
        "errCode": ErrorCodes.StaleConfig
    }
});

// Check that the recipient shard secondary received the query and returned results.
profilerHasSingleMatchingEntryOrThrow({
    profileDB: recipientShardSecondary.getDB('test'),
    filter: {
        "ns": "test.foo",
        "command.count": "foo",
        "command.query": {x: 1},
        "command.shardVersion": {"$exists": true},
        "command.$readPreference": {"mode": "secondary"},
        "command.readConcern": {"level": "local"},
        "errCode": {"$exists": false}
    }
});

st.stop();
})();