summaryrefslogtreecommitdiff
path: root/jstests/core/txns/transactions_profiling.js
blob: 548c800eeb4fd6b817e1dbb4535f51c9089cdcb0 (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
// Test profiling for commands in multi-document transactions.
// @tags: [uses_transactions]
(function() {
    "use strict";
    load("jstests/libs/profiler.js");  // For getLatestProfilerEntry.

    const dbName = "test";
    const collName = "transactions_profiling";
    const testDB = db.getSiblingDB(dbName);
    testDB[collName].drop({writeConcern: {w: "majority"}});

    testDB.setProfilingLevel(2);

    const sessionOptions = {causalConsistency: false};
    let session = testDB.getMongo().startSession(sessionOptions);
    let sessionDB = session.getDatabase(dbName);
    let sessionColl = sessionDB[collName];

    assert.commandWorked(sessionColl.insert({_id: "findAndModify-doc"}));
    assert.commandWorked(sessionColl.insert({_id: "delete-doc"}));
    assert.commandWorked(sessionColl.insert({_id: "multi-delete-doc-1"}));
    assert.commandWorked(sessionColl.insert({_id: "multi-delete-doc-2"}));
    assert.commandWorked(sessionColl.insert({_id: "multi-delete-doc-3"}));
    assert.commandWorked(sessionColl.insert({_id: "multi-delete-doc-4"}));
    assert.commandWorked(sessionColl.insert({_id: "read-doc"}));
    assert.commandWorked(sessionColl.insert({_id: "update-doc"}));
    assert.commandWorked(sessionColl.insert({_id: "multi-update-doc-1"}));
    assert.commandWorked(sessionColl.insert({_id: "multi-update-doc-2"}));
    assert.commandWorked(testDB.runCommand({
        createIndexes: collName,
        indexes: [{key: {haystack: "geoHaystack", a: 1}, name: "haystack_geo", bucketSize: 1}],
        writeConcern: {w: "majority"}
    }));

    jsTestLog("Test commands that can use shell helpers.");
    session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});

    jsTestLog("Test aggregate.");
    assert.eq(1, sessionColl.aggregate([{$match: {_id: "read-doc"}}]).itcount());
    let profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "command", tojson(profileObj));
    assert.eq(profileObj.command.aggregate, sessionColl.getName(), tojson(profileObj));
    assert.eq(profileObj.nreturned, 1, tojson(profileObj));

    jsTestLog("Test delete.");
    assert.commandWorked(sessionColl.deleteOne({_id: "delete-doc"}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "remove", tojson(profileObj));
    assert.eq(profileObj.ndeleted, 1, tojson(profileObj));

    jsTestLog("Test multi delete.");
    assert.commandWorked(
        sessionColl.deleteMany({_id: {$in: ["multi-delete-doc-1", "multi-delete-doc-2"]}}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "remove", tojson(profileObj));
    assert.eq(profileObj.ndeleted, 2, tojson(profileObj));

    jsTestLog("Test batch delete.");
    assert.commandWorked(sessionDB.runCommand({
        delete: collName,
        deletes: [
            {q: {_id: "multi-delete-doc-3"}, limit: 1},
            {q: {_id: "multi-delete-doc-4"}, limit: 1}
        ]
    }));
    // We see the profile entry from the second delete.
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "remove", tojson(profileObj));
    assert.eq(profileObj.ndeleted, 1, tojson(profileObj));

    jsTestLog("Test distinct.");
    assert.eq(["read-doc"], sessionColl.distinct("_id", {_id: "read-doc"}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "command", tojson(profileObj));
    assert.eq(profileObj.command.distinct, sessionColl.getName(), tojson(profileObj));

    jsTestLog("Test find.");
    assert.eq(1, sessionColl.find({_id: "read-doc"}).itcount());
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "query", tojson(profileObj));
    assert.eq(profileObj.nreturned, 1, tojson(profileObj));

    jsTestLog("Test findAndModify.");
    assert.eq({_id: "findAndModify-doc", updated: true},
              sessionColl.findAndModify(
                  {query: {_id: "findAndModify-doc"}, update: {$set: {updated: true}}, new: true}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.op, "command", tojson(profileObj));
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.command.findandmodify, sessionColl.getName(), tojson(profileObj));
    assert.eq(profileObj.nMatched, 1, tojson(profileObj));
    assert.eq(profileObj.nModified, 1, tojson(profileObj));

    jsTestLog("Test geoSearch.");
    assert.commandWorked(
        sessionDB.runCommand({geoSearch: collName, near: [0, 0], maxDistance: 1, search: {a: 1}}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.op, "command", tojson(profileObj));
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.command.geoSearch, sessionColl.getName(), tojson(profileObj));

    jsTestLog("Test getMore.");
    let res = assert.commandWorked(
        sessionDB.runCommand({find: collName, filter: {_id: "read-doc"}, batchSize: 0}));
    assert(res.hasOwnProperty("cursor"), tojson(res));
    assert(res.cursor.hasOwnProperty("id"), tojson(res));
    let cursorId = res.cursor.id;
    res = assert.commandWorked(sessionDB.runCommand({getMore: cursorId, collection: collName}));
    assert.eq([{_id: "read-doc"}], res.cursor.nextBatch, tojson(res));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "getmore", tojson(profileObj));
    assert.eq(profileObj.nreturned, 1, tojson(profileObj));

    jsTestLog("Test insert.");
    assert.commandWorked(sessionColl.insert({_id: "insert-doc"}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "insert", tojson(profileObj));
    assert.eq(profileObj.ninserted, 1, tojson(profileObj));

    jsTestLog("Test update.");
    assert.commandWorked(sessionColl.update({_id: "update-doc"}, {$set: {updated: true}}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "update", tojson(profileObj));
    assert.eq(profileObj.nMatched, 1, tojson(profileObj));
    assert.eq(profileObj.nModified, 1, tojson(profileObj));

    jsTestLog("Test multi update.");
    assert.commandWorked(sessionColl.updateMany(
        {_id: {$in: ["multi-update-doc-1", "multi-update-doc-2"]}}, {$set: {updated: true}}));
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "update", tojson(profileObj));
    assert.eq(profileObj.nMatched, 2, tojson(profileObj));
    assert.eq(profileObj.nModified, 2, tojson(profileObj));

    jsTestLog("Test batch update.");
    assert.commandWorked(sessionDB.runCommand({
        update: collName,
        updates: [
            {q: {_id: "multi-update-doc-1"}, u: {$set: {batch_updated: true}}},
            {q: {_id: "multi-update-doc-2"}, u: {$set: {batch_updated: true}}}
        ]
    }));
    // We see the profile entry from the second update.
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "update", tojson(profileObj));
    assert.eq(profileObj.nMatched, 1, tojson(profileObj));
    assert.eq(profileObj.nModified, 1, tojson(profileObj));

    jsTestLog("Committing transaction.");
    assert.commandWorked(session.commitTransaction_forTesting());

    jsTestLog("Test delete with a write conflict.");
    assert.commandWorked(sessionColl.insert({_id: "delete-doc"}, {writeConcern: {w: "majority"}}));
    session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});

    // Perform an operation in the transaction to establish the snapshot.
    assert.eq(1, sessionColl.find({_id: "read-doc"}).itcount());

    // Update the document outside of the transaction.
    assert.commandWorked(testDB[collName].update({_id: "delete-doc"}, {$set: {conflict: true}}));

    // Deleting the document in the transaction fails, but profiling is still successful.
    assert.throws(function() {
        sessionColl.deleteOne({_id: "delete-doc"});
    });
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "remove", tojson(profileObj));
    assert.eq(profileObj.errCode, ErrorCodes.WriteConflict, tojson(profileObj));
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    jsTestLog("Test findAndModify with a write conflict.");
    session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});

    // Perform an operation in the transaction to establish the snapshot.
    assert.eq(1, sessionColl.find({_id: "read-doc"}).itcount());

    // Update the document outside of the transaction.
    assert.commandWorked(
        testDB[collName].update({_id: "findAndModify-doc"}, {$set: {conflict: true}}));

    // Modifying the document in the transaction fails, but profiling is still successful.
    assert.throws(function() {
        sessionColl.findAndModify(
            {query: {_id: "findAndModify-doc"}, update: {$set: {conflict: false}}});
    });
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.op, "command", tojson(profileObj));
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.command.findandmodify, sessionColl.getName(), tojson(profileObj));
    assert.eq(profileObj.errCode, ErrorCodes.WriteConflict, tojson(profileObj));
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    jsTestLog("Test insert with a write conflict.");
    session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});

    // Perform an operation in the transaction to establish the snapshot.
    assert.eq(1, sessionColl.find({_id: "read-doc"}).itcount());

    // Insert a document outside of the transaction.
    assert.commandWorked(testDB[collName].insert({_id: "conflict-doc"}));

    // Inserting a document with the same _id in the transaction fails, but profiling is still
    // successful.
    assert.commandFailedWithCode(sessionColl.insert({_id: "conflict-doc"}),
                                 ErrorCodes.WriteConflict);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "insert", tojson(profileObj));
    assert.eq(profileObj.ninserted, 0, tojson(profileObj));
    assert.eq(profileObj.errCode, ErrorCodes.WriteConflict, tojson(profileObj));
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    jsTestLog("Test update with a write conflict.");
    session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});

    // Perform an operation in the transaction to establish the snapshot.
    assert.eq(1, sessionColl.find({_id: "read-doc"}).itcount());

    // Update the document outside of the transaction.
    assert.commandWorked(testDB[collName].update({_id: "update-doc"}, {$set: {conflict: true}}));

    // Updating the document in the transaction fails, but profiling is still successful.
    assert.commandFailedWithCode(sessionColl.update({_id: "update-doc"}, {$set: {conflict: false}}),
                                 ErrorCodes.WriteConflict);
    profileObj = getLatestProfilerEntry(testDB);
    assert.eq(profileObj.ns, sessionColl.getFullName(), tojson(profileObj));
    assert.eq(profileObj.op, "update", tojson(profileObj));
    assert.eq(profileObj.errCode, ErrorCodes.WriteConflict, tojson(profileObj));
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    session.endSession();
}());