diff options
author | A. Jesse Jiryu Davis <jesse@mongodb.com> | 2020-11-13 13:41:06 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-11-13 19:34:57 +0000 |
commit | b670258ac185ecea0492c742f2f50da9d8ab618f (patch) | |
tree | 820931f0cd560702b2f28ce460cadc796b16deaf /src/mongo | |
parent | 3251ffe481e2c981e16e9bbf166cb2de7a493e0b (diff) | |
download | mongo-b670258ac185ecea0492c742f2f50da9d8ab618f.tar.gz |
SERVER-52541 Specify dropDatabase in IDL
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/commands.cpp | 25 | ||||
-rw-r--r-- | src/mongo/db/commands.h | 15 | ||||
-rw-r--r-- | src/mongo/db/commands/dbcommands.cpp | 16 | ||||
-rw-r--r-- | src/mongo/db/drop_database.idl | 58 | ||||
-rw-r--r-- | src/mongo/db/s/config/configsvr_move_primary_command.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/s/config/configsvr_shard_collection_command.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/s/move_primary_source_manager.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_add_shard_cmd.cpp | 3 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_drop_cmd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_drop_database_cmd.cpp | 14 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_enable_sharding_cmd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_map_reduce_agg.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_move_primary_cmd.cpp | 2 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_remove_shard_cmd.cpp | 4 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_shard_collection_cmd.cpp | 2 |
16 files changed, 124 insertions, 28 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index 698477f284b..83723ba26a5 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -568,6 +568,7 @@ env.Library( target="commands", source=[ 'commands.cpp', + 'drop_database.idl', ], LIBDEPS=[ '$BUILD_DIR/mongo/base', diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp index 65233e75a94..68fa01719d5 100644 --- a/src/mongo/db/commands.cpp +++ b/src/mongo/db/commands.cpp @@ -396,11 +396,11 @@ void CommandHelpers::appendCommandWCStatus(BSONObjBuilder& result, } } -BSONObj CommandHelpers::appendPassthroughFields(const BSONObj& cmdObjWithPassthroughFields, - const BSONObj& request) { +BSONObj CommandHelpers::appendGenericCommandArgs(const BSONObj& cmdObjWithGenericArgs, + const BSONObj& request) { BSONObjBuilder b; b.appendElements(request); - for (const auto& elem : filterCommandRequestForPassthrough(cmdObjWithPassthroughFields)) { + for (const auto& elem : filterCommandRequestForPassthrough(cmdObjWithGenericArgs)) { const auto name = elem.fieldNameStringData(); if (isGenericArgument(name) && !request.hasField(name)) { b.append(elem); @@ -409,6 +409,25 @@ BSONObj CommandHelpers::appendPassthroughFields(const BSONObj& cmdObjWithPassthr return b.obj(); } +void CommandHelpers::appendGenericReplyFields(const BSONObj& replyObjWithGenericReplyFields, + const BSONObj& reply, + BSONObjBuilder* replyBuilder) { + replyBuilder->appendElements(reply); + for (const auto& elem : filterCommandReplyForPassthrough(replyObjWithGenericReplyFields)) { + const auto name = elem.fieldNameStringData(); + if (isGenericArgument(name) && !reply.hasField(name)) { + replyBuilder->append(elem); + } + } +} + +BSONObj CommandHelpers::appendGenericReplyFields(const BSONObj& replyObjWithGenericReplyFields, + const BSONObj& reply) { + BSONObjBuilder b; + appendGenericReplyFields(replyObjWithGenericReplyFields, reply, &b); + return b.obj(); +} + BSONObj CommandHelpers::appendMajorityWriteConcern(const BSONObj& cmdObj, WriteConcernOptions defaultWC) { WriteConcernOptions newWC = kMajorityWriteConcern; diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h index e1805991cab..7cbdf08defb 100644 --- a/src/mongo/db/commands.h +++ b/src/mongo/db/commands.h @@ -191,10 +191,19 @@ struct CommandHelpers { const WriteConcernResult& wcResult = WriteConcernResult()); /** - * Appends passthrough fields from a cmdObj to a given request. + * Forward generic arguments from a client request to shards. */ - static BSONObj appendPassthroughFields(const BSONObj& cmdObjWithPassthroughFields, - const BSONObj& request); + static BSONObj appendGenericCommandArgs(const BSONObj& cmdObjWithGenericArgs, + const BSONObj& request); + + /** + * Forward generic reply fields from a shard's reply to the client. + */ + static void appendGenericReplyFields(const BSONObj& replyObjWithGenericReplyFields, + const BSONObj& reply, + BSONObjBuilder* replyBuilder); + static BSONObj appendGenericReplyFields(const BSONObj& replyObjWithGenericReplyFields, + const BSONObj& reply); /** * Returns a copy of 'cmdObj' with a majority writeConcern appended. If the command object does diff --git a/src/mongo/db/commands/dbcommands.cpp b/src/mongo/db/commands/dbcommands.cpp index 6d22f29867f..1306678f0e7 100644 --- a/src/mongo/db/commands/dbcommands.cpp +++ b/src/mongo/db/commands/dbcommands.cpp @@ -62,6 +62,7 @@ #include "mongo/db/db_raii.h" #include "mongo/db/dbdirectclient.h" #include "mongo/db/dbhelpers.h" +#include "mongo/db/drop_database_gen.h" #include "mongo/db/exec/working_set_common.h" #include "mongo/db/index/index_access_method.h" #include "mongo/db/index/index_descriptor.h" @@ -155,20 +156,21 @@ public: str::stream() << "Cannot drop '" << dbname << "' database while replication is active"); } - BSONElement e = cmdObj.firstElement(); - int p = (int)e.number(); - if (p != 1) { + + auto request = DropDatabase::parse(IDLParserErrorContext("dropDatabase"), cmdObj); + if (request.getCommandParameter() != 1) { uasserted(ErrorCodes::IllegalOperation, "have to pass 1 as db parameter"); } Status status = dropDatabase(opCtx, dbname); - if (status == ErrorCodes::NamespaceNotFound) { - return true; + if (status != ErrorCodes::NamespaceNotFound) { + uassertStatusOK(status); } + DropDatabaseReply reply; if (status.isOK()) { - result.append("dropped", dbname); + reply.setDropped(request.getDbName()); } - uassertStatusOK(status); + reply.serialize(&result); return true; } diff --git a/src/mongo/db/drop_database.idl b/src/mongo/db/drop_database.idl new file mode 100644 index 00000000000..65871ce89f8 --- /dev/null +++ b/src/mongo/db/drop_database.idl @@ -0,0 +1,58 @@ +# Copyright (C) 2020-present MongoDB, Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Server Side Public License, version 1, +# as published by MongoDB, Inc. +# +# 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 +# Server Side Public License for more details. +# +# You should have received a copy of the Server Side Public License +# along with this program. If not, see +# <http://www.mongodb.com/licensing/server-side-public-license>. +# +# 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 Server Side 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. +# + +global: + cpp_namespace: "mongo" + +imports: + - "mongo/idl/basic_types.idl" + +structs: + DropDatabaseReply: + description: "The dropDatabase command's reply" + strict: false + fields: + dropped: + description: "The database's name, if it existed and was dropped" + type: string + optional: true + info: + description: "May provide information on dropDatabase's outcome" + type: string + optional: true + +commands: + dropDatabase: + description: "Parser for the dropDatabase command" + command_name: dropDatabase + namespace: type + type: safeInt64 + cpp_name: DropDatabase + strict: true + api_version: "1" + reply_type: DropDatabaseReply diff --git a/src/mongo/db/s/config/configsvr_move_primary_command.cpp b/src/mongo/db/s/config/configsvr_move_primary_command.cpp index 07e6b74d91c..8df30873d90 100644 --- a/src/mongo/db/s/config/configsvr_move_primary_command.cpp +++ b/src/mongo/db/s/config/configsvr_move_primary_command.cpp @@ -192,7 +192,7 @@ public: ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields(cmdObj, shardMovePrimaryRequest.toBSON())), + CommandHelpers::appendGenericCommandArgs(cmdObj, shardMovePrimaryRequest.toBSON())), Shard::RetryPolicy::kIdempotent)); CommandHelpers::filterCommandReplyForPassthrough(cmdResponse.response, &result); diff --git a/src/mongo/db/s/config/configsvr_shard_collection_command.cpp b/src/mongo/db/s/config/configsvr_shard_collection_command.cpp index 6ea9ee9a212..0ed4daa417d 100644 --- a/src/mongo/db/s/config/configsvr_shard_collection_command.cpp +++ b/src/mongo/db/s/config/configsvr_shard_collection_command.cpp @@ -343,7 +343,7 @@ public: opCtx, ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", - CommandHelpers::appendMajorityWriteConcern(CommandHelpers::appendPassthroughFields( + CommandHelpers::appendMajorityWriteConcern(CommandHelpers::appendGenericCommandArgs( cmdObj, shardsvrShardCollectionRequest.toBSON())), Shard::RetryPolicy::kIdempotent)); diff --git a/src/mongo/db/s/move_primary_source_manager.cpp b/src/mongo/db/s/move_primary_source_manager.cpp index 53648cd79ef..c3dc50fc304 100644 --- a/src/mongo/db/s/move_primary_source_manager.cpp +++ b/src/mongo/db/s/move_primary_source_manager.cpp @@ -238,7 +238,7 @@ Status MovePrimarySourceManager::commitOnConfig(OperationContext* opCtx) { opCtx, ReadPreferenceSetting{ReadPreference::PrimaryOnly}, "admin", - CommandHelpers::appendMajorityWriteConcern(CommandHelpers::appendPassthroughFields( + CommandHelpers::appendMajorityWriteConcern(CommandHelpers::appendGenericCommandArgs( finalCommandObj, commitMovePrimaryRequest.toBSON())), Shard::RetryPolicy::kIdempotent); diff --git a/src/mongo/s/commands/cluster_add_shard_cmd.cpp b/src/mongo/s/commands/cluster_add_shard_cmd.cpp index c6d943a6740..11cc10beab7 100644 --- a/src/mongo/s/commands/cluster_add_shard_cmd.cpp +++ b/src/mongo/s/commands/cluster_add_shard_cmd.cpp @@ -85,7 +85,8 @@ public: kPrimaryOnlyReadPreference, "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields(cmdObj, parsedRequest.toCommandForConfig()), + CommandHelpers::appendGenericCommandArgs(cmdObj, + parsedRequest.toCommandForConfig()), opCtx->getWriteConcern()), Shard::RetryPolicy::kIdempotent); diff --git a/src/mongo/s/commands/cluster_drop_cmd.cpp b/src/mongo/s/commands/cluster_drop_cmd.cpp index f727489ccc0..da31d35765a 100644 --- a/src/mongo/s/commands/cluster_drop_cmd.cpp +++ b/src/mongo/s/commands/cluster_drop_cmd.cpp @@ -98,7 +98,7 @@ public: ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields( + CommandHelpers::appendGenericCommandArgs( cmdObj, BSON("_configsvrDropCollection" << nss.toString())), opCtx->getWriteConcern()), Shard::RetryPolicy::kIdempotent)); diff --git a/src/mongo/s/commands/cluster_drop_database_cmd.cpp b/src/mongo/s/commands/cluster_drop_database_cmd.cpp index a78cb00fd79..f96a2cef9fc 100644 --- a/src/mongo/s/commands/cluster_drop_database_cmd.cpp +++ b/src/mongo/s/commands/cluster_drop_database_cmd.cpp @@ -33,6 +33,7 @@ #include "mongo/base/status.h" #include "mongo/db/commands.h" +#include "mongo/db/drop_database_gen.h" #include "mongo/db/operation_context.h" #include "mongo/s/catalog_cache.h" #include "mongo/s/client/shard_registry.h" @@ -82,9 +83,10 @@ public: "Cannot drop the admin database", dbname != NamespaceString::kAdminDb); + auto request = DropDatabase::parse(IDLParserErrorContext("dropDatabase"), cmdObj); uassert(ErrorCodes::BadValue, "have to pass 1 as db parameter", - cmdObj.firstElement().isNumber() && cmdObj.firstElement().number() == 1); + request.getCommandParameter() == 1); // Invalidate the database metadata so the next access kicks off a full reload, even if // sending the command to the config server fails due to e.g. a NetworkError. @@ -97,12 +99,16 @@ public: ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields(cmdObj, - BSON("_configsvrDropDatabase" << dbname)), + CommandHelpers::appendGenericCommandArgs(cmdObj, + BSON("_configsvrDropDatabase" << dbname)), opCtx->getWriteConcern()), Shard::RetryPolicy::kIdempotent)); - CommandHelpers::filterCommandReplyForPassthrough(cmdResponse.response, &result); + // The cmdResponse status can be OK even if the config server replied ok: 0. + uassertStatusOK(cmdResponse.commandStatus); + auto reply = DropDatabaseReply::parse(IDLParserErrorContext("dropDatabase-reply"), + cmdResponse.response); + CommandHelpers::appendGenericReplyFields(cmdResponse.response, reply.toBSON(), &result); return true; } diff --git a/src/mongo/s/commands/cluster_enable_sharding_cmd.cpp b/src/mongo/s/commands/cluster_enable_sharding_cmd.cpp index 4ae07e0264c..9feea9a8a8e 100644 --- a/src/mongo/s/commands/cluster_enable_sharding_cmd.cpp +++ b/src/mongo/s/commands/cluster_enable_sharding_cmd.cpp @@ -113,7 +113,7 @@ public: ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields(cmdObj, remoteCmdObj.obj()), + CommandHelpers::appendGenericCommandArgs(cmdObj, remoteCmdObj.obj()), opCtx->getWriteConcern()), Shard::RetryPolicy::kIdempotent)); diff --git a/src/mongo/s/commands/cluster_map_reduce_agg.cpp b/src/mongo/s/commands/cluster_map_reduce_agg.cpp index 58ac2b0129e..3f23738d141 100644 --- a/src/mongo/s/commands/cluster_map_reduce_agg.cpp +++ b/src/mongo/s/commands/cluster_map_reduce_agg.cpp @@ -132,7 +132,7 @@ Document serializeToCommand(BSONObj originalCmd, const MapReduce& parsedMr, Pipe } // Append generic command options. - for (const auto& elem : CommandHelpers::appendPassthroughFields(originalCmd, BSONObj())) { + for (const auto& elem : CommandHelpers::appendGenericCommandArgs(originalCmd, BSONObj())) { translatedCmd[elem.fieldNameStringData()] = Value(elem); } return translatedCmd.freeze(); diff --git a/src/mongo/s/commands/cluster_move_primary_cmd.cpp b/src/mongo/s/commands/cluster_move_primary_cmd.cpp index 35919c5c348..596fc0282ad 100644 --- a/src/mongo/s/commands/cluster_move_primary_cmd.cpp +++ b/src/mongo/s/commands/cluster_move_primary_cmd.cpp @@ -116,7 +116,7 @@ public: ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields(cmdObj, configMovePrimaryRequest.toBSON()), + CommandHelpers::appendGenericCommandArgs(cmdObj, configMovePrimaryRequest.toBSON()), opCtx->getWriteConcern()), Shard::RetryPolicy::kIdempotent)); diff --git a/src/mongo/s/commands/cluster_remove_shard_cmd.cpp b/src/mongo/s/commands/cluster_remove_shard_cmd.cpp index 0e2472be0c2..1690c48b79c 100644 --- a/src/mongo/s/commands/cluster_remove_shard_cmd.cpp +++ b/src/mongo/s/commands/cluster_remove_shard_cmd.cpp @@ -85,8 +85,8 @@ public: ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields(cmdObj, - BSON("_configsvrRemoveShard" << target)), + CommandHelpers::appendGenericCommandArgs(cmdObj, + BSON("_configsvrRemoveShard" << target)), opCtx->getWriteConcern()), Shard::RetryPolicy::kIdempotent)); uassertStatusOK(cmdResponseStatus.commandStatus); diff --git a/src/mongo/s/commands/cluster_shard_collection_cmd.cpp b/src/mongo/s/commands/cluster_shard_collection_cmd.cpp index d4c4d7901ad..3c95e9ba0d3 100644 --- a/src/mongo/s/commands/cluster_shard_collection_cmd.cpp +++ b/src/mongo/s/commands/cluster_shard_collection_cmd.cpp @@ -115,7 +115,7 @@ public: ReadPreferenceSetting(ReadPreference::PrimaryOnly), "admin", CommandHelpers::appendMajorityWriteConcern( - CommandHelpers::appendPassthroughFields(cmdObj, configShardCollRequest.toBSON()), + CommandHelpers::appendGenericCommandArgs(cmdObj, configShardCollRequest.toBSON()), opCtx->getWriteConcern()), Shard::RetryPolicy::kIdempotent)); |