/
collection_cloner.cpp
317 lines (283 loc) · 13.1 KB
/
collection_cloner.cpp
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
/**
* Copyright (C) 2019-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.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kReplicationInitialSync
#include "mongo/platform/basic.h"
#include "mongo/base/string_data.h"
#include "mongo/db/commands/list_collections_filter.h"
#include "mongo/db/repl/collection_bulk_loader.h"
#include "mongo/db/repl/collection_cloner.h"
#include "mongo/db/repl/database_cloner_gen.h"
#include "mongo/db/repl/repl_server_parameters_gen.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/log.h"
namespace mongo {
namespace repl {
// Failpoint which causes initial sync to hang when it has cloned 'numDocsToClone' documents to
// collection 'namespace'.
MONGO_FAIL_POINT_DEFINE(initialSyncHangDuringCollectionClone);
// Failpoint which causes initial sync to hang after handling the next batch of results from the
// DBClientConnection, optionally limited to a specific collection.
MONGO_FAIL_POINT_DEFINE(initialSyncHangCollectionClonerAfterHandlingBatchResponse);
CollectionCloner::CollectionCloner(const NamespaceString& sourceNss,
const CollectionOptions& collectionOptions,
InitialSyncSharedData* sharedData,
const HostAndPort& source,
DBClientConnection* client,
StorageInterface* storageInterface,
ThreadPool* dbPool,
ClockSource* clock)
: BaseCloner(
"CollectionCloner"_sd, sharedData, source, client, storageInterface, dbPool, clock),
_sourceNss(sourceNss),
_collectionOptions(collectionOptions),
_sourceDbAndUuid(NamespaceString("UNINITIALIZED")),
_collectionClonerBatchSize(collectionClonerBatchSize),
_countStage("count", this, &CollectionCloner::countStage),
_listIndexesStage("listIndexes", this, &CollectionCloner::listIndexesStage),
_createCollectionStage("createCollection", this, &CollectionCloner::createCollectionStage),
_queryStage("query", this, &CollectionCloner::queryStage),
_progressMeter(1U, // total will be replaced with count command result.
kProgressMeterSecondsBetween,
kProgressMeterCheckInterval,
"documents copied",
str::stream() << _sourceNss.toString() << " collection clone progress"),
_dbWorkTaskRunner(dbPool),
_scheduleDbWorkFn([this](executor::TaskExecutor::CallbackFn work) {
auto task = [ this, work = std::move(work) ](
OperationContext * opCtx,
const Status& status) mutable noexcept->TaskRunner::NextAction {
try {
work(executor::TaskExecutor::CallbackArgs(nullptr, {}, status, opCtx));
} catch (const DBException& e) {
setInitialSyncFailedStatus(e.toStatus());
}
return TaskRunner::NextAction::kDisposeOperationContext;
};
_dbWorkTaskRunner.schedule(std::move(task));
return executor::TaskExecutor::CallbackHandle();
}) {
invariant(sourceNss.isValid());
invariant(collectionOptions.uuid);
_sourceDbAndUuid = NamespaceStringOrUUID(sourceNss.db().toString(), *collectionOptions.uuid);
_stats.ns = _sourceNss.ns();
}
BaseCloner::ClonerStages CollectionCloner::getStages() {
return {&_countStage, &_listIndexesStage, &_createCollectionStage, &_queryStage};
}
void CollectionCloner::preStage() {
stdx::lock_guard<Latch> lk(_mutex);
_stats.start = getClock()->now();
}
void CollectionCloner::postStage() {
stdx::lock_guard<Latch> lk(_mutex);
_stats.end = getClock()->now();
}
// Collection cloner stages exit normally if the collection is not found.
BaseCloner::AfterStageBehavior CollectionCloner::CollectionClonerStage::run() {
try {
return ClonerStage<CollectionCloner>::run();
} catch (const ExceptionFor<ErrorCodes::NamespaceNotFound>&) {
log() << "CollectionCloner ns: '" << getCloner()->getSourceNss() << "' uuid: UUID(\""
<< getCloner()->getSourceUuid()
<< "\") stopped because collection was dropped on source.";
return kSkipRemainingStages;
}
}
BaseCloner::AfterStageBehavior CollectionCloner::countStage() {
auto count = getClient()->count(_sourceDbAndUuid, {} /* Query */, QueryOption_SlaveOk);
// The count command may return a negative value after an unclean shutdown,
// so we set it to zero here to avoid aborting the collection clone.
// Note that this count value is only used for reporting purposes.
if (count < 0) {
warning() << "Count command returned negative value. Updating to 0 to allow progress "
"meter to function properly. ";
count = 0;
}
_progressMeter.setTotalWhileRunning(static_cast<unsigned long long>(count));
{
stdx::lock_guard<Latch> lk(_mutex);
_stats.documentToCopy = count;
}
return kContinueNormally;
}
BaseCloner::AfterStageBehavior CollectionCloner::listIndexesStage() {
auto indexSpecs = getClient()->getIndexSpecs(_sourceDbAndUuid, QueryOption_SlaveOk);
if (indexSpecs.empty()) {
warning() << "No indexes found for collection " << _sourceNss.ns() << " while cloning from "
<< getSource();
}
for (auto&& spec : indexSpecs) {
if (spec["name"].str() == "_id_"_sd) {
_idIndexSpec = spec.getOwned();
} else {
_indexSpecs.push_back(spec.getOwned());
}
}
{
stdx::lock_guard<Latch> lk(_mutex);
_stats.indexes = _indexSpecs.size() + (_idIndexSpec.isEmpty() ? 0 : 1);
};
if (!_idIndexSpec.isEmpty() && _collectionOptions.autoIndexId == CollectionOptions::NO) {
warning()
<< "Found the _id_ index spec but the collection specified autoIndexId of false on ns:"
<< this->_sourceNss;
}
return kContinueNormally;
}
BaseCloner::AfterStageBehavior CollectionCloner::createCollectionStage() {
auto collectionBulkLoader = getStorageInterface()->createCollectionForBulkLoading(
_sourceNss, _collectionOptions, _idIndexSpec, _indexSpecs);
uassertStatusOK(collectionBulkLoader.getStatus());
_collLoader = std::move(collectionBulkLoader.getValue());
return kContinueNormally;
}
BaseCloner::AfterStageBehavior CollectionCloner::queryStage() {
getClient()->query([this](DBClientCursorBatchIterator& iter) { handleNextBatch(iter); },
_sourceDbAndUuid,
QUERY("query" << BSONObj() << "$readOnce" << true),
nullptr /* fieldsToReturn */,
QueryOption_NoCursorTimeout | QueryOption_SlaveOk |
(collectionClonerUsesExhaust ? QueryOption_Exhaust : 0),
_collectionClonerBatchSize);
_dbWorkTaskRunner.join();
// We want to free the _collLoader regardless of whether the commit succeeds.
std::unique_ptr<CollectionBulkLoader> loader = std::move(_collLoader);
uassertStatusOK(loader->commit());
return kContinueNormally;
}
void CollectionCloner::handleNextBatch(DBClientCursorBatchIterator& iter) {
{
stdx::lock_guard<InitialSyncSharedData> lk(*getSharedData());
if (!getSharedData()->getInitialSyncStatus(lk).isOK()) {
std::string message = str::stream()
<< "Collection cloning cancelled due to initial sync failure: "
<< getSharedData()->getInitialSyncStatus(lk).toString();
log() << message;
uasserted(ErrorCodes::CallbackCanceled, message);
}
}
{
stdx::lock_guard<Latch> lk(_mutex);
_stats.receivedBatches++;
while (iter.moreInCurrentBatch()) {
_documentsToInsert.emplace_back(iter.nextSafe());
}
}
// Schedule the next document batch insertion.
auto&& scheduleResult = _scheduleDbWorkFn(
[=](const executor::TaskExecutor::CallbackArgs& cbd) { insertDocumentsCallback(cbd); });
if (!scheduleResult.isOK()) {
Status newStatus = scheduleResult.getStatus().withContext(
str::stream() << "Error cloning collection '" << _sourceNss.ns() << "'");
// We must throw an exception to terminate query.
uassertStatusOK(newStatus);
}
initialSyncHangCollectionClonerAfterHandlingBatchResponse.executeIf(
[&](const BSONObj&) {
while (MONGO_unlikely(
initialSyncHangCollectionClonerAfterHandlingBatchResponse.shouldFail()) &&
!mustExit()) {
log() << "initialSyncHangCollectionClonerAfterHandlingBatchResponse fail point "
"enabled for "
<< _sourceNss.toString() << ". Blocking until fail point is disabled.";
mongo::sleepsecs(1);
}
},
[&](const BSONObj& data) {
// Only hang when cloning the specified collection, or if no collection was specified.
auto nss = data["nss"].str();
return nss.empty() || nss == _sourceNss.toString();
});
}
void CollectionCloner::insertDocumentsCallback(const executor::TaskExecutor::CallbackArgs& cbd) {
uassertStatusOK(cbd.status);
std::vector<BSONObj> docs;
{
stdx::lock_guard<Latch> lk(_mutex);
if (_documentsToInsert.size() == 0) {
warning() << "_insertDocumentsCallback, but no documents to insert for ns:"
<< _sourceNss;
return;
}
_documentsToInsert.swap(docs);
_stats.documentsCopied += docs.size();
++_stats.fetchedBatches;
}
_progressMeter.hit(int(docs.size()));
invariant(_collLoader);
uassertStatusOK(_collLoader->insertDocuments(docs.cbegin(), docs.cend()));
initialSyncHangDuringCollectionClone.executeIf(
[&](const BSONObj&) {
log() << "initial sync - initialSyncHangDuringCollectionClone fail point "
"enabled. Blocking until fail point is disabled.";
while (MONGO_unlikely(initialSyncHangDuringCollectionClone.shouldFail()) &&
!mustExit()) {
mongo::sleepsecs(1);
}
},
[&](const BSONObj& data) {
return data["namespace"].String() == _sourceNss.ns() &&
static_cast<int>(_stats.documentsCopied) >= data["numDocsToClone"].numberInt();
});
}
bool CollectionCloner::isMyFailPoint(const BSONObj& data) const {
auto nss = data["nss"].str();
return (nss.empty() || nss == _sourceNss.toString()) && BaseCloner::isMyFailPoint(data);
}
CollectionCloner::Stats CollectionCloner::getStats() const {
stdx::lock_guard<Latch> lk(_mutex);
return _stats;
}
std::string CollectionCloner::Stats::toString() const {
return toBSON().toString();
}
BSONObj CollectionCloner::Stats::toBSON() const {
BSONObjBuilder bob;
bob.append("ns", ns);
append(&bob);
return bob.obj();
}
void CollectionCloner::Stats::append(BSONObjBuilder* builder) const {
builder->appendNumber(kDocumentsToCopyFieldName, documentToCopy);
builder->appendNumber(kDocumentsCopiedFieldName, documentsCopied);
builder->appendNumber("indexes", indexes);
builder->appendNumber("fetchedBatches", fetchedBatches);
if (start != Date_t()) {
builder->appendDate("start", start);
if (end != Date_t()) {
builder->appendDate("end", end);
auto elapsed = end - start;
long long elapsedMillis = duration_cast<Milliseconds>(elapsed).count();
builder->appendNumber("elapsedMillis", elapsedMillis);
}
}
builder->appendNumber("receivedBatches", receivedBatches);
}
} // namespace repl
} // namespace mongo