summaryrefslogtreecommitdiff
path: root/jstests/core/txns/prepare_conflict_aggregation_behavior.js
blob: 972aad211f3909033c1c01af2d56088c801eac7e (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
/**
 * Test the behavior of aggregation with prepare conflicts. Reads from an aggregate pipeline
 * should not block on prepare conflicts, but writing out to a collection as a part of an aggregate
 * pipeline should block on prepare conflicts.
 *
 * @tags: [uses_transactions, uses_prepare_transaction, assumes_superuser_permissions]
 */
(function() {
"use strict";
load("jstests/core/txns/libs/prepare_helpers.js");

const failureTimeout = 5 * 1000;  // 5 seconds.
const dbName = "test";
const collName = "prepare_conflict_aggregation_behavior";
const outCollName = collName + "_out";
const testDB = db.getSiblingDB(dbName);
const testColl = testDB.getCollection(collName);
const outColl = testDB.getCollection(outCollName);

testColl.drop({writeConcern: {w: "majority"}});
outColl.drop({writeConcern: {w: "majority"}});
assert.commandWorked(testDB.runCommand({create: collName, writeConcern: {w: "majority"}}));
assert.commandWorked(testDB.runCommand({create: outCollName, writeConcern: {w: "majority"}}));

const session = db.getMongo().startSession({causalConsistency: false});
const sessionDB = session.getDatabase(dbName);
const sessionColl = sessionDB.getCollection(collName);
const sessionOutColl = sessionDB.getCollection(outCollName);

assert.commandWorked(testColl.insert({_id: 1}));
assert.commandWorked(outColl.insert({_id: 0}));

session.startTransaction();
assert.commandWorked(sessionColl.update({_id: 1}, {a: 1}));
assert.commandWorked(sessionOutColl.update({_id: 0}, {a: 1}));
let prepareTimestamp = PrepareHelpers.prepareTransaction(session);

jsTestLog("Test that reads from an aggregation pipeline with $merge don't block on prepare" +
          " conflicts");
testColl.aggregate([
    {$addFields: {b: 1}},
    {$merge: {into: outCollName, whenMatched: "fail", whenNotMatched: "insert"}}
]);

// Make sure that we can see the inserts from the aggregation but not the updates from the
// prepared transaction.
assert.eq([{_id: 0}, {_id: 1, b: 1}], outColl.find().toArray());

assert.commandWorked(session.abortTransaction_forTesting());
session.startTransaction();
assert.commandWorked(sessionOutColl.update({_id: 1}, {_id: 1, a: 1}));
prepareTimestamp = PrepareHelpers.prepareTransaction(session);

jsTestLog("Test that writes from an aggregation pipeline block on prepare conflicts");
let pipeline = [
    {$addFields: {c: 1}},
    {$merge: {into: outCollName, whenMatched: "replace", whenNotMatched: "insert"}}
];
assert.commandFailedWithCode(testDB.runCommand({
    aggregate: collName,
    pipeline: pipeline,
    cursor: {},
    maxTimeMS: failureTimeout,
}),
                             ErrorCodes.MaxTimeMSExpired);

// Make sure that we can't see the update from the aggregation or the prepared transaction.
assert.eq([{_id: 0}, {_id: 1, b: 1}], outColl.find().toArray());

assert.commandWorked(PrepareHelpers.commitTransaction(session, prepareTimestamp));

// Make sure that the $merge pipeline works once the transaction is committed.
testColl.aggregate(pipeline);
assert.eq([{_id: 0}, {_id: 1, c: 1}], outColl.find().toArray());

// At the time of this writing, change streams can sometimes adjust the readConcern to
// 'majority' after receiving the command and thus need to wait for read concern again. When
// doing this, we assume that a change stream with a stage which performs writes is not allowed.
// Test that this is true.
pipeline = [{$changeStream: {}}, {$addFields: {d: 1}}, {$out: outCollName}];
assert.commandFailedWithCode(testDB.runCommand({
    aggregate: collName,
    pipeline: pipeline,
    cursor: {},
    maxTimeMS: failureTimeout,
}),
                             ErrorCodes.IllegalOperation);

session.endSession();
}());