summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/reindex_crash_rebuilds_id_index.js
blob: 573810fd2624428e90b1579c39f4146760a85ee1 (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
/**
 * If reIndex crashes after dropping indexes but before rebuilding them, a collection may exist
 * without an _id index. On startup, mongod should automatically build any missing _id indexes.
 *
 * @tags: [
 *   requires_journaling,
 *   requires_persistence
 * ]
 */
(function() {

load("jstests/libs/get_index_helpers.js");  // For GetIndexHelpers.

// This test triggers an unclean shutdown, which may cause inaccurate fast counts.
TestData.skipEnforceFastCountOnValidate = true;

const baseName = 'reindex_crash_rebuilds_id_index';
const collName = baseName;
const dbpath = MongoRunner.dataPath + baseName + '/';
resetDbpath(dbpath);

const mongodOptions = {
    dbpath: dbpath,
    noCleanData: true
};
let conn = MongoRunner.runMongod(mongodOptions);

let testDB = conn.getDB('test');
let testColl = testDB.getCollection(collName);

// Insert a single document and create the collection.
testColl.insert({a: 1});
let spec = GetIndexHelpers.findByKeyPattern(testColl.getIndexes(), {_id: 1});
assert.neq(null, spec, "_id index not found");
assert.eq("_id_", spec.name, tojson(spec));

// Enable a failpoint that causes reIndex to crash after dropping the indexes but before
// rebuilding them.
assert.commandWorked(
    testDB.adminCommand({configureFailPoint: 'reIndexCrashAfterDrop', mode: 'alwaysOn'}));
assert.throws(() => testColl.runCommand({reIndex: collName}));

// The server should have crashed from the failpoint.
MongoRunner.stopMongod(conn, null, {allowedExitCode: MongoRunner.EXIT_ABRUPT});

// The server should start up successfully after rebuilding the _id index.
conn = MongoRunner.runMongod(mongodOptions);
testDB = conn.getDB('test');
testColl = testDB.getCollection(collName);
assert(testColl.exists());

// The _id index should exist.
spec = GetIndexHelpers.findByKeyPattern(testColl.getIndexes(), {_id: 1});
assert.neq(null, spec, "_id index not found");
assert.eq("_id_", spec.name, tojson(spec));

MongoRunner.stopMongod(conn);
})();