summaryrefslogtreecommitdiff
path: root/jstests/replsets/read_after_optime.js
blob: 1059d5b1c622e91f63252df7d2bd67d88f898031 (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
// Test read after opTime functionality with maxTimeMS.

(function() {
    "use strict";
    load("jstests/libs/check_log.js");

    var replTest = new ReplSetTest({nodes: 2});
    replTest.startSet();
    replTest.initiate();
    var config = replTest.getReplSetConfigFromNode();

    var runTest = function(testDB, primaryConn) {
        var dbName = testDB.getName();
        assert.writeOK(primaryConn.getDB(dbName).user.insert({x: 1}, {writeConcern: {w: 2}}));

        var localDB = primaryConn.getDB('local');

        var oplogTS = localDB.oplog.rs.find().sort({$natural: -1}).limit(1).next();
        var twoKSecTS = new Timestamp(oplogTS.ts.getTime() + 2000, 0);

        var term = oplogTS.t;

        // Test timeout with maxTimeMS
        var runTimeoutTest = function() {
            assert.commandFailedWithCode(testDB.runCommand({
                find: 'user',
                filter: {x: 1},
                readConcern: {afterOpTime: {ts: twoKSecTS, t: term}},
                maxTimeMS: 5000,
            }),
                                         ErrorCodes.ExceededTimeLimit);
        };

        // Run the time out test 3 times with replication debug log level increased to 2
        // for first and last run. The time out message should be logged twice.
        testDB.setLogLevel(2, 'command');
        runTimeoutTest();
        testDB.setLogLevel(0, 'command');

        var msg = 'Command on database ' + testDB.getName() +
            ' timed out waiting for read concern to be satisfied. Command:';
        checkLog.containsWithCount(testDB.getMongo(), msg, 1);

        // Read concern timed out message should not be logged.
        runTimeoutTest();

        testDB.setLogLevel(2, 'command');
        runTimeoutTest();
        testDB.setLogLevel(0, 'command');

        checkLog.containsWithCount(testDB.getMongo(), msg, 2);

        // Test read on future afterOpTime that will eventually occur.
        primaryConn.getDB(dbName).parallelShellStarted.drop();
        oplogTS = localDB.oplog.rs.find().sort({$natural: -1}).limit(1).next();
        var insertFunc = startParallelShell('let testDB = db.getSiblingDB("' + dbName + '"); ' +
                                                'sleep(3000); ' +
                                                'testDB.user.insert({y: 1});',
                                            primaryConn.port);

        var twoSecTS = new Timestamp(oplogTS.ts.getTime() + 2, 0);
        var res = assert.commandWorked(testDB.runCommand({
            find: 'user',
            filter: {y: 1},
            readConcern: {
                afterOpTime: {ts: twoSecTS, t: term},
            },
            maxTimeMS: 90 * 1000,
        }));

        assert.eq(null, res.code);
        assert.eq(res.cursor.firstBatch[0].y, 1);
        insertFunc();
    };

    var primary = replTest.getPrimary();
    jsTest.log("test1");
    runTest(primary.getDB('test1'), primary);
    jsTest.log("test2");
    runTest(replTest.getSecondary().getDB('test2'), primary);

    replTest.stopSet();
})();