@@ -32,9 +32,11 @@ Copyright (C) 2021-present Percona and/or its affiliates. All rights reserved.
32
32
33
33
#include " mongo/db/storage/wiredtiger/wiredtiger_backup_cursor_hooks.h"
34
34
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"
36
37
#include " mongo/db/dbhelpers.h"
37
38
#include " mongo/db/operation_context.h"
39
+ #include " mongo/db/repl/storage_interface.h"
38
40
#include " mongo/db/storage/encryption_hooks.h"
39
41
#include " mongo/logv2/log.h"
40
42
#include " mongo/logv2/log_options.h"
@@ -97,6 +99,8 @@ BackupCursorState WiredTigerBackupCursorHooks::openBackupCursor(
97
99
" $backupCursor can succeed." ,
98
100
_state != kHotBackup );
99
101
102
+ repl::ReplicationStateTransitionLockGuard replTransitionLock (opCtx, MODE_IX);
103
+
100
104
// Replica sets must also return the opTime's of the earliest and latest oplog entry. The
101
105
// range represented by the oplog start/end values must exist in the backup copy, but are
102
106
// not expected to be exact.
@@ -106,15 +110,12 @@ BackupCursorState WiredTigerBackupCursorHooks::openBackupCursor(
106
110
// If the oplog exists, capture the last oplog entry before opening the backup cursor. This
107
111
// value will be checked again after the cursor is established to guarantee it still exists
108
112
// (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);
118
119
}
119
120
120
121
auto * engine = opCtx->getServiceContext ()->getStorageEngine ();
0 commit comments