diff options
author | Amirsaman Memaripour <amirsaman.memaripour@mongodb.com> | 2020-08-03 14:56:59 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-08-04 17:35:01 +0000 |
commit | b4db8c01a13fd70997a05857be17548b0adec020 (patch) | |
tree | 6f64b62a3b85fa0bb608f98dbad69d20bbe2a926 | |
parent | 45bed9f6033a9c0df0cc54243e4a24aa9ff8bd2a (diff) | |
download | mongo-b4db8c01a13fd70997a05857be17548b0adec020.tar.gz |
SERVER-50064 Make recursion depth thread-local in ServiceExecutor tests
-rw-r--r-- | src/mongo/transport/service_executor_fixed.cpp | 5 | ||||
-rw-r--r-- | src/mongo/transport/service_executor_fixed.h | 6 | ||||
-rw-r--r-- | 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<int>(_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<ServiceExecutorFixed> _executor; }; - - auto makeRecursionGuard() { - _recursionDepth.fetchAndAdd(1); - return makeGuard([this] { _recursionDepth.fetchAndSubtract(1); }); - } - - auto getRecursionDepth() const { - return _recursionDepth.load(); - } - -private: - AtomicWord<int> _recursionDepth{0}; }; TEST_F(ServiceExecutorFixedFixture, ScheduleFailsBeforeStartup) { @@ -251,8 +239,8 @@ TEST_F(ServiceExecutorFixedFixture, RecursiveTask) { std::function<void()> 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<void()> 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 { |