diff options
author | Yoonsoo Kim <yoonsoo.kim@mongodb.com> | 2021-06-25 04:15:26 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-06-25 04:36:33 +0000 |
commit | 8a8d6f27bdd2cfe3d9861126d15153ea249613e6 (patch) | |
tree | bd1678783050843dc5d153349599786280b71f96 | |
parent | d8401e342a8d273c1f8b466a93a9d002a8b363cb (diff) | |
download | mongo-8a8d6f27bdd2cfe3d9861126d15153ea249613e6.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 | 39 | ||||
-rw-r--r-- | src/mongo/shell/bench.h | 3 |
3 files changed, 139 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 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()) { diff --git a/src/mongo/shell/bench.h b/src/mongo/shell/bench.h index 830e3c6924a..9789f6814a9 100644 --- a/src/mongo/shell/bench.h +++ b/src/mongo/shell/bench.h @@ -140,6 +140,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; }; |