diff options
32 files changed, 298 insertions, 1019 deletions
diff --git a/src/mongo/client/dbclient_base.cpp b/src/mongo/client/dbclient_base.cpp index 5c2238ebec9..2c2bcf36412 100644 --- a/src/mongo/client/dbclient_base.cpp +++ b/src/mongo/client/dbclient_base.cpp @@ -584,31 +584,6 @@ bool DBClientBase::exists(const string& ns) { const uint64_t DBClientBase::INVALID_SOCK_CREATION_TIME = std::numeric_limits<uint64_t>::max(); -unique_ptr<DBClientCursor> DBClientBase::query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) { - unique_ptr<DBClientCursor> c(new DBClientCursor(this, - nsOrUuid, - filter, - querySettings, - limit, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj)); - if (c->init()) - return c; - return nullptr; -} - std::unique_ptr<DBClientCursor> DBClientBase::find(FindCommandRequest findRequest, const ReadPreferenceSetting& readPref, ExhaustMode exhaustMode) { @@ -651,46 +626,12 @@ BSONObj DBClientBase::findOne(const NamespaceStringOrUUID& nssOrUuid, BSONObj fi unique_ptr<DBClientCursor> DBClientBase::getMore(const string& ns, long long cursorId) { unique_ptr<DBClientCursor> c( - new DBClientCursor(this, NamespaceString(ns), cursorId, 0 /* limit */, 0 /* options */)); + new DBClientCursor(this, NamespaceString(ns), cursorId, false /*isExhaust*/)); if (c->init()) return c; return nullptr; } -unsigned long long DBClientBase::query_DEPRECATED( - std::function<void(DBClientCursorBatchIterator&)> f, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) { - // mask options - queryOptions &= (int)(QueryOption_NoCursorTimeout | QueryOption_SecondaryOk); - - unique_ptr<DBClientCursor> c(this->query_DEPRECATED(nsOrUuid, - filter, - querySettings, - 0, - 0, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj)); - // query_DEPRECATED() throws on network error so OK to uassert with numeric code here. - uassert(16090, "socket error for mapping query", c.get()); - - unsigned long long n = 0; - - while (c->more()) { - DBClientCursorBatchIterator i(*c); - f(i); - n += i.n(); - } - return n; -} - namespace { OpMsgRequest createInsertRequest(const string& ns, const vector<BSONObj>& v, diff --git a/src/mongo/client/dbclient_base.h b/src/mongo/client/dbclient_base.h index 0a1c7bdd77d..3c933f6d3d3 100644 --- a/src/mongo/client/dbclient_base.h +++ b/src/mongo/client/dbclient_base.h @@ -35,7 +35,6 @@ #include "mongo/base/string_data.h" #include "mongo/client/authenticate.h" #include "mongo/client/client_api_version_parameters_gen.h" -#include "mongo/client/client_deprecated.h" #include "mongo/client/connection_string.h" #include "mongo/client/dbclient_cursor.h" #include "mongo/client/index_spec.h" @@ -578,29 +577,6 @@ public: BSONObj findOne(const NamespaceStringOrUUID& nssOrUuid, BSONObj filter); /** - * Legacy find API. Do not add new callers! Use the 'find*()' methods above instead. - */ - virtual std::unique_ptr<DBClientCursor> query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings = client_deprecated::Query(), - int limit = 0, - int nToSkip = 0, - const BSONObj* fieldsToReturn = nullptr, - int queryOptions = 0, - int batchSize = 0, - boost::optional<BSONObj> readConcernObj = boost::none); - virtual unsigned long long query_DEPRECATED( - std::function<void(DBClientCursorBatchIterator&)> f, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings = client_deprecated::Query(), - const BSONObj* fieldsToReturn = nullptr, - int queryOptions = QueryOption_Exhaust, - int batchSize = 0, - boost::optional<BSONObj> readConcernObj = boost::none); - - /** * Don't use this - called automatically by DBClientCursor for you. * 'cursorId': Id of cursor to retrieve. * Returns an handle to a previously allocated cursor. diff --git a/src/mongo/client/dbclient_connection.cpp b/src/mongo/client/dbclient_connection.cpp index c3651edb01c..1b87829c1cd 100644 --- a/src/mongo/client/dbclient_connection.cpp +++ b/src/mongo/client/dbclient_connection.cpp @@ -625,67 +625,6 @@ uint64_t DBClientConnection::getSockCreationMicroSec() const { } } -unsigned long long DBClientConnection::query_DEPRECATED( - std::function<void(DBClientCursorBatchIterator&)> f, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) { - if (!(queryOptions & QueryOption_Exhaust)) { - return DBClientBase::query_DEPRECATED(f, - nsOrUuid, - filter, - querySettings, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj); - } - - // mask options - queryOptions &= - (int)(QueryOption_NoCursorTimeout | QueryOption_SecondaryOk | QueryOption_Exhaust); - - unique_ptr<DBClientCursor> c(this->query_DEPRECATED(nsOrUuid, - filter, - querySettings, - 0, - 0, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj)); - // Note that this->query will throw for network errors, so it is OK to return a numeric - // error code here. - uassert(13386, "socket error for mapping query", c.get()); - - unsigned long long n = 0; - - try { - while (1) { - while (c->moreInCurrentBatch()) { - DBClientCursorBatchIterator i(*c); - f(i); - n += i.n(); - } - - if (!c->more()) - break; - } - } catch (std::exception&) { - /* connection CANNOT be used anymore as more data may be on the way from the server. - we have to reconnect. - */ - _markFailed(kEndSession); - throw; - } - - return n; -} - DBClientConnection::DBClientConnection(bool _autoReconnect, double so_timeout, MongoURI uri, diff --git a/src/mongo/client/dbclient_connection.h b/src/mongo/client/dbclient_connection.h index 45ffcf97b78..61096ba59b3 100644 --- a/src/mongo/client/dbclient_connection.h +++ b/src/mongo/client/dbclient_connection.h @@ -62,7 +62,6 @@ struct RemoteCommandResponse; } class DBClientCursor; -class DBClientCursorBatchIterator; /** * A basic connection to the database. @@ -142,38 +141,6 @@ public: */ void logout(const std::string& dbname, BSONObj& info) override; - std::unique_ptr<DBClientCursor> query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings = client_deprecated::Query(), - int limit = 0, - int nToSkip = 0, - const BSONObj* fieldsToReturn = nullptr, - int queryOptions = 0, - int batchSize = 0, - boost::optional<BSONObj> readConcernObj = boost::none) override { - checkConnection(); - return DBClientBase::query_DEPRECATED(nsOrUuid, - filter, - querySettings, - limit, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj); - } - - unsigned long long query_DEPRECATED( - std::function<void(DBClientCursorBatchIterator&)>, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize = 0, - boost::optional<BSONObj> readConcernObj = boost::none) override; - using DBClientBase::runCommandWithTarget; std::pair<rpc::UniqueReply, DBClientBase*> runCommandWithTarget(OpMsgRequest request) override; std::pair<rpc::UniqueReply, std::shared_ptr<DBClientBase>> runCommandWithTarget( diff --git a/src/mongo/client/dbclient_cursor.cpp b/src/mongo/client/dbclient_cursor.cpp index 33fb9e86508..dc6a32acd07 100644 --- a/src/mongo/client/dbclient_cursor.cpp +++ b/src/mongo/client/dbclient_cursor.cpp @@ -72,137 +72,32 @@ BSONObj addMetadata(DBClientBase* client, BSONObj command) { } } -Message assembleCommandRequest(DBClientBase* cli, +Message assembleCommandRequest(DBClientBase* client, StringData database, - int legacyQueryOptions, - BSONObj legacyQuery) { - auto request = rpc::upconvertRequest(database, std::move(legacyQuery), legacyQueryOptions); - request.body = addMetadata(cli, std::move(request.body)); - return request.serialize(); -} - -Message assembleFromFindCommandRequest(DBClientBase* client, - StringData database, - const FindCommandRequest& request, - const ReadPreferenceSetting& readPref) { - BSONObj findCmd = request.toBSON(BSONObj()); - + BSONObj commandObj, + const ReadPreferenceSetting& readPref) { // Add the $readPreference field to the request. { - BSONObjBuilder builder{findCmd}; + BSONObjBuilder builder{commandObj}; readPref.toContainingBSON(&builder); - findCmd = builder.obj(); + commandObj = builder.obj(); } - findCmd = addMetadata(client, std::move(findCmd)); - auto opMsgRequest = OpMsgRequest::fromDBAndBody(database, findCmd); + commandObj = addMetadata(client, std::move(commandObj)); + auto opMsgRequest = OpMsgRequest::fromDBAndBody(database, commandObj); return opMsgRequest.serialize(); } - -std::unique_ptr<FindCommandRequest> fromLegacyQuery(NamespaceStringOrUUID nssOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - const BSONObj& proj, - int ntoskip, - int queryOptions) { - auto findCommand = std::make_unique<FindCommandRequest>(std::move(nssOrUuid)); - - client_deprecated::initFindFromLegacyOptions( - querySettings.getFullSettingsDeprecated(), queryOptions, findCommand.get()); - - findCommand->setFilter(filter.getOwned()); - - if (!proj.isEmpty()) { - findCommand->setProjection(proj.getOwned()); - } - if (ntoskip) { - findCommand->setSkip(ntoskip); - } - - uassertStatusOK(query_request_helper::validateFindCommandRequest(*findCommand)); - - return findCommand; -} - -int queryOptionsFromFindCommand(const FindCommandRequest& findCmd, - const ReadPreferenceSetting& readPref, - bool isExhaust) { - int queryOptions = 0; - if (readPref.canRunOnSecondary()) { - queryOptions = queryOptions | QueryOption_SecondaryOk; - } - if (findCmd.getTailable()) { - queryOptions = queryOptions | QueryOption_CursorTailable; - } - if (findCmd.getNoCursorTimeout()) { - queryOptions = queryOptions | QueryOption_NoCursorTimeout; - } - if (findCmd.getAwaitData()) { - queryOptions = queryOptions | QueryOption_AwaitData; - } - if (findCmd.getAllowPartialResults()) { - queryOptions = queryOptions | QueryOption_PartialResults; - } - if (isExhaust) { - queryOptions = queryOptions | QueryOption_Exhaust; - } - return queryOptions; -} - } // namespace -Message DBClientCursor::initFromLegacyRequest() { - auto findCommand = fromLegacyQuery(_nsOrUuid, - _filter, - _querySettings, - _fieldsToReturn ? *_fieldsToReturn : BSONObj(), - _nToSkip, - _opts); - - if (_limit) { - findCommand->setLimit(_limit); - } - if (_batchSize) { - findCommand->setBatchSize(_batchSize); - } - - const BSONObj querySettings = _querySettings.getFullSettingsDeprecated(); - // We prioritize the readConcern parsed from the query object over '_readConcernObj'. - if (!findCommand->getReadConcern()) { - if (_readConcernObj) { - findCommand->setReadConcern(_readConcernObj); - } else { - // If no readConcern was specified, initialize it to an empty readConcern object, ie. - // equivalent to `readConcern: {}`. This ensures that mongos passes this empty - // readConcern to shards. - findCommand->setReadConcern(BSONObj()); - } - } - - BSONObj cmd = findCommand->toBSON(BSONObj()); - if (auto readPref = querySettings["$readPreference"]) { - // FindCommandRequest doesn't handle $readPreference. - cmd = BSONObjBuilder(std::move(cmd)).append(readPref).obj(); - } - - return assembleCommandRequest(_client, _ns.db(), _opts, std::move(cmd)); -} - Message DBClientCursor::assembleInit() { if (_cursorId) { return assembleGetMore(); } // We haven't gotten a cursorId yet so we need to issue the initial find command. - if (_findRequest) { - // The caller described their find command using the modern 'FindCommandRequest' API. - return assembleFromFindCommandRequest(_client, _ns.db(), *_findRequest, _readPref); - } else { - // The caller used a legacy API to describe the find operation, which may include $-prefixed - // directives in the format previously expected for an OP_QUERY. We need to upconvert this - // OP_QUERY-inspired format to a find command. - return initFromLegacyRequest(); - } + invariant(_findRequest); + BSONObj findCmd = _findRequest->toBSON(BSONObj()); + return assembleCommandRequest(_client, _ns.db(), std::move(findCmd), _readPref); } Message DBClientCursor::assembleGetMore() { @@ -217,10 +112,10 @@ Message DBClientCursor::assembleGetMore() { getMoreRequest.setTerm(static_cast<std::int64_t>(*_term)); } getMoreRequest.setLastKnownCommittedOpTime(_lastKnownCommittedOpTime); - auto msg = assembleCommandRequest(_client, _ns.db(), _opts, getMoreRequest.toBSON({})); + auto msg = assembleCommandRequest(_client, _ns.db(), getMoreRequest.toBSON({}), _readPref); // Set the exhaust flag if needed. - if (_opts & QueryOption_Exhaust && msg.operation() == dbMsg) { + if (_isExhaust) { OpMsg::setFlag(&msg, OpMsg::kExhaustSupported); } return msg; @@ -251,8 +146,7 @@ bool DBClientCursor::init() { void DBClientCursor::requestMore() { // For exhaust queries, once the stream has been initiated we get data blasted to us // from the remote server, without a need to send any more 'getMore' requests. - const auto isExhaust = _opts & QueryOption_Exhaust; - if (isExhaust && _connectionHasPendingReplies) { + if (_isExhaust && _connectionHasPendingReplies) { return exhaustReceiveMore(); } @@ -277,7 +171,7 @@ void DBClientCursor::requestMore() { } /** - * With QueryOption_Exhaust, the server just blasts data at us. The end of a stream is marked with a + * For exhaust cursors, the server just blasts data at us. The end of a stream is marked with a * cursor id of 0. */ void DBClientCursor::exhaustReceiveMore() { @@ -295,9 +189,9 @@ BSONObj DBClientCursor::commandDataReceived(const Message& reply) { invariant(op == opReply || op == dbMsg); // Check if the reply indicates that it is part of an exhaust stream. - const auto isExhaust = OpMsg::isFlagSet(reply, OpMsg::kMoreToCome); - _connectionHasPendingReplies = isExhaust; - if (isExhaust) { + const auto isExhaustReply = OpMsg::isFlagSet(reply, OpMsg::kMoreToCome); + _connectionHasPendingReplies = isExhaustReply; + if (isExhaustReply) { _lastRequestId = reply.header().getId(); } @@ -431,83 +325,20 @@ void DBClientCursor::attach(AScopedConnection* conn) { DBClientCursor::DBClientCursor(DBClientBase* client, const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) - : DBClientCursor(client, - nsOrUuid, - filter, - querySettings, - 0, // cursorId - limit, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize, - {}, - readConcernObj, - boost::none) {} - -DBClientCursor::DBClientCursor(DBClientBase* client, - const NamespaceStringOrUUID& nsOrUuid, long long cursorId, - int limit, - int queryOptions, + bool isExhaust, std::vector<BSONObj> initialBatch, boost::optional<Timestamp> operationTime, boost::optional<BSONObj> postBatchResumeToken) - : DBClientCursor(client, - nsOrUuid, - BSONObj(), // filter - client_deprecated::Query(), - cursorId, - limit, - 0, // nToSkip - nullptr, // fieldsToReturn - queryOptions, - 0, - std::move(initialBatch), // batchSize - boost::none, - operationTime, - postBatchResumeToken) {} - -DBClientCursor::DBClientCursor(DBClientBase* client, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - long long cursorId, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - std::vector<BSONObj> initialBatch, - boost::optional<BSONObj> readConcernObj, - boost::optional<Timestamp> operationTime, - boost::optional<BSONObj> postBatchResumeToken) : _batch{std::move(initialBatch)}, _client(client), _originalHost(_client->getServerAddress()), _nsOrUuid(nsOrUuid), _ns(nsOrUuid.nss() ? *nsOrUuid.nss() : NamespaceString(nsOrUuid.dbname())), _cursorId(cursorId), - _batchSize(batchSize == 1 ? 2 : batchSize), - _limit(limit), - _filter(filter), - _querySettings(querySettings), - _nToSkip(nToSkip), - _fieldsToReturn(fieldsToReturn), - _readConcernObj(readConcernObj), - _opts(queryOptions), + _isExhaust(isExhaust), _operationTime(operationTime), - _postBatchResumeToken(postBatchResumeToken) { - tassert(5746103, "DBClientCursor limit must be non-negative", _limit >= 0); -} + _postBatchResumeToken(postBatchResumeToken) {} DBClientCursor::DBClientCursor(DBClientBase* client, FindCommandRequest findRequest, @@ -518,10 +349,9 @@ DBClientCursor::DBClientCursor(DBClientBase* client, _nsOrUuid(findRequest.getNamespaceOrUUID()), _ns(_nsOrUuid.nss() ? *_nsOrUuid.nss() : NamespaceString(_nsOrUuid.dbname())), _batchSize(findRequest.getBatchSize().value_or(0)), - _limit(findRequest.getLimit().value_or(0)), _findRequest(std::move(findRequest)), _readPref(readPref), - _opts(queryOptionsFromFindCommand(*_findRequest, _readPref, isExhaust)) { + _isExhaust(isExhaust) { // Internal clients should always pass an explicit readConcern. If the caller did not already // pass a readConcern than we must explicitly initialize an empty readConcern so that it ends up // in the serialized version of the find command which will be sent across the wire. @@ -565,8 +395,7 @@ StatusWith<std::unique_ptr<DBClientCursor>> DBClientCursor::fromAggregationReque return {std::make_unique<DBClientCursor>(client, aggRequest.getNamespace(), cursorId, - 0, - useExhaust ? QueryOption_Exhaust : 0, + useExhaust, firstBatch, operationTime, postBatchResumeToken)}; @@ -594,5 +423,4 @@ void DBClientCursor::kill() { _cursorId = 0; } - } // namespace mongo diff --git a/src/mongo/client/dbclient_cursor.h b/src/mongo/client/dbclient_cursor.h index 941eda47318..f13f861d96c 100644 --- a/src/mongo/client/dbclient_cursor.h +++ b/src/mongo/client/dbclient_cursor.h @@ -31,10 +31,8 @@ #include <stack> -#include "mongo/client/client_deprecated.h" +#include "mongo/client/read_preference.h" #include "mongo/db/dbmessage.h" -#include "mongo/db/jsobj.h" -#include "mongo/db/json.h" #include "mongo/db/namespace_string.h" #include "mongo/db/query/find_command_gen.h" #include "mongo/rpc/message.h" @@ -61,31 +59,26 @@ public: bool secondaryOk, bool useExhaust); + /** + * Constructs a 'DBClientCursor' that will be opened by issuing the find command described by + * 'findRequest'. + */ DBClientCursor(DBClientBase* client, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int bs, - boost::optional<BSONObj> readConcernObj = boost::none); + FindCommandRequest findRequest, + const ReadPreferenceSetting& readPref, + bool isExhaust); + /** + * Constructs a 'DBClientCursor' from a pre-existing cursor id. + */ DBClientCursor(DBClientBase* client, const NamespaceStringOrUUID& nsOrUuid, long long cursorId, - int limit, - int options, + bool isExhaust, std::vector<BSONObj> initialBatch = {}, boost::optional<Timestamp> operationTime = boost::none, boost::optional<BSONObj> postBatchResumeToken = boost::none); - DBClientCursor(DBClientBase* client, - FindCommandRequest findRequest, - const ReadPreferenceSetting& readPref, - bool isExhaust); - virtual ~DBClientCursor(); /** @@ -170,11 +163,11 @@ public: } bool tailable() const { - return (_opts & QueryOption_CursorTailable) != 0; + return _findRequest && _findRequest->getTailable(); } bool tailableAwaitData() const { - return tailable() && (_opts & QueryOption_AwaitData); + return tailable() && _findRequest->getAwaitData(); } /** @@ -277,21 +270,6 @@ protected: Batch _batch; private: - DBClientCursor(DBClientBase* client, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - long long cursorId, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int bs, - std::vector<BSONObj> initialBatch, - boost::optional<BSONObj> readConcernObj, - boost::optional<Timestamp> operationTime, - boost::optional<BSONObj> postBatchResumeToken = boost::none); - void dataReceived(const Message& reply) { bool retry; std::string lazyHost; @@ -312,13 +290,6 @@ private: Message assembleInit(); Message assembleGetMore(); - /** - * Constructs the initial find commmand request based on a legacy OP_QUERY-style description of - * the find operation. Only used if the caller constructed the 'DBClientCursor' with the legacy - * API. - */ - Message initFromLegacyRequest(); - DBClientBase* _client; std::string _originalHost; NamespaceStringOrUUID _nsOrUuid; @@ -336,32 +307,16 @@ private: bool _connectionHasPendingReplies = false; int _lastRequestId = 0; - int _batchSize; - int _limit = 0; + int _batchSize = 0; - // If the caller describes the find command being executed by this cursor as a - // 'FindCommandRequest', then that request object and the associated read preference are set - // here. Otherwise, if the caller uses the legacy OP_QUERY-inspired API, these members are - // default-initialized but never used. + // A description of the find command provided by the caller which is used to open the cursor. + // + // Has a value of boost::none if the caller constructed this cursor using a pre-existing cursor + // id. boost::optional<FindCommandRequest> _findRequest; - ReadPreferenceSetting _readPref; - // These data members are only used if the cursor was constructed using the legacy - // OP_QUERY-inspired API. If the cursor was constructed using the 'FindCommandRequest'-based - // API, these are initialized to their default values but never used. - BSONObj _filter; - client_deprecated::Query _querySettings; - int _nToSkip = 0; - const BSONObj* _fieldsToReturn = nullptr; - boost::optional<BSONObj> _readConcernObj; - - // This has the same meaning as the flags bit vector from the no-longer-supported OP_QUERY wire - // protocol message. However, it is initialized even if the caller constructed the cursor using - // the 'FindCommandRequest`-based API. - // - // We should eventually stop using the OP_QUERY flags bit vector in server code, since OP_QUERY - // is no longer supported. - int _opts; + ReadPreferenceSetting _readPref; + bool _isExhaust; Milliseconds _awaitDataTimeout = Milliseconds{0}; boost::optional<long long> _term; @@ -370,38 +325,4 @@ private: boost::optional<BSONObj> _postBatchResumeToken; }; -/** iterate over objects in current batch only - will not cause a network call - */ -class DBClientCursorBatchIterator { -public: - DBClientCursorBatchIterator(DBClientCursor& c) : _c(c), _n() {} - bool moreInCurrentBatch() { - return _c.moreInCurrentBatch(); - } - BSONObj nextSafe() { - massert(13383, "BatchIterator empty", moreInCurrentBatch()); - ++_n; - return _c.nextSafe(); - } - int n() const { - return _n; - } - // getNamespaceString() will return the NamespaceString returned by the 'find' command. - const NamespaceString& getNamespaceString() { - return _c.getNamespaceString(); - } - - long long getCursorId() const { - return _c.getCursorId(); - } - - boost::optional<BSONObj> getPostBatchResumeToken() const { - return _c.getPostBatchResumeToken(); - } - -private: - DBClientCursor& _c; - int _n; -}; - } // namespace mongo diff --git a/src/mongo/client/dbclient_mockcursor.cpp b/src/mongo/client/dbclient_mockcursor.cpp index 7082f55517e..0e33d4360d1 100644 --- a/src/mongo/client/dbclient_mockcursor.cpp +++ b/src/mongo/client/dbclient_mockcursor.cpp @@ -42,7 +42,7 @@ DBClientMockCursor::DBClientMockCursor(mongo::DBClientBase* client, const BSONArray& mockCollection, const bool provideResumeToken, unsigned long batchSize) - : mongo::DBClientCursor(client, NamespaceString(), 0, 0, 0), + : mongo::DBClientCursor(client, NamespaceString(), 0 /*cursorId*/, false /*isExhaust*/), _collectionArray(mockCollection), _iter(_collectionArray), _provideResumeToken(provideResumeToken), diff --git a/src/mongo/client/dbclient_mockcursor.h b/src/mongo/client/dbclient_mockcursor.h index 1138ee41286..7430a1aa3cb 100644 --- a/src/mongo/client/dbclient_mockcursor.h +++ b/src/mongo/client/dbclient_mockcursor.h @@ -35,9 +35,6 @@ namespace mongo { -// DBClientMockCursor supports only a small subset of DBClientCursor operations. -// It supports only iteration, including use of DBClientCursorBatchIterator. If a batchsize -// is given, iteration is broken up into multiple batches at batchSize boundaries. class DBClientMockCursor : public DBClientCursor { public: DBClientMockCursor(mongo::DBClientBase* client, diff --git a/src/mongo/client/dbclient_rs.cpp b/src/mongo/client/dbclient_rs.cpp index bf4259dd6ed..582ccf7b16d 100644 --- a/src/mongo/client/dbclient_rs.cpp +++ b/src/mongo/client/dbclient_rs.cpp @@ -27,15 +27,13 @@ * it in the license file. */ - -#include "mongo/platform/basic.h" - #include "mongo/client/dbclient_rs.h" #include <memory> #include <utility> #include "mongo/bson/util/builder.h" +#include "mongo/client/client_deprecated.h" #include "mongo/client/connpool.h" #include "mongo/client/dbclient_cursor.h" #include "mongo/client/global_conn_pool.h" @@ -592,89 +590,6 @@ std::unique_ptr<DBClientCursor> DBClientReplicaSet::find(FindCommandRequest find return checkPrimary()->find(std::move(findRequest), readPref, exhaustMode); } -unique_ptr<DBClientCursor> DBClientReplicaSet::query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) { - shared_ptr<ReadPreferenceSetting> readPref(_extractReadPref(querySettings, queryOptions)); - invariant(nsOrUuid.nss()); - const string ns = nsOrUuid.nss()->ns(); - if (_isSecondaryQuery(ns, filter, *readPref)) { - LOGV2_DEBUG(20133, - 3, - "dbclient_rs query using secondary or tagged node selection in {replicaSet}, " - "read pref is {readPref} " - "(primary : {primary}, lastTagged : {lastTagged})", - "dbclient_rs query using secondary or tagged node selection", - "replicaSet"_attr = _getMonitor()->getName(), - "readPref"_attr = readPref->toString(), - "primary"_attr = - (_primary.get() != nullptr ? _primary->getServerAddress() : "[not cached]"), - "lastTagged"_attr = (_lastSecondaryOkConn.get() != nullptr - ? _lastSecondaryOkConn->getServerAddress() - : "[not cached]")); - - string lastNodeErrMsg; - - for (size_t retry = 0; retry < MAX_RETRY; retry++) { - try { - DBClientConnection* conn = selectNodeUsingTags(readPref); - - if (conn == nullptr) { - break; - } - - unique_ptr<DBClientCursor> cursor = conn->query_DEPRECATED(nsOrUuid, - filter, - querySettings, - limit, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj); - - return checkSecondaryQueryResult(std::move(cursor)); - } catch (const DBException& ex) { - const Status status = ex.toStatus(str::stream() << "can't query replica set node " - << _lastSecondaryOkHost); - lastNodeErrMsg = status.reason(); - _invalidateLastSecondaryOkCache(status); - } - } - - StringBuilder assertMsg; - assertMsg << "Failed to do query, no good nodes in " << _getMonitor()->getName(); - if (!lastNodeErrMsg.empty()) { - assertMsg << ", last error: " << lastNodeErrMsg; - } - - uasserted(16370, assertMsg.str()); - } - - LOGV2_DEBUG(20134, - 3, - "dbclient_rs query to primary node in {replicaSet}", - "dbclient_rs query to primary node", - "replicaSet"_attr = _getMonitor()->getName()); - - return checkPrimary()->query_DEPRECATED(nsOrUuid, - filter, - querySettings, - limit, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj); -} - void DBClientReplicaSet::killCursor(const NamespaceString& ns, long long cursorID) { // we should never call killCursor on a replica set connection // since we don't know which server it belongs to diff --git a/src/mongo/client/dbclient_rs.h b/src/mongo/client/dbclient_rs.h index ebab6854ffc..fa796039f2c 100644 --- a/src/mongo/client/dbclient_rs.h +++ b/src/mongo/client/dbclient_rs.h @@ -58,7 +58,6 @@ typedef std::shared_ptr<ReplicaSetMonitor> ReplicaSetMonitorPtr; class DBClientReplicaSet : public DBClientBase { public: using DBClientBase::find; - using DBClientBase::query_DEPRECATED; /** Call connect() after constructing. autoReconnect is always on for DBClientReplicaSet * connections. */ @@ -93,18 +92,6 @@ public: const ReadPreferenceSetting& readPref, ExhaustMode exhaustMode) override; - /** throws userassertion "no primary found" */ - std::unique_ptr<DBClientCursor> query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit = 0, - int nToSkip = 0, - const BSONObj* fieldsToReturn = nullptr, - int queryOptions = 0, - int batchSize = 0, - boost::optional<BSONObj> readConcernObj = boost::none) override; - void insert(const std::string& ns, BSONObj obj, bool ordered = true, diff --git a/src/mongo/client/dbclient_rs_test.cpp b/src/mongo/client/dbclient_rs_test.cpp index 2bbbc78858a..7053d8fe623 100644 --- a/src/mongo/client/dbclient_rs_test.cpp +++ b/src/mongo/client/dbclient_rs_test.cpp @@ -152,57 +152,16 @@ void assertNodeSelected(MockReplicaSet* replSet, ReadPreference rp, StringData h assertOneOfNodesSelected(replSet, rp, std::vector<std::string>{host.toString()}); } -/** - * Runs a find operation against 'replConn' using both the modern 'find()' API and the deprecated - * API. In both cases, verifies the results by passing the resulting cursor to 'assertionFunc'. - * - * The operation is a simple find command against the given NamespaceString with no arguments other - * than 'readPref'. - */ -void assertWithBothQueryApis(DBClientReplicaSet& replConn, - const NamespaceString& nss, - ReadPreference readPref, - std::function<void(std::unique_ptr<DBClientCursor>)> assertionFunc) { - std::unique_ptr<DBClientCursor> cursor = - replConn.find(FindCommandRequest{nss}, ReadPreferenceSetting{readPref}); - assertionFunc(std::move(cursor)); - - client_deprecated::Query readPrefHolder; - readPrefHolder.readPref(readPref, BSONArray{}); - cursor = replConn.query_DEPRECATED(nss, BSONObj{}, readPrefHolder); - assertionFunc(std::move(cursor)); -} - -/** - * Runs a find operation against 'replConn' using both the modern 'find()' API and the deprecated - * API. In both cases, verifies that the find operation throws an exception. - * - * The operation is a simple find command against the given NamespaceString with no arguments other - * than 'readPref'. - */ -void assertBothQueryApisThrow(DBClientReplicaSet& replConn, - const NamespaceString& nss, - ReadPreference readPref) { - ASSERT_THROWS(replConn.find(FindCommandRequest{nss}, ReadPreferenceSetting{readPref}), - AssertionException); - - client_deprecated::Query readPrefHolder; - readPrefHolder.readPref(readPref, BSONArray{}); - ASSERT_THROWS(replConn.query_DEPRECATED(nss, BSONObj{}, readPrefHolder), AssertionException); -} - TEST_F(BasicRS, QueryPrimary) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::PrimaryOnly, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryOnly}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); } TEST_F(BasicRS, CommandPrimary) { @@ -214,14 +173,11 @@ TEST_F(BasicRS, QuerySecondaryOnly) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::SecondaryOnly, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getSecondaries().front(), - doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::SecondaryOnly}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getSecondaries().front(), doc[HostField.name()].str()); } TEST_F(BasicRS, CommandSecondaryOnly) { @@ -234,13 +190,11 @@ TEST_F(BasicRS, QueryPrimaryPreferred) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::PrimaryPreferred, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryPreferred}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); } TEST_F(BasicRS, CommandPrimaryPreferred) { @@ -252,14 +206,11 @@ TEST_F(BasicRS, QuerySecondaryPreferred) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::SecondaryPreferred, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getSecondaries().front(), - doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = replConn.find(std::move(findCmd), + ReadPreferenceSetting{ReadPreference::SecondaryPreferred}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getSecondaries().front(), doc[HostField.name()].str()); } TEST_F(BasicRS, CommandSecondaryPreferred) { @@ -319,7 +270,10 @@ TEST_F(AllNodesDown, QueryPrimary) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertBothQueryApisThrow(replConn, NamespaceString{IdentityNS}, ReadPreference::PrimaryOnly); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + ASSERT_THROWS( + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryOnly}), + AssertionException); } TEST_F(AllNodesDown, CommandPrimary) { @@ -330,7 +284,10 @@ TEST_F(AllNodesDown, QuerySecondaryOnly) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertBothQueryApisThrow(replConn, NamespaceString{IdentityNS}, ReadPreference::SecondaryOnly); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + ASSERT_THROWS( + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::SecondaryOnly}), + AssertionException); } TEST_F(AllNodesDown, CommandSecondaryOnly) { @@ -341,8 +298,10 @@ TEST_F(AllNodesDown, QueryPrimaryPreferred) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertBothQueryApisThrow( - replConn, NamespaceString{IdentityNS}, ReadPreference::PrimaryPreferred); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + ASSERT_THROWS( + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryPreferred}), + AssertionException); } TEST_F(AllNodesDown, CommandPrimaryPreferred) { @@ -353,8 +312,10 @@ TEST_F(AllNodesDown, QuerySecondaryPreferred) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertBothQueryApisThrow( - replConn, NamespaceString{IdentityNS}, ReadPreference::SecondaryPreferred); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + ASSERT_THROWS(replConn.find(std::move(findCmd), + ReadPreferenceSetting{ReadPreference::SecondaryPreferred}), + AssertionException); } TEST_F(AllNodesDown, CommandSecondaryPreferred) { @@ -365,7 +326,9 @@ TEST_F(AllNodesDown, QueryNearest) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertBothQueryApisThrow(replConn, NamespaceString{IdentityNS}, ReadPreference::Nearest); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + ASSERT_THROWS(replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::Nearest}), + AssertionException); } TEST_F(AllNodesDown, CommandNearest) { @@ -409,7 +372,10 @@ TEST_F(PrimaryDown, QueryPrimary) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertBothQueryApisThrow(replConn, NamespaceString{IdentityNS}, ReadPreference::PrimaryOnly); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + ASSERT_THROWS( + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryOnly}), + AssertionException); } TEST_F(PrimaryDown, CommandPrimary) { @@ -421,14 +387,11 @@ TEST_F(PrimaryDown, QuerySecondaryOnly) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::SecondaryOnly, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getSecondaries().front(), - doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::SecondaryOnly}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getSecondaries().front(), doc[HostField.name()].str()); } TEST_F(PrimaryDown, CommandSecondaryOnly) { @@ -441,14 +404,11 @@ TEST_F(PrimaryDown, QueryPrimaryPreferred) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::PrimaryPreferred, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getSecondaries().front(), - doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryPreferred}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getSecondaries().front(), doc[HostField.name()].str()); } TEST_F(PrimaryDown, CommandPrimaryPreferred) { @@ -461,14 +421,11 @@ TEST_F(PrimaryDown, QuerySecondaryPreferred) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::SecondaryPreferred, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getSecondaries().front(), - doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = replConn.find(std::move(findCmd), + ReadPreferenceSetting{ReadPreference::SecondaryPreferred}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getSecondaries().front(), doc[HostField.name()].str()); } TEST_F(PrimaryDown, CommandSecondaryPreferred) { @@ -480,14 +437,10 @@ TEST_F(PrimaryDown, Nearest) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::Nearest, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getSecondaries().front(), - doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::Nearest}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getSecondaries().front(), doc[HostField.name()].str()); } /** @@ -529,13 +482,11 @@ TEST_F(SecondaryDown, QueryPrimary) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::PrimaryOnly, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryOnly}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); } TEST_F(SecondaryDown, CommandPrimary) { @@ -546,7 +497,10 @@ TEST_F(SecondaryDown, QuerySecondaryOnly) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertBothQueryApisThrow(replConn, NamespaceString{IdentityNS}, ReadPreference::SecondaryOnly); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + ASSERT_THROWS( + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::SecondaryOnly}), + AssertionException); } TEST_F(SecondaryDown, CommandSecondaryOnly) { @@ -558,13 +512,11 @@ TEST_F(SecondaryDown, QueryPrimaryPreferred) { DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); // Note: IdentityNS contains the name of the server. - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::PrimaryPreferred, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = + replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::PrimaryPreferred}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); } TEST_F(SecondaryDown, CommandPrimaryPreferred) { @@ -575,13 +527,11 @@ TEST_F(SecondaryDown, QuerySecondaryPreferred) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::SecondaryPreferred, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = replConn.find(std::move(findCmd), + ReadPreferenceSetting{ReadPreference::SecondaryPreferred}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); } TEST_F(SecondaryDown, CommandSecondaryPreferred) { @@ -592,13 +542,10 @@ TEST_F(SecondaryDown, QueryNearest) { MockReplicaSet* replSet = getReplSet(); DBClientReplicaSet replConn(replSet->getSetName(), replSet->getHosts(), StringData()); - assertWithBothQueryApis(replConn, - NamespaceString{IdentityNS}, - ReadPreference::Nearest, - [&](std::unique_ptr<DBClientCursor> cursor) { - BSONObj doc = cursor->next(); - ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); - }); + FindCommandRequest findCmd{NamespaceString{IdentityNS}}; + auto cursor = replConn.find(std::move(findCmd), ReadPreferenceSetting{ReadPreference::Nearest}); + BSONObj doc = cursor->next(); + ASSERT_EQUALS(replSet->getPrimary(), doc[HostField.name()].str()); } TEST_F(SecondaryDown, CommandNearest) { diff --git a/src/mongo/db/cloner.cpp b/src/mongo/db/cloner.cpp index 7c2ed6583d4..a6f394b481d 100644 --- a/src/mongo/db/cloner.cpp +++ b/src/mongo/db/cloner.cpp @@ -90,11 +90,11 @@ BSONObj Cloner::_getIdIndexSpec(const std::list<BSONObj>& indexSpecs) { Cloner::Cloner() {} -struct Cloner::Fun { - Fun(OperationContext* opCtx, const std::string& dbName) +struct Cloner::BatchHandler { + BatchHandler(OperationContext* opCtx, const std::string& dbName) : lastLog(0), opCtx(opCtx), _dbName(dbName) {} - void operator()(DBClientCursorBatchIterator& i) { + void operator()(DBClientCursor& cursor) { boost::optional<Lock::DBLock> dbLock; dbLock.emplace(opCtx, _dbName, MODE_X); uassert(ErrorCodes::NotWritablePrimary, @@ -128,7 +128,7 @@ struct Cloner::Fun { }); } - while (i.moreInCurrentBatch()) { + while (cursor.moreInCurrentBatch()) { if (numSeen % 128 == 127) { time_t now = time(nullptr); if (now - lastLog >= 60) { @@ -164,7 +164,7 @@ struct Cloner::Fun { collection); } - BSONObj tmp = i.nextSafe(); + BSONObj tmp = cursor.nextSafe(); /* assure object is valid. note this will slow us down a little. */ // We allow cloning of collections containing decimal data even if decimal is disabled. @@ -245,23 +245,24 @@ void Cloner::_copy(OperationContext* opCtx, logAttrs(nss), "conn_getServerAddress"_attr = conn->getServerAddress()); - Fun f(opCtx, toDBName); - f.numSeen = 0; - f.nss = nss; - f.from_options = from_opts; - f.from_id_index = from_id_index; - f.saveLast = time(nullptr); - - int options = QueryOption_NoCursorTimeout | QueryOption_Exhaust; - - conn->query_DEPRECATED(std::function<void(DBClientCursorBatchIterator&)>(f), - nss, - BSONObj{} /* filter */, - client_deprecated::Query() /* querySettings */, - nullptr, - options, - 0 /* batchSize */, - repl::ReadConcernArgs::kLocal); + BatchHandler batchHandler{opCtx, toDBName}; + batchHandler.numSeen = 0; + batchHandler.nss = nss; + batchHandler.from_options = from_opts; + batchHandler.from_id_index = from_id_index; + batchHandler.saveLast = time(nullptr); + + FindCommandRequest findCmd{nss}; + findCmd.setNoCursorTimeout(true); + findCmd.setReadConcern(repl::ReadConcernArgs::kLocal); + auto cursor = conn->find(std::move(findCmd), + ReadPreferenceSetting{ReadPreference::SecondaryPreferred}, + ExhaustMode::kOn); + + // Process the results of the cursor in batches. + while (cursor->more()) { + batchHandler(*cursor); + } } void Cloner::_copyIndexes(OperationContext* opCtx, diff --git a/src/mongo/db/cloner.h b/src/mongo/db/cloner.h index 8d1d512fe1f..5cbb4d76337 100644 --- a/src/mongo/db/cloner.h +++ b/src/mongo/db/cloner.h @@ -104,7 +104,7 @@ private: const std::list<BSONObj>& from_indexes, DBClientBase* conn); - struct Fun; + struct BatchHandler; }; } // namespace mongo diff --git a/src/mongo/db/commands/user_management_commands.cpp b/src/mongo/db/commands/user_management_commands.cpp index d1bfe34e501..63bfeb73a03 100644 --- a/src/mongo/db/commands/user_management_commands.cpp +++ b/src/mongo/db/commands/user_management_commands.cpp @@ -1461,8 +1461,11 @@ UsersInfoReply CmdUMCTyped<UsersInfoCommand, UMCInfoParams>::Invocation::typedRu CommandHelpers::appendSimpleCommandStatus(bodyBuilder, true); bodyBuilder.doneFast(); auto response = CursorResponse::parseFromBSONThrowing(replyBuilder.releaseBody()); - DBClientCursor cursor( - &client, response.getNSS(), response.getCursorId(), 0, 0, response.releaseBatch()); + DBClientCursor cursor(&client, + response.getNSS(), + response.getCursorId(), + false /*isExhaust*/, + response.releaseBatch()); while (cursor.more()) { users.push_back(cursor.next().getOwned()); diff --git a/src/mongo/db/repl/apply_ops.cpp b/src/mongo/db/repl/apply_ops.cpp index 7cae9ba2b01..4887982c95c 100644 --- a/src/mongo/db/repl/apply_ops.cpp +++ b/src/mongo/db/repl/apply_ops.cpp @@ -31,6 +31,7 @@ #include "mongo/db/repl/apply_ops.h" #include "mongo/bson/util/bson_extract.h" +#include "mongo/client/client_deprecated.h" #include "mongo/db/catalog/collection.h" #include "mongo/db/catalog/database.h" #include "mongo/db/catalog/database_holder.h" diff --git a/src/mongo/db/repl/collection_cloner.cpp b/src/mongo/db/repl/collection_cloner.cpp index dd261037b08..e380fbe6238 100644 --- a/src/mongo/db/repl/collection_cloner.cpp +++ b/src/mongo/db/repl/collection_cloner.cpp @@ -317,38 +317,43 @@ BaseCloner::AfterStageBehavior CollectionCloner::setupIndexBuildersForUnfinished } void CollectionCloner::runQuery() { - // Non-resumable query. - client_deprecated::Query query; + FindCommandRequest findCmd{_sourceDbAndUuid}; if (_resumeToken) { // Resume the query from where we left off. LOGV2_DEBUG(21133, 1, "Collection cloner will resume the last successful query"); - query.requestResumeToken(true).resumeAfter(_resumeToken.get()); + findCmd.setRequestResumeToken(true); + findCmd.setResumeAfter(_resumeToken.get()); } else { // New attempt at a resumable query. LOGV2_DEBUG(21134, 1, "Collection cloner will run a new query"); - query.requestResumeToken(true); + findCmd.setRequestResumeToken(true); } - query.hint(BSON("$natural" << 1)); + + findCmd.setHint(BSON("$natural" << 1)); + findCmd.setNoCursorTimeout(true); + findCmd.setReadConcern(ReadConcernArgs::kLocal); + if (_collectionClonerBatchSize) { + findCmd.setBatchSize(_collectionClonerBatchSize); + } + + ExhaustMode exhaustMode = collectionClonerUsesExhaust ? ExhaustMode::kOn : ExhaustMode::kOff; // We reset this every time we retry or resume a query. // We distinguish the first batch from the rest so that we only store the remote cursor id // the first time we get it. _firstBatchOfQueryRound = true; - getClient()->query_DEPRECATED( - [this](DBClientCursorBatchIterator& iter) { handleNextBatch(iter); }, - _sourceDbAndUuid, - BSONObj{}, - query, - nullptr /* fieldsToReturn */, - QueryOption_NoCursorTimeout | QueryOption_SecondaryOk | - (collectionClonerUsesExhaust ? QueryOption_Exhaust : 0), - _collectionClonerBatchSize, - ReadConcernArgs::kLocal); + auto cursor = getClient()->find( + std::move(findCmd), ReadPreferenceSetting{ReadPreference::SecondaryPreferred}, exhaustMode); + + // Process the results of the cursor one batch at a time. + while (cursor->more()) { + handleNextBatch(*cursor); + } } -void CollectionCloner::handleNextBatch(DBClientCursorBatchIterator& iter) { +void CollectionCloner::handleNextBatch(DBClientCursor& cursor) { { stdx::lock_guard<InitialSyncSharedData> lk(*getSharedData()); if (!getSharedData()->getStatus(lk).isOK()) { @@ -370,15 +375,15 @@ void CollectionCloner::handleNextBatch(DBClientCursorBatchIterator& iter) { if (_firstBatchOfQueryRound) { // Store the cursorId of the remote cursor. - _remoteCursorId = iter.getCursorId(); + _remoteCursorId = cursor.getCursorId(); } _firstBatchOfQueryRound = false; { stdx::lock_guard<Latch> lk(_mutex); _stats.receivedBatches++; - while (iter.moreInCurrentBatch()) { - _documentsToInsert.emplace_back(iter.nextSafe()); + while (cursor.moreInCurrentBatch()) { + _documentsToInsert.emplace_back(cursor.nextSafe()); } } @@ -394,7 +399,7 @@ void CollectionCloner::handleNextBatch(DBClientCursorBatchIterator& iter) { } // Store the resume token for this batch. - _resumeToken = iter.getPostBatchResumeToken(); + _resumeToken = cursor.getPostBatchResumeToken(); initialSyncHangCollectionClonerAfterHandlingBatchResponse.executeIf( [&](const BSONObj&) { diff --git a/src/mongo/db/repl/collection_cloner.h b/src/mongo/db/repl/collection_cloner.h index 80d8a9d72bc..085c6abdb3f 100644 --- a/src/mongo/db/repl/collection_cloner.h +++ b/src/mongo/db/repl/collection_cloner.h @@ -207,10 +207,10 @@ private: AfterStageBehavior setupIndexBuildersForUnfinishedIndexesStage(); /** - * Put all results from a query batch into a buffer to be inserted, and schedule - * it to be inserted. + * Put all results from a query batch into a buffer to be inserted, and schedule it to be + * inserted. */ - void handleNextBatch(DBClientCursorBatchIterator& iter); + void handleNextBatch(DBClientCursor& cursor); /** * Called whenever there is a new batch of documents ready from the DBClientConnection. diff --git a/src/mongo/db/repl/oplog_fetcher.cpp b/src/mongo/db/repl/oplog_fetcher.cpp index ea7e73a4033..6ec6c9778de 100644 --- a/src/mongo/db/repl/oplog_fetcher.cpp +++ b/src/mongo/db/repl/oplog_fetcher.cpp @@ -265,12 +265,8 @@ OpTime OplogFetcher::getLastOpTimeFetched_forTest() const { return _getLastOpTimeFetched(); } -BSONObj OplogFetcher::getFindQueryFilter_forTest() const { - return _makeFindQueryFilter(); -} - -client_deprecated::Query OplogFetcher::getFindQuerySettings_forTest(long long findTimeout) const { - return _makeFindQuerySettings(findTimeout); +FindCommandRequest OplogFetcher::makeFindCmdRequest_forTest(long long findTimeout) const { + return _makeFindCmdRequest(findTimeout); } Milliseconds OplogFetcher::getAwaitDataTimeout_forTest() const { @@ -584,46 +580,56 @@ AggregateCommandRequest OplogFetcher::_makeAggregateCommandRequest(long long max return aggRequest; } -BSONObj OplogFetcher::_makeFindQueryFilter() const { - BSONObjBuilder queryBob; - - auto lastOpTimeFetched = _getLastOpTimeFetched(); - BSONObjBuilder filterBob; - filterBob.append("ts", BSON("$gte" << lastOpTimeFetched.getTimestamp())); - // Handle caller-provided filter. - if (!_config.queryFilter.isEmpty()) { - filterBob.append( - "$or", - BSON_ARRAY(_config.queryFilter << BSON("ts" << lastOpTimeFetched.getTimestamp()))); +FindCommandRequest OplogFetcher::_makeFindCmdRequest(long long findTimeout) const { + FindCommandRequest findCmd{_nss}; + + // Construct the find command's filter and set it on the 'FindCommandRequest'. + { + BSONObjBuilder queryBob; + + auto lastOpTimeFetched = _getLastOpTimeFetched(); + BSONObjBuilder filterBob; + filterBob.append("ts", BSON("$gte" << lastOpTimeFetched.getTimestamp())); + // Handle caller-provided filter. + if (!_config.queryFilter.isEmpty()) { + filterBob.append( + "$or", + BSON_ARRAY(_config.queryFilter << BSON("ts" << lastOpTimeFetched.getTimestamp()))); + } + findCmd.setFilter(filterBob.obj()); + } + + findCmd.setTailable(true); + findCmd.setAwaitData(true); + findCmd.setMaxTimeMS(findTimeout); + + if (_config.batchSize) { + findCmd.setBatchSize(_config.batchSize); } - return filterBob.obj(); -} -client_deprecated::Query OplogFetcher::_makeFindQuerySettings(long long findTimeout) const { - auto query = client_deprecated::Query().maxTimeMS(findTimeout); if (_config.requestResumeToken) { - query.hint(BSON("$natural" << 1)).requestResumeToken(true); + findCmd.setHint(BSON("$natural" << 1)); + findCmd.setRequestResumeToken(true); } auto lastCommittedWithCurrentTerm = _dataReplicatorExternalState->getCurrentTermAndLastCommittedOpTime(); auto term = lastCommittedWithCurrentTerm.value; if (term != OpTime::kUninitializedTerm) { - query.term(term); + findCmd.setTerm(term); } if (_config.queryReadConcern.isEmpty()) { // This ensures that the sync source waits for all earlier oplog writes to be visible. // Since Timestamp(0, 0) isn't allowed, Timestamp(0, 1) is the minimal we can use. - query.readConcern(BSON("level" - << "local" - << "afterClusterTime" << Timestamp(0, 1))); + findCmd.setReadConcern(BSON("level" + << "local" + << "afterClusterTime" << Timestamp(0, 1))); } else { // Caller-provided read concern. - query.appendElements(_config.queryReadConcern.toBSON()); + findCmd.setReadConcern(_config.queryReadConcern.toBSONInner()); } - - return query; + return findCmd; } Status OplogFetcher::_createNewCursor(bool initialFind) { @@ -651,17 +657,9 @@ Status OplogFetcher::_createNewCursor(bool initialFind) { } _cursor = std::move(ret.getValue()); } else { + auto findCmd = _makeFindCmdRequest(maxTimeMs); _cursor = std::make_unique<DBClientCursor>( - _conn.get(), - _nss, - _makeFindQueryFilter(), - _makeFindQuerySettings(maxTimeMs), - 0 /* limit */, - 0 /* nToSkip */, - nullptr /* fieldsToReturn */, - QueryOption_CursorTailable | QueryOption_AwaitData | - (oplogFetcherUsesExhaust ? QueryOption_Exhaust : 0), - _config.batchSize); + _conn.get(), std::move(findCmd), ReadPreferenceSetting{}, oplogFetcherUsesExhaust); } _firstBatch = true; diff --git a/src/mongo/db/repl/oplog_fetcher.h b/src/mongo/db/repl/oplog_fetcher.h index 07cdf982b38..2147eb9ebde 100644 --- a/src/mongo/db/repl/oplog_fetcher.h +++ b/src/mongo/db/repl/oplog_fetcher.h @@ -275,8 +275,7 @@ public: /** * Returns the `find` query run on the sync source's oplog. */ - BSONObj getFindQueryFilter_forTest() const; - client_deprecated::Query getFindQuerySettings_forTest(long long findTimeout) const; + FindCommandRequest makeFindCmdRequest_forTest(long long findTimeout) const; /** * Returns the OpTime of the last oplog entry fetched and processed. @@ -387,11 +386,9 @@ private: /** * This function will create the `find` query to issue to the sync source. It is provided with - * whether this is the initial attempt to create the `find` query to determine what the find - * timeout should be. + * the value to use as the "maxTimeMS" for the find command. */ - BSONObj _makeFindQueryFilter() const; - client_deprecated::Query _makeFindQuerySettings(long long findTimeout) const; + FindCommandRequest _makeFindCmdRequest(long long findTimeout) const; /** * Gets the next batch from the exhaust cursor. diff --git a/src/mongo/db/repl/oplog_fetcher_test.cpp b/src/mongo/db/repl/oplog_fetcher_test.cpp index e98039a0f8a..adc09da1300 100644 --- a/src/mongo/db/repl/oplog_fetcher_test.cpp +++ b/src/mongo/db/repl/oplog_fetcher_test.cpp @@ -806,19 +806,25 @@ TEST_F(OplogFetcherTest, auto oplogFetcher = makeOplogFetcher(); auto findTimeout = durationCount<Milliseconds>(oplogFetcher->getInitialFindMaxTime_forTest()); - auto filter = oplogFetcher->getFindQueryFilter_forTest(); + auto findCmdRequest = oplogFetcher->makeFindCmdRequest_forTest(findTimeout); + + auto filter = findCmdRequest.getFilter(); ASSERT_BSONOBJ_EQ(BSON("ts" << BSON("$gte" << lastFetched.getTimestamp())), filter); - auto queryObj = - (oplogFetcher->getFindQuerySettings_forTest(findTimeout)).getFullSettingsDeprecated(); - ASSERT_EQUALS(60000, queryObj.getIntField("$maxTimeMS")); + auto maxTimeMS = findCmdRequest.getMaxTimeMS(); + ASSERT(maxTimeMS); + ASSERT_EQUALS(60000, *maxTimeMS); - ASSERT_EQUALS(mongo::BSONType::Object, queryObj["readConcern"].type()); + auto readConcern = findCmdRequest.getReadConcern(); + ASSERT(readConcern); ASSERT_BSONOBJ_EQ(BSON("level" << "local" << "afterClusterTime" << Timestamp(0, 1)), - queryObj["readConcern"].Obj()); - ASSERT_EQUALS(dataReplicatorExternalState->currentTerm, queryObj["term"].numberLong()); + *readConcern); + + auto term = findCmdRequest.getTerm(); + ASSERT(term); + ASSERT_EQUALS(dataReplicatorExternalState->currentTerm, *term); } TEST_F(OplogFetcherTest, @@ -826,21 +832,26 @@ TEST_F(OplogFetcherTest, dataReplicatorExternalState->currentTerm = OpTime::kUninitializedTerm; auto oplogFetcher = makeOplogFetcher(); - auto filter = oplogFetcher->getFindQueryFilter_forTest(); - ASSERT_BSONOBJ_EQ(BSON("ts" << BSON("$gte" << lastFetched.getTimestamp())), filter); - // Test that the correct maxTimeMS is set if we are retrying the 'find' query. auto findTimeout = durationCount<Milliseconds>(oplogFetcher->getRetriedFindMaxTime_forTest()); - auto queryObj = - (oplogFetcher->getFindQuerySettings_forTest(findTimeout)).getFullSettingsDeprecated(); - ASSERT_EQUALS(2000, queryObj.getIntField("$maxTimeMS")); + auto findCmdRequest = oplogFetcher->makeFindCmdRequest_forTest(findTimeout); - ASSERT_EQUALS(mongo::BSONType::Object, queryObj["readConcern"].type()); + auto filter = findCmdRequest.getFilter(); + ASSERT_BSONOBJ_EQ(BSON("ts" << BSON("$gte" << lastFetched.getTimestamp())), filter); + + auto maxTimeMS = findCmdRequest.getMaxTimeMS(); + ASSERT(maxTimeMS); + ASSERT_EQUALS(2000, *maxTimeMS); + + auto readConcern = findCmdRequest.getReadConcern(); + ASSERT(readConcern); ASSERT_BSONOBJ_EQ(BSON("level" << "local" << "afterClusterTime" << Timestamp(0, 1)), - queryObj["readConcern"].Obj()); - ASSERT_FALSE(queryObj.hasField("term")); + *readConcern); + + auto term = findCmdRequest.getTerm(); + ASSERT(!term); } TEST_F( diff --git a/src/mongo/db/repl/tenant_collection_cloner.cpp b/src/mongo/db/repl/tenant_collection_cloner.cpp index 0635903d48d..165538954bd 100644 --- a/src/mongo/db/repl/tenant_collection_cloner.cpp +++ b/src/mongo/db/repl/tenant_collection_cloner.cpp @@ -474,35 +474,42 @@ BaseCloner::AfterStageBehavior TenantCollectionCloner::queryStage() { } void TenantCollectionCloner::runQuery() { - const BSONObj& filter = _lastDocId.isEmpty() - ? BSONObj{} // Use $expr and the aggregation version of $gt to avoid type bracketing. - : BSON("$expr" << BSON("$gt" << BSON_ARRAY("$_id" << _lastDocId["_id"]))); - - auto query = _collectionOptions.clusteredIndex - // RecordIds are _id values and has no separate _id index - ? client_deprecated::Query().hint(BSON("$natural" << 1)) - : client_deprecated::Query().hint(BSON("_id" << 1)); - - // Any errors that are thrown here (including NamespaceNotFound) will be handled on the stage - // level. - getClient()->query_DEPRECATED( - [this](DBClientCursorBatchIterator& iter) { handleNextBatch(iter); }, - _sourceDbAndUuid, - filter, - query, - nullptr /* fieldsToReturn */, - QueryOption_NoCursorTimeout | QueryOption_SecondaryOk | - (collectionClonerUsesExhaust ? QueryOption_Exhaust : 0), - _collectionClonerBatchSize, - ReadConcernArgs(ReadConcernLevel::kMajorityReadConcern).toBSONInner()); + FindCommandRequest findCmd{_sourceDbAndUuid}; + + findCmd.setFilter( + _lastDocId.isEmpty() + ? BSONObj{} // Use $expr and the aggregation version of $gt to avoid type bracketing. + : BSON("$expr" << BSON("$gt" << BSON_ARRAY("$_id" << _lastDocId["_id"])))); + + if (_collectionOptions.clusteredIndex) { + findCmd.setHint(BSON("$natural" << 1)); + } else { + findCmd.setHint(BSON("_id" << 1)); + } + + findCmd.setNoCursorTimeout(true); + findCmd.setReadConcern(ReadConcernArgs(ReadConcernLevel::kMajorityReadConcern).toBSONInner()); + if (_collectionClonerBatchSize) { + findCmd.setBatchSize(_collectionClonerBatchSize); + } + + ExhaustMode exhaustMode = collectionClonerUsesExhaust ? ExhaustMode::kOn : ExhaustMode::kOff; + + auto cursor = getClient()->find( + std::move(findCmd), ReadPreferenceSetting{ReadPreference::SecondaryPreferred}, exhaustMode); + + // Process the results of the cursor one batch at a time. + while (cursor->more()) { + handleNextBatch(*cursor); + } } -void TenantCollectionCloner::handleNextBatch(DBClientCursorBatchIterator& iter) { +void TenantCollectionCloner::handleNextBatch(DBClientCursor& cursor) { { stdx::lock_guard<Latch> lk(_mutex); _stats.receivedBatches++; - while (iter.moreInCurrentBatch()) { - _documentsToInsert.emplace_back(iter.nextSafe()); + while (cursor.moreInCurrentBatch()) { + _documentsToInsert.emplace_back(cursor.nextSafe()); } } diff --git a/src/mongo/db/repl/tenant_collection_cloner.h b/src/mongo/db/repl/tenant_collection_cloner.h index b9c22928917..12bd9bbb832 100644 --- a/src/mongo/db/repl/tenant_collection_cloner.h +++ b/src/mongo/db/repl/tenant_collection_cloner.h @@ -209,10 +209,10 @@ private: AfterStageBehavior queryStage(); /** - * Put all results from a query batch into a buffer to be inserted, and schedule - * it to be inserted. + * Put all results from a query batch into a buffer to be inserted, and schedule it to be + * inserted. */ - void handleNextBatch(DBClientCursorBatchIterator& iter); + void handleNextBatch(DBClientCursor& cursor); /** * Called whenever there is a new batch of documents ready from the DBClientConnection. diff --git a/src/mongo/db/repl/tenant_file_cloner.cpp b/src/mongo/db/repl/tenant_file_cloner.cpp index 83ae3c65fc8..b909039eed1 100644 --- a/src/mongo/db/repl/tenant_file_cloner.cpp +++ b/src/mongo/db/repl/tenant_file_cloner.cpp @@ -188,8 +188,7 @@ void TenantFileCloner::runQuery() { getClient(), std::move(aggRequest), true /* secondaryOk */, useExhaust)); try { while (cursor->more()) { - DBClientCursorBatchIterator iter(*cursor); - handleNextBatch(iter); + handleNextBatch(*cursor); } } catch (const DBException& e) { // We cannot continue after an error when processing exhaust cursors. Instead we must @@ -207,7 +206,7 @@ void TenantFileCloner::runQuery() { } } -void TenantFileCloner::handleNextBatch(DBClientCursorBatchIterator& iter) { +void TenantFileCloner::handleNextBatch(DBClientCursor& cursor) { LOGV2_DEBUG(6113307, 3, "TenantFileCloner handleNextBatch", @@ -215,7 +214,7 @@ void TenantFileCloner::handleNextBatch(DBClientCursorBatchIterator& iter) { "backupId"_attr = _backupId, "remoteFile"_attr = _remoteFileName, "fileOffset"_attr = getFileOffset(), - "moreInCurrentBatch"_attr = iter.moreInCurrentBatch()); + "moreInCurrentBatch"_attr = cursor.moreInCurrentBatch()); { stdx::lock_guard<TenantMigrationSharedData> lk(*getSharedData()); if (!getSharedData()->getStatus(lk).isOK()) { @@ -225,11 +224,11 @@ void TenantFileCloner::handleNextBatch(DBClientCursorBatchIterator& iter) { str::stream() << message << ": " << getSharedData()->getStatus(lk)); } } - while (iter.moreInCurrentBatch()) { + while (cursor.moreInCurrentBatch()) { stdx::lock_guard<Latch> lk(_mutex); _stats.receivedBatches++; - while (iter.moreInCurrentBatch()) { - _dataToWrite.emplace_back(iter.nextSafe()); + while (cursor.moreInCurrentBatch()) { + _dataToWrite.emplace_back(cursor.nextSafe()); } } diff --git a/src/mongo/db/repl/tenant_file_cloner.h b/src/mongo/db/repl/tenant_file_cloner.h index 90e37946224..27ff89fbc3a 100644 --- a/src/mongo/db/repl/tenant_file_cloner.h +++ b/src/mongo/db/repl/tenant_file_cloner.h @@ -160,7 +160,7 @@ private: /** * Put all results from a query batch into a buffer, and schedule it to be written to disk. */ - void handleNextBatch(DBClientCursorBatchIterator& iter); + void handleNextBatch(DBClientCursor& cursor); /** * Called whenever there is a new batch of documents ready from the DBClientConnection. diff --git a/src/mongo/dbtests/mock/mock_dbclient_connection.cpp b/src/mongo/dbtests/mock/mock_dbclient_connection.cpp index f66746b71c0..957888cfa35 100644 --- a/src/mongo/dbtests/mock/mock_dbclient_connection.cpp +++ b/src/mongo/dbtests/mock/mock_dbclient_connection.cpp @@ -160,55 +160,6 @@ std::unique_ptr<DBClientCursor> MockDBClientConnection::find( return nullptr; } -std::unique_ptr<mongo::DBClientCursor> MockDBClientConnection::query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) { - checkConnection(); - - try { - mongo::BSONArray result(_remoteServer->query(_remoteServerInstanceID, - nsOrUuid, - filter, - querySettings, - limit, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj)); - - BSONArray resultsInCursor; - - // A simple mock implementation of a resumable query, where we skip the first 'n' fields - // where 'n' is given by the mock resume token. - auto nToSkip = 0; - BSONObj querySettingsAsBSON = querySettings.getFullSettingsDeprecated(); - if (querySettingsAsBSON.hasField("$_resumeAfter")) { - nToSkip = nToSkipFromResumeAfter(querySettingsAsBSON.getField("$_resumeAfter").Obj()); - } - - bool provideResumeToken = false; - if (querySettingsAsBSON.hasField("$_requestResumeToken")) { - provideResumeToken = true; - } - - - return bsonArrayToCursor(std::move(result), nToSkip, provideResumeToken, batchSize); - } catch (const mongo::DBException&) { - _failed.store(true); - throw; - } - - return nullptr; -} - mongo::ConnectionString::ConnectionType MockDBClientConnection::type() const { return mongo::ConnectionString::ConnectionType::kCustom; } diff --git a/src/mongo/dbtests/mock/mock_dbclient_connection.h b/src/mongo/dbtests/mock/mock_dbclient_connection.h index 0baac0ebfba..4b60f2bec4a 100644 --- a/src/mongo/dbtests/mock/mock_dbclient_connection.h +++ b/src/mongo/dbtests/mock/mock_dbclient_connection.h @@ -104,7 +104,6 @@ public: // DBClientBase methods // using DBClientBase::find; - using DBClientBase::query_DEPRECATED; bool connect(const char* hostName, StringData applicationName, std::string& errmsg); @@ -125,17 +124,6 @@ public: const ReadPreferenceSetting& /*unused*/, ExhaustMode /*unused*/) override; - std::unique_ptr<mongo::DBClientCursor> query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter = BSONObj{}, - const client_deprecated::Query& querySettings = client_deprecated::Query(), - int limit = 0, - int nToSkip = 0, - const mongo::BSONObj* fieldsToReturn = nullptr, - int queryOptions = 0, - int batchSize = 0, - boost::optional<BSONObj> readConcernObj = boost::none) override; - uint64_t getSockCreationMicroSec() const override; void insert(const std::string& ns, diff --git a/src/mongo/dbtests/mock/mock_remote_db_server.cpp b/src/mongo/dbtests/mock/mock_remote_db_server.cpp index b25a4021beb..0b98308d1d2 100644 --- a/src/mongo/dbtests/mock/mock_remote_db_server.cpp +++ b/src/mongo/dbtests/mock/mock_remote_db_server.cpp @@ -228,20 +228,6 @@ mongo::BSONArray MockRemoteDBServer::find(MockRemoteDBServer::InstanceID id, return findImpl(id, findRequest.getNamespaceOrUUID(), findRequest.getProjection()); } -mongo::BSONArray MockRemoteDBServer::query(MockRemoteDBServer::InstanceID id, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) { - BSONObj projection = fieldsToReturn ? *fieldsToReturn : BSONObj{}; - return findImpl(id, nsOrUuid, std::move(projection)); -} - mongo::ConnectionString::ConnectionType MockRemoteDBServer::type() const { return mongo::ConnectionString::ConnectionType::kCustom; } diff --git a/src/mongo/dbtests/mock/mock_remote_db_server.h b/src/mongo/dbtests/mock/mock_remote_db_server.h index c20dc851580..034ad8e7ea4 100644 --- a/src/mongo/dbtests/mock/mock_remote_db_server.h +++ b/src/mongo/dbtests/mock/mock_remote_db_server.h @@ -32,7 +32,6 @@ #include <string> #include <vector> -#include "mongo/client/client_deprecated.h" #include "mongo/client/connection_string.h" #include "mongo/db/jsobj.h" #include "mongo/db/query/find_command_gen.h" @@ -168,20 +167,6 @@ public: */ mongo::BSONArray find(InstanceID id, const FindCommandRequest& findRequest); - /** - * Legacy query API: New callers should use 'find()' rather than this method. - */ - mongo::BSONArray query(InstanceID id, - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit = 0, - int nToSkip = 0, - const mongo::BSONObj* fieldsToReturn = nullptr, - int queryOptions = 0, - int batchSize = 0, - boost::optional<BSONObj> readConcernObj = boost::none); - // // Getters // diff --git a/src/mongo/dbtests/mock_dbclient_conn_test.cpp b/src/mongo/dbtests/mock_dbclient_conn_test.cpp index 91740b4358f..b9228513cf6 100644 --- a/src/mongo/dbtests/mock_dbclient_conn_test.cpp +++ b/src/mongo/dbtests/mock_dbclient_conn_test.cpp @@ -77,45 +77,6 @@ TEST(MockDBClientConnTest, QueryCount) { } } -// This test should be removed when the legacy query API is removed. -TEST(MockDBClientConnTest, LegacyQueryApiBumpsQueryCount) { - MockRemoteDBServer server("test"); - MockDBClientConnection conn(&server); - ASSERT_EQUALS(0U, server.getQueryCount()); - conn.query_DEPRECATED(NamespaceString("foo.bar")); - ASSERT_EQUALS(1U, server.getQueryCount()); -} - -// This test should be removed when the legacy query API is removed. -TEST(MockDBClientConnTest, LegacyQueryApiReturnsInsertedDocuments) { - MockRemoteDBServer server("test"); - const std::string ns("test.user"); - - { - MockDBClientConnection conn(&server); - std::unique_ptr<mongo::DBClientCursor> cursor = conn.query_DEPRECATED(NamespaceString(ns)); - ASSERT(!cursor->more()); - - server.insert(ns, BSON("x" << 1)); - server.insert(ns, BSON("y" << 2)); - } - - { - MockDBClientConnection conn(&server); - std::unique_ptr<mongo::DBClientCursor> cursor = conn.query_DEPRECATED(NamespaceString(ns)); - - ASSERT(cursor->more()); - BSONObj firstDoc = cursor->next(); - ASSERT_EQUALS(1, firstDoc["x"].numberInt()); - - ASSERT(cursor->more()); - BSONObj secondDoc = cursor->next(); - ASSERT_EQUALS(2, secondDoc["y"].numberInt()); - - ASSERT(!cursor->more()); - } -} - TEST(MockDBClientConnTest, SkipBasedOnResumeAfter) { MockRemoteDBServer server{"test"}; const std::string ns{"test.user"}; diff --git a/src/mongo/scripting/mozjs/mongo.cpp b/src/mongo/scripting/mozjs/mongo.cpp index dffc1163e47..849d74aca36 100644 --- a/src/mongo/scripting/mozjs/mongo.cpp +++ b/src/mongo/scripting/mozjs/mongo.cpp @@ -34,6 +34,7 @@ #include "mongo/bson/simple_bsonelement_comparator.h" #include "mongo/client/client_api_version_parameters_gen.h" +#include "mongo/client/client_deprecated.h" #include "mongo/client/dbclient_base.h" #include "mongo/client/dbclient_rs.h" #include "mongo/client/global_conn_pool.h" diff --git a/src/mongo/shell/encrypted_dbclient_base.cpp b/src/mongo/shell/encrypted_dbclient_base.cpp index 0aeeb911885..ba83ab07471 100644 --- a/src/mongo/shell/encrypted_dbclient_base.cpp +++ b/src/mongo/shell/encrypted_dbclient_base.cpp @@ -571,27 +571,6 @@ std::unique_ptr<DBClientCursor> EncryptedDBClientBase::find(FindCommandRequest f return _conn->find(std::move(findRequest), readPref, exhaustMode); } -std::unique_ptr<DBClientCursor> EncryptedDBClientBase::query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj) { - return _conn->query_DEPRECATED(nsOrUuid, - filter, - querySettings, - limit, - nToSkip, - fieldsToReturn, - queryOptions, - batchSize, - readConcernObj); -} - bool EncryptedDBClientBase::isFailed() const { return _conn->isFailed(); } diff --git a/src/mongo/shell/encrypted_dbclient_base.h b/src/mongo/shell/encrypted_dbclient_base.h index 4af6eb03804..ddb0c18e235 100644 --- a/src/mongo/shell/encrypted_dbclient_base.h +++ b/src/mongo/shell/encrypted_dbclient_base.h @@ -87,7 +87,6 @@ class EncryptedDBClientBase : public DBClientBase, public FLEKeyVault { public: using DBClientBase::find; - using DBClientBase::query_DEPRECATED; EncryptedDBClientBase(std::unique_ptr<DBClientBase> conn, ClientSideFLEOptions encryptionOptions, @@ -131,17 +130,6 @@ public: const ReadPreferenceSetting& readPref, ExhaustMode exhaustMode) final; - std::unique_ptr<DBClientCursor> query_DEPRECATED( - const NamespaceStringOrUUID& nsOrUuid, - const BSONObj& filter, - const client_deprecated::Query& querySettings, - int limit, - int nToSkip, - const BSONObj* fieldsToReturn, - int queryOptions, - int batchSize, - boost::optional<BSONObj> readConcernObj = boost::none) final; - bool isFailed() const final; bool isStillConnected() final; |