summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/validate_memory_limit.js
blob: 106e94f54878a00173bc8e0f98e269a48410e475 (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
/**
 * Test that the memory usage of validate is properly limited according to the
 * maxValidateMemoryUsageMB parameter.
 *
 * @tags: [
 *   requires_persistence,
 *   requires_wiredtiger,
 * ]
 */
(function() {
"use strict";

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

const kIndexKeyLength = 1024 * 1024;

const baseName = "validate_memory_limit";
const dbpath = MongoRunner.dataPath + baseName + "/";
let conn = MongoRunner.runMongod({dbpath: dbpath});
let coll = conn.getDB("test").getCollection("corrupt");

function corruptIndex() {
    const uri = getUriForIndex(coll, "_id_");
    conn = truncateUriAndRestartMongod(uri, conn);
    coll = conn.getDB("test").getCollection("corrupt");
}

function checkValidate(errorPrefix, numMissingIndexEntries) {
    conn.getDB("test").adminCommand({setParameter: 1, maxValidateMemoryUsageMB: 1});
    const res = coll.validate();
    assert.commandWorked(res);
    assert(!res.valid);
    assert.containsPrefix(errorPrefix, res.errors);
    assert.eq(res.missingIndexEntries.length, numMissingIndexEntries);
}

function checkValidateRepair(expectRepair) {
    const res = coll.validate({repair: true});
    assert.commandWorked(res);
    assert(!res.valid, printjson(res));
    assert.eq(res.repaired, expectRepair, printjson(res));
}

const noneReportedPrefix =
    "Unable to report index entry inconsistencies due to memory limitations.";
const notAllReportedPrefix =
    "Not all index entry inconsistencies are reported due to memory limitations.";

// Insert a document with a key larger than maxValidateMemoryUsageMB so that validate does not
// report any missing index entries.
const indexKey = "a".repeat(kIndexKeyLength);
assert.commandWorked(coll.insert({_id: indexKey}));
corruptIndex();
checkValidate(noneReportedPrefix, 0);

// Can't repair successfully if there aren't any index inconsistencies reported.
checkValidateRepair(false);

// Insert a document with a small key so that validate reports one missing index entry.
assert.commandWorked(coll.insert({_id: 1}));
corruptIndex();
checkValidate(notAllReportedPrefix, 1);

// Repair, but incompletely if only some inconsistencies are reported.
checkValidateRepair(true);

MongoRunner.stopMongod(conn, null, {skipValidation: true});
})();