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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/**
* Copyright (C) 2013-2014 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This file tests db/exec/oplogstart.{h,cpp}. OplogStart is an execution stage
* responsible for walking the oplog backwards in order to find where the oplog should
* be replayed from for replication.
*/
#include "mongo/platform/basic.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/client.h"
#include "mongo/db/db.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/exec/oplogstart.h"
#include "mongo/db/exec/working_set.h"
#include "mongo/db/matcher/extensions_callback_disallow_extensions.h"
#include "mongo/db/query/canonical_query.h"
#include "mongo/db/repl/repl_settings.h"
#include "mongo/db/service_context.h"
#include "mongo/dbtests/dbtests.h"
namespace OplogStartTests {
using std::unique_ptr;
using std::string;
static const NamespaceString nss("unittests.oplogstarttests");
class Base {
public:
Base()
: _scopedXact(&_txn, MODE_X),
_lk(_txn.lockState()),
_context(&_txn, nss.ns()),
_client(&_txn) {
Collection* c = _context.db()->getCollection(nss.ns());
if (!c) {
WriteUnitOfWork wuow(&_txn);
c = _context.db()->createCollection(&_txn, nss.ns());
wuow.commit();
}
ASSERT(c->getIndexCatalog()->haveIdIndex(&_txn));
}
~Base() {
client()->dropCollection(nss.ns());
// The OplogStart stage is not allowed to outlive it's RecoveryUnit.
_stage.reset();
}
protected:
Collection* collection() {
return _context.db()->getCollection(nss.ns());
}
DBDirectClient* client() {
return &_client;
}
void setupFromQuery(const BSONObj& query) {
auto qr = stdx::make_unique<QueryRequest>(nss);
qr->setFilter(query);
auto statusWithCQ = CanonicalQuery::canonicalize(
&_txn, std::move(qr), ExtensionsCallbackDisallowExtensions());
ASSERT_OK(statusWithCQ.getStatus());
_cq = std::move(statusWithCQ.getValue());
_oplogws.reset(new WorkingSet());
_stage.reset(new OplogStart(&_txn, collection(), _cq->root(), _oplogws.get()));
}
void assertWorkingSetMemberHasId(WorkingSetID id, int expectedId) {
WorkingSetMember* member = _oplogws->get(id);
BSONElement idEl = member->obj.value()["_id"];
ASSERT(!idEl.eoo());
ASSERT(idEl.isNumber());
ASSERT_EQUALS(idEl.numberInt(), expectedId);
}
unique_ptr<CanonicalQuery> _cq;
unique_ptr<WorkingSet> _oplogws;
unique_ptr<OplogStart> _stage;
private:
// The order of these is important in order to ensure order of destruction
const ServiceContext::UniqueOperationContext _txnPtr = cc().makeOperationContext();
OperationContext& _txn = *_txnPtr;
ScopedTransaction _scopedXact;
Lock::GlobalWrite _lk;
OldClientContext _context;
DBDirectClient _client;
};
/**
* When the ts is newer than the oldest document, the OplogStart
* stage should find the oldest document using a backwards collection
* scan.
*/
class OplogStartIsOldest : public Base {
public:
void run() {
for (int i = 0; i < 10; ++i) {
client()->insert(nss.ns(), BSON("_id" << i << "ts" << i));
}
setupFromQuery(BSON("ts" << BSON("$gte" << 10)));
WorkingSetID id = WorkingSet::INVALID_ID;
// collection scan needs to be initialized
ASSERT_EQUALS(_stage->work(&id), PlanStage::NEED_TIME);
// finds starting record
ASSERT_EQUALS(_stage->work(&id), PlanStage::ADVANCED);
ASSERT(_stage->isBackwardsScanning());
assertWorkingSetMemberHasId(id, 9);
}
};
/**
* Find the starting oplog record by scanning backwards
* all the way to the beginning.
*/
class OplogStartIsNewest : public Base {
public:
void run() {
for (int i = 0; i < 10; ++i) {
client()->insert(nss.ns(), BSON("_id" << i << "ts" << i));
}
setupFromQuery(BSON("ts" << BSON("$gte" << 1)));
WorkingSetID id = WorkingSet::INVALID_ID;
// collection scan needs to be initialized
ASSERT_EQUALS(_stage->work(&id), PlanStage::NEED_TIME);
// full collection scan back to the first oplog record
for (int i = 0; i < 9; ++i) {
ASSERT_EQUALS(_stage->work(&id), PlanStage::NEED_TIME);
ASSERT(_stage->isBackwardsScanning());
}
ASSERT_EQUALS(_stage->work(&id), PlanStage::ADVANCED);
assertWorkingSetMemberHasId(id, 0);
}
};
/**
* Find the starting oplog record by hopping to the
* beginning of the extent.
*/
class OplogStartIsNewestExtentHop : public Base {
public:
void run() {
for (int i = 0; i < 10; ++i) {
client()->insert(nss.ns(), BSON("_id" << i << "ts" << i));
}
setupFromQuery(BSON("ts" << BSON("$gte" << 1)));
WorkingSetID id = WorkingSet::INVALID_ID;
// ensure that we go into extent hopping mode immediately
_stage->setBackwardsScanTime(0);
// We immediately switch to extent hopping mode, and
// should find the beginning of the extent
ASSERT_EQUALS(_stage->work(&id), PlanStage::ADVANCED);
ASSERT(_stage->isExtentHopping());
assertWorkingSetMemberHasId(id, 0);
}
};
class SizedExtentHopBase : public Base {
public:
SizedExtentHopBase() {
client()->dropCollection(nss.ns());
}
virtual ~SizedExtentHopBase() {
client()->dropCollection(nss.ns());
}
void run() {
buildCollection();
WorkingSetID id = WorkingSet::INVALID_ID;
setupFromQuery(BSON("ts" << BSON("$gte" << tsGte())));
// ensure that we go into extent hopping mode immediately
_stage->setBackwardsScanTime(0);
// hop back extent by extent
for (int i = 0; i < numHops(); i++) {
ASSERT_EQUALS(_stage->work(&id), PlanStage::NEED_TIME);
ASSERT(_stage->isExtentHopping());
}
// find the right loc without hopping again
ASSERT_EQUALS(_stage->work(&id), finalState());
int startDocId = tsGte() - 1;
if (startDocId >= 0) {
assertWorkingSetMemberHasId(id, startDocId);
}
}
protected:
void buildCollection() {
BSONObj info;
// Create a collection with specified extent sizes
BSONObj command =
BSON("create" << nss.coll() << "capped" << true << "$nExtents" << extentSizes()
<< "autoIndexId"
<< false);
ASSERT(client()->runCommand(nss.db().toString(), command, info));
// Populate documents.
for (int i = 0; i < numDocs(); ++i) {
client()->insert(nss.ns(), BSON("_id" << i << "ts" << i << "payload" << payload8k()));
}
}
static string payload8k() {
return string(8 * 1024, 'a');
}
/** An extent of this size is too small to contain one document containing payload8k(). */
static int tooSmall() {
return 1 * 1024;
}
/** An extent of this size fits one document. */
static int fitsOne() {
return 10 * 1024;
}
/** An extent of this size fits many documents. */
static int fitsMany() {
return 50 * 1024;
}
// to be defined by subclasses
virtual BSONArray extentSizes() const = 0;
virtual int numDocs() const = 0;
virtual int numHops() const = 0;
virtual PlanStage::StageState finalState() const {
return PlanStage::ADVANCED;
}
virtual int tsGte() const {
return 1;
}
};
/**
* Test hopping over a single empty extent.
*
* Collection structure:
*
* [--- extent 0 --] [ ext 1 ] [--- extent 2 ---]
* [ {_id: 0} ] [<empty>] [ {_id: 1} ]
*/
class OplogStartOneEmptyExtent : public SizedExtentHopBase {
virtual int numDocs() const {
return 2;
}
virtual int numHops() const {
return 1;
}
virtual BSONArray extentSizes() const {
return BSON_ARRAY(fitsOne() << tooSmall() << fitsOne());
}
};
/**
* Test hopping over two consecutive empty extents.
*
* Collection structure:
*
* [--- extent 0 --] [ ext 1 ] [ ext 2 ] [--- extent 3 ---]
* [ {_id: 0} ] [<empty>] [<empty>] [ {_id: 1} ]
*/
class OplogStartTwoEmptyExtents : public SizedExtentHopBase {
virtual int numDocs() const {
return 2;
}
virtual int numHops() const {
return 1;
}
virtual BSONArray extentSizes() const {
return BSON_ARRAY(fitsOne() << tooSmall() << tooSmall() << fitsOne());
}
};
/**
* Two extents, each filled with several documents. This
* should require us to make just a single extent hop.
*/
class OplogStartTwoFullExtents : public SizedExtentHopBase {
virtual int numDocs() const {
return 10;
}
virtual int numHops() const {
return 1;
}
virtual BSONArray extentSizes() const {
return BSON_ARRAY(fitsMany() << fitsMany());
}
};
/**
* Four extents in total. Three are populated with multiple
* documents, but one of the middle extents is empty. This
* should require two extent hops.
*/
class OplogStartThreeFullOneEmpty : public SizedExtentHopBase {
virtual int numDocs() const {
return 14;
}
virtual int numHops() const {
return 2;
}
virtual BSONArray extentSizes() const {
return BSON_ARRAY(fitsMany() << fitsMany() << tooSmall() << fitsMany());
}
};
/**
* Test that extent hopping mode works properly in the
* special case of one extent.
*/
class OplogStartOneFullExtent : public SizedExtentHopBase {
virtual int numDocs() const {
return 4;
}
virtual int numHops() const {
return 0;
}
virtual BSONArray extentSizes() const {
return BSON_ARRAY(fitsMany());
}
};
/**
* Collection structure:
*
* [ ext 0 ] [--- extent 1 --] [--- extent 2 ---]
* [<empty>] [ {_id: 0} ] [ {_id: 1} ]
*/
class OplogStartFirstExtentEmpty : public SizedExtentHopBase {
virtual int numDocs() const {
return 2;
}
virtual int numHops() const {
return 1;
}
virtual BSONArray extentSizes() const {
return BSON_ARRAY(tooSmall() << fitsOne() << fitsOne());
}
};
/**
* Find that we need to start from the very beginning of
* the collection (the EOF case), after extent hopping
* to the beginning.
*
* This requires two hops: one between the two extents,
* and one to hop back to the "null extent" which precedes
* the first extent.
*/
class OplogStartEOF : public SizedExtentHopBase {
virtual int numDocs() const {
return 2;
}
virtual int numHops() const {
return 2;
}
virtual BSONArray extentSizes() const {
return BSON_ARRAY(fitsOne() << fitsOne());
}
virtual PlanStage::StageState finalState() const {
return PlanStage::IS_EOF;
}
virtual int tsGte() const {
return 0;
}
};
class All : public Suite {
public:
All() : Suite("oplogstart") {}
void setupTests() {
add<OplogStartIsOldest>();
add<OplogStartIsNewest>();
// These tests rely on extent allocation details specific to mmapv1.
// TODO figure out a way to generically test this.
if (getGlobalServiceContext()->getGlobalStorageEngine()->isMmapV1()) {
add<OplogStartIsNewestExtentHop>();
add<OplogStartOneEmptyExtent>();
add<OplogStartTwoEmptyExtents>();
add<OplogStartTwoFullExtents>();
add<OplogStartThreeFullOneEmpty>();
add<OplogStartOneFullExtent>();
add<OplogStartFirstExtentEmpty>();
add<OplogStartEOF>();
}
}
};
SuiteInstance<All> oplogStart;
} // namespace OplogStartTests
|