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
|
/**
* Tests that writes which complete before stepdown correctly report their errors after the
* stepdown.
*/
(function() {
"use strict";
load("jstests/libs/check_log.js");
const rst = new ReplSetTest({nodes: [{}, {rsConfig: {priority: 0}}]});
rst.startSet();
rst.initiate();
const primary = rst.getPrimary();
const primaryAdmin = primary.getDB("admin");
// We need a separate connection to avoid interference with the ReplSetTestMechanism.
const primaryDataConn = new Mongo(primary.host);
const primaryDb = primaryDataConn.getDB("test");
const collname = "last_error_reported_after_stepdown";
const coll = primaryDb[collname];
// Never retry on network error, because this test needs to detect the network error.
TestData.skipRetryOnNetworkError = true;
// This is specifically testing unacknowledged legacy writes.
primaryDataConn.forceWriteMode('legacy');
assert.commandWorked(
coll.insert([{_id: 'deleteme'}, {_id: 'updateme', nullfield: null}, {_id: 'findme'}],
{writeConcern: {w: 1}}));
rst.awaitReplication();
// Note that "operation" should always be on primaryDataConn, so the stepdown doesn't clear
// the last error.
function runStepDownTest({description, logMsg, operation, errorCode, nDocs}) {
jsTestLog(`Trying ${description} on the primary, then stepping down`);
// We need to make sure the command is complete before stepping down.
assert.commandWorked(
primaryAdmin.adminCommand({setParameter: 1, logComponentVerbosity: {command: 1}}));
operation();
// Wait for the operation to complete.
checkLog.contains(primary, logMsg + ' appName: "MongoDB Shell"');
assert.commandWorked(
primaryAdmin.adminCommand({setParameter: 1, logComponentVerbosity: {command: 0}}));
assert.commandWorked(primaryAdmin.adminCommand({replSetStepDown: 60, force: true}));
rst.waitForState(primary, ReplSetTest.State.SECONDARY);
var lastError = assert.commandWorked(primaryDb.runCommand({getLastError: 1}));
if (typeof(errorCode) == "number")
assert.eq(lastError.code,
errorCode,
"Expected error code " + errorCode + ", got lastError of " +
JSON.stringify(lastError));
else {
assert(!lastError.err,
"Expected no error, got lastError of " + JSON.stringify(lastError));
}
if (typeof(nDocs) == "number") {
assert.eq(lastError.n, nDocs, "Wrong number of documents modified or updated");
}
// Allow the primary to be re-elected, and wait for it.
assert.commandWorked(primaryAdmin.adminCommand({replSetFreeze: 0}));
rst.getPrimary();
}
// Tests which should have no errors.
// Clear log messages to avoid picking up the log of the insertion of the 'deleteme'
// document.
assert.commandWorked(primaryAdmin.adminCommand({clearLog: 'global'}));
runStepDownTest({
description: "insert",
logMsg: "insert " + coll.getFullName(),
operation: () => coll.insert({_id: 0})
});
runStepDownTest({
description: "update",
logMsg: "update ",
operation: () => coll.update({_id: 'updateme'}, {'$inc': {x: 1}}),
nDocs: 1
});
runStepDownTest({
description: "remove",
logMsg: "remove ",
operation: () => coll.remove({_id: 'deleteme'}),
nDocs: 1
});
// Tests which should have errors.
// We repeat log messages from tests above, so clear the log first.
assert.commandWorked(primaryAdmin.adminCommand({clearLog: 'global'}));
runStepDownTest({
description: "insert with error",
logMsg: "insert " + coll.getFullName(),
operation: () => coll.insert({_id: 0}),
errorCode: ErrorCodes.DuplicateKey
});
runStepDownTest({
description: "update with error",
logMsg: "update ",
operation: () => coll.update({_id: 'updateme'}, {'$inc': {nullfield: 1}}),
errorCode: ErrorCodes.TypeMismatch,
nDocs: 0
});
runStepDownTest({
description: "remove with error",
logMsg: "remove ",
operation: () => coll.remove({'$nonsense': {x: 1}}),
errorCode: ErrorCodes.BadValue,
nDocs: 0
});
rst.stopSet();
})();
|