summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/dbcheck_detects_data_corruption.js
blob: cb52793dc8fe3b1a1948576181ba8c73353f7a48 (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
/**
 * This tests that errors are logged when dbCheck finds evidence of corruption, but does not cause
 * the operation to fail.
 */
(function() {

const replSet = new ReplSetTest({nodes: 2});
replSet.startSet();
replSet.initiate();

const primary = replSet.getPrimary();
const secondary = replSet.getSecondary();

const db = primary.getDB('test');
const collName = 'coll';
const coll = db[collName];

assert.commandWorked(coll.insert({_id: 0, a: "first"}));

// Create the same type of corruption on both nodes.
assert.commandWorked(db.adminCommand({
    configureFailPoint: "skipUnindexingDocumentWhenDeleted",
    mode: "alwaysOn",
    data: {indexName: "_id_"}
}));
assert.commandWorked(secondary.getDB('admin').runCommand({
    configureFailPoint: "skipUnindexingDocumentWhenDeleted",
    mode: "alwaysOn",
    data: {indexName: "_id_"}
}));

const docId = 1;
assert.commandWorked(coll.insert({_id: docId, a: "second"}));
assert.commandWorked(coll.remove({_id: docId}));

// Validate should detect this inconsistency.
let res = coll.validate();
assert.commandWorked(res);
assert(!res.valid, res);

assert.commandWorked(db.runCommand({"dbCheck": 1}));

// Wait for both nodes to finish checking.
const healthlogSecondary = secondary.getDB('local').system.healthlog;
assert.soon(() => healthlogSecondary.find({operation: "dbCheckStop"}).itcount() == 1);

[primary, secondary].forEach((node) => {
    print("checking " + tojson(node));
    let entry = node.getDB('local').system.healthlog.findOne({severity: 'error'});
    assert(entry, "No healthlog entry found on " + tojson(node));
    assert.eq("Erroneous index key found with reference to non-existent record id",
              entry.msg,
              tojson(entry));

    // The erroneous index key should not affect the hashes. The documents should still be the same.
    assert.eq(1, node.getDB('local').system.healthlog.count({severity: 'error'}));
});

replSet.stopSet(undefined /* signal */, false /* forRestart */, {skipValidation: true});
})();