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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/platform/basic.h"
#include <algorithm>
#include <functional>
#include <map>
#include <utility>
#include <vector>
#include "mongo/db/client.h"
#include "mongo/db/concurrency/lock_manager_test_help.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/field_parser.h"
#include "mongo/db/repl/oplog.h"
#include "mongo/db/repl/oplog_entry.h"
#include "mongo/db/repl/oplog_interface_local.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/repl/replication_coordinator_mock.h"
#include "mongo/db/service_context_d_test_fixture.h"
#include "mongo/platform/mutex.h"
#include "mongo/unittest/barrier.h"
#include "mongo/util/concurrency/thread_pool.h"
namespace mongo {
namespace repl {
namespace {
class OplogTest : public ServiceContextMongoDTest {
private:
void setUp() override;
};
void OplogTest::setUp() {
// Set up mongod.
ServiceContextMongoDTest::setUp();
auto service = getServiceContext();
auto opCtx = cc().makeOperationContext();
// Set up ReplicationCoordinator and create oplog.
ReplicationCoordinator::set(service, std::make_unique<ReplicationCoordinatorMock>(service));
setOplogCollectionName(service);
createOplog(opCtx.get());
// Ensure that we are primary.
auto replCoord = ReplicationCoordinator::get(opCtx.get());
ASSERT_OK(replCoord->setFollowerMode(MemberState::RS_PRIMARY));
}
/**
* Assert that oplog only has a single entry and return that oplog entry.
*/
OplogEntry _getSingleOplogEntry(OperationContext* opCtx) {
OplogInterfaceLocal oplogInterface(opCtx);
auto oplogIter = oplogInterface.makeIterator();
auto opEntry = unittest::assertGet(oplogIter->next());
ASSERT_EQUALS(ErrorCodes::CollectionIsEmpty, oplogIter->next().getStatus())
<< "Expected only 1 document in the oplog collection " << NamespaceString::kRsOplogNamespace
<< " but found more than 1 document instead";
return unittest::assertGet(OplogEntry::parse(opEntry.first));
}
TEST_F(OplogTest, LogOpReturnsOpTimeOnSuccessfulInsertIntoOplogCollection) {
auto opCtx = cc().makeOperationContext();
const NamespaceString nss("test.coll");
auto msgObj = BSON("msg"
<< "hello, world!");
// Write to the oplog.
OpTime opTime;
{
MutableOplogEntry oplogEntry;
oplogEntry.setOpType(repl::OpTypeEnum::kNoop);
oplogEntry.setNss(nss);
oplogEntry.setObject(msgObj);
oplogEntry.setWallClockTime(Date_t::now());
AutoGetDb autoDb(opCtx.get(), nss.db(), MODE_X);
WriteUnitOfWork wunit(opCtx.get());
opTime = logOp(opCtx.get(), &oplogEntry);
ASSERT_FALSE(opTime.isNull());
wunit.commit();
}
OplogEntry oplogEntry = _getSingleOplogEntry(opCtx.get());
// Ensure that msg fields were properly added to the oplog entry.
ASSERT_EQUALS(opTime, oplogEntry.getOpTime())
<< "OpTime returned from logOp() did not match that in the oplog entry written to the "
"oplog: "
<< oplogEntry.toBSON();
ASSERT(OpTypeEnum::kNoop == oplogEntry.getOpType())
<< "Expected 'n' op type but found '" << OpType_serializer(oplogEntry.getOpType())
<< "' instead: " << oplogEntry.toBSON();
ASSERT_BSONOBJ_EQ(msgObj, oplogEntry.getObject());
// Ensure that the msg optime returned is the same as the last optime in the ReplClientInfo.
ASSERT_EQUALS(ReplClientInfo::forClient(&cc()).getLastOp(), opTime);
}
/**
* Checks optime and namespace in oplog entry.
*/
void _checkOplogEntry(const OplogEntry& oplogEntry,
const OpTime& expectedOpTime,
const NamespaceString& expectedNss) {
ASSERT_EQUALS(expectedOpTime, oplogEntry.getOpTime()) << oplogEntry.toBSON();
ASSERT_EQUALS(expectedNss, oplogEntry.getNss()) << oplogEntry.toBSON();
}
void _checkOplogEntry(const OplogEntry& oplogEntry,
const std::pair<OpTime, NamespaceString>& expectedOpTimeAndNss) {
_checkOplogEntry(oplogEntry, expectedOpTimeAndNss.first, expectedOpTimeAndNss.second);
}
/**
* Test function that schedules two concurrent logOp() tasks using a thread pool.
* Checks the state of the oplog collection against the optimes returned from logOp().
* Before returning, updates 'opTimeNssMap' with the optimes from logOp() and 'oplogEntries' with
* the contents of the oplog collection.
*/
using OpTimeNamespaceStringMap = std::map<OpTime, NamespaceString>;
template <typename F>
void _testConcurrentLogOp(const F& makeTaskFunction,
OpTimeNamespaceStringMap* opTimeNssMap,
std::vector<OplogEntry>* oplogEntries,
std::size_t expectedNumOplogEntries) {
ASSERT_LESS_THAN_OR_EQUALS(expectedNumOplogEntries, 2U);
// Run 2 concurrent logOp() requests using the thread pool.
ThreadPool::Options options;
options.maxThreads = 2U;
options.onCreateThread = [](const std::string& name) { Client::initThread(name); };
ThreadPool pool(options);
pool.startup();
// Run 2 concurrent logOp() requests using the thread pool.
// Use a barrier with a thread count of 3 to ensure both logOp() tasks are complete before this
// test thread can proceed with shutting the thread pool down.
auto mtx = MONGO_MAKE_LATCH();
unittest::Barrier barrier(3U);
const NamespaceString nss1("test1.coll");
const NamespaceString nss2("test2.coll");
pool.schedule([&](auto status) mutable {
ASSERT_OK(status) << "Failed to schedule logOp() task for namespace " << nss1;
makeTaskFunction(nss1, &mtx, opTimeNssMap, &barrier)();
});
pool.schedule([&](auto status) mutable {
ASSERT_OK(status) << "Failed to schedule logOp() task for namespace " << nss2;
makeTaskFunction(nss2, &mtx, opTimeNssMap, &barrier)();
});
barrier.countDownAndWait();
// Shut thread pool down.
pool.shutdown();
pool.join();
// Read oplog entries from the oplog collection starting with the entry with the most recent
// optime.
auto opCtx = cc().makeOperationContext();
OplogInterfaceLocal oplogInterface(opCtx.get());
auto oplogIter = oplogInterface.makeIterator();
auto nextValue = oplogIter->next();
while (nextValue.isOK()) {
const auto& doc = nextValue.getValue().first;
oplogEntries->emplace_back(unittest::assertGet(OplogEntry::parse(doc)));
nextValue = oplogIter->next();
}
ASSERT_EQUALS(expectedNumOplogEntries, oplogEntries->size());
// Reverse 'oplogEntries' because we inserted the oplog entries in descending order by optime.
std::reverse(oplogEntries->begin(), oplogEntries->end());
// Look up namespaces and their respective optimes (returned by logOp()) in the map.
stdx::lock_guard<Latch> lock(mtx);
ASSERT_EQUALS(2U, opTimeNssMap->size());
}
/**
* Inserts noop oplog entry with embedded namespace string.
* Inserts optime/namespace pair into map while holding a lock on the mutex.
* Returns optime of generated oplog entry.
*/
OpTime _logOpNoopWithMsg(OperationContext* opCtx,
Mutex* mtx,
OpTimeNamespaceStringMap* opTimeNssMap,
const NamespaceString& nss) {
stdx::lock_guard<Latch> lock(*mtx);
// logOp() must be called while holding lock because ephemeralForTest storage engine does not
// support concurrent updates to its internal state.
MutableOplogEntry oplogEntry;
oplogEntry.setOpType(repl::OpTypeEnum::kNoop);
oplogEntry.setNss(nss);
oplogEntry.setObject(BSON("msg" << nss.ns()));
oplogEntry.setWallClockTime(Date_t::now());
auto opTime = logOp(opCtx, &oplogEntry);
ASSERT_FALSE(opTime.isNull());
ASSERT(opTimeNssMap->find(opTime) == opTimeNssMap->end())
<< "Unable to add namespace " << nss << " to map - map contains duplicate entry for optime "
<< opTime;
opTimeNssMap->insert(std::make_pair(opTime, nss));
return opTime;
}
TEST_F(OplogTest, ConcurrentLogOpWithoutDocLockingSupport) {
OpTimeNamespaceStringMap opTimeNssMap;
std::vector<OplogEntry> oplogEntries;
_testConcurrentLogOp(
[](const NamespaceString& nss,
Mutex* mtx,
OpTimeNamespaceStringMap* opTimeNssMap,
unittest::Barrier* barrier) {
return [=] {
auto opCtx = cc().makeOperationContext();
AutoGetDb autoDb(opCtx.get(), nss.db(), MODE_X);
WriteUnitOfWork wunit(opCtx.get());
_logOpNoopWithMsg(opCtx.get(), mtx, opTimeNssMap, nss);
// In a storage engine that does not support doc locking, upon returning from
// logOp(), this thread still holds an implicit MODE_X lock on the oplog collection
// until it commits the WriteUnitOfWork. Therefore, we must wait on the barrier
// after the WUOW is committed.
wunit.commit();
barrier->countDownAndWait();
};
},
&opTimeNssMap,
&oplogEntries,
2U);
_checkOplogEntry(oplogEntries[0], *(opTimeNssMap.begin()));
_checkOplogEntry(oplogEntries[1], *(opTimeNssMap.rbegin()));
}
TEST_F(OplogTest, ConcurrentLogOpWithDocLockingSupport) {
OpTimeNamespaceStringMap opTimeNssMap;
std::vector<OplogEntry> oplogEntries;
ForceSupportsDocLocking support(true);
_testConcurrentLogOp(
[](const NamespaceString& nss,
Mutex* mtx,
OpTimeNamespaceStringMap* opTimeNssMap,
unittest::Barrier* barrier) {
return [=] {
auto opCtx = cc().makeOperationContext();
AutoGetDb autoDb(opCtx.get(), nss.db(), MODE_X);
WriteUnitOfWork wunit(opCtx.get());
_logOpNoopWithMsg(opCtx.get(), mtx, opTimeNssMap, nss);
// In a storage engine that supports doc locking, it is okay for multiple threads to
// maintain uncommitted WUOWs upon returning from logOp() because each thread will
// hold an implicit MODE_IX lock on the oplog collection.
barrier->countDownAndWait();
wunit.commit();
};
},
&opTimeNssMap,
&oplogEntries,
2U);
_checkOplogEntry(oplogEntries[0], *(opTimeNssMap.begin()));
_checkOplogEntry(oplogEntries[1], *(opTimeNssMap.rbegin()));
}
TEST_F(OplogTest, ConcurrentLogOpWithDocLockingSupportRevertFirstOplogEntry) {
OpTimeNamespaceStringMap opTimeNssMap;
std::vector<OplogEntry> oplogEntries;
ForceSupportsDocLocking support(true);
_testConcurrentLogOp(
[](const NamespaceString& nss,
Mutex* mtx,
OpTimeNamespaceStringMap* opTimeNssMap,
unittest::Barrier* barrier) {
return [=] {
auto opCtx = cc().makeOperationContext();
AutoGetDb autoDb(opCtx.get(), nss.db(), MODE_X);
WriteUnitOfWork wunit(opCtx.get());
auto opTime = _logOpNoopWithMsg(opCtx.get(), mtx, opTimeNssMap, nss);
// In a storage engine that supports doc locking, it is okay for multiple threads to
// maintain uncommitted WUOWs upon returning from logOp() because each thread will
// hold an implicit MODE_IX lock on the oplog collection.
barrier->countDownAndWait();
// Revert the first logOp() call and confirm that there are no holes in the
// oplog after committing the oplog entry with the more recent optime.
{
stdx::lock_guard<Latch> lock(*mtx);
auto firstOpTimeAndNss = *(opTimeNssMap->cbegin());
if (opTime == firstOpTimeAndNss.first) {
ASSERT_EQUALS(nss, firstOpTimeAndNss.second)
<< "optime matches entry in optime->nss map but namespace in map is "
"different.";
// Abort WUOW by returning early. The oplog entry for this task should not
// be present in the oplog.
return;
}
}
wunit.commit();
};
},
&opTimeNssMap,
&oplogEntries,
1U);
_checkOplogEntry(oplogEntries[0], *(opTimeNssMap.crbegin()));
}
TEST_F(OplogTest, ConcurrentLogOpWithDocLockingSupportRevertLastOplogEntry) {
OpTimeNamespaceStringMap opTimeNssMap;
std::vector<OplogEntry> oplogEntries;
ForceSupportsDocLocking support(true);
_testConcurrentLogOp(
[](const NamespaceString& nss,
Mutex* mtx,
OpTimeNamespaceStringMap* opTimeNssMap,
unittest::Barrier* barrier) {
return [=] {
auto opCtx = cc().makeOperationContext();
AutoGetDb autoDb(opCtx.get(), nss.db(), MODE_X);
WriteUnitOfWork wunit(opCtx.get());
auto opTime = _logOpNoopWithMsg(opCtx.get(), mtx, opTimeNssMap, nss);
// In a storage engine that supports doc locking, it is okay for multiple threads to
// maintain uncommitted WUOWs upon returning from logOp() because each thread will
// hold an implicit MODE_IX lock on the oplog collection.
barrier->countDownAndWait();
// Revert the last logOp() call and confirm that there are no holes in the
// oplog after committing the oplog entry with the earlier optime.
{
stdx::lock_guard<Latch> lock(*mtx);
auto lastOpTimeAndNss = *(opTimeNssMap->crbegin());
if (opTime == lastOpTimeAndNss.first) {
ASSERT_EQUALS(nss, lastOpTimeAndNss.second)
<< "optime matches entry in optime->nss map but namespace in map is "
"different.";
// Abort WUOW by returning early. The oplog entry for this task should not
// be present in the oplog.
return;
}
}
wunit.commit();
};
},
&opTimeNssMap,
&oplogEntries,
1U);
_checkOplogEntry(oplogEntries[0], *(opTimeNssMap.cbegin()));
}
} // namespace
} // namespace repl
} // namespace mongo
|