summaryrefslogtreecommitdiff
path: root/jstests/replsets/optime.js
blob: a716ca3dbca91dec3adc29786144ea3a4d94444c (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
// Tests tracking of latestOptime and earliestOptime in serverStatus.oplog

function timestampCompare(o1, o2) {
    if (o1.t < o2.t) {
        return -1;
    } else if (o1.t > o2.t) {
        return 1;
    } else {
        if (o1.i < o2.i) {
            return -1;
        } else if (o1.i > o2.i) {
            return 1;
        } else {
            return 0;
        }
    }
}

function optimesAreEqual(replTest) {
    var prevStatus = replTest.nodes[0].getDB('admin').serverStatus({oplog: true}).oplog;
    for (var i = 1; i < replTest.nodes.length; i++) {
        var status = replTest.nodes[i].getDB('admin').serverStatus({oplog: true}).oplog;
        if (timestampCompare(prevStatus.latestOptime, status.latestOptime) != 0) {
            return false;
        }
        prevStatus = status;
    }
    return true;
}

var replTest = new ReplSetTest({name: "replStatus", nodes: 3, oplogSize: 1});

replTest.startSet();
replTest.initiate();
var master = replTest.getPrimary();
replTest.awaitReplication();
replTest.awaitSecondaryNodes();

// Check initial optimes
assert(optimesAreEqual(replTest));
var initialInfo = master.getDB('admin').serverStatus({oplog: true}).oplog;

// Do an insert to increment optime, but without rolling the oplog
// latestOptime should be updated, but earliestOptime should be unchanged
var options = {
    writeConcern: {w: replTest.nodes.length}
};
assert.writeOK(master.getDB('test').foo.insert({a: 1}, options));
assert(optimesAreEqual(replTest));

var info = master.getDB('admin').serverStatus({oplog: true}).oplog;
assert.gt(timestampCompare(info.latestOptime, initialInfo.latestOptime), 0);
assert.eq(timestampCompare(info.earliestOptime, initialInfo.earliestOptime), 0);

// Insert some large documents to force the oplog to roll over
var largeString = new Array(1024 * 10).toString();
for (var i = 0; i < 2000; i++) {
    master.getDB('test').foo.insert({largeString: largeString}, options);
}
assert.soon(function() {
    return optimesAreEqual(replTest);
});

// Test that earliestOptime was updated
info = master.getDB('admin').serverStatus({oplog: true}).oplog;
assert.gt(timestampCompare(info.latestOptime, initialInfo.latestOptime), 0);
assert.gt(timestampCompare(info.earliestOptime, initialInfo.earliestOptime), 0);

replTest.stopSet();