summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2021-11-11 20:38:01 -0500
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-11-12 02:04:13 +0000
commit8289d8a886a1926156e19314f2f68badd1dbc754 (patch)
tree1aa74084f0ef863f723ed39184e3c96999a2a0c3
parentde455570170e4e1c568a6c91bff8d317af141e41 (diff)
downloadmongo-8289d8a886a1926156e19314f2f68badd1dbc754.tar.gz
SERVER-61160 add dryRun to collMod request
This option directs collMod to attempt the requested modification without actually updating the database state. At this time, support is limited to the unique index conversion option.
-rw-r--r--jstests/core/collmod_convert_to_unique.js18
-rw-r--r--src/mongo/db/catalog/coll_mod.cpp27
-rw-r--r--src/mongo/db/coll_mod.idl7
3 files changed, 52 insertions, 0 deletions
diff --git a/jstests/core/collmod_convert_to_unique.js b/jstests/core/collmod_convert_to_unique.js
index 1d45005b1c6..046b6cc337a 100644
--- a/jstests/core/collmod_convert_to_unique.js
+++ b/jstests/core/collmod_convert_to_unique.js
@@ -63,6 +63,24 @@ const duplicateKeyError = assert.commandFailedWithCode(
jsTestLog('Duplicate key error from failed conversion: ' + tojson(duplicateKeyError));
assert.commandWorked(coll.remove({_id: 2}));
+//
+// Dry-run mode tests.
+//
+
+// Currently, support for dry run mode should be limited to unique conversion.
+assert.commandFailedWithCode(db.runCommand({
+ collMod: collName,
+ index: {keyPattern: {a: 1}, hidden: true},
+ dryRun: true,
+}),
+ ErrorCodes.InvalidOptions);
+assert.commandFailedWithCode(db.runCommand({
+ collMod: collName,
+ validationLevel: 'off',
+ dryRun: true,
+}),
+ ErrorCodes.InvalidOptions);
+
// Successfully converts to a unique index.
const result = assert.commandWorked(
db.runCommand({collMod: collName, index: {keyPattern: {a: 1}, unique: true}}));
diff --git a/src/mongo/db/catalog/coll_mod.cpp b/src/mongo/db/catalog/coll_mod.cpp
index 9119725b0b0..0d45fb82e67 100644
--- a/src/mongo/db/catalog/coll_mod.cpp
+++ b/src/mongo/db/catalog/coll_mod.cpp
@@ -114,6 +114,7 @@ struct ParsedCollModRequest {
boost::optional<ValidationLevelEnum> collValidationLevel;
bool recordPreImages = false;
boost::optional<ChangeStreamPreAndPostImagesOptions> changeStreamPreAndPostImagesOptions;
+ bool dryRun = false;
};
StatusWith<ParsedCollModRequest> parseCollModRequest(OperationContext* opCtx,
@@ -413,6 +414,10 @@ StatusWith<ParsedCollModRequest> parseCollModRequest(OperationContext* opCtx,
}
cmr.timeseries = e;
+ } else if (fieldName == CollMod::kDryRunFieldName) {
+ cmr.dryRun = e.trueValue();
+ // The dry run option should never be included in a collMod oplog entry.
+ continue;
} else {
if (isTimeseries) {
return Status(ErrorCodes::InvalidOptions,
@@ -474,11 +479,33 @@ void _setClusteredExpireAfterSeconds(OperationContext* opCtx,
coll->updateClusteredIndexTTLSetting(opCtx, newExpireAfterSeconds);
}
+Status _processCollModDryRunMode(OperationContext* opCtx,
+ const NamespaceStringOrUUID& nsOrUUID,
+ const CollMod& cmd,
+ BSONObjBuilder* result,
+ boost::optional<repl::OplogApplication::Mode> mode) {
+ // Ensure that the unique option is specified before validation the rest of the request
+ // and resolving the index descriptor.
+ if (!cmd.getIndex()) {
+ return {ErrorCodes::InvalidOptions, "dry run mode requires an valid index modification."};
+ }
+ if (!cmd.getIndex()->getUnique().value_or(false)) {
+ return {ErrorCodes::InvalidOptions,
+ "dry run mode requires an index modification with unique: true."};
+ }
+
+ return Status::OK();
+}
+
Status _collModInternal(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const CollMod& cmd,
BSONObjBuilder* result,
boost::optional<repl::OplogApplication::Mode> mode) {
+ if (cmd.getDryRun().value_or(false)) {
+ return _processCollModDryRunMode(opCtx, nsOrUUID, cmd, result, mode);
+ }
+
AutoGetCollection coll(opCtx, nsOrUUID, MODE_X, AutoGetCollectionViewMode::kViewsPermitted);
auto nss = coll.getNss();
StringData dbName = nss.db();
diff --git a/src/mongo/db/coll_mod.idl b/src/mongo/db/coll_mod.idl
index 90edf38c9dc..f9152f731a0 100644
--- a/src/mongo/db/coll_mod.idl
+++ b/src/mongo/db/coll_mod.idl
@@ -166,4 +166,11 @@ commands:
time-series buckets namespace before being sent to shards."
optional: true
type: bool
+ dryRun:
+ description: "Runs the requested modification without modifying any database state.
+ This can be used to determine in advance if a particular collMod
+ request can be completed without errors."
+ optional: true
+ type: safeBool
+ unstable: true
reply_type: CollModReply