diff options
author | jannaerin <golden.janna@gmail.com> | 2018-05-18 11:50:34 -0400 |
---|---|---|
committer | jannaerin <golden.janna@gmail.com> | 2018-05-23 15:33:25 -0400 |
commit | c8497a2c85e65680b439603caba6874b54082355 (patch) | |
tree | c9f1b3335608c64143c24825cc72d9475c6db3bf /src | |
parent | 48130ffebe242959eac9b685e6ea46373f4fddb3 (diff) | |
download | mongo-c8497a2c85e65680b439603caba6874b54082355.tar.gz |
SERVER-33639 Protect against distlock acquisition timeout at createDatabase time
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/s/config/configsvr_create_database_command.cpp | 3 | ||||
-rw-r--r-- | src/mongo/s/catalog/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/s/catalog/namespace_serializer.cpp | 80 | ||||
-rw-r--r-- | src/mongo/s/catalog/namespace_serializer.h | 77 | ||||
-rw-r--r-- | src/mongo/s/catalog/sharding_catalog_manager.h | 11 |
5 files changed, 172 insertions, 0 deletions
diff --git a/src/mongo/db/s/config/configsvr_create_database_command.cpp b/src/mongo/db/s/config/configsvr_create_database_command.cpp index 86690de4e2e..b5f84c94911 100644 --- a/src/mongo/db/s/config/configsvr_create_database_command.cpp +++ b/src/mongo/db/s/config/configsvr_create_database_command.cpp @@ -122,6 +122,9 @@ public: // Remove the backwards compatible lock after 3.6 ships. auto const catalogClient = Grid::get(opCtx)->catalogClient(); + auto scopedLock = + ShardingCatalogManager::get(opCtx)->serializeCreateDatabase(opCtx, dbname); + auto backwardsCompatibleDbDistLock = uassertStatusOK( catalogClient->getDistLockManager()->lock(opCtx, dbname + "-movePrimary", diff --git a/src/mongo/s/catalog/SConscript b/src/mongo/s/catalog/SConscript index 23329d25999..e39a8cf831c 100644 --- a/src/mongo/s/catalog/SConscript +++ b/src/mongo/s/catalog/SConscript @@ -145,6 +145,7 @@ env.Library( 'sharding_catalog_manager_database_operations.cpp', 'sharding_catalog_manager_shard_operations.cpp', 'sharding_catalog_manager_zone_operations.cpp', + 'namespace_serializer.cpp', ], LIBDEPS=[ '$BUILD_DIR/mongo/db/catalog/catalog_raii', diff --git a/src/mongo/s/catalog/namespace_serializer.cpp b/src/mongo/s/catalog/namespace_serializer.cpp new file mode 100644 index 00000000000..883f37d2e1c --- /dev/null +++ b/src/mongo/s/catalog/namespace_serializer.cpp @@ -0,0 +1,80 @@ +/** + * Copyright (C) 2018 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects + * for all of the code used other than as permitted herein. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you do not + * wish to do so, delete this exception statement from your version. If you + * delete this exception statement from all source files in the program, + * then also delete it in the license file. + */ +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kDefault +#include "mongo/platform/basic.h" + +#include "mongo/s/catalog/namespace_serializer.h" + +#include <map> +#include <memory> +#include <string> + +#include "mongo/db/namespace_string.h" +#include "mongo/db/operation_context.h" +#include "mongo/s/catalog/sharding_catalog_manager.h" +#include "mongo/util/log.h" +#include "mongo/util/scopeguard.h" + +namespace mongo { + +NamespaceSerializer::NamespaceSerializer() {} + +NamespaceSerializer::ScopedLock::ScopedLock(StringData ns, NamespaceSerializer& nsSerializer) + : _ns(ns.toString()), _nsSerializer(nsSerializer) {} + +NamespaceSerializer::ScopedLock::~ScopedLock() { + stdx::unique_lock<stdx::mutex> lock(_nsSerializer._mutex); + auto iter = _nsSerializer._inProgressMap.find(_ns); + + iter->second->numWaiting--; + iter->second->isInProgress = false; + iter->second->cvLocked.notify_one(); + + if (iter->second->numWaiting == 0) { + _nsSerializer._inProgressMap.erase(_ns); + } +} + +NamespaceSerializer::ScopedLock NamespaceSerializer::lock(OperationContext* opCtx, StringData nss) { + stdx::unique_lock<stdx::mutex> lock(_mutex); + auto iter = _inProgressMap.find(nss); + + if (iter == _inProgressMap.end()) { + _inProgressMap.try_emplace(nss, std::make_shared<NSLock>()); + } else { + auto& nsLock = iter->second; + nsLock->numWaiting++; + opCtx->waitForConditionOrInterrupt( + nsLock->cvLocked, lock, [&nsLock]() { return !nsLock->isInProgress; }); + nsLock->isInProgress = true; + } + + return ScopedLock(nss, *this); +} + +} // namespace mongo diff --git a/src/mongo/s/catalog/namespace_serializer.h b/src/mongo/s/catalog/namespace_serializer.h new file mode 100644 index 00000000000..8683b7578b1 --- /dev/null +++ b/src/mongo/s/catalog/namespace_serializer.h @@ -0,0 +1,77 @@ +/** + * Copyright (C) 2018 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects + * for all of the code used other than as permitted herein. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you do not + * wish to do so, delete this exception statement from your version. If you + * delete this exception statement from all source files in the program, + * then also delete it in the license file. + */ + +#pragma once + +#include <map> +#include <memory> +#include <string> + +#include "mongo/base/status.h" +#include "mongo/base/status_with.h" +#include "mongo/db/namespace_string.h" +#include "mongo/stdx/condition_variable.h" +#include "mongo/stdx/mutex.h" +#include "mongo/util/string_map.h" + +namespace mongo { + +class OperationContext; + +class NamespaceSerializer { + MONGO_DISALLOW_COPYING(NamespaceSerializer); + +public: + class ScopedLock { + public: + ~ScopedLock(); + + private: + friend class NamespaceSerializer; + ScopedLock(StringData ns, NamespaceSerializer& nsSerializer); + + std::string _ns; + NamespaceSerializer& _nsSerializer; + }; + + NamespaceSerializer(); + + ScopedLock lock(OperationContext* opCtx, StringData ns); + +private: + struct NSLock { + stdx::condition_variable cvLocked; + int numWaiting = 1; + bool isInProgress = true; + }; + + stdx::mutex _mutex; + StringMap<std::shared_ptr<NSLock>> _inProgressMap; +}; + +} // namespace mongo diff --git a/src/mongo/s/catalog/sharding_catalog_manager.h b/src/mongo/s/catalog/sharding_catalog_manager.h index 670c57fb678..56457993a78 100644 --- a/src/mongo/s/catalog/sharding_catalog_manager.h +++ b/src/mongo/s/catalog/sharding_catalog_manager.h @@ -33,6 +33,7 @@ #include "mongo/db/concurrency/d_concurrency.h" #include "mongo/db/repl/optime_with.h" #include "mongo/executor/task_executor.h" +#include "mongo/s/catalog/namespace_serializer.h" #include "mongo/s/catalog/type_chunk.h" #include "mongo/s/catalog/type_database.h" #include "mongo/s/catalog/type_shard.h" @@ -203,6 +204,14 @@ public: DatabaseType createDatabase(OperationContext* opCtx, const std::string& dbName); /** + * Creates a ScopedLock on the database name in _namespaceSerializer. This is to prevent + * timeouts waiting on the dist lock if multiple threads attempt to create the same db. + */ + auto serializeCreateDatabase(OperationContext* opCtx, StringData dbName) { + return _namespaceSerializer.lock(opCtx, dbName); + } + + /** * Creates the database if it does not exist, then marks its entry in config.databases as * sharding-enabled. * @@ -478,6 +487,8 @@ private: * be removed while they are running (such as removeShardFromZone) to take this in shared mode. */ Lock::ResourceMutex _kShardMembershipLock; + + NamespaceSerializer _namespaceSerializer; }; } // namespace mongo |