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
|
/**
* Tests that a retryable write started on one primary can be continued on a different node after
* failover.
*/
(function() {
"use strict";
load("jstests/libs/retryable_writes_util.js");
if (!RetryableWritesUtil.storageEngineSupportsRetryableWrites(jsTest.options().storageEngine)) {
jsTestLog("Retryable writes are not supported, skipping test");
return;
}
function stepDownPrimary(replTest) {
let exception = assert.throws(function() {
let res = assert.commandWorked(
replTest.getPrimary().adminCommand({replSetStepDown: 10, force: true}));
print("replSetStepDown did not throw exception but returned: " + tojson(res));
});
assert(isNetworkError(exception),
"replSetStepDown did not disconnect client; failed with " + tojson(exception));
}
const replTest = new ReplSetTest({nodes: 3});
replTest.startSet();
replTest.initiate();
////////////////////////////////////////////////////////////////////////
// Test insert command
let insertCmd = {
insert: "foo",
documents: [{_id: 10}, {_id: 30}],
ordered: false,
lsid: {id: UUID()},
txnNumber: NumberLong(5)
};
// Run the command on the primary and wait for replication.
let primary = replTest.getPrimary();
let testDB = primary.getDB("test");
let result = assert.commandWorked(testDB.runCommand(insertCmd));
assert.eq(2, testDB.foo.find().itcount());
replTest.awaitReplication();
// Step down the primary and wait for a new one.
stepDownPrimary(replTest);
let newPrimary = replTest.getPrimary();
testDB = newPrimary.getDB("test");
let oplog = newPrimary.getDB("local").oplog.rs;
let insertOplogEntries = oplog.find({ns: "test.foo", op: "i"}).itcount();
// Retry the command on the secondary and verify it wasn't repeated.
let retryResult = assert.commandWorked(testDB.runCommand(insertCmd));
assert.eq(result.ok, retryResult.ok);
assert.eq(result.n, retryResult.n);
assert.eq(result.writeErrors, retryResult.writeErrors);
assert.eq(result.writeConcernErrors, retryResult.writeConcernErrors);
assert.eq(2, testDB.foo.find().itcount());
assert.eq(insertOplogEntries, oplog.find({ns: "test.foo", op: "i"}).itcount());
////////////////////////////////////////////////////////////////////////
// Test update command
let updateCmd = {
update: "foo",
updates: [
{q: {_id: 10}, u: {$inc: {x: 1}}}, // in place
{q: {_id: 20}, u: {$inc: {y: 1}}, upsert: true},
{q: {_id: 30}, u: {z: 1}} // replacement
],
ordered: false,
lsid: {id: UUID()},
txnNumber: NumberLong(10),
};
primary = replTest.getPrimary();
testDB = primary.getDB("test");
// Run the command on the primary and wait for replication.
result = assert.commandWorked(testDB.runCommand(updateCmd));
assert.eq(3, testDB.foo.find().itcount());
replTest.awaitReplication();
// Step down the primary and wait for a new one.
stepDownPrimary(replTest);
newPrimary = replTest.getPrimary();
testDB = newPrimary.getDB("test");
oplog = newPrimary.getDB("local").oplog.rs;
let updateOplogEntries = oplog.find({ns: "test.foo", op: "u"}).itcount();
// Upserts are stored as inserts if they match no existing documents.
insertOplogEntries = oplog.find({ns: "test.foo", op: "i"}).itcount();
// Retry the command on the secondary and verify it wasn't repeated.
retryResult = assert.commandWorked(testDB.runCommand(updateCmd));
assert.eq(result.ok, retryResult.ok);
assert.eq(result.n, retryResult.n);
assert.eq(result.nModified, retryResult.nModified);
assert.eq(result.upserted, retryResult.upserted);
assert.eq(result.writeErrors, retryResult.writeErrors);
assert.eq(result.writeConcernErrors, retryResult.writeConcernErrors);
assert.eq(3, testDB.foo.find().itcount());
assert.eq({_id: 10, x: 1}, testDB.foo.findOne({_id: 10}));
assert.eq({_id: 20, y: 1}, testDB.foo.findOne({_id: 20}));
assert.eq({_id: 30, z: 1}, testDB.foo.findOne({_id: 30}));
assert.eq(updateOplogEntries, oplog.find({ns: "test.foo", op: "u"}).itcount());
assert.eq(insertOplogEntries, oplog.find({ns: "test.foo", op: "i"}).itcount());
////////////////////////////////////////////////////////////////////////
// Test delete command
let deleteCmd = {
delete: "foo",
deletes: [{q: {x: 1}, limit: 1}, {q: {y: 1}, limit: 1}],
ordered: false,
lsid: {id: UUID()},
txnNumber: NumberLong(15),
};
primary = replTest.getPrimary();
testDB = primary.getDB("test");
assert.writeOK(testDB.foo.insert({_id: 40, x: 1}));
assert.writeOK(testDB.foo.insert({_id: 50, y: 1}));
// Run the command on the primary and wait for replication.
result = assert.commandWorked(testDB.runCommand(deleteCmd));
assert.eq(1, testDB.foo.find({x: 1}).itcount());
assert.eq(1, testDB.foo.find({y: 1}).itcount());
replTest.awaitReplication();
// Step down the primary and wait for a new one.
stepDownPrimary(replTest);
newPrimary = replTest.getPrimary();
testDB = newPrimary.getDB("test");
oplog = newPrimary.getDB("local").oplog.rs;
let deleteOplogEntries = oplog.find({ns: "test.foo", op: "d"}).itcount();
// Retry the command on the secondary and verify it wasn't repeated.
retryResult = assert.commandWorked(testDB.runCommand(deleteCmd));
assert.eq(result.ok, retryResult.ok);
assert.eq(result.n, retryResult.n);
assert.eq(result.writeErrors, retryResult.writeErrors);
assert.eq(result.writeConcernErrors, retryResult.writeConcernErrors);
assert.eq(1, testDB.foo.find({x: 1}).itcount());
assert.eq(1, testDB.foo.find({y: 1}).itcount());
assert.eq(deleteOplogEntries, oplog.find({ns: "test.foo", op: "d"}).itcount());
replTest.stopSet();
})();
|