Skip to content

Commit d32076e

Browse files
committed
PSMDB-1541 Ensure that backup cursor returns oplogEnd without holes
1 parent 9f32a1a commit d32076e

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Diff for: src/mongo/db/storage/wiredtiger/wiredtiger_backup_cursor_hooks.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ Copyright (C) 2021-present Percona and/or its affiliates. All rights reserved.
3232

3333
#include "mongo/db/storage/wiredtiger/wiredtiger_backup_cursor_hooks.h"
3434

35-
#include "mongo/db/db_raii.h"
35+
#include "mongo/db/concurrency/lock_manager_defs.h"
36+
#include "mongo/db/concurrency/replication_state_transition_lock_guard.h"
3637
#include "mongo/db/dbhelpers.h"
3738
#include "mongo/db/operation_context.h"
39+
#include "mongo/db/repl/storage_interface.h"
3840
#include "mongo/db/storage/encryption_hooks.h"
3941
#include "mongo/logv2/log.h"
4042
#include "mongo/logv2/log_options.h"
@@ -97,6 +99,8 @@ BackupCursorState WiredTigerBackupCursorHooks::openBackupCursor(
9799
"$backupCursor can succeed.",
98100
_state != kHotBackup);
99101

102+
repl::ReplicationStateTransitionLockGuard replTransitionLock(opCtx, MODE_IX);
103+
100104
// Replica sets must also return the opTime's of the earliest and latest oplog entry. The
101105
// range represented by the oplog start/end values must exist in the backup copy, but are
102106
// not expected to be exact.
@@ -106,15 +110,12 @@ BackupCursorState WiredTigerBackupCursorHooks::openBackupCursor(
106110
// If the oplog exists, capture the last oplog entry before opening the backup cursor. This
107111
// value will be checked again after the cursor is established to guarantee it still exists
108112
// (and was not truncated before the backup cursor was established.
109-
{
110-
AutoGetCollectionForRead coll(opCtx, NamespaceString::kRsOplogNamespace);
111-
if (coll.getCollection()) {
112-
BSONObj lastEntry;
113-
if (Helpers::getLast(opCtx, NamespaceString::kRsOplogNamespace, lastEntry)) {
114-
auto oplogEntry = fassertNoTrace(50913, repl::OplogEntry::parse(lastEntry));
115-
oplogEnd = oplogEntry.getOpTime();
116-
}
117-
}
113+
auto replCoordinator = repl::ReplicationCoordinator::get(opCtx);
114+
// Using UNSAFE version because we have RSTL acquired.
115+
if (replCoordinator->isReplEnabled() && replCoordinator->isInPrimaryOrSecondaryState_UNSAFE()) {
116+
oplogEnd = replCoordinator->getMyLastAppliedOpTime();
117+
// ensure there are no oplog holes before oplogEnd
118+
repl::StorageInterface::get(opCtx)->waitForAllEarlierOplogWritesToBeVisible(opCtx);
118119
}
119120

120121
auto* engine = opCtx->getServiceContext()->getStorageEngine();

0 commit comments

Comments
 (0)