diff options
Diffstat (limited to 'src/mongo/s/commands')
-rw-r--r-- | src/mongo/s/commands/cluster_find_cmd.cpp | 72 | ||||
-rw-r--r-- | src/mongo/s/commands/strategy.cpp | 54 | ||||
-rw-r--r-- | src/mongo/s/commands/strategy.h | 6 |
3 files changed, 67 insertions, 65 deletions
diff --git a/src/mongo/s/commands/cluster_find_cmd.cpp b/src/mongo/s/commands/cluster_find_cmd.cpp index 0403314e50b..20508a7fc66 100644 --- a/src/mongo/s/commands/cluster_find_cmd.cpp +++ b/src/mongo/s/commands/cluster_find_cmd.cpp @@ -56,31 +56,29 @@ using std::vector; const char kTermField[] = "term"; -// Parses the command object to a QueryRequest, validates that no runtime constants were supplied +// Parses the command object to a FindCommand, validates that no runtime constants were supplied // with the command, and sets the constant runtime values that will be forwarded to each shard. -std::unique_ptr<QueryRequest> parseCmdObjectToQueryRequest(OperationContext* opCtx, - NamespaceString nss, - BSONObj cmdObj, - bool isExplain) { - auto qr = - QueryRequest::makeFromFindCommand(std::move(cmdObj), - isExplain, - std::move(nss), - APIParameters::get(opCtx).getAPIStrict().value_or(false)); - if (!qr->getReadConcern()) { +std::unique_ptr<FindCommand> parseCmdObjectToFindCommand(OperationContext* opCtx, + NamespaceString nss, + BSONObj cmdObj) { + auto findCommand = query_request_helper::makeFromFindCommand( + std::move(cmdObj), + std::move(nss), + APIParameters::get(opCtx).getAPIStrict().value_or(false)); + if (!findCommand->getReadConcern()) { if (opCtx->isStartingMultiDocumentTransaction() || !opCtx->inMultiDocumentTransaction()) { // If there is no explicit readConcern in the cmdObj, and this is either the first // operation in a transaction, or not running in a transaction, then use the readConcern // from the opCtx (which may be a cluster-wide default). const auto& readConcernArgs = repl::ReadConcernArgs::get(opCtx); - qr->setReadConcern(readConcernArgs.toBSONInner()); + findCommand->setReadConcern(readConcernArgs.toBSONInner()); } } uassert(51202, "Cannot specify runtime constants option to a mongos", - !qr->getLegacyRuntimeConstants()); - qr->setLegacyRuntimeConstants(Variables::generateRuntimeConstants(opCtx)); - return qr; + !findCommand->getLegacyRuntimeConstants()); + findCommand->setLegacyRuntimeConstants(Variables::generateRuntimeConstants(opCtx)); + return findCommand; } /** @@ -153,31 +151,31 @@ public: void explain(OperationContext* opCtx, ExplainOptions::Verbosity verbosity, rpc::ReplyBuilderInterface* result) override { - // Parse the command BSON to a QueryRequest. - const bool isExplain = true; - auto qr = parseCmdObjectToQueryRequest(opCtx, ns(), _request.body, isExplain); + // Parse the command BSON to a FindCommand. + auto findCommand = parseCmdObjectToFindCommand(opCtx, ns(), _request.body); try { const auto explainCmd = - ClusterExplain::wrapAsExplain(qr->asFindCommand(), verbosity); + ClusterExplain::wrapAsExplain(findCommand->toBSON(BSONObj()), verbosity); long long millisElapsed; std::vector<AsyncRequestsSender::Response> shardResponses; // We will time how long it takes to run the commands on the shards. Timer timer; - const auto routingInfo = uassertStatusOK( - Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo(opCtx, qr->nss())); - shardResponses = - scatterGatherVersionedTargetByRoutingTable(opCtx, - qr->nss().db(), - qr->nss(), - routingInfo, - explainCmd, - ReadPreferenceSetting::get(opCtx), - Shard::RetryPolicy::kIdempotent, - qr->getFilter(), - qr->getCollation()); + const auto routingInfo = + uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo( + opCtx, *findCommand->getNamespaceOrUUID().nss())); + shardResponses = scatterGatherVersionedTargetByRoutingTable( + opCtx, + findCommand->getNamespaceOrUUID().nss()->db(), + *findCommand->getNamespaceOrUUID().nss(), + routingInfo, + explainCmd, + ReadPreferenceSetting::get(opCtx), + Shard::RetryPolicy::kIdempotent, + findCommand->getFilter(), + findCommand->getCollation()); millisElapsed = timer.millis(); const char* mongosStageName = @@ -195,7 +193,8 @@ public: auto bodyBuilder = result->getBodyBuilder(); bodyBuilder.resetToEmpty(); - auto aggCmdOnView = uassertStatusOK(qr->asAggregationCommand()); + auto aggCmdOnView = + uassertStatusOK(query_request_helper::asAggregationCommand(*findCommand)); auto viewAggregationCommand = OpMsgRequest::fromDBAndBody(_dbName, aggCmdOnView).body; @@ -226,13 +225,13 @@ public: opCtx, mongo::LogicalOp::opQuery); }); - const bool isExplain = false; - auto qr = parseCmdObjectToQueryRequest(opCtx, ns(), _request.body, isExplain); + auto findCommand = parseCmdObjectToFindCommand(opCtx, ns(), _request.body); const boost::intrusive_ptr<ExpressionContext> expCtx; auto cq = uassertStatusOK( CanonicalQuery::canonicalize(opCtx, - std::move(qr), + std::move(findCommand), + false, /* isExplain */ expCtx, ExtensionsCallbackNoop(), MatchExpressionParser::kAllowAllSpecialFeatures)); @@ -261,7 +260,8 @@ public: } catch (const ExceptionFor<ErrorCodes::CommandOnShardedViewNotSupportedOnMongod>& ex) { result->reset(); - auto aggCmdOnView = uassertStatusOK(cq->getQueryRequest().asAggregationCommand()); + auto aggCmdOnView = uassertStatusOK( + query_request_helper::asAggregationCommand(cq->getFindCommand())); auto viewAggregationCommand = OpMsgRequest::fromDBAndBody(_dbName, aggCmdOnView).body; diff --git a/src/mongo/s/commands/strategy.cpp b/src/mongo/s/commands/strategy.cpp index bb97a416c7d..f4db2effe4b 100644 --- a/src/mongo/s/commands/strategy.cpp +++ b/src/mongo/s/commands/strategy.cpp @@ -58,7 +58,7 @@ #include "mongo/db/ops/write_ops.h" #include "mongo/db/query/find_common.h" #include "mongo/db/query/getmore_request.h" -#include "mongo/db/query/query_request.h" +#include "mongo/db/query/query_request_helper.h" #include "mongo/db/read_write_concern_defaults.h" #include "mongo/db/stats/api_version_metrics.h" #include "mongo/db/stats/counters.h" @@ -498,9 +498,9 @@ void ParseAndRunCommand::_parseCommand() { // maxTimeMS altogether on a getMore command. uassert(ErrorCodes::InvalidOptions, "no such command option $maxTimeMs; use maxTimeMS instead", - request.body[QueryRequest::queryOptionMaxTimeMS].eoo()); + request.body[query_request_helper::queryOptionMaxTimeMS].eoo()); const int maxTimeMS = - uassertStatusOK(parseMaxTimeMS(request.body[QueryRequest::cmdOptionMaxTimeMS])); + uassertStatusOK(parseMaxTimeMS(request.body[query_request_helper::cmdOptionMaxTimeMS])); if (maxTimeMS > 0 && command->getLogicalOp() != LogicalOp::opGetMore) { opCtx->setDeadlineAfterNowBy(Milliseconds{maxTimeMS}, ErrorCodes::MaxTimeMSExpired); } @@ -1096,29 +1096,29 @@ DbResponse Strategy::queryOp(OperationContext* opCtx, const NamespaceString& nss ExtensionsCallbackNoop(), MatchExpressionParser::kAllowAllSpecialFeatures)); - const QueryRequest& queryRequest = canonicalQuery->getQueryRequest(); + const FindCommand& findCommand = canonicalQuery->getFindCommand(); // Handle query option $maxTimeMS (not used with commands). - if (queryRequest.getMaxTimeMS() > 0) { + if (findCommand.getMaxTimeMS().value_or(0) > 0) { uassert(50749, "Illegal attempt to set operation deadline within DBDirectClient", !opCtx->getClient()->isInDirectClient()); - opCtx->setDeadlineAfterNowBy(Milliseconds{queryRequest.getMaxTimeMS()}, + opCtx->setDeadlineAfterNowBy(Milliseconds{findCommand.getMaxTimeMS().value_or(0)}, ErrorCodes::MaxTimeMSExpired); } opCtx->checkForInterrupt(); // May trigger maxTimeAlwaysTimeOut fail point. // If the $explain flag was set, we must run the operation on the shards as an explain command // rather than a find command. - if (queryRequest.isExplain()) { - const BSONObj findCommand = queryRequest.asFindCommand(); + if (canonicalQuery->getExplain()) { + const BSONObj findCommandObj = findCommand.toBSON(BSONObj()); // We default to allPlansExecution verbosity. const auto verbosity = ExplainOptions::Verbosity::kExecAllPlans; BSONObjBuilder explainBuilder; Strategy::explainFind(opCtx, + findCommandObj, findCommand, - queryRequest, verbosity, ReadPreferenceSetting::get(opCtx), &explainBuilder); @@ -1444,12 +1444,12 @@ void Strategy::writeOp(std::shared_ptr<RequestExecutionContext> rec) { } void Strategy::explainFind(OperationContext* opCtx, - const BSONObj& findCommand, - const QueryRequest& qr, + const BSONObj& findCommandObj, + const FindCommand& findCommand, ExplainOptions::Verbosity verbosity, const ReadPreferenceSetting& readPref, BSONObjBuilder* out) { - const auto explainCmd = ClusterExplain::wrapAsExplain(findCommand, verbosity); + const auto explainCmd = ClusterExplain::wrapAsExplain(findCommandObj, verbosity); long long millisElapsed; std::vector<AsyncRequestsSender::Response> shardResponses; @@ -1460,18 +1460,20 @@ void Strategy::explainFind(OperationContext* opCtx, // We will time how long it takes to run the commands on the shards. Timer timer; try { - const auto routingInfo = uassertStatusOK( - Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo(opCtx, qr.nss())); - shardResponses = - scatterGatherVersionedTargetByRoutingTable(opCtx, - qr.nss().db(), - qr.nss(), - routingInfo, - explainCmd, - readPref, - Shard::RetryPolicy::kIdempotent, - qr.getFilter(), - qr.getCollation()); + invariant(findCommand.getNamespaceOrUUID().nss()); + const auto routingInfo = + uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo( + opCtx, *findCommand.getNamespaceOrUUID().nss())); + shardResponses = scatterGatherVersionedTargetByRoutingTable( + opCtx, + findCommand.getNamespaceOrUUID().nss()->db(), + *findCommand.getNamespaceOrUUID().nss(), + routingInfo, + explainCmd, + readPref, + Shard::RetryPolicy::kIdempotent, + findCommand.getFilter(), + findCommand.getCollation()); millisElapsed = timer.millis(); break; } catch (ExceptionFor<ErrorCodes::ShardInvalidatedForTargeting>&) { @@ -1524,9 +1526,9 @@ void Strategy::explainFind(OperationContext* opCtx, } const char* mongosStageName = - ClusterExplain::getStageNameForReadOp(shardResponses.size(), findCommand); + ClusterExplain::getStageNameForReadOp(shardResponses.size(), findCommandObj); uassertStatusOK(ClusterExplain::buildExplainResult( - opCtx, shardResponses, mongosStageName, millisElapsed, findCommand, out)); + opCtx, shardResponses, mongosStageName, millisElapsed, findCommandObj, out)); } } // namespace mongo diff --git a/src/mongo/s/commands/strategy.h b/src/mongo/s/commands/strategy.h index 95f3f8c9449..4ea30690bf2 100644 --- a/src/mongo/s/commands/strategy.h +++ b/src/mongo/s/commands/strategy.h @@ -43,7 +43,7 @@ struct DbResponse; class Message; class NamespaceString; class OperationContext; -class QueryRequest; +class FindCommand; /** * Legacy interface for processing client read/write/cmd requests. @@ -93,8 +93,8 @@ public: * $explain modifier. */ static void explainFind(OperationContext* opCtx, - const BSONObj& findCommand, - const QueryRequest& qr, + const BSONObj& findCommandObj, + const FindCommand& findCommand, ExplainOptions::Verbosity verbosity, const ReadPreferenceSetting& readPref, BSONObjBuilder* out); |