summaryrefslogtreecommitdiff
path: root/src/mongo/s/commands/cluster_drop_database_cmd.cpp
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-12-16 09:51:56 -0500
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-12-16 21:24:49 -0500
commit834fe4857af1cd75f2cd70f7682540dbe3a2ced6 (patch)
tree7d890e3614e62d616da903d629e69fbed48c1f7f /src/mongo/s/commands/cluster_drop_database_cmd.cpp
parente41c763fb9f750fc3ecf22cf284c79361d345762 (diff)
downloadmongo-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/cluster_drop_database_cmd.cpp')
-rw-r--r--src/mongo/s/commands/cluster_drop_database_cmd.cpp69
1 files changed, 25 insertions, 44 deletions
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);