diff options
Diffstat (limited to 'src/mongo/s')
-rw-r--r-- | src/mongo/s/commands/SConscript | 4 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_abort_transaction_cmd.h (renamed from src/mongo/s/commands/cluster_abort_transaction_cmd.cpp) | 45 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_abort_transaction_cmd_s.cpp | 56 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_commit_transaction_cmd.h (renamed from src/mongo/s/commands/cluster_commit_transaction_cmd.cpp) | 34 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_commit_transaction_cmd_s.cpp | 56 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_find_cmd.h | 4 | ||||
-rw-r--r-- | src/mongo/s/commands/cluster_find_cmd_s.cpp | 4 |
7 files changed, 165 insertions, 38 deletions
diff --git a/src/mongo/s/commands/SConscript b/src/mongo/s/commands/SConscript index b394655d563..43f5aa29602 100644 --- a/src/mongo/s/commands/SConscript +++ b/src/mongo/s/commands/SConscript @@ -24,7 +24,7 @@ env.Library( target='cluster_commands', source=[ 'cluster_abort_reshard_collection_cmd.cpp', - 'cluster_abort_transaction_cmd.cpp', + 'cluster_abort_transaction_cmd_s.cpp', 'cluster_available_query_options_cmd.cpp', 'cluster_build_info.cpp', 'cluster_change_stream_options_command.cpp', @@ -32,7 +32,7 @@ env.Library( 'cluster_coll_stats_cmd.cpp', 'cluster_collection_mod_cmd.cpp', 'cluster_commit_reshard_collection_cmd.cpp', - 'cluster_commit_transaction_cmd.cpp', + 'cluster_commit_transaction_cmd_s.cpp', 'cluster_compact_cmd.cpp', 'cluster_convert_to_capped_cmd.cpp', 'cluster_count_cmd.cpp', diff --git a/src/mongo/s/commands/cluster_abort_transaction_cmd.cpp b/src/mongo/s/commands/cluster_abort_transaction_cmd.h index b2581fc59eb..7b7b738fdaa 100644 --- a/src/mongo/s/commands/cluster_abort_transaction_cmd.cpp +++ b/src/mongo/s/commands/cluster_abort_transaction_cmd.h @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018-present MongoDB, Inc. + * Copyright (C) 2022-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, @@ -27,9 +27,7 @@ * it in the license file. */ -#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding - -#include "mongo/platform/basic.h" +#pragma once #include "mongo/db/commands.h" #include "mongo/db/commands/txn_cmds_gen.h" @@ -41,35 +39,36 @@ namespace mongo { namespace { -static const Status kOnlyTransactionsReadConcernsSupported{ - ErrorCodes::InvalidOptions, "only read concerns valid in transactions are supported"}; -static const Status kDefaultReadConcernNotPermitted{ErrorCodes::InvalidOptions, - "default read concern not permitted"}; - /** - * Implements the abortTransaction command on mongos. + * Implements the abortTransaction command for a router. */ -class ClusterAbortTransactionCmd - : public BasicCommandWithRequestParser<ClusterAbortTransactionCmd> { +template <typename Impl> +class ClusterAbortTransactionCmdBase + : public BasicCommandWithRequestParser<ClusterAbortTransactionCmdBase<Impl>> { public: - using BasicCommandWithRequestParser::BasicCommandWithRequestParser; + ClusterAbortTransactionCmdBase() + : BasicCommandWithRequestParser<ClusterAbortTransactionCmdBase<Impl>>(Impl::kName) {} + using Request = AbortTransaction; using Reply = OkReply; + using BaseType = BasicCommandWithRequestParser<ClusterAbortTransactionCmdBase<Impl>>; + using RequestParser = + typename BasicCommandWithRequestParser<ClusterAbortTransactionCmdBase<Impl>>::RequestParser; void validateResult(const BSONObj& resultObj) final { auto ctx = IDLParserErrorContext("AbortReply"); - if (!checkIsErrorStatus(resultObj, ctx)) { + if (!BaseType::checkIsErrorStatus(resultObj, ctx)) { // Will throw if the result doesn't match the abortReply. Reply::parse(ctx, resultObj); } } const std::set<std::string>& apiVersions() const { - return kApiVersions1; + return Impl::getApiVersions(); } - AllowedOnSecondary secondaryAllowed(ServiceContext*) const override { - return AllowedOnSecondary::kAlways; + BasicCommand::AllowedOnSecondary secondaryAllowed(ServiceContext*) const override { + return BasicCommand::AllowedOnSecondary::kAlways; } bool adminOnly() const override { @@ -83,6 +82,11 @@ public: ReadConcernSupportResult supportsReadConcern(const BSONObj& cmdObj, repl::ReadConcernLevel level, bool isImplicitDefault) const override { + static const Status kOnlyTransactionsReadConcernsSupported{ + ErrorCodes::InvalidOptions, "only read concerns valid in transactions are supported"}; + static const Status kDefaultReadConcernNotPermitted{ErrorCodes::InvalidOptions, + "default read concern not permitted"}; + // abortTransaction commences running inside a transaction (even though the transaction will // be ended by the time it completes). Therefore it needs to accept any readConcern which // is valid within a transaction. However it is not appropriate to apply the default @@ -100,7 +104,7 @@ public: Status checkAuthForOperation(OperationContext* opCtx, const std::string& dbname, const BSONObj& cmdObj) const override { - return Status::OK(); + return Impl::checkAuthForOperation(opCtx); } bool runWithRequestParser(OperationContext* opCtx, @@ -108,6 +112,8 @@ public: const BSONObj& cmdObj, const RequestParser& requestParser, BSONObjBuilder& result) final { + Impl::checkCanRunHere(opCtx); + auto txnRouter = TransactionRouter::get(opCtx); uassert(ErrorCodes::InvalidOptions, "abortTransaction can only be run within a session", @@ -121,8 +127,7 @@ public: const AuthorizationContract* getAuthorizationContract() const final { return &::mongo::AbortTransaction::kAuthorizationContract; } - -} clusterAbortTransactionCmd; +}; } // namespace } // namespace mongo diff --git a/src/mongo/s/commands/cluster_abort_transaction_cmd_s.cpp b/src/mongo/s/commands/cluster_abort_transaction_cmd_s.cpp new file mode 100644 index 00000000000..cf9aa7e5e57 --- /dev/null +++ b/src/mongo/s/commands/cluster_abort_transaction_cmd_s.cpp @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2022-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/s/commands/cluster_abort_transaction_cmd.h" + +namespace mongo { +namespace { + +/** + * Implements the cluster abortTransaction command on mongos. + */ +struct ClusterAbortTransactionCmdS { + static constexpr StringData kName = "abortTransaction"_sd; + + static const std::set<std::string>& getApiVersions() { + return kApiVersions1; + } + + static Status checkAuthForOperation(OperationContext* opCtx) { + return Status::OK(); + } + + static void checkCanRunHere(OperationContext* opCtx) { + // Can always run on a mongos. + } +}; +ClusterAbortTransactionCmdBase<ClusterAbortTransactionCmdS> clusterAbortTransactionS; + +} // namespace +} // namespace mongo diff --git a/src/mongo/s/commands/cluster_commit_transaction_cmd.cpp b/src/mongo/s/commands/cluster_commit_transaction_cmd.h index f1b22e4c0b4..975ced1752a 100644 --- a/src/mongo/s/commands/cluster_commit_transaction_cmd.cpp +++ b/src/mongo/s/commands/cluster_commit_transaction_cmd.h @@ -1,5 +1,5 @@ /** - * Copyright (C) 2018-present MongoDB, Inc. + * Copyright (C) 2022-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, @@ -27,9 +27,7 @@ * it in the license file. */ -#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kSharding - -#include "mongo/platform/basic.h" +#pragma once #include "mongo/db/commands.h" #include "mongo/db/commands/txn_cmds_gen.h" @@ -41,29 +39,35 @@ namespace mongo { namespace { /** - * Implements the commitTransaction command on mongos. + * Implements the commitTransaction command for a router. */ -class ClusterCommitTransactionCmd - : public BasicCommandWithRequestParser<ClusterCommitTransactionCmd> { +template <typename Impl> +class ClusterCommitTransactionCmdBase + : public BasicCommandWithRequestParser<ClusterCommitTransactionCmdBase<Impl>> { public: - using BasicCommandWithRequestParser::BasicCommandWithRequestParser; + ClusterCommitTransactionCmdBase() + : BasicCommandWithRequestParser<ClusterCommitTransactionCmdBase<Impl>>(Impl::kName) {} + using Request = CommitTransaction; using Reply = OkReply; + using BaseType = BasicCommandWithRequestParser<ClusterCommitTransactionCmdBase<Impl>>; + using RequestParser = typename BasicCommandWithRequestParser< + ClusterCommitTransactionCmdBase<Impl>>::RequestParser; void validateResult(const BSONObj& resultObj) final { auto ctx = IDLParserErrorContext("CommitReply"); - if (!checkIsErrorStatus(resultObj, ctx)) { + if (!BaseType::checkIsErrorStatus(resultObj, ctx)) { // Will throw if the result doesn't match the commitReply. Reply::parse(ctx, resultObj); } } const std::set<std::string>& apiVersions() const { - return kApiVersions1; + return Impl::getApiVersions(); } - AllowedOnSecondary secondaryAllowed(ServiceContext*) const override { - return AllowedOnSecondary::kAlways; + BasicCommand::AllowedOnSecondary secondaryAllowed(ServiceContext*) const override { + return BasicCommand::AllowedOnSecondary::kAlways; } bool adminOnly() const override { @@ -81,7 +85,7 @@ public: Status checkAuthForOperation(OperationContext* opCtx, const std::string& dbname, const BSONObj& cmdObj) const override { - return Status::OK(); + return Impl::checkAuthForOperation(opCtx); } bool runWithRequestParser(OperationContext* opCtx, @@ -89,6 +93,8 @@ public: const BSONObj& cmdObj, const RequestParser& requestParser, BSONObjBuilder& result) final { + Impl::checkCanRunHere(opCtx); + auto txnRouter = TransactionRouter::get(opCtx); uassert(ErrorCodes::InvalidOptions, "commitTransaction can only be run within a session", @@ -103,7 +109,7 @@ public: const AuthorizationContract* getAuthorizationContract() const final { return &::mongo::CommitTransaction::kAuthorizationContract; } -} clusterCommitTransactionCmd; +}; } // namespace } // namespace mongo diff --git a/src/mongo/s/commands/cluster_commit_transaction_cmd_s.cpp b/src/mongo/s/commands/cluster_commit_transaction_cmd_s.cpp new file mode 100644 index 00000000000..ac15c39f699 --- /dev/null +++ b/src/mongo/s/commands/cluster_commit_transaction_cmd_s.cpp @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2022-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/s/commands/cluster_commit_transaction_cmd.h" + +namespace mongo { +namespace { + +/** + * Implements the cluster commitTransaction command on mongos. + */ +struct ClusterCommitTransactionCmdS { + static constexpr StringData kName = "commitTransaction"_sd; + + static const std::set<std::string>& getApiVersions() { + return kApiVersions1; + } + + static Status checkAuthForOperation(OperationContext* opCtx) { + return Status::OK(); + } + + static void checkCanRunHere(OperationContext* opCtx) { + // Can always run on a mongos. + } +}; +ClusterCommitTransactionCmdBase<ClusterCommitTransactionCmdS> clusterCommitTransactionS; + +} // namespace +} // namespace mongo diff --git a/src/mongo/s/commands/cluster_find_cmd.h b/src/mongo/s/commands/cluster_find_cmd.h index 7c716729e6c..6be39d411ab 100644 --- a/src/mongo/s/commands/cluster_find_cmd.h +++ b/src/mongo/s/commands/cluster_find_cmd.h @@ -51,7 +51,7 @@ namespace mongo { /** - * Implements the find command on mongos. + * Implements the find command for a router. */ template <typename Impl> class ClusterFindCmdBase final : public Command { @@ -194,7 +194,7 @@ public: // We count find command as a query op. globalOpCounters.gotQuery(); - Grid::get(opCtx)->assertShardingIsInitialized(); + Impl::checkCanRunHere(opCtx); ON_BLOCK_EXIT([opCtx] { Grid::get(opCtx)->catalogCache()->checkAndRecordOperationBlockedByRefresh( diff --git a/src/mongo/s/commands/cluster_find_cmd_s.cpp b/src/mongo/s/commands/cluster_find_cmd_s.cpp index 03402dc3c30..793d14999be 100644 --- a/src/mongo/s/commands/cluster_find_cmd_s.cpp +++ b/src/mongo/s/commands/cluster_find_cmd_s.cpp @@ -48,6 +48,10 @@ struct ClusterFindCmdS { uassertStatusOK( auth::checkAuthForFind(AuthorizationSession::get(opCtx->getClient()), nss, hasTerm)); } + + static void checkCanRunHere(OperationContext* opCtx) { + // Can always run on a mongos. + } }; ClusterFindCmdBase<ClusterFindCmdS> clusterFindCmdS; |