diff options
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/commands.cpp | 13 | ||||
-rw-r--r-- | src/mongo/db/commands_in_multi_doc_txn_params.idl | 37 | ||||
-rw-r--r-- | src/mongo/db/ops/write_ops_exec.cpp | 7 |
4 files changed, 56 insertions, 2 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index 131cfc1a228..f93d37a0f6f 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -514,6 +514,7 @@ env.Library( target="commands", source=[ 'commands.cpp', + env.Idlc('commands_in_multi_doc_txn_params.idl')[0], ], LIBDEPS=[ '$BUILD_DIR/mongo/base', diff --git a/src/mongo/db/commands.cpp b/src/mongo/db/commands.cpp index 142037458bb..b2187a0e697 100644 --- a/src/mongo/db/commands.cpp +++ b/src/mongo/db/commands.cpp @@ -49,6 +49,7 @@ #include "mongo/db/client.h" #include "mongo/db/command_generic_argument.h" #include "mongo/db/commands/test_commands_enabled.h" +#include "mongo/db/commands_in_multi_doc_txn_params_gen.h" #include "mongo/db/curop.h" #include "mongo/db/error_labels.h" #include "mongo/db/jsobj.h" @@ -110,8 +111,7 @@ bool checkAuthorizationImplPreParse(OperationContext* opCtx, } // The command names that are allowed in a multi-document transaction. -const StringMap<int> txnCmdWhitelistFCV44 = { - {"create", 1}, {"createIndexes", 1}, {"_configsvrCreateCollection", 1}}; +const StringMap<int> txnCmdWhitelistFCV44 = {{"create", 1}, {"createIndexes", 1}}; const StringMap<int> txnCmdWhitelist = {{"abortTransaction", 1}, {"aggregate", 1}, {"commitTransaction", 1}, @@ -496,6 +496,15 @@ void CommandHelpers::canUseTransactions(const NamespaceString& nss, str::stream() << "Cannot run '" << cmdName << "' in a multi-document transaction.", inTxnWhitelist || inTxnWhitelistFCV44); + if (cmdName == "createCollection"_sd || cmdName == "createIndexes"_sd) { + uassert(ErrorCodes::OperationNotSupportedInTransaction, + str::stream() << "Cannot run '" << cmdName + << "' in a multi-document transaction " + "because creation of collections and " + "indexes inside multi-document transactions is disabled.", + gShouldMultiDocTxnCreateCollectionAndIndexes.load()); + } + const auto dbName = nss.db(); uassert(ErrorCodes::OperationNotSupportedInTransaction, diff --git a/src/mongo/db/commands_in_multi_doc_txn_params.idl b/src/mongo/db/commands_in_multi_doc_txn_params.idl new file mode 100644 index 00000000000..f9f0568286a --- /dev/null +++ b/src/mongo/db/commands_in_multi_doc_txn_params.idl @@ -0,0 +1,37 @@ +# Copyright (C) 2020-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. +# +global: + cpp_namespace: "mongo" + +server_parameters: + shouldMultiDocTxnCreateCollectionAndIndexes: + description: 'Enable ability to create new collections and indexes inside multi-document transactions. Default is true.' + set_at: [ startup, runtime ] + cpp_varname: 'gShouldMultiDocTxnCreateCollectionAndIndexes' + cpp_vartype: 'AtomicWord<bool>' + default: true diff --git a/src/mongo/db/ops/write_ops_exec.cpp b/src/mongo/db/ops/write_ops_exec.cpp index 8ebd6c54a8c..be883aa7013 100644 --- a/src/mongo/db/ops/write_ops_exec.cpp +++ b/src/mongo/db/ops/write_ops_exec.cpp @@ -42,6 +42,7 @@ #include "mongo/db/catalog/document_validation.h" #include "mongo/db/catalog_raii.h" #include "mongo/db/commands.h" +#include "mongo/db/commands_in_multi_doc_txn_params_gen.h" #include "mongo/db/concurrency/write_conflict_exception.h" #include "mongo/db/curop_failpoint_helpers.h" #include "mongo/db/curop_metrics.h" @@ -226,6 +227,12 @@ void makeCollection(OperationContext* opCtx, const NamespaceString& ns) { << " in multi-document transaction unless featureCompatibilityVersion is 4.4.", isFullyUpgradedTo44 || !inTransaction); + uassert(ErrorCodes::OperationNotSupportedInTransaction, + str::stream() << "Cannot create namespace " << ns.ns() + << "because creation of collections and indexes inside " + "multi-document transactions is disabled.", + !inTransaction || gShouldMultiDocTxnCreateCollectionAndIndexes.load()); + writeConflictRetry(opCtx, "implicit collection creation", ns.ns(), [&opCtx, &ns] { AutoGetOrCreateDb db(opCtx, ns.db(), MODE_IX); Lock::CollectionLock collLock(opCtx, ns, MODE_IX); |