summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/timeseries_delete_one_transaction.js
blob: 69cf1524b61234e1bdcb3f093e6dfdbde6d2deb8 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
 * Tests the deleteOne command on time-series collections in multi-document transactions.
 */
(function() {
"use strict";

load("jstests/core/timeseries/libs/timeseries.js");
load("jstests/libs/fail_point_util.js");
load('jstests/libs/parallel_shell_helpers.js');

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

const metaFieldName = "mm";
const timeFieldName = "tt";
const collectionNamePrefix = "test_coll_";
let collectionCounter = 0;

const testDB = rst.getPrimary().getDB(jsTestName());
let testColl = testDB[collectionNamePrefix + collectionCounter];
assert.commandWorked(testDB.dropDatabase());

const docsPerMetaField = 3;
const initializeData = function() {
    testColl = testDB[collectionNamePrefix + ++collectionCounter];
    assert.commandWorked(testDB.createCollection(
        testColl.getName(), {timeseries: {timeField: timeFieldName, metaField: metaFieldName}}));

    let docs = [];
    for (let i = 0; i < docsPerMetaField; ++i) {
        docs.push({_id: i, [timeFieldName]: new Date(), [metaFieldName]: 0});
        docs.push({_id: i, [timeFieldName]: new Date(), [metaFieldName]: 1});
        docs.push({_id: i, [timeFieldName]: new Date(), [metaFieldName]: 2});
    }

    // Insert test documents.
    assert.commandWorked(testColl.insertMany(docs));
    printjson("Printing docs: " + tojson(testColl.find({}).toArray()));
};

// 1. Delete one document from the collection in a transaction.
(function basicDeleteOne() {
    jsTestLog("Running 'basicDeleteOne'");
    initializeData();

    const session = testDB.getMongo().startSession();
    const sessionColl = session.getDatabase(jsTestName()).getCollection(testColl.getName());

    session.startTransaction();
    assert.commandWorked(sessionColl.deleteOne({_id: 0, [metaFieldName]: 0}));
    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // Expect one deleted document with meta: 0.
    assert.eq(testColl.find({[metaFieldName]: 0}).toArray().length, docsPerMetaField - 1);
})();

// 2. deleteOne should not have visible changes when the transaction is aborted.
(function deleteOneTransactionAborts() {
    jsTestLog("Running 'deleteOneTransactionAborts'");
    initializeData();

    const session = testDB.getMongo().startSession();
    const sessionColl = session.getDatabase(jsTestName()).getCollection(testColl.getName());

    session.startTransaction();
    assert.commandWorked(sessionColl.deleteOne({_id: 0, [metaFieldName]: 1}));
    assert.commandWorked(session.abortTransaction_forTesting());
    session.endSession();

    // The transaction was aborted so no documents should have been deleted.
    assert.eq(testColl.find({[metaFieldName]: 1}).toArray().length, docsPerMetaField);
})();

// 3. Run a few deleteOnes in a single transaction.
(function multipleDeleteOne() {
    jsTestLog("Running 'multipleDeleteOne'");
    initializeData();

    const session = testDB.getMongo().startSession();
    const sessionColl = session.getDatabase(jsTestName()).getCollection(testColl.getName());
    session.startTransaction();

    for (let i = 0; i < docsPerMetaField; ++i) {
        assert.commandWorked(sessionColl.deleteOne({_id: i, [metaFieldName]: 0}));
    }

    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // Expect all documents with {meta: 0} to be deleted.
    assert.eq(testColl.find({[metaFieldName]: 0}).toArray().length, 0);
})();

// 4. Tests performing deleteOnes in and out of a transaction on abort.
(function mixedDeleteOneAbortTxn() {
    jsTestLog("Running 'mixedDeleteOneAbortTxn'");
    initializeData();

    const session = testDB.getMongo().startSession();
    const sessionColl = session.getDatabase(jsTestName()).getCollection(testColl.getName());
    session.startTransaction();

    // Delete all documents for meta values 0, 1.
    for (let i = 0; i < docsPerMetaField; ++i) {
        assert.commandWorked(sessionColl.deleteOne({_id: i, [metaFieldName]: 0}));
        assert.commandWorked(sessionColl.deleteOne({_id: i, [metaFieldName]: 1}));
    }

    // Outside of the session and transaction, perform a deleteOne.
    const docFilterNoTxn = {_id: 0, [metaFieldName]: 2};
    assert.commandWorked(testColl.deleteOne(docFilterNoTxn));

    assert.commandWorked(session.abortTransaction_forTesting());
    session.endSession();

    // The aborted transaction should not have deleted any documents.
    assert.eq(testColl.find({[metaFieldName]: 0}).toArray().length, docsPerMetaField);
    assert.eq(testColl.find({[metaFieldName]: 1}).toArray().length, docsPerMetaField);

    // The delete outside of the transaction should have succeeded.
    assert.eq(testColl.find(docFilterNoTxn).toArray().length, 0);
})();

// 5. Tests performing deleteOnes in and out of a transaction on commit.
(function mixedDeleteOneCommitTxn() {
    jsTestLog("Running 'mixedDeleteOneCommitTxn'");
    initializeData();

    const session = testDB.getMongo().startSession();
    const sessionColl = session.getDatabase(jsTestName()).getCollection(testColl.getName());
    session.startTransaction();

    for (let i = 0; i < docsPerMetaField; ++i) {
        // Within the transaction.
        assert.commandWorked(sessionColl.deleteOne({_id: i, [metaFieldName]: 0}));
        assert.commandWorked(sessionColl.deleteOne({_id: i, [metaFieldName]: 1}));

        // Outside of the session and transaction, perform deleteOne.
        assert.commandWorked(testColl.deleteOne({_id: i, [metaFieldName]: 2}));
    }

    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // Expect all documents to have been deleted.
    assert.eq(testColl.find({}).toArray().length, 0);
})();

// 6. Tests a race to delete the same document in and out of a transaction.
(function raceToDeleteOne() {
    jsTestLog("Running 'raceToDeleteOne'");
    initializeData();

    const session = testDB.getMongo().startSession();
    const sessionColl = session.getDatabase(jsTestName()).getCollection(testColl.getName());
    session.startTransaction();

    // Within the transaction, perform deleteOne.
    const deleteFilter = {_id: 1, [metaFieldName]: 0};
    assert.commandWorked(sessionColl.deleteOne(deleteFilter));

    // Note: there is a change the parallel shell runs after the transcation is committed and that
    // is fine as both interleavings should succeed.
    const awaitTestDelete = startParallelShell(
        funWithArgs(function(dbName, collName, filter) {
            const testDB = db.getSiblingDB(dbName);
            const coll = testDB.getCollection(collName);

            // Outside of the session and transaction, perform deleteOne.
            assert.commandWorked(coll.deleteOne(filter));
        }, testDB.getName(), testColl.getName(), deleteFilter), testDB.getMongo().port);

    assert.commandWorked(session.commitTransaction_forTesting());
    assert.eq(testColl.find(deleteFilter).toArray().length, 0);
    session.endSession();

    // Allow non-transactional deleteOne to finish.
    awaitTestDelete();
    assert.eq(testColl.find(deleteFilter).toArray().length, 0);
})();

// 7. Tests a transactional deleteOne on a document which gets inserted after the transaction
// starts.
(function deleteOneAndInsertBeforeCommit() {
    jsTestLog("Running 'deleteOneAndInsertBeforeCommit'");
    initializeData();

    const session = testDB.getMongo().startSession();
    const sessionColl = session.getDatabase(jsTestName()).getCollection(testColl.getName());
    const newDoc = {_id: 101, [timeFieldName]: new Date(), [metaFieldName]: 101};

    session.startTransaction();
    // Ensure the document does not exist within the snapshot of the newly started transaction.
    assert.eq(sessionColl.find(newDoc).toArray().length, 0);

    // Outside of the session and transaction, insert document.
    assert.commandWorked(testColl.insert(newDoc));

    // Double check the document is still not visible from within the transaction.
    assert.eq(sessionColl.find(newDoc).toArray().length, 0);

    // Within the transaction, perform deleteOne.
    assert.commandWorked(sessionColl.deleteOne(newDoc));
    assert.eq(sessionColl.find(newDoc).toArray().length, 0);

    assert.commandWorked(session.commitTransaction_forTesting());
    session.endSession();

    // The newly inserted document should be present even though the transaction commits after the
    // insert.
    assert.eq(testColl.find(newDoc).toArray().length, 1);
})();

// 8. Tests two side-by-side transactional deleteOnes on the same document.
(function deleteOneInTwoTransactions() {
    jsTestLog("Running 'deleteOneInTwoTransactions'");
    initializeData();

    const sessionA = testDB.getMongo().startSession();
    const sessionB = testDB.getMongo().startSession();
    const collA = sessionA.getDatabase(jsTestName()).getCollection(testColl.getName());
    const collB = sessionB.getDatabase(jsTestName()).getCollection(testColl.getName());

    const docToDelete = {_id: 1, [metaFieldName]: 1};

    // Start transactions on different sessions.
    sessionA.startTransaction({readConcern: {level: "snapshot"}});
    sessionB.startTransaction({readConcern: {level: "snapshot"}});

    // Ensure the document exists in the snapshot of both transactions.
    assert.eq(collA.find(docToDelete).toArray().length, 1);
    assert.eq(collB.find(docToDelete).toArray().length, 1);

    // Perform deleteOne on transaction A.
    assert.commandWorked(collA.deleteOne(docToDelete));

    const deleteCommand = {
        delete: collB.getName(),
        deletes: [{
            q: docToDelete,
            limit: 1,
        }]
    };

    // We expect the deleteOne on transaction B to fail, causing the transaction to abort.
    // Sidenote: avoiding the deleteOne method from 'crud_api.js' because it throws.
    assert.commandFailedWithCode(collB.runCommand(deleteCommand), ErrorCodes.WriteConflict);
    assert.commandFailedWithCode(sessionB.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
    sessionB.endSession();

    // Ensure the document does not exist in the snapshot of transaction A.
    assert.eq(collA.find(docToDelete).toArray().length, 0);
    // Since transaction A has not committed yet, the document should still be present outside of
    // the transaction.
    assert.eq(testColl.find(docToDelete).toArray().length, 1);

    // Ensure the document has been successfully deleted after transaction A commits.
    assert.commandWorked(sessionA.commitTransaction_forTesting());
    assert.eq(testColl.find(docToDelete).toArray().length, 0);

    sessionA.endSession();
})();

rst.stopSet();
})();