diff options
author | Yuhong Zhang <danielzhangyh@gmail.com> | 2022-01-26 21:26:16 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-01-28 16:50:38 +0000 |
commit | f02247dfac78ed35cbd4bc8b0a510209463c6080 (patch) | |
tree | 2aff8e3c3eddbd7cd4ec524847776b11e4f596ae /src/mongo/db/catalog/coll_mod.cpp | |
parent | db799be5aebf432380cb5f7acb0f204fbc120a13 (diff) | |
download | mongo-f02247dfac78ed35cbd4bc8b0a510209463c6080.tar.gz |
SERVER-62886 Add the option `disallowNewDuplicateKeys` to collMod command
Diffstat (limited to 'src/mongo/db/catalog/coll_mod.cpp')
-rw-r--r-- | src/mongo/db/catalog/coll_mod.cpp | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/mongo/db/catalog/coll_mod.cpp b/src/mongo/db/catalog/coll_mod.cpp index 2b4bc8ee5f2..ac2b11d73b1 100644 --- a/src/mongo/db/catalog/coll_mod.cpp +++ b/src/mongo/db/catalog/coll_mod.cpp @@ -164,15 +164,16 @@ StatusWith<ParsedCollModRequest> parseCollModRequest(OperationContext* opCtx, } if (!cmdIndex.getExpireAfterSeconds() && !cmdIndex.getHidden() && - !cmdIndex.getUnique()) { - return Status(ErrorCodes::InvalidOptions, - "no expireAfterSeconds, hidden, or unique field"); + !cmdIndex.getUnique() && !cmdIndex.getDisallowNewDuplicateKeys()) { + return Status( + ErrorCodes::InvalidOptions, + "no expireAfterSeconds, hidden, unique, or disallowNewDuplicateKeys field"); } auto cmrIndex = &cmr.indexRequest; auto indexObj = e.Obj(); - if (cmdIndex.getUnique()) { + if (cmdIndex.getUnique() || cmdIndex.getDisallowNewDuplicateKeys()) { uassert(ErrorCodes::InvalidOptions, "collMod does not support converting an index to unique", feature_flags::gCollModIndexUnique.isEnabled( @@ -309,6 +310,15 @@ StatusWith<ParsedCollModRequest> parseCollModRequest(OperationContext* opCtx, } } + // The 'disallowNewDuplicateKeys' option is an ephemeral setting. It is replicated but + // still susceptible to process restarts. We do not compare the requested change with + // the existing state, so there is no need for the no-op conversion logic that we have + // for 'hidden' or 'unique'. + if (cmdIndex.getDisallowNewDuplicateKeys()) { + cmr.numModifications++; + cmrIndex->indexDisallowNewDuplicateKeys = cmdIndex.getDisallowNewDuplicateKeys(); + } + // The index options doc must contain either the name or key pattern, but not both. // If we have just one field, the index modifications requested matches the current // state in catalog and there is nothing further to do. @@ -450,6 +460,12 @@ StatusWith<ParsedCollModRequest> parseCollModRequest(OperationContext* opCtx, oplogEntryBuilder->append(e); } + // Currently disallows the use of 'indexDisallowNewDuplicateKeys' with other collMod options. + if (cmr.indexRequest.indexDisallowNewDuplicateKeys && cmr.numModifications > 1) { + return {ErrorCodes::InvalidOptions, + "disallowNewDuplicateKeys cannot be combined with any other modification."}; + } + return {std::move(cmr)}; } @@ -572,10 +588,6 @@ StatusWith<std::unique_ptr<CollModWriteOpsTracker::Token>> _setUpCollModIndexUni const auto& cmr = statusW.getValue(); auto idx = cmr.indexRequest.idx; auto violatingRecordsList = scanIndexForDuplicates(opCtx, collection, idx); - if (!violatingRecordsList.empty()) { - uassertStatusOK(buildConvertUniqueErrorStatus( - buildDuplicateViolations(opCtx, collection, violatingRecordsList))); - } CurOpFailpointHelpers::waitWhileFailPointEnabled(&hangAfterCollModIndexUniqueSideWriteTracker, opCtx, @@ -583,6 +595,11 @@ StatusWith<std::unique_ptr<CollModWriteOpsTracker::Token>> _setUpCollModIndexUni []() {}, nss); + if (!violatingRecordsList.empty()) { + uassertStatusOK(buildConvertUniqueErrorStatus( + buildDuplicateViolations(opCtx, collection, violatingRecordsList))); + } + return std::move(writeOpsToken); } |