diff options
Diffstat (limited to 'src/mongo/db/repl')
21 files changed, 106 insertions, 156 deletions
diff --git a/src/mongo/db/repl/bgsync.cpp b/src/mongo/db/repl/bgsync.cpp index c0211a21022..86cc962c926 100644 --- a/src/mongo/db/repl/bgsync.cpp +++ b/src/mongo/db/repl/bgsync.cpp @@ -660,8 +660,7 @@ OpTimeWithHash BackgroundSync::_readLastAppliedOpTimeWithHash(OperationContext* BSONObj oplogEntry; try { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock lk(opCtx->lockState(), "local", MODE_X); + Lock::DBLock lk(opCtx, "local", MODE_X); bool success = Helpers::getLast(opCtx, rsOplogName.c_str(), oplogEntry); if (!success) { // This can happen when we are to do an initial sync. lastHash will be set diff --git a/src/mongo/db/repl/database_task.cpp b/src/mongo/db/repl/database_task.cpp index 5c4f9422ea9..41f29aff342 100644 --- a/src/mongo/db/repl/database_task.cpp +++ b/src/mongo/db/repl/database_task.cpp @@ -46,8 +46,7 @@ DatabaseTask::Task DatabaseTask::makeGlobalExclusiveLockTask(const Task& task) { return task(opCtx, status); } MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lock(opCtx->lockState()); + Lock::GlobalWrite lock(opCtx); return task(opCtx, status); } MONGO_WRITE_CONFLICT_RETRY_LOOP_END(opCtx, "globalExclusiveLockTask", "global"); @@ -66,9 +65,7 @@ DatabaseTask::Task DatabaseTask::makeDatabaseLockTask(const Task& task, return task(opCtx, status); } MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - LockMode permissiveLockMode = isSharedLockMode(mode) ? MODE_IS : MODE_IX; - ScopedTransaction transaction(opCtx, permissiveLockMode); - Lock::DBLock lock(opCtx->lockState(), databaseName, mode); + Lock::DBLock lock(opCtx, databaseName, mode); return task(opCtx, status); } MONGO_WRITE_CONFLICT_RETRY_LOOP_END(opCtx, "databaseLockTask", databaseName); @@ -88,8 +85,7 @@ DatabaseTask::Task DatabaseTask::makeCollectionLockTask(const Task& task, } MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { LockMode permissiveLockMode = isSharedLockMode(mode) ? MODE_IS : MODE_IX; - ScopedTransaction transaction(opCtx, permissiveLockMode); - Lock::DBLock lock(opCtx->lockState(), nss.db(), permissiveLockMode); + Lock::DBLock lock(opCtx, nss.db(), permissiveLockMode); Lock::CollectionLock collectionLock(opCtx->lockState(), nss.toString(), mode); return task(opCtx, status); } diff --git a/src/mongo/db/repl/master_slave.cpp b/src/mongo/db/repl/master_slave.cpp index f76856d2802..8b11676d9b1 100644 --- a/src/mongo/db/repl/master_slave.cpp +++ b/src/mongo/db/repl/master_slave.cpp @@ -196,8 +196,7 @@ void ReplSource::ensureMe(OperationContext* opCtx) { bool exists = Helpers::getSingleton(opCtx, "local.me", _me); if (!exists || !_me.hasField("host") || _me["host"].String() != myname) { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dblk(opCtx->lockState(), "local", MODE_X); + Lock::DBLock dblk(opCtx, "local", MODE_X); WriteUnitOfWork wunit(opCtx); // clean out local.me Helpers::emptyCollection(opCtx, "local.me"); @@ -771,7 +770,7 @@ void ReplSource::_sync_pullOpLog_applyOperation(OperationContext* opCtx, } } - unique_ptr<Lock::GlobalWrite> lk(alreadyLocked ? 0 : new Lock::GlobalWrite(opCtx->lockState())); + unique_ptr<Lock::GlobalWrite> lk(alreadyLocked ? 0 : new Lock::GlobalWrite(opCtx)); if (replAllDead) { // hmmm why is this check here and not at top of this function? does it get set between top @@ -914,8 +913,7 @@ int ReplSource::_sync_pullOpLog(OperationContext* opCtx, int& nApplied) { } // obviously global isn't ideal, but non-repl set is old so // keeping it simple - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); save(opCtx); } @@ -977,8 +975,7 @@ int ReplSource::_sync_pullOpLog(OperationContext* opCtx, int& nApplied) { log() << ns << " oplog is empty" << endl; } { - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); save(opCtx); } return okResultCode; @@ -1045,8 +1042,7 @@ int ReplSource::_sync_pullOpLog(OperationContext* opCtx, int& nApplied) { const bool moreInitialSyncsPending = !addDbNextPass.empty() && n; if (moreInitialSyncsPending || !oplogReader.more()) { - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); if (tailing) { okResultCode = restartSync; // don't sleep @@ -1060,8 +1056,7 @@ int ReplSource::_sync_pullOpLog(OperationContext* opCtx, int& nApplied) { OCCASIONALLY if (n > 0 && (n > 100000 || time(0) - saveLast > 60)) { // periodically note our progress, in case we are doing a lot of work and crash - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); syncedTo = nextOpTime; // can't update local log ts since there are pending operations from our peer save(opCtx); @@ -1075,8 +1070,7 @@ int ReplSource::_sync_pullOpLog(OperationContext* opCtx, int& nApplied) { int b = replApplyBatchSize.load(); bool justOne = b == 1; - unique_ptr<Lock::GlobalWrite> lk(justOne ? 0 - : new Lock::GlobalWrite(opCtx->lockState())); + unique_ptr<Lock::GlobalWrite> lk(justOne ? 0 : new Lock::GlobalWrite(opCtx)); while (1) { BSONElement ts = op.getField("ts"); if (!(ts.type() == Date || ts.type() == bsonTimestamp)) { @@ -1108,8 +1102,7 @@ int ReplSource::_sync_pullOpLog(OperationContext* opCtx, int& nApplied) { oplogReader.putBack(op); _sleepAdviceTime = nextOpTime.getSecs() + durationCount<Seconds>(replSettings.getSlaveDelaySecs()) + 1; - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); if (n > 0) { syncedTo = last; save(opCtx); @@ -1191,8 +1184,7 @@ _ reuse that cursor when we can int _replMain(OperationContext* opCtx, ReplSource::SourceVector& sources, int& nApplied) { { ReplInfo r("replMain load sources"); - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); ReplSource::loadAll(opCtx, sources); // only need this param for initial reset @@ -1254,8 +1246,7 @@ static void replMain(OperationContext* opCtx) { while (1) { auto s = restartSync; { - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); if (replAllDead) { // throttledForceResyncDead can throw if (!getGlobalReplicationCoordinator()->getSettings().isAutoResyncEnabled() || @@ -1288,8 +1279,7 @@ static void replMain(OperationContext* opCtx) { } { - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); invariant(syncing.swap(0) == 1); } @@ -1324,7 +1314,7 @@ static void replMasterThread() { OperationContext& opCtx = *opCtxPtr; AuthorizationSession::get(opCtx.getClient())->grantInternalAuthorization(); - Lock::GlobalWrite globalWrite(opCtx.lockState(), 1); + Lock::GlobalWrite globalWrite(&opCtx, 1); if (globalWrite.isLocked()) { toSleep = 10; @@ -1405,9 +1395,8 @@ void pretouchN(vector<BSONObj>& v, unsigned a, unsigned b) { Client::initThreadIfNotAlready("pretouchN"); const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); - OperationContext& opCtx = *opCtxPtr; - ScopedTransaction transaction(&opCtx, MODE_S); - Lock::GlobalRead lk(opCtx.lockState()); + OperationContext& opCtx = *opCtxPtr; // XXX + Lock::GlobalRead lk(&opCtx); for (unsigned i = a; i <= b; i++) { const BSONObj& op = v[i]; @@ -1464,7 +1453,7 @@ void pretouchOperation(OperationContext* opCtx, const BSONObj& op) { BSONObjBuilder b; b.append(_id); BSONObj result; - AutoGetCollectionForRead ctx(opCtx, NamespaceString(ns)); + AutoGetCollectionForReadCommand ctx(opCtx, NamespaceString(ns)); if (Helpers::findById(opCtx, ctx.getDb(), ns, b.done(), result)) { _dummy_z += result.objsize(); // touch } diff --git a/src/mongo/db/repl/noop_writer.cpp b/src/mongo/db/repl/noop_writer.cpp index f127b236614..926db4aff07 100644 --- a/src/mongo/db/repl/noop_writer.cpp +++ b/src/mongo/db/repl/noop_writer.cpp @@ -31,6 +31,7 @@ #include "mongo/platform/basic.h" #include "mongo/db/commands.h" +#include "mongo/db/concurrency/d_concurrency.h" #include "mongo/db/concurrency/write_conflict_exception.h" #include "mongo/db/curop.h" #include "mongo/db/op_observer.h" @@ -136,10 +137,9 @@ void NoopWriter::stopWritingPeriodicNoops() { } void NoopWriter::_writeNoop(OperationContext* opCtx) { - ScopedTransaction transaction(opCtx, MODE_IX); // Use GlobalLock + lockMMAPV1Flush instead of DBLock to allow return when the lock is not // available. It may happen when the primary steps down and a shared global lock is acquired. - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx, MODE_IX, 1); if (!lock.isLocked()) { LOG(1) << "Global lock is not available skipping noopWrite"; return; diff --git a/src/mongo/db/repl/oplog.cpp b/src/mongo/db/repl/oplog.cpp index 3d5547ed526..258180af98a 100644 --- a/src/mongo/db/repl/oplog.cpp +++ b/src/mongo/db/repl/oplog.cpp @@ -298,7 +298,6 @@ OplogDocWriter _logOpWriter(OperationContext* opCtx, // Truncates the oplog after and including the "truncateTimestamp" entry. void truncateOplogTo(OperationContext* opCtx, Timestamp truncateTimestamp) { const NamespaceString oplogNss(rsOplogName); - ScopedTransaction transaction(opCtx, MODE_IX); AutoGetDb autoDb(opCtx, oplogNss.db(), MODE_IX); Lock::CollectionLock oplogCollectionLoc(opCtx->lockState(), oplogNss.ns(), MODE_X); Collection* oplogCollection = autoDb.getDb()->getCollection(oplogNss); @@ -402,7 +401,7 @@ void logOp(OperationContext* opCtx, ReplicationCoordinator* replCoord = getGlobalReplicationCoordinator(); Collection* oplog = getLocalOplogCollection(opCtx, _oplogCollectionName); - Lock::DBLock lk(opCtx->lockState(), "local", MODE_IX); + Lock::DBLock lk(opCtx, "local", MODE_IX); Lock::CollectionLock lock(opCtx->lockState(), _oplogCollectionName, MODE_IX); OplogSlot slot; getNextOpTime(opCtx, oplog, replCoord, replMode, 1, &slot); @@ -428,7 +427,7 @@ void logOps(OperationContext* opCtx, std::vector<OplogDocWriter> writers; writers.reserve(count); Collection* oplog = getLocalOplogCollection(opCtx, _oplogCollectionName); - Lock::DBLock lk(opCtx->lockState(), "local", MODE_IX); + Lock::DBLock lk(opCtx, "local", MODE_IX); Lock::CollectionLock lock(opCtx->lockState(), _oplogCollectionName, MODE_IX); std::unique_ptr<OplogSlot[]> slots(new OplogSlot[count]); getNextOpTime(opCtx, oplog, replCoord, replMode, count, slots.get()); @@ -490,8 +489,7 @@ long long getNewOplogSizeBytes(OperationContext* opCtx, const ReplSettings& repl } // namespace void createOplog(OperationContext* opCtx, const std::string& oplogCollectionName, bool isReplSet) { - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx); const ReplSettings& replSettings = ReplicationCoordinator::get(opCtx)->getSettings(); @@ -1221,7 +1219,7 @@ void SnapshotThread::run() { try { auto opCtx = client.makeOperationContext(); - Lock::GlobalLock globalLock(opCtx->lockState(), MODE_IS, UINT_MAX); + Lock::GlobalLock globalLock(opCtx.get(), MODE_IS, UINT_MAX); if (!replCoord->getMemberState().readable()) { // If our MemberState isn't readable, we may not be in a consistent state so don't @@ -1253,7 +1251,7 @@ void SnapshotThread::run() { auto opTimeOfSnapshot = OpTime(); { - AutoGetCollectionForRead oplog(opCtx.get(), NamespaceString(rsOplogName)); + AutoGetCollectionForReadCommand oplog(opCtx.get(), NamespaceString(rsOplogName)); invariant(oplog.getCollection()); // Read the latest op from the oplog. auto cursor = oplog.getCollection()->getCursor(opCtx.get(), /*forward*/ false); diff --git a/src/mongo/db/repl/oplog_buffer_collection_test.cpp b/src/mongo/db/repl/oplog_buffer_collection_test.cpp index 1ee2c3ccc8c..3e7d172b7c8 100644 --- a/src/mongo/db/repl/oplog_buffer_collection_test.cpp +++ b/src/mongo/db/repl/oplog_buffer_collection_test.cpp @@ -71,8 +71,8 @@ void OplogBufferCollectionTest::setUp() { ServiceContextMongoDTest::setUp(); auto service = getServiceContext(); - // AutoGetCollectionForRead requires a valid replication coordinator in order to check the shard - // version. + // AutoGetCollectionForReadCommand requires a valid replication coordinator in order to check + // the shard version. ReplSettings replSettings; replSettings.setOplogSizeBytes(5 * 1024 * 1024); ReplicationCoordinator::set( @@ -137,10 +137,10 @@ void testStartupCreatesCollection(OperationContext* opCtx, OplogBufferCollection oplogBuffer(storageInterface, nss); // Collection should not exist until startup() is called. - ASSERT_FALSE(AutoGetCollectionForRead(opCtx, nss).getCollection()); + ASSERT_FALSE(AutoGetCollectionForReadCommand(opCtx, nss).getCollection()); oplogBuffer.startup(opCtx); - ASSERT_TRUE(AutoGetCollectionForRead(opCtx, nss).getCollection()); + ASSERT_TRUE(AutoGetCollectionForReadCommand(opCtx, nss).getCollection()); } TEST_F(OplogBufferCollectionTest, StartupWithDefaultNamespaceCreatesCollection) { @@ -158,7 +158,7 @@ TEST_F(OplogBufferCollectionTest, StartupDropsExistingCollectionBeforeCreatingNe ASSERT_OK(_storageInterface->createCollection(_opCtx.get(), nss, CollectionOptions())); OplogBufferCollection oplogBuffer(_storageInterface, nss); oplogBuffer.startup(_opCtx.get()); - ASSERT_TRUE(AutoGetCollectionForRead(_opCtx.get(), nss).getCollection()); + ASSERT_TRUE(AutoGetCollectionForReadCommand(_opCtx.get(), nss).getCollection()); } DEATH_TEST_F(OplogBufferCollectionTest, @@ -172,9 +172,9 @@ TEST_F(OplogBufferCollectionTest, ShutdownDropsCollection) { OplogBufferCollection oplogBuffer(_storageInterface, nss); oplogBuffer.startup(_opCtx.get()); - ASSERT_TRUE(AutoGetCollectionForRead(_opCtx.get(), nss).getCollection()); + ASSERT_TRUE(AutoGetCollectionForReadCommand(_opCtx.get(), nss).getCollection()); oplogBuffer.shutdown(_opCtx.get()); - ASSERT_FALSE(AutoGetCollectionForRead(_opCtx.get(), nss).getCollection()); + ASSERT_FALSE(AutoGetCollectionForReadCommand(_opCtx.get(), nss).getCollection()); } TEST_F(OplogBufferCollectionTest, extractEmbeddedOplogDocumentChangesIdToTimestamp) { @@ -542,7 +542,7 @@ TEST_F(OplogBufferCollectionTest, ClearClearsCollection) { _assertDocumentsInCollectionEquals(_opCtx.get(), nss, {oplog, sentinel, oplog2}); oplogBuffer.clear(_opCtx.get()); - ASSERT_TRUE(AutoGetCollectionForRead(_opCtx.get(), nss).getCollection()); + ASSERT_TRUE(AutoGetCollectionForReadCommand(_opCtx.get(), nss).getCollection()); ASSERT_EQUALS(oplogBuffer.getCount(), 0UL); ASSERT_EQUALS(oplogBuffer.getSize(), 0UL); ASSERT_EQUALS(0U, oplogBuffer.getSentinelCount_forTest()); diff --git a/src/mongo/db/repl/oplog_interface_local.cpp b/src/mongo/db/repl/oplog_interface_local.cpp index a68fd8db0d6..c27e8ea798e 100644 --- a/src/mongo/db/repl/oplog_interface_local.cpp +++ b/src/mongo/db/repl/oplog_interface_local.cpp @@ -48,7 +48,6 @@ public: StatusWith<Value> next() override; private: - ScopedTransaction _transaction; Lock::DBLock _dbLock; Lock::CollectionLock _collectionLock; OldClientContext _ctx; @@ -56,8 +55,7 @@ private: }; OplogIteratorLocal::OplogIteratorLocal(OperationContext* opCtx, const std::string& collectionName) - : _transaction(opCtx, MODE_IS), - _dbLock(opCtx->lockState(), nsToDatabase(collectionName), MODE_IS), + : _dbLock(opCtx, nsToDatabase(collectionName), MODE_IS), _collectionLock(opCtx->lockState(), collectionName, MODE_S), _ctx(opCtx, collectionName), _exec(InternalPlanner::collectionScan(opCtx, diff --git a/src/mongo/db/repl/repl_set_commands.cpp b/src/mongo/db/repl/repl_set_commands.cpp index ab756559784..dec36e4d1af 100644 --- a/src/mongo/db/repl/repl_set_commands.cpp +++ b/src/mongo/db/repl/repl_set_commands.cpp @@ -429,8 +429,7 @@ public: status = getGlobalReplicationCoordinator()->processReplSetReconfig(opCtx, parsedArgs, &result); - ScopedTransaction scopedXact(opCtx, MODE_X); - Lock::GlobalWrite globalWrite(opCtx->lockState()); + Lock::GlobalWrite globalWrite(opCtx); WriteUnitOfWork wuow(opCtx); if (status.isOK() && !parsedArgs.force) { diff --git a/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp b/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp index c2619816034..e85ec3c8095 100644 --- a/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp +++ b/src/mongo/db/repl/replication_coordinator_external_state_impl.cpp @@ -365,9 +365,8 @@ OldThreadPool* ReplicationCoordinatorExternalStateImpl::getDbWorkThreadPool() co Status ReplicationCoordinatorExternalStateImpl::runRepairOnLocalDB(OperationContext* opCtx) { try { - ScopedTransaction scopedXact(opCtx, MODE_X); - Lock::GlobalWrite globalWrite(opCtx->lockState()); - StorageEngine* engine = _service->getGlobalStorageEngine(); + Lock::GlobalWrite globalWrite(opCtx); + StorageEngine* engine = getGlobalServiceContext()->getGlobalStorageEngine(); if (!engine->isMmapV1()) { return Status::OK(); @@ -390,8 +389,7 @@ Status ReplicationCoordinatorExternalStateImpl::initializeReplSetStorage(Operati createOplog(opCtx); MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction scopedXact(opCtx, MODE_X); - Lock::GlobalWrite globalWrite(opCtx->lockState()); + Lock::GlobalWrite globalWrite(opCtx); WriteUnitOfWork wuow(opCtx); Helpers::putSingleton(opCtx, configCollectionName, config); @@ -436,8 +434,6 @@ OpTime ReplicationCoordinatorExternalStateImpl::onTransitionToPrimary(OperationC if (isV1ElectionProtocol) { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction scopedXact(opCtx, MODE_X); - WriteUnitOfWork wuow(opCtx); opCtx->getClient()->getServiceContext()->getOpObserver()->onOpMessage( opCtx, @@ -466,8 +462,7 @@ OID ReplicationCoordinatorExternalStateImpl::ensureMe(OperationContext* opCtx) { std::string myname = getHostName(); OID myRID; { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock lock(opCtx->lockState(), meDatabaseName, MODE_X); + Lock::DBLock lock(opCtx, meDatabaseName, MODE_X); BSONObj me; // local.me is an identifier for a server for getLastError w:2+ @@ -514,8 +509,7 @@ Status ReplicationCoordinatorExternalStateImpl::storeLocalConfigDocument(Operati const BSONObj& config) { try { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dbWriteLock(opCtx->lockState(), configDatabaseName, MODE_X); + Lock::DBLock dbWriteLock(opCtx, configDatabaseName, MODE_X); Helpers::putSingleton(opCtx, configCollectionName, config); return Status::OK(); } @@ -550,8 +544,7 @@ Status ReplicationCoordinatorExternalStateImpl::storeLocalLastVoteDocument( BSONObj lastVoteObj = lastVote.toBSON(); try { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dbWriteLock(opCtx->lockState(), lastVoteDatabaseName, MODE_X); + Lock::DBLock dbWriteLock(opCtx, lastVoteDatabaseName, MODE_X); // If there is no last vote document, we want to store one. Otherwise, we only want to // replace it if the new last vote document would have a higher term. We both check diff --git a/src/mongo/db/repl/replication_coordinator_impl.cpp b/src/mongo/db/repl/replication_coordinator_impl.cpp index 18a25b08f26..963672cc67e 100644 --- a/src/mongo/db/repl/replication_coordinator_impl.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl.cpp @@ -938,8 +938,7 @@ void ReplicationCoordinatorImpl::signalDrainComplete(OperationContext* opCtx, } } - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite globalWriteLock(opCtx->lockState()); + Lock::GlobalWrite globalWriteLock(opCtx); lk.lock(); // Exit drain mode when the buffer is empty in the current term and we're in Draining mode. @@ -1806,7 +1805,7 @@ Status ReplicationCoordinatorImpl::stepDown(OperationContext* opCtx, return {ErrorCodes::NotMaster, "not primary so can't step down"}; } - Lock::GlobalLock globalReadLock(opCtx->lockState(), MODE_S, Lock::GlobalLock::EnqueueOnly()); + Lock::GlobalLock globalReadLock(opCtx, MODE_S, Lock::GlobalLock::EnqueueOnly()); // We've requested the global shared lock which will stop new writes from coming in, // but existing writes could take a long time to finish, so kill all user operations diff --git a/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp index be2370b7108..34bb0a9c101 100644 --- a/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp @@ -1431,7 +1431,7 @@ TEST_F(PrimaryCatchUpTest, PrimaryDoNotNeedToCatchUp) { ASSERT_EQUALS(1, countLogLinesContaining("My optime is most up-to-date, skipping catch-up")); auto opCtx = makeOperationContext(); getReplCoord()->signalDrainComplete(opCtx.get(), getReplCoord()->getTerm()); - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT_TRUE(getReplCoord()->canAcceptWritesForDatabase(opCtx.get(), "test")); } @@ -1455,7 +1455,7 @@ TEST_F(PrimaryCatchUpTest, PrimaryFreshnessScanTimeout) { ASSERT_EQUALS(1, countLogLinesContaining("Could not access any nodes within timeout")); auto opCtx = makeOperationContext(); getReplCoord()->signalDrainComplete(opCtx.get(), getReplCoord()->getTerm()); - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT_TRUE(getReplCoord()->canAcceptWritesForDatabase(opCtx.get(), "test")); } @@ -1485,7 +1485,7 @@ TEST_F(PrimaryCatchUpTest, PrimaryCatchUpSucceeds) { ASSERT_EQUALS(1, countLogLinesContaining("Finished catch-up oplog after becoming primary.")); auto opCtx = makeOperationContext(); getReplCoord()->signalDrainComplete(opCtx.get(), getReplCoord()->getTerm()); - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT_TRUE(getReplCoord()->canAcceptWritesForDatabase(opCtx.get(), "test")); } @@ -1509,7 +1509,7 @@ TEST_F(PrimaryCatchUpTest, PrimaryCatchUpTimeout) { ASSERT_EQUALS(1, countLogLinesContaining("Cannot catch up oplog after becoming primary")); auto opCtx = makeOperationContext(); getReplCoord()->signalDrainComplete(opCtx.get(), getReplCoord()->getTerm()); - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT_TRUE(getReplCoord()->canAcceptWritesForDatabase(opCtx.get(), "test")); } @@ -1537,7 +1537,7 @@ TEST_F(PrimaryCatchUpTest, PrimaryStepsDownDuringFreshnessScan) { stopCapturingLogMessages(); ASSERT_EQUALS(1, countLogLinesContaining("Stopped transition to primary")); auto opCtx = makeOperationContext(); - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT_FALSE(getReplCoord()->canAcceptWritesForDatabase(opCtx.get(), "test")); } @@ -1571,7 +1571,7 @@ TEST_F(PrimaryCatchUpTest, PrimaryStepsDownDuringCatchUp) { ASSERT(getReplCoord()->getApplierState() == ApplierState::Running); stopCapturingLogMessages(); ASSERT_EQUALS(1, countLogLinesContaining("Cannot catch up oplog after becoming primary")); - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT_FALSE(getReplCoord()->canAcceptWritesForDatabase(opCtx.get(), "test")); } @@ -1620,11 +1620,11 @@ TEST_F(PrimaryCatchUpTest, PrimaryStepsDownDuringDrainMode) { ASSERT(replCoord->getApplierState() == ApplierState::Draining); auto opCtx = makeOperationContext(); { - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT_FALSE(replCoord->canAcceptWritesForDatabase(opCtx.get(), "test")); } replCoord->signalDrainComplete(opCtx.get(), replCoord->getTerm()); - Lock::GlobalLock lock(opCtx->lockState(), MODE_IX, 1); + Lock::GlobalLock lock(opCtx.get(), MODE_IX, 1); ASSERT(replCoord->getApplierState() == ApplierState::Stopped); ASSERT_TRUE(replCoord->canAcceptWritesForDatabase(opCtx.get(), "test")); } diff --git a/src/mongo/db/repl/replication_coordinator_impl_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_test.cpp index ae8d5b5fe92..e69c85d57d1 100644 --- a/src/mongo/db/repl/replication_coordinator_impl_test.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl_test.cpp @@ -1605,7 +1605,7 @@ TEST_F(StepDownTest, const auto opCtx = makeOperationContext(); // Make sure stepDown cannot grab the global shared lock - Lock::GlobalWrite lk(opCtx->lockState()); + Lock::GlobalWrite lk(opCtx.get()); Status status = getReplCoord()->stepDown(opCtx.get(), false, Milliseconds(0), Milliseconds(1000)); diff --git a/src/mongo/db/repl/replication_info.cpp b/src/mongo/db/repl/replication_info.cpp index e0a49f5849f..28bc80d04c8 100644 --- a/src/mongo/db/repl/replication_info.cpp +++ b/src/mongo/db/repl/replication_info.cpp @@ -95,7 +95,7 @@ void appendReplicationInfo(OperationContext* opCtx, BSONObjBuilder& result, int list<BSONObj> src; { const NamespaceString localSources{"local.sources"}; - AutoGetCollectionForRead ctx(opCtx, localSources); + AutoGetCollectionForReadCommand ctx(opCtx, localSources); unique_ptr<PlanExecutor> exec(InternalPlanner::collectionScan( opCtx, localSources.ns(), ctx.getCollection(), PlanExecutor::YIELD_MANUAL)); BSONObj obj; diff --git a/src/mongo/db/repl/resync.cpp b/src/mongo/db/repl/resync.cpp index 848700d215a..67b230af0af 100644 --- a/src/mongo/db/repl/resync.cpp +++ b/src/mongo/db/repl/resync.cpp @@ -105,8 +105,7 @@ public: } // Master/Slave resync. - ScopedTransaction transaction(opCtx, MODE_X); - Lock::GlobalWrite globalWriteLock(opCtx->lockState()); + Lock::GlobalWrite globalWriteLock(opCtx); // below this comment pertains only to master/slave replication if (cmdObj.getBoolField("force")) { if (!waitForSyncToFinish(opCtx, errmsg)) diff --git a/src/mongo/db/repl/rs_initialsync.cpp b/src/mongo/db/repl/rs_initialsync.cpp index 32d03e31902..7c882dbe3c4 100644 --- a/src/mongo/db/repl/rs_initialsync.cpp +++ b/src/mongo/db/repl/rs_initialsync.cpp @@ -144,8 +144,7 @@ bool _initialSyncClone(OperationContext* opCtx, options.createCollections = false; // Make database stable - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dbWrite(opCtx->lockState(), db, MODE_X); + Lock::DBLock dbWrite(opCtx, db, MODE_X); Status status = cloner.copyDb(opCtx, db, host, options, nullptr, collections); if (!status.isOK()) { @@ -360,8 +359,7 @@ Status _initialSync(OperationContext* opCtx, BackgroundSync* bgsync) { createCollectionParams.push_back(params); } - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dbWrite(opCtx->lockState(), db, MODE_X); + Lock::DBLock dbWrite(opCtx, db, MODE_X); auto createStatus = cloner.createCollectionsForDb(opCtx, createCollectionParams, db); if (!createStatus.isOK()) { diff --git a/src/mongo/db/repl/rs_rollback.cpp b/src/mongo/db/repl/rs_rollback.cpp index 88fbf63f036..d32404132e4 100644 --- a/src/mongo/db/repl/rs_rollback.cpp +++ b/src/mongo/db/repl/rs_rollback.cpp @@ -438,8 +438,7 @@ void syncFixUp(OperationContext* opCtx, { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dbLock(opCtx->lockState(), nss.db(), MODE_X); + Lock::DBLock dbLock(opCtx, nss.db(), MODE_X); Database* db = dbHolder().openDb(opCtx, nss.db().toString()); invariant(db); WriteUnitOfWork wunit(opCtx); @@ -454,8 +453,7 @@ void syncFixUp(OperationContext* opCtx, log() << "rollback 4.1.2 coll metadata resync " << ns; const NamespaceString nss(ns); - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dbLock(opCtx->lockState(), nss.db(), MODE_X); + Lock::DBLock dbLock(opCtx, nss.db(), MODE_X); auto db = dbHolder().openDb(opCtx, nss.db().toString()); invariant(db); auto collection = db->getCollection(ns); @@ -534,9 +532,8 @@ void syncFixUp(OperationContext* opCtx, invariant(!fixUpInfo.indexesToDrop.count(*it)); - ScopedTransaction transaction(opCtx, MODE_IX); const NamespaceString nss(*it); - Lock::DBLock dbLock(opCtx->lockState(), nss.db(), MODE_X); + Lock::DBLock dbLock(opCtx, nss.db(), MODE_X); Database* db = dbHolder().get(opCtx, nsToDatabaseSubstring(*it)); if (db) { Helpers::RemoveSaver removeSaver("rollback", "", *it); @@ -580,8 +577,7 @@ void syncFixUp(OperationContext* opCtx, const string& indexName = it->second; log() << "rollback drop index: collection: " << nss.toString() << ". index: " << indexName; - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dbLock(opCtx->lockState(), nss.db(), MODE_X); + Lock::DBLock dbLock(opCtx, nss.db(), MODE_X); auto db = dbHolder().get(opCtx, nss.db()); if (!db) { continue; @@ -641,8 +637,7 @@ void syncFixUp(OperationContext* opCtx, // TODO: Lots of overhead in context. This can be faster. const NamespaceString docNss(doc.ns); - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock docDbLock(opCtx->lockState(), docNss.db(), MODE_X); + Lock::DBLock docDbLock(opCtx, docNss.db(), MODE_X); OldClientContext ctx(opCtx, doc.ns); Collection* collection = ctx.db()->getCollection(doc.ns); @@ -761,8 +756,7 @@ void syncFixUp(OperationContext* opCtx, LOG(2) << "rollback truncate oplog after " << fixUpInfo.commonPoint.toString(); { const NamespaceString oplogNss(rsOplogName); - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock oplogDbLock(opCtx->lockState(), oplogNss.db(), MODE_IX); + Lock::DBLock oplogDbLock(opCtx, oplogNss.db(), MODE_IX); Lock::CollectionLock oplogCollectionLoc(opCtx->lockState(), oplogNss.ns(), MODE_X); OldClientContext ctx(opCtx, rsOplogName); Collection* oplogCollection = ctx.db()->getCollection(rsOplogName); @@ -895,7 +889,7 @@ void rollback(OperationContext* opCtx, // then. { log() << "rollback 0"; - Lock::GlobalWrite globalWrite(opCtx->lockState()); + Lock::GlobalWrite globalWrite(opCtx); if (!replCoord->setFollowerMode(MemberState::RS_ROLLBACK)) { log() << "Cannot transition from " << replCoord->getMemberState().toString() << " to " << MemberState(MemberState::RS_ROLLBACK).toString(); diff --git a/src/mongo/db/repl/rs_rollback_test.cpp b/src/mongo/db/repl/rs_rollback_test.cpp index 5df25a37427..90843d9a445 100644 --- a/src/mongo/db/repl/rs_rollback_test.cpp +++ b/src/mongo/db/repl/rs_rollback_test.cpp @@ -292,7 +292,7 @@ TEST_F(RSRollbackTest, BothOplogsAtCommonPoint) { Collection* _createCollection(OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions& options) { - Lock::DBLock dbLock(opCtx->lockState(), nss.db(), MODE_X); + Lock::DBLock dbLock(opCtx, nss.db(), MODE_X); mongo::WriteUnitOfWork wuow(opCtx); auto db = dbHolder().openDb(opCtx, nss.db()); ASSERT_TRUE(db); @@ -355,7 +355,7 @@ int _testRollbackDelete(OperationContext* opCtx, storageInterface)); ASSERT_TRUE(rollbackSource.called); - Lock::DBLock dbLock(opCtx->lockState(), "test", MODE_S); + Lock::DBLock dbLock(opCtx, "test", MODE_S); Lock::CollectionLock collLock(opCtx->lockState(), "test.t", MODE_S); auto db = dbHolder().get(opCtx, "test"); ASSERT_TRUE(db); @@ -451,7 +451,7 @@ TEST_F(RSRollbackTest, RollbackCreateIndexCommand) { << "v" << static_cast<int>(kIndexVersion)); { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_X); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_X); MultiIndexBlock indexer(_opCtx.get(), collection); ASSERT_OK(indexer.init(indexSpec).getStatus()); WriteUnitOfWork wunit(_opCtx.get()); @@ -502,7 +502,7 @@ TEST_F(RSRollbackTest, RollbackCreateIndexCommand) { countLogLinesContaining("rollback drop index: collection: test.t. index: a_1")); ASSERT_FALSE(rollbackSource.called); { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_S); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_S); auto indexCatalog = collection->getIndexCatalog(); ASSERT(indexCatalog); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(_opCtx.get())); @@ -520,7 +520,7 @@ TEST_F(RSRollbackTest, RollbackCreateIndexCommandIndexNotInCatalog) { << "a_1"); // Skip index creation to trigger warning during rollback. { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_S); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_S); auto indexCatalog = collection->getIndexCatalog(); ASSERT(indexCatalog); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(_opCtx.get())); @@ -564,7 +564,7 @@ TEST_F(RSRollbackTest, RollbackCreateIndexCommandIndexNotInCatalog) { ASSERT_EQUALS(1, countLogLinesContaining("rollback failed to drop index a_1 in test.t")); ASSERT_FALSE(rollbackSource.called); { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_S); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_S); auto indexCatalog = collection->getIndexCatalog(); ASSERT(indexCatalog); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(_opCtx.get())); @@ -619,7 +619,7 @@ TEST_F(RSRollbackTest, RollbackDropIndexCommandWithOneIndex) { createOplog(_opCtx.get()); auto collection = _createCollection(_opCtx.get(), "test.t", CollectionOptions()); { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_S); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_S); auto indexCatalog = collection->getIndexCatalog(); ASSERT(indexCatalog); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(_opCtx.get())); @@ -677,7 +677,7 @@ TEST_F(RSRollbackTest, RollbackDropIndexCommandWithMultipleIndexes) { createOplog(_opCtx.get()); auto collection = _createCollection(_opCtx.get(), "test.t", CollectionOptions()); { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_S); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_S); auto indexCatalog = collection->getIndexCatalog(); ASSERT(indexCatalog); ASSERT_EQUALS(1, indexCatalog->numIndexesReady(_opCtx.get())); @@ -860,7 +860,7 @@ TEST_F(RSRollbackTest, RollbackUnknownCommand) { << "t")), RecordId(2)); { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_X); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_X); mongo::WriteUnitOfWork wuow(_opCtx.get()); auto db = dbHolder().openDb(_opCtx.get(), "test"); ASSERT_TRUE(db); @@ -1083,7 +1083,7 @@ TEST_F(RSRollbackTest, RollbackApplyOpsCommand) { ASSERT_EQUALS(1U, rollbackSource.searchedIds.count(3)); ASSERT_EQUALS(1U, rollbackSource.searchedIds.count(4)); - AutoGetCollectionForRead acr(_opCtx.get(), NamespaceString("test.t")); + AutoGetCollectionForReadCommand acr(_opCtx.get(), NamespaceString("test.t")); BSONObj result; ASSERT(Helpers::findOne(_opCtx.get(), acr.getCollection(), BSON("_id" << 1), result)); ASSERT_EQUALS(1, result["v"].numberInt()) << result; @@ -1119,7 +1119,7 @@ TEST_F(RSRollbackTest, RollbackCreateCollectionCommand) { _coordinator, &_storageInterface)); { - Lock::DBLock dbLock(_opCtx->lockState(), "test", MODE_S); + Lock::DBLock dbLock(_opCtx.get(), "test", MODE_S); auto db = dbHolder().get(_opCtx.get(), "test"); ASSERT_TRUE(db); ASSERT_FALSE(db->getCollection("test.t")); diff --git a/src/mongo/db/repl/storage_interface_impl.cpp b/src/mongo/db/repl/storage_interface_impl.cpp index bece9cedeb6..b98af9c8537 100644 --- a/src/mongo/db/repl/storage_interface_impl.cpp +++ b/src/mongo/db/repl/storage_interface_impl.cpp @@ -104,8 +104,7 @@ NamespaceString StorageInterfaceImpl::getMinValidNss() const { BSONObj StorageInterfaceImpl::getMinValidDocument(OperationContext* opCtx) const { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IS); - Lock::DBLock dblk(opCtx->lockState(), _minValidNss.db(), MODE_IS); + Lock::DBLock dblk(opCtx, _minValidNss.db(), MODE_IS); Lock::CollectionLock lk(opCtx->lockState(), _minValidNss.ns(), MODE_IS); BSONObj doc; bool found = Helpers::getSingleton(opCtx, _minValidNss.ns().c_str(), doc); @@ -121,9 +120,8 @@ BSONObj StorageInterfaceImpl::getMinValidDocument(OperationContext* opCtx) const void StorageInterfaceImpl::updateMinValidDocument(OperationContext* opCtx, const BSONObj& updateSpec) { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); // For now this needs to be MODE_X because it sometimes creates the collection. - Lock::DBLock dblk(opCtx->lockState(), _minValidNss.db(), MODE_X); + Lock::DBLock dblk(opCtx, _minValidNss.db(), MODE_X); Helpers::putSingleton(opCtx, _minValidNss.ns().c_str(), updateSpec); } MONGO_WRITE_CONFLICT_RETRY_LOOP_END( @@ -263,7 +261,6 @@ StorageInterfaceImpl::createCollectionForBulkLoading( // Retry if WCE. MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { // Get locks and create the collection. - ScopedTransaction transaction(opCtx, MODE_IX); auto db = stdx::make_unique<AutoGetOrCreateDb>(opCtx, nss.db(), MODE_IX); auto coll = stdx::make_unique<AutoGetCollection>(opCtx, nss, MODE_X); collection = coll->getCollection(); @@ -321,7 +318,6 @@ Status insertDocumentsSingleBatch(OperationContext* opCtx, const NamespaceString& nss, std::vector<BSONObj>::const_iterator begin, std::vector<BSONObj>::const_iterator end) { - ScopedTransaction transaction(opCtx, MODE_IX); AutoGetCollection autoColl(opCtx, nss, MODE_IX); auto collection = autoColl.getCollection(); if (!collection) { @@ -384,7 +380,7 @@ Status StorageInterfaceImpl::createOplog(OperationContext* opCtx, const Namespac StatusWith<size_t> StorageInterfaceImpl::getOplogMaxSize(OperationContext* opCtx, const NamespaceString& nss) { - AutoGetCollectionForRead collection(opCtx, nss); + AutoGetCollectionForReadCommand collection(opCtx, nss); if (!collection.getCollection()) { return {ErrorCodes::NamespaceNotFound, str::stream() << "Your oplog doesn't exist: " << nss.ns()}; @@ -401,7 +397,6 @@ Status StorageInterfaceImpl::createCollection(OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions& options) { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); AutoGetOrCreateDb databaseWriteGuard(opCtx, nss.db(), MODE_X); auto db = databaseWriteGuard.getDb(); invariant(db); @@ -424,7 +419,6 @@ Status StorageInterfaceImpl::createCollection(OperationContext* opCtx, Status StorageInterfaceImpl::dropCollection(OperationContext* opCtx, const NamespaceString& nss) { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); AutoGetDb autoDB(opCtx, nss.db(), MODE_X); if (!autoDB.getDb()) { // Database does not exist - nothing to do. @@ -470,7 +464,6 @@ StatusWith<std::vector<BSONObj>> _findOrDeleteDocuments( MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { auto collectionAccessMode = isFind ? MODE_IS : MODE_IX; - ScopedTransaction transaction(opCtx, collectionAccessMode); AutoGetCollection collectionGuard(opCtx, nss, collectionAccessMode); auto collection = collectionGuard.getCollection(); if (!collection) { @@ -607,7 +600,6 @@ StatusWith<std::vector<BSONObj>> StorageInterfaceImpl::deleteDocuments( } Status StorageInterfaceImpl::isAdminDbValid(OperationContext* opCtx) { - ScopedTransaction transaction(opCtx, MODE_IX); AutoGetDb autoDB(opCtx, "admin", MODE_X); return checkAdminDatabase(opCtx, autoDB.getDb()); } diff --git a/src/mongo/db/repl/storage_interface_impl_test.cpp b/src/mongo/db/repl/storage_interface_impl_test.cpp index e94761e5d35..6ca1058cf5c 100644 --- a/src/mongo/db/repl/storage_interface_impl_test.cpp +++ b/src/mongo/db/repl/storage_interface_impl_test.cpp @@ -89,8 +89,7 @@ NamespaceString makeNamespace(const T& t, const char* suffix = "") { */ BSONObj getMinValidDocument(OperationContext* opCtx, const NamespaceString& minValidNss) { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IS); - Lock::DBLock dblk(opCtx->lockState(), minValidNss.db(), MODE_IS); + Lock::DBLock dblk(opCtx, minValidNss.db(), MODE_IS); Lock::CollectionLock lk(opCtx->lockState(), minValidNss.ns(), MODE_IS); BSONObj mv; if (Helpers::getSingleton(opCtx, minValidNss.ns().c_str(), mv)) { @@ -120,8 +119,7 @@ void createCollection(OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions& options = CollectionOptions()) { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dblk(opCtx->lockState(), nss.db(), MODE_X); + Lock::DBLock dblk(opCtx, nss.db(), MODE_X); OldClientContext ctx(opCtx, nss.ns()); auto db = ctx.db(); ASSERT_TRUE(db); @@ -449,7 +447,7 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, InsertMissingDocWorksOnExistingCap opts.cappedSize = 1024 * 1024; createCollection(opCtx, nss, opts); ASSERT_OK(storage.insertDocument(opCtx, nss, BSON("_id" << 1))); - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_TRUE(autoColl.getCollection()); } @@ -459,7 +457,7 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, InsertMissingDocWorksOnExistingCol NamespaceString nss("foo.bar"); createCollection(opCtx, nss); ASSERT_OK(storage.insertDocument(opCtx, nss, BSON("_id" << 1))); - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_TRUE(autoColl.getCollection()); } @@ -487,7 +485,7 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, CreateCollectionWithIDIndexCommits ASSERT_OK(loader->insertDocuments(docs.begin(), docs.end())); ASSERT_OK(loader->commit()); - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); auto coll = autoColl.getCollection(); ASSERT(coll); ASSERT_EQ(coll->getRecordStore()->numRecords(opCtx), 2LL); @@ -516,7 +514,7 @@ void _testDestroyUncommitedCollectionBulkLoader( // Collection and ID index should not exist after 'loader' is destroyed. destroyLoaderFn(std::move(loader)); - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); auto coll = autoColl.getCollection(); // Bulk loader is used to create indexes. The collection is not dropped when the bulk loader is @@ -590,12 +588,12 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, CreateOplogCreateCappedCollection) StorageInterfaceImpl storage; NamespaceString nss("local.oplog.X"); { - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_FALSE(autoColl.getCollection()); } ASSERT_OK(storage.createOplog(opCtx, nss)); { - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_TRUE(autoColl.getCollection()); ASSERT_EQ(nss.toString(), autoColl.getCollection()->ns().toString()); ASSERT_TRUE(autoColl.getCollection()->isCapped()); @@ -608,7 +606,7 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, StorageInterfaceImpl storage; NamespaceString nss("local.oplog.Y"); { - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_FALSE(autoColl.getCollection()); } @@ -622,12 +620,12 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, CreateCollectionFailsIfCollectionE StorageInterfaceImpl storage; auto nss = makeNamespace(_agent); { - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_FALSE(autoColl.getCollection()); } ASSERT_OK(storage.createCollection(opCtx, nss, CollectionOptions())); { - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_TRUE(autoColl.getCollection()); ASSERT_EQ(nss.toString(), autoColl.getCollection()->ns().toString()); } @@ -652,7 +650,7 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, DropCollectionWorksWithExistingEmp NamespaceString nss("foo.bar"); createCollection(opCtx, nss); ASSERT_OK(storage.dropCollection(opCtx, nss)); - AutoGetCollectionForRead autoColl(opCtx, nss); + AutoGetCollectionForReadCommand autoColl(opCtx, nss); ASSERT_FALSE(autoColl.getCollection()); } @@ -662,7 +660,7 @@ TEST_F(StorageInterfaceImplWithReplCoordTest, DropCollectionWorksWithMissingColl NamespaceString nss("foo.bar"); ASSERT_FALSE(AutoGetDb(opCtx, nss.db(), MODE_IS).getDb()); ASSERT_OK(storage.dropCollection(opCtx, nss)); - ASSERT_FALSE(AutoGetCollectionForRead(opCtx, nss).getCollection()); + ASSERT_FALSE(AutoGetCollectionForReadCommand(opCtx, nss).getCollection()); // Database should not be created after running dropCollection. ASSERT_FALSE(AutoGetDb(opCtx, nss.db(), MODE_IS).getDb()); } diff --git a/src/mongo/db/repl/sync_tail.cpp b/src/mongo/db/repl/sync_tail.cpp index c42b51905b2..105f40bcd5a 100644 --- a/src/mongo/db/repl/sync_tail.cpp +++ b/src/mongo/db/repl/sync_tail.cpp @@ -312,7 +312,7 @@ Status SyncTail::syncApply(OperationContext* opCtx, MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { // a command may need a global write lock. so we will conservatively go // ahead and grab one here. suboptimal. :-( - Lock::GlobalWrite globalWriteLock(opCtx->lockState()); + Lock::GlobalWrite globalWriteLock(opCtx); // special case apply for commands to avoid implicit database creation Status status = applyCommandInLock(opCtx, op, inSteadyStateReplication); @@ -339,7 +339,7 @@ Status SyncTail::syncApply(OperationContext* opCtx, if (isNoOp || (opType[0] == 'i' && nsToCollectionSubstring(ns) == "system.indexes")) { auto opStr = isNoOp ? "syncApply_noop" : "syncApply_indexBuild"; MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - Lock::DBLock dbLock(opCtx->lockState(), nsToDatabaseSubstring(ns), MODE_X); + Lock::DBLock dbLock(opCtx, nsToDatabaseSubstring(ns), MODE_X); OldClientContext ctx(opCtx, ns); return applyOp(ctx.db()); } @@ -361,7 +361,7 @@ Status SyncTail::syncApply(OperationContext* opCtx, // drop the DB lock before acquiring // the upgraded one. dbLock.reset(); - dbLock.reset(new Lock::DBLock(opCtx->lockState(), dbName, mode)); + dbLock.reset(new Lock::DBLock(opCtx, dbName, mode)); collectionLock.reset(new Lock::CollectionLock(opCtx->lockState(), ns, mode)); }; @@ -418,7 +418,7 @@ void prefetchOp(const BSONObj& op) { // for multiple prefetches if they are for the same database. const ServiceContext::UniqueOperationContext opCtxPtr = cc().makeOperationContext(); OperationContext& opCtx = *opCtxPtr; - AutoGetCollectionForRead ctx(&opCtx, NamespaceString(ns)); + AutoGetCollectionForReadCommand ctx(&opCtx, NamespaceString(ns)); Database* db = ctx.getDb(); if (db) { prefetchPagesForReplicatedOp(&opCtx, db, op); @@ -478,8 +478,8 @@ void scheduleWritesToOplog(OperationContext* opCtx, // guarantees that 'ops' will stay in scope until the spawned threads complete. return [&ops, begin, end] { initializeWriterThread(); - const auto txnHolder = cc().makeOperationContext(); - const auto opCtx = txnHolder.get(); + const auto opCtxHolder = cc().makeOperationContext(); + const auto opCtx = opCtxHolder.get(); opCtx->lockState()->setShouldConflictWithSecondaryBatchApplication(false); UnreplicatedWritesBlock uwb(opCtx); @@ -552,7 +552,7 @@ private: CollectionProperties getCollectionPropertiesImpl(OperationContext* opCtx, StringData ns) { CollectionProperties collProperties; - Lock::DBLock dbLock(opCtx->lockState(), nsToDatabaseSubstring(ns), MODE_IS); + Lock::DBLock dbLock(opCtx, nsToDatabaseSubstring(ns), MODE_IS); auto db = dbHolder().get(opCtx, ns); if (!db) { return collProperties; @@ -640,8 +640,7 @@ void tryToGoLiveAsASecondary(OperationContext* opCtx, ReplicationCoordinator* re // This needs to happen after the attempt so readers can be sure we've already tried. ON_BLOCK_EXIT([] { attemptsToBecomeSecondary.increment(); }); - ScopedTransaction transaction(opCtx, MODE_S); - Lock::GlobalRead readLock(opCtx->lockState()); + Lock::GlobalRead readLock(opCtx); if (replCoord->getMaintenanceMode()) { LOG(1) << "Can't go live (tryToGoLiveAsASecondary) as maintenance mode is active."; diff --git a/src/mongo/db/repl/sync_tail_test.cpp b/src/mongo/db/repl/sync_tail_test.cpp index b1e62a2efa5..7e2cda18fe9 100644 --- a/src/mongo/db/repl/sync_tail_test.cpp +++ b/src/mongo/db/repl/sync_tail_test.cpp @@ -177,8 +177,7 @@ void createCollection(OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions& options) { MONGO_WRITE_CONFLICT_RETRY_LOOP_BEGIN { - ScopedTransaction transaction(opCtx, MODE_IX); - Lock::DBLock dblk(opCtx->lockState(), nss.db(), MODE_X); + Lock::DBLock dblk(opCtx, nss.db(), MODE_X); OldClientContext ctx(opCtx, nss.ns()); auto db = ctx.db(); ASSERT_TRUE(db); @@ -363,7 +362,7 @@ TEST_F(SyncTailTest, SyncApplyInsertDocumentDatabaseMissing) { TEST_F(SyncTailTest, SyncApplyInsertDocumentCollectionMissing) { { - Lock::GlobalWrite globalLock(_opCtx->lockState()); + Lock::GlobalWrite globalLock(_opCtx.get()); bool justCreated = false; Database* db = dbHolder().openDb(_opCtx.get(), "test", &justCreated); ASSERT_TRUE(db); @@ -374,7 +373,7 @@ TEST_F(SyncTailTest, SyncApplyInsertDocumentCollectionMissing) { TEST_F(SyncTailTest, SyncApplyInsertDocumentCollectionExists) { { - Lock::GlobalWrite globalLock(_opCtx->lockState()); + Lock::GlobalWrite globalLock(_opCtx.get()); bool justCreated = false; Database* db = dbHolder().openDb(_opCtx.get(), "test", &justCreated); ASSERT_TRUE(db); @@ -614,7 +613,7 @@ TEST_F(SyncTailTest, MultiSyncApplyUsesSyncApplyToApplyOperation) { multiSyncApply(&ops, nullptr); // Collection should be created after SyncTail::syncApply() processes operation. _opCtx = cc().makeOperationContext(); - ASSERT_TRUE(AutoGetCollectionForRead(_opCtx.get(), nss).getCollection()); + ASSERT_TRUE(AutoGetCollectionForReadCommand(_opCtx.get(), nss).getCollection()); } TEST_F(SyncTailTest, MultiSyncApplyDisablesDocumentValidationWhileApplyingOperations) { @@ -866,7 +865,7 @@ TEST_F(SyncTailTest, // Since the missing document is not found on the sync source, the collection referenced by // the failed operation should not be automatically created. - ASSERT_FALSE(AutoGetCollectionForRead(_opCtx.get(), nss).getCollection()); + ASSERT_FALSE(AutoGetCollectionForReadCommand(_opCtx.get(), nss).getCollection()); ASSERT_EQUALS(fetchCount.load(), 1U); } @@ -1007,14 +1006,14 @@ OplogEntry IdempotencyTest::dropIndex(const std::string& indexName) { } std::string IdempotencyTest::validate() { - auto collection = AutoGetCollectionForRead(_opCtx.get(), nss).getCollection(); + auto collection = AutoGetCollectionForReadCommand(_opCtx.get(), nss).getCollection(); if (!collection) { return "CollectionNotFound"; } ValidateResults validateResults; BSONObjBuilder bob; - Lock::DBLock lk(_opCtx->lockState(), nss.db(), MODE_IS); + Lock::DBLock lk(_opCtx.get(), nss.db(), MODE_IS); Lock::CollectionLock lock(_opCtx->lockState(), nss.ns(), MODE_IS); ASSERT_OK(collection->validate(_opCtx.get(), kValidateFull, &validateResults, &bob)); ASSERT_TRUE(validateResults.valid); |