summaryrefslogtreecommitdiff
path: root/jstests/dur/dur1_tool.js
blob: f0c2916012da58f753e986d4ce55dbb2bd8fef7d (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
145
146
147
148
149
150
151
/* 
   test durability option with tools (same a dur1.js but use mongorestore to do repair)
*/

var debugging = false;
var testname = "dur1_tool";
var step = 1;
var conn = null;

function checkNoJournalFiles(path, pass) {
    var files = listFiles(path);
    if (files.some(function (f) { return f.name.indexOf("prealloc") < 0; })) {
        if (pass == null) {
            // wait a bit longer for mongod to potentially finish if it is still running.
            sleep(10000);
            return checkNoJournalFiles(path, 1);
        }
        print("\n\n\n");
        print("FAIL path:" + path);
        print("unexpected files:");
        printjson(files);
        assert(false, "FAIL a journal/lsn file is present which is unexpected");
    }
}

function runDiff(a, b) {
    function reSlash(s) {
        var x = s;
        if (_isWindows()) {
            while (1) {
                var y = x.replace('/', '\\');
                if (y == x)
                    break;
                x = y;
            }
        }
        return x;
    }
    a = reSlash(a);
    b = reSlash(b);
    print("diff " + a + " " + b);
    return run("diff", a, b);
}

function log(str) {
    print();
    if(str)
        print(testname+" step " + step++ + " " + str);
    else
        print(testname+" step " + step++);
}

// if you do inserts here, you will want to set _id.  otherwise they won't match on different 
// runs so we can't do a binary diff of the resulting files to check they are consistent.
function work() {
    log("work");
    var d = conn.getDB("test");
    d.foo.insert({ _id: 3, x: 22 });
    d.foo.insert({ _id: 4, x: 22 });
    d.a.insert({ _id: 3, x: 22, y: [1, 2, 3] });
    d.a.insert({ _id: 4, x: 22, y: [1, 2, 3] });
    d.a.update({ _id: 4 }, { $inc: { x: 1} });

    // try building an index.  however, be careful as object id's in system.indexes would vary, so we do it manually:
    d.system.indexes.insert({ _id: 99, ns: "test.a", key: { x: 1 }, name: "x_1" });
    log("endwork");
    return d;
}

function verify() { 
    log("verify test.foo.count == 2");
    var d = conn.getDB("test");
    var ct = d.foo.count();
    if (ct != 2) {
        print("\n\n\nFAIL dur1.js count is wrong in verify(): " + ct + "\n\n\n");
        assert(ct == 2);
    }
}

if( debugging ) { 
    // mongod already running in debugger
    conn = db.getMongo();
    work();
    sleep(30000);
    quit();
}

log();

// directories
var path1 = MongoRunner.dataPath + testname+"nodur";
var path2 = MongoRunner.dataPath + testname+"dur";

// non-durable version
log("run mongod without journaling");

conn = MongoRunner.runMongod({dbpath: path1, nodur: "", smallfiles: ""});
work();
MongoRunner.stopMongod(conn);

// durable version
log("run mongod with --journal");
conn = MongoRunner.runMongod({dbpath: path2, journal: "", smallfiles: "", journalOptions: 8});
work();

// wait for group commit.
printjson(conn.getDB('admin').runCommand({getlasterror:1, fsync:1}));

// kill the process hard
log("kill 9");
MongoRunner.stopMongod(conn, /*signal*/9);

// journal file should be present, and non-empty as we killed hard

// mongod with --dbpath and --journal options should do a recovery pass
// empty.bson is an empty file so it won't actually insert anything
log("use mongod to recover");
conn = MongoRunner.runMongod({restart: true,
                              cleanData: false,
                              dbpath: path2,
                              journal: "",
                              smallfiles: "",
                              noprealloc: "",
                              bind_ip: "127.0.0.1"});
verify();
MongoRunner.stopMongod(conn);

// at this point, after clean shutdown, there should be no journal files
log("check no journal files (after presumably clean shutdown)");
checkNoJournalFiles(path2 + "/journal");

log("check data matches ns");
var diff = runDiff(path1 + "/test.ns", path2 + "/test.ns");
if (diff != "") {
    print("\n\n\nDIFFERS\n");
    print(diff);
}
assert(diff == "", "error test.ns files differ");

log("check data matches .0");
var diff = runDiff(path1 + "/test.0", path2 + "/test.0");
if (diff != "") {
    print("\n\n\nDIFFERS\n");
    print(diff);
}
assert(diff == "", "error test.0 files differ");

log("check data matches done");

print(testname + " SUCCESS");