summaryrefslogtreecommitdiff
path: root/jstests/dur/dur2.js
blob: 5ff6aa9840e4194988af864b79cc6943a52026b5 (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
/* test durability
   runs mongod, kill -9's, recovers
*/

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

var start = new Date();
function howLongSecs() {
    return (new Date() - start) / 1000;
}

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

function verify() {
    log("verify");
    var d = conn.getDB("test");
    var mycount = d.foo.count();
    // print("count:" + mycount);
    assert(mycount > 2, "count wrong");
}

function work() {
    log("work");
    x = 'x';
    while (x.length < 1024)
        x += x;
    var d = conn.getDB("test");
    d.foo.drop();
    d.foo.insert({});

    // go long enough we will have time to kill it later during recovery
    var j = 2;
    var MaxTime = 90;
    while (1) {
        d.foo.insert({_id: j, z: x});
        d.foo.update({_id: j}, {$inc: {a: 1}});
        if (j % 25 == 0)
            d.foo.remove({_id: j});
        j++;
        if (j % 3 == 0)
            d.foo.update({_id: j}, {$inc: {a: 1}}, true);
        if (j % 10000 == 0)
            print(j);
        if (howLongSecs() > MaxTime)
            break;
    }

    verify();
    d.runCommand({getLastError: 1, fsync: 1});
}

if (debugging) {
    // mongod already running in debugger
    print(
        "DOING DEBUG MODE BEHAVIOR AS 'db' IS DEFINED -- RUN mongo --nodb FOR REGULAR TEST BEHAVIOR");
    conn = db.getMongo();
    work();
    sleep(30000);
    quit();
}

// directories
var path = MongoRunner.dataPath + testname + "dur";

log("run mongod with --dur");
conn = MongoRunner.runMongod({
    dbpath: path,
    journal: "",
    smallfiles: "",
    journalOptions: 8 /*DurParanoid*/,
    master: "",
    oplogSize: 64
});
work();

log("kill -9");
MongoRunner.stopMongod(conn, /*signal*/ 9);

// journal file should be present, and non-empty as we killed hard
assert(listFiles(path + "/journal/").length > 0,
       "journal directory is unexpectantly empty after kill");

// restart and recover
log("restart mongod and recover");
conn = MongoRunner.runMongod({
    restart: true,
    cleanData: false,
    dbpath: path,
    journal: "",
    smallfiles: "",
    journalOptions: 8,
    master: "",
    oplogSize: 64
});
verify();

log("stopping mongod " + conn.port);
MongoRunner.stopMongod(conn);

print(testname + " SUCCESS");