diff options
-rw-r--r-- | src/mongo/db/s/balancer/migration_manager.cpp | 61 | ||||
-rw-r--r-- | src/mongo/db/s/balancer/migration_manager.h | 14 |
2 files changed, 37 insertions, 38 deletions
diff --git a/src/mongo/db/s/balancer/migration_manager.cpp b/src/mongo/db/s/balancer/migration_manager.cpp index 8ab7933ef24..68aa90b49d1 100644 --- a/src/mongo/db/s/balancer/migration_manager.cpp +++ b/src/mongo/db/s/balancer/migration_manager.cpp @@ -500,49 +500,48 @@ std::shared_ptr<Notification<RemoteCommandResponse>> MigrationManager::_schedule auto retVal = migration.completionNotification; - _schedule(lock, - opCtx, - fromHostStatus.getValue(), - std::move(migration), - migrateInfo, - waitForDelete, - scopedMigrationRequests); + _acquireDistLockAndSchedule(lock, + opCtx, + fromHostStatus.getValue(), + std::move(migration), + migrateInfo, + waitForDelete, + scopedMigrationRequests); return retVal; } -void MigrationManager::_schedule(WithLock lock, - OperationContext* opCtx, - const HostAndPort& targetHost, - Migration migration, - const MigrateInfo& migrateInfo, - bool waitForDelete, - ScopedMigrationRequestsMap* scopedMigrationRequests) { +void MigrationManager::_acquireDistLockAndSchedule( + WithLock lock, + OperationContext* opCtx, + const HostAndPort& targetHost, + Migration migration, + const MigrateInfo& migrateInfo, + bool waitForDelete, + ScopedMigrationRequestsMap* scopedMigrationRequests) noexcept { auto executor = Grid::get(opCtx)->getExecutorPool()->getFixedExecutor(); const NamespaceString nss(migration.nss); auto it = _activeMigrations.find(nss); if (it == _activeMigrations.end()) { - const std::string whyMessage(str::stream() - << "Migrating chunk(s) in collection " << nss.ns()); - - // Acquire the local lock for this nss (blocking call) - auto scopedLock = DistLockManager::get(opCtx)->lockDirectLocally( - opCtx, nss.ns(), DistLockManager::kSingleLockAttemptTimeout); - - // Acquire the collection distributed lock (blocking call) - auto status = DistLockManager::get(opCtx)->lockDirect( - opCtx, nss.ns(), whyMessage, DistLockManager::kSingleLockAttemptTimeout); - - if (!status.isOK()) { + boost::optional<DistLockManager::ScopedLock> scopedLock; + try { + scopedLock.emplace(DistLockManager::get(opCtx)->lockDirectLocally( + opCtx, nss.ns(), DistLockManager::kSingleLockAttemptTimeout)); + + const std::string whyMessage(str::stream() + << "Migrating chunk(s) in collection " << nss.ns()); + uassertStatusOK(DistLockManager::get(opCtx)->lockDirect( + opCtx, nss.ns(), whyMessage, DistLockManager::kSingleLockAttemptTimeout)); + } catch (const DBException& ex) { migration.completionNotification->set( - status.withContext(str::stream() << "Could not acquire collection lock for " - << nss.ns() << " to migrate chunks")); + ex.toStatus(str::stream() << "Could not acquire collection lock for " << nss.ns() + << " to migrate chunks")); return; } - MigrationsState migrationsState(std::move(scopedLock)); + MigrationsState migrationsState(std::move(*scopedLock)); it = _activeMigrations.insert(std::make_pair(nss, std::move(migrationsState))).first; } @@ -553,7 +552,6 @@ void MigrationManager::_schedule(WithLock lock, // recovered by the Balancer. auto statusWithScopedMigrationRequest = ScopedMigrationRequest::writeMigration(opCtx, migrateInfo, waitForDelete); - if (!statusWithScopedMigrationRequest.isOK()) { migrationRequestStatus = std::move(statusWithScopedMigrationRequest.getStatus()); } else { @@ -564,7 +562,8 @@ void MigrationManager::_schedule(WithLock lock, auto migrations = &it->second.migrationsList; - // Add ourselves to the list of migrations on this collection + // Add ourselves to the list of migrations on this collection. From that point onwards, requests + // must call _complete regardless of success or failure in order to remove it from the list. migrations->push_front(std::move(migration)); auto itMigration = migrations->begin(); diff --git a/src/mongo/db/s/balancer/migration_manager.h b/src/mongo/db/s/balancer/migration_manager.h index 631e6057c2c..073353adccb 100644 --- a/src/mongo/db/s/balancer/migration_manager.h +++ b/src/mongo/db/s/balancer/migration_manager.h @@ -208,13 +208,13 @@ private: * pointer is passed. Otherwise, writes the migration document under the collection distributed * lock and adds it to the map. */ - void _schedule(WithLock, - OperationContext* opCtx, - const HostAndPort& targetHost, - Migration migration, - const MigrateInfo& migrateInfo, - bool waitForDelete, - ScopedMigrationRequestsMap* scopedMigrationRequests); + void _acquireDistLockAndSchedule(WithLock, + OperationContext* opCtx, + const HostAndPort& targetHost, + Migration migration, + const MigrateInfo& migrateInfo, + bool waitForDelete, + ScopedMigrationRequestsMap* scopedMigrationRequests) noexcept; /** * Used internally for migrations scheduled with the distributed lock acquired by the config |