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
|
/**
* Confirms inclusion of a 'transaction' object containing lsid and txnNumber in
* currentOp() for a prepared transaction and an active non-prepared transaction.
* @tags: [uses_transactions, uses_prepare_transaction]
*/
(function() {
'use strict';
load("jstests/libs/parallel_shell_helpers.js");
function transactionFn(isPrepared) {
const collName = 'currentop_active_transaction';
const session = db.getMongo().startSession({causalConsistency: false});
const sessionDB = session.getDatabase('test');
session.startTransaction({readConcern: {level: 'snapshot'}});
sessionDB[collName].update({}, {x: 2});
if (isPrepared) {
// Load the prepare helpers to be called in the parallel shell.
load('jstests/core/txns/libs/prepare_helpers.js');
const prepareTimestamp = PrepareHelpers.prepareTransaction(session);
PrepareHelpers.commitTransaction(session, prepareTimestamp);
} else {
assert.commandWorked(session.commitTransaction_forTesting());
}
}
function checkCurrentOpFields(currentOp,
isPrepared,
operationTime,
timeBeforeTransactionStarts,
timeAfterTransactionStarts,
timeBeforeCurrentOp) {
const transactionDocument = currentOp[0].transaction;
assert.eq(transactionDocument.parameters.autocommit,
false,
"Expected 'autocommit' to be false but got " +
transactionDocument.parameters.autocommit +
" instead: " + tojson(transactionDocument));
assert.docEq(transactionDocument.parameters.readConcern,
{level: 'snapshot'},
"Expected 'readConcern' to be level: snapshot but got " +
tojson(transactionDocument.parameters.readConcern) +
" instead: " + tojson(transactionDocument));
assert.gte(transactionDocument.readTimestamp,
operationTime,
"Expected 'readTimestamp' to be at least " + operationTime + " but got " +
transactionDocument.readTimestamp + " instead: " + tojson(transactionDocument));
assert.gte(ISODate(transactionDocument.startWallClockTime),
timeBeforeTransactionStarts,
"Expected 'startWallClockTime' to be at least" + timeBeforeTransactionStarts +
" but got " + transactionDocument.startWallClockTime +
" instead: " + tojson(transactionDocument));
const expectedTimeOpen = (timeBeforeCurrentOp - timeAfterTransactionStarts) * 1000;
assert.gt(transactionDocument.timeOpenMicros,
expectedTimeOpen,
"Expected 'timeOpenMicros' to be at least" + expectedTimeOpen + " but got " +
transactionDocument.timeOpenMicros + " instead: " + tojson(transactionDocument));
assert.gte(transactionDocument.timeActiveMicros,
0,
"Expected 'timeActiveMicros' to be at least 0: " + tojson(transactionDocument));
assert.gte(transactionDocument.timeInactiveMicros,
0,
"Expected 'timeInactiveMicros' to be at least 0: " + tojson(transactionDocument));
const actualExpiryTime = ISODate(transactionDocument.expiryTime).getTime();
const expectedExpiryTime =
ISODate(transactionDocument.startWallClockTime).getTime() + transactionLifeTime * 1000;
assert.eq(expectedExpiryTime,
actualExpiryTime,
"Expected 'expiryTime' to be " + expectedExpiryTime + " but got " + actualExpiryTime +
" instead: " + tojson(transactionDocument));
if (isPrepared) {
assert.gte(
transactionDocument.timePreparedMicros,
0,
"Expected 'timePreparedMicros' to be at least 0: " + tojson(transactionDocument));
}
}
const rst = new ReplSetTest({nodes: 1});
rst.startSet();
rst.initiate();
const collName = 'currentop_active_transaction';
const testDB = rst.getPrimary().getDB('test');
const adminDB = rst.getPrimary().getDB('admin');
testDB.runCommand({drop: collName, writeConcern: {w: "majority"}});
assert.commandWorked(testDB[collName].insert({x: 1}, {writeConcern: {w: "majority"}}));
// Run an operation prior to starting the transaction and save its operation time. We will use
// this later to assert that our subsequent transaction's readTimestamp is greater than or equal
// to this operation time.
let res = assert.commandWorked(testDB.runCommand({insert: collName, documents: [{x: 1}]}));
// Set and save the transaction's lifetime. We will use this later to assert that our
// transaction's expiry time is equal to its start time + lifetime.
const transactionLifeTime = 10;
assert.commandWorked(
testDB.adminCommand({setParameter: 1, transactionLifetimeLimitSeconds: transactionLifeTime}));
// This will make the transaction hang.
assert.commandWorked(testDB.adminCommand(
{configureFailPoint: 'hangAfterSettingPrepareStartTime', mode: 'alwaysOn'}));
let timeBeforeTransactionStarts = new ISODate();
let isPrepared = true;
const joinPreparedTransaction =
startParallelShell(funWithArgs(transactionFn, isPrepared), rst.ports[0]);
const prepareTransactionFilter = {
active: true,
'lsid': {$exists: true},
'transaction.parameters.txnNumber': {$eq: 0},
'transaction.parameters.autocommit': {$eq: false},
'transaction.timePreparedMicros': {$exists: true}
};
// Keep running currentOp() until we see the transaction subdocument.
assert.soon(function() {
return 1 ===
adminDB.aggregate([{$currentOp: {}}, {$match: prepareTransactionFilter}]).itcount();
});
let timeAfterTransactionStarts = new ISODate();
// Sleep here to allow some time between timeAfterTransactionStarts and timeBeforeCurrentOp to
// elapse.
sleep(100);
let timeBeforeCurrentOp = new ISODate();
// Check that the currentOp's transaction subdocument's fields align with our expectations.
let currentOp = adminDB.aggregate([{$currentOp: {}}, {$match: prepareTransactionFilter}]).toArray();
checkCurrentOpFields(currentOp,
isPrepared,
res.operationTime,
timeBeforeTransactionStarts,
timeAfterTransactionStarts,
timeBeforeCurrentOp);
// Now the transaction can proceed.
assert.commandWorked(
testDB.adminCommand({configureFailPoint: 'hangAfterSettingPrepareStartTime', mode: 'off'}));
joinPreparedTransaction();
// Conduct the same test but with a non-prepared transaction.
res = assert.commandWorked(testDB.runCommand({insert: collName, documents: [{x: 1}]}));
// This will make the transaction hang.
assert.commandWorked(
testDB.adminCommand({configureFailPoint: 'hangDuringBatchUpdate', mode: 'alwaysOn'}));
timeBeforeTransactionStarts = new ISODate();
isPrepared = false;
const joinTransaction = startParallelShell(funWithArgs(transactionFn, isPrepared), rst.ports[0]);
const transactionFilter = {
active: true,
'lsid': {$exists: true},
'transaction.parameters.txnNumber': {$eq: 0},
'transaction.parameters.autocommit': {$eq: false},
'transaction.timePreparedMicros': {$exists: false}
};
// Keep running currentOp() until we see the transaction subdocument.
assert.soon(function() {
return 1 === adminDB.aggregate([{$currentOp: {}}, {$match: transactionFilter}]).itcount();
});
timeAfterTransactionStarts = new ISODate();
// Sleep here to allow some time between timeAfterTransactionStarts and timeBeforeCurrentOp to
// elapse.
sleep(100);
timeBeforeCurrentOp = new ISODate();
// Check that the currentOp's transaction subdocument's fields align with our expectations.
currentOp = adminDB.aggregate([{$currentOp: {}}, {$match: transactionFilter}]).toArray();
checkCurrentOpFields(currentOp,
isPrepared,
res.operationTime,
timeBeforeTransactionStarts,
timeAfterTransactionStarts,
timeBeforeCurrentOp);
// Now the transaction can proceed.
assert.commandWorked(
testDB.adminCommand({configureFailPoint: 'hangDuringBatchUpdate', mode: 'off'}));
joinTransaction();
rst.stopSet();
})();
|