summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandolph Tan <randolph@10gen.com>2022-09-21 19:09:07 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-09-21 22:08:43 +0000
commite4b31b9bbb28c49f34ce433bb8935c0e1781dc8f (patch)
tree27fd0f24f52b142c507322db93ef9cb5c2b97fbe
parent236a408f3b8cd9a72c5c1d8fe623315f63565b4b (diff)
downloadmongo-e4b31b9bbb28c49f34ce433bb8935c0e1781dc8f.tar.gz
SERVER-67563 Implement global index cloner abort and interruption
-rw-r--r--src/mongo/db/s/global_index/global_index_cloning_service.cpp122
-rw-r--r--src/mongo/db/s/global_index/global_index_cloning_service.h21
-rw-r--r--src/mongo/db/s/global_index/global_index_cloning_service_test.cpp31
3 files changed, 120 insertions, 54 deletions
diff --git a/src/mongo/db/s/global_index/global_index_cloning_service.cpp b/src/mongo/db/s/global_index/global_index_cloning_service.cpp
index fd3648bd6bc..2f7bd32ae67 100644
--- a/src/mongo/db/s/global_index/global_index_cloning_service.cpp
+++ b/src/mongo/db/s/global_index/global_index_cloning_service.cpp
@@ -85,7 +85,7 @@ void GlobalIndexCloningService::checkIfConflictsWithOtherInstances(
GlobalIndexCloningService::CloningStateMachine::CloningStateMachine(
ServiceContext* serviceContext,
- const GlobalIndexCloningService* cloningService,
+ GlobalIndexCloningService* cloningService,
std::unique_ptr<GlobalIndexCloningService::CloningExternalState> externalState,
std::unique_ptr<GlobalIndexClonerFetcherFactoryInterface> fetcherFactory,
GlobalIndexClonerDoc clonerDoc)
@@ -111,32 +111,44 @@ GlobalIndexCloningService::CloningStateMachine::CloningStateMachine(
SemiFuture<void> GlobalIndexCloningService::CloningStateMachine::run(
std::shared_ptr<executor::ScopedTaskExecutor> executor,
const CancellationToken& stepdownToken) noexcept {
- auto abortToken = _initAbortSource(stepdownToken);
+ auto cleanupToken = _initCleanupToken(stepdownToken);
_execForCancelableOpCtx->startup();
- _retryingCancelableOpCtxFactory.emplace(abortToken, _execForCancelableOpCtx);
+ _retryingCancelableOpCtxFactory.emplace(cleanupToken, _execForCancelableOpCtx);
_init(executor);
_readyToCommitPromise.setFrom(ExecutorFuture(**executor)
- .then([this, executor, abortToken] {
- return _persistStateDocument(executor, abortToken);
+ .then([this, executor, cleanupToken] {
+ return _persistStateDocument(executor, cleanupToken);
})
- .then([this, executor, abortToken] {
- return _runUntilDoneCloning(executor, abortToken);
+ .then([this, executor, cleanupToken] {
+ return _runUntilDoneCloning(executor, cleanupToken);
})
.unsafeToInlineFuture());
_completionPromise.setFrom(
_readyToCommitPromise.getFuture()
.thenRunOn(**executor)
- .then([this, stepdownToken] {
- _retryingCancelableOpCtxFactory.emplace(stepdownToken, _execForCancelableOpCtx);
- })
.then([this, executor, stepdownToken] {
return future_util::withCancellation(_waitForCleanupPromise.getFuture(),
stepdownToken);
})
- .then([this, executor, stepdownToken] { return _cleanup(executor, stepdownToken); })
+ .thenRunOn(_cloningService->getInstanceCleanupExecutor())
+ // The shared_ptr stored in the PrimaryOnlyService's map for the this instance can be
+ // cleared during replication step up. Stashing a shared_ptr to the callback will
+ // ensure that this instance will be kept alive while the callback is being used.
+ .onCompletion([this, cleanupToken, stepdownToken, self = shared_from_this()](
+ const Status& status) {
+ _retryingCancelableOpCtxFactory.emplace(stepdownToken, _execForCancelableOpCtx);
+
+ auto wasCleanupTriggered = cleanupToken.isCanceled() && !stepdownToken.isCanceled();
+ if (status.isOK() || wasCleanupTriggered) {
+ return _cleanup(_cloningService->getInstanceCleanupExecutor(), stepdownToken)
+ .semi();
+ }
+
+ return SemiFuture<void>(status);
+ })
.unsafeToInlineFuture()
.tapError([](const Status& status) {
LOGV2(6755903,
@@ -147,34 +159,50 @@ SemiFuture<void> GlobalIndexCloningService::CloningStateMachine::run(
return _completionPromise.getFuture().semi();
}
-void GlobalIndexCloningService::CloningStateMachine::interrupt(Status status) {}
-
-void GlobalIndexCloningService::CloningStateMachine::abort() {
- stdx::lock_guard<Latch> lk(_mutex);
- if (_abortSource) {
- _abortSource->cancel();
- }
+void GlobalIndexCloningService::CloningStateMachine::interrupt(Status status) {
+ // This service relies on the cancellation of the token from the primary only service and
+ // ignores the interrupts.
}
ExecutorFuture<void> GlobalIndexCloningService::CloningStateMachine::_cleanup(
- std::shared_ptr<executor::ScopedTaskExecutor> executor,
- const CancellationToken& stepdownToken) {
+ std::shared_ptr<executor::TaskExecutor> executor, const CancellationToken& stepdownToken) {
return _retryingCancelableOpCtxFactory
->withAutomaticRetry([this, executor, stepdownToken](auto& cancelableFactory) {
- return ExecutorFuture(**executor)
- .then([this, executor, stepdownToken, &cancelableFactory] {
+ return ExecutorFuture(executor).then(
+ [this, executor, stepdownToken, &cancelableFactory] {
auto opCtx = cancelableFactory.makeOperationContext(&cc());
- PersistentTaskStore<GlobalIndexClonerDoc> store(
- _cloningService->getStateDocumentsNS());
- store.remove(opCtx.get(),
- BSON(GlobalIndexClonerDoc::kIndexCollectionUUIDFieldName
- << _indexCollectionUUID));
+
+ const BSONObj instanceId(
+ BSON(GlobalIndexClonerDoc::kIndexCollectionUUIDFieldName
+ << _indexCollectionUUID));
+
+ DBDirectClient client(opCtx.get());
+ auto result = client.remove([&] {
+ write_ops::DeleteCommandRequest deleteRequest(
+ _cloningService->getStateDocumentsNS());
+
+ write_ops::DeleteOpEntry deleteStateDoc;
+ deleteStateDoc.setQ(instanceId);
+ deleteStateDoc.setMulti(false);
+ deleteRequest.setDeletes({deleteStateDoc});
+
+ return deleteRequest;
+ }());
+
+ if (const auto& errorList = result.getWriteErrors()) {
+ uassertStatusOK(errorList->front().getStatus());
+ }
+
+ if (result.getN() != 1) {
+ // This is to handle cases where the state document hasn't been inserted.
+ _cloningService->releaseInstance(instanceId, Status::OK());
+ }
});
})
.onTransientError([](const auto& status) {})
.onUnrecoverableError([](const auto& status) {})
.until<Status>([](const Status& status) { return status.isOK(); })
- .on(**executor, stepdownToken);
+ .on(executor, stepdownToken);
}
void GlobalIndexCloningService::CloningStateMachine::_init(
@@ -238,8 +266,6 @@ boost::optional<BSONObj> GlobalIndexCloningService::CloningStateMachine::reportF
return boost::none;
}
-void GlobalIndexCloningService::CloningStateMachine::abort(bool isUserCancelled) {}
-
void GlobalIndexCloningService::CloningStateMachine::checkIfOptionsConflict(
const BSONObj& stateDoc) const {
auto newCloning =
@@ -252,16 +278,22 @@ void GlobalIndexCloningService::CloningStateMachine::checkIfOptionsConflict(
newCloning.getNss() == _sourceNss && newCloning.getCollectionUUID() == _sourceCollUUID);
}
-CancellationToken GlobalIndexCloningService::CloningStateMachine::_initAbortSource(
+CancellationToken GlobalIndexCloningService::CloningStateMachine::_initCleanupToken(
const CancellationToken& stepdownToken) {
- {
+ auto cleanupCalled = ([&] {
stdx::lock_guard<Latch> lk(_mutex);
- _abortSource = CancellationSource(stepdownToken);
- }
+ _cleanupSignalSource = CancellationSource(stepdownToken);
+
+ return _cleanupCalled;
+ })();
- // TODO: SERVER-67563 Handle possible race between _initAbortSource and abort
+ if (cleanupCalled) {
+ // cleanup request came in before _cleanupSignalSource was initialized,
+ // so we should cancel it.
+ _cleanupSignalSource->cancel();
+ }
- return _abortSource->token();
+ return _cleanupSignalSource->token();
}
ExecutorFuture<void> GlobalIndexCloningService::CloningStateMachine::_persistStateDocument(
@@ -417,13 +449,21 @@ void GlobalIndexCloningService::CloningStateMachine::_ensureCollection(Operation
}
void GlobalIndexCloningService::CloningStateMachine::cleanup() {
- stdx::unique_lock lk(_mutex);
+ auto cleanupSource = ([&] {
+ stdx::lock_guard lk(_mutex);
- if (!_waitForCleanupPromise.getFuture().isReady()) {
- _waitForCleanupPromise.emplaceValue();
- }
+ if (!_waitForCleanupPromise.getFuture().isReady()) {
+ _waitForCleanupPromise.emplaceValue();
+ }
- // TODO: SERVER-67563 Implement abort
+ _cleanupCalled = true;
+
+ return _cleanupSignalSource;
+ })();
+
+ if (cleanupSource) {
+ cleanupSource->cancel();
+ }
}
GlobalIndexClonerStateEnum GlobalIndexCloningService::CloningStateMachine::_getState() const {
diff --git a/src/mongo/db/s/global_index/global_index_cloning_service.h b/src/mongo/db/s/global_index/global_index_cloning_service.h
index 67d3ecdc456..7ee684d9c8a 100644
--- a/src/mongo/db/s/global_index/global_index_cloning_service.h
+++ b/src/mongo/db/s/global_index/global_index_cloning_service.h
@@ -82,7 +82,7 @@ class GlobalIndexCloningService::CloningStateMachine final
public:
CloningStateMachine(
ServiceContext* service,
- const GlobalIndexCloningService* cloningService,
+ GlobalIndexCloningService* cloningService,
std::unique_ptr<GlobalIndexCloningService::CloningExternalState> externalState,
std::unique_ptr<GlobalIndexClonerFetcherFactoryInterface> fetcherFactory,
GlobalIndexClonerDoc clonerDoc);
@@ -92,8 +92,6 @@ public:
void interrupt(Status status) override;
- void abort();
-
SharedSemiFuture<void> getReadyToCommitFuture() const {
return _readyToCommitPromise.getFuture();
}
@@ -111,11 +109,6 @@ public:
MongoProcessInterface::CurrentOpSessionsMode) noexcept override;
/**
- * Initiates the cancellation of the cloning operation.
- */
- void abort(bool isUserCancelled);
-
- /**
* Tells this cloner to perform cleanup. This can cause this cloner to abort if it is still
* running.
*/
@@ -128,7 +121,7 @@ private:
* Initializes the _abortSource and generates a token from it to return back the caller.
* Should only be called once per lifetime.
*/
- CancellationToken _initAbortSource(const CancellationToken& stepdownToken);
+ CancellationToken _initCleanupToken(const CancellationToken& stepdownToken);
/**
* Initializes the necessary components.
@@ -169,7 +162,7 @@ private:
/**
* Removes the side collections created by this cloner.
*/
- ExecutorFuture<void> _cleanup(std::shared_ptr<executor::ScopedTaskExecutor> executor,
+ ExecutorFuture<void> _cleanup(std::shared_ptr<executor::TaskExecutor> executor,
const CancellationToken& stepdownToken);
/**
@@ -205,7 +198,7 @@ private:
ServiceContext* const _serviceContext; // (TS)
// The primary-only service instance corresponding to the cloner instance. Not owned.
- const GlobalIndexCloningService* const _cloningService; // (TS)
+ GlobalIndexCloningService* const _cloningService; // (TS)
const UUID _indexCollectionUUID;
const NamespaceString _sourceNss;
@@ -228,7 +221,11 @@ private:
GlobalIndexClonerMutableState _mutableState; // (NC)
// Canceled when there is an unrecoverable error or stepdown.
- boost::optional<CancellationSource> _abortSource; // (M)
+ boost::optional<CancellationSource> _cleanupSignalSource; // (M)
+
+ // True if cleanup was called. Main purpose is to act as a stand-in when _abortSource
+ // is not yet initialized.
+ bool _cleanupCalled{false}; // (M)
std::unique_ptr<GlobalIndexClonerFetcherFactoryInterface> _fetcherFactory; // (TS)
std::unique_ptr<GlobalIndexClonerFetcherInterface> _fetcher; // (NC)
diff --git a/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp b/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp
index 0eb66087f6f..98c6adeacd5 100644
--- a/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp
+++ b/src/mongo/db/s/global_index/global_index_cloning_service_test.cpp
@@ -355,7 +355,7 @@ TEST_F(GlobalIndexClonerServiceTest, ShouldBeSafeToRetryOnStepDown) {
auto prevState = GlobalIndexClonerStateEnum::kUnused;
for (const auto& nextState : states) {
LOGV2(6870601,
- "Testing next state",
+ "Testing step down prior to state",
"state"_attr = GlobalIndexClonerState_serializer(nextState));
auto cloner = ([&] {
@@ -446,6 +446,35 @@ TEST_F(GlobalIndexClonerServiceTest, ShouldWorkWithEmptyCollection) {
checkIndexCollection(rawOpCtx);
}
+TEST_F(GlobalIndexClonerServiceTest, CleanupBeforeReadyResultsInAbort) {
+ auto doc = makeStateDocument();
+ auto opCtx = makeOperationContext();
+ auto rawOpCtx = opCtx.get();
+
+ const std::vector<GlobalIndexClonerStateEnum> states{
+ GlobalIndexClonerStateEnum::kCloning, GlobalIndexClonerStateEnum::kReadyToCommit};
+ PauseDuringStateTransitions stateTransitionsGuard{stateTransitionController(), states};
+
+ for (const auto& nextState : states) {
+ LOGV2(6756300,
+ "Testing cleanup abort",
+ "state"_attr = GlobalIndexClonerState_serializer(nextState));
+
+ auto cloner = GlobalIndexStateMachine::getOrCreate(rawOpCtx, _service, doc.toBSON());
+ auto readyToCommitFuture = cloner->getReadyToCommitFuture();
+ auto completionFuture = cloner->getCompletionFuture();
+
+ stateTransitionsGuard.wait(nextState);
+
+ cloner->cleanup();
+
+ ASSERT_THROWS(readyToCommitFuture.get(), DBException);
+ completionFuture.get();
+
+ stateTransitionsGuard.unset(nextState);
+ }
+}
+
} // namespace
} // namespace global_index
} // namespace mongo