summaryrefslogtreecommitdiff
path: root/jstests/disk/repair2.js
blob: 67dd0f4cdf7adc7568a7911df9adfd1401a08830 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// repair with --directoryperdb

var baseName = "jstests_disk_repair2";

function check() {
    files = listFiles(dbpath);
    for (f in files) {
        assert(!new RegExp("^" + dbpath + "backup_").test(files[f].name),
               "backup dir " + files[f].name + " in dbpath");
    }

    assert.eq.automsg("1", "db[ baseName ].count()");
}

var dbpath = MongoRunner.dataPath + baseName + "/";
var repairpath = dbpath + "repairDir/";
var longDBName = Array(61).join('a');
var longRepairPath = dbpath + Array(61).join('b') + '/';

resetDbpath(dbpath);
resetDbpath(repairpath);

var m = MongoRunner.runMongod({
    directoryperdb: "",
    dbpath: dbpath,
    repairpath: repairpath,
    noCleanData: true,
});
db = m.getDB(baseName);
db[baseName].save({});
assert.commandWorked(db.runCommand({repairDatabase: 1, backupOriginalFiles: true}));

// Check that repair files exist in the repair directory, and nothing else
db.adminCommand({fsync: 1});
files = listFiles(repairpath + "/backup_repairDatabase_0/" + baseName);
var fileCount = 0;
for (f in files) {
    print(files[f].name);
    if (files[f].isDirectory)
        continue;
    fileCount += 1;
    assert(/\.bak$/.test(files[f].name),
           "In database repair directory, found unexpected file: " + files[f].name);
}
assert(fileCount > 0, "Expected more than zero nondirectory files in the database directory");

check();
MongoRunner.stopMongod(m.port);

resetDbpath(repairpath);
m = MongoRunner.runMongod({
    port: m.port,
    directoryperdb: "",
    dbpath: dbpath,
    noCleanData: true,
});
db = m.getDB(baseName);
assert.commandWorked(db.runCommand({repairDatabase: 1}));
check();
MongoRunner.stopMongod(m.port);

// Test long database names
resetDbpath(repairpath);
m = MongoRunner.runMongod({
    port: m.port,
    directoryperdb: "",
    dbpath: dbpath,
    noCleanData: true,
});
db = m.getDB(longDBName);
assert.writeOK(db[baseName].save({}));
assert.commandWorked(db.runCommand({repairDatabase: 1}));
MongoRunner.stopMongod(m.port);

// Test long repairPath
resetDbpath(longRepairPath);
m = MongoRunner.runMongod({
    port: m.port,
    directoryperdb: "",
    dbpath: dbpath,
    repairpath: longRepairPath,
    noCleanData: true,
});
db = m.getDB(longDBName);
assert.commandWorked(db.runCommand({repairDatabase: 1, backupOriginalFiles: true}));
check();
MongoRunner.stopMongod(m.port);

// Test database name and repairPath with --repair
resetDbpath(longRepairPath);
var returnCode = runMongoProgram("mongod",
                                 "--port",
                                 m.port,
                                 "--repair",
                                 "--directoryperdb",
                                 "--dbpath",
                                 dbpath,
                                 "--repairpath",
                                 longRepairPath);
assert.eq(returnCode, 0);
m = MongoRunner.runMongod({
    port: m.port,
    directoryperdb: "",
    dbpath: dbpath,
    noCleanData: true,
});
db = m.getDB(longDBName);
check();
MongoRunner.stopMongod(m.port);

resetDbpath(repairpath);
returnCode = runMongoProgram("mongod",
                             "--port",
                             m.port,
                             "--repair",
                             "--directoryperdb",
                             "--dbpath",
                             dbpath,
                             "--repairpath",
                             repairpath);
assert.eq(returnCode, 0);
m = MongoRunner.runMongod({
    port: m.port,
    directoryperdb: "",
    dbpath: dbpath,
    repairpath: repairpath,
    noCleanData: true,
});
db = m.getDB(baseName);
check();
MongoRunner.stopMongod(m.port);

resetDbpath(repairpath);
returnCode =
    runMongoProgram("mongod", "--port", m.port, "--repair", "--directoryperdb", "--dbpath", dbpath);
assert.eq(returnCode, 0);
m = MongoRunner.runMongod({
    port: m.port,
    directoryperdb: "",
    dbpath: dbpath,
    noCleanData: true,
});
db = m.getDB(baseName);
check();