summaryrefslogtreecommitdiff
path: root/jstests/mmap_v1/dur_remove_old_journals.js
blob: 0d1b930815b8e11ae3e37bec814c6482b03c2549 (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
/**
 * Test that old journal files are eventually deleted.
 */

if (db.serverBuildInfo().bits == 32) {
    print("skip on 32 bit systems");
} else {
    var conn = MongoRunner.runMongod({
        journal: "",
        smallfiles: "",
        syncdelay: 5,  // seconds between fsyncs.
    });
    db = conn.getDB("test");

    // listFiles can return Access Denied on Windows if the file
    // is deleted at the same time as listFiles is run, in this
    // case we sleep and retry.
    function listFilesRetry(path) {
        for (var i = 0; i < 5; ++i) {
            try {
                return listFiles(path);
            } catch (e) {
                print("Exception during listFiles: " + e);
                // Sleep for 10 milliseconds
                sleep(10);
            }
        }

        throw new Error("listFilesRetry failed");
    }

    // Returns true if j._0 exists.
    function firstJournalFileExists() {
        var files = listFilesRetry(conn.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 = listFilesRetry(conn.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});
            gc();
        }
    }

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