diff options
5 files changed, 157 insertions, 38 deletions
diff --git a/src/mongo/db/s/SConscript b/src/mongo/db/s/SConscript index a31e205c457..f1552d8deee 100644 --- a/src/mongo/db/s/SConscript +++ b/src/mongo/db/s/SConscript @@ -344,6 +344,7 @@ env.Library( 'move_chunk_command.cpp', 'move_primary_command.cpp', 'rename_collection_coordinator.cpp', + 'refine_collection_shard_key_coordinator.cpp', 'set_shard_version_command.cpp', 'sharding_ddl_coordinator.cpp', 'sharding_server_status.cpp', diff --git a/src/mongo/db/s/config/configsvr_refine_collection_shard_key_command.cpp b/src/mongo/db/s/config/configsvr_refine_collection_shard_key_command.cpp index 05b82b1af50..96c62cab8c9 100644 --- a/src/mongo/db/s/config/configsvr_refine_collection_shard_key_command.cpp +++ b/src/mongo/db/s/config/configsvr_refine_collection_shard_key_command.cpp @@ -72,18 +72,6 @@ public: const auto catalogClient = Grid::get(opCtx)->catalogClient(); - // Acquire distlocks on the namespace's database and collection. - DistLockManager::ScopedDistLock dbDistLock(uassertStatusOK( - DistLockManager::get(opCtx)->lock(opCtx, - nss.db(), - "refineCollectionShardKey", - DistLockManager::kDefaultLockTimeout))); - DistLockManager::ScopedDistLock collDistLock(uassertStatusOK( - DistLockManager::get(opCtx)->lock(opCtx, - nss.ns(), - "refineCollectionShardKey", - DistLockManager::kDefaultLockTimeout))); - // Validate the given namespace is (i) sharded, (ii) doesn't already have the proposed // key, and (iii) has the same epoch as the router that received // refineCollectionShardKey had in its routing table cache. diff --git a/src/mongo/db/s/refine_collection_shard_key_coordinator.cpp b/src/mongo/db/s/refine_collection_shard_key_coordinator.cpp new file mode 100644 index 00000000000..b814b8280b9 --- /dev/null +++ b/src/mongo/db/s/refine_collection_shard_key_coordinator.cpp @@ -0,0 +1,99 @@ +/** + * 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/db/s/refine_collection_shard_key_coordinator.h" + +#include "mongo/db/commands.h" +#include "mongo/db/s/dist_lock_manager.h" +#include "mongo/logv2/log.h" +#include "mongo/s/catalog_cache.h" +#include "mongo/s/grid.h" +#include "mongo/s/request_types/refine_collection_shard_key_gen.h" + +namespace mongo { + +RefineCollectionShardKeyCoordinator::RefineCollectionShardKeyCoordinator( + OperationContext* opCtx, const NamespaceString& nss, const KeyPattern newShardKey) + : ShardingDDLCoordinator_NORESILIENT(opCtx, nss), + _serviceContext(opCtx->getServiceContext()), + _newShardKey(std::move(newShardKey)){}; + +SemiFuture<void> RefineCollectionShardKeyCoordinator::runImpl( + std::shared_ptr<executor::TaskExecutor> executor) { + return ExecutorFuture<void>(executor, Status::OK()) + .then([this, anchor = shared_from_this()]() { + ThreadClient tc{"RefineCollectionShardKeyCoordinator", _serviceContext}; + auto opCtxHolder = tc->makeOperationContext(); + auto* opCtx = opCtxHolder.get(); + _forwardableOpMetadata.setOn(opCtx); + + auto distLockManager = DistLockManager::get(opCtx->getServiceContext()); + const auto dbDistLock = + uassertStatusOK(distLockManager->lock(opCtx, + _nss.db(), + "RefineCollectionShardKey", + DistLockManager::kDefaultLockTimeout)); + const auto fromCollDistLock = + uassertStatusOK(distLockManager->lock(opCtx, + _nss.ns(), + "RefineCollectionShardKey", + DistLockManager::kDefaultLockTimeout)); + + const auto cm = uassertStatusOK( + Grid::get(opCtx)->catalogCache()->getShardedCollectionRoutingInfoWithRefresh(opCtx, + _nss)); + ConfigsvrRefineCollectionShardKey configsvrRefineCollShardKey( + _nss, _newShardKey.toBSON(), cm.getVersion().epoch()); + configsvrRefineCollShardKey.setDbName(_nss.db().toString()); + + auto configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard(); + auto cmdResponse = uassertStatusOK(configShard->runCommandWithFixedRetryAttempts( + opCtx, + ReadPreferenceSetting(ReadPreference::PrimaryOnly), + "admin", + CommandHelpers::appendMajorityWriteConcern(configsvrRefineCollShardKey.toBSON({}), + opCtx->getWriteConcern()), + Shard::RetryPolicy::kIdempotent)); + + uassertStatusOK(cmdResponse.commandStatus); + uassertStatusOK(cmdResponse.writeConcernStatus); + }) + .onError([this, anchor = shared_from_this()](const Status& status) { + LOGV2_ERROR(5277700, + "Error running refine collection shard key", + "namespace"_attr = _nss, + "error"_attr = redact(status)); + return status; + }) + .semi(); +} + +} // namespace mongo diff --git a/src/mongo/db/s/refine_collection_shard_key_coordinator.h b/src/mongo/db/s/refine_collection_shard_key_coordinator.h new file mode 100644 index 00000000000..417278bd008 --- /dev/null +++ b/src/mongo/db/s/refine_collection_shard_key_coordinator.h @@ -0,0 +1,53 @@ +/** + * 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. + */ + +#pragma once + +#include "mongo/db/keypattern.h" +#include "mongo/db/s/sharding_ddl_coordinator.h" + +namespace mongo { + +class RefineCollectionShardKeyCoordinator final + : public ShardingDDLCoordinator_NORESILIENT, + public std::enable_shared_from_this<RefineCollectionShardKeyCoordinator> { +public: + RefineCollectionShardKeyCoordinator(OperationContext* opCtx, + const NamespaceString& nss, + const KeyPattern newShardKey); + +private: + SemiFuture<void> runImpl(std::shared_ptr<executor::TaskExecutor> executor) override; + + ServiceContext* _serviceContext; + + const KeyPattern _newShardKey; +}; + +} // namespace mongo diff --git a/src/mongo/db/s/shardsvr_refine_collection_shard_key_command.cpp b/src/mongo/db/s/shardsvr_refine_collection_shard_key_command.cpp index feed625c073..f899ae1dfea 100644 --- a/src/mongo/db/s/shardsvr_refine_collection_shard_key_command.cpp +++ b/src/mongo/db/s/shardsvr_refine_collection_shard_key_command.cpp @@ -31,15 +31,9 @@ #include "mongo/db/auth/authorization_session.h" #include "mongo/db/commands.h" -#include "mongo/db/drop_database_gen.h" -#include "mongo/db/s/config/sharding_catalog_manager.h" -#include "mongo/db/s/sharding_logging.h" -#include "mongo/s/catalog/type_database.h" -#include "mongo/s/catalog_cache.h" -#include "mongo/s/grid.h" +#include "mongo/db/s/refine_collection_shard_key_coordinator.h" #include "mongo/s/request_types/refine_collection_shard_key_gen.h" #include "mongo/s/request_types/sharded_ddl_commands_gen.h" -#include "mongo/s/sharded_collections_ddl_parameters_gen.h" namespace mongo { namespace { @@ -67,25 +61,9 @@ public: using InvocationBase::InvocationBase; void typedRun(OperationContext* opCtx) { - - const auto cm = uassertStatusOK( - Grid::get(opCtx)->catalogCache()->getShardedCollectionRoutingInfoWithRefresh(opCtx, - ns())); - ConfigsvrRefineCollectionShardKey configsvrRefineCollShardKey( - ns(), request().getNewShardKey().toBSON(), cm.getVersion().epoch()); - configsvrRefineCollShardKey.setDbName(request().getDbName()); - - auto configShard = Grid::get(opCtx)->shardRegistry()->getConfigShard(); - auto cmdResponse = uassertStatusOK(configShard->runCommandWithFixedRetryAttempts( - opCtx, - ReadPreferenceSetting(ReadPreference::PrimaryOnly), - "admin", - CommandHelpers::appendMajorityWriteConcern(configsvrRefineCollShardKey.toBSON({}), - opCtx->getWriteConcern()), - Shard::RetryPolicy::kIdempotent)); - - uassertStatusOK(cmdResponse.commandStatus); - uassertStatusOK(cmdResponse.writeConcernStatus); + auto refineCoordinator = std::make_shared<RefineCollectionShardKeyCoordinator>( + opCtx, ns(), request().getNewShardKey()); + refineCoordinator->run(opCtx).get(opCtx); } bool supportsWriteConcern() const override { |