diff options
author | Jason Zhang <jason.zhang@mongodb.com> | 2019-07-12 15:41:25 -0400 |
---|---|---|
committer | Jason Zhang <jason.zhang@mongodb.com> | 2019-07-18 14:52:12 -0400 |
commit | 7f15f3efd3a6e68211b168239259bff8c08147d3 (patch) | |
tree | bc7a1a2fc998d931e1fdf6cfe9b249a57b42527d /src/mongo/db/s | |
parent | 36dc61299993ce6473a4660150bfb25a59afce77 (diff) | |
download | mongo-7f15f3efd3a6e68211b168239259bff8c08147d3.tar.gz |
SERVER-42120 Add configsvrRenameCollection that takes NamespaceSerializer locks and distributed locks on the database and collection.
Diffstat (limited to 'src/mongo/db/s')
-rw-r--r-- | src/mongo/db/s/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/s/config/configsvr_rename_collection_command.cpp | 127 |
2 files changed, 128 insertions, 0 deletions
diff --git a/src/mongo/db/s/SConscript b/src/mongo/db/s/SConscript index 528b880d93c..62583c09cf8 100644 --- a/src/mongo/db/s/SConscript +++ b/src/mongo/db/s/SConscript @@ -266,6 +266,7 @@ env.Library( 'config/configsvr_move_chunk_command.cpp', 'config/configsvr_move_primary_command.cpp', 'config/configsvr_refine_collection_shard_key_command.cpp', + 'config/configsvr_rename_collection_command.cpp', 'config/configsvr_remove_shard_command.cpp', 'config/configsvr_remove_shard_from_zone_command.cpp', 'config/configsvr_shard_collection_command.cpp', diff --git a/src/mongo/db/s/config/configsvr_rename_collection_command.cpp b/src/mongo/db/s/config/configsvr_rename_collection_command.cpp new file mode 100644 index 00000000000..71f30139d4f --- /dev/null +++ b/src/mongo/db/s/config/configsvr_rename_collection_command.cpp @@ -0,0 +1,127 @@ +/** + * Copyright (C) 2019-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * 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 + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * 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 Server Side 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::kSharding + +#include "mongo/platform/basic.h" + +#include "mongo/db/auth/authorization_session.h" +#include "mongo/db/commands.h" +#include "mongo/db/namespace_string.h" +#include "mongo/db/s/config/sharding_catalog_manager.h" +#include "mongo/s/grid.h" +#include "mongo/s/request_types/rename_collection_gen.h" + +namespace mongo { +namespace { +/** + * Internal command run on config servers to rename a collection. + */ +class ConfigSvrRenameCollectionCommand final + : public TypedCommand<ConfigSvrRenameCollectionCommand> { +public: + using Request = ConfigsvrRenameCollection; + + class Invocation final : public InvocationBase { + public: + using InvocationBase::InvocationBase; + + void typedRun(OperationContext* opCtx) { + const NamespaceString& nssSource = ns(); + const NamespaceString nssTarget = Request().getTo(); + + uassert(ErrorCodes::IllegalOperation, + "_configsvrRenameCollection can only be run on config servers", + serverGlobalParams.clusterRole == ClusterRole::ConfigServer); + uassert(ErrorCodes::InvalidOptions, + "renameCollection must be called with majority writeConcern", + opCtx->getWriteConcern().wMode == WriteConcernOptions::kMajority); + + // Set the operation context read concern level to local for reads into the config + // database. + repl::ReadConcernArgs::get(opCtx) = + repl::ReadConcernArgs(repl::ReadConcernLevel::kLocalReadConcern); + + const auto catalogClient = Grid::get(opCtx)->catalogClient(); + + // For renameCollection within a database, we already take an exclusive lock on the + // database, so subsequent locks are more of a locking pattern formality rather than + // a necessity. Currently only assumes renaming within a database not across databases. + auto scopedDbLockSource = + ShardingCatalogManager::get(opCtx)->serializeCreateOrDropDatabase(opCtx, + nssSource.db()); + auto scopedCollLockSource = + ShardingCatalogManager::get(opCtx)->serializeCreateOrDropCollection(opCtx, + nssSource); + auto scopedCollLockTarget = + ShardingCatalogManager::get(opCtx)->serializeCreateOrDropCollection(opCtx, + nssTarget); + + auto dbDistLockSource = uassertStatusOK(catalogClient->getDistLockManager()->lock( + opCtx, nssSource.db(), "renameCollection", DistLockManager::kDefaultLockTimeout)); + auto collDistLockSource = uassertStatusOK(catalogClient->getDistLockManager()->lock( + opCtx, nssSource.ns(), "renameCollection", DistLockManager::kDefaultLockTimeout)); + auto collDistLockTarget = uassertStatusOK(catalogClient->getDistLockManager()->lock( + opCtx, nssTarget.ns(), "renameCollection", DistLockManager::kDefaultLockTimeout)); + } + + private: + NamespaceString ns() const override { + return request().getRenameCollection(); + } + + bool supportsWriteConcern() const override { + return true; + } + + void doCheckAuthorization(OperationContext* opCtx) const override { + uassert(ErrorCodes::Unauthorized, + "Unauthorized", + AuthorizationSession::get(opCtx->getClient()) + ->isAuthorizedForActionsOnResource(ResourcePattern::forClusterResource(), + ActionType::internal)); + } + }; + + std::string help() const override { + return "Internal command, which is exported by the sharding config server." + "Renames a collection"; + } + + bool adminOnly() const override { + return true; + } + + AllowedOnSecondary secondaryAllowed(ServiceContext*) const override { + return AllowedOnSecondary::kNever; + } +} configSvrRenameCollectionCmd; + +} // namespace +} // namespace mongo |