summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/dur_remove_old_journals.js
blob: 334d6f5ffae3694ab073d8ac05a6a90ba2830c34 (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
// this test makes sure that old journal files are removed

var dbpath = MongoRunner.dataDir + "/dur_remove_old_journals";
var conn = MongoRunner.runMongod({
    journal: "",
    smallfiles: "",
    syncdelay: 5,  // seconds between fsyncs.
    dbpath: dbpath,
});
db = conn.getDB("test");

// Returns true if j._0 exists.
function firstJournalFileExists() {
    var files = listFiles(dbpath + "/journal");
    for (var i = 0; i < files.length; i++) {
        if (files[i].baseName === "j._0") {
            return true;
        }
    }
    return false;
}

// Represents the cummulative total of the number of journal files created.
function getLatestJournalFileNum() {
    var files = listFiles(dbpath + "/journal");
    var latest = 0;
    files.forEach(function(file) {
        if (file.baseName !== "lsn") {
            var fileNum = NumberInt(file.baseName[file.baseName.length - 1]);
            latest = Math.max(latest, fileNum);
        }
    });
    return latest;
}

var stringSize = 1024*1024;
var longString = new Array(stringSize).join("x");

// Insert some data to create the first journal file.
var numInserted = 0;
while (numInserted < 100) {
    db.foo.insert({_id: numInserted++, s: longString});
}
assert.soon(firstJournalFileExists, "Should have created a journal file");


// Do writes until the first journal file is deleted, or we give up waiting.
var maxJournalFiles = 10;
while (firstJournalFileExists() && getLatestJournalFileNum() < maxJournalFiles) {
    db.foo.insert({_id: numInserted++, s: longString});

    if (numInserted % 100 == 0){
        jsTestLog("numInserted: " + numInserted);
        db.adminCommand({fsync:1});
        db.foo.remove({});
        db.adminCommand({fsync:1});
    }
}

assert(!firstJournalFileExists(), "Expected to have deleted the first journal file by now");
MongoRunner.stopMongod(conn);
print("*** success ***");