diff options
author | Dianna Hohensee <dianna.hohensee@mongodb.com> | 2020-02-14 15:06:40 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-02-19 18:29:37 +0000 |
commit | 2f318f6bc8a136d9273b5cc1973f590af374bae0 (patch) | |
tree | 4e53cb8ff9e801ee04b2836ef35ead80fbb0a3a2 /src/mongo/db/repl/storage_interface_impl.cpp | |
parent | e6a5c661be1d4c33273f73e01665109778ae8333 (diff) | |
download | mongo-2f318f6bc8a136d9273b5cc1973f590af374bae0.tar.gz |
SERVER-46185 Add StorageInterface utilities for replicate before journaling changes and use them to simplify replication recovery oplog truncation code.
Diffstat (limited to 'src/mongo/db/repl/storage_interface_impl.cpp')
-rw-r--r-- | src/mongo/db/repl/storage_interface_impl.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/mongo/db/repl/storage_interface_impl.cpp b/src/mongo/db/repl/storage_interface_impl.cpp index 92841eeacae..69ebcfdbcd1 100644 --- a/src/mongo/db/repl/storage_interface_impl.cpp +++ b/src/mongo/db/repl/storage_interface_impl.cpp @@ -1019,6 +1019,47 @@ Status StorageInterfaceImpl::deleteByFilter(OperationContext* opCtx, }); } +boost::optional<BSONObj> StorageInterfaceImpl::findOplogEntryLessThanOrEqualToTimestamp( + OperationContext* opCtx, Collection* oplog, const Timestamp& timestamp) { + invariant(oplog); + invariant(opCtx->lockState()->isLocked()); + + std::unique_ptr<PlanExecutor, PlanExecutor::Deleter> exec = + InternalPlanner::collectionScan(opCtx, + NamespaceString::kRsOplogNamespace.ns(), + oplog, + PlanExecutor::NO_YIELD, + InternalPlanner::BACKWARD); + + // A record id in the oplog collection is equivalent to the document's timestamp field. + RecordId desiredRecordId = RecordId(timestamp.asULL()); + + // Iterate the collection in reverse until the desiredRecordId, or one less than, is found. + BSONObj bson; + RecordId recordId; + PlanExecutor::ExecState state; + while (PlanExecutor::ADVANCED == (state = exec->getNext(&bson, &recordId))) { + if (recordId <= desiredRecordId) { + invariant(!bson.isEmpty(), + "An empty oplog entry was returned while searching for an oplog entry <= " + + timestamp.toString()); + return bson.getOwned(); + } + } + + return boost::none; +} + +Timestamp StorageInterfaceImpl::getLatestOplogTimestamp(OperationContext* opCtx) { + AutoGetCollectionForReadCommand autoColl(opCtx, NamespaceString::kRsOplogNamespace); + auto statusWithTimestamp = + autoColl.getCollection()->getRecordStore()->getLatestOplogTimestamp(opCtx); + invariant(statusWithTimestamp.isOK(), + str::stream() << "Expected oplog entries to exist: " + << statusWithTimestamp.getStatus()); + return statusWithTimestamp.getValue(); +} + StatusWith<StorageInterface::CollectionSize> StorageInterfaceImpl::getCollectionSize( OperationContext* opCtx, const NamespaceString& nss) { AutoGetCollectionForRead autoColl(opCtx, nss); |