diff options
author | Jordi Serra Torrens <jordi.serra-torrens@mongodb.com> | 2021-02-17 12:54:44 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-02-25 08:49:19 +0000 |
commit | 1d3c1ca0ddb2c984ca66381cc39565cbec01c645 (patch) | |
tree | 9f3d69748bd8eaee8fa2af56792981f722699769 /src/mongo/db/s/config | |
parent | d1a3ee71e82eb35a7f1e1ea6fcee4fa920317346 (diff) | |
download | mongo-1d3c1ca0ddb2c984ca66381cc39565cbec01c645.tar.gz |
SERVER-53861: Implement stop migrations procedure for DDL operations
Diffstat (limited to 'src/mongo/db/s/config')
3 files changed, 165 insertions, 0 deletions
diff --git a/src/mongo/db/s/config/configsvr_set_allow_migrations_command.cpp b/src/mongo/db/s/config/configsvr_set_allow_migrations_command.cpp new file mode 100644 index 00000000000..8ab105f72e6 --- /dev/null +++ b/src/mongo/db/s/config/configsvr_set_allow_migrations_command.cpp @@ -0,0 +1,105 @@ +/** + * Copyright (C) 2021-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_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding + +#include "mongo/platform/basic.h" + +#include "mongo/db/auth/authorization_session.h" +#include "mongo/db/commands.h" +#include "mongo/db/s/config/sharding_catalog_manager.h" +#include "mongo/s/request_types/set_allow_migrations_gen.h" + +namespace mongo { +namespace { + +class ConfigsvrSetAllowMigrationsCommand final + : public TypedCommand<ConfigsvrSetAllowMigrationsCommand> { +public: + using Request = ConfigsvrSetAllowMigrations; + + class Invocation final : public InvocationBase { + public: + using InvocationBase::InvocationBase; + + void typedRun(OperationContext* opCtx) { + const NamespaceString& nss = ns(); + + uassert(ErrorCodes::IllegalOperation, + "_configsvrSetAllowMigrations can only be run on config servers", + serverGlobalParams.clusterRole == ClusterRole::ConfigServer); + uassert(ErrorCodes::InvalidOptions, + "_configsvrSetAllowMigrations 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 allowMigrations = request().getAllowMigrations(); + + ShardingCatalogManager::get(opCtx)->setAllowMigrationsAndBumpOneChunk( + opCtx, nss, allowMigrations); + } + + private: + NamespaceString ns() const override { + return request().getCommandParameter(); + } + + 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. Do not call " + "directly. Sets the allowMigrations flag on the specified collection."; + } + + bool adminOnly() const override { + return true; + } + + AllowedOnSecondary secondaryAllowed(ServiceContext*) const override { + return AllowedOnSecondary::kNever; + } +} configsvrSetAllowMigrationsCmd; + +} // namespace +} // namespace mongo diff --git a/src/mongo/db/s/config/sharding_catalog_manager.h b/src/mongo/db/s/config/sharding_catalog_manager.h index 9de828a0b46..7413e595e0d 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager.h +++ b/src/mongo/db/s/config/sharding_catalog_manager.h @@ -301,6 +301,14 @@ public: const NamespaceString& nss, const BSONObj& minKey); + /** + * In a transaction, sets the 'allowMigrations' to the requested state and bumps the collection + * version. + */ + void setAllowMigrationsAndBumpOneChunk(OperationContext* opCtx, + const NamespaceString& nss, + bool allowMigrations); + // // Database Operations // diff --git a/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp b/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp index 811d9e9cc66..f4a1d7c5349 100644 --- a/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp +++ b/src/mongo/db/s/config/sharding_catalog_manager_chunk_operations.cpp @@ -45,6 +45,7 @@ #include "mongo/db/operation_context.h" #include "mongo/db/repl/repl_client_info.h" #include "mongo/db/s/sharding_logging.h" +#include "mongo/db/s/sharding_util.h" #include "mongo/db/server_options.h" #include "mongo/db/snapshot_window_options_gen.h" #include "mongo/db/transaction_participant_gen.h" @@ -1453,4 +1454,55 @@ void ShardingCatalogManager::splitOrMarkJumbo(OperationContext* opCtx, } } +void ShardingCatalogManager::setAllowMigrationsAndBumpOneChunk(OperationContext* opCtx, + const NamespaceString& nss, + bool allowMigrations) { + std::set<ShardId> shardsIds; + { + // Take _kChunkOpLock in exclusive mode to prevent concurrent chunk splits, merges, and + // migrations + Lock::ExclusiveLock lk(opCtx->lockState(), _kChunkOpLock); + + const auto cm = uassertStatusOK( + Grid::get(opCtx)->catalogCache()->getShardedCollectionRoutingInfoWithRefresh(opCtx, + nss)); + cm.getAllShardIds(&shardsIds); + withTransaction( + opCtx, CollectionType::ConfigNS, [&](OperationContext* opCtx, TxnNumber txnNumber) { + // Update the 'allowMigrations' field. An unset 'allowMigrations' field implies + // 'true'. To ease backwards compatibility we omit 'allowMigrations' instead of + // setting it explicitly to 'true'. + const auto update = allowMigrations + ? BSON("$unset" << BSON(CollectionType::kAllowMigrationsFieldName << "")) + : BSON("$set" << BSON(CollectionType::kAllowMigrationsFieldName << false)); + + writeToConfigDocumentInTxn( + opCtx, + CollectionType::ConfigNS, + BatchedCommandRequest::buildUpdateOp( + CollectionType::ConfigNS, + BSON(CollectionType::kNssFieldName << nss.ns()) /* query */, + update /* update */, + false /* upsert */, + false /* multi */), + txnNumber); + + // Bump the chunk version for one single chunk + invariant(!shardsIds.empty()); + bumpMajorVersionOneChunkPerShard(opCtx, nss, txnNumber, {*shardsIds.begin()}); + }); + + // From now on migrations are not allowed anymore, so it is not possible that new shards + // will own chunks for this collection. + } + + // Trigger a refresh on each shard containing chunks for this collection. + const auto executor = Grid::get(opCtx)->getExecutorPool()->getFixedExecutor(); + sharding_util::tellShardsToRefreshCollection( + opCtx, + {std::make_move_iterator(shardsIds.begin()), std::make_move_iterator(shardsIds.end())}, + nss, + executor); +} + } // namespace mongo |