diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2016-12-16 09:51:56 -0500 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2016-12-16 21:24:49 -0500 |
commit | 834fe4857af1cd75f2cd70f7682540dbe3a2ced6 (patch) | |
tree | 7d890e3614e62d616da903d629e69fbed48c1f7f /src/mongo/s/commands | |
parent | e41c763fb9f750fc3ecf22cf284c79361d345762 (diff) | |
download | mongo-834fe4857af1cd75f2cd70f7682540dbe3a2ced6.tar.gz |
SERVER-27419 Move cluster 'drop' command to a separate file
Also renames DBConfig::invalidateNs to markNSNotSharded to better convey the intent of the function.
Diffstat (limited to 'src/mongo/s/commands')
-rw-r--r-- | src/mongo/s/commands/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_commands_common.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_commands_common.h | 7 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_drop_cmd.cpp | 158 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_drop_database_cmd.cpp | 69 | ||||
-rw-r--r-- | src/mongo/s/commands/commands_public.cpp | 49 |
6 files changed, 184 insertions, 102 deletions
diff --git a/src/mongo/s/commands/SConscript b/src/mongo/s/commands/SConscript index 2d8b0f7bf34..123481cf2a6 100644 --- a/src/mongo/s/commands/SConscript +++ b/src/mongo/s/commands/SConscript @@ -27,6 +27,7 @@ env.Library( 'cluster_count_cmd.cpp', 'cluster_current_op.cpp', 'cluster_db_stats_cmd.cpp', + 'cluster_drop_cmd.cpp', 'cluster_drop_database_cmd.cpp', 'cluster_enable_sharding_cmd.cpp', 'cluster_explain.cpp', diff --git a/src/mongo/s/commands/cluster_commands_common.cpp b/src/mongo/s/commands/cluster_commands_common.cpp index 529b39be403..65ed5c6882c 100644 --- a/src/mongo/s/commands/cluster_commands_common.cpp +++ b/src/mongo/s/commands/cluster_commands_common.cpp @@ -37,8 +37,6 @@ #include "mongo/db/query/cursor_response.h" #include "mongo/s/client/shard_connection.h" #include "mongo/s/client/version_manager.h" -#include "mongo/s/query/cluster_client_cursor_impl.h" -#include "mongo/s/query/cluster_cursor_manager.h" #include "mongo/s/stale_exception.h" #include "mongo/util/log.h" diff --git a/src/mongo/s/commands/cluster_commands_common.h b/src/mongo/s/commands/cluster_commands_common.h index b759c14c45e..c45e39b1196 100644 --- a/src/mongo/s/commands/cluster_commands_common.h +++ b/src/mongo/s/commands/cluster_commands_common.h @@ -38,17 +38,10 @@ namespace mongo { -class BSONObj; - class AScopedConnection; -class ClusterCursorManager; class DBClientBase; class DBClientCursor; -namespace executor { -class TaskExecutor; -} // namespace executor - /** * DEPRECATED - do not use in any new code. All new code must use the TaskExecutor interface * instead. diff --git a/src/mongo/s/commands/cluster_drop_cmd.cpp b/src/mongo/s/commands/cluster_drop_cmd.cpp new file mode 100644 index 00000000000..564d348e043 --- /dev/null +++ b/src/mongo/s/commands/cluster_drop_cmd.cpp @@ -0,0 +1,158 @@ +/** + * Copyright (C) 2016 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::kCommand + +#include "mongo/platform/basic.h" + +#include "mongo/base/status.h" +#include "mongo/db/commands.h" +#include "mongo/db/operation_context.h" +#include "mongo/s/catalog/catalog_cache.h" +#include "mongo/s/catalog/dist_lock_manager.h" +#include "mongo/s/catalog/sharding_catalog_client.h" +#include "mongo/s/catalog/type_database.h" +#include "mongo/s/client/shard_registry.h" +#include "mongo/s/commands/cluster_commands_common.h" +#include "mongo/s/commands/sharded_command_processing.h" +#include "mongo/s/config.h" +#include "mongo/s/grid.h" +#include "mongo/s/sharding_raii.h" +#include "mongo/s/stale_exception.h" +#include "mongo/util/log.h" + +namespace mongo { +namespace { + +class DropCmd : public Command { +public: + DropCmd() : Command("drop") {} + + bool slaveOk() const override { + return true; + } + + bool adminOnly() const override { + return false; + } + + bool supportsWriteConcern(const BSONObj& cmd) const override { + return true; + } + + void addRequiredPrivileges(const std::string& dbname, + const BSONObj& cmdObj, + std::vector<Privilege>* out) override { + ActionSet actions; + actions.addAction(ActionType::dropCollection); + out->push_back(Privilege(parseResourcePattern(dbname, cmdObj), actions)); + } + + bool run(OperationContext* txn, + const std::string& dbname, + BSONObj& cmdObj, + int options, + std::string& errmsg, + BSONObjBuilder& result) override { + const NamespaceString nss(parseNsCollectionRequired(dbname, cmdObj)); + + auto scopedDbStatus = ScopedShardDatabase::getExisting(txn, dbname); + if (scopedDbStatus == ErrorCodes::NamespaceNotFound) { + return true; + } + + uassertStatusOK(scopedDbStatus.getStatus()); + + auto const db = scopedDbStatus.getValue().db(); + + if (!db->isShardingEnabled() || !db->isSharded(nss.ns())) { + _dropUnshardedCollectionFromShard(txn, db->getPrimaryId(), nss, &result); + } else { + uassertStatusOK(Grid::get(txn)->catalogClient(txn)->dropCollection(txn, nss)); + db->markNSNotSharded(nss.ns()); + } + + return true; + } + +private: + /** + * Sends the 'drop' command for the specified collection to the specified shard. Throws + * DBException on failure. + */ + static void _dropUnshardedCollectionFromShard(OperationContext* txn, + const ShardId& shardId, + const NamespaceString& nss, + BSONObjBuilder* result) { + const auto shardRegistry = Grid::get(txn)->shardRegistry(); + + const auto dropCommandBSON = [shardRegistry, txn, &shardId, &nss] { + BSONObjBuilder builder; + builder.append("drop", nss.coll()); + + // Append the chunk version for the specified namespace indicating that we believe it is + // not sharded. Collections residing on the config server are never sharded so do not + // send the shard version. + if (shardId != shardRegistry->getConfigShard()->getId()) { + ChunkVersion::UNSHARDED().appendForCommands(&builder); + } + + if (!txn->getWriteConcern().usedDefault) { + builder.append(WriteConcernOptions::kWriteConcernField, + txn->getWriteConcern().toBSON()); + } + + return builder.obj(); + }(); + + const auto shard = uassertStatusOK(shardRegistry->getShard(txn, shardId)); + auto cmdDropResult = uassertStatusOK(shard->runCommandWithFixedRetryAttempts( + txn, + ReadPreferenceSetting{ReadPreference::PrimaryOnly}, + nss.db().toString(), + dropCommandBSON, + Shard::RetryPolicy::kIdempotent)); + + // Special-case SendStaleVersion errors + if (cmdDropResult.commandStatus == ErrorCodes::SendStaleConfig) { + throw RecvStaleConfigException( + str::stream() << "Stale config while dropping collection", cmdDropResult.response); + } + + uassertStatusOK(cmdDropResult.commandStatus); + + if (!cmdDropResult.writeConcernStatus.isOK()) { + appendWriteConcernErrorToCmdResponse( + shardId, cmdDropResult.response["writeConcernError"], *result); + } + } + +} clusterDropCmd; + +} // namespace +} // namespace mongo diff --git a/src/mongo/s/commands/cluster_drop_database_cmd.cpp b/src/mongo/s/commands/cluster_drop_database_cmd.cpp index 05ac92c22ed..f8f418d90d7 100644 --- a/src/mongo/s/commands/cluster_drop_database_cmd.cpp +++ b/src/mongo/s/commands/cluster_drop_database_cmd.cpp @@ -38,6 +38,8 @@ #include "mongo/s/catalog/sharding_catalog_client.h" #include "mongo/s/catalog/type_database.h" #include "mongo/s/client/shard_registry.h" +#include "mongo/s/commands/cluster_commands_common.h" +#include "mongo/s/commands/sharded_command_processing.h" #include "mongo/s/config.h" #include "mongo/s/grid.h" #include "mongo/s/sharding_raii.h" @@ -114,22 +116,23 @@ public: std::set<std::string> namespaces; db->getAllShardedCollections(namespaces); - std::set<ShardId> allShardIds; - // Drop the database's collections from metadata for (const auto& ns : namespaces) { - const auto collectionShards = - _dropShardedCollectionFromConfig(txn, NamespaceString(ns)); - allShardIds.insert(collectionShards.begin(), collectionShards.end()); + uassertStatusOK(catalogClient->dropCollection(txn, NamespaceString(ns))); + db->markNSNotSharded(ns); } - // Drop the database from the primary shard first _dropDatabaseFromShard(txn, db->getPrimaryId(), dbname); // Drop the database from each of the remaining shards - for (const ShardId& shardId : allShardIds) { - _dropDatabaseFromShard(txn, shardId, dbname); + { + std::vector<ShardId> allShardIds; + Grid::get(txn)->shardRegistry()->getAllShardIds(&allShardIds); + + for (const ShardId& shardId : allShardIds) { + _dropDatabaseFromShard(txn, shardId, dbname); + } } // Remove the database entry from the metadata @@ -157,52 +160,30 @@ public: private: /** - * Drops the specified sharded collection from the config server metadata only and returns the - * set of shards on which it was located when it was being dropped. - * - * Throws DBException on failure. - */ - static std::set<ShardId> _dropShardedCollectionFromConfig(OperationContext* txn, - NamespaceString nss) { - auto scopedCMStatus = ScopedChunkManager::refreshAndGet(txn, nss); - - if (scopedCMStatus == ErrorCodes::NamespaceNotFound || - scopedCMStatus == ErrorCodes::NamespaceNotSharded) { - // Skip collection if we cannot find it - return std::set<ShardId>{}; - } else if (!scopedCMStatus.isOK()) { - uassertStatusOK({scopedCMStatus.getStatus().code(), - str::stream() << "Failed to drop collection " << nss.ns() << " due to " - << scopedCMStatus.getStatus().reason()}); - } - - auto const db = scopedCMStatus.getValue().db(); - auto const cm = scopedCMStatus.getValue().cm(); - - std::set<ShardId> shardIds; - cm->getAllShardIds(&shardIds); - - uassertStatusOK(Grid::get(txn)->catalogClient(txn)->dropCollection(txn, nss)); - - db->invalidateNs(nss.ns()); - - return shardIds; - } - - /** - * Sends the 'drop' command for the specified database to the specified shard. Throws + * Sends the 'dropDatabase' command for the specified database to the specified shard. Throws * DBException on failure. */ static void _dropDatabaseFromShard(OperationContext* txn, const ShardId& shardId, const std::string& dbName) { + const auto dropDatabaseCommandBSON = [txn, &dbName] { + BSONObjBuilder builder; + builder.append("dropDatabase", 1); + + if (!txn->getWriteConcern().usedDefault) { + builder.append(WriteConcernOptions::kWriteConcernField, + txn->getWriteConcern().toBSON()); + } + + return builder.obj(); + }(); + const auto shard = uassertStatusOK(Grid::get(txn)->shardRegistry()->getShard(txn, shardId)); auto cmdDropDatabaseResult = uassertStatusOK(shard->runCommandWithFixedRetryAttempts( txn, ReadPreferenceSetting{ReadPreference::PrimaryOnly}, dbName, - BSON("dropDatabase" << 1 << WriteConcernOptions::kWriteConcernField - << txn->getWriteConcern().toBSON()), + dropDatabaseCommandBSON, Shard::RetryPolicy::kIdempotent)); uassertStatusOK(cmdDropDatabaseResult.commandStatus); diff --git a/src/mongo/s/commands/commands_public.cpp b/src/mongo/s/commands/commands_public.cpp index 74e94ffc96e..fe17c4650e1 100644 --- a/src/mongo/s/commands/commands_public.cpp +++ b/src/mongo/s/commands/commands_public.cpp @@ -537,55 +537,6 @@ public: } createCmd; -class DropCmd : public PublicGridCommand { -public: - DropCmd() : PublicGridCommand("drop") {} - virtual void addRequiredPrivileges(const std::string& dbname, - const BSONObj& cmdObj, - std::vector<Privilege>* out) { - ActionSet actions; - actions.addAction(ActionType::dropCollection); - out->push_back(Privilege(parseResourcePattern(dbname, cmdObj), actions)); - } - - virtual bool supportsWriteConcern(const BSONObj& cmd) const override { - return true; - } - - bool run(OperationContext* txn, - const string& dbName, - BSONObj& cmdObj, - int options, - string& errmsg, - BSONObjBuilder& result) { - auto status = Grid::get(txn)->catalogCache()->getDatabase(txn, dbName); - if (!status.isOK()) { - if (status == ErrorCodes::NamespaceNotFound) { - return true; - } - - return appendCommandStatus(result, status.getStatus()); - } - - const NamespaceString nss(parseNsCollectionRequired(dbName, cmdObj)); - - log() << "DROP: " << nss.ns(); - - const auto& db = status.getValue(); - if (!db->isShardingEnabled() || !db->isSharded(nss.ns())) { - log() << "\tdrop going to do passthrough"; - return passthrough(txn, db.get(), cmdObj, result); - } - - uassertStatusOK(Grid::get(txn)->catalogClient(txn)->dropCollection(txn, nss)); - - // Force a full reload next time the just dropped namespace is accessed - db->invalidateNs(nss.ns()); - - return true; - } -} dropCmd; - class RenameCollectionCmd : public PublicGridCommand { public: RenameCollectionCmd() : PublicGridCommand("renameCollection") {} |