diff options
-rw-r--r-- | src/mongo/client/client_deprecated.cpp | 68 | ||||
-rw-r--r-- | src/mongo/client/client_deprecated.h | 92 | ||||
-rw-r--r-- | src/mongo/client/dbclient_rs.cpp | 168 | ||||
-rw-r--r-- | src/mongo/db/query/query_request_helper.h | 5 |
4 files changed, 2 insertions, 331 deletions
diff --git a/src/mongo/client/client_deprecated.cpp b/src/mongo/client/client_deprecated.cpp index e8bbf0542f3..2df1d1be3ac 100644 --- a/src/mongo/client/client_deprecated.cpp +++ b/src/mongo/client/client_deprecated.cpp @@ -201,74 +201,6 @@ Status initFindFromOpQueryObj(const BSONObj& querySettings, FindCommandRequest* } // namespace -const BSONField<BSONObj> Query::ReadPrefField("$readPreference"); - -void Query::makeComplex() { - if (isComplex()) - return; - BSONObjBuilder b; - b.append("query", obj); - obj = b.obj(); -} - -Query& Query::sort(const BSONObj& s) { - appendComplex("orderby", s); - return *this; -} - -Query& Query::hint(BSONObj keyPattern) { - appendComplex("$hint", keyPattern); - return *this; -} - -Query& Query::readPref(ReadPreference pref, const BSONArray& tags) { - appendComplex(ReadPrefField.name().c_str(), - ReadPreferenceSetting(pref, TagSet(tags)).toInnerBSON()); - return *this; -} - -bool Query::isComplex(bool* hasDollar) const { - return isComplexQueryObj(obj, hasDollar); -} - -Query& Query::appendElements(BSONObj elements) { - makeComplex(); - BSONObjBuilder b(std::move(obj)); - b.appendElements(elements); - obj = b.obj(); - return *this; -} - -Query& Query::requestResumeToken(bool enable) { - appendComplex("$_requestResumeToken", enable); - return *this; -} - -Query& Query::resumeAfter(BSONObj point) { - appendComplex("$_resumeAfter", point); - return *this; -} - -Query& Query::maxTimeMS(long long timeout) { - appendComplex("$maxTimeMS", timeout); - return *this; -} - -Query& Query::term(long long value) { - appendComplex("term", value); - return *this; -} - -Query& Query::readConcern(BSONObj rc) { - appendComplex("readConcern", rc); - return *this; -} - -Query& Query::readOnce(bool enable) { - appendComplex("$readOnce", enable); - return *this; -} - void initFindFromLegacyOptions(BSONObj bsonOptions, int options, FindCommandRequest* findCommand) { invariant(findCommand); BSONObj filter = filterFromOpQueryObj(bsonOptions); diff --git a/src/mongo/client/client_deprecated.h b/src/mongo/client/client_deprecated.h index d8eb80e5afa..fa4509c62f8 100644 --- a/src/mongo/client/client_deprecated.h +++ b/src/mongo/client/client_deprecated.h @@ -41,98 +41,6 @@ namespace mongo { * added because OP_QUERY is no longer supported by the shell or server. */ namespace client_deprecated { - -/** - * Represents a subset of query settings, such as sort, hint, etc. It is only used in the context of - * the deprecated query API in 'DBClientBase', which has been superseded by `DBClientBase::find()` - * and friends. Additional uses of this class should not be added to the code base! - */ -class Query { -public: - static const BSONField<BSONObj> ReadPrefField; - - /** - * Creating a Query object from raw BSON is on its way out. Please don't add new callers under - * any circumstances. - */ - static Query fromBSONDeprecated(const BSONObj& b) { - Query q; - q.obj = b; - return q; - } - - Query() : obj(BSONObj()) {} - - /** Add a sort (ORDER BY) criteria to the query expression. - @param sortPattern the sort order template. For example to order by name ascending, time - descending: - { name : 1, ts : -1 } - i.e. - BSON( "name" << 1 << "ts" << -1 ) - or - fromjson(" name : 1, ts : -1 ") - */ - Query& sort(const BSONObj& sortPattern); - - /** Provide a hint to the query. - @param keyPattern Key pattern for the index to use. - Example: - hint("{ts:1}") - */ - Query& hint(BSONObj keyPattern); - - /** - * Sets the read preference for this query. - * - * @param pref the read preference mode for this query. - * @param tags the set of tags to use for this query. - */ - Query& readPref(ReadPreference pref, const BSONArray& tags); - - /** - * A temporary accessor that returns a reference to the internal BSON object. No new callers - * should be introduced! - * NB: must be implemented in the header because db/query/query_request cannot link against - * client/client_query. - */ - const BSONObj& getFullSettingsDeprecated() const { - return obj; - } - - /** - * The setters below were added to make the contents of the Query's settings internal BSON - * explicit. They will be reviewed and deprecated/removed as appropriate. - */ - Query& appendElements(BSONObj elements); - Query& requestResumeToken(bool enable); - Query& resumeAfter(BSONObj point); - Query& maxTimeMS(long long timeout); - Query& term(long long value); - Query& readConcern(BSONObj rc); - Query& readOnce(bool enable); - -private: - BSONObj obj; - - /** - * @return true if this query has an orderby, hint, or some other field - */ - bool isComplex(bool* hasDollar = nullptr) const; - - void makeComplex(); - template <class T> - void appendComplex(const char* fieldName, const T& val) { - makeComplex(); - BSONObjBuilder b(std::move(obj)); - b.append(fieldName, val); - obj = b.obj(); - } -}; - -inline std::ostream& operator<<(std::ostream& s, const Query& q) { - return s << q.getFullSettingsDeprecated().toString(); -} - /** * WARNING: This function exists only to support special code paths that use an OP_QUERY-style query * representation (even though the OP_QUERY wire protocol message itself is no longer supported). Do diff --git a/src/mongo/client/dbclient_rs.cpp b/src/mongo/client/dbclient_rs.cpp index 582ccf7b16d..a48fe50a2fa 100644 --- a/src/mongo/client/dbclient_rs.cpp +++ b/src/mongo/client/dbclient_rs.cpp @@ -85,42 +85,6 @@ public: */ const size_t MAX_RETRY = 3; -/** - * Extracts the read preference settings from the query document. Note that this method - * assumes that the query is ok for secondaries so it defaults to - * ReadPreference::SecondaryPreferred when nothing is specified. Supports the following - * format: - * - * Format A (official format): - * { query: <actual query>, $readPreference: <read pref obj> } - * - * Format B (unofficial internal format from mongos): - * { <actual query>, $queryOptions: { $readPreference: <read pref obj> }} - * - * @param query the raw query document - * - * @return the read preference setting if a read preference exists, otherwise the default read - * preference of Primary_Only. If the tags field was not present, it will contain one - * empty tag document {} which matches any tag. - * - * @throws AssertionException if the read preference object is malformed - */ -std::unique_ptr<ReadPreferenceSetting> _extractReadPref( - const client_deprecated::Query& querySettings, int queryOptions) { - // Default read pref is primary only or secondary preferred with secondaryOK - const auto defaultReadPref = queryOptions & QueryOption_SecondaryOk - ? ReadPreference::SecondaryPreferred - : ReadPreference::PrimaryOnly; - - BSONObj readPrefContainingObj = querySettings.getFullSettingsDeprecated(); - if (auto elem = readPrefContainingObj["$queryOptions"]) { - // The readPreference is embedded in the $queryOptions field. - readPrefContainingObj = elem.Obj(); - } - return std::make_unique<ReadPreferenceSetting>(uassertStatusOK( - ReadPreferenceSetting::fromContainingBSON(readPrefContainingObj, defaultReadPref))); -} - } // namespace // -------------------------------- @@ -734,70 +698,6 @@ void DBClientReplicaSet::say(Message& toSend, bool isRetry, string* actualServer if (!isRetry) _lastClient = nullptr; - const int lastOp = toSend.operation(); - - if (lastOp == dbQuery) { - // TODO: might be possible to do this faster by changing api - DbMessage dm(toSend); - QueryMessage qm(dm); - - shared_ptr<ReadPreferenceSetting> readPref(_extractReadPref( - client_deprecated::Query::fromBSONDeprecated(qm.query), qm.queryOptions)); - if (_isSecondaryQuery(qm.ns, qm.query, *readPref)) { - LOGV2_DEBUG(20141, - 3, - "dbclient_rs say using secondary or tagged node selection in {replicaSet}, " - "read pref is {readPref} " - "(primary : {primary}, lastTagged : {lastTagged})", - "dbclient_rs say 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; - } - - if (actualServer != nullptr) { - *actualServer = conn->getServerAddress(); - } - - conn->say(toSend); - - _lastClient = conn; - } catch (const DBException& ex) { - const Status status = - ex.toStatus(str::stream() << "can't callLazy replica set node " - << _lastSecondaryOkHost.toString()); - lastNodeErrMsg = status.reason(); - _invalidateLastSecondaryOkCache(status); - - continue; - } - - return; - } - - StringBuilder assertMsg; - assertMsg << "Failed to call say, no good nodes in " << _getMonitor()->getName(); - if (!lastNodeErrMsg.empty()) { - assertMsg << ", last error: " << lastNodeErrMsg; - } - - uasserted(16380, assertMsg.str()); - } - } - LOGV2_DEBUG(20142, 3, "dbclient_rs say to primary node in {replicaSet}", @@ -899,60 +799,6 @@ bool DBClientReplicaSet::call(Message& toSend, Message& response, bool assertOk, string* actualServer) { - const char* ns = nullptr; - - if (toSend.operation() == dbQuery) { - // TODO: might be possible to do this faster by changing api - DbMessage dm(toSend); - QueryMessage qm(dm); - ns = qm.ns; - - shared_ptr<ReadPreferenceSetting> readPref(_extractReadPref( - client_deprecated::Query::fromBSONDeprecated(qm.query), qm.queryOptions)); - if (_isSecondaryQuery(ns, qm.query, *readPref)) { - LOGV2_DEBUG( - 20145, - 3, - "dbclient_rs call using secondary or tagged node selection in {replicaSet}, " - "read pref is {readPref} " - "(primary : {primary}, lastTagged : {lastTagged})", - "dbclient_rs call 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]")); - - for (size_t retry = 0; retry < MAX_RETRY; retry++) { - try { - DBClientConnection* conn = selectNodeUsingTags(readPref); - - if (conn == nullptr) { - return false; - } - - if (actualServer != nullptr) { - *actualServer = conn->getServerAddress(); - } - - return conn->call(toSend, response, assertOk, nullptr); - } catch (const DBException& ex) { - if (actualServer) - *actualServer = ""; - - const Status status = ex.toStatus(); - _invalidateLastSecondaryOkCache(status.withContext( - str::stream() << "can't call replica set node " << _lastSecondaryOkHost)); - } - } - - // Was not able to successfully send after max retries - return false; - } - } - LOGV2_DEBUG(20146, 3, "dbclient_rs call to primary node in {replicaSet}", @@ -966,20 +812,6 @@ bool DBClientReplicaSet::call(Message& toSend, if (!m->call(toSend, response, assertOk, nullptr)) return false; - if (ns) { - QueryResult::View res = response.singleData().view2ptr(); - if (res.getNReturned() == 1) { - BSONObj x(res.data()); - if (str::contains(ns, "$cmd")) { - if (isNotPrimaryErrorString(x["errmsg"])) - isNotPrimary(); - } else { - if (isNotPrimaryErrorString(getErrField(x))) - isNotPrimary(); - } - } - } - return true; } diff --git a/src/mongo/db/query/query_request_helper.h b/src/mongo/db/query/query_request_helper.h index bfbfbca8ec1..4edad47e067 100644 --- a/src/mongo/db/query/query_request_helper.h +++ b/src/mongo/db/query/query_request_helper.h @@ -40,14 +40,13 @@ namespace mongo { -class QueryMessage; class Status; template <typename T> class StatusWith; /** - * Parses the QueryMessage or find command received from the user and makes the various fields - * more easily accessible. + * Parses the find command received from the user and makes the various fields more easily + * accessible. */ namespace query_request_helper { |