summaryrefslogtreecommitdiff
path: root/src/mongo/db/index_builds_coordinator_mongod.cpp
blob: 5a5773b18771b60892e4f386f33339c5e44043cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */


#include "mongo/platform/basic.h"

#include "mongo/db/index_builds_coordinator_mongod.h"

#include <algorithm>

#include "mongo/db/audit.h"
#include "mongo/db/catalog/collection_catalog.h"
#include "mongo/db/catalog/index_build_entry_gen.h"
#include "mongo/db/concurrency/locker.h"
#include "mongo/db/concurrency/replication_state_transition_lock_guard.h"
#include "mongo/db/curop.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/index_build_entry_helpers.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/repl/tenant_migration_access_blocker_util.h"
#include "mongo/db/s/forwardable_operation_metadata.h"
#include "mongo/db/s/global_user_write_block_state.h"
#include "mongo/db/s/operation_sharding_state.h"
#include "mongo/db/service_context.h"
#include "mongo/db/stats/resource_consumption_metrics.h"
#include "mongo/db/storage/two_phase_index_build_knobs_gen.h"
#include "mongo/executor/task_executor.h"
#include "mongo/logv2/log.h"
#include "mongo/rpc/get_status_from_command_result.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/scoped_counter.h"
#include "mongo/util/str.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kStorage


namespace mongo {

namespace {

MONGO_FAIL_POINT_DEFINE(hangAfterAcquiringIndexBuildSlot);
MONGO_FAIL_POINT_DEFINE(hangBeforeInitializingIndexBuild);
MONGO_FAIL_POINT_DEFINE(hangIndexBuildAfterSignalPrimaryForCommitReadiness);
MONGO_FAIL_POINT_DEFINE(hangBeforeRunningIndexBuild);
MONGO_FAIL_POINT_DEFINE(hangIndexBuildBeforeSignalingPrimaryForAbort);
MONGO_FAIL_POINT_DEFINE(hangIndexBuildBeforeTransitioningReplStateTokAwaitPrimaryAbort);
MONGO_FAIL_POINT_DEFINE(hangBeforeVoteCommitIndexBuild);

const StringData kMaxNumActiveUserIndexBuildsServerParameterName = "maxNumActiveUserIndexBuilds"_sd;

/**
 * Constructs the options for the loader thread pool.
 */
ThreadPool::Options makeDefaultThreadPoolOptions() {
    ThreadPool::Options options;
    options.poolName = "IndexBuildsCoordinatorMongod";
    options.minThreads = 0;
    // Both the primary and secondary nodes will have an unlimited thread pool size. This is done to
    // allow secondary nodes to startup as many index builders as necessary in order to prevent
    // scheduling deadlocks during initial sync or oplog application. When commands are run from
    // user connections that need to create indexes, those commands will hang until there are less
    // than 'maxNumActiveUserIndexBuilds' running index build threads, or until the operation is
    // interrupted.
    options.maxThreads = ThreadPool::Options::kUnlimited;

    // Ensure all threads have a client.
    options.onCreateThread = [](const std::string& threadName) {
        Client::initThread(threadName.c_str());

        stdx::lock_guard<Client> lk(cc());
        cc().setSystemOperationUnkillableByStepdown(lk);
    };

    return options;
}

void runVoteCommand(OperationContext* opCtx,
                    ReplIndexBuildState* replState,
                    std::function<BSONObj(const UUID&, const std::string&)> generateCommand,
                    std::function<bool(BSONObj, const UUID)> validateFn) {

    auto replCoord = repl::ReplicationCoordinator::get(opCtx);

    // No locks should be held.
    invariant(!opCtx->lockState()->isLocked());

    Backoff exponentialBackoff(Seconds(1), Seconds(2));

    auto onRemoteCmdScheduled = [opCtx, replState](executor::TaskExecutor::CallbackHandle handle) {
        replState->onVoteRequestScheduled(opCtx, handle);
    };

    auto onRemoteCmdComplete = [replState](executor::TaskExecutor::CallbackHandle) {
        replState->clearVoteRequestCbk();
    };

    auto needToVote = [replState]() -> bool {
        return !replState->getNextActionNoWait();
    };

    // Retry command on error until we have been signaled either with commit or abort. This way, we
    // can make sure majority of nodes will never stop voting and wait for commit or abort signal
    // until they have received commit or abort signal.
    while (needToVote()) {
        // Check for any interrupts, including shutdown-related ones, before starting the voting
        // process.
        opCtx->checkForInterrupt();

        // Don't hammer the network.
        sleepFor(exponentialBackoff.nextSleep());
        // When index build started during startup recovery can try to get it's address when
        // rsConfig is uninitialized. So, retry till it gets initialized. Also, it's important, when
        // we retry, we check if we have received commit or abort signal to ensure liveness. For
        // e.g., consider a case where  index build gets restarted on startup recovery and
        // indexBuildsCoordinator thread waits for valid address w/o checking commit or abort
        // signal. Now, things can go wrong if we try to replay commitIndexBuild oplog entry for
        // that index build on startup recovery. Oplog applier would get stuck waiting on the
        // indexBuildsCoordinator thread. As a result, we won't be able to transition to secondary
        // state, get stuck on startup state.
        auto myAddress = replCoord->getMyHostAndPort();
        if (myAddress.empty()) {
            continue;
        }

        const BSONObj voteCmdRequest = generateCommand(replState->buildUUID, myAddress.toString());

        BSONObj voteCmdResponse;
        try {
            voteCmdResponse = replCoord->runCmdOnPrimaryAndAwaitResponse(
                opCtx, "admin", voteCmdRequest, onRemoteCmdScheduled, onRemoteCmdComplete);
        } catch (DBException& ex) {
            // All errors, including CallbackCanceled and network errors, should be retried.
            // If ErrorCodes::CallbackCanceled is due to shutdown, then checkForInterrupt() at the
            // beginning of this loop will catch it and throw an error to the caller. Or, if we
            // received the CallbackCanceled error because the index build was signaled with abort
            // or commit signal, then needToVote() would return false and we don't retry the voting
            // process.
            LOGV2_DEBUG(4666400,
                        1,
                        "Failed to run index build vote command.",
                        "command"_attr = voteCmdRequest.firstElement().fieldName(),
                        "indexBuildUUID"_attr = replState->buildUUID,
                        "errorMsg"_attr = ex);
            continue;
        }

        // Check if the command has to be retried.
        if (validateFn(voteCmdResponse, replState->buildUUID)) {
            break;
        }
    }
};
}  // namespace

IndexBuildsCoordinatorMongod::IndexBuildsCoordinatorMongod()
    : _threadPool(makeDefaultThreadPoolOptions()) {
    _threadPool.startup();

    // Change the 'setOnUpdate' function for the server parameter to signal the condition variable
    // when the value changes.
    using ParamT =
        IDLServerParameterWithStorage<ServerParameterType::kStartupAndRuntime, AtomicWord<int>>;
    ServerParameterSet::getNodeParameterSet()
        ->get<ParamT>(kMaxNumActiveUserIndexBuildsServerParameterName)
        ->setOnUpdate([this](const int) -> Status {
            _indexBuildFinished.notify_all();
            return Status::OK();
        });
}

void IndexBuildsCoordinatorMongod::shutdown(OperationContext* opCtx) {
    // Stop new scheduling.
    _threadPool.shutdown();

    // Wait for all active builds to stop.
    waitForAllIndexBuildsToStopForShutdown(opCtx);

    // Wait for active threads to finish.
    _threadPool.join();
}

StatusWith<SharedSemiFuture<ReplIndexBuildState::IndexCatalogStats>>
IndexBuildsCoordinatorMongod::startIndexBuild(OperationContext* opCtx,
                                              const DatabaseName& dbName,
                                              const UUID& collectionUUID,
                                              const std::vector<BSONObj>& specs,
                                              const UUID& buildUUID,
                                              IndexBuildProtocol protocol,
                                              IndexBuildOptions indexBuildOptions) {
    return _startIndexBuild(
        opCtx, dbName, collectionUUID, specs, buildUUID, protocol, indexBuildOptions, boost::none);
}

StatusWith<SharedSemiFuture<ReplIndexBuildState::IndexCatalogStats>>
IndexBuildsCoordinatorMongod::resumeIndexBuild(OperationContext* opCtx,
                                               const DatabaseName& dbName,
                                               const UUID& collectionUUID,
                                               const std::vector<BSONObj>& specs,
                                               const UUID& buildUUID,
                                               const ResumeIndexInfo& resumeInfo) {
    IndexBuildsCoordinator::IndexBuildOptions indexBuildOptions;
    indexBuildOptions.applicationMode = ApplicationMode::kStartupRepair;
    return _startIndexBuild(opCtx,
                            dbName,
                            collectionUUID,
                            specs,
                            buildUUID,
                            IndexBuildProtocol::kTwoPhase,
                            indexBuildOptions,
                            resumeInfo);
}

StatusWith<SharedSemiFuture<ReplIndexBuildState::IndexCatalogStats>>
IndexBuildsCoordinatorMongod::_startIndexBuild(OperationContext* opCtx,
                                               const DatabaseName& dbName,
                                               const UUID& collectionUUID,
                                               const std::vector<BSONObj>& specs,
                                               const UUID& buildUUID,
                                               IndexBuildProtocol protocol,
                                               IndexBuildOptions indexBuildOptions,
                                               const boost::optional<ResumeIndexInfo>& resumeInfo) {
    const NamespaceStringOrUUID nssOrUuid{dbName, collectionUUID};

    auto writeBlockState = GlobalUserWriteBlockState::get(opCtx);

    invariant(!opCtx->lockState()->isRSTLExclusive(), buildUUID.toString());

    const auto nss = CollectionCatalog::get(opCtx)->resolveNamespaceStringOrUUID(opCtx, nssOrUuid);

    {
        // Only operations originating from user connections need to wait while there are more than
        // 'maxNumActiveUserIndexBuilds' index builds currently running.
        if (opCtx->getClient()->isFromUserConnection()) {
            {
                // The global lock acquires the RSTL lock which we use to assert that we're the
                // primary node when running user operations. Additionally, releasing this lock
                // allows the node to step down after we have checked the replication state. If this
                // node steps down after this check, similar assertions will cause the index build
                // to fail later on when locks are reacquired. Therefore, this assertion is not
                // required for correctness, but only intended to rate limit index builds started on
                // primaries.
                ShouldNotConflictWithSecondaryBatchApplicationBlock shouldNotConflictBlock(
                    opCtx->lockState());
                Lock::GlobalLock globalLk(opCtx, MODE_IX);

                auto replCoord = repl::ReplicationCoordinator::get(opCtx);
                uassert(ErrorCodes::NotWritablePrimary,
                        "Not primary while waiting to start an index build",
                        replCoord->canAcceptWritesFor(opCtx, nssOrUuid));
            }

            // The checks here catch empty index builds and also allow us to stop index
            // builds before waiting for throttling. It may race with the abort at the start
            // of migration so we do check again later.
            uassertStatusOK(tenant_migration_access_blocker::checkIfCanBuildIndex(opCtx, dbName));
            uassertStatusOK(writeBlockState->checkIfIndexBuildAllowedToStart(opCtx, nss));

            stdx::unique_lock<Latch> lk(_throttlingMutex);
            bool messageLogged = false;
            opCtx->waitForConditionOrInterrupt(_indexBuildFinished, lk, [&] {
                const int maxActiveBuilds = maxNumActiveUserIndexBuilds.load();
                if (_numActiveIndexBuilds < maxActiveBuilds) {
                    _numActiveIndexBuilds++;
                    return true;
                }

                if (!messageLogged) {
                    LOGV2(
                        4715500,
                        "Too many index builds running simultaneously, waiting until the number of "
                        "active index builds is below the threshold",
                        "numActiveIndexBuilds"_attr = _numActiveIndexBuilds,
                        "maxNumActiveUserIndexBuilds"_attr = maxActiveBuilds,
                        "indexSpecs"_attr = specs,
                        "buildUUID"_attr = buildUUID,
                        "collectionUUID"_attr = collectionUUID);
                    messageLogged = true;
                }
                return false;
            });
        } else {
            // System index builds have no limit and never wait, but do consume a slot.
            stdx::unique_lock<Latch> lk(_throttlingMutex);
            _numActiveIndexBuilds++;
        }
    }

    ScopeGuard onScopeExitGuard([&] {
        stdx::unique_lock<Latch> lk(_throttlingMutex);
        _numActiveIndexBuilds--;
        _indexBuildFinished.notify_one();
    });

    if (MONGO_unlikely(hangAfterAcquiringIndexBuildSlot.shouldFail())) {
        LOGV2(4886201, "Hanging index build due to failpoint 'hangAfterAcquiringIndexBuildSlot'");
        hangAfterAcquiringIndexBuildSlot.pauseWhileSet();
    }

    if (indexBuildOptions.applicationMode == ApplicationMode::kStartupRepair) {
        // Two phase index build recovery goes though a different set-up procedure because we will
        // either resume the index build or the original index will be dropped first.
        invariant(protocol == IndexBuildProtocol::kTwoPhase);
        auto status = Status::OK();
        if (resumeInfo) {
            status = _setUpResumeIndexBuild(
                opCtx, dbName, collectionUUID, specs, buildUUID, resumeInfo.value());
        } else {
            status = _setUpIndexBuildForTwoPhaseRecovery(
                opCtx, dbName, collectionUUID, specs, buildUUID);
        }
        if (!status.isOK()) {
            return status;
        }
    } else {
        auto statusWithOptionalResult =
            _filterSpecsAndRegisterBuild(opCtx, dbName, collectionUUID, specs, buildUUID, protocol);
        if (!statusWithOptionalResult.isOK()) {
            return statusWithOptionalResult.getStatus();
        }

        if (statusWithOptionalResult.getValue()) {
            invariant(statusWithOptionalResult.getValue()->isReady());
            // The requested index (specs) are already built or are being built. Return success
            // early (this is v4.0 behavior compatible).
            return statusWithOptionalResult.getValue().value();
        }

        if (opCtx->getClient()->isFromUserConnection()) {
            auto migrationStatus =
                tenant_migration_access_blocker::checkIfCanBuildIndex(opCtx, dbName);
            if (!migrationStatus.isOK()) {
                LOGV2(4886200,
                      "Aborted index build before start due to tenant migration",
                      "error"_attr = migrationStatus,
                      "buildUUID"_attr = buildUUID,
                      "collectionUUID"_attr = collectionUUID);
                activeIndexBuilds.unregisterIndexBuild(&_indexBuildsManager,
                                                       invariant(_getIndexBuild(buildUUID)));
                return migrationStatus;
            }

            auto buildBlockedStatus = writeBlockState->checkIfIndexBuildAllowedToStart(opCtx, nss);
            if (!buildBlockedStatus.isOK()) {
                LOGV2(6511603,
                      "Aborted index build due to user index builds being blocked",
                      "error"_attr = buildBlockedStatus,
                      "buildUUID"_attr = buildUUID,
                      "collectionUUID"_attr = collectionUUID);
                activeIndexBuilds.unregisterIndexBuild(&_indexBuildsManager,
                                                       invariant(_getIndexBuild(buildUUID)));
                return buildBlockedStatus;
            }
        }
    }

    auto& oss = OperationShardingState::get(opCtx);

    // The builder thread updates to curOp description to be that of a createIndexes command, but we
    // still want to transfer whatever extra information there is available from the caller.
    BSONObj opDesc;
    {
        stdx::unique_lock<Client> lk(*opCtx->getClient());
        auto curOp = CurOp::get(opCtx);
        opDesc = curOp->opDescription().getOwned();
    }

    // If this index build was started during secondary batch application, it will have a commit
    // timestamp that must be copied over to timestamp the write to initialize the index build.
    const auto startTimestamp = opCtx->recoveryUnit()->getCommitTimestamp();

    // Use a promise-future pair to wait until the index build has been started. This future will
    // only return when the index build thread has started and the initial catalog write has been
    // written, or an error has been encountered otherwise.
    auto [startPromise, startFuture] = makePromiseFuture<void>();


    auto replState = invariant(_getIndexBuild(buildUUID));

    // Since index builds occur in a separate thread, client attributes that are audited must be
    // extracted from the client object and passed into the thread separately.
    audit::ImpersonatedClientAttrs impersonatedClientAttrs(opCtx->getClient());
    ForwardableOperationMetadata forwardableOpMetadata(opCtx);

    // The thread pool task will be responsible for signalling the condition variable when the index
    // build thread is done running.
    onScopeExitGuard.dismiss();
    _threadPool.schedule([this,
                          buildUUID,
                          dbName,
                          nss,
                          indexBuildOptions,
                          opDesc,
                          replState,
                          startPromise = std::move(startPromise),
                          startTimestamp,
                          shardVersion = oss.getShardVersion(nss),
                          dbVersion = oss.getDbVersion(dbName.toStringWithTenantId()),
                          resumeInfo,
                          impersonatedClientAttrs = std::move(impersonatedClientAttrs),
                          forwardableOpMetadata =
                              std::move(forwardableOpMetadata)](auto status) mutable noexcept {
        ScopeGuard onScopeExitGuard([&] {
            stdx::unique_lock<Latch> lk(_throttlingMutex);
            _numActiveIndexBuilds--;
            _indexBuildFinished.notify_one();
        });

        // Clean up if we failed to schedule the task.
        if (!status.isOK()) {
            activeIndexBuilds.unregisterIndexBuild(&_indexBuildsManager, replState);
            startPromise.setError(status);
            return;
        }

        auto opCtx = Client::getCurrent()->makeOperationContext();
        // Indicate that the index build is scheduled and running under this opCtx.
        replState->onThreadScheduled(opCtx.get());

        // Set up the thread's currentOp information to display createIndexes cmd information,
        // merged with the caller's opDesc.
        updateCurOpOpDescription(opCtx.get(), nss, replState->indexSpecs, opDesc);

        // Forward the forwardable operation metadata from the external client to this thread's
        // client.
        forwardableOpMetadata.setOn(opCtx.get());

        // Load the external client's attributes into this thread's client for auditing.
        auto authSession = AuthorizationSession::get(opCtx->getClient());
        if (authSession) {
            authSession->setImpersonatedUserData(std::move(impersonatedClientAttrs.userName),
                                                 std::move(impersonatedClientAttrs.roleNames));
        }

        ScopedSetShardRole scopedSetShardRole(opCtx.get(), nss, shardVersion, dbVersion);

        while (MONGO_unlikely(hangBeforeInitializingIndexBuild.shouldFail())) {
            sleepmillis(100);
        }

        // Start collecting metrics for the index build. The metrics for this operation will only be
        // aggregated globally if the node commits or aborts while it is primary.
        auto& metricsCollector = ResourceConsumption::MetricsCollector::get(opCtx.get());
        if (ResourceConsumption::shouldCollectMetricsForDatabase(dbName.toStringWithTenantId()) &&
            ResourceConsumption::isMetricsCollectionEnabled()) {
            metricsCollector.beginScopedCollecting(opCtx.get(), dbName.toStringWithTenantId());
        }

        // Index builds should never take the PBWM lock, even on a primary. This allows the
        // index build to continue running after the node steps down to a secondary.
        ShouldNotConflictWithSecondaryBatchApplicationBlock shouldNotConflictBlock(
            opCtx->lockState());

        if (indexBuildOptions.applicationMode != ApplicationMode::kStartupRepair) {
            status = _setUpIndexBuild(opCtx.get(), buildUUID, startTimestamp, indexBuildOptions);
            if (!status.isOK()) {
                startPromise.setError(status);
                // Do not exit with an incomplete future, even if setup fails, we should still
                // signal waiters.
                invariant(replState->sharedPromise.getFuture().isReady());
                return;
            }
        }

        // Signal that the index build started successfully.
        startPromise.setWith([] {});

        hangBeforeRunningIndexBuild.pauseWhileSet();

        // Runs the remainder of the index build. Sets the promise result and cleans up the index
        // build.
        _runIndexBuild(opCtx.get(), buildUUID, indexBuildOptions, resumeInfo);

        // Do not exit with an incomplete future.
        invariant(replState->sharedPromise.getFuture().isReady());

        try {
            // Logs the index build statistics if it took longer than the server parameter `slowMs`
            // to complete.
            CurOp::get(opCtx.get())
                ->completeAndLogOperation(MONGO_LOGV2_DEFAULT_COMPONENT,
                                          CollectionCatalog::get(opCtx.get())
                                              ->getDatabaseProfileSettings(nss.dbName())
                                              .filter);
        } catch (const DBException& e) {
            LOGV2(4656002, "unable to log operation", "error"_attr = e);
        }
    });

    // Waits until the index build has either been started or failed to start.
    // Ignore any interruption state in 'opCtx'.
    // If 'opCtx' is interrupted, the caller will be notified after startIndexBuild() returns when
    // it checks the future associated with 'sharedPromise'.
    auto status = startFuture.getNoThrow(Interruptible::notInterruptible());
    if (!status.isOK()) {
        return status;
    }
    return replState->sharedPromise.getFuture();
}

Status IndexBuildsCoordinatorMongod::voteAbortIndexBuild(OperationContext* opCtx,
                                                         const UUID& buildUUID,
                                                         const HostAndPort& votingNode,
                                                         const StringData& reason) {

    const auto replCoord = repl::ReplicationCoordinator::get(opCtx);
    auto memberConfig = replCoord->findConfigMemberByHostAndPort(votingNode);
    if (!memberConfig || memberConfig->isArbiter()) {
        return Status{ErrorCodes::Error{7329201},
                      "Non-member and arbiter nodes cannot vote to abort an index build"};
    }

    if (replCoord->getMyHostAndPort() == votingNode) {
        LOGV2_DEBUG(7329404, 2, "'voteAbortIndexBuild' loopback", "node"_attr = votingNode);
    }

    bool aborted = abortIndexBuildByBuildUUID(
        opCtx,
        buildUUID,
        IndexBuildAction::kPrimaryAbort,
        fmt::format("'voteAbortIndexBuild' received from '{}': {}", votingNode.toString(), reason));

    if (aborted) {
        return Status::OK();
    }

    // Index build does not exist or cannot be aborted because it is committing.
    // No need to wait for write concern.
    return Status{ErrorCodes::Error{7329202}, "Index build cannot be aborted"};
}

Status IndexBuildsCoordinatorMongod::voteCommitIndexBuild(OperationContext* opCtx,
                                                          const UUID& buildUUID,
                                                          const HostAndPort& votingNode) {
    hangBeforeVoteCommitIndexBuild.pauseWhileSet(opCtx);

    auto swReplState = _getIndexBuild(buildUUID);
    if (!swReplState.isOK()) {
        // Index build might have got torn down.
        return swReplState.getStatus();
    }

    auto replState = swReplState.getValue();

    {
        // Secondary nodes will always try to vote regardless of the commit quorum value. If the
        // commit quorum is disabled, do not record their entry into the commit ready nodes.
        // If we fail to retrieve the persisted commit quorum, the index build might be in the
        // middle of tearing down.
        Lock::SharedLock commitQuorumLk(opCtx, *replState->commitQuorumLock);
        auto commitQuorum =
            uassertStatusOK(indexbuildentryhelpers::getCommitQuorum(opCtx, buildUUID));
        if (commitQuorum.numNodes == CommitQuorumOptions::kDisabled) {
            return Status::OK();
        }
    }

    // Our current contract is that commit quorum can't be disabled for an active index build with
    // commit quorum on (i.e., commit value set as non-zero or a valid tag) and vice-versa. So,
    // after this point, it's not possible for the index build's commit quorum value to get updated
    // to CommitQuorumOptions::kDisabled.
    Status persistStatus = Status::OK();

    IndexBuildEntry indexbuildEntry(
        buildUUID, replState->collectionUUID, CommitQuorumOptions(), replState->indexNames);
    std::vector<HostAndPort> votersList{votingNode};
    indexbuildEntry.setCommitReadyMembers(votersList);

    {
        // Updates don't need to acquire pbwm lock.
        ShouldNotConflictWithSecondaryBatchApplicationBlock noPBWMBlock(opCtx->lockState());
        persistStatus =
            indexbuildentryhelpers::persistCommitReadyMemberInfo(opCtx, indexbuildEntry);
    }

    if (persistStatus.isOK()) {
        _signalIfCommitQuorumIsSatisfied(opCtx, replState);
    }
    return persistStatus;
}

void IndexBuildsCoordinatorMongod::_sendCommitQuorumSatisfiedSignal(
    OperationContext* opCtx, std::shared_ptr<ReplIndexBuildState> replState) {
    replState->setCommitQuorumSatisfied(opCtx);
}

bool IndexBuildsCoordinatorMongod::_signalIfCommitQuorumIsSatisfied(
    OperationContext* opCtx, std::shared_ptr<ReplIndexBuildState> replState) {

    // Acquire the commitQuorumLk in shared mode to make sure commit quorum value did not change
    // after reading it from config.system.indexBuilds collection.
    Lock::SharedLock commitQuorumLk(opCtx, *replState->commitQuorumLock);

    // Read the index builds entry from config.system.indexBuilds collection.
    auto swIndexBuildEntry =
        indexbuildentryhelpers::getIndexBuildEntry(opCtx, replState->buildUUID);
    auto indexBuildEntry = uassertStatusOK(swIndexBuildEntry);

    auto voteMemberList = indexBuildEntry.getCommitReadyMembers();
    // This can occur when no vote got received and stepup tries to check if commit quorum is
    // satisfied.
    if (!voteMemberList)
        return false;

    bool commitQuorumSatisfied = repl::ReplicationCoordinator::get(opCtx)->isCommitQuorumSatisfied(
        indexBuildEntry.getCommitQuorum(), voteMemberList.value());

    if (!commitQuorumSatisfied)
        return false;

    LOGV2(
        3856201, "Index build: commit quorum satisfied", "indexBuildEntry"_attr = indexBuildEntry);
    _sendCommitQuorumSatisfiedSignal(opCtx, replState);
    return true;
}

bool IndexBuildsCoordinatorMongod::_signalIfCommitQuorumNotEnabled(
    OperationContext* opCtx, std::shared_ptr<ReplIndexBuildState> replState) {
    auto replCoord = repl::ReplicationCoordinator::get(opCtx);

    if (IndexBuildProtocol::kSinglePhase == replState->protocol) {
        replState->setSinglePhaseCommit(opCtx);
        return true;
    }

    invariant(IndexBuildProtocol::kTwoPhase == replState->protocol);

    const NamespaceStringOrUUID dbAndUUID(replState->dbName, replState->collectionUUID);
    repl::ReplicationStateTransitionLockGuard rstl(opCtx, MODE_IX);

    // Secondaries should always try to vote even if the commit quorum is disabled. Secondaries
    // must not read the on-disk commit quorum value as it may not be present at all times, such as
    // during initial sync.
    if (!replCoord->canAcceptWritesFor(opCtx, dbAndUUID)) {
        return false;
    }

    // Acquire the commitQuorumLk in shared mode to make sure commit quorum value did not change
    // after reading it from config.system.indexBuilds collection.
    Lock::SharedLock commitQuorumLk(opCtx, *replState->commitQuorumLock);

    // Read the commit quorum value from config.system.indexBuilds collection.
    auto commitQuorum = uassertStatusOKWithContext(
        indexbuildentryhelpers::getCommitQuorum(opCtx, replState->buildUUID),
        str::stream() << "failed to get commit quorum before committing index build: "
                      << replState->buildUUID);

    // Check if the commit quorum is disabled for the index build.
    if (commitQuorum.numNodes != CommitQuorumOptions::kDisabled) {
        return false;
    }

    _sendCommitQuorumSatisfiedSignal(opCtx, replState);
    return true;
}

void IndexBuildsCoordinatorMongod::_signalPrimaryForAbortAndWaitForExternalAbort(
    OperationContext* opCtx, ReplIndexBuildState* replState, const Status& abortStatus) {

    hangIndexBuildBeforeTransitioningReplStateTokAwaitPrimaryAbort.pauseWhileSet(opCtx);

    LOGV2(7419402,
          "Index build: signaling primary to abort index build",
          "buildUUID"_attr = replState->buildUUID,
          logAttrs(replState->dbName),
          "collectionUUID"_attr = replState->collectionUUID,
          "reason"_attr = abortStatus);
    const auto transitionedToWaitForAbort = replState->requestAbortFromPrimary(abortStatus);

    if (!transitionedToWaitForAbort) {
        // The index build has likely been aborted externally (e.g. its underlying collection was
        // dropped), and it's in the midst of tearing down. There's nothing else to do here.
        LOGV2(7530800,
              "Index build: the build is already in aborted state; not signaling primary to abort",
              "buildUUID"_attr = replState->buildUUID,
              "db"_attr = replState->dbName,
              "collectionUUID"_attr = replState->collectionUUID,
              "reason"_attr = abortStatus);
        return;
    }

    hangIndexBuildBeforeSignalingPrimaryForAbort.pauseWhileSet(opCtx);

    // The abort command might loop back to the same node, resulting in the command killing the
    // index builder thread (current), causing the cancellation of the command callback handle due
    // to the opCtx being interrupted. De-registering the callback due to cancellation means the
    // command did actually abort the index, so it is a non issue to just let it be cancelled. This
    // might be reflected in the logs as a cancelled command request, even if the command did
    // actually abort the build.
    const auto reason = replState->getAbortReason();

    const auto generateCmd = [reason](const UUID& uuid, const std::string& address) {
        return BSON("voteAbortIndexBuild" << uuid << "hostAndPort" << address << "reason" << reason
                                          << "writeConcern"
                                          << BSON("w"
                                                  << "majority"));
    };

    const auto checkVoteAbortIndexCmdDone = [](const BSONObj& response,
                                               const UUID& indexBuildUUID) {
        auto commandStatus = getStatusFromCommandResult(response);
        auto wcStatus = getWriteConcernStatusFromCommandResult(response);
        if (commandStatus.isOK() && wcStatus.isOK()) {
            return true;
        }
        if (!commandStatus.isA<ErrorCategory::NotPrimaryError>()) {
            // If the index build failed to abort on the primary, retrying won't solve the issue.
            // This may only happen if the build cannot be aborted because it is being committed, or
            // because it is already aborted or committed, meaning an active index build is not
            // registered. In any of these cases, the primary will eventually replicate either a
            // 'commitIndexBuild' or 'abortIndexBuild' oplog entry. So it is safe to just wait for
            // the next action after the vote request is done.
            LOGV2(7329400,
                  "Index build: 'voteAbortIndexBuild' command failed, index build was not found or "
                  "is in the process of committing. The command won't be retried.",
                  "buildUUID"_attr = indexBuildUUID,
                  "responseStatus"_attr = response);
            return true;
        }

        // We should retry if the error is due to primary change.
        LOGV2(7329401,
              "Index build: 'voteAbortIndexBuild' command failed due to primary change and will be "
              "retried",
              "buildUUID"_attr = indexBuildUUID,
              "responseStatus"_attr = response);
        return false;
    };

    runVoteCommand(opCtx, replState, generateCmd, checkVoteAbortIndexCmdDone);

    // Wait until the index build is externally aborted in this node, either through loopback or
    // replication, causing the index building thread to be interrupted, or the promise to be
    // fulfilled. Cleanup is done by the async command or oplog applier thread. If a
    // 'commitIndexBuild' is replicated in this state, the secondary will crash.
    try {
        if (!replState->getNextActionFuture().isReady()) {
            LOGV2(7329406,
                  "Index build: waiting for primary to abort the index build after "
                  "'voteAbortIndexBuild' request",
                  "buildUUID"_attr = replState->buildUUID);
            replState->getNextActionFuture().wait(opCtx);
        }
        // The promise was fullfilled before waiting.
        return;
    } catch (const DBException& ex) {
        if (ex.code() == ErrorCodes::IndexBuildAborted) {
            // The build was aborted, and the opCtx interrupted, before the thread checked the
            // future.
            return;
        }
        throw;
    }
}

void IndexBuildsCoordinatorMongod::_signalPrimaryForCommitReadiness(
    OperationContext* opCtx, std::shared_ptr<ReplIndexBuildState> replState) {
    // Before voting see if we are eligible to skip voting and signal
    // to commit index build if the node is primary.
    if (_signalIfCommitQuorumNotEnabled(opCtx, replState)) {
        LOGV2(7568001,
              "Index build: skipping vote for commit readiness",
              "buildUUID"_attr = replState->buildUUID,
              logAttrs(replState->dbName),
              "collectionUUID"_attr = replState->collectionUUID);
        return;
    }

    LOGV2(7568000,
          "Index build: vote for commit readiness",
          "buildUUID"_attr = replState->buildUUID,
          logAttrs(replState->dbName),
          "collectionUUID"_attr = replState->collectionUUID);

    // Indicate that the index build in this node has already tried to vote for commit readiness.
    // We do not try to determine whether the vote has actually succeeded or not, as it is
    // challenging due to the asynchronous request and potential concurrent interrupts. After this
    // point, the node cannot vote to abort this index build, and if it needs to abort the index
    // build it must try to do so independently. Meaning, as a primary it will succeed, but as a
    // secondary it will fassert.
    replState->setVotedForCommitReadiness(opCtx);

    const auto generateCmd = [](const UUID& uuid, const std::string& address) {
        return BSON("voteCommitIndexBuild" << uuid << "hostAndPort" << address << "writeConcern"
                                           << BSON("w"
                                                   << "majority"));
    };

    const auto checkVoteCommitIndexCmdSucceeded = [](const BSONObj& response,
                                                     const UUID& indexBuildUUID) {
        // Command error and write concern error have to be retried.
        auto commandStatus = getStatusFromCommandResult(response);
        auto wcStatus = getWriteConcernStatusFromCommandResult(response);
        if (commandStatus.isOK() && wcStatus.isOK()) {
            return true;
        }
        LOGV2(3856202,
              "'voteCommitIndexBuild' command failed.",
              "indexBuildUUID"_attr = indexBuildUUID,
              "responseStatus"_attr = response);
        return false;
    };

    runVoteCommand(opCtx, replState.get(), generateCmd, checkVoteCommitIndexCmdSucceeded);

    if (MONGO_unlikely(hangIndexBuildAfterSignalPrimaryForCommitReadiness.shouldFail())) {
        LOGV2(4841707, "Hanging index build after signaling the primary for commit readiness");
        hangIndexBuildAfterSignalPrimaryForCommitReadiness.pauseWhileSet(opCtx);
    }
    return;
}

IndexBuildAction IndexBuildsCoordinatorMongod::_drainSideWritesUntilNextActionIsAvailable(
    OperationContext* opCtx, std::shared_ptr<ReplIndexBuildState> replState) {
    auto future = replState->getNextActionFuture();

    // Waits until the promise is fulfilled or the deadline expires.
    IndexBuildAction nextAction;
    auto waitUntilNextActionIsReady = [&]() {
        auto deadline = Date_t::now() + Milliseconds(1000);
        auto timeoutError = opCtx->getTimeoutError();

        try {
            nextAction =
                opCtx->runWithDeadline(deadline, timeoutError, [&] { return future.get(opCtx); });
        } catch (const ExceptionForCat<ErrorCategory::ExceededTimeLimitError>& e) {
            if (e.code() == timeoutError) {
                return false;
            }
            throw;
        }
        return true;
    };

    // Continuously drain incoming writes until the future is ready. This is an optimization that
    // allows the critical section of committing, which must drain the remainder of the side writes,
    // to be as short as possible.
    while (!waitUntilNextActionIsReady()) {
        _insertKeysFromSideTablesWithoutBlockingWrites(opCtx, replState);
    }
    return nextAction;
}

void IndexBuildsCoordinatorMongod::_waitForNextIndexBuildActionAndCommit(
    OperationContext* opCtx,
    std::shared_ptr<ReplIndexBuildState> replState,
    const IndexBuildOptions& indexBuildOptions) {
    LOGV2(3856203,
          "Index build: waiting for next action before completing final phase",
          "buildUUID"_attr = replState->buildUUID);

    while (true) {
        // Future wait should hold no locks.
        invariant(!opCtx->lockState()->isLocked(),
                  str::stream() << "holding locks while waiting for commit or abort: "
                                << replState->buildUUID);

        auto const nextAction = [&] {
            indexBuildsSSS.waitForCommitQuorum.addAndFetch(1);
            // Future wait can be interrupted.
            return _drainSideWritesUntilNextActionIsAvailable(opCtx, replState);
        }();
        LOGV2(3856204,
              "Index build: received signal",
              "buildUUID"_attr = replState->buildUUID,
              "action"_attr = indexBuildActionToString(nextAction));

        // Tenant migration abort should have been re-written as primary abort before reaching here.
        invariant(nextAction != IndexBuildAction::kTenantMigrationAbort);

        // If the index build was aborted, this serves as a final interruption point. Since the
        // index builder thread is interrupted before the action is set, this must fail if the build
        // was aborted.
        opCtx->checkForInterrupt();

        bool needsToRetryWait = false;

        auto commitTimestamp = replState->getCommitTimestamp();

        switch (nextAction) {
            case IndexBuildAction::kOplogCommit: {
                invariant(replState->protocol == IndexBuildProtocol::kTwoPhase);
                invariant(!commitTimestamp.isNull(), replState->buildUUID.toString());
                LOGV2(3856205,
                      "Index build: committing from oplog entry",
                      "buildUUID"_attr = replState->buildUUID,
                      "commitTimestamp"_attr = commitTimestamp,
                      "collectionUUID"_attr = replState->collectionUUID);
                break;
            }
            case IndexBuildAction::kCommitQuorumSatisfied: {
                invariant(commitTimestamp.isNull(),
                          str::stream() << "commit ts: " << commitTimestamp.toString()
                                        << "; index build: " << replState->buildUUID.toString());
                break;
            }
            case IndexBuildAction::kSinglePhaseCommit:
                invariant(replState->protocol == IndexBuildProtocol::kSinglePhase,
                          str::stream() << "commit ts: " << commitTimestamp.toString()
                                        << "; index build: " << replState->buildUUID.toString());
                break;
            case IndexBuildAction::kOplogAbort:
            case IndexBuildAction::kRollbackAbort:
            case IndexBuildAction::kInitialSyncAbort:
            case IndexBuildAction::kPrimaryAbort:
            case IndexBuildAction::kTenantMigrationAbort:
                // The calling thread should have interrupted us before signaling an abort action.
                LOGV2_FATAL(4698901, "Index build abort should have interrupted this operation");
            case IndexBuildAction::kNoAction:
                return;
        }

        auto result = _insertKeysFromSideTablesAndCommit(
            opCtx, replState, nextAction, indexBuildOptions, commitTimestamp);
        switch (result) {
            case CommitResult::kNoLongerPrimary:
                invariant(nextAction != IndexBuildAction::kOplogCommit);
                // Reset the promise as the node has stepped down. Wait for the new primary to
                // coordinate the index build and send the new signal/action.
                LOGV2(3856207,
                      "No longer primary while attempting to commit. Waiting again for next action "
                      "before completing final phase",
                      "buildUUID"_attr = replState->buildUUID);
                replState->resetNextActionPromise();
                needsToRetryWait = true;
                break;
            case CommitResult::kLockTimeout:
                LOGV2(4698900,
                      "Unable to acquire RSTL for commit within deadline. Releasing locks and "
                      "trying again",
                      "buildUUID"_attr = replState->buildUUID);
                needsToRetryWait = true;
                break;
            case CommitResult::kSuccess:
                break;
        }

        if (!needsToRetryWait) {
            break;
        }
    }
}

Status IndexBuildsCoordinatorMongod::setCommitQuorum(OperationContext* opCtx,
                                                     const NamespaceString& nss,
                                                     const std::vector<StringData>& indexNames,
                                                     const CommitQuorumOptions& newCommitQuorum) {
    if (indexNames.empty()) {
        return Status(ErrorCodes::IndexNotFound,
                      str::stream()
                          << "Cannot set a new commit quorum on an index build in collection '"
                          << nss.toStringForErrorMsg() << "' without providing any indexes.");
    }

    // Take the MODE_IX lock now, so that when we actually persist the value later, we don't need to
    // upgrade the lock.
    AutoGetCollection collection(opCtx, nss, MODE_IX);
    if (!collection) {
        return Status(ErrorCodes::NamespaceNotFound,
                      str::stream()
                          << "Collection '" << nss.toStringForErrorMsg() << "' was not found.");
    }

    UUID collectionUUID = collection->uuid();
    std::shared_ptr<ReplIndexBuildState> replState;

    auto pred = [&](const auto& replState) {
        if (collectionUUID != replState.collectionUUID) {
            return false;
        }
        if (indexNames.size() != replState.indexNames.size()) {
            return false;
        }
        // Ensure the ReplIndexBuildState has the same indexes as 'indexNames'.
        return std::equal(
            replState.indexNames.begin(), replState.indexNames.end(), indexNames.begin());
    };
    auto collIndexBuilds = activeIndexBuilds.filterIndexBuilds(pred);
    if (collIndexBuilds.empty()) {
        return Status(ErrorCodes::IndexNotFound,
                      str::stream()
                          << "Cannot find an index build on collection '"
                          << nss.toStringForErrorMsg() << "' with the provided index names");
    }
    invariant(
        1U == collIndexBuilds.size(),
        str::stream() << "Found multiple index builds with the same index names on collection "
                      << nss.toStringForErrorMsg() << " (" << collectionUUID
                      << "): first index name: " << indexNames.front());

    replState = collIndexBuilds.front();

    // See if the new commit quorum is satisfiable.
    auto replCoord = repl::ReplicationCoordinator::get(opCtx);
    Status status = replCoord->checkIfCommitQuorumCanBeSatisfied(newCommitQuorum);
    if (!status.isOK()) {
        return status;
    }

    // Read the index builds entry from config.system.indexBuilds collection.
    auto swOnDiskCommitQuorum =
        indexbuildentryhelpers::getCommitQuorum(opCtx, replState->buildUUID);
    // Index build has not yet started.
    if (swOnDiskCommitQuorum == ErrorCodes::NoMatchingDocument) {
        return Status(ErrorCodes::IndexNotFound,
                      str::stream()
                          << "Index build not yet started for the provided indexes in collection '"
                          << nss.toStringForErrorMsg() << "'.");
    }

    auto currentCommitQuorum = invariantStatusOK(swOnDiskCommitQuorum);
    if (currentCommitQuorum.numNodes == CommitQuorumOptions::kDisabled ||
        newCommitQuorum.numNodes == CommitQuorumOptions::kDisabled) {
        return Status(ErrorCodes::BadValue,
                      str::stream()
                          << "Commit quorum value can be changed only for index builds "
                          << "with commit quorum enabled, nss: '" << nss.toStringForErrorMsg()
                          << "' first index name: '" << indexNames.front()
                          << "' currentCommitQuorum: " << currentCommitQuorum.toBSON()
                          << " providedCommitQuorum: " << newCommitQuorum.toBSON());
    }

    invariant(opCtx->lockState()->isRSTLLocked());
    // About to update the commit quorum value on-disk. So, take the lock in exclusive mode to
    // prevent readers from reading the commit quorum value and making decision on commit quorum
    // satisfied with the stale read commit quorum value.
    Lock::ExclusiveLock commitQuorumLk(opCtx, *replState->commitQuorumLock);
    {
        if (auto action = replState->getNextActionNoWait()) {
            return Status(ErrorCodes::CommandFailed,
                          str::stream() << "Commit quorum can't be changed as index build is "
                                           "ready to commit or abort: "
                                        << indexBuildActionToString(*action));
        }
    }

    IndexBuildEntry indexbuildEntry(
        replState->buildUUID, replState->collectionUUID, newCommitQuorum, replState->indexNames);
    status = indexbuildentryhelpers::persistIndexCommitQuorum(opCtx, indexbuildEntry);
    if (!status.isOK()) {
        return status;
    }

    // Check to see the index build hasn't received commit index build signal while updating
    // the commit quorum value on-disk.
    if (auto action = replState->getNextActionNoWait()) {
        uassert(ErrorCodes::CommandFailed,
                str::stream() << "Commit quorum is already satisfied, this command has no effect",
                *action != IndexBuildAction::kCommitQuorumSatisfied);
    }

    // If the index builder is already waiting for the commit quorum to be satisfied and the commit
    // quorum changes, we need to signal the index builder to make it aware of the change.
    _signalIfCommitQuorumIsSatisfied(opCtx, replState);

    return Status::OK();
}

Status IndexBuildsCoordinatorMongod::_finishScanningPhase() {
    // TODO: implement.
    return Status::OK();
}

Status IndexBuildsCoordinatorMongod::_finishVerificationPhase() {
    // TODO: implement.
    return Status::OK();
}

Status IndexBuildsCoordinatorMongod::_finishCommitPhase() {
    // TODO: implement.
    return Status::OK();
}

StatusWith<bool> IndexBuildsCoordinatorMongod::_checkCommitQuorum(
    const BSONObj& commitQuorum, const std::vector<HostAndPort>& confirmedMembers) {
    // TODO: not yet implemented.
    return false;
}

void IndexBuildsCoordinatorMongod::_refreshReplStateFromPersisted(OperationContext* opCtx,
                                                                  const UUID& buildUUID) {
    // TODO: not yet implemented.
}

}  // namespace mongo