summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/out/batch_writes.js
blob: 941b795d27db9eccebdd42bd0d257f183f37a531 (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
// Tests the behavior of an $out stage which encounters an error in the middle of processing. We
// don't guarantee any particular behavior in this scenario, but this test exists to make sure
// nothing horrendous happens and to characterize the current behavior.
// @tags: [assumes_unsharded_collection]
(function() {
    "use strict";

    load("jstests/aggregation/extras/utils.js");  // For assertErrorCode.

    const coll = db.batch_writes;
    const outColl = db.batch_writes_out;
    coll.drop();
    outColl.drop();

    // Test with 2 very large documents that do not fit into a single batch.
    const kSize15MB = 15 * 1024 * 1024;
    const largeArray = new Array(kSize15MB).join("a");
    assert.commandWorked(coll.insert({_id: 0, a: largeArray}));
    assert.commandWorked(coll.insert({_id: 1, a: largeArray}));

    // Make sure the $out succeeds without any duplicate keys.
    ["replaceCollection", "insertDocuments", "replaceDocuments"].forEach(mode => {
        coll.aggregate([{$out: {to: outColl.getName(), mode: mode}}]);
        assert.eq(2, outColl.find().itcount());
        outColl.drop();
    });

    coll.drop();
    for (let i = 0; i < 10; i++) {
        assert.commandWorked(coll.insert({_id: i, a: i}));
    }

    // Create a unique index on 'a' in the output collection to create a unique key violation when
    // running the $out. The second document to be written ({_id: 1, a: 1}) will conflict with the
    // existing document in the output collection. We use a unique index on a field other than _id
    // because "replaceDocuments" mode will not change _id when one already exists.
    outColl.drop();
    assert.commandWorked(outColl.insert({_id: 2, a: 1}));
    assert.commandWorked(outColl.createIndex({a: 1}, {unique: true}));

    // Test that both batched updates and inserts will successfully write the first document but
    // fail on the second. We don't guarantee any particular behavior in this case, but this test is
    // meant to characterize the current behavior.
    ["insertDocuments", "replaceDocuments"].forEach(mode => {
        assertErrorCode(
            coll, [{$out: {to: outColl.getName(), mode: mode}}], ErrorCodes.DuplicateKey);
        assert.soon(() => {
            return outColl.find().itcount() == 2;
        });
    });

    // Mode "replaceCollection" will drop the contents of the output collection, so there is no
    // duplicate key error.
    outColl.drop();
    assert.commandWorked(outColl.insert({_id: 2, a: 1}));
    assert.commandWorked(outColl.createIndex({a: 1}, {unique: true}));
    coll.aggregate([{$out: {to: outColl.getName(), mode: "replaceCollection"}}]);
    assert.eq(10, outColl.find().itcount());
}());