diff options
author | Benety Goh <benety@mongodb.com> | 2018-09-25 13:07:19 -0400 |
---|---|---|
committer | Benety Goh <benety@mongodb.com> | 2018-09-25 17:47:21 -0400 |
commit | eb38d663ade9477810458c0b425da66aaeb8b56a (patch) | |
tree | 49a408f265ced03fee657c69d422f12f3369eb94 | |
parent | 1e4f12dc5e4c12e7656270907cf9d49715fa732f (diff) | |
download | mongo-eb38d663ade9477810458c0b425da66aaeb8b56a.tar.gz |
SERVER-36944 applyOps uses applyOperation_inlock() for system.indexes inserts
(cherry picked from commit bf2cfbd09d6695e2c84b295c3aa539adaf6496a8)
(cherry picked from commit 8fe372afcbad4cd67d46f993291bdc090423caa6)
-rw-r--r-- | jstests/core/apply_ops_invalid_index_spec.js | 7 | ||||
-rw-r--r-- | src/mongo/db/catalog/apply_ops.cpp | 48 |
2 files changed, 19 insertions, 36 deletions
diff --git a/jstests/core/apply_ops_invalid_index_spec.js b/jstests/core/apply_ops_invalid_index_spec.js index 47ff5e2e045..d4d9b73ff05 100644 --- a/jstests/core/apply_ops_invalid_index_spec.js +++ b/jstests/core/apply_ops_invalid_index_spec.js @@ -43,16 +43,15 @@ o: {v: 2, key: {a: 1}, name: 'a_1_system_v2', ns: collNs, unknown: 1}, }], }), - ErrorCodes.UnknownError); + ErrorCodes.InvalidIndexSpecificationOption); // Inserting a v:1 index directly into system.indexes with an unknown field in the index spec // should ignore the unrecognized field and create the index. - assert.commandFailedWithCode(db.adminCommand({ + assert.commandWorked(db.adminCommand({ applyOps: [{ op: 'i', ns: systemIndexesNs, o: {v: 1, key: {a: 1}, name: 'a_1_system_v1', ns: collNs, unknown: 1}, }], - }), - ErrorCodes.UnknownError); + })); })(); diff --git a/src/mongo/db/catalog/apply_ops.cpp b/src/mongo/db/catalog/apply_ops.cpp index a7306940d68..770bc24b616 100644 --- a/src/mongo/db/catalog/apply_ops.cpp +++ b/src/mongo/db/catalog/apply_ops.cpp @@ -180,39 +180,23 @@ Status _applyOps(OperationContext* opCtx, NamespaceString requestNss{ns}; if (nss.isSystemDotIndexes()) { - BSONObj indexSpec; - NamespaceString indexNss; - std::tie(indexSpec, indexNss) = - repl::prepForApplyOpsIndexInsert(fieldO, opObj, requestNss); - if (!indexSpec["collation"]) { - // If the index spec does not include a collation, explicitly - // specify the simple collation, so the index does not inherit the - // collection default collation. - auto indexVersion = indexSpec["v"]; - // The index version is populated by prepForApplyOpsIndexInsert(). - invariant(indexVersion); - if (indexVersion.isNumber() && - (indexVersion.numberInt() >= - static_cast<int>(IndexDescriptor::IndexVersion::kV2))) { - BSONObjBuilder bob; - bob.append("collation", CollationSpec::kSimpleSpec); - bob.appendElements(indexSpec); - indexSpec = bob.obj(); - } - } - BSONObjBuilder command; - command.append("createIndexes", indexNss.coll()); - { - BSONArrayBuilder indexes(command.subarrayStart("indexes")); - indexes.append(indexSpec); - indexes.doneFast(); + invariant(opCtx->lockState()->isW()); + OldClientContext ctx(opCtx, nss.ns()); + status = + repl::applyOperation_inlock(opCtx, ctx.db(), opObj, alwaysUpsert); + + // applyOperation_inlock() builds the index but does not notify the + // OpObserver. Previously, applyOps relied on the createIndexes command + // to perform this function. The value used for the 'forMigrate' + // argument is consistent with create_indexes.cpp. + if (status.isOK()) { + WriteUnitOfWork wuow(opCtx); + auto opObserver = getGlobalServiceContext()->getOpObserver(); + invariant(opObserver); + auto indexSpec = fieldO.embeddedObject(); + opObserver->onCreateIndex(opCtx, nss.ns(), indexSpec, false); + wuow.commit(); } - const BSONObj commandObj = command.done(); - - DBDirectClient client(opCtx); - BSONObj infoObj; - client.runCommand(nsToDatabase(ns), commandObj, infoObj); - status = getStatusFromCommandResult(infoObj); } else { AutoGetCollection autoColl(opCtx, nss, MODE_IX); if (!autoColl.getCollection() && !nss.isSystemDotIndexes()) { |