diff options
author | Andrew Morrow <acm@mongodb.com> | 2016-03-13 15:22:43 -0400 |
---|---|---|
committer | Andrew Morrow <acm@mongodb.com> | 2016-03-21 22:55:32 -0400 |
commit | 6877d732e6d5739f9526d89d8e1360c08672eda7 (patch) | |
tree | 83552e1461ce9d62f667e2a0c81ca8350361d565 | |
parent | d5dce08c36ab4a928c5345ee18c57327234940f1 (diff) | |
download | mongo-6877d732e6d5739f9526d89d8e1360c08672eda7.tar.gz |
SERVER-23103 Make libcommand not depend on libcurop
-rw-r--r-- | src/mongo/client/SConscript | 2 | ||||
-rw-r--r-- | src/mongo/db/SConscript | 33 | ||||
-rw-r--r-- | src/mongo/db/commands.cpp | 37 | ||||
-rw-r--r-- | src/mongo/db/commands.h | 4 | ||||
-rw-r--r-- | src/mongo/db/commands_test_crutch.cpp | 50 | ||||
-rw-r--r-- | src/mongo/db/instance.cpp | 1 | ||||
-rw-r--r-- | src/mongo/db/repl/SConscript | 48 | ||||
-rw-r--r-- | src/mongo/db/run_commands.cpp | 80 | ||||
-rw-r--r-- | src/mongo/db/run_commands.h | 43 |
9 files changed, 249 insertions, 49 deletions
diff --git a/src/mongo/client/SConscript b/src/mongo/client/SConscript index bfd559b7ed4..3fa12078251 100644 --- a/src/mongo/client/SConscript +++ b/src/mongo/client/SConscript @@ -266,6 +266,8 @@ env.CppUnitTest( source='fetcher_test.cpp', LIBDEPS=[ 'fetcher', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', '$BUILD_DIR/mongo/executor/thread_pool_task_executor_test_fixture', ], ) diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index c7ae1d3ac1c..ce10e87b829 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -151,6 +151,7 @@ env.Library( ], LIBDEPS=[ '$BUILD_DIR/mongo/bson/mutable/mutable_bson', + '$BUILD_DIR/mongo/db/commands', '$BUILD_DIR/mongo/db/concurrency/lock_manager', '$BUILD_DIR/mongo/db/service_context', '$BUILD_DIR/mongo/db/query/command_request_response', @@ -166,7 +167,9 @@ env.CppUnitTest( 'curop_test.cpp', ], LIBDEPS=[ + 'auth/authorization_manager_mock_init', 'curop', + 'commands_test_crutch', ], ) @@ -373,6 +376,8 @@ env.CppUnitTest( ], LIBDEPS=[ '$BUILD_DIR/mongo/db/repl/replmocks', + 'auth/authorization_manager_mock_init', + 'commands_test_crutch', 'common', 'range_deleter', ], @@ -489,7 +494,6 @@ env.Library( 'auth/authorization_manager_global', 'commands/server_status_core', 'commands/test_commands_enabled', - 'curop', 'service_context', ], LIBDEPS_TAGS=[ @@ -499,6 +503,32 @@ env.Library( ], ) +env.Library( + target="commands_test_crutch", + source=[ + "commands_test_crutch.cpp", + ], + LIBDEPS=[ + "commands", + ], +) + +env.Library( + target='run_commands', + source=[ + 'run_commands.cpp', + ], + LIBDEPS=[ + "commands", + "curop", + ], + LIBDEPS_TAGS=[ + # Dependency on Command::execCommand, which is not uniquely + # defined. + 'incomplete', + ], +) + # mongod files - also files used in tools. present in dbtests, but not in mongos and not in client # libs. serverOnlyFiles = [ @@ -678,6 +708,7 @@ serveronlyLibdeps = [ "repl/rslog", "repl/sync_tail", "repl/topology_coordinator_impl", + "run_commands", "s/commands", "s/metadata", "s/sharding", diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp index 69e6011d8a0..8590039041d 100644 --- a/src/mongo/db/commands.cpp +++ b/src/mongo/db/commands.cpp @@ -461,41 +461,4 @@ void Command::generateErrorResponse(OperationContext* txn, _generateErrorResponse(txn, replyBuilder, exception, rpc::makeEmptyMetadata()); } -void runCommands(OperationContext* txn, - const rpc::RequestInterface& request, - rpc::ReplyBuilderInterface* replyBuilder) { - try { - dassert(replyBuilder->getState() == rpc::ReplyBuilderInterface::State::kCommandReply); - - Command* c = nullptr; - // In the absence of a Command object, no redaction is possible. Therefore - // to avoid displaying potentially sensitive information in the logs, - // we restrict the log message to the name of the unrecognized command. - // However, the complete command object will still be echoed to the client. - if (!(c = Command::findCommand(request.getCommandName()))) { - Command::unknownCommands.increment(); - std::string msg = str::stream() << "no such command: '" << request.getCommandName() - << "'"; - LOG(2) << msg; - uasserted(ErrorCodes::CommandNotFound, - str::stream() << msg << ", bad cmd: '" << request.getCommandArgs() << "'"); - } - - LOG(2) << "run command " << request.getDatabase() << ".$cmd" << ' ' - << c->getRedactedCopyForLogging(request.getCommandArgs()); - - { - // Try to set this as early as possible, as soon as we have figured out the command. - stdx::lock_guard<Client> lk(*txn->getClient()); - CurOp::get(txn)->setLogicalOp_inlock(c->getLogicalOp()); - } - - Command::execCommand(txn, c, request, replyBuilder); - } - - catch (const DBException& ex) { - Command::generateErrorResponse(txn, replyBuilder, ex, request); - } -} - } // namespace mongo diff --git a/src/mongo/db/commands.h b/src/mongo/db/commands.h index 19fadaeeee3..1112094db7a 100644 --- a/src/mongo/db/commands.h +++ b/src/mongo/db/commands.h @@ -455,8 +455,4 @@ private: const BSONObj& cmdObj); }; -void runCommands(OperationContext* txn, - const rpc::RequestInterface& request, - rpc::ReplyBuilderInterface* replyBuilder); - } // namespace mongo diff --git a/src/mongo/db/commands_test_crutch.cpp b/src/mongo/db/commands_test_crutch.cpp new file mode 100644 index 00000000000..de083acb447 --- /dev/null +++ b/src/mongo/db/commands_test_crutch.cpp @@ -0,0 +1,50 @@ +/** + * Copyright (C) 2016 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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. + */ + +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kCommand + +#include "mongo/platform/basic.h" + +#include "mongo/db/commands.h" + +#include "mongo/util/assert_util.h" + +namespace mongo { + +void Command::execCommand(OperationContext*, + Command*, + const rpc::RequestInterface&, + rpc::ReplyBuilderInterface*) { + invariant(false); +} + +void Command::registerError(OperationContext*, const DBException&) { + invariant(false); +} + +} // namespace mongo diff --git a/src/mongo/db/instance.cpp b/src/mongo/db/instance.cpp index 59f13ce1957..9238fd5a48a 100644 --- a/src/mongo/db/instance.cpp +++ b/src/mongo/db/instance.cpp @@ -79,6 +79,7 @@ #include "mongo/db/repl/oplog.h" #include "mongo/db/repl/repl_client_info.h" #include "mongo/db/repl/replication_coordinator_global.h" +#include "mongo/db/run_commands.h" #include "mongo/db/s/operation_sharding_state.h" #include "mongo/db/s/sharded_connection_info.h" #include "mongo/db/s/sharding_state.h" diff --git a/src/mongo/db/repl/SConscript b/src/mongo/db/repl/SConscript index a64778ca1a2..35a69b22919 100644 --- a/src/mongo/db/repl/SConscript +++ b/src/mongo/db/repl/SConscript @@ -90,6 +90,7 @@ env.Library( LIBDEPS=[ 'replication_executor', 'replmocks', + '$BUILD_DIR/mongo/db/commands_test_crutch', '$BUILD_DIR/mongo/executor/task_executor_test_fixture', ], ) @@ -101,6 +102,7 @@ env.CppUnitTest( ], LIBDEPS=[ 'replication_executor_test_fixture', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', '$BUILD_DIR/mongo/executor/task_executor_test_fixture', '$BUILD_DIR/mongo/unittest/concurrency', ], @@ -284,6 +286,7 @@ env.CppUnitTest('topology_coordinator_impl_test', 'topology_coordinator_impl_test.cpp', LIBDEPS=['topology_coordinator_impl', 'replica_set_messages', + '$BUILD_DIR/mongo/db/commands_test_crutch', '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', ]) @@ -291,6 +294,7 @@ env.CppUnitTest('topology_coordinator_impl_v1_test', 'topology_coordinator_impl_v1_test.cpp', LIBDEPS=['topology_coordinator_impl', 'replica_set_messages', + '$BUILD_DIR/mongo/db/commands_test_crutch', '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', ]) @@ -335,6 +339,7 @@ env.Library('repl_coordinator_test_fixture', LIBDEPS=[ '$BUILD_DIR/mongo/unittest/unittest', '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', 'repl_coordinator_impl', 'replmocks', 'topology_coordinator_impl',\ @@ -355,13 +360,18 @@ env.CppUnitTest('replica_set_config_checks_test', 'replica_set_config_checks_test.cpp', LIBDEPS=[ 'repl_coordinator_impl', - 'replmocks' + 'replmocks', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', ]) env.CppUnitTest('scatter_gather_test', 'scatter_gather_test.cpp', LIBDEPS=['repl_coordinator_impl', - 'replmocks']) + 'replmocks', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', + ]) env.CppUnitTest('check_quorum_for_config_change_test', 'check_quorum_for_config_change_test.cpp', @@ -369,35 +379,51 @@ env.CppUnitTest('check_quorum_for_config_change_test', 'repl_coordinator_impl', 'replication_executor', 'replmocks', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', ]) env.CppUnitTest('freshness_checker_test', 'freshness_checker_test.cpp', LIBDEPS=['repl_coordinator_impl', 'replica_set_messages', - 'replmocks']) + 'replmocks', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', + ]) env.CppUnitTest('vote_requester_test', 'vote_requester_test.cpp', LIBDEPS=['repl_coordinator_impl', 'replica_set_messages', - 'replmocks']) + 'replmocks', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', + ]) env.CppUnitTest('election_winner_declarer_test', 'election_winner_declarer_test.cpp', LIBDEPS=['repl_coordinator_impl', 'replica_set_messages', - 'replmocks']) + 'replmocks', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', + ]) env.CppUnitTest('elect_cmd_runner_test', 'elect_cmd_runner_test.cpp', LIBDEPS=['repl_coordinator_impl', 'replica_set_messages', - 'replmocks']) + 'replmocks', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', + ]) env.CppUnitTest('replication_coordinator_impl_elect_test', 'replication_coordinator_impl_elect_test.cpp', - LIBDEPS=['repl_coordinator_test_fixture']) + LIBDEPS=[ + 'repl_coordinator_test_fixture', + ]) env.CppUnitTest('replication_coordinator_impl_elect_v1_test', 'replication_coordinator_impl_elect_v1_test.cpp', @@ -548,6 +574,8 @@ env.CppUnitTest( source='reporter_test.cpp', LIBDEPS=[ 'reporter', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', '$BUILD_DIR/mongo/executor/thread_pool_task_executor_test_fixture', '$BUILD_DIR/mongo/unittest/task_executor_proxy', ], @@ -580,6 +608,7 @@ env.CppUnitTest( target='collection_cloner_test', source='collection_cloner_test.cpp', LIBDEPS=[ + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', 'collection_cloner', 'base_cloner_test_fixture', ], @@ -599,6 +628,7 @@ env.CppUnitTest( target='database_cloner_test', source='database_cloner_test.cpp', LIBDEPS=[ + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', 'database_cloner', 'base_cloner_test_fixture', ], @@ -655,6 +685,8 @@ env.CppUnitTest( 'database_task', 'replmocks', 'task_runner_test_fixture', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', + '$BUILD_DIR/mongo/db/commands_test_crutch', ], ) @@ -705,6 +737,7 @@ env.CppUnitTest( 'applier', 'replication_executor_test_fixture', '$BUILD_DIR/mongo/unittest/concurrency', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', ], ) @@ -732,6 +765,7 @@ env.CppUnitTest( 'base_cloner_test_fixture', 'data_replicator', 'replication_executor_test_fixture', + '$BUILD_DIR/mongo/db/auth/authorization_manager_mock_init', '$BUILD_DIR/mongo/unittest/concurrency', ], ) diff --git a/src/mongo/db/run_commands.cpp b/src/mongo/db/run_commands.cpp new file mode 100644 index 00000000000..108ff103696 --- /dev/null +++ b/src/mongo/db/run_commands.cpp @@ -0,0 +1,80 @@ +/* Copyright 2016 10gen Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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. + */ + +#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kCommand + +#include "mongo/platform/basic.h" + +#include "mongo/db/run_commands.h" + +#include "mongo/db/client.h" +#include "mongo/db/commands.h" +#include "mongo/db/curop.h" +#include "mongo/rpc/reply_builder_interface.h" +#include "mongo/rpc/request_interface.h" +#include "mongo/util/log.h" + +namespace mongo { + +void runCommands(OperationContext* txn, + const rpc::RequestInterface& request, + rpc::ReplyBuilderInterface* replyBuilder) { + try { + dassert(replyBuilder->getState() == rpc::ReplyBuilderInterface::State::kCommandReply); + + Command* c = nullptr; + // In the absence of a Command object, no redaction is possible. Therefore + // to avoid displaying potentially sensitive information in the logs, + // we restrict the log message to the name of the unrecognized command. + // However, the complete command object will still be echoed to the client. + if (!(c = Command::findCommand(request.getCommandName()))) { + Command::unknownCommands.increment(); + std::string msg = str::stream() << "no such command: '" << request.getCommandName() + << "'"; + LOG(2) << msg; + uasserted(ErrorCodes::CommandNotFound, + str::stream() << msg << ", bad cmd: '" << request.getCommandArgs() << "'"); + } + + LOG(2) << "run command " << request.getDatabase() << ".$cmd" << ' ' + << c->getRedactedCopyForLogging(request.getCommandArgs()); + + { + // Try to set this as early as possible, as soon as we have figured out the command. + stdx::lock_guard<Client> lk(*txn->getClient()); + CurOp::get(txn)->setLogicalOp_inlock(c->getLogicalOp()); + } + + Command::execCommand(txn, c, request, replyBuilder); + } + + catch (const DBException& ex) { + Command::generateErrorResponse(txn, replyBuilder, ex, request); + } +} + +} // namespace mongo diff --git a/src/mongo/db/run_commands.h b/src/mongo/db/run_commands.h new file mode 100644 index 00000000000..c0c48e0dfa0 --- /dev/null +++ b/src/mongo/db/run_commands.h @@ -0,0 +1,43 @@ +/* Copyright 2016 10gen Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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 + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * 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 GNU Affero General 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 + +namespace mongo { + +class OperationContext; + +namespace rpc { +class ReplyBuilderInterface; +class RequestInterface; +} // namespace rpc + +void runCommands(OperationContext* txn, + const rpc::RequestInterface& request, + rpc::ReplyBuilderInterface* replyBuilder); + +} // namespace mongo |