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
|
// Tests that a transaction expires and is then aborted by the server. Uses the server parameter
// 'transactionLifetimeLimitSeconds' to lower the transaction lifetime for quicker transaction
// expiration.
//
// @tags: [uses_transactions]
(function() {
"use strict";
const testDBName = "testDB";
const testCollName = "abort_expired_transaction";
const ns = testDBName + "." + testCollName;
const testDB = db.getSiblingDB(testDBName);
const testColl = testDB[testCollName];
testColl.drop();
// Need the original 'transactionLifetimeLimitSeconds' value so that we can reset it back at the
// end of the test.
const res = assert.commandWorked(
db.adminCommand({getParameter: 1, transactionLifetimeLimitSeconds: 1}));
const originalTransactionLifetimeLimitSeconds = res.transactionLifetimeLimitSeconds;
try {
jsTest.log("Decrease transactionLifetimeLimitSeconds from " +
originalTransactionLifetimeLimitSeconds + " to 1 second.");
assert.commandWorked(
db.adminCommand({setParameter: 1, transactionLifetimeLimitSeconds: 1}));
jsTest.log("Create a collection '" + ns + "' outside of the transaction.");
assert.writeOK(testColl.insert({foo: "bar"}));
jsTest.log("Set up the session.");
const sessionOptions = {causalConsistency: false};
const session = db.getMongo().startSession(sessionOptions);
const sessionDb = session.getDatabase(testDBName);
let txnNumber = 0;
jsTest.log("Insert a document starting a transaction.");
assert.commandWorked(sessionDb.runCommand({
insert: testCollName,
documents: [{_id: "insert-1"}],
txnNumber: NumberLong(txnNumber),
startTransaction: true,
autocommit: false,
}));
// We can deterministically wait for the transaction to be aborted by waiting for currentOp
// to cease reporting the inactive transaction: the transaction should disappear from the
// currentOp results once aborted.
jsTest.log("Wait for the transaction to expire and be aborted.");
assert.soon(
function() {
const sessionFilter = {
active: false,
opid: {$exists: false},
desc: "inactive transaction", "lsid.id": session.getSessionId().id,
txnNumber: NumberLong(txnNumber),
};
const res = db.getSiblingDB("admin").aggregate(
[{$currentOp: {allUsers: true, idleSessions: true}}, {$match: sessionFilter}]);
return (res.itcount() == 0);
},
"currentOp reports that the idle transaction still exists, it has not been " +
"aborted as expected.");
jsTest.log(
"Attempt to do a write in the transaction, which should fail because the transaction " +
"was aborted");
assert.commandFailedWithCode(sessionDb.runCommand({
insert: testCollName,
documents: [{_id: "insert-2"}],
txnNumber: NumberLong(txnNumber),
autocommit: false,
}),
ErrorCodes.NoSuchTransaction);
session.endSession();
} finally {
// Must ensure that the transactionLifetimeLimitSeconds is reset so that it does not impact
// other tests in the suite.
assert.commandWorked(db.adminCommand({
setParameter: 1,
transactionLifetimeLimitSeconds: originalTransactionLifetimeLimitSeconds
}));
}
}());
|