summaryrefslogtreecommitdiff
path: root/jstests/core/bulk_legacy_enforce_gle.js
blob: 88b7c51e7582c2a0a7a7de8f3f7f26720379e652 (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
/**
 * Tests the resetError logic when the bulk api enforces the write concern for unordered
 * writes. The tests indirectly checks whether resetError was called by inspecting the
 * response of the getLastError command after executing the bulk ops.
 *
 */

(function() {
    "use strict";
    const coll = db.bulk_legacy_enforce_gle;

    /**
     * Inserts 'doc' into the collection, asserting that the write succeeds. This runs a
     * getLastError if the insert does not return a response.
     */
    function insertDocument(doc) {
        let res = coll.insert(doc);
        if (res) {
            assert.writeOK(res);
        } else {
            assert.gleOK(db.runCommand({getLastError: 1}));
        }
    }

    coll.drop();
    let bulk = coll.initializeUnorderedBulkOp();
    bulk.find({_id: 1}).upsert().updateOne({_id: 1});
    assert.writeOK(bulk.execute());
    let gle = assert.gleOK(db.runCommand({getLastError: 1}));
    assert.eq(1, gle.n, tojson(gle));

    // Batch of size 1 should not call resetError even when it errors out.
    assert(coll.drop());
    insertDocument({_id: 1});
    bulk = coll.initializeUnorderedBulkOp();
    bulk.find({none: 1}).upsert().updateOne({_id: 1});
    assert.throws(function() {
        bulk.execute();
    });

    gle = db.runCommand({getLastError: 1});
    assert(gle.ok, tojson(gle));
    assert.neq(null, gle.err, tojson(gle));

    // Batch with all error except last should not call resetError.
    assert(coll.drop());
    insertDocument({_id: 1});
    bulk = coll.initializeUnorderedBulkOp();
    bulk.find({none: 1}).upsert().updateOne({_id: 1});
    bulk.find({none: 1}).upsert().updateOne({_id: 1});
    bulk.find({_id: 0}).upsert().updateOne({_id: 0});
    let res = assert.throws(function() {
        bulk.execute();
    });
    assert.eq(2, res.getWriteErrors().length);

    gle = db.runCommand({getLastError: 1});
    assert(gle.ok, tojson(gle));
    assert.eq(1, gle.n, tojson(gle));

    // Batch with error at middle should not call resetError.
    assert(coll.drop());
    insertDocument({_id: 1});
    bulk = coll.initializeUnorderedBulkOp();
    bulk.find({_id: 0}).upsert().updateOne({_id: 0});
    bulk.find({none: 1}).upsert().updateOne({_id: 1});
    bulk.find({_id: 2}).upsert().updateOne({_id: 2});
    res = assert.throws(function() {
        bulk.execute();
    });
    assert.eq(1, res.getWriteErrors().length);

    gle = db.runCommand({getLastError: 1});
    assert(gle.ok, tojson(gle));
    // For legacy writes, mongos sends the bulk as one while the shell sends the write individually.
    assert.gte(gle.n, 1, tojson(gle));

    // Batch with error at last should call resetError.
    assert(coll.drop());
    insertDocument({_id: 2});
    bulk = coll.initializeUnorderedBulkOp();
    bulk.find({_id: 0}).upsert().updateOne({_id: 0});
    bulk.find({_id: 1}).upsert().updateOne({_id: 1});
    bulk.find({none: 1}).upsert().updateOne({_id: 2});
    res = assert.throws(function() {
        bulk.execute();
    });
    assert.eq(1, res.getWriteErrors().length);

    gle = db.runCommand({getLastError: 1});
    assert(gle.ok, tojson(gle));
    assert.eq(0, gle.n, tojson(gle));

    // Batch with error at last should not call resetError if { w: 1 }.
    assert(coll.drop());
    insertDocument({_id: 2});
    bulk = coll.initializeUnorderedBulkOp();
    bulk.find({_id: 0}).upsert().updateOne({_id: 0});
    bulk.find({_id: 1}).upsert().updateOne({_id: 1});
    bulk.find({none: 1}).upsert().updateOne({_id: 2});
    res = assert.throws(function() {
        bulk.execute();
    });
    assert.eq(1, res.getWriteErrors().length);

    gle = db.runCommand({getLastError: 1, w: 1});
    assert(gle.ok, tojson(gle));
    assert.neq(null, gle.err, tojson(gle));

    // Batch with error at last should not call resetError if { w: 0 }.
    assert(coll.drop());
    insertDocument({_id: 2});
    bulk = coll.initializeUnorderedBulkOp();
    bulk.find({_id: 0}).upsert().updateOne({_id: 0});
    bulk.find({_id: 1}).upsert().updateOne({_id: 1});
    bulk.find({none: 1}).upsert().updateOne({_id: 2});
    res = assert.throws(function() {
        bulk.execute();
    });
    assert.eq(1, res.getWriteErrors().length, () => tojson(res));

    gle = db.runCommand({getLastError: 1, w: 0});
    assert(gle.ok, tojson(gle));
    assert.neq(null, gle.err, tojson(gle));
}());