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
|
/**
* 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.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kReplication
#include "mongo/platform/basic.h"
#include "mongo/db/repl/replication_consistency_markers_impl.h"
#include "mongo/db/bson/bson_helper.h"
#include "mongo/db/catalog/collection_options.h"
#include "mongo/db/concurrency/d_concurrency.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/repl/optime.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/repl/storage_interface.h"
#include "mongo/util/log.h"
namespace mongo {
namespace repl {
constexpr StringData ReplicationConsistencyMarkersImpl::kDefaultMinValidNamespace;
constexpr StringData ReplicationConsistencyMarkersImpl::kDefaultOplogTruncateAfterPointNamespace;
namespace {
const BSONObj kInitialSyncFlag(BSON(MinValidDocument::kInitialSyncFlagFieldName << true));
const BSONObj kOplogTruncateAfterPointId(BSON("_id"
<< "oplogTruncateAfterPoint"));
} // namespace
ReplicationConsistencyMarkersImpl::ReplicationConsistencyMarkersImpl(
StorageInterface* storageInterface)
: ReplicationConsistencyMarkersImpl(
storageInterface,
NamespaceString(ReplicationConsistencyMarkersImpl::kDefaultMinValidNamespace),
NamespaceString(
ReplicationConsistencyMarkersImpl::kDefaultOplogTruncateAfterPointNamespace)) {}
ReplicationConsistencyMarkersImpl::ReplicationConsistencyMarkersImpl(
StorageInterface* storageInterface,
NamespaceString minValidNss,
NamespaceString oplogTruncateAfterPointNss)
: _storageInterface(storageInterface),
_minValidNss(minValidNss),
_oplogTruncateAfterPointNss(oplogTruncateAfterPointNss) {}
boost::optional<MinValidDocument> ReplicationConsistencyMarkersImpl::_getMinValidDocument(
OperationContext* opCtx) const {
auto result = _storageInterface->findSingleton(opCtx, _minValidNss);
if (!result.isOK()) {
if (result.getStatus() == ErrorCodes::NamespaceNotFound ||
result.getStatus() == ErrorCodes::CollectionIsEmpty) {
return boost::none;
}
// Fail if there is an error other than the collection being missing or being empty.
fassertFailedWithStatus(40466, result.getStatus());
}
auto minValid =
MinValidDocument::parse(IDLParserErrorContext("MinValidDocument"), result.getValue());
return minValid;
}
void ReplicationConsistencyMarkersImpl::_updateMinValidDocument(
OperationContext* opCtx, const TimestampedBSONObj& updateSpec) {
Status status = _storageInterface->putSingleton(opCtx, _minValidNss, updateSpec);
invariant(status);
}
void ReplicationConsistencyMarkersImpl::initializeMinValidDocument(OperationContext* opCtx) {
LOG(3) << "Initializing minValid document";
// This initializes the values of the required fields if they are not already set.
// If one of the fields is already set, the $max will prefer the existing value since it
// will always be greater than the provided ones.
TimestampedBSONObj upsert;
upsert.obj = BSON("$max" << BSON(MinValidDocument::kMinValidTimestampFieldName
<< Timestamp()
<< MinValidDocument::kMinValidTermFieldName
<< OpTime::kUninitializedTerm));
// The initialization write should go into the first checkpoint taken, so we provide no
// timestamp. The 'minValid' document could exist already and this could simply add fields to
// the 'minValid' document, but we still want the initialization write to go into the next
// checkpoint since a newly initialized 'minValid' document is always valid.
upsert.timestamp = Timestamp();
fassert(40467, _storageInterface->putSingleton(opCtx, _minValidNss, upsert));
}
bool ReplicationConsistencyMarkersImpl::getInitialSyncFlag(OperationContext* opCtx) const {
auto doc = _getMinValidDocument(opCtx);
invariant(doc); // Initialized at startup so it should never be missing.
boost::optional<bool> flag = doc->getInitialSyncFlag();
if (!flag) {
LOG(3) << "No initial sync flag set, returning initial sync flag value of false.";
return false;
}
LOG(3) << "returning initial sync flag value of " << flag.get();
return flag.get();
}
void ReplicationConsistencyMarkersImpl::setInitialSyncFlag(OperationContext* opCtx) {
LOG(3) << "setting initial sync flag";
TimestampedBSONObj update;
update.obj = BSON("$set" << kInitialSyncFlag);
// We do not provide a timestamp when we set the initial sync flag. Initial sync can only
// occur right when we start up, and thus there cannot be any checkpoints being taken. This
// write should go into the next checkpoint.
update.timestamp = Timestamp();
_updateMinValidDocument(opCtx, update);
opCtx->recoveryUnit()->waitUntilDurable();
}
void ReplicationConsistencyMarkersImpl::clearInitialSyncFlag(OperationContext* opCtx) {
LOG(3) << "clearing initial sync flag";
auto replCoord = repl::ReplicationCoordinator::get(opCtx);
OpTimeAndWallTime opTimeAndWallTime = replCoord->getMyLastAppliedOpTimeAndWallTime();
const auto time = opTimeAndWallTime.opTime;
TimestampedBSONObj update;
update.obj = BSON("$unset" << kInitialSyncFlag << "$set"
<< BSON(MinValidDocument::kMinValidTimestampFieldName
<< time.getTimestamp()
<< MinValidDocument::kMinValidTermFieldName
<< time.getTerm()
<< MinValidDocument::kAppliedThroughFieldName
<< time));
// We clear the initial sync flag at the 'lastAppliedOpTime'. This is unnecessary, since there
// should not be any stable checkpoints being taken that this write could inadvertantly enter.
// This 'lastAppliedOpTime' will be the first stable timestamp candidate, so it will be in the
// first stable checkpoint taken after initial sync. This provides more clarity than providing
// no timestamp.
update.timestamp = time.getTimestamp();
_updateMinValidDocument(opCtx, update);
// Make sure to clear the oplogTrucateAfterPoint in case it is stale. Otherwise, we risk the
// possibility of deleting oplog entries that we want to keep. It is safe to clear this
// here since we are consistent at the top of our oplog at this point.
setOplogTruncateAfterPoint(opCtx, Timestamp());
if (getGlobalServiceContext()->getStorageEngine()->isDurable()) {
opCtx->recoveryUnit()->waitUntilDurable();
replCoord->setMyLastDurableOpTimeAndWallTime(opTimeAndWallTime);
}
}
OpTime ReplicationConsistencyMarkersImpl::getMinValid(OperationContext* opCtx) const {
auto doc = _getMinValidDocument(opCtx);
invariant(doc); // Initialized at startup so it should never be missing.
auto minValid = OpTime(doc->getMinValidTimestamp(), doc->getMinValidTerm());
LOG(3) << "returning minvalid: " << minValid.toString() << "(" << minValid.toBSON() << ")";
return minValid;
}
void ReplicationConsistencyMarkersImpl::setMinValid(OperationContext* opCtx,
const OpTime& minValid) {
LOG(3) << "setting minvalid to exactly: " << minValid.toString() << "(" << minValid.toBSON()
<< ")";
TimestampedBSONObj update;
update.obj = BSON("$set" << BSON(MinValidDocument::kMinValidTimestampFieldName
<< minValid.getTimestamp()
<< MinValidDocument::kMinValidTermFieldName
<< minValid.getTerm()));
// This method is only used with storage engines that do not support recover to stable
// timestamp. As a result, their timestamps do not matter.
invariant(!opCtx->getServiceContext()->getStorageEngine()->supportsRecoverToStableTimestamp());
update.timestamp = Timestamp();
_updateMinValidDocument(opCtx, update);
}
void ReplicationConsistencyMarkersImpl::setMinValidToAtLeast(OperationContext* opCtx,
const OpTime& minValid) {
LOG(3) << "setting minvalid to at least: " << minValid.toString() << "(" << minValid.toBSON()
<< ")";
auto& termField = MinValidDocument::kMinValidTermFieldName;
auto& tsField = MinValidDocument::kMinValidTimestampFieldName;
// Always update both fields of optime.
auto updateSpec =
BSON("$set" << BSON(tsField << minValid.getTimestamp() << termField << minValid.getTerm()));
BSONObj query;
if (minValid.getTerm() == OpTime::kUninitializedTerm) {
// Only compare timestamps in PV0, but update both fields of optime.
// e.g { ts: { $lt: Timestamp 1508961481000|2 } }
query = BSON(tsField << LT << minValid.getTimestamp());
} else {
// Set the minValid only if the given term is higher or the terms are the same but
// the given timestamp is higher.
// e.g. { $or: [ { t: { $lt: 1 } }, { t: 1, ts: { $lt: Timestamp 1508961481000|6 } } ] }
query = BSON(
OR(BSON(termField << LT << minValid.getTerm()),
BSON(termField << minValid.getTerm() << tsField << LT << minValid.getTimestamp())));
}
TimestampedBSONObj update;
update.obj = updateSpec;
// We write to the 'minValid' document with the 'minValid' timestamp. We only take stable
// checkpoints when we are consistent. Thus, the next checkpoint we can take is at this
// 'minValid'. If we gave it a timestamp from before the batch, and we took a stable checkpoint
// at that timestamp, then we would consider that checkpoint inconsistent, even though it is
// consistent.
update.timestamp = minValid.getTimestamp();
Status status = _storageInterface->updateSingleton(opCtx, _minValidNss, query, update);
invariant(status);
}
void ReplicationConsistencyMarkersImpl::setAppliedThrough(OperationContext* opCtx,
const OpTime& optime,
bool setTimestamp) {
invariant(!optime.isNull());
LOG(3) << "setting appliedThrough to: " << optime.toString() << "(" << optime.toBSON() << ")";
// We set the 'appliedThrough' to the provided timestamp. The 'appliedThrough' is only valid
// in checkpoints that contain all writes through this timestamp since it indicates the top of
// the oplog.
TimestampedBSONObj update;
if (setTimestamp) {
update.timestamp = optime.getTimestamp();
}
update.obj = BSON("$set" << BSON(MinValidDocument::kAppliedThroughFieldName << optime));
_updateMinValidDocument(opCtx, update);
}
void ReplicationConsistencyMarkersImpl::clearAppliedThrough(OperationContext* opCtx,
const Timestamp& writeTimestamp) {
LOG(3) << "clearing appliedThrough at: " << writeTimestamp.toString();
TimestampedBSONObj update;
update.timestamp = writeTimestamp;
update.obj = BSON("$unset" << BSON(MinValidDocument::kAppliedThroughFieldName << 1));
_updateMinValidDocument(opCtx, update);
}
OpTime ReplicationConsistencyMarkersImpl::getAppliedThrough(OperationContext* opCtx) const {
auto doc = _getMinValidDocument(opCtx);
invariant(doc); // Initialized at startup so it should never be missing.
auto appliedThrough = doc->getAppliedThrough();
if (!appliedThrough) {
LOG(3) << "No appliedThrough OpTime set, returning empty appliedThrough OpTime.";
return {};
}
LOG(3) << "returning appliedThrough: " << appliedThrough->toString() << "("
<< appliedThrough->toBSON() << ")";
return appliedThrough.get();
}
boost::optional<OplogTruncateAfterPointDocument>
ReplicationConsistencyMarkersImpl::_getOplogTruncateAfterPointDocument(
OperationContext* opCtx) const {
auto doc = _storageInterface->findById(
opCtx, _oplogTruncateAfterPointNss, kOplogTruncateAfterPointId["_id"]);
if (!doc.isOK()) {
if (doc.getStatus() == ErrorCodes::NoSuchKey ||
doc.getStatus() == ErrorCodes::NamespaceNotFound) {
return boost::none;
} else {
// Fails if there is an error other than the collection being missing or being empty.
fassertFailedWithStatus(40510, doc.getStatus());
}
}
auto oplogTruncateAfterPoint = OplogTruncateAfterPointDocument::parse(
IDLParserErrorContext("OplogTruncateAfterPointDocument"), doc.getValue());
return oplogTruncateAfterPoint;
}
void ReplicationConsistencyMarkersImpl::_upsertOplogTruncateAfterPointDocument(
OperationContext* opCtx, const BSONObj& updateSpec) {
fassert(40512,
_storageInterface->upsertById(
opCtx, _oplogTruncateAfterPointNss, kOplogTruncateAfterPointId["_id"], updateSpec));
}
void ReplicationConsistencyMarkersImpl::setOplogTruncateAfterPoint(OperationContext* opCtx,
const Timestamp& timestamp) {
LOG(3) << "setting oplog truncate after point to: " << timestamp.toBSON();
_upsertOplogTruncateAfterPointDocument(
opCtx,
BSON("$set" << BSON(OplogTruncateAfterPointDocument::kOplogTruncateAfterPointFieldName
<< timestamp)));
}
Timestamp ReplicationConsistencyMarkersImpl::getOplogTruncateAfterPoint(
OperationContext* opCtx) const {
auto doc = _getOplogTruncateAfterPointDocument(opCtx);
if (!doc) {
LOG(3) << "Returning empty oplog truncate after point since document did not exist";
return {};
}
Timestamp out = doc->getOplogTruncateAfterPoint();
LOG(3) << "returning oplog truncate after point: " << out;
return out;
}
Status ReplicationConsistencyMarkersImpl::createInternalCollections(OperationContext* opCtx) {
for (auto nss : std::vector<NamespaceString>({_oplogTruncateAfterPointNss, _minValidNss})) {
auto status = _storageInterface->createCollection(opCtx, nss, CollectionOptions());
if (!status.isOK() && status.code() != ErrorCodes::NamespaceExists) {
return {ErrorCodes::CannotCreateCollection,
str::stream() << "Failed to create collection. Ns: " << nss.ns() << " Error: "
<< status.toString()};
}
}
return Status::OK();
}
} // namespace repl
} // namespace mongo
|