diff options
author | Yoonsoo Kim <yoonsoo.kim@mongodb.com> | 2021-06-25 21:55:55 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-06-25 22:08:34 +0000 |
commit | 819fd3897f3eae62234eb4135960338b4564f2ed (patch) | |
tree | 62dd130bb0499bb42e91c753df0f7f20d24da86e | |
parent | 49b61ee7ad5d53c97ff8dc509655acfa08c062bd (diff) | |
download | mongo-819fd3897f3eae62234eb4135960338b4564f2ed.tar.gz |
SERVER-57897 Add readPrefMode option to benchRun find/findOne ops
-rw-r--r-- | jstests/noPassthrough/benchrun_read_pref_mode.js | 101 | ||||
-rw-r--r-- | src/mongo/shell/bench.cpp | 41 | ||||
-rw-r--r-- | src/mongo/shell/bench.h | 3 |
3 files changed, 141 insertions, 4 deletions
diff --git a/jstests/noPassthrough/benchrun_read_pref_mode.js b/jstests/noPassthrough/benchrun_read_pref_mode.js new file mode 100644 index 00000000000..d9c6027f4eb --- /dev/null +++ b/jstests/noPassthrough/benchrun_read_pref_mode.js @@ -0,0 +1,101 @@ +/** + * Verifies that readPrefMode param works for find/fineOne/query ops in benchRun(). + * + * @tags: [requires_replication] + */ + +(function() { +"use strict"; + +const rs = new ReplSetTest({nodes: 2}); +rs.startSet(); +rs.initiate(); + +const primary = rs.getPrimary(); +const secondary = rs.getSecondary(); +const collName = primary.getDB(jsTestName()).getCollection("coll").getFullName(); + +const verifyNoError = res => { + assert.eq(res.errCount, 0); + assert.gt(res.totalOps, 0); +}; + +const benchArgArray = [ + { + ops: [{op: "find", readCmd: true, query: {}, ns: collName, readPrefMode: "primary"}], + parallel: 1, + host: primary.host + }, + { + ops: [{ + op: "findOne", + readCmd: true, + query: {}, + ns: collName, + readPrefMode: "primaryPreferred" + }], + parallel: 1, + host: primary.host + }, + { + ops: [{op: "find", readCmd: true, query: {}, ns: collName, readPrefMode: "secondary"}], + parallel: 1, + host: secondary.host + }, + { + ops: [{ + op: "findOne", + readCmd: true, + query: {}, + ns: collName, + readPrefMode: "secondaryPreferred" + }], + parallel: 1, + host: secondary.host + }, + { + ops: [{op: "query", readCmd: true, query: {}, ns: collName, readPrefMode: "nearest"}], + parallel: 1, + host: secondary.host + }, +]; + +benchArgArray.forEach(benchArg => verifyNoError(benchRun(benchArg))); + +const invalidArgAndError = [ + { + benchArg: { + ops: [{op: "find", readCmd: true, query: {}, ns: collName, readPrefMode: 1}], + parallel: 1, + host: primary.host + }, + error: ErrorCodes.BadValue + }, + { + benchArg: { + ops: + [{op: "find", readCmd: true, query: {}, ns: collName, readPrefMode: "invalidPref"}], + parallel: 1, + host: primary.host + }, + error: ErrorCodes.BadValue + }, + { + benchArg: { + ops: [ + {op: "insert", writeCmd: true, doc: {a: 1}, ns: collName, readPrefMode: "primary"} + ], + parallel: 1, + host: primary.host + }, + error: ErrorCodes.InvalidOptions + }, +]; + +invalidArgAndError.forEach(argAndError => { + const res = assert.throws(() => benchRun(argAndError.benchArg)); + assert.commandFailedWithCode(res, argAndError.error); +}); + +rs.stopSet(); +})(); diff --git a/src/mongo/shell/bench.cpp b/src/mongo/shell/bench.cpp index d4c6d771502..c0d9a876b4e 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/base/shim.h" #include "mongo/client/dbclient_cursor.h" @@ -232,16 +233,22 @@ 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); + if (!readPrefObj.isEmpty()) { + 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, @@ -310,7 +317,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); @@ -638,6 +645,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); } @@ -1029,8 +1056,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); @@ -1146,6 +1178,7 @@ void BenchRunOp::executeOnce(DBClientBase* conn, txnNumberForOp, std::move(qr), Milliseconds(delayBeforeGetMore), + readPrefObj, nullptr); } else { if (!this->sort.isEmpty()) { diff --git a/src/mongo/shell/bench.h b/src/mongo/shell/bench.h index 9c554845883..b0b068fe98f 100644 --- a/src/mongo/shell/bench.h +++ b/src/mongo/shell/bench.h @@ -139,6 +139,9 @@ struct BenchRunOp { // resources that a snapshot transaction would hold for a time. int maxRandomMillisecondDelayBeforeGetMore{0}; + // Format: {mode: modeStr}. Only mode field is allowed. + BSONObj readPrefObj; + // This is an owned copy of the raw operation. All unowned members point into this. BSONObj myBsonOp; }; |