summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2018-10-01 15:17:48 -0400
committerBenety Goh <benety@mongodb.com>2018-10-01 15:17:48 -0400
commita70a8ce8cdc5734353acdd577956e58872169c64 (patch)
treeaa5dc4cc8d46db626630f4f183c645862411429a
parent15d15831540f2b59a889bdcb3fcf9725d6a23c96 (diff)
downloadmongo-a70a8ce8cdc5734353acdd577956e58872169c64.tar.gz
SERVER-37333 applyOps builds background indexes in the foreground
-rw-r--r--jstests/core/apply_ops_invalid_index_spec.js50
-rw-r--r--src/mongo/db/catalog/apply_ops.cpp9
2 files changed, 59 insertions, 0 deletions
diff --git a/jstests/core/apply_ops_invalid_index_spec.js b/jstests/core/apply_ops_invalid_index_spec.js
index d4d9b73ff05..2ca88081fa7 100644
--- a/jstests/core/apply_ops_invalid_index_spec.js
+++ b/jstests/core/apply_ops_invalid_index_spec.js
@@ -54,4 +54,54 @@
o: {v: 1, key: {a: 1}, name: 'a_1_system_v1', ns: collNs, unknown: 1},
}],
}));
+
+ //
+ // Background indexes should be subject to the same level of validation as foreground indexes.
+ //
+
+ // Inserting a background index directly into system.indexes with a bad index key pattern should
+ // return an error.
+ assert.commandFailedWithCode(db.adminCommand({
+ applyOps: [{
+ op: 'i',
+ ns: systemIndexesNs,
+ o: {key: {b: 'sideways'}, name: 'b_1_bg_system_v2', ns: collNs, background: true},
+ }],
+ }),
+ ErrorCodes.CannotCreateIndex);
+
+ // Inserting a v:2 background index directly into system.indexes with an unknown field in the
+ // index spec should return an error.
+ assert.commandFailedWithCode(db.adminCommand({
+ applyOps: [{
+ op: 'i',
+ ns: systemIndexesNs,
+ o: {
+ v: 2,
+ key: {b: 1},
+ name: 'b_1_bg_system_v2',
+ ns: collNs,
+ background: true,
+ unknown: true,
+ },
+ }],
+ }),
+ ErrorCodes.InvalidIndexSpecificationOption);
+
+ // Inserting a background v:1 index directly into system.indexes with an unknown field in the
+ // index spec should work.
+ assert.commandWorked(db.adminCommand({
+ applyOps: [{
+ op: 'i',
+ ns: systemIndexesNs,
+ o: {
+ v: 1,
+ key: {b: 1},
+ name: 'b_1_bg_system_v1',
+ ns: collNs,
+ background: true,
+ unknown: true,
+ },
+ }],
+ }));
})();
diff --git a/src/mongo/db/catalog/apply_ops.cpp b/src/mongo/db/catalog/apply_ops.cpp
index 770bc24b616..552376301cb 100644
--- a/src/mongo/db/catalog/apply_ops.cpp
+++ b/src/mongo/db/catalog/apply_ops.cpp
@@ -181,6 +181,15 @@ Status _applyOps(OperationContext* opCtx,
if (nss.isSystemDotIndexes()) {
invariant(opCtx->lockState()->isW());
+
+ // Disable background index builds when inserting into system.indexes.
+ // This causes the TempRelease to fail within applyOperation_inlock(),
+ // leading to the background index being built in the foreground.
+ // We do not want a background index build because we need to validate
+ // the index spec and also to avoid issues resulting from any metadata
+ // changes before the background thread starts.
+ Lock::GlobalWrite nestedGlobalWriteLock(opCtx->lockState());
+
OldClientContext ctx(opCtx, nss.ns());
status =
repl::applyOperation_inlock(opCtx, ctx.db(), opObj, alwaysUpsert);