diff options
author | Ian Boros <ian.boros@mongodb.com> | 2021-03-01 18:38:40 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-03-10 22:14:54 +0000 |
commit | d5487c0c029def84aae860dd1db6a9f2316ea5e0 (patch) | |
tree | 833ce8693e8cdaaeda577725364110981c1b9e4f | |
parent | 16e27da22758f0709cc6185899b181c77b9f28a4 (diff) | |
download | mongo-d5487c0c029def84aae860dd1db6a9f2316ea5e0.tar.gz |
SERVER-5722 Support 'sort' in benchrun
-rw-r--r-- | src/mongo/shell/bench.cpp | 31 | ||||
-rw-r--r-- | src/mongo/shell/bench.h | 1 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/mongo/shell/bench.cpp b/src/mongo/shell/bench.cpp index c8f5a6acbf0..511dcb4293a 100644 --- a/src/mongo/shell/bench.cpp +++ b/src/mongo/shell/bench.cpp @@ -131,6 +131,16 @@ BSONObj fixQuery(const BSONObj& obj, BsonTemplateEvaluator& btl) { return b.obj(); } +/** + * Adds a '$orderby' to the query document. Useful when running on the legacy reads path. + */ +BSONObj makeQueryLegacyCompatible(const BSONObj& query, const BSONObj& sortSpec) { + BSONObjBuilder bob; + bob.append("$query", query); + bob.append("$orderby", sortSpec); + return bob.obj(); +} + bool runCommandWithSession(DBClientBase* conn, const std::string& dbname, const BSONObj& cmdObj, @@ -499,6 +509,16 @@ BenchRunOp opFromBson(const BSONObj& op) { << opType, (opType == "find") || (opType == "query")); myOp.skip = arg.numberInt(); + } else if (name == "sort") { + uassert(ErrorCodes::BadValue, // TODO + str::stream() + << "Field 'sort' is only valid for query, fineOne and find. Op type is " + << opType, + (opType == "findOne") || (opType == "query") || (opType == "find")); + uassert(ErrorCodes::BadValue, + "Expected sort to be an object", + arg.type() == BSONType::Object); + myOp.sort = arg.Obj(); } else if (name == "showError") { myOp.showError = arg.trueValue(); } else if (name == "showResult") { @@ -903,6 +923,7 @@ void BenchRunOp::executeOnce(DBClientBase* conn, qr->setProj(this->projection); qr->setLimit(1LL); qr->setWantMore(false); + qr->setSort(this->sort); if (config.useSnapshotReads) { qr->setReadConcern(readConcernSnapshot); } @@ -917,6 +938,9 @@ void BenchRunOp::executeOnce(DBClientBase* conn, } runQueryWithReadCommands(conn, lsid, txnNumberForOp, std::move(qr), &result); } else { + if (!this->sort.isEmpty()) { + fixedQuery = makeQueryLegacyCompatible(std::move(fixedQuery), this->sort); + } BenchRunEventTrace _bret(&state->stats->findOneCounter); result = conn->findOne( this->ns, fixedQuery, nullptr, DBClientCursor::QueryOptionLocal_forceOpQuery); @@ -992,6 +1016,10 @@ void BenchRunOp::executeOnce(DBClientBase* conn, if (this->batchSize) { qr->setBatchSize(this->batchSize); } + if (!this->sort.isEmpty()) { + qr->setSort(this->sort); + } + BSONObjBuilder readConcernBuilder; if (config.useSnapshotReads) { qr->setReadConcern(readConcernSnapshot); } @@ -1007,6 +1035,9 @@ void BenchRunOp::executeOnce(DBClientBase* conn, count = runQueryWithReadCommands(conn, lsid, txnNumberForOp, std::move(qr), nullptr); } else { + if (!this->sort.isEmpty()) { + fixedQuery = makeQueryLegacyCompatible(std::move(fixedQuery), this->sort); + } // Use special query function for exhaust query option. if (this->options & QueryOption_Exhaust) { BenchRunEventTrace _bret(&state->stats->queryCounter); diff --git a/src/mongo/shell/bench.h b/src/mongo/shell/bench.h index 89df91e97c2..cee301cfaa0 100644 --- a/src/mongo/shell/bench.h +++ b/src/mongo/shell/bench.h @@ -110,6 +110,7 @@ struct BenchRunOp { BSONObj query; bool safe = false; int skip = 0; + BSONObj sort; bool showError = false; bool showResult = false; std::string target; |