From 76c21bda5674f7c38f2bf52be9abda6ff1cd77e3 Mon Sep 17 00:00:00 2001 From: Yuhong Zhang Date: Wed, 9 Feb 2022 03:22:20 +0000 Subject: SERVER-63443 Make `disallowNewDuplicateKeys` persisted in catalog --- src/mongo/db/index/index_access_method.cpp | 7 ++++--- src/mongo/db/index/index_access_method.h | 18 ------------------ src/mongo/db/index/index_descriptor.cpp | 11 +++++++++++ src/mongo/db/index/index_descriptor.h | 6 ++++++ 4 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/mongo/db/index') diff --git a/src/mongo/db/index/index_access_method.cpp b/src/mongo/db/index/index_access_method.cpp index 52e02ccbce8..d291efd6105 100644 --- a/src/mongo/db/index/index_access_method.cpp +++ b/src/mongo/db/index/index_access_method.cpp @@ -268,6 +268,7 @@ Status SortedDataIndexAccessMethod::insertKeys(OperationContext* opCtx, *numInserted = 0; } bool unique = _descriptor->unique(); + bool disallowNewDuplicateKeys = _descriptor->disallowNewDuplicateKeys(); bool dupsAllowed; if (!_descriptor->isIdIndex() && !opCtx->isEnforcingConstraints() && coll->isIndexReady(_descriptor->indexName())) { @@ -279,7 +280,7 @@ Status SortedDataIndexAccessMethod::insertKeys(OperationContext* opCtx, // Additionally, unique indexes conflict checking can cause out-of-order updates in // wiredtiger. See SERVER-59831. dupsAllowed = true; - } else if (isEnforcingDuplicateConstraints()) { + } else if (disallowNewDuplicateKeys) { // This currently is only used by collMod command when converting a regular index to a // unique index. The regular index will start rejecting duplicates even before the // conversion finishes. @@ -294,7 +295,7 @@ Status SortedDataIndexAccessMethod::insertKeys(OperationContext* opCtx, // When duplicates are encountered and allowed, retry with dupsAllowed. Call // onDuplicateKey() with the inserted duplicate key. if (ErrorCodes::DuplicateKey == result.getStatus().code() && options.dupsAllowed && - !isEnforcingDuplicateConstraints()) { + !disallowNewDuplicateKeys) { invariant(unique); result = _newInterface->insert(opCtx, keyString, true /* dupsAllowed */); @@ -541,7 +542,7 @@ Status SortedDataIndexAccessMethod::doUpdate(OperationContext* opCtx, // Add all new data keys into the index. for (const auto& keyString : ticket.added) { - bool dupsAllowed = !isEnforcingDuplicateConstraints() && ticket.dupsAllowed; + bool dupsAllowed = !_descriptor->disallowNewDuplicateKeys() && ticket.dupsAllowed; auto result = _newInterface->insert(opCtx, keyString, dupsAllowed); if (!result.isOK()) return result.getStatus(); diff --git a/src/mongo/db/index/index_access_method.h b/src/mongo/db/index/index_access_method.h index 8b911c01603..2fe9efc1778 100644 --- a/src/mongo/db/index/index_access_method.h +++ b/src/mongo/db/index/index_access_method.h @@ -43,7 +43,6 @@ #include "mongo/db/sorter/sorter.h" #include "mongo/db/storage/sorted_data_interface.h" #include "mongo/db/yieldable.h" -#include "mongo/platform/atomic_word.h" namespace mongo { @@ -240,23 +239,6 @@ public: size_t maxMemoryUsageBytes, const boost::optional& stateInfo, StringData dbName) = 0; - - void setEnforceDuplicateConstraints(bool enforceDuplicateConstraints) { - _enforceDuplicateConstraints.swap(enforceDuplicateConstraints); - } - - /** - * When `true`, disallows duplicates when inserting to or updating the index. Otherwise, sets - * `dupsAllowed` according to other options. - * Currently only temporarily set to `true` during collMod converting index to unique. This - * should always remain `false` otherwise. - */ - bool isEnforcingDuplicateConstraints() const { - return _enforceDuplicateConstraints.load(); - } - -private: - AtomicWord _enforceDuplicateConstraints{false}; }; /** diff --git a/src/mongo/db/index/index_descriptor.cpp b/src/mongo/db/index/index_descriptor.cpp index 69fac811cd6..b276fb9b38c 100644 --- a/src/mongo/db/index/index_descriptor.cpp +++ b/src/mongo/db/index/index_descriptor.cpp @@ -41,6 +41,7 @@ #include "mongo/db/matcher/expression_parser.h" #include "mongo/db/query/collation/collator_factory_interface.h" #include "mongo/db/server_options.h" +#include "mongo/db/storage/storage_parameters_gen.h" namespace mongo { @@ -106,6 +107,7 @@ constexpr StringData IndexDescriptor::kTextVersionFieldName; constexpr StringData IndexDescriptor::kUniqueFieldName; constexpr StringData IndexDescriptor::kHiddenFieldName; constexpr StringData IndexDescriptor::kWeightsFieldName; +constexpr StringData IndexDescriptor::kDisallowNewDuplicateKeysFieldName; IndexDescriptor::IndexDescriptor(const std::string& accessMethodName, BSONObj infoObj) : _accessMethodName(accessMethodName), @@ -133,6 +135,15 @@ IndexDescriptor::IndexDescriptor(const std::string& accessMethodName, BSONObj in invariant(collationElement.isABSONObj()); _collation = collationElement.Obj().getOwned(); } + + if (BSONElement disallowNewDuplicateKeysElement = + _infoObj[kDisallowNewDuplicateKeysFieldName]) { + uassert( + ErrorCodes::InvalidOptions, + "Index does not support the 'disallowNewDuplicateKeys' field", + feature_flags::gCollModIndexUnique.isEnabled(serverGlobalParams.featureCompatibility)); + _disallowNewDuplicateKeys = disallowNewDuplicateKeysElement.trueValue(); + } } bool IndexDescriptor::isIndexVersionSupported(IndexVersion indexVersion) { diff --git a/src/mongo/db/index/index_descriptor.h b/src/mongo/db/index/index_descriptor.h index 76e5dce4c91..35a005371b4 100644 --- a/src/mongo/db/index/index_descriptor.h +++ b/src/mongo/db/index/index_descriptor.h @@ -88,6 +88,7 @@ public: static constexpr StringData kUniqueFieldName = "unique"_sd; static constexpr StringData kWeightsFieldName = "weights"_sd; static constexpr StringData kOriginalSpecFieldName = "originalSpec"_sd; + static constexpr StringData kDisallowNewDuplicateKeysFieldName = "disallowNewDuplicateKeys"_sd; /** * infoObj is a copy of the index-describing BSONObj contained in the catalog. @@ -226,6 +227,10 @@ public: return _partialFilterExpression; } + bool disallowNewDuplicateKeys() const { + return _disallowNewDuplicateKeys; + } + /** * Returns true if the key pattern is for the _id index. * The _id index must have form exactly {_id : 1} or {_id : -1}. @@ -275,6 +280,7 @@ private: IndexVersion _version; BSONObj _collation; BSONObj _partialFilterExpression; + bool _disallowNewDuplicateKeys = false; // Many query stages require going from an IndexDescriptor to its IndexCatalogEntry, so for // now we need this. -- cgit v1.2.1