summaryrefslogtreecommitdiff
path: root/jstests/replsets/dbcheck_write_concern.js
blob: a87d0bb43d1970decc30feed88ace933b9d911b1 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
 * Test the behavior of per-batch writeConcern in dbCheck.
 *
 * @tags: [
 *   # We need persistence as we temporarily restart nodes.
 *   requires_persistence,
 *   assumes_against_mongod_not_mongos,
 * ]
 */
(function() {
"use strict";

const replSet = new ReplSetTest({
    name: "dbCheckWriteConcern",
    nodes: 2,
    nodeOptions: {setParameter: {dbCheckHealthLogEveryNBatches: 1}},
    settings: {
        // Prevent the primary from stepping down when we temporarily shut down the secondary.
        electionTimeoutMillis: 60000
    }
});
replSet.startSet();
replSet.initiate();

function forEachSecondary(f) {
    for (let secondary of replSet.getSecondaries()) {
        f(secondary);
    }
}

function forEachNode(f) {
    f(replSet.getPrimary());
    forEachSecondary(f);
}

// Clear local.system.healthlog.
function clearLog() {
    forEachNode(conn => conn.getDB("local").system.healthlog.drop());
}

const dbName = "dbCheck-writeConcern";
const collName = "test";
const primary = replSet.getPrimary();
const db = primary.getDB(dbName);
const coll = db[collName];
const healthlog = db.getSiblingDB('local').system.healthlog;

// Validate that w:majority behaves normally.
(function testWMajority() {
    clearLog();
    coll.drop();

    // Insert 1000 docs and run a few small batches to ensure we wait for write concern between
    // each one.
    const nDocs = 1000;
    const maxDocsPerBatch = 100;
    assert.commandWorked(coll.insertMany([...Array(nDocs).keys()].map(x => ({a: x}))));
    assert.commandWorked(db.runCommand({
        dbCheck: coll.getName(),
        maxDocsPerBatch: maxDocsPerBatch,
        batchWriteConcern: {w: 'majority'},
    }));

    // Confirm dbCheck logs the expected number of batches.
    assert.soon(function() {
        return (healthlog.find({operation: "dbCheckBatch", severity: "info"}).itcount() ==
                nDocs / maxDocsPerBatch);
    }, "dbCheck doesn't seem to complete", 60 * 1000);

    // Confirm there are no warnings or errors.
    assert.eq(healthlog.find({operation: "dbCheckBatch", severity: "warning"}).itcount(), 0);
    assert.eq(healthlog.find({operation: "dbCheckBatch", severity: "error"}).itcount(), 0);
})();

// Validate that w:2 behaves normally.
(function testW2() {
    clearLog();
    coll.drop();

    // Insert 1000 docs and run a few small batches to ensure we wait for write concern between
    // each one.
    const nDocs = 1000;
    const maxDocsPerBatch = 100;
    assert.commandWorked(coll.insertMany([...Array(nDocs).keys()].map(x => ({a: x}))));
    assert.commandWorked(db.runCommand({
        dbCheck: coll.getName(),
        maxDocsPerBatch: maxDocsPerBatch,
        batchWriteConcern: {w: 2},
    }));

    // Confirm dbCheck logs the expected number of batches.
    assert.soon(function() {
        return (healthlog.find({operation: "dbCheckBatch", severity: "info"}).itcount() ==
                nDocs / maxDocsPerBatch);
    }, "dbCheck doesn't seem to complete", 60 * 1000);

    // Confirm there are no warnings or errors.
    assert.eq(healthlog.find({operation: "dbCheckBatch", severity: "warning"}).itcount(), 0);
    assert.eq(healthlog.find({operation: "dbCheckBatch", severity: "error"}).itcount(), 0);
})();

// Validate that dbCheck completes with w:majority even when the secondary is down and a wtimeout is
// specified.
(function testWMajorityUnavailable() {
    clearLog();
    coll.drop();

    // Insert 1000 docs and run a few small batches to ensure we wait for write concern between
    // each one.
    const nDocs = 1000;
    const maxDocsPerBatch = 100;
    assert.commandWorked(coll.insertMany([...Array(nDocs).keys()].map(x => ({a: x}))));
    replSet.awaitReplication();

    // Stop the secondary and expect that the dbCheck batches still complete on the primary.
    const secondaryConn = replSet.getSecondary();
    const secondaryNodeId = replSet.getNodeId(secondaryConn);
    replSet.stop(secondaryNodeId, {forRestart: true /* preserve dbPath */});

    assert.commandWorked(db.runCommand({
        dbCheck: coll.getName(),
        maxDocsPerBatch: maxDocsPerBatch,
        batchWriteConcern: {w: 'majority', wtimeout: 10},
    }));

    // Confirm dbCheck logs the expected number of batches.
    assert.soon(function() {
        return (healthlog.find({operation: "dbCheckBatch", severity: "info"}).itcount() ==
                nDocs / maxDocsPerBatch);
    }, "dbCheck doesn't seem to complete", 60 * 1000);

    // Confirm dbCheck logs a warning for every batch.
    assert.soon(function() {
        return (healthlog.find({operation: "dbCheckBatch", severity: "warning"}).itcount() ==
                nDocs / maxDocsPerBatch);
    }, "dbCheck did not log writeConcern warnings", 60 * 1000);
    // There should be no errors.
    assert.eq(healthlog.find({operation: "dbCheckBatch", severity: "error"}).itcount(), 0);

    replSet.start(secondaryNodeId, {}, true /*restart*/);
    replSet.awaitNodesAgreeOnPrimaryNoAuth();
    replSet.awaitReplication();
})();

// Validate that an invalid 'w' setting still allows dbCheck to succeed when presented with a
// wtimeout.
(function testW3Unavailable() {
    clearLog();
    coll.drop();

    // Insert 1000 docs and run a few small batches to ensure we wait for write concern between
    // each one.
    const nDocs = 1000;
    const maxDocsPerBatch = 100;
    assert.commandWorked(coll.insertMany([...Array(nDocs).keys()].map(x => ({a: x}))));
    replSet.awaitReplication();

    // Stop the secondary and expect that the dbCheck batches still complete on the primary.
    const secondaryConn = replSet.getSecondary();
    const secondaryNodeId = replSet.getNodeId(secondaryConn);
    replSet.stop(secondaryNodeId, {forRestart: true /* preserve dbPath */});

    assert.commandWorked(db.runCommand({
        dbCheck: coll.getName(),
        maxDocsPerBatch: maxDocsPerBatch,
        batchWriteConcern: {w: 3, wtimeout: 10},
    }));

    // Confirm dbCheck logs the expected number of batches.
    assert.soon(function() {
        return (healthlog.find({operation: "dbCheckBatch", severity: "info"}).itcount() ==
                nDocs / maxDocsPerBatch);
    }, "dbCheck doesn't seem to complete", 60 * 1000);

    // Confirm dbCheck logs a warning for every batch.
    assert.soon(function() {
        return (healthlog.find({operation: "dbCheckBatch", severity: "warning"}).itcount() ==
                nDocs / maxDocsPerBatch);
    }, "dbCheck did not log writeConcern warnings", 60 * 1000);
    // There should be no errors.
    assert.eq(healthlog.find({operation: "dbCheckBatch", severity: "error"}).itcount(), 0);
})();

replSet.stopSet();
})();