From b4db8c01a13fd70997a05857be17548b0adec020 Mon Sep 17 00:00:00 2001 From: Amirsaman Memaripour Date: Mon, 3 Aug 2020 14:56:59 +0000 Subject: SERVER-50064 Make recursion depth thread-local in ServiceExecutor tests --- src/mongo/transport/service_executor_fixed.cpp | 5 +++++ src/mongo/transport/service_executor_fixed.h | 6 ++++++ src/mongo/transport/service_executor_test.cpp | 19 +++---------------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/mongo/transport/service_executor_fixed.cpp b/src/mongo/transport/service_executor_fixed.cpp index abdd99ca586..495a59887d7 100644 --- a/src/mongo/transport/service_executor_fixed.cpp +++ b/src/mongo/transport/service_executor_fixed.cpp @@ -162,5 +162,10 @@ void ServiceExecutorFixed::appendStats(BSONObjBuilder* bob) const { << static_cast(_numRunningExecutorThreads.load()); } +int ServiceExecutorFixed::getRecursionDepthForExecutorThread() const { + invariant(_executorContext); + return _executorContext->getRecursionDepth(); +} + } // namespace transport } // namespace mongo diff --git a/src/mongo/transport/service_executor_fixed.h b/src/mongo/transport/service_executor_fixed.h index b7a90d43447..9aae4ab1718 100644 --- a/src/mongo/transport/service_executor_fixed.h +++ b/src/mongo/transport/service_executor_fixed.h @@ -67,6 +67,12 @@ public: void appendStats(BSONObjBuilder* bob) const override; + /** + * Returns the recursion depth of the active executor thread. + * It is forbidden to invoke this method outside scheduled tasks. + */ + int getRecursionDepthForExecutorThread() const; + private: // Maintains the execution state (e.g., recursion depth) for executor threads class ExecutorThreadContext { diff --git a/src/mongo/transport/service_executor_test.cpp b/src/mongo/transport/service_executor_test.cpp index c1d2b2b5f7e..608b0a09232 100644 --- a/src/mongo/transport/service_executor_test.cpp +++ b/src/mongo/transport/service_executor_test.cpp @@ -213,18 +213,6 @@ public: const bool _skipShutdown; std::shared_ptr _executor; }; - - auto makeRecursionGuard() { - _recursionDepth.fetchAndAdd(1); - return makeGuard([this] { _recursionDepth.fetchAndSubtract(1); }); - } - - auto getRecursionDepth() const { - return _recursionDepth.load(); - } - -private: - AtomicWord _recursionDepth{0}; }; TEST_F(ServiceExecutorFixedFixture, ScheduleFailsBeforeStartup) { @@ -251,8 +239,8 @@ TEST_F(ServiceExecutorFixedFixture, RecursiveTask) { std::function recursiveTask; recursiveTask = [&, barrier, executor = *executorHandle] { - auto recursionGuard = makeRecursionGuard(); - if (getRecursionDepth() < fixedServiceExecutorRecursionLimit.load()) { + if (executor->getRecursionDepthForExecutorThread() < + fixedServiceExecutorRecursionLimit.load()) { ASSERT_OK(executor->scheduleTask(recursiveTask, ServiceExecutor::kMayRecurse)); } else { // This test never returns unless the service executor can satisfy the recursion depth. @@ -275,8 +263,7 @@ TEST_F(ServiceExecutorFixedFixture, FlattenRecursiveScheduledTasks) { // recursion depth remains zero during its execution. std::function recursiveTask; recursiveTask = [&, barrier, executor = *executorHandle] { - auto recursionGuard = makeRecursionGuard(); - ASSERT_EQ(getRecursionDepth(), 1); + ASSERT_EQ(executor->getRecursionDepthForExecutorThread(), 1); if (tasksToSchedule.fetchAndSubtract(1) > 0) { ASSERT_OK(executor->scheduleTask(recursiveTask, ServiceExecutor::kEmptyFlags)); } else { -- cgit v1.2.1