summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/txn_index_catalog_changes.js
blob: 68e82426056e3b01214be47622aa933d95463d8c (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
/**
 * Verifies that a multi-document transaction aborts with WriteConflictError if an index build has
 * committed since the transaction's read snapshot.
 *
 * @tags: [
 *   requires_replication,
 *   requires_snapshot_read,
 * ]
 */
(function() {
'use strict';

const replTest = new ReplSetTest({nodes: 2});
replTest.startSet();
replTest.initiate();

const primary = replTest.getPrimary();
const db = primary.getDB('test');

// Transaction inserting an index key.
{
    assert.commandWorked(db['c'].insertOne({_id: 0, num: 0}));

    const s0 = db.getMongo().startSession();
    s0.startTransaction();
    assert.commandWorked(s0.getDatabase('test')['c'].deleteOne({_id: 0}));
    s0.commitTransaction();

    const clusterTime = s0.getClusterTime().clusterTime;

    assert.commandWorked(db['c'].createIndex({num: 1}));

    // Start a transaction whose snapshot predates the completion of the index build, and which
    // reserves an oplog entry after the index build commits.
    try {
        const s1 = db.getMongo().startSession();
        s1.startTransaction({readConcern: {level: "snapshot", atClusterTime: clusterTime}});
        s1.getDatabase('test').c.insertOne({_id: 1, num: 1});

        // Transaction should have failed.
        assert(0);
    } catch (e) {
        assert(e.hasOwnProperty("errorLabels"), tojson(e));
        assert.contains("TransientTransactionError", e.errorLabels, tojson(e));
        assert.eq(e["code"], ErrorCodes.WriteConflict, tojson(e));
    }
}

db.c.drop();

// Transaction deleting an index key.
{
    assert.commandWorked(db.createCollection('c'));

    const s0 = db.getMongo().startSession();
    s0.startTransaction();
    assert.commandWorked(s0.getDatabase('test')['c'].insertOne({_id: 0, num: 0}));
    s0.commitTransaction();

    const clusterTime = s0.getClusterTime().clusterTime;

    assert.commandWorked(db['c'].createIndex({num: 1}));

    // Start a transaction whose snapshot predates the completion of the index build, and which
    // reserves an oplog entry after the index build commits.
    try {
        const s1 = db.getMongo().startSession();
        s1.startTransaction({readConcern: {level: "snapshot", atClusterTime: clusterTime}});
        s1.getDatabase('test').c.deleteOne({_id: 0});

        // Transaction should have failed.
        assert(0);
    } catch (e) {
        assert(e.hasOwnProperty("errorLabels"), tojson(e));
        assert.contains("TransientTransactionError", e.errorLabels, tojson(e));
        assert.eq(e["code"], ErrorCodes.WriteConflict, tojson(e));
    }
}

replTest.stopSet();
})();