summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoanna Huang <joannahuang@Joannas-MacBook-Pro.local>2017-08-07 16:34:18 -0400
committerJoanna Huang <joannahuang@Joannas-MacBook-Pro.local>2017-08-17 16:42:07 -0400
commitd7325950d72c6aacd25ecbd65888c050fe63482c (patch)
tree1116db8356886e1814705b3165e3451479aa5694
parent28c94950e960688fa6f7ac8af12c32c8e9e8ed9c (diff)
downloadmongo-d7325950d72c6aacd25ecbd65888c050fe63482c.tar.gz
SERVER-30534 Create a _configsvrDropCollection command
-rw-r--r--jstests/core/views/views_all_commands.js1
-rw-r--r--src/mongo/db/s/SConscript1
-rw-r--r--src/mongo/db/s/config/configsvr_drop_collection_command.cpp145
3 files changed, 147 insertions, 0 deletions
diff --git a/jstests/core/views/views_all_commands.js b/jstests/core/views/views_all_commands.js
index 61e209f789c..03b1695ac32 100644
--- a/jstests/core/views/views_all_commands.js
+++ b/jstests/core/views/views_all_commands.js
@@ -67,6 +67,7 @@
_configsvrCommitChunkMigration: {skip: isAnInternalCommand},
_configsvrCommitChunkSplit: {skip: isAnInternalCommand},
_configsvrCreateDatabase: {skip: isAnInternalCommand},
+ _configsvrDropCollection: {skip: isAnInternalCommand},
_configsvrEnableSharding: {skip: isAnInternalCommand},
_configsvrMoveChunk: {skip: isAnInternalCommand},
_configsvrMovePrimary: {skip: isAnInternalCommand},
diff --git a/src/mongo/db/s/SConscript b/src/mongo/db/s/SConscript
index baa5f344a41..86292a29021 100644
--- a/src/mongo/db/s/SConscript
+++ b/src/mongo/db/s/SConscript
@@ -167,6 +167,7 @@ env.Library(
'config/configsvr_commit_chunk_migration_command.cpp',
'config/configsvr_control_balancer_command.cpp',
'config/configsvr_create_database_command.cpp',
+ 'config/configsvr_drop_collection_command.cpp',
'config/configsvr_enable_sharding_command.cpp',
'config/configsvr_merge_chunk_command.cpp',
'config/configsvr_move_chunk_command.cpp',
diff --git a/src/mongo/db/s/config/configsvr_drop_collection_command.cpp b/src/mongo/db/s/config/configsvr_drop_collection_command.cpp
new file mode 100644
index 00000000000..33b78b8c2ba
--- /dev/null
+++ b/src/mongo/db/s/config/configsvr_drop_collection_command.cpp
@@ -0,0 +1,145 @@
+/**
+ * Copyright (C) 2017 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/auth/authorization_session.h"
+#include "mongo/db/commands.h"
+#include "mongo/db/operation_context.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_helpers.h"
+#include "mongo/s/grid.h"
+#include "mongo/s/stale_exception.h"
+#include "mongo/util/log.h"
+
+namespace mongo {
+namespace {
+
+class ConfigSvrDropCollectionCommand : public BasicCommand {
+public:
+ ConfigSvrDropCollectionCommand() : BasicCommand("_configsvrDropCollection") {}
+
+ bool slaveOk() const override {
+ return false;
+ }
+
+ bool adminOnly() const override {
+ return true;
+ }
+
+ bool supportsWriteConcern(const BSONObj& cmd) const override {
+ return true;
+ }
+
+ void help(std::stringstream& help) const override {
+ help << "Internal command, which is exported by the sharding config server. Do not call "
+ "directly. Drops a collection.";
+ }
+
+ Status checkAuthForCommand(Client* client,
+ const std::string& dbname,
+ const BSONObj& cmdObj) override {
+ if (!AuthorizationSession::get(client)->isAuthorizedForActionsOnResource(
+ ResourcePattern::forClusterResource(), ActionType::internal)) {
+ return Status(ErrorCodes::Unauthorized, "Unauthorized");
+ }
+ return Status::OK();
+ }
+
+ bool run(OperationContext* opCtx,
+ const std::string& dbname,
+ const BSONObj& cmdObj,
+ BSONObjBuilder& result) override {
+ return true;
+ }
+
+private:
+ /**
+ * Sends the 'drop' command for the specified collection to the specified shard. Throws
+ * DBException on failure.
+ */
+ static void _dropUnshardedCollectionFromShard(OperationContext* opCtx,
+ const ShardId& shardId,
+ const NamespaceString& nss,
+ BSONObjBuilder* result) {
+ const auto catalogClient = Grid::get(opCtx)->catalogClient();
+ const auto shardRegistry = Grid::get(opCtx)->shardRegistry();
+
+ auto scopedDistLock = uassertStatusOK(catalogClient->getDistLockManager()->lock(
+ opCtx, nss.ns(), "drop", DistLockManager::kDefaultLockTimeout));
+
+ const auto dropCommandBSON = [shardRegistry, opCtx, &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 (!opCtx->getWriteConcern().usedDefault) {
+ builder.append(WriteConcernOptions::kWriteConcernField,
+ opCtx->getWriteConcern().toBSON());
+ }
+
+ return builder.obj();
+ }();
+
+ const auto shard = uassertStatusOK(shardRegistry->getShard(opCtx, shardId));
+ auto cmdDropResult = uassertStatusOK(shard->runCommandWithFixedRetryAttempts(
+ opCtx,
+ 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);
+ }
+ }
+
+} configsvrDropCollectionCmd;
+
+} // namespace
+} // namespace mongo