diff options
author | Moustafa Maher Khalil <m.maher@mongodb.com> | 2021-10-25 21:42:18 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-10-25 22:09:46 +0000 |
commit | 3d9a0b0a424be5f16dee60c1933f886945cd5603 (patch) | |
tree | 6fa3672ef87a06b52cf98b7dbbe2de562334154b | |
parent | 99f4696e26b26a33902067d2f3b71b053413b4ab (diff) | |
download | mongo-3d9a0b0a424be5f16dee60c1933f886945cd5603.tar.gz |
SERVER-57828 Complete initial sync and doing replication recovery
-rw-r--r-- | src/mongo/db/repl/replication_recovery.cpp | 36 | ||||
-rw-r--r-- | src/mongo/db/repl/replication_recovery.h | 12 | ||||
-rw-r--r-- | src/mongo/db/repl/replication_recovery_mock.h | 3 |
3 files changed, 36 insertions, 15 deletions
diff --git a/src/mongo/db/repl/replication_recovery.cpp b/src/mongo/db/repl/replication_recovery.cpp index 4ba3692bb9a..413ccadfe7f 100644 --- a/src/mongo/db/repl/replication_recovery.cpp +++ b/src/mongo/db/repl/replication_recovery.cpp @@ -56,6 +56,7 @@ #include "mongo/db/transaction_history_iterator.h" #include "mongo/db/transaction_participant.h" #include "mongo/logv2/log.h" +#include "mongo/util/scopeguard.h" #include "mongo/util/timer.h" namespace mongo { @@ -322,9 +323,15 @@ void ReplicationRecoveryImpl::_assertNoRecoveryNeededOnUnstableCheckpoint(Operat } } -void ReplicationRecoveryImpl::recoverFromOplogAsStandalone(OperationContext* opCtx) { +void ReplicationRecoveryImpl::recoverFromOplogAsStandalone(OperationContext* opCtx, + bool duringInitialSync) { + _duringInitialSync = duringInitialSync; + ScopeGuard resetInitialSyncFlagOnExit([this] { _duringInitialSync = false; }); auto recoveryTS = recoverFromOplogPrecursor(opCtx, _storageInterface); + // We support only recovery from stable checkpoints during initial sync. + invariant(!_duringInitialSync || recoveryTS); + // Initialize the cached pointer to the oplog collection. acquireOplogCollectionForLogging(opCtx); @@ -359,10 +366,13 @@ void ReplicationRecoveryImpl::recoverFromOplogAsStandalone(OperationContext* opC reconstructPreparedTransactions(opCtx, OplogApplication::Mode::kRecovering); - LOGV2_WARNING(21558, - "Setting mongod to readOnly mode as a result of specifying " - "'recoverFromOplogAsStandalone'"); - storageGlobalParams.readOnly = true; + if (!_duringInitialSync) { + LOGV2_WARNING(21558, + "Setting mongod to readOnly mode as a result of specifying " + "'recoverFromOplogAsStandalone'"); + + storageGlobalParams.readOnly = true; + } } void ReplicationRecoveryImpl::recoverFromOplogUpTo(OperationContext* opCtx, Timestamp endPoint) { @@ -508,10 +518,13 @@ void ReplicationRecoveryImpl::_recoverFromStableTimestamp(OperationContext* opCt "Starting recovery oplog application at the stable timestamp", "stableTimestamp"_attr = stableTimestamp); - if (recoveryMode == RecoveryMode::kStartupFromStableTimestamp && startupRecoveryForRestore) { - LOGV2_WARNING(5576600, - "Replication startup parameter 'startupRecoveryForRestore' is set, " - "recovering without preserving history before top of oplog."); + if (recoveryMode == RecoveryMode::kStartupFromStableTimestamp && + (startupRecoveryForRestore || _duringInitialSync)) { + if (startupRecoveryForRestore) { + LOGV2_WARNING(5576600, + "Replication startup parameter 'startupRecoveryForRestore' is set, " + "recovering without preserving history before top of oplog."); + } // Take only unstable checkpoints during the recovery process. _storageInterface->setInitialDataTimestamp(opCtx->getServiceContext(), Timestamp::kAllowUnstableCheckpointsSentinel); @@ -520,7 +533,8 @@ void ReplicationRecoveryImpl::_recoverFromStableTimestamp(OperationContext* opCt } auto startPoint = _adjustStartPointIfNecessary(opCtx, stableTimestamp); _applyToEndOfOplog(opCtx, startPoint, topOfOplog.getTimestamp(), recoveryMode); - if (recoveryMode == RecoveryMode::kStartupFromStableTimestamp && startupRecoveryForRestore) { + if (recoveryMode == RecoveryMode::kStartupFromStableTimestamp && + (startupRecoveryForRestore || _duringInitialSync)) { _storageInterface->setInitialDataTimestamp(opCtx->getServiceContext(), topOfOplog.getTimestamp()); } @@ -694,7 +708,7 @@ Timestamp ReplicationRecoveryImpl::_applyOplogOperations(OperationContext* opCtx // mode, which discards history before the top of oplog), we aren't doing new checkpoints during // recovery so there is no point in advancing the consistency marker and we cannot advance // "oldest" becaue it would be later than "stable". - const bool advanceTimestampsEachBatch = startupRecoveryForRestore && + const bool advanceTimestampsEachBatch = (startupRecoveryForRestore || _duringInitialSync) && (recoveryMode == RecoveryMode::kStartupFromStableTimestamp || recoveryMode == RecoveryMode::kStartupFromUnstableCheckpoint); diff --git a/src/mongo/db/repl/replication_recovery.h b/src/mongo/db/repl/replication_recovery.h index 614d7fba865..c6c4a000918 100644 --- a/src/mongo/db/repl/replication_recovery.h +++ b/src/mongo/db/repl/replication_recovery.h @@ -42,7 +42,8 @@ class StorageInterface; class ReplicationConsistencyMarkers; /** - * This class is used by the replication system to recover after an unclean shutdown or a rollback. + * This class is used by the replication system to recover after an unclean shutdown, a rollback or + * during initial sync. */ class ReplicationRecovery { public: @@ -61,7 +62,8 @@ public: * 'takeUnstableCheckpointOnShutdown' is specified and an unstable checkpoint is present, * ensures that recovery can be skipped safely. */ - virtual void recoverFromOplogAsStandalone(OperationContext* opCtx) = 0; + virtual void recoverFromOplogAsStandalone(OperationContext* opCtx, + bool duringInitialSync = false) = 0; /** * Recovers the data on disk from the oplog up to and including the given timestamp. @@ -80,7 +82,8 @@ public: void recoverFromOplog(OperationContext* opCtx, boost::optional<Timestamp> stableTimestamp) override; - void recoverFromOplogAsStandalone(OperationContext* opCtx) override; + void recoverFromOplogAsStandalone(OperationContext* opCtx, + bool duringInitialSync = false) override; void recoverFromOplogUpTo(OperationContext* opCtx, Timestamp endPoint) override; @@ -180,6 +183,9 @@ private: StorageInterface* _storageInterface; ReplicationConsistencyMarkers* _consistencyMarkers; + + // Flag to indicate whether the recovery is being done during initial sync or not. + bool _duringInitialSync = false; }; } // namespace repl diff --git a/src/mongo/db/repl/replication_recovery_mock.h b/src/mongo/db/repl/replication_recovery_mock.h index ec546d594c7..97072a4fdd9 100644 --- a/src/mongo/db/repl/replication_recovery_mock.h +++ b/src/mongo/db/repl/replication_recovery_mock.h @@ -45,7 +45,8 @@ public: void recoverFromOplog(OperationContext* opCtx, boost::optional<Timestamp> stableTimestamp) override {} - void recoverFromOplogAsStandalone(OperationContext* opCtx) override {} + void recoverFromOplogAsStandalone(OperationContext* opCtx, + bool duringInitialSync = false) override {} void recoverFromOplogUpTo(OperationContext* opCtx, Timestamp endPoint) override {} }; |