summaryrefslogtreecommitdiff
path: root/jstests/replsets/retryable_write_concern.js
blob: 953b83a70fca73373e047a018a943915b36a4e3d (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
/**
 * Tests for making sure that retried writes will wait properly for writeConcern.
 */
(function() {

    "use strict";

    load("jstests/libs/retryable_writes_util.js");
    load("jstests/libs/write_concern_util.js");

    if (!RetryableWritesUtil.storageEngineSupportsRetryableWrites(jsTest.options().storageEngine)) {
        jsTestLog("Retryable writes are not supported, skipping test");
        return;
    }

    const kNodes = 2;

    let checkWriteConcernTimedOut = function(result) {
        let wcErr = result.writeConcernError;
        assert.neq(null, wcErr, tojson(result));

        let errInfo = wcErr.errInfo;
        assert.neq(null, errInfo, tojson(result));

        assert.eq(true, errInfo.wtimeout, tojson(result));
    };

    let runTest = function(priConn, secConn, cmd) {
        // Send a dummy write to this connection so it will have the Client object initialized.
        let secondPriConn = new Mongo(priConn.host);
        let testDB2 = secondPriConn.getDB('test');
        assert.writeOK(testDB2.dummy.insert({x: 1}, {writeConcern: {w: kNodes}}));

        stopServerReplication(secConn);

        let testDB = priConn.getDB('test');
        checkWriteConcernTimedOut(testDB.runCommand(cmd));
        checkWriteConcernTimedOut(testDB2.runCommand(cmd));

        restartServerReplication(secConn);
    };

    let replTest = new ReplSetTest({nodes: kNodes});
    replTest.startSet({verbose: 1});
    replTest.initiate();

    let priConn = replTest.getPrimary();
    let secConn = replTest.getSecondary();

    let lsid = UUID();

    runTest(priConn, secConn, {
        insert: 'user',
        documents: [{_id: 10}, {_id: 30}],
        ordered: false,
        lsid: {id: lsid},
        txnNumber: NumberLong(34),
        writeConcern: {w: 'majority', wtimeout: 200},
    });

    runTest(priConn, secConn, {
        update: 'user',
        updates: [
            {q: {_id: 10}, u: {$inc: {x: 1}}},
        ],
        ordered: false,
        lsid: {id: lsid},
        txnNumber: NumberLong(35),
        writeConcern: {w: 'majority', wtimeout: 200},
    });

    runTest(priConn, secConn, {
        delete: 'user',
        deletes: [{q: {x: 1}, limit: 1}, {q: {y: 1}, limit: 1}],
        ordered: false,
        lsid: {id: lsid},
        txnNumber: NumberLong(36),
        writeConcern: {w: 'majority', wtimeout: 200},
    });

    runTest(priConn, secConn, {
        findAndModify: 'user',
        query: {_id: 60},
        update: {$inc: {x: 1}},
        new: true,
        upsert: true,
        lsid: {id: lsid},
        txnNumber: NumberLong(37),
        writeConcern: {w: 'majority', wtimeout: 200},
    });

    replTest.stopSet();
})();