summaryrefslogtreecommitdiff
path: root/jstests/replsets/read_concern_majority_getmore_secondaries.js
blob: 59414c6b4e2382111c076c5b941dea55cde2cda4 (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
// Test that getMore for a majority read on a secondary only reads committed data.
// @tags: [requires_majority_read_concern]
(function() {
"use strict";

load("jstests/libs/write_concern_util.js");

const name = "read_concern_majority_getmore_secondaries";
const replSet = new ReplSetTest({
    name: name,
    nodes: [{}, {rsConfig: {priority: 0}}, {rsConfig: {priority: 0}}, {rsConfig: {priority: 0}}],
    settings: {chainingAllowed: false}
});
replSet.startSet();
replSet.initiate();

const dbName = "test";
const collName = "coll";

const primary = replSet.getPrimary();
const secondaries = replSet.getSecondaries();
const secondary = secondaries[0];

const primaryDB = primary.getDB(dbName);
const secondaryDB = secondary.getDB(dbName);

// The default WC is majority and stopServerReplication will prevent satisfying any majority writes.
assert.commandWorked(primary.adminCommand(
    {setDefaultRWConcern: 1, defaultWriteConcern: {w: 1}, writeConcern: {w: "majority"}}));

// Insert data on primary and allow it to become committed.
for (let i = 0; i < 4; i++) {
    assert.commandWorked(primaryDB[collName].insert({_id: i}));
}

// Await commit.
replSet.awaitReplication();
replSet.awaitLastOpCommitted();

// Stop data replication on 2 secondaries to prevent writes being committed.
stopServerReplication(secondaries[1]);
stopServerReplication(secondaries[2]);

// Write more data to primary.
for (let i = 4; i < 8; i++) {
    assert.commandWorked(primaryDB[collName].insert({_id: i}, {writeConcern: {w: 2}}));
}

// Check that it reached the secondary.
assert.docEq([{_id: 0}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}, {_id: 5}, {_id: 6}, {_id: 7}],
             secondaryDB[collName].find().sort({_id: 1}).toArray());

// It is important that this query does not do an in-memory sort. Otherwise the initial find
// will consume all of the results from the storage engine in order to sort them, so we will not
// be testing that the getMore does not read uncommitted data from the storage engine.
let res = primaryDB[collName].find().sort({_id: 1}).batchSize(2).readConcern("majority");
assert.docEq([{_id: 0}, {_id: 1}, {_id: 2}, {_id: 3}], res.toArray());

// Similarly, this query must not do an in-memory sort.
res = secondaryDB[collName].find().sort({_id: 1}).batchSize(2).readConcern("majority");
assert.docEq([{_id: 0}, {_id: 1}, {_id: 2}, {_id: 3}], res.toArray());

// Disable failpoints and shutdown.
restartReplSetReplication(replSet);
replSet.stopSet();
}());