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
|
/**
* 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/catalog/index_catalog.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/query/canonical_query.h"
#include "mongo/db/repl/optime.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() : _lk(&_opCtx), _context(&_opCtx, nss.ns()), _client(&_opCtx) {
Collection* c = _context.db()->getCollection(&_opCtx, nss);
if (!c) {
WriteUnitOfWork wuow(&_opCtx);
c = _context.db()->createCollection(&_opCtx, nss.ns());
wuow.commit();
}
ASSERT(c->getIndexCatalog()->haveIdIndex(&_opCtx));
}
~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(&_opCtx, nss);
}
DBDirectClient* client() {
return &_client;
}
void setupFromQuery(const BSONObj& query) {
auto qr = stdx::make_unique<QueryRequest>(nss);
qr->setFilter(query);
auto statusWithCQ = CanonicalQuery::canonicalize(&_opCtx, std::move(qr));
ASSERT_OK(statusWithCQ.getStatus());
_cq = std::move(statusWithCQ.getValue());
_oplogws.reset(new WorkingSet());
const auto timestamp = query[repl::OpTime::kTimestampFieldName]
.embeddedObjectUserCheck()
.firstElement()
.timestamp();
_stage = stdx::make_unique<OplogStart>(&_opCtx, collection(), timestamp, _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 _opCtxPtr = cc().makeOperationContext();
OperationContext& _opCtx = *_opCtxPtr;
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" << Timestamp(1000, i)));
}
setupFromQuery(BSON("ts" << BSON("$gte" << Timestamp(1000, 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" << Timestamp(1000, i)));
}
setupFromQuery(BSON("ts" << BSON("$gte" << Timestamp(1000, 0))));
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);
}
};
class All : public Suite {
public:
All() : Suite("oplogstart") {}
void setupTests() {
// Replication is not supported by mobile SE.
if (mongo::storageGlobalParams.engine == "mobile") {
return;
}
add<OplogStartIsOldest>();
add<OplogStartIsNewest>();
}
};
SuiteInstance<All> oplogStart;
} // namespace OplogStartTests
|