summaryrefslogtreecommitdiff
path: root/jstests/disk/repair_corrupt_document.js
blob: 428bbf247566d1d5589854c05768875c52dc99a0 (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
/**
 * Tests that --repair deletes corrupt BSON documents.
 */

(function() {

load('jstests/disk/libs/wt_file_helper.js');

const baseName = "repair_corrupt_document";
const collName = "test";
const dbpath = MongoRunner.dataPath + baseName + "/";
const doc1 = {
    a: 1
};
const doc2 = {
    a: 2
};
const indexName = "a_1";

let indexUri;
let validDoc;

resetDbpath(dbpath);
let port;

// Initialize test collection.
let createCollWithDoc = function(coll) {
    assert.commandWorked(coll.insert(doc1));
    validDoc = coll.findOne(doc1);

    assert.commandWorked(coll.createIndex({a: 1}, {name: indexName}));
    assertQueryUsesIndex(coll, doc1, indexName);
    indexUri = getUriForIndex(coll, indexName);
    return coll;
};

// Insert corrupt document for testing via failpoint.
let corruptDocumentOnInsert = function(db, coll) {
    jsTestLog("Corrupt document BSON on insert.");
    assert.commandWorked(
        db.adminCommand({configureFailPoint: "corruptDocumentOnInsert", mode: "alwaysOn"}));
    assert.commandWorked(coll.insert(doc2));
    assert.commandWorked(
        db.adminCommand({configureFailPoint: "corruptDocumentOnInsert", mode: "off"}));
};

/**
 * Test 1: Insert corrupt document and verify results are valid without rebuilding indexes after
 * running repair.
 */
(function startStandaloneWithCorruptDoc() {
    jsTestLog("Entering startStandaloneWithCorruptDoc...");

    let mongod = startMongodOnExistingPath(dbpath);
    port = mongod.port;
    let db = mongod.getDB(baseName);
    let testColl = db[collName];

    testColl = createCollWithDoc(testColl);
    corruptDocumentOnInsert(db, testColl);

    assert.eq(testColl.count(), 2);

    MongoRunner.stopMongod(mongod, null, {skipValidation: true});
    jsTestLog("Exiting startStandaloneWithCorruptDoc.");
})();

// Run validate with repair mode and verify corrupt document is removed from collection.
(function runRepairAndVerifyCollectionDocs() {
    jsTestLog("Entering runRepairAndVerifyCollectionDocs...");

    assertRepairSucceeds(dbpath, port, {});

    let mongod = startMongodOnExistingPath(dbpath);
    let testColl = mongod.getDB(baseName)[collName];

    // Repair removed the corrupt document.
    assert.eq(testColl.count(), 1);
    assert.eq(testColl.findOne(doc1), validDoc);

    // Repair did not need to rebuild indexes.
    assert.eq(indexUri, getUriForIndex(testColl, indexName));

    MongoRunner.stopMongod(mongod);
    jsTestLog("Exiting runValidateWithRepairMode.");
})();
})();