summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-12-13 18:09:56 -0500
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2016-12-15 18:44:38 -0500
commit20bc3f49fafb2c8a8d9e8224e8723cd767549d2c (patch)
tree761ad67f8b7f265f3c901c3450dca8dbe3babd69
parent88dd11b030dbbc47332d42d431a40865cc534435 (diff)
downloadmongo-20bc3f49fafb2c8a8d9e8224e8723cd767549d2c.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.
-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, 183 insertions, 106 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..86d1118a856
--- /dev/null
+++ b/src/mongo/s/commands/cluster_drop_cmd.cpp
@@ -0,0 +1,152 @@
+/**
+ * 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 05ac92c22ed..7dcf02e2213 100644
--- a/src/mongo/s/commands/cluster_drop_database_cmd.cpp
+++ b/src/mongo/s/commands/cluster_drop_database_cmd.cpp
@@ -38,6 +38,7 @@
#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"
@@ -114,22 +115,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 +159,29 @@ 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") {}
diff --git a/src/mongo/s/config.cpp b/src/mongo/s/config.cpp
index 9a0194d8139..a9edfdaf99b 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::invalidateNs(const std::string& ns) {
+void DBConfig::markNSNotSharded(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 0cba78036f9..79893ddb2b2 100644
--- a/src/mongo/s/config.h
+++ b/src/mongo/s/config.h
@@ -120,10 +120,13 @@ public:
ShardId getPrimaryId();
/**
- * Removes all cached metadata for the specified namespace so that subsequent attempts to
- * retrieve it will cause a full reload.
+ * 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.
*/
- void invalidateNs(const std::string& ns);
+ void markNSNotSharded(const std::string& ns);
void enableSharding(OperationContext* txn);