summaryrefslogtreecommitdiff
path: root/src/mongo/db/repl/oplog_buffer_collection.cpp
blob: b31fa1cf96d7b9eff78b17cdf44a0f02d458a8da (plain)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
 *    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 "mongo/db/repl/oplog_buffer_collection.h"

#include <algorithm>
#include <iterator>
#include <numeric>

#include "mongo/base/string_data.h"
#include "mongo/bson/simple_bsonobj_comparator.h"
#include "mongo/bson/util/bson_extract.h"
#include "mongo/db/catalog/clustered_collection_util.h"
#include "mongo/db/catalog/collection_options.h"
#include "mongo/db/catalog/document_validation.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/ops/write_ops_exec.h"
#include "mongo/db/repl/storage_interface.h"
#include "mongo/util/assert_util.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kReplication


namespace mongo {
namespace repl {

namespace {

const StringData kDefaultOplogCollectionNamespace = "local.temp_oplog_buffer"_sd;
const StringData kOplogEntryFieldName = "entry"_sd;
const StringData kIdFieldName = "_id"_sd;
const StringData kTimestampFieldName = "ts"_sd;
const StringData kIdIdxName = "_id_"_sd;

}  // namespace

NamespaceString OplogBufferCollection::getDefaultNamespace() {
    return NamespaceString(kDefaultOplogCollectionNamespace);
}

std::tuple<BSONObj, Timestamp> OplogBufferCollection::addIdToDocument(const BSONObj& orig) {
    invariant(!orig.isEmpty());
    const auto ts = orig[kTimestampFieldName].timestamp();
    invariant(!ts.isNull());
    auto doc = BSON(_keyForTimestamp(ts).firstElement() << kOplogEntryFieldName << orig);
    return std::make_tuple(doc, ts);
}

BSONObj OplogBufferCollection::extractEmbeddedOplogDocument(const BSONObj& orig) {
    return orig.getObjectField(kOplogEntryFieldName);
}


OplogBufferCollection::OplogBufferCollection(StorageInterface* storageInterface, Options options)
    : OplogBufferCollection(storageInterface, getDefaultNamespace(), std::move(options)) {}

OplogBufferCollection::OplogBufferCollection(StorageInterface* storageInterface,
                                             const NamespaceString& nss,
                                             Options options)
    : _storageInterface(storageInterface), _nss(nss), _options(std::move(options)) {}

NamespaceString OplogBufferCollection::getNamespace() const {
    return _nss;
}

OplogBufferCollection::Options OplogBufferCollection::getOptions() const {
    return _options;
}

void OplogBufferCollection::startup(OperationContext* opCtx) {
    if (_options.dropCollectionAtStartup) {
        clear(opCtx);
        return;
    }

    // If the collection doesn't already exist, create it.
    _createCollection(opCtx);

    stdx::lock_guard<Latch> lk(_mutex);
    // If we are starting from an existing collection, we must populate the in memory state of the
    // buffer.
    auto sizeResult = _storageInterface->getCollectionSize(opCtx, _nss);
    fassert(40403, sizeResult);
    _size = sizeResult.getValue();
    _sizeIsValid = true;

    // We always start from the beginning, with _lastPoppedKey being empty. This is safe because
    // it is always safe to replay old oplog entries in order. We explicitly reset all fields
    // since nothing prevents reusing an OplogBufferCollection, and the underlying collection may
    // have changed since the last time we used this OplogBufferCollection.
    _lastPoppedKey = {};
    _peekCache = std::queue<BSONObj>();

    _updateLastPushedTimestampFromCollection(lk, opCtx);
}

void OplogBufferCollection::_updateLastPushedTimestampFromCollection(WithLock,
                                                                     OperationContext* opCtx) {
    auto countResult = _storageInterface->getCollectionCount(opCtx, _nss);
    fassert(40404, countResult);
    _count = countResult.getValue();

    if (_count == 0) {
        _lastPushedTimestamp = {};
        return;
    }

    auto lastPushedObj = _lastDocumentPushed_inlock(opCtx);
    if (lastPushedObj) {
        auto lastPushedId = lastPushedObj->getObjectField(kIdFieldName);
        fassert(
            40405,
            bsonExtractTimestampField(lastPushedId, kTimestampFieldName, &_lastPushedTimestamp));
    } else {
        _lastPushedTimestamp = {};
    }
}

void OplogBufferCollection::shutdown(OperationContext* opCtx) {
    if (_options.dropCollectionAtShutdown) {
        stdx::lock_guard<Latch> lk(_mutex);
        _dropCollection(opCtx);
        _size = 0;
        _count = 0;
        _lastPushedTimestamp = {};
        _lastPoppedKey = {};
        _peekCache = std::queue<BSONObj>();
    }
}

void OplogBufferCollection::push(OperationContext* opCtx,
                                 Batch::const_iterator begin,
                                 Batch::const_iterator end) {
    if (begin == end) {
        return;
    }
    stdx::lock_guard<Latch> lk(_mutex);
    // Make sure timestamp order is correct.
    auto ts = _lastPushedTimestamp;
    std::for_each(begin, end, [&ts](const Value& value) {
        auto previousTimestamp = ts;
        ts = value[kTimestampFieldName].timestamp();
        invariant(!ts.isNull());
        invariant(ts > previousTimestamp,
                  str::stream() << "ts: " << ts.toString()
                                << ", previous: " << previousTimestamp.toString());
    });

    _push(lk, opCtx, begin, end);
    _lastPushedTimestamp = ts;
}

void OplogBufferCollection::preload(OperationContext* opCtx,
                                    Batch::const_iterator begin,
                                    Batch::const_iterator end) {
    if (begin == end) {
        return;
    }
    stdx::lock_guard<Latch> lk(_mutex);
    invariant(_lastPoppedKey.isEmpty());
    _push(lk, opCtx, begin, end);
    _updateLastPushedTimestampFromCollection(lk, opCtx);
}

void OplogBufferCollection::_push(WithLock,
                                  OperationContext* opCtx,
                                  Batch::const_iterator begin,
                                  Batch::const_iterator end) {
    size_t numDocs = std::distance(begin, end);
    std::vector<BSONObj> docsToInsert(numDocs);
    std::transform(begin, end, docsToInsert.begin(), [](const Value& value) {
        auto [doc, ts] = addIdToDocument(value);
        invariant(!value.isEmpty());
        return doc;
    });

    // Disabling internal document validation because the oplog buffer document inserts
    // can violate max data size limit (which is BSONObjMaxUserSize 16MB) check. Since, the max
    // user document size is 16MB, the oplog generated for those writes can exceed 16MB
    // (16MB user data  + additional bytes for oplog fields like ’’op”, “ns”, “ui”).
    DisableDocumentValidation documentValidationDisabler(
        opCtx,
        DocumentValidationSettings::kDisableSchemaValidation |
            DocumentValidationSettings::kDisableInternalValidation);

    write_ops::InsertCommandRequest insertOp(_nss);
    insertOp.setDocuments(std::move(docsToInsert));
    insertOp.setWriteCommandRequestBase([] {
        write_ops::WriteCommandRequestBase wcb;
        wcb.setOrdered(true);
        return wcb;
    }());

    auto writeResult = write_ops_exec::performInserts(opCtx, insertOp);
    invariant(!writeResult.results.empty());
    // Since the writes are ordered, it's ok to check just the last writeOp result.
    uassertStatusOK(writeResult.results.back());

    _count += numDocs;
    if (_sizeIsValid) {
        _size += std::accumulate(begin, end, 0U, [](const size_t& docSize, const Value& value) {
            return docSize + size_t(value.objsize());
        });
    }
    _cvNoLongerEmpty.notify_all();
}

void OplogBufferCollection::waitForSpace(OperationContext* opCtx, std::size_t size) {}

bool OplogBufferCollection::isEmpty() const {
    stdx::lock_guard<Latch> lk(_mutex);
    return _count == 0;
}

std::size_t OplogBufferCollection::getMaxSize() const {
    return 0;
}

std::size_t OplogBufferCollection::getSize() const {
    stdx::lock_guard<Latch> lk(_mutex);
    uassert(4940100, "getSize() called on OplogBufferCollection after seek", _sizeIsValid);
    return _size;
}

std::size_t OplogBufferCollection::getCount() const {
    stdx::lock_guard<Latch> lk(_mutex);
    return _count;
}

void OplogBufferCollection::clear(OperationContext* opCtx) {
    stdx::lock_guard<Latch> lk(_mutex);
    _dropCollection(opCtx);
    _createCollection(opCtx);
    _size = 0;
    _sizeIsValid = true;
    _count = 0;
    _lastPushedTimestamp = {};
    _lastPoppedKey = {};
    _peekCache = std::queue<BSONObj>();
}

bool OplogBufferCollection::tryPop(OperationContext* opCtx, Value* value) {
    stdx::lock_guard<Latch> lk(_mutex);
    if (_count == 0) {
        return false;
    }
    return _pop_inlock(opCtx, value);
}

bool OplogBufferCollection::waitForDataFor(Milliseconds waitDuration,
                                           Interruptible* interruptible) {
    stdx::unique_lock<Latch> lk(_mutex);
    if (!interruptible->waitForConditionOrInterruptFor(
            _cvNoLongerEmpty, lk, waitDuration, [&]() { return _count != 0; })) {
        return false;
    }
    return _count != 0;
}

bool OplogBufferCollection::waitForDataUntil(Date_t deadline, Interruptible* interruptible) {
    stdx::unique_lock<Latch> lk(_mutex);
    if (!interruptible->waitForConditionOrInterruptUntil(
            _cvNoLongerEmpty, lk, deadline, [&]() { return _count != 0; })) {
        return false;
    }
    return _count != 0;
}

bool OplogBufferCollection::peek(OperationContext* opCtx, Value* value) {
    stdx::lock_guard<Latch> lk(_mutex);
    if (_count == 0) {
        return false;
    }
    *value = _peek_inlock(opCtx, PeekMode::kExtractEmbeddedDocument);
    return true;
}

boost::optional<OplogBuffer::Value> OplogBufferCollection::lastObjectPushed(
    OperationContext* opCtx) const {
    stdx::lock_guard<Latch> lk(_mutex);
    auto lastDocumentPushed = _lastDocumentPushed_inlock(opCtx);
    if (lastDocumentPushed) {
        BSONObj entryObj = extractEmbeddedOplogDocument(*lastDocumentPushed);
        entryObj.shareOwnershipWith(*lastDocumentPushed);
        return entryObj;
    }
    return boost::none;
}

/* static */
BSONObj OplogBufferCollection::_keyForTimestamp(const Timestamp& ts) {
    return BSON(kIdFieldName << BSON(kTimestampFieldName << ts));
}

StatusWith<BSONObj> OplogBufferCollection::_getDocumentWithTimestamp(OperationContext* opCtx,
                                                                     const Timestamp& ts) {
    return _storageInterface->findById(opCtx, _nss, _keyForTimestamp(ts).firstElement());
}

StatusWith<OplogBuffer::Value> OplogBufferCollection::findByTimestamp(OperationContext* opCtx,
                                                                      const Timestamp& ts) {
    auto docWithStatus = _getDocumentWithTimestamp(opCtx, ts);
    if (!docWithStatus.isOK()) {
        return docWithStatus.getStatus();
    }
    return extractEmbeddedOplogDocument(docWithStatus.getValue()).getOwned();
}

Status OplogBufferCollection::seekToTimestamp(OperationContext* opCtx,
                                              const Timestamp& ts,
                                              SeekStrategy exact) {
    stdx::lock_guard<Latch> lk(_mutex);
    BSONObj docWithTimestamp;
    auto docWithStatus = _getDocumentWithTimestamp(opCtx, ts);
    if (docWithStatus.isOK()) {
        docWithTimestamp = std::move(docWithStatus.getValue());
    } else if (exact == SeekStrategy::kExact) {
        return docWithStatus.getStatus();
    }
    _peekCache = std::queue<BSONObj>();
    auto key = _keyForTimestamp(ts);
    if (docWithTimestamp.isEmpty()) {
        // The document with the requested timestamp was not found.  Set _lastPoppedKey to
        // the key for that document, so next time we pop we will read the next document after
        // the requested timestamp.
        _lastPoppedKey = key;
    } else {
        // The document with the requested timestamp was found.  _lastPoppedKey will be set to that
        // document's timestamp once the document is popped from the peek cache in _pop_inlock().
        _lastPoppedKey = {};
        _peekCache.push(docWithTimestamp);
    }
    // Unfortunately StorageInterface and InternalPlanner don't support count-by-index, so we're
    // stuck with DBDirectClient.
    DBDirectClient client(opCtx);
    auto query = BSON(kIdFieldName << BSON("$gte" << key[kIdFieldName]));
    _count = client.count(_nss, query);

    // We have no way of accurately determining the size remaining after the seek
    _sizeIsValid = false;
    return Status::OK();
}

boost::optional<OplogBuffer::Value> OplogBufferCollection::_lastDocumentPushed_inlock(
    OperationContext* opCtx) const {
    if (_count == 0) {
        return boost::none;
    }
    const auto docs =
        fassert(40348,
                _storageInterface->findDocuments(opCtx,
                                                 _nss,
                                                 kIdIdxName,
                                                 StorageInterface::ScanDirection::kBackward,
                                                 {},
                                                 BoundInclusion::kIncludeStartKeyOnly,
                                                 1U));
    invariant(1U == docs.size());
    return docs.front();
}

bool OplogBufferCollection::_pop_inlock(OperationContext* opCtx, Value* value) {
    BSONObj docFromCollection =
        _peek_inlock(opCtx, PeekMode::kReturnUnmodifiedDocumentFromCollection);
    _lastPoppedKey = docFromCollection[kIdFieldName].wrap("");
    *value = extractEmbeddedOplogDocument(docFromCollection).getOwned();

    invariant(!_peekCache.empty());
    invariant(!SimpleBSONObjComparator::kInstance.compare(docFromCollection, _peekCache.front()));
    _peekCache.pop();

    invariant(_count > 0);
    if (_sizeIsValid) {
        invariant(_size >= std::size_t(value->objsize()));
        _size -= value->objsize();
    }
    _count--;
    return true;
}

BSONObj OplogBufferCollection::_peek_inlock(OperationContext* opCtx, PeekMode peekMode) {
    invariant(_count > 0);

    BSONObj startKey;
    auto boundInclusion = BoundInclusion::kIncludeStartKeyOnly;

    // Previously popped documents are not actually removed from the collection. We use the last
    // popped key to skip ahead to the first document that has not been popped.
    if (!_lastPoppedKey.isEmpty()) {
        startKey = _lastPoppedKey;
        boundInclusion = BoundInclusion::kIncludeEndKeyOnly;
    }

    bool isPeekCacheEnabled = _options.peekCacheSize > 0;
    // Check read ahead cache and read additional documents into cache if necessary - only valid
    // when size of read ahead cache is greater than zero in the options.
    if (_peekCache.empty()) {
        std::size_t limit = isPeekCacheEnabled ? _options.peekCacheSize : 1U;
        const auto docs =
            fassert(40163,
                    _storageInterface->findDocuments(opCtx,
                                                     _nss,
                                                     kIdIdxName,
                                                     StorageInterface::ScanDirection::kForward,
                                                     startKey,
                                                     boundInclusion,
                                                     limit));
        invariant(!docs.empty());
        for (const auto& doc : docs) {
            _peekCache.push(doc);
        }
    }
    auto&& doc = _peekCache.front();

    switch (peekMode) {
        case PeekMode::kExtractEmbeddedDocument:
            return extractEmbeddedOplogDocument(doc).getOwned();
            break;
        case PeekMode::kReturnUnmodifiedDocumentFromCollection:
            invariant(doc.isOwned());
            return doc;
            break;
    }

    MONGO_UNREACHABLE;
}

void OplogBufferCollection::_createCollection(OperationContext* opCtx) {
    CollectionOptions options;
    options.temp = _options.useTemporaryCollection;
    // This oplog-like collection will benefit from clustering by _id to reduce storage engine
    // overhead and improve _id query efficiency.
    options.clusteredIndex = clustered_util::makeDefaultClusteredIdIndex();
    UninterruptibleLockGuard noInterrupt(opCtx->lockState());
    auto status = _storageInterface->createCollection(opCtx, _nss, options);
    if (status.code() == ErrorCodes::NamespaceExists)
        return;
    uassertStatusOK(status);
}

void OplogBufferCollection::_dropCollection(OperationContext* opCtx) {
    UninterruptibleLockGuard noInterrupt(opCtx->lockState());
    uassertStatusOK(_storageInterface->dropCollection(opCtx, _nss));
}

Timestamp OplogBufferCollection::getLastPushedTimestamp() const {
    stdx::lock_guard<Latch> lk(_mutex);
    return _lastPushedTimestamp;
}

Timestamp OplogBufferCollection::getLastPoppedTimestamp_forTest() const {
    stdx::lock_guard<Latch> lk(_mutex);
    return _lastPoppedKey.isEmpty() ? Timestamp()
                                    : _lastPoppedKey[""].Obj()[kTimestampFieldName].timestamp();
}

std::queue<BSONObj> OplogBufferCollection::getPeekCache_forTest() const {
    stdx::lock_guard<Latch> lk(_mutex);
    return _peekCache;
}

}  // namespace repl
}  // namespace mongo