diff options
author | Irina Yatsenko <irina.yatsenko@mongodb.com> | 2021-08-14 23:11:21 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-08-14 23:54:17 +0000 |
commit | 1f64d42977db0572b08d7ab19133bc3f21323ce0 (patch) | |
tree | 6478091409d1eabc3ed836646fdad68bdc075ec6 /src/mongo/rpc | |
parent | 3e64baa99c9331619de112b3957657313e1d4100 (diff) | |
download | mongo-1f64d42977db0572b08d7ab19133bc3f21323ce0.tar.gz |
SERVER-58670 Modernize DBClientBase query interface to avoid OP_QUERY-derived characteristics
Diffstat (limited to 'src/mongo/rpc')
-rw-r--r-- | src/mongo/rpc/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/rpc/factory.cpp | 11 | ||||
-rw-r--r-- | src/mongo/rpc/factory.h | 10 | ||||
-rw-r--r-- | src/mongo/rpc/legacy_request_builder.cpp | 116 | ||||
-rw-r--r-- | src/mongo/rpc/legacy_request_builder.h | 41 | ||||
-rw-r--r-- | src/mongo/rpc/legacy_request_test.cpp | 105 |
6 files changed, 0 insertions, 285 deletions
diff --git a/src/mongo/rpc/SConscript b/src/mongo/rpc/SConscript index d553d875e48..d4cf4d5cac7 100644 --- a/src/mongo/rpc/SConscript +++ b/src/mongo/rpc/SConscript @@ -57,7 +57,6 @@ env.Library( 'legacy_reply.cpp', 'legacy_reply_builder.cpp', 'legacy_request.cpp', - 'legacy_request_builder.cpp', 'object_check.cpp', 'object_check.idl', 'reply_builder_interface.cpp', @@ -176,7 +175,6 @@ if wiredtiger: target='rpc_test', source=[ 'get_status_from_command_result_test.cpp', - 'legacy_request_test.cpp', 'metadata/client_metadata_test.cpp', 'metadata/egress_metadata_hook_list_test.cpp', 'metadata/oplog_query_metadata_test.cpp', diff --git a/src/mongo/rpc/factory.cpp b/src/mongo/rpc/factory.cpp index e7d7c2ec67b..8211fb03d39 100644 --- a/src/mongo/rpc/factory.cpp +++ b/src/mongo/rpc/factory.cpp @@ -36,7 +36,6 @@ #include "mongo/rpc/legacy_reply.h" #include "mongo/rpc/legacy_reply_builder.h" #include "mongo/rpc/legacy_request.h" -#include "mongo/rpc/legacy_request_builder.h" #include "mongo/rpc/message.h" #include "mongo/rpc/op_msg_rpc_impls.h" #include "mongo/rpc/protocol.h" @@ -46,16 +45,6 @@ namespace mongo { namespace rpc { -Message messageFromOpMsgRequest(Protocol proto, const OpMsgRequest& request) { - switch (proto) { - case Protocol::kOpMsg: - return request.serialize(); - case Protocol::kOpQuery: - return legacyRequestFromOpMsgRequest(request); - } - MONGO_UNREACHABLE; -} - std::unique_ptr<ReplyInterface> makeReply(const Message* unownedMessage) { switch (unownedMessage->operation()) { case mongo::dbMsg: diff --git a/src/mongo/rpc/factory.h b/src/mongo/rpc/factory.h index 54bdc029923..9aec1557f19 100644 --- a/src/mongo/rpc/factory.h +++ b/src/mongo/rpc/factory.h @@ -52,16 +52,6 @@ class ReplyInterface; std::unique_ptr<ReplyInterface> makeReply(const Message* unownedMessage); /** - * Serializes an OpMsgRequest for a server that speaks the requested protocol. - */ -Message messageFromOpMsgRequest(Protocol proto, const OpMsgRequest&); -inline Message messageFromOpMsgRequest(ProtocolSet clientProtos, - ProtocolSet serverProtos, - const OpMsgRequest& request) { - return messageFromOpMsgRequest(uassertStatusOK(negotiate(clientProtos, serverProtos)), request); -} - -/** * Parses the message (from any protocol) into an OpMsgRequest. */ OpMsgRequest opMsgRequestFromAnyProtocol(const Message& unownedMessage); diff --git a/src/mongo/rpc/legacy_request_builder.cpp b/src/mongo/rpc/legacy_request_builder.cpp deleted file mode 100644 index 66ed5eeb069..00000000000 --- a/src/mongo/rpc/legacy_request_builder.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the Server Side Public License in all respects for - * all of the code used other than as permitted herein. If you modify file(s) - * with this exception, you may extend this exception to your version of the - * file(s), but you are not obligated to do so. If you do not wish to do so, - * delete this exception statement from your version. If you delete this - * exception statement from all source files in the program, then also delete - * it in the license file. - */ - -#include "mongo/platform/basic.h" - -#include "mongo/rpc/legacy_request_builder.h" - -#include <memory> -#include <tuple> -#include <utility> - -#include "mongo/client/read_preference.h" -#include "mongo/db/dbmessage.h" -#include "mongo/db/namespace_string.h" -#include "mongo/rpc/message.h" -#include "mongo/rpc/metadata.h" -#include "mongo/util/assert_util.h" - -namespace mongo { -namespace rpc { - -namespace { -void mergeInDocumentSequences(const OpMsgRequest& request, BSONObjBuilder* body) { - for (auto&& seq : request.sequences) { - invariant(seq.name.find('.') == std::string::npos); // Only support top-level for now. - dassert(!body->asTempObj().hasField(seq.name)); - body->append(seq.name, seq.objs); - } -} - -/** - * Given a command request, attempts to construct a legacy command - * object and query flags bitfield augmented with the given metadata. - */ -BSONObj downconvertRequestBody(const OpMsgRequest& request, int* queryOptions) { - *queryOptions = 0; - - if (auto readPref = request.body["$readPreference"]) { - auto parsed = ReadPreferenceSetting::fromInnerBSON(readPref); - if (parsed.isOK() && parsed.getValue().canRunOnSecondary()) { - *queryOptions |= QueryOption_SecondaryOk; - } - - BSONObjBuilder outer; - { - BSONObjBuilder inner(outer.subobjStart("$query")); - for (auto field : request.body) { - const auto name = field.fieldNameStringData(); - if (name == "$readPreference" || name == "$db") { - // skip field. - } else { - inner.append(field); - } - } - mergeInDocumentSequences(request, &inner); - } - outer.append(readPref); - return outer.obj(); - } else { - BSONObjBuilder body(request.body.removeField("$db")); - mergeInDocumentSequences(request, &body); - return body.obj(); - } -} -} // namespace - -Message legacyRequestFromOpMsgRequest(const OpMsgRequest& request) { - BufBuilder builder; - builder.skip(mongo::MsgData::MsgDataHeaderSize); - - const auto cmdNS = NamespaceString(request.getDatabase(), "").getCommandNS().toString(); - - int queryOptions; - const auto downconvertedBody = downconvertRequestBody(request, &queryOptions); - - builder.appendNum(queryOptions); - builder.appendStr(cmdNS); - builder.appendNum(0); // nToSkip - builder.appendNum(1); // nToReturn - - downconvertedBody.appendSelfToBufBuilder(builder); - - MsgData::View msg = builder.buf(); - msg.setLen(builder.len()); - msg.setOperation(dbQuery); - return Message(builder.release()); -} - -} // namespace rpc -} // namespace mongo diff --git a/src/mongo/rpc/legacy_request_builder.h b/src/mongo/rpc/legacy_request_builder.h deleted file mode 100644 index 2696bb6e6fe..00000000000 --- a/src/mongo/rpc/legacy_request_builder.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the Server Side Public License in all respects for - * all of the code used other than as permitted herein. If you modify file(s) - * with this exception, you may extend this exception to your version of the - * file(s), but you are not obligated to do so. If you do not wish to do so, - * delete this exception statement from your version. If you delete this - * exception statement from all source files in the program, then also delete - * it in the license file. - */ - -#pragma once - -#include "mongo/rpc/message.h" -#include "mongo/rpc/op_msg.h" - -namespace mongo { -namespace rpc { - -Message legacyRequestFromOpMsgRequest(const OpMsgRequest& request); - -} // namespace rpc -} // namespace mongo diff --git a/src/mongo/rpc/legacy_request_test.cpp b/src/mongo/rpc/legacy_request_test.cpp deleted file mode 100644 index ac8fa3a4c2f..00000000000 --- a/src/mongo/rpc/legacy_request_test.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/** - * Copyright (C) 2018-present MongoDB, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the Server Side Public License, version 1, - * as published by MongoDB, Inc. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * Server Side Public License for more details. - * - * You should have received a copy of the Server Side Public License - * along with this program. If not, see - * <http://www.mongodb.com/licensing/server-side-public-license>. - * - * As a special exception, the copyright holders give permission to link the - * code of portions of this program with the OpenSSL library under certain - * conditions as described in each individual source file and distribute - * linked combinations including the program with the OpenSSL library. You - * must comply with the Server Side Public License in all respects for - * all of the code used other than as permitted herein. If you modify file(s) - * with this exception, you may extend this exception to your version of the - * file(s), but you are not obligated to do so. If you do not wish to do so, - * delete this exception statement from your version. If you delete this - * exception statement from all source files in the program, then also delete - * it in the license file. - */ - -#include "mongo/platform/basic.h" - -#include "mongo/bson/json.h" -#include "mongo/db/dbmessage.h" -#include "mongo/db/jsobj.h" -#include "mongo/rpc/legacy_request.h" -#include "mongo/rpc/legacy_request_builder.h" -#include "mongo/unittest/unittest.h" - -namespace { - -using namespace mongo; - -TEST(LegacyRequest, RoundTrip) { - auto databaseName = "barbaz"; - auto commandName = "foobar"; - - BSONObjBuilder metadataBob{}; - metadataBob.append("$replData", BSONObj()); - auto metadata = metadataBob.done(); - - BSONObjBuilder commandArgsBob{}; - commandArgsBob.append(commandName, "baz"); - auto commandArgs = commandArgsBob.done(); - - auto request = OpMsgRequest::fromDBAndBody(databaseName, commandArgs, metadata); - request.sequences.push_back({"sequence", {BSON("a" << 1), BSON("b" << 2)}}); - auto msg = rpc::legacyRequestFromOpMsgRequest(request); - - auto metadataAndSequece = BSONObjBuilder(metadata) - .append("sequence", BSON_ARRAY(BSON("a" << 1) << BSON("b" << 2))) - .obj(); - - auto parsed = rpc::opMsgRequestFromLegacyRequest(msg); - ASSERT_BSONOBJ_EQ( - parsed.body, - OpMsgRequest::fromDBAndBody(databaseName, commandArgs, metadataAndSequece).body); -} - -TEST(LegacyRequestBuilder, DownconvertSecondaryReadPreference) { - auto readPref = BSON("mode" - << "secondary"); - auto msg = rpc::legacyRequestFromOpMsgRequest( - OpMsgRequest::fromDBAndBody("admin", BSON("ping" << 1 << "$readPreference" << readPref))); - auto parsed = QueryMessage(msg); - - ASSERT_EQ(parsed.ns, "admin.$cmd"_sd); - ASSERT_EQ(parsed.queryOptions, QueryOption_SecondaryOk); - ASSERT_BSONOBJ_EQ(parsed.query, - fromjson("{$query: {ping: 1}, $readPreference : {mode: 'secondary'}}")); -} - -TEST(CommandRequestBuilder, DownconvertExplicitPrimaryReadPreference) { - auto readPref = BSON("mode" - << "primary"); - auto msg = rpc::legacyRequestFromOpMsgRequest( - OpMsgRequest::fromDBAndBody("admin", BSON("ping" << 1 << "$readPreference" << readPref))); - auto parsed = QueryMessage(msg); - - ASSERT_EQ(parsed.ns, "admin.$cmd"_sd); - ASSERT_EQ(parsed.queryOptions, 0); - ASSERT_BSONOBJ_EQ(parsed.query, - fromjson("{$query: {ping: 1}, $readPreference : {mode: 'primary'}}")); -} - -TEST(CommandRequestBuilder, DownconvertImplicitPrimaryReadPreference) { - auto msg = - rpc::legacyRequestFromOpMsgRequest(OpMsgRequest::fromDBAndBody("admin", BSON("ping" << 1))); - auto parsed = QueryMessage(msg); - - ASSERT_EQ(parsed.ns, "admin.$cmd"_sd); - ASSERT_EQ(parsed.queryOptions, 0); - ASSERT_BSONOBJ_EQ(parsed.query, fromjson("{ping: 1}")); -} - -} // namespace |