summaryrefslogtreecommitdiff
path: root/jstests/sharding/transient_txn_error_labels.js
blob: 36715f301f7c96a486b06c03a754a48ac3d99330 (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
269
270
/**
 * Test TransientTransactionErrors error label in transactions.
 * @tags: [
 *   uses_transactions,
 * ]
 */

(function() {
"use strict";

load("jstests/libs/write_concern_util.js");
load("jstests/libs/parallelTester.js");  // For Thread.

const dbName = "test";
const collName = "no_error_labels_outside_txn";

// We are testing coordinateCommitTransaction, which requires the nodes to be started with
// --shardsvr.
const st = new ShardingTest(
    {config: 1, mongos: 1, shards: {rs0: {nodes: [{}, {rsConfig: {priority: 0}}]}}});
const primary = st.rs0.getPrimary();
const secondary = st.rs0.getSecondary();

const testDB = primary.getDB(dbName);
const adminDB = testDB.getSiblingDB("admin");
const testColl = testDB.getCollection(collName);

const sessionOptions = {
    causalConsistency: false
};
let session = primary.startSession(sessionOptions);
let sessionDb = session.getDatabase(dbName);
let sessionColl = sessionDb.getCollection(collName);
let secondarySession = secondary.startSession(sessionOptions);
let secondarySessionDb = secondarySession.getDatabase(dbName);

assert.commandWorked(testDB.createCollection(collName, {writeConcern: {w: "majority"}}));

jsTest.log("Insert inside a transaction on secondary should fail but return error labels");
let txnNumber = 0;
let res = secondarySessionDb.runCommand({
    insert: collName,
    documents: [{_id: "insert-1"}],
    readConcern: {level: "snapshot"},
    txnNumber: NumberLong(txnNumber),
    startTransaction: true,
    autocommit: false
});
assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
assert.eq(res.errorLabels, ["TransientTransactionError"]);

jsTest.log("failCommand with errorLabels but without errorCode or writeConcernError should not " +
           "interfere with server's error labels attaching");
txnNumber++;
// This failCommand should have no effect.
assert.commandWorked(secondary.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorLabels: ["foo"], failCommands: ["insert"]}
}));
res = secondarySessionDb.runCommand({
    insert: collName,
    documents: [{_id: "insert-1"}],
    readConcern: {level: "snapshot"},
    txnNumber: NumberLong(txnNumber),
    startTransaction: true,
    autocommit: false
});
assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
// Server should continue to return TransientTransactionError label.
assert.eq(res.errorLabels, ["TransientTransactionError"]);
assert.commandWorked(secondary.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

jsTest.log("Insert as a retryable write on secondary should fail with retryable error labels");
txnNumber++;
// Insert as a retryable write.
res = secondarySessionDb.runCommand(
    {insert: collName, documents: [{_id: "insert-1"}], txnNumber: NumberLong(txnNumber)});

assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
assert.eq(res.errorLabels, ["RetryableWriteError"], res);
secondarySession.endSession();

jsTest.log("failCommand should be able to return errors with TransientTransactionError");
assert.commandWorked(testDB.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: ErrorCodes.WriteConflict, failCommands: ["insert"]}
}));
session.startTransaction();
jsTest.log("WriteCommandError should have error labels inside transactions.");
res = sessionColl.insert({_id: "write-fail-point"});
assert.commandFailedWithCode(res, ErrorCodes.WriteConflict);
assert(res instanceof WriteCommandError);
assert.eq(res.errorLabels, ["TransientTransactionError"]);
res = testColl.insert({_id: "write-fail-point-outside-txn"});
jsTest.log("WriteCommandError should not have error labels outside transactions.");
// WriteConflict will not be returned outside transactions in real cases, but it's fine for
// testing purpose.
assert.commandFailedWithCode(res, ErrorCodes.WriteConflict);
assert(res instanceof WriteCommandError);
assert(!res.hasOwnProperty("errorLabels"));
assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));
assert.commandFailedWithCode(session.abortTransaction_forTesting(), ErrorCodes.NoSuchTransaction);

jsTest.log("WriteConflict returned by commitTransaction command is TransientTransactionError");
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "commitTransaction-fail-point"}));
assert.commandWorked(testDB.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: ErrorCodes.WriteConflict, failCommands: ["commitTransaction"]}
}));
res = session.commitTransaction_forTesting();
assert.commandFailedWithCode(res, ErrorCodes.WriteConflict);
assert.eq(res.errorLabels, ["TransientTransactionError"]);
assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

jsTest.log("NotMaster returned by commitTransaction command is not TransientTransactionError but" +
           " RetryableWriteError");
// commitTransaction will attempt to perform a noop write in response to a NoSuchTransaction
// error and non-empty writeConcern. This will throw NotMaster.
res = secondarySessionDb.adminCommand({
    commitTransaction: 1,
    txnNumber: NumberLong(secondarySession.getTxnNumber_forTesting() + 1),
    autocommit: false,
    writeConcern: {w: "majority"}
});
assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
assert.eq(res.errorLabels, ["RetryableWriteError"], res);

jsTest.log(
    "NotMaster returned by coordinateCommitTransaction command is not TransientTransactionError" +
    " but RetryableWriteError");
// coordinateCommitTransaction will attempt to perform a noop write in response to a
// NoSuchTransaction error and non-empty writeConcern. This will throw NotMaster.
res = secondarySessionDb.adminCommand({
    coordinateCommitTransaction: 1,
    participants: [],
    txnNumber: NumberLong(secondarySession.getTxnNumber_forTesting() + 1),
    autocommit: false,
    writeConcern: {w: "majority"}
});
assert.commandFailedWithCode(res, ErrorCodes.NotMaster);
assert.eq(res.errorLabels, ["RetryableWriteError"], res);

jsTest.log("ShutdownInProgress returned by write commands is TransientTransactionError");
session.startTransaction();
assert.commandWorked(testDB.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: ErrorCodes.ShutdownInProgress, failCommands: ["insert"]}
}));
res = sessionColl.insert({_id: "commitTransaction-fail-point"});
assert.commandFailedWithCode(res, ErrorCodes.ShutdownInProgress);
assert(res instanceof WriteCommandError);
assert.eq(res.errorLabels, ["TransientTransactionError"]);
assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));
assert.commandFailedWithCode(session.abortTransaction_forTesting(), ErrorCodes.NoSuchTransaction);

jsTest.log(
    "ShutdownInProgress returned by commitTransaction command is not TransientTransactionError" +
    " but RetryableWriteError");
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "commitTransaction-fail-point"}));
assert.commandWorked(testDB.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: ErrorCodes.ShutdownInProgress, failCommands: ["commitTransaction"]}
}));
res = session.commitTransaction_forTesting();
assert.commandFailedWithCode(res, ErrorCodes.ShutdownInProgress);
assert.eq(res.errorLabels, ["RetryableWriteError"], res);
assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

jsTest.log("ShutdownInProgress returned by coordinateCommitTransaction command is not" +
           " TransientTransactionError but RetryableWriteError");
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "coordinateCommitTransaction-fail-point"}));
assert.commandWorked(testDB.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: ErrorCodes.ShutdownInProgress, failCommands: ["coordinateCommitTransaction"]}
}));
res = sessionDb.adminCommand({
    coordinateCommitTransaction: 1,
    participants: [],
    txnNumber: NumberLong(session.getTxnNumber_forTesting()),
    autocommit: false
});
assert.commandFailedWithCode(res, ErrorCodes.ShutdownInProgress);
assert.eq(res.errorLabels, ["RetryableWriteError"], res);
assert.commandWorked(session.abortTransaction_forTesting());
assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

jsTest.log("LockTimeout should be TransientTransactionError");
// Start a transaction to hold the DBLock in IX mode so that drop will be blocked.
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "lock-timeout-1"}));
function dropCmdFunc(primaryHost, dbName, collName) {
    const primary = new Mongo(primaryHost);
    return primary.getDB(dbName).runCommand({drop: collName, writeConcern: {w: "majority"}});
}
const thread = new Thread(dropCmdFunc, primary.host, dbName, collName);
thread.start();
// Wait for the drop to have a pending MODE_X lock on the database.
assert.soon(
    function() {
        return adminDB
                   .aggregate([
                       {$currentOp: {}},
                       {$match: {"command.drop": collName, waitingForLock: true}}
                   ])
                   .itcount() === 1;
    },
    function() {
        return "Failed to find drop in currentOp output: " +
            tojson(adminDB.aggregate([{$currentOp: {}}]).toArray());
    });
// Start another transaction in a new session, which cannot acquire the database lock in time.
let sessionOther = primary.startSession(sessionOptions);
sessionOther.startTransaction();
res = sessionOther.getDatabase(dbName).getCollection(collName).insert({_id: "lock-timeout-2"});
assert.commandFailedWithCode(res, ErrorCodes.LockTimeout);
assert(res instanceof WriteCommandError);
assert.eq(res.errorLabels, ["TransientTransactionError"]);
assert.commandFailedWithCode(sessionOther.abortTransaction_forTesting(),
                             ErrorCodes.NoSuchTransaction);
assert.commandWorked(session.abortTransaction_forTesting());
thread.join();
assert.commandWorked(thread.returnData());

// Re-create the collection for later test cases.
assert.commandWorked(testDB.createCollection(collName, {writeConcern: {w: "majority"}}));

jsTest.log("Network errors for in-progress statements should be transient");
session.startTransaction();
assert.commandWorked(testDB.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: ErrorCodes.HostUnreachable, failCommands: ["aggregate"]}
}));
res = sessionDb.runCommand({aggregate: collName, pipeline: [{$match: {}}], cursor: {}});
assert.commandFailedWithCode(res, ErrorCodes.HostUnreachable);
assert.eq(res.errorLabels, ["TransientTransactionError"]);
assert.commandFailedWithCode(session.abortTransaction_forTesting(), ErrorCodes.NoSuchTransaction);
assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

jsTest.log("Network errors for commit should not be transient but RetryableWriteError");
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "commitTransaction-network-error"}));
assert.commandWorked(testDB.adminCommand({
    configureFailPoint: "failCommand",
    mode: "alwaysOn",
    data: {errorCode: ErrorCodes.HostUnreachable, failCommands: ["commitTransaction"]}
}));
res = sessionDb.adminCommand({
    commitTransaction: 1,
    txnNumber: NumberLong(session.getTxnNumber_forTesting()),
    autocommit: false
});
assert.commandFailedWithCode(res, ErrorCodes.HostUnreachable);
assert.eq(res.errorLabels, ["RetryableWriteError"], res);
assert.commandWorked(session.abortTransaction_forTesting());
assert.commandWorked(testDB.adminCommand({configureFailPoint: "failCommand", mode: "off"}));

session.endSession();

st.stop();
}());