summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2022-09-06 13:52:07 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-09-06 15:01:29 +0000
commit980b6ffc3d242afc1fc6ad07526014c218c6d3f1 (patch)
treef07fbcc2936cf7f505f29832930f88d54046b640 /src
parentad49775116690643d21e7b3e519439faa3e829f9 (diff)
downloadmongo-980b6ffc3d242afc1fc6ad07526014c218c6d3f1.tar.gz
SERVER-69431 Get rid of the "non-locking" constructor of ResourceLock
Diffstat (limited to 'src')
-rw-r--r--src/mongo/db/commands/sleep_command.cpp9
-rw-r--r--src/mongo/db/concurrency/d_concurrency.cpp21
-rw-r--r--src/mongo/db/concurrency/d_concurrency.h19
-rw-r--r--src/mongo/db/concurrency/d_concurrency_test.cpp19
4 files changed, 32 insertions, 36 deletions
diff --git a/src/mongo/db/commands/sleep_command.cpp b/src/mongo/db/commands/sleep_command.cpp
index 156615b2f4c..0ad5a7c5b99 100644
--- a/src/mongo/db/commands/sleep_command.cpp
+++ b/src/mongo/db/commands/sleep_command.cpp
@@ -125,15 +125,16 @@ public:
}
void _sleepInPBWM(mongo::OperationContext* opCtx, long long millis) {
- Lock::ResourceLock pbwm(opCtx->lockState(), resourceIdParallelBatchWriterMode);
- pbwm.lock(nullptr, MODE_X);
+ Lock::ResourceLock pbwm(
+ opCtx, opCtx->lockState(), resourceIdParallelBatchWriterMode, MODE_X);
+ LOGV2(6001604, "PBWM MODE_X lock acquired by sleep command.");
opCtx->sleepFor(Milliseconds(millis));
pbwm.unlock();
}
void _sleepInRSTL(mongo::OperationContext* opCtx, long long millis) {
- Lock::ResourceLock rstl(opCtx->lockState(), resourceIdReplicationStateTransitionLock);
- rstl.lock(nullptr, MODE_X);
+ Lock::ResourceLock rstl(
+ opCtx, opCtx->lockState(), resourceIdReplicationStateTransitionLock, MODE_X);
LOGV2(6001600, "RSTL MODE_X lock acquired by sleep command.");
opCtx->sleepFor(Milliseconds(millis));
rstl.unlock();
diff --git a/src/mongo/db/concurrency/d_concurrency.cpp b/src/mongo/db/concurrency/d_concurrency.cpp
index b20e9ba2b9f..04a8c688405 100644
--- a/src/mongo/db/concurrency/d_concurrency.cpp
+++ b/src/mongo/db/concurrency/d_concurrency.cpp
@@ -27,9 +27,6 @@
* it in the license file.
*/
-
-#include "mongo/platform/basic.h"
-
#include "mongo/db/concurrency/d_concurrency.h"
#include <memory>
@@ -48,9 +45,7 @@
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault
-
namespace mongo {
-
namespace {
/**
@@ -133,9 +128,6 @@ Lock::GlobalLock::GlobalLock(OperationContext* opCtx,
InterruptBehavior behavior,
bool skipRSTLLock)
: _opCtx(opCtx),
- _result(LOCK_INVALID),
- _pbwm(opCtx->lockState(), resourceIdParallelBatchWriterMode),
- _fcvLock(opCtx->lockState(), resourceIdFeatureCompatibilityVersion),
_interruptBehavior(behavior),
_skipRSTLLock(skipRSTLLock),
_isOutermostLock(!opCtx->lockState()->isLocked()) {
@@ -143,20 +135,25 @@ Lock::GlobalLock::GlobalLock(OperationContext* opCtx,
try {
if (_opCtx->lockState()->shouldConflictWithSecondaryBatchApplication()) {
- _pbwm.lock(opCtx, MODE_IS, deadline);
+ _pbwm.emplace(
+ opCtx, opCtx->lockState(), resourceIdParallelBatchWriterMode, MODE_IS, deadline);
}
ScopeGuard unlockPBWM([this] {
if (_opCtx->lockState()->shouldConflictWithSecondaryBatchApplication()) {
- _pbwm.unlock();
+ _pbwm.reset();
}
});
if (_opCtx->lockState()->shouldConflictWithSetFeatureCompatibilityVersion()) {
- _fcvLock.lock(_opCtx, isSharedLockMode(lockMode) ? MODE_IS : MODE_IX, deadline);
+ _fcvLock.emplace(_opCtx,
+ opCtx->lockState(),
+ resourceIdFeatureCompatibilityVersion,
+ isSharedLockMode(lockMode) ? MODE_IS : MODE_IX,
+ deadline);
}
ScopeGuard unlockFCVLock([this] {
if (_opCtx->lockState()->shouldConflictWithSetFeatureCompatibilityVersion()) {
- _fcvLock.unlock();
+ _fcvLock.reset();
}
});
diff --git a/src/mongo/db/concurrency/d_concurrency.h b/src/mongo/db/concurrency/d_concurrency.h
index df64a741e59..ae8501303a7 100644
--- a/src/mongo/db/concurrency/d_concurrency.h
+++ b/src/mongo/db/concurrency/d_concurrency.h
@@ -54,15 +54,16 @@ public:
ResourceLock& operator=(const ResourceLock&) = delete;
public:
- ResourceLock(Locker* locker, ResourceId rid)
- : _rid(rid), _locker(locker), _result(LOCK_INVALID) {}
-
ResourceLock(Locker* locker, ResourceId rid, LockMode mode)
: ResourceLock(nullptr, locker, rid, mode) {}
- ResourceLock(OperationContext* opCtx, Locker* locker, ResourceId rid, LockMode mode)
+ ResourceLock(OperationContext* opCtx,
+ Locker* locker,
+ ResourceId rid,
+ LockMode mode,
+ Date_t deadline = Date_t::max())
: _rid(rid), _locker(locker), _result(LOCK_INVALID) {
- lock(opCtx, mode);
+ lock(opCtx, mode, deadline);
}
ResourceLock(ResourceLock&& otherLock)
@@ -250,9 +251,11 @@ public:
void _unlock();
OperationContext* const _opCtx;
- LockResult _result;
- ResourceLock _pbwm;
- ResourceLock _fcvLock;
+ LockResult _result{LOCK_INVALID};
+
+ boost::optional<ResourceLock> _pbwm;
+ boost::optional<ResourceLock> _fcvLock;
+
InterruptBehavior _interruptBehavior;
bool _skipRSTLLock;
const bool _isOutermostLock;
diff --git a/src/mongo/db/concurrency/d_concurrency_test.cpp b/src/mongo/db/concurrency/d_concurrency_test.cpp
index d86c861f259..22c638c811f 100644
--- a/src/mongo/db/concurrency/d_concurrency_test.cpp
+++ b/src/mongo/db/concurrency/d_concurrency_test.cpp
@@ -27,9 +27,6 @@
* it in the license file.
*/
-
-#include "mongo/platform/basic.h"
-
#include <functional>
#include <memory>
#include <string>
@@ -53,7 +50,6 @@
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest
-
namespace mongo {
namespace {
@@ -2338,18 +2334,17 @@ TEST_F(DConcurrencyTestFixture, FailPointInLockDoesNotFailUninterruptibleNonInte
TEST_F(DConcurrencyTestFixture, PBWMRespectsMaxTimeMS) {
auto clientOpCtxPairs = makeKClientsWithLockers(2);
- auto opCtx1 = clientOpCtxPairs[0].second.get();
- auto opCtx2 = clientOpCtxPairs[1].second.get();
- Lock::ResourceLock pbwm1(opCtx1->lockState(), resourceIdParallelBatchWriterMode);
- pbwm1.lock(nullptr, MODE_X);
+ auto opCtx1 = clientOpCtxPairs[0].second.get();
+ Lock::ResourceLock pbwm1(
+ opCtx1, opCtx1->lockState(), resourceIdParallelBatchWriterMode, MODE_X);
+ auto opCtx2 = clientOpCtxPairs[1].second.get();
opCtx2->setDeadlineAfterNowBy(Seconds{1}, ErrorCodes::ExceededTimeLimit);
-
- Lock::ResourceLock pbwm2(opCtx2->lockState(), resourceIdParallelBatchWriterMode);
-
ASSERT_THROWS_CODE(
- pbwm2.lock(opCtx2, MODE_X), AssertionException, ErrorCodes::ExceededTimeLimit);
+ Lock::ResourceLock(opCtx2, opCtx2->lockState(), resourceIdParallelBatchWriterMode, MODE_X),
+ AssertionException,
+ ErrorCodes::ExceededTimeLimit);
}
TEST_F(DConcurrencyTestFixture, DifferentTenantsTakeDBLockOnConflictingNamespaceOk) {