diff options
author | Mathias Stearn <mathias@10gen.com> | 2017-05-01 14:42:30 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2017-05-12 12:07:30 -0400 |
commit | cb3e9d87a1150fcfd90b7db3e289e298c5c70bff (patch) | |
tree | 5b933d5a40a4e5deba1e684bb435e1ca8bbf1eb9 /src/mongo/s | |
parent | f9b8a9eb2d196d93a08714c9bbcecbb7fe30a5f3 (diff) | |
download | mongo-cb3e9d87a1150fcfd90b7db3e289e298c5c70bff.tar.gz |
SERVER-28814 Clean up ClusterExplain::wrapAsExplain()
Diffstat (limited to 'src/mongo/s')
-rw-r--r-- | src/mongo/s/commands/cluster_count_cmd.cpp | 7 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_explain.cpp | 40 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_explain.h | 33 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_explain_cmd.cpp | 19 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_find_and_modify_cmd.cpp | 7 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_write_cmd.cpp | 5 | ||||
-rw-r--r-- | src/mongo/s/commands/commands_public.cpp | 22 | ||||
-rw-r--r-- | src/mongo/s/commands/strategy.cpp | 5 |
8 files changed, 46 insertions, 92 deletions
diff --git a/src/mongo/s/commands/cluster_count_cmd.cpp b/src/mongo/s/commands/cluster_count_cmd.cpp index d100c4c6f91..2389a7c9b97 100644 --- a/src/mongo/s/commands/cluster_count_cmd.cpp +++ b/src/mongo/s/commands/cluster_count_cmd.cpp @@ -265,8 +265,7 @@ public: return status; } - BSONObjBuilder explainCmdBob; - ClusterExplain::wrapAsExplain(cmdObj, verbosity, &explainCmdBob); + const auto explainCmd = ClusterExplain::wrapAsExplain(cmdObj, verbosity); // We will time how long it takes to run the commands on the shards Timer timer; @@ -274,8 +273,8 @@ public: BSONObj viewDefinition; auto swShardResponses = scatterGatherForNamespace(opCtx, nss, - explainCmdBob.obj(), - getReadPref(ssm), + explainCmd, + getReadPref(explainCmd), targetingQuery, targetingCollation, true, // do shard versioning diff --git a/src/mongo/s/commands/cluster_explain.cpp b/src/mongo/s/commands/cluster_explain.cpp index 3add2b0e580..59f4ca83cc5 100644 --- a/src/mongo/s/commands/cluster_explain.cpp +++ b/src/mongo/s/commands/cluster_explain.cpp @@ -31,7 +31,6 @@ #include "mongo/bson/bsonmisc.h" #include "mongo/db/commands.h" #include "mongo/rpc/get_status_from_command_result.h" -#include "mongo/rpc/metadata/server_selection_metadata.h" #include "mongo/s/client/shard_registry.h" #include "mongo/s/commands/cluster_explain.h" #include "mongo/s/grid.h" @@ -130,35 +129,20 @@ std::vector<Strategy::CommandResult> ClusterExplain::downconvert( } // static -void ClusterExplain::wrapAsExplain(const BSONObj& cmdObj, - ExplainOptions::Verbosity verbosity, - BSONObjBuilder* explainBuilder) { - explainBuilder->append("explain", cmdObj); - explainBuilder->append("verbosity", ExplainOptions::verbosityString(verbosity)); - - // Propagate readConcern - if (auto readConcern = cmdObj["readConcern"]) { - explainBuilder->append(readConcern); +BSONObj ClusterExplain::wrapAsExplain(const BSONObj& cmdObj, ExplainOptions::Verbosity verbosity) { + BSONObjBuilder out; + out.append("explain", cmdObj); + out.append("verbosity", ExplainOptions::verbosityString(verbosity)); + + // Propagate all generic arguments out of the inner command since the shards will only process + // them at the top level. + for (auto elem : cmdObj) { + if (Command::isGenericArgument(elem.fieldNameStringData())) { + out.append(elem); + } } -} -// static -void ClusterExplain::wrapAsExplainDeprecated( - const BSONObj& cmdObj, - ExplainOptions::Verbosity verbosity, - const rpc::ServerSelectionMetadata& serverSelectionMetadata, - BSONObjBuilder* out, - int* optionsOut) { - BSONObjBuilder explainBuilder; - wrapAsExplain(cmdObj, verbosity, &explainBuilder); - const BSONObj explainCmdObj = explainBuilder.done(); - - // Attach metadata to the explain command in legacy format. - BSONObjBuilder metadataBuilder; - serverSelectionMetadata.writeToMetadata(&metadataBuilder); - const BSONObj metadataObj = metadataBuilder.done(); - uassertStatusOK( - serverSelectionMetadata.downconvert(explainCmdObj, metadataObj, out, optionsOut)); + return out.obj(); } // static diff --git a/src/mongo/s/commands/cluster_explain.h b/src/mongo/s/commands/cluster_explain.h index 1bc72aacd6e..01542b2b16f 100644 --- a/src/mongo/s/commands/cluster_explain.h +++ b/src/mongo/s/commands/cluster_explain.h @@ -39,10 +39,6 @@ namespace mongo { class OperationContext; -namespace rpc { -class ServerSelectionMetadata; -} // namespace rpc - /** * Namespace for the collection of static methods used by commands in the implementation of * explain on mongos. @@ -61,33 +57,10 @@ public: OperationContext* opCtx, const std::vector<AsyncRequestsSender::Response>& responses); /** - * Given the BSON specification for a command, 'cmdObj', wraps the object in order to produce - * the BSON for an explain of that command, at the given verbosity level 'verbosity.' - * - * Adds the result to the BSONObjBuidler 'out'. - * - * Unlike wrapAsExplain, does not downconvert the command to OP_QUERY. Should be used for paths - * that send the command over the NetworkInterfaceASIO rather than DBClient. - */ - static void wrapAsExplain(const BSONObj& cmdObj, - ExplainOptions::Verbosity verbosity, - BSONObjBuilder* explainBuilder); - - /** - * Given the BSON specification for a command, 'cmdObj', wraps the object in order to - * produce the BSON for an explain of that command, at the given verbosity level - * 'verbosity' and according to the metadata in 'serverSelectionMetadata'. - * - * Adds the result to the BSONObj builder 'out'. - * - * Also uses 'serverSelectionMetdata' to set 'optionsOut' to the options bit vector that should - * be forwarded to the shards. + * Returns an explain command request wrapping the passed in command at the given verbosity + * level, propagating generic top-level command arguments. */ - static void wrapAsExplainDeprecated(const BSONObj& cmdObj, - ExplainOptions::Verbosity verbosity, - const rpc::ServerSelectionMetadata& serverSelectionMetadata, - BSONObjBuilder* out, - int* optionsOut); + static BSONObj wrapAsExplain(const BSONObj& cmdObj, ExplainOptions::Verbosity verbosity); /** * Determines the kind of "execution stage" that mongos would use in order to collect diff --git a/src/mongo/s/commands/cluster_explain_cmd.cpp b/src/mongo/s/commands/cluster_explain_cmd.cpp index fed93b5247e..db053de01a0 100644 --- a/src/mongo/s/commands/cluster_explain_cmd.cpp +++ b/src/mongo/s/commands/cluster_explain_cmd.cpp @@ -116,8 +116,23 @@ public: return appendCommandStatus(result, verbosity.getStatus()); } - // This is the nested command which we are explaining. - BSONObj explainObj = cmdObj.firstElement().Obj(); + // This is the nested command which we are explaining. We need to propagate generic + // arguments into the inner command since it is what is passed to the virtual + // Command::explain() method. + const BSONObj explainObj = ([&] { + const auto innerObj = cmdObj.firstElement().Obj(); + BSONObjBuilder bob; + bob.appendElements(innerObj); + for (auto outerElem : cmdObj) { + // If the argument is in both the inner and outer command, we currently let the + // inner version take precedence. + const auto name = outerElem.fieldNameStringData(); + if (Command::isGenericArgument(name) && !innerObj.hasField(name)) { + bob.append(outerElem); + } + } + return bob.obj(); + }()); const std::string cmdName = explainObj.firstElementFieldName(); Command* commToExplain = Command::findCommand(cmdName); diff --git a/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp b/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp index befc371efdb..f16c508ca59 100644 --- a/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp +++ b/src/mongo/s/commands/cluster_find_and_modify_cmd.cpp @@ -128,16 +128,13 @@ public: shard = shardStatus.getValue(); } - BSONObjBuilder explainCmd; - int options = 0; - ClusterExplain::wrapAsExplainDeprecated( - cmdObj, verbosity, serverSelectionMetadata, &explainCmd, &options); + const auto explainCmd = ClusterExplain::wrapAsExplain(cmdObj, verbosity); // Time how long it takes to run the explain command on the shard. Timer timer; BSONObjBuilder result; - bool ok = _runCommand(opCtx, chunkMgr, shard->getId(), nss, explainCmd.obj(), result); + bool ok = _runCommand(opCtx, chunkMgr, shard->getId(), nss, explainCmd, result); long long millisElapsed = timer.millis(); if (!ok) { diff --git a/src/mongo/s/commands/cluster_write_cmd.cpp b/src/mongo/s/commands/cluster_write_cmd.cpp index 38cf186155c..dc9ab0c27b9 100644 --- a/src/mongo/s/commands/cluster_write_cmd.cpp +++ b/src/mongo/s/commands/cluster_write_cmd.cpp @@ -110,8 +110,7 @@ public: return Status(ErrorCodes::InvalidLength, "explained write batches must be of size 1"); } - BSONObjBuilder explainCmdBob; - ClusterExplain::wrapAsExplain(cmdObj, verbosity, &explainCmdBob); + const auto explainCmd = ClusterExplain::wrapAsExplain(cmdObj, verbosity); // We will time how long it takes to run the commands on the shards. Timer timer; @@ -120,7 +119,7 @@ public: BatchItemRef targetingBatchItem(&request, 0); vector<Strategy::CommandResult> shardResults; Status status = - _commandOpWrite(opCtx, dbname, explainCmdBob.obj(), targetingBatchItem, &shardResults); + _commandOpWrite(opCtx, dbname, explainCmd, targetingBatchItem, &shardResults); if (!status.isOK()) { return status; } diff --git a/src/mongo/s/commands/commands_public.cpp b/src/mongo/s/commands/commands_public.cpp index 5d006fb2c85..208539821b9 100644 --- a/src/mongo/s/commands/commands_public.cpp +++ b/src/mongo/s/commands/commands_public.cpp @@ -52,7 +52,6 @@ #include "mongo/db/views/resolved_view.h" #include "mongo/executor/task_executor_pool.h" #include "mongo/rpc/get_status_from_command_result.h" -#include "mongo/rpc/metadata/server_selection_metadata.h" #include "mongo/s/async_requests_sender.h" #include "mongo/s/catalog/sharding_catalog_client.h" #include "mongo/s/catalog_cache.h" @@ -893,17 +892,7 @@ public: BSONObjBuilder* out) const override { // We will time how long it takes to run the commands on the shards. Timer timer; - - BSONObj command; - int options = 0; - - { - BSONObjBuilder explainCmdBob; - ClusterExplain::wrapAsExplainDeprecated( - cmdObj, verbosity, serverSelectionMetadata, &explainCmdBob, &options); - command = explainCmdBob.obj(); - } - + BSONObj command = ClusterExplain::wrapAsExplain(cmdObj, verbosity); const NamespaceString nss(parseNs(dbname, cmdObj)); auto routingInfo = @@ -919,7 +908,7 @@ public: ShardConnection conn(routingInfo.primary()->getConnString(), ""); // TODO: this can throw a stale config when mongos is not up-to-date -- fix. - if (!conn->runCommand(nss.db().toString(), command, shardResult, options)) { + if (!conn->runCommand(nss.db().toString(), command, shardResult)) { conn.done(); return Status(ErrorCodes::OperationFailed, str::stream() << "Passthrough command failed: " << command @@ -1150,8 +1139,7 @@ public: // Extract the targeting collation. auto targetingCollation = uassertStatusOK(getCollation(cmdObj)); - BSONObjBuilder explainCmdBob; - ClusterExplain::wrapAsExplain(cmdObj, verbosity, &explainCmdBob); + const auto explainCmd = ClusterExplain::wrapAsExplain(cmdObj, verbosity); // We will time how long it takes to run the commands on the shards. Timer timer; @@ -1159,8 +1147,8 @@ public: BSONObj viewDefinition; auto swShardResponses = scatterGatherForNamespace(opCtx, nss, - explainCmdBob.obj(), - getReadPref(ssm), + explainCmd, + getReadPref(explainCmd), targetingQuery, targetingCollation, true, // do shard versioning diff --git a/src/mongo/s/commands/strategy.cpp b/src/mongo/s/commands/strategy.cpp index fb7166c6a28..707b728efe4 100644 --- a/src/mongo/s/commands/strategy.cpp +++ b/src/mongo/s/commands/strategy.cpp @@ -619,8 +619,7 @@ Status Strategy::explainFind(OperationContext* opCtx, ExplainOptions::Verbosity verbosity, const rpc::ServerSelectionMetadata& ssm, BSONObjBuilder* out) { - BSONObjBuilder explainCmdBob; - ClusterExplain::wrapAsExplain(findCommand, verbosity, &explainCmdBob); + const auto explainCmd = ClusterExplain::wrapAsExplain(findCommand, verbosity); // We will time how long it takes to run the commands on the shards. Timer timer; @@ -628,7 +627,7 @@ Status Strategy::explainFind(OperationContext* opCtx, BSONObj viewDefinition; auto swShardResponses = scatterGatherForNamespace(opCtx, qr.nss(), - explainCmdBob.obj(), + explainCmd, getReadPref(ssm), qr.getFilter(), qr.getCollation(), |