From cf8fbf54354a805f0bdb5bc4282c8081f4802971 Mon Sep 17 00:00:00 2001 From: Louis Williams Date: Tue, 27 Nov 2018 14:51:48 -0500 Subject: SERVER-37911 createIndex via applyOps should notify opObserver in the WUOW of index commit --- src/mongo/db/index_builder.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/mongo/db/index_builder.cpp') diff --git a/src/mongo/db/index_builder.cpp b/src/mongo/db/index_builder.cpp index c8e38cab94d..3335b0a421d 100644 --- a/src/mongo/db/index_builder.cpp +++ b/src/mongo/db/index_builder.cpp @@ -44,6 +44,7 @@ #include "mongo/db/curop.h" #include "mongo/db/db_raii.h" #include "mongo/db/logical_clock.h" +#include "mongo/db/op_observer.h" #include "mongo/db/repl/timestamp_block.h" #include "mongo/util/assert_util.h" #include "mongo/util/log.h" @@ -110,10 +111,14 @@ void _setBgIndexStarting() { } } // namespace -IndexBuilder::IndexBuilder(const BSONObj& index, bool relaxConstraints, Timestamp initIndexTs) +IndexBuilder::IndexBuilder(const BSONObj& index, + IndexConstraints indexConstraints, + ReplicatedWrites replicatedWrites, + Timestamp initIndexTs) : BackgroundJob(true /* self-delete */), _index(index.getOwned()), - _relaxConstraints(relaxConstraints), + _indexConstraints(indexConstraints), + _replicatedWrites(replicatedWrites), _initIndexTs(initIndexTs), _name(str::stream() << "repl index builder " << _indexBuildCount.addAndFetch(1)) {} @@ -130,6 +135,12 @@ void IndexBuilder::run() { auto opCtx = cc().makeOperationContext(); ShouldNotConflictWithSecondaryBatchApplicationBlock shouldNotConflictBlock(opCtx->lockState()); + // If the calling thread is not replicating writes, neither should this thread. + boost::optional unreplicatedWrites; + if (_replicatedWrites == ReplicatedWrites::kUnreplicated) { + unreplicatedWrites.emplace(opCtx.get()); + } + AuthorizationSession::get(opCtx->getClient())->grantInternalAuthorization(); { @@ -238,7 +249,8 @@ Status IndexBuilder::_build(OperationContext* opCtx, } if (status == ErrorCodes::IndexAlreadyExists || - (status == ErrorCodes::IndexOptionsConflict && _relaxConstraints)) { + (status == ErrorCodes::IndexOptionsConflict && + _indexConstraints == IndexConstraints::kRelax)) { LOG(1) << "Ignoring indexing error: " << redact(status); if (allowBackgroundBuilding) { // Must set this in case anyone is waiting for this build. @@ -295,9 +307,12 @@ Status IndexBuilder::_build(OperationContext* opCtx, return _failIndexBuild(opCtx, indexer, dbLock, status, allowBackgroundBuilding); } - status = writeConflictRetry(opCtx, "Commit index build", ns.ns(), [this, opCtx, &indexer, &ns] { + status = writeConflictRetry(opCtx, "Commit index build", ns.ns(), [opCtx, coll, &indexer, &ns] { WriteUnitOfWork wunit(opCtx); - auto status = indexer.commit(); + auto status = indexer.commit([opCtx, coll, &ns](const BSONObj& indexSpec) { + opCtx->getServiceContext()->getOpObserver()->onCreateIndex( + opCtx, ns, *(coll->uuid()), indexSpec, false); + }); if (!status.isOK()) { return status; } -- cgit v1.2.1