summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/change_stream_transaction.js
blob: 801eab6f0a0c83927eaabd6494f02c552ddafb76 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Confirms that change streams only see committed operations for prepared transactions.
// @tags: [uses_transactions,uses_change_streams,requires_majority_read_concern,
// exclude_from_large_txns_due_to_change_streams]
(function() {
    "use strict";

    load("jstests/core/txns/libs/prepare_helpers.js");  // For PrepareHelpers.

    const dbName = "test";
    const collName = "change_stream_transaction";

    /**
     * Asserts that the expected operation type and documentKey are found on the change stream
     * cursor. Returns the change stream document.
     */
    function assertWriteVisible(cursor, operationType, documentKey) {
        assert.soon(() => cursor.hasNext());
        const changeDoc = cursor.next();
        assert.eq(operationType, changeDoc.operationType, changeDoc);
        assert.eq(documentKey, changeDoc.documentKey, changeDoc);
        return changeDoc;
    }

    /**
     * Asserts that the expected operation type and documentKey are found on the change stream
     * cursor. Pushes the corresponding resume token and change stream document to an array.
     */
    function assertWriteVisibleWithCapture(cursor, operationType, documentKey, changeList) {
        const changeDoc = assertWriteVisible(cursor, operationType, documentKey);
        changeList.push(changeDoc);
    }

    /**
     * Asserts that there are no changes waiting on the change stream cursor.
     */
    function assertNoChanges(cursor) {
        assert(!cursor.hasNext(), () => {
            return "Unexpected change set: " + tojson(cursor.toArray());
        });
    }

    function runTest(conn) {
        const db = conn.getDB(dbName);
        const coll = db.getCollection(collName);
        const unwatchedColl = db.getCollection(collName + "_unwatched");
        let changeList = [];

        // Collections must be created outside of any transaction.
        assert.commandWorked(db.createCollection(coll.getName()));
        assert.commandWorked(db.createCollection(unwatchedColl.getName()));

        //
        // Start transaction 1.
        //
        const session1 = db.getMongo().startSession();
        const sessionDb1 = session1.getDatabase(dbName);
        const sessionColl1 = sessionDb1[collName];
        session1.startTransaction({readConcern: {level: "majority"}});

        //
        // Start transaction 2.
        //
        const session2 = db.getMongo().startSession();
        const sessionDb2 = session2.getDatabase(dbName);
        const sessionColl2 = sessionDb2[collName];
        session2.startTransaction({readConcern: {level: "majority"}});

        //
        // Start transaction 3.
        //
        const session3 = db.getMongo().startSession();
        const sessionDb3 = session3.getDatabase(dbName);
        const sessionColl3 = sessionDb3[collName];
        session3.startTransaction({readConcern: {level: "majority"}});

        // Open a change stream on the test collection.
        const changeStreamCursor = coll.watch();

        // Insert a document and confirm that the change stream has it.
        assert.commandWorked(coll.insert({_id: "no-txn-doc-1"}, {writeConcern: {w: "majority"}}));
        assertWriteVisibleWithCapture(
            changeStreamCursor, "insert", {_id: "no-txn-doc-1"}, changeList);

        // Insert two documents under each transaction and confirm no change stream updates.
        assert.commandWorked(sessionColl1.insert([{_id: "txn1-doc-1"}, {_id: "txn1-doc-2"}]));
        assert.commandWorked(sessionColl2.insert([{_id: "txn2-doc-1"}, {_id: "txn2-doc-2"}]));
        assertNoChanges(changeStreamCursor);

        // Update one document under each transaction and confirm no change stream updates.
        assert.commandWorked(sessionColl1.update({_id: "txn1-doc-1"}, {$set: {"updated": 1}}));
        assert.commandWorked(sessionColl2.update({_id: "txn2-doc-1"}, {$set: {"updated": 1}}));
        assertNoChanges(changeStreamCursor);

        // Update and then remove the second doc under each transaction and confirm no change stream
        // events are seen.
        assert.commandWorked(
            sessionColl1.update({_id: "txn1-doc-2"}, {$set: {"update-before-delete": 1}}));
        assert.commandWorked(
            sessionColl2.update({_id: "txn2-doc-2"}, {$set: {"update-before-delete": 1}}));
        assert.commandWorked(sessionColl1.remove({_id: "txn1-doc-2"}));
        assert.commandWorked(sessionColl2.remove({_id: "txn2-doc-2"}));
        assertNoChanges(changeStreamCursor);

        // Perform a write to the 'session1' transaction in a collection that is not being watched
        // by 'changeStreamCursor'. We do not expect to see this write in the change stream either
        // now or on commit.
        assert.commandWorked(
            sessionDb1[unwatchedColl.getName()].insert({_id: "txn1-doc-unwatched-collection"}));
        assertNoChanges(changeStreamCursor);

        // Perform a write to the 'session3' transaction in a collection that is not being watched
        // by 'changeStreamCursor'. We do not expect to see this write in the change stream either
        // now or on commit.
        assert.commandWorked(
            sessionDb3[unwatchedColl.getName()].insert({_id: "txn3-doc-unwatched-collection"}));
        assertNoChanges(changeStreamCursor);

        // Perform a write outside of a transaction and confirm that the change stream sees only
        // this write.
        assert.commandWorked(coll.insert({_id: "no-txn-doc-2"}, {writeConcern: {w: "majority"}}));
        assertWriteVisibleWithCapture(
            changeStreamCursor, "insert", {_id: "no-txn-doc-2"}, changeList);
        assertNoChanges(changeStreamCursor);

        let prepareTimestampTxn1;
        prepareTimestampTxn1 = PrepareHelpers.prepareTransaction(session1);
        assertNoChanges(changeStreamCursor);

        // TODO SERVER-39036: Change writeConcern to majority. Prior to this ticket a majority write
        // will block on a prepared transaction. We should also be able to move the check for
        // document existence prior to the transaction commit with this change.
        // Perform a write at writeConcern w: local.
        assert.commandWorked(coll.insert({_id: "no-txn-doc-3"}, {writeConcern: {w: 1}}));

        //
        // Commit first transaction and confirm expected changes.
        //
        assert.commandWorked(PrepareHelpers.commitTransaction(session1, prepareTimestampTxn1));
        assertWriteVisibleWithCapture(
            changeStreamCursor, "insert", {_id: "no-txn-doc-3"}, changeList);
        assertWriteVisibleWithCapture(
            changeStreamCursor, "insert", {_id: "txn1-doc-1"}, changeList);
        assertWriteVisibleWithCapture(
            changeStreamCursor, "insert", {_id: "txn1-doc-2"}, changeList);
        assertWriteVisibleWithCapture(
            changeStreamCursor, "update", {_id: "txn1-doc-1"}, changeList);
        assertWriteVisibleWithCapture(
            changeStreamCursor, "update", {_id: "txn1-doc-2"}, changeList);
        assertWriteVisibleWithCapture(
            changeStreamCursor, "delete", {_id: "txn1-doc-2"}, changeList);
        assertNoChanges(changeStreamCursor);

        // Transition the second transaction to prepared. We skip capturing the prepare
        // timestamp it is not required for abortTransaction_forTesting().
        PrepareHelpers.prepareTransaction(session2);
        assertNoChanges(changeStreamCursor);

        // TODO SERVER-39036: Change writeConcern to majority. Prior to this ticket a majority write
        // will block on a prepared transaction. We should also be able to move the check for
        // document existence prior to the transaction abort with this change.
        // Perform a write at writeConcern w: local.
        assert.commandWorked(coll.insert({_id: "no-txn-doc-4"}, {writeConcern: {w: 1}}));

        //
        // Abort second transaction.
        //
        assert.commandWorked(session2.abortTransaction_forTesting());
        assertWriteVisibleWithCapture(
            changeStreamCursor, "insert", {_id: "no-txn-doc-4"}, changeList);
        assertNoChanges(changeStreamCursor);
        changeStreamCursor.close();

        // Test that change stream resume returns the expected set of documents at each point
        // captured by this test.
        for (let i = 0; i < changeList.length; ++i) {
            const resumeCursor = coll.watch([], {startAfter: changeList[i]._id});

            for (let x = (i + 1); x < changeList.length; ++x) {
                const expectedChangeDoc = changeList[x];
                assertWriteVisible(
                    resumeCursor, expectedChangeDoc.operationType, expectedChangeDoc.documentKey);
            }

            assertNoChanges(resumeCursor);
            resumeCursor.close();
        }

        //
        // Prepare and commit the third transaction and confirm that there are no visible changes.
        //
        let prepareTimestampTxn3;
        prepareTimestampTxn3 = PrepareHelpers.prepareTransaction(session3);
        assertNoChanges(changeStreamCursor);

        assert.commandWorked(PrepareHelpers.commitTransaction(session3, prepareTimestampTxn3));
        assertNoChanges(changeStreamCursor);

        assert.commandWorked(db.dropDatabase());
    }

    const rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();

    runTest(rst.getPrimary());

    rst.stopSet();
})();