@@ -474,31 +474,24 @@ Status MultiIndexBlock::insertAllDocumentsInCollection(
474474 PlanExecutor::ExecState state;
475475 while (PlanExecutor::ADVANCED == (state = exec->getNext (&objToIndex, &loc)) ||
476476 MONGO_unlikely (hangAfterStartingIndexBuild.shouldFail ())) {
477- auto interruptStatus = opCtx->checkForInterruptNoAssert ();
478- if (!interruptStatus.isOK ())
479- return interruptStatus;
477+ opCtx->checkForInterrupt ();
480478
481479 if (PlanExecutor::ADVANCED != state) {
482480 continue ;
483481 }
484482
485483 progress->setTotalWhileRunning (collection->numRecords (opCtx));
486484
487- interruptStatus =
485+ uassertStatusOK (
488486 failPointHangDuringBuild (opCtx,
489487 &hangIndexBuildDuringCollectionScanPhaseBeforeInsertion,
490488 " before" ,
491489 objToIndex,
492- n);
493- if (!interruptStatus.isOK ())
494- return interruptStatus;
490+ n));
495491
496492 // The external sorter is not part of the storage engine and therefore does not need a
497493 // WriteUnitOfWork to write keys.
498- Status ret = insertSingleDocumentForInitialSyncOrRecovery (opCtx, objToIndex, loc);
499- if (!ret.isOK ()) {
500- return ret;
501- }
494+ uassertStatusOK (insertSingleDocumentForInitialSyncOrRecovery (opCtx, objToIndex, loc));
502495
503496 failPointHangDuringBuild (opCtx,
504497 &hangIndexBuildDuringCollectionScanPhaseAfterInsertion,
@@ -511,9 +504,29 @@ Status MultiIndexBlock::insertAllDocumentsInCollection(
511504 progress->hit ();
512505 n++;
513506 }
514- } catch (...) {
515- _phase = IndexBuildPhaseEnum::kInitialized ;
516- return exceptionToStatus ();
507+ } catch (DBException& ex) {
508+ if (ex.isA <ErrorCategory::Interruption>() || ex.isA <ErrorCategory::ShutdownError>()) {
509+ // If the collection scan is stopped because due to an interrupt or shutdown event, we
510+ // leave the internal state intact to ensure we have the correct information for
511+ // resuming this index build during startup and rollback.
512+ } else {
513+ // Restore pre-collection scan state.
514+ _phase = IndexBuildPhaseEnum::kInitialized ;
515+ }
516+
517+ auto readSource = opCtx->recoveryUnit ()->getTimestampReadSource ();
518+ LOGV2 (4984704 ,
519+ " Index build: collection scan stopped" ,
520+ " buildUUID" _attr = _buildUUID,
521+ " totalRecords" _attr = n,
522+ " duration" _attr = duration_cast<Milliseconds>(Seconds (t.seconds ())),
523+ " readSource" _attr = RecoveryUnit::toString (readSource),
524+ " error" _attr = ex);
525+ ex.addContext (str::stream ()
526+ << " collection scan stopped. totalRecords: " << n
527+ << " ; durationMillis: " << duration_cast<Milliseconds>(Seconds (t.seconds ()))
528+ << " ; readSource: " << RecoveryUnit::toString (readSource));
529+ return ex.toStatus ();
517530 }
518531
519532 if (MONGO_unlikely (leaveIndexBuildUnfinishedForShutdown.shouldFail ())) {
@@ -550,6 +563,8 @@ Status MultiIndexBlock::insertAllDocumentsInCollection(
550563 " Index build: collection scan done" ,
551564 " buildUUID" _attr = _buildUUID,
552565 " totalRecords" _attr = n,
566+ " readSource" _attr =
567+ RecoveryUnit::toString (opCtx->recoveryUnit ()->getTimestampReadSource ()),
553568 " duration" _attr = duration_cast<Milliseconds>(Seconds (t.seconds ())));
554569
555570 Status ret = dumpInsertsFromBulk (opCtx);
0 commit comments