diff options
Diffstat (limited to 'src/mongo/shell/bench.cpp')
-rw-r--r-- | src/mongo/shell/bench.cpp | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/src/mongo/shell/bench.cpp b/src/mongo/shell/bench.cpp index 1f5fd3e4775..1de6b59fff3 100644 --- a/src/mongo/shell/bench.cpp +++ b/src/mongo/shell/bench.cpp @@ -34,6 +34,7 @@ #include "mongo/shell/bench.h" #include <pcrecpp.h> +#include <string> #include "mongo/client/dbclient_cursor.h" #include "mongo/db/namespace_string.h" @@ -231,16 +232,20 @@ int runQueryWithReadCommands(DBClientBase* conn, boost::optional<TxnNumber> txnNumber, std::unique_ptr<QueryRequest> qr, Milliseconds delayBeforeGetMore, + BSONObj readPrefObj, BSONObj* objOut) { const auto dbName = qr->nss().db().toString(); BSONObj findCommandResult; + BSONObjBuilder findCommandBuilder; + qr->asFindCommand(&findCommandBuilder); + findCommandBuilder.append("$readPreference", readPrefObj); uassert(ErrorCodes::CommandFailed, str::stream() << "find command failed; reply was: " << findCommandResult, runCommandWithSession( conn, dbName, - qr->asFindCommand(), + findCommandBuilder.obj(), // read command with txnNumber implies performing reads in a // multi-statement transaction txnNumber ? kStartTransactionOption | kMultiStatementTransactionOption : kNoOptions, @@ -309,7 +314,7 @@ Timestamp getLatestClusterTime(DBClientBase* conn) { BSONObj oplogResult; int count = runQueryWithReadCommands( - conn, boost::none, boost::none, std::move(qr), Milliseconds(0), &oplogResult); + conn, boost::none, boost::none, std::move(qr), Milliseconds(0), BSONObj(), &oplogResult); uassert(ErrorCodes::OperationFailed, str::stream() << "Find cmd on the oplog collection failed; reply was: " << oplogResult, count == 1); @@ -637,6 +642,26 @@ BenchRunOp opFromBson(const BSONObj& op) { << opType, (opType == "find") || (opType == "query")); myOp.maxRandomMillisecondDelayBeforeGetMore = arg.numberInt(); + } else if (name == "readPrefMode") { + uassert( + ErrorCodes::InvalidOptions, + str::stream() << "Field 'readPrefMode' is only valid for find op types. Type is " + << opType, + (opType == "find") || (opType == "query") || (opType == "findOne")); + uassert(ErrorCodes::BadValue, + str::stream() << "Field 'readPrefMode' should be a string, instead it's type: " + << typeName(arg.type()), + arg.type() == BSONType::String); + + ReadPreference mode; + try { + mode = ReadPreference_parse(IDLParserErrorContext("mode"), arg.str()); + } catch (DBException& e) { + e.addContext("benchRun(): Could not parse readPrefMode argument"); + throw; + } + + myOp.readPrefObj = ReadPreferenceSetting(mode).toInnerBSON(); } else { uassert(34394, str::stream() << "Benchrun op has unsupported field: " << name, false); } @@ -1020,8 +1045,13 @@ void BenchRunOp::executeOnce(DBClientBase* conn, txnNumberForOp = state->txnNumber; state->inProgressMultiStatementTxn = true; } - runQueryWithReadCommands( - conn, lsid, txnNumberForOp, std::move(qr), Milliseconds(0), &result); + runQueryWithReadCommands(conn, + lsid, + txnNumberForOp, + std::move(qr), + Milliseconds(0), + readPrefObj, + &result); } else { if (!this->sort.isEmpty()) { fixedQuery = makeQueryLegacyCompatible(std::move(fixedQuery), this->sort); @@ -1137,6 +1167,7 @@ void BenchRunOp::executeOnce(DBClientBase* conn, txnNumberForOp, std::move(qr), Milliseconds(delayBeforeGetMore), + readPrefObj, nullptr); } else { if (!this->sort.isEmpty()) { |