summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-12-16 09:51:13 -0500
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-12-16 09:51:13 -0500
commit971dad2ee9c23fcb21723575fb133a3298a3e44e (patch)
tree4ded752baf2fc76e4dd3786c067b2d43194e81d8 /src/mongo
parent7f74694a21f4e9c0a75e69e715133cfb590129b7 (diff)
downloadmongo-971dad2ee9c23fcb21723575fb133a3298a3e44e.tar.gz
Revert "SERVER-27419 Move cluster 'drop' command to a separate file"
This reverts commit 20bc3f49fafb2c8a8d9e8224e8723cd767549d2c.
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/s/commands/SConscript1
-rw-r--r--src/mongo/s/commands/cluster_commands_common.cpp2
-rw-r--r--src/mongo/s/commands/cluster_commands_common.h7
-rw-r--r--src/mongo/s/commands/cluster_drop_cmd.cpp152
-rw-r--r--src/mongo/s/commands/cluster_drop_database_cmd.cpp67
-rw-r--r--src/mongo/s/commands/commands_public.cpp49
-rw-r--r--src/mongo/s/config.cpp2
-rw-r--r--src/mongo/s/config.h9
8 files changed, 106 insertions, 183 deletions
diff --git a/src/mongo/s/commands/SConscript b/src/mongo/s/commands/SConscript
index 123481cf2a6..2d8b0f7bf34 100644
--- a/src/mongo/s/commands/SConscript
+++ b/src/mongo/s/commands/SConscript
@@ -27,7 +27,6 @@ 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 65ed5c6882c..529b39be403 100644
--- a/src/mongo/s/commands/cluster_commands_common.cpp
+++ b/src/mongo/s/commands/cluster_commands_common.cpp
@@ -37,6 +37,8 @@
#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 c45e39b1196..b759c14c45e 100644
--- a/src/mongo/s/commands/cluster_commands_common.h
+++ b/src/mongo/s/commands/cluster_commands_common.h
@@ -38,10 +38,17 @@
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
deleted file mode 100644
index 86d1118a856..00000000000
--- a/src/mongo/s/commands/cluster_drop_cmd.cpp
+++ /dev/null
@@ -1,152 +0,0 @@
-/**
- * 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/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);
- } 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) {
- 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(Grid::get(txn)->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);
- uassertStatusOK(cmdDropResult.writeConcernStatus);
- }
-
-} 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 7dcf02e2213..05ac92c22ed 100644
--- a/src/mongo/s/commands/cluster_drop_database_cmd.cpp
+++ b/src/mongo/s/commands/cluster_drop_database_cmd.cpp
@@ -38,7 +38,6 @@
#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/config.h"
#include "mongo/s/grid.h"
#include "mongo/s/sharding_raii.h"
@@ -115,23 +114,22 @@ 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) {
- uassertStatusOK(catalogClient->dropCollection(txn, NamespaceString(ns)));
- db->markNSNotSharded(ns);
+ const auto collectionShards =
+ _dropShardedCollectionFromConfig(txn, NamespaceString(ns));
+ allShardIds.insert(collectionShards.begin(), collectionShards.end());
}
+
// Drop the database from the primary shard first
_dropDatabaseFromShard(txn, db->getPrimaryId(), dbname);
// Drop the database from each of the remaining shards
- {
- std::vector<ShardId> allShardIds;
- Grid::get(txn)->shardRegistry()->getAllShardIds(&allShardIds);
-
- for (const ShardId& shardId : allShardIds) {
- _dropDatabaseFromShard(txn, shardId, dbname);
- }
+ for (const ShardId& shardId : allShardIds) {
+ _dropDatabaseFromShard(txn, shardId, dbname);
}
// Remove the database entry from the metadata
@@ -159,29 +157,52 @@ public:
private:
/**
- * Sends the 'dropDatabase' command for the specified database to the specified shard. Throws
+ * 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
* 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,
- dropDatabaseCommandBSON,
+ BSON("dropDatabase" << 1 << WriteConcernOptions::kWriteConcernField
+ << txn->getWriteConcern().toBSON()),
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 fe17c4650e1..74e94ffc96e 100644
--- a/src/mongo/s/commands/commands_public.cpp
+++ b/src/mongo/s/commands/commands_public.cpp
@@ -537,6 +537,55 @@ 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") {}
diff --git a/src/mongo/s/config.cpp b/src/mongo/s/config.cpp
index a9edfdaf99b..9a0194d8139 100644
--- a/src/mongo/s/config.cpp
+++ b/src/mongo/s/config.cpp
@@ -152,7 +152,7 @@ bool DBConfig::isSharded(const string& ns) {
return i->second.isSharded();
}
-void DBConfig::markNSNotSharded(const std::string& ns) {
+void DBConfig::invalidateNs(const std::string& ns) {
stdx::lock_guard<stdx::mutex> lk(_lock);
CollectionInfoMap::iterator it = _collections.find(ns);
diff --git a/src/mongo/s/config.h b/src/mongo/s/config.h
index 79893ddb2b2..0cba78036f9 100644
--- a/src/mongo/s/config.h
+++ b/src/mongo/s/config.h
@@ -120,13 +120,10 @@ public:
ShardId getPrimaryId();
/**
- * Removes the specified namespace from the set of collections under this database entry so that
- * from then onwards it will be treated as unsharded.
- *
- * Note that this method doesn't do any writes to the config metadata, but simply drops the
- * specified namespace from the cache.
+ * Removes all cached metadata for the specified namespace so that subsequent attempts to
+ * retrieve it will cause a full reload.
*/
- void markNSNotSharded(const std::string& ns);
+ void invalidateNs(const std::string& ns);
void enableSharding(OperationContext* txn);