summaryrefslogtreecommitdiff
path: root/jstests/disk/wt_repair_missing_files.js
blob: 06cc97e5055b769a84c034608557778aa59b4e4b (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
/**
 * Tests that --repair on WiredTiger correctly and gracefully handles missing data files and
 * directories.
 *
 * @tags: [requires_wiredtiger]
 */

(function() {

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

    const baseName = "wt_repair_missing_files";
    const collName = "test";
    const dbpath = MongoRunner.dataPath + baseName + "/";

    resetDbpath(dbpath);

    /**
     * Test 1. Create a collection, delete it's .wt file, run repair. Verify that repair succeeds at
     * re-creating it. The collection should be visible on normal startup.
     */

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

    const doc = {a: 1};
    assert.commandWorked(testColl.insert(doc));

    let testCollUri = getUriForColl(testColl);
    let testCollFile = dbpath + testCollUri + ".wt";

    MongoRunner.stopMongod(mongod);

    jsTestLog("deleting collection file: " + testCollFile);
    removeFile(testCollFile);

    assertRepairSucceeds(dbpath, mongod.port);

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

    assert.eq(testCollUri, getUriForColl(testColl));
    assert.eq(testColl.find({}).itcount(), 0);
    assert.eq(testColl.count(), 0);

    /**
     * Test 2. Delete an index file. Verify that repair rebuilds and allows MongoDB to start up
     * normally.
     */

    assert.commandWorked(testColl.insert(doc));

    const indexName = "a_1";
    assert.commandWorked(testColl.createIndex({a: 1}, {name: indexName}));
    assertQueryUsesIndex(testColl, doc, indexName);

    let indexUri = getUriForIndex(testColl, indexName);

    MongoRunner.stopMongod(mongod);

    let indexFile = dbpath + indexUri + ".wt";
    jsTestLog("deleting index file: " + indexFile);
    removeFile(indexFile);

    assertRepairSucceeds(dbpath, mongod.port);
    mongod = startMongodOnExistingPath(dbpath);
    testColl = mongod.getDB(baseName)[collName];

    // Repair creates new idents.
    assert.neq(indexUri, getUriForIndex(testColl, indexName));

    assertQueryUsesIndex(testColl, doc, indexName);
    assert.eq(testColl.find(doc).itcount(), 1);
    assert.eq(testColl.count(), 1);

    MongoRunner.stopMongod(mongod);

    /**
     * Test 3. Delete the _mdb_catalog. Verify that repair suceeds in creating an empty catalog and
     * MongoDB starts up normally with no data.
     */

    let mdbCatalogFile = dbpath + "_mdb_catalog.wt";
    jsTestLog("deleting catalog file: " + mdbCatalogFile);
    removeFile(mdbCatalogFile);

    assertRepairSucceeds(dbpath, mongod.port);

    mongod = startMongodOnExistingPath(dbpath);
    testColl = mongod.getDB(baseName)[collName];
    assert.commandFailed(testColl.stats());

    assert.eq(testColl.find(doc).itcount(), 0);
    assert.eq(testColl.count(), 0);

    /**
     * Test 4. Verify that using repair with --directoryperdb creates a missing directory and its
     * files, allowing MongoDB to start up normally.
     */

    MongoRunner.stopMongod(mongod);
    resetDbpath(dbpath);

    mongod = startMongodOnExistingPath(dbpath, {directoryperdb: ""});
    testColl = mongod.getDB(baseName)[collName];

    assert.commandWorked(testColl.insert(doc));

    testCollUri = getUriForColl(testColl);

    MongoRunner.stopMongod(mongod);

    let dataDir = dbpath + baseName;
    jsTestLog("deleting data directory: " + dataDir);
    removeFile(dataDir);

    assertRepairSucceeds(dbpath, mongod.port, {directoryperdb: ""});

    mongod = startMongodOnExistingPath(dbpath, {directoryperdb: ""});
    testColl = mongod.getDB(baseName)[collName];

    assert.eq(testCollUri, getUriForColl(testColl));
    assert.eq(testColl.find({}).itcount(), 0);
    assert.eq(testColl.count(), 0);

    MongoRunner.stopMongod(mongod);
})();