summaryrefslogtreecommitdiff
path: root/src/mongo/db/serverless/shard_split_donor_service.cpp
blob: 0939a556a00268b5b54e3603c43218339d7d63fd (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
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
/**
 *    Copyright (C) 2021-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/db/serverless/shard_split_donor_service.h"

#include "mongo/client/streamable_replica_set_monitor.h"
#include "mongo/db/catalog/collection_write_path.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/concurrency/exception_util.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/dbhelpers.h"
#include "mongo/db/index_builds_coordinator.h"
#include "mongo/db/repl/repl_client_info.h"
#include "mongo/db/repl/tenant_migration_access_blocker_util.h"
#include "mongo/db/repl/wait_for_majority_service.h"
#include "mongo/db/s/resharding/resharding_util.h"
#include "mongo/db/serverless/shard_split_statistics.h"
#include "mongo/db/serverless/shard_split_utils.h"
#include "mongo/db/shard_role.h"
#include "mongo/executor/cancelable_executor.h"
#include "mongo/executor/connection_pool.h"
#include "mongo/executor/network_interface_factory.h"
#include "mongo/executor/network_interface_thread_pool.h"
#include "mongo/executor/thread_pool_task_executor.h"
#include "mongo/logv2/log.h"
#include "mongo/rpc/metadata/egress_metadata_hook_list.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/future_util.h"
#include "mongo/util/time_support.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTenantMigration


namespace mongo {

namespace {

MONGO_FAIL_POINT_DEFINE(abortShardSplitBeforeLeavingBlockingState);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitBeforeBlockingState);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitAfterBlocking);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitAfterDecision);
MONGO_FAIL_POINT_DEFINE(skipShardSplitGarbageCollectionTimeout);
MONGO_FAIL_POINT_DEFINE(skipShardSplitWaitForSplitAcceptance);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitBeforeRecipientCleanup);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitAfterMarkingStateGarbageCollectable);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitBeforeSplitConfigRemoval);
MONGO_FAIL_POINT_DEFINE(skipShardSplitRecipientCleanup);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitBeforeLeavingBlockingState);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitAfterUpdatingToCommittedState);
MONGO_FAIL_POINT_DEFINE(pauseShardSplitAfterReceivingAbortCmd);

const Backoff kExponentialBackoff(Seconds(1), Milliseconds::max());

bool isAbortedDocumentPersistent(WithLock, ShardSplitDonorDocument& stateDoc) {
    return !!stateDoc.getAbortReason();
}

void checkForTokenInterrupt(const CancellationToken& token) {
    uassert(ErrorCodes::CallbackCanceled, "Donor service interrupted", !token.isCanceled());
}

}  // namespace

namespace detail {

SemiFuture<HostAndPort> makeRecipientAcceptSplitFuture(
    std::shared_ptr<executor::TaskExecutor> taskExecutor,
    const CancellationToken& abortToken,
    const ConnectionString& recipientConnectionString,
    const UUID migrationId) {

    // build a vector of single server discovery monitors to listen for heartbeats
    auto eventsPublisher = std::make_shared<sdam::TopologyEventsPublisher>(taskExecutor);

    auto listener = std::make_shared<mongo::serverless::RecipientAcceptSplitListener>(
        recipientConnectionString);
    eventsPublisher->registerListener(listener);

    auto managerStats = std::make_shared<ReplicaSetMonitorManagerStats>();
    auto stats = std::make_shared<ReplicaSetMonitorStats>(managerStats);
    auto recipientNodes = recipientConnectionString.getServers();

    std::vector<SingleServerDiscoveryMonitorPtr> monitors;
    for (const auto& server : recipientNodes) {
        SdamConfiguration sdamConfiguration(std::vector<HostAndPort>{server});
        auto connectionString = ConnectionString::forStandalones(std::vector<HostAndPort>{server});

        monitors.push_back(
            std::make_shared<SingleServerDiscoveryMonitor>(MongoURI{connectionString},
                                                           server,
                                                           boost::none,
                                                           sdamConfiguration,
                                                           eventsPublisher,
                                                           taskExecutor,
                                                           stats));
        monitors.back()->init();
    }

    return future_util::withCancellation(listener->getSplitAcceptedFuture(), abortToken)
        .thenRunOn(taskExecutor)
        // Preserve lifetime of listener and monitor until the future is fulfilled and remove the
        // listener.
        .onCompletion(
            [monitors = std::move(monitors), listener, eventsPublisher, taskExecutor, migrationId](
                StatusWith<HostAndPort> s) {
                eventsPublisher->close();

                for (auto& monitor : monitors) {
                    monitor->shutdown();
                }

                return s;
            })
        .semi();
}

}  // namespace detail

ThreadPool::Limits ShardSplitDonorService::getThreadPoolLimits() const {
    ThreadPool::Limits limits;
    limits.maxThreads = repl::maxShardSplitDonorServiceThreadPoolSize;
    limits.minThreads = repl::minShardSplitDonorServiceThreadPoolSize;
    return limits;
}

void ShardSplitDonorService::checkIfConflictsWithOtherInstances(
    OperationContext* opCtx,
    BSONObj initialState,
    const std::vector<const repl::PrimaryOnlyService::Instance*>& existingInstances) {
    auto stateDoc = ShardSplitDonorDocument::parse(IDLParserContext("donorStateDoc"), initialState);

    for (auto& instance : existingInstances) {
        auto existingTypedInstance =
            checked_cast<const ShardSplitDonorService::DonorStateMachine*>(instance);
        bool isGarbageCollectable = existingTypedInstance->isGarbageCollectable();
        bool existingIsAborted =
            existingTypedInstance->getStateDocState() == ShardSplitDonorStateEnum::kAborted &&
            isGarbageCollectable;

        uassert(ErrorCodes::ConflictingOperationInProgress,
                str::stream() << "Can't start a concurent shard split operation, currently running"
                              << " migrationId: " << existingTypedInstance->getId(),
                existingIsAborted);
    }
}

std::shared_ptr<repl::PrimaryOnlyService::Instance> ShardSplitDonorService::constructInstance(
    BSONObj initialState) {
    return std::make_shared<DonorStateMachine>(
        _serviceContext,
        this,
        ShardSplitDonorDocument::parse(IDLParserContext("donorStateDoc"), initialState));
}

void ShardSplitDonorService::abortAllSplits(OperationContext* opCtx) {
    LOGV2(8423361, "Aborting all active shard split operations.");
    auto instances = getAllInstances(opCtx);
    for (auto& instance : instances) {
        auto typedInstance =
            checked_pointer_cast<ShardSplitDonorService::DonorStateMachine>(instance);
        typedInstance->tryAbort();
    }
}

boost::optional<TaskExecutorPtr>
    ShardSplitDonorService::DonorStateMachine::_splitAcceptanceTaskExecutorForTest;
ShardSplitDonorService::DonorStateMachine::DonorStateMachine(
    ServiceContext* serviceContext,
    ShardSplitDonorService* splitService,
    const ShardSplitDonorDocument& initialState)
    : repl::PrimaryOnlyService::TypedInstance<DonorStateMachine>(),
      _migrationId(initialState.getId()),
      _serviceContext(serviceContext),
      _shardSplitService(splitService),
      _stateDoc(initialState),
      _markKilledExecutor(std::make_shared<ThreadPool>([] {
          ThreadPool::Options options;
          options.poolName = "ShardSplitCancelableOpCtxPool";
          options.minThreads = 1;
          options.maxThreads = 1;
          return options;
      }())) {}

void ShardSplitDonorService::DonorStateMachine::tryAbort() {
    LOGV2(6086502, "Received 'abortShardSplit' command.", "id"_attr = _migrationId);
    {
        stdx::lock_guard<Latch> lg(_mutex);
        _abortRequested = true;
        if (_abortSource) {
            _abortSource->cancel();
        }
    }
    pauseShardSplitAfterReceivingAbortCmd.pauseWhileSet();
}

void ShardSplitDonorService::DonorStateMachine::tryForget() {
    LOGV2(6236601, "Received 'forgetShardSplit' command.", "id"_attr = _migrationId);
    stdx::lock_guard<Latch> lg(_mutex);
    if (_forgetShardSplitReceivedPromise.getFuture().isReady()) {
        return;
    }

    _forgetShardSplitReceivedPromise.emplaceValue();
}

void ShardSplitDonorService::DonorStateMachine::checkIfOptionsConflict(
    const BSONObj& stateDocBson) const {
    auto stateDoc = ShardSplitDonorDocument::parse(IDLParserContext("donorStateDoc"), stateDocBson);

    stdx::lock_guard<Latch> lg(_mutex);
    invariant(stateDoc.getId() == _stateDoc.getId());

    if (_stateDoc.getTenantIds() != stateDoc.getTenantIds() ||
        _stateDoc.getRecipientTagName() != stateDoc.getRecipientTagName() ||
        _stateDoc.getRecipientSetName() != stateDoc.getRecipientSetName()) {
        uasserted(ErrorCodes::ConflictingOperationInProgress,
                  str::stream() << "Found active migration for migrationId \""
                                << _stateDoc.getId().toBSON() << "\" with different options "
                                << _stateDoc.toBSON());
    }
}

SemiFuture<void> ShardSplitDonorService::DonorStateMachine::run(
    ScopedTaskExecutorPtr executor, const CancellationToken& primaryToken) noexcept {
    auto abortToken = [&]() {
        stdx::lock_guard<Latch> lg(_mutex);
        _abortSource = CancellationSource(primaryToken);
        if (_abortRequested || _stateDoc.getState() == ShardSplitDonorStateEnum::kAborted) {
            _abortSource->cancel();
        }

        // We must abort the migration if we try to start or resume while upgrading or downgrading.
        // (Generic FCV reference): This FCV check should exist across LTS binary versions.
        if (serverGlobalParams.featureCompatibility.isUpgradingOrDowngrading()) {
            LOGV2(8423360, "Aborting shard split since donor is upgrading or downgrading.");
            _abortSource->cancel();
        }

        return _abortSource->token();
    }();

    _markKilledExecutor->startup();
    _cancelableOpCtxFactory.emplace(primaryToken, _markKilledExecutor);

    auto criticalSectionTimer = std::make_shared<Timer>();
    auto criticalSectionWithoutCatchupTimer = std::make_shared<Timer>();

    const bool shouldRemoveStateDocumentOnRecipient = [&]() {
        auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
        stdx::lock_guard<Latch> lg(_mutex);
        return serverless::shouldRemoveStateDocumentOnRecipient(opCtx.get(), _stateDoc);
    }();

    _decisionPromise.setWith([&] {
        if (shouldRemoveStateDocumentOnRecipient) {
            pauseShardSplitBeforeRecipientCleanup.pauseWhileSet();

            return ExecutorFuture(**executor)
                .then([this, executor, primaryToken, anchor = shared_from_this()] {
                    if (MONGO_unlikely(skipShardSplitRecipientCleanup.shouldFail())) {
                        return ExecutorFuture(**executor);
                    }

                    return _cleanRecipientStateDoc(executor, primaryToken);
                })
                .then([this, executor, migrationId = _migrationId]() {
                    stdx::lock_guard<Latch> lg(_mutex);
                    return DurableState{ShardSplitDonorStateEnum::kCommitted,
                                        boost::none,
                                        _stateDoc.getBlockOpTime()};
                })
                .unsafeToInlineFuture();
        }

        LOGV2(6086506,
              "Starting shard split.",
              "id"_attr = _migrationId,
              "timeout"_attr = repl::shardSplitTimeoutMS.load());

        auto isConfigValidWithStatus = [&]() {
            stdx::lock_guard<Latch> lg(_mutex);
            auto replCoord = repl::ReplicationCoordinator::get(cc().getServiceContext());
            invariant(replCoord);
            return serverless::validateRecipientNodesForShardSplit(_stateDoc,
                                                                   replCoord->getConfig());
        }();

        if (!isConfigValidWithStatus.isOK()) {
            stdx::lock_guard<Latch> lg(_mutex);

            LOGV2_ERROR(6395900,
                        "Failed to validate recipient nodes for shard split.",
                        "id"_attr = _migrationId,
                        "status"_attr = isConfigValidWithStatus);

            _abortReason = isConfigValidWithStatus;
        }

        _initiateTimeout(executor, abortToken);
        return ExecutorFuture(**executor)
            .then([this, executor, primaryToken, abortToken] {
                // Note we do not use the abort split token here because the abortShardSplit
                // command waits for a decision to be persisted which will not happen if
                // inserting the initial state document fails.
                return _enterAbortIndexBuildsOrAbortedState(executor, primaryToken, abortToken);
            })
            .then([this, executor, abortToken] {
                // Start tracking the abortToken for killing operation contexts
                _cancelableOpCtxFactory.emplace(abortToken, _markKilledExecutor);
                return _abortIndexBuildsAndEnterBlockingState(executor, abortToken);
            })
            .then([this, executor, abortToken, criticalSectionTimer] {
                criticalSectionTimer->reset();

                auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
                pauseShardSplitAfterBlocking.pauseWhileSet(opCtx.get());

                return _waitForRecipientToReachBlockOpTime(executor, abortToken);
            })
            .then([this, executor, abortToken, criticalSectionWithoutCatchupTimer] {
                criticalSectionWithoutCatchupTimer->reset();
                return _applySplitConfigToDonor(executor, abortToken);
            })
            .then([this, executor, primaryToken, abortToken] {
                return _waitForSplitAcceptanceAndEnterCommittedState(
                    executor, primaryToken, abortToken);
            })
            // anchor ensures the instance will still exists even if the primary stepped down
            .onCompletion([this,
                           executor,
                           primaryToken,
                           abortToken,
                           criticalSectionTimer,
                           criticalSectionWithoutCatchupTimer,
                           anchor = shared_from_this()](Status status) {
                // only cancel operations on stepdown from here out
                _cancelableOpCtxFactory.emplace(primaryToken, _markKilledExecutor);

                {
                    stdx::lock_guard<Latch> lg(_mutex);
                    if (!_stateDoc.getExpireAt()) {
                        if (_abortReason) {
                            ShardSplitStatistics::get(_serviceContext)->incrementTotalAborted();
                        } else {
                            ShardSplitStatistics::get(_serviceContext)
                                ->incrementTotalCommitted(
                                    Milliseconds{criticalSectionTimer->millis()},
                                    Milliseconds{criticalSectionWithoutCatchupTimer->millis()});
                        }
                    }
                }

                if (!status.isOK()) {
                    return _handleErrorOrEnterAbortedState(
                        status, executor, primaryToken, abortToken);
                }

                LOGV2(6236700,
                      "Shard split decision reached",
                      "id"_attr = _migrationId,
                      "state"_attr = ShardSplitDonorState_serializer(_stateDoc.getState()));

                stdx::lock_guard<Latch> lg(_mutex);
                return ExecutorFuture(
                    **executor,
                    DurableState{_stateDoc.getState(), _abortReason, _stateDoc.getBlockOpTime()});
            })
            .unsafeToInlineFuture();
    });

    _garbageCollectablePromise.setWith([&] {
        if (shouldRemoveStateDocumentOnRecipient) {
            return ExecutorFuture(**executor)
                .then([&] { return _decisionPromise.getFuture().semi().ignoreValue(); })
                .unsafeToInlineFuture();
        }

        return ExecutorFuture(**executor)
            .then([&] { return _decisionPromise.getFuture().semi().ignoreValue(); })
            .then([this, executor, primaryToken]() {
                // Always remove the split config after the split decision
                auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
                pauseShardSplitBeforeSplitConfigRemoval.pauseWhileSetAndNotCanceled(opCtx.get(),
                                                                                    primaryToken);
                return _removeSplitConfigFromDonor(executor, primaryToken);
            })
            .then([this, executor, primaryToken] {
                auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
                pauseShardSplitAfterDecision.pauseWhileSet(opCtx.get());

                return _waitForForgetCmdThenMarkGarbageCollectable(executor, primaryToken);
            })
            .unsafeToInlineFuture();
    });

    _completionPromise.setWith([&] {
        if (shouldRemoveStateDocumentOnRecipient) {
            return ExecutorFuture(**executor)
                .then([&] { return _garbageCollectablePromise.getFuture().semi().ignoreValue(); })
                .onCompletion(
                    [this, executor, anchor = shared_from_this(), primaryToken](Status status) {
                        if (!status.isOK()) {
                            LOGV2_ERROR(6753100,
                                        "Failed to cleanup the state document on recipient nodes",
                                        "id"_attr = _migrationId,
                                        "abortReason"_attr = _abortReason,
                                        "status"_attr = status);
                        } else {
                            LOGV2(6753101,
                                  "Successfully cleaned up the state document on recipient nodes.",
                                  "id"_attr = _migrationId,
                                  "abortReason"_attr = _abortReason,
                                  "status"_attr = status);
                        }
                    })
                .unsafeToInlineFuture();
        }

        return ExecutorFuture(**executor)
            .then([&] { return _garbageCollectablePromise.getFuture().semi().ignoreValue(); })
            .then([this, executor, primaryToken] {
                return _waitForGarbageCollectionTimeoutThenDeleteStateDoc(executor, primaryToken);
            })
            .then([this, executor, primaryToken, anchor = shared_from_this()] {
                stdx::lock_guard<Latch> lg(_mutex);
                LOGV2(8423356,
                      "Shard split completed.",
                      "id"_attr = _stateDoc.getId(),
                      "abortReason"_attr = _abortReason);
            })
            .unsafeToInlineFuture();
    });

    return _completionPromise.getFuture().semi();
}

void ShardSplitDonorService::DonorStateMachine::interrupt(Status status) {}

boost::optional<BSONObj> ShardSplitDonorService::DonorStateMachine::reportForCurrentOp(
    MongoProcessInterface::CurrentOpConnectionsMode connMode,
    MongoProcessInterface::CurrentOpSessionsMode sessionMode) noexcept {

    stdx::lock_guard<Latch> lg(_mutex);
    BSONObjBuilder bob;
    bob.append("desc", "shard split operation");
    _migrationId.appendToBuilder(&bob, "instanceID"_sd);
    bob.append("reachedDecision", _decisionPromise.getFuture().isReady());
    if (_stateDoc.getExpireAt()) {
        bob.append("expireAt", *_stateDoc.getExpireAt());
    }
    const auto& tenantIds = _stateDoc.getTenantIds();
    if (tenantIds) {
        std::vector<std::string> tenantIdsAsStrings;
        for (const auto& tid : *tenantIds) {
            tenantIdsAsStrings.push_back(tid.toString());
        }
        bob.append("tenantIds", tenantIdsAsStrings);
    }
    if (_stateDoc.getBlockOpTime()) {
        _stateDoc.getBlockOpTime()->append(&bob, "blockOpTime");
    }
    if (_stateDoc.getCommitOrAbortOpTime()) {
        _stateDoc.getCommitOrAbortOpTime()->append(&bob, "commitOrAbortOpTime");
    }
    if (_stateDoc.getAbortReason()) {
        bob.append("abortReason", *_stateDoc.getAbortReason());
    }
    if (_stateDoc.getRecipientConnectionString()) {
        bob.append("recipientConnectionString",
                   _stateDoc.getRecipientConnectionString()->toString());
    }
    if (_stateDoc.getRecipientSetName()) {
        bob.append("recipientSetName", *_stateDoc.getRecipientSetName());
    }
    if (_stateDoc.getRecipientTagName()) {
        bob.append("recipientTagName", *_stateDoc.getRecipientTagName());
    }

    return bob.obj();
}

bool ShardSplitDonorService::DonorStateMachine::_hasInstalledSplitConfig(WithLock lock) {
    auto replCoord = repl::ReplicationCoordinator::get(cc().getServiceContext());
    auto config = replCoord->getConfig();

    invariant(_stateDoc.getRecipientSetName());
    return config.isSplitConfig() &&
        config.getRecipientConfig()->getReplSetName() == *_stateDoc.getRecipientSetName();
}

ConnectionString ShardSplitDonorService::DonorStateMachine::_setupAcceptanceMonitoring(
    WithLock lock, const CancellationToken& abortToken) {
    auto recipientConnectionString = [stateDoc = _stateDoc]() {
        if (stateDoc.getRecipientConnectionString()) {
            return *stateDoc.getRecipientConnectionString();
        }

        auto recipientTagName = stateDoc.getRecipientTagName();
        invariant(recipientTagName);
        auto recipientSetName = stateDoc.getRecipientSetName();
        invariant(recipientSetName);
        auto config = repl::ReplicationCoordinator::get(cc().getServiceContext())->getConfig();
        return serverless::makeRecipientConnectionString(
            config, *recipientTagName, *recipientSetName);
    }();

    // Always start the replica set monitor if we haven't reached a decision yet
    _splitAcceptancePromise.setWith([&]() {
        if (_stateDoc.getState() > ShardSplitDonorStateEnum::kBlocking ||
            MONGO_unlikely(skipShardSplitWaitForSplitAcceptance.shouldFail())) {
            return Future<HostAndPort>::makeReady(StatusWith<HostAndPort>(HostAndPort{}));
        }

        // Optionally select a task executor for unit testing
        auto executor = _splitAcceptanceTaskExecutorForTest
            ? *_splitAcceptanceTaskExecutorForTest
            : _shardSplitService->getInstanceCleanupExecutor();

        LOGV2(6142508,
              "Monitoring recipient nodes for split acceptance.",
              "id"_attr = _migrationId,
              "recipientConnectionString"_attr = recipientConnectionString);

        return detail::makeRecipientAcceptSplitFuture(
                   executor, abortToken, recipientConnectionString, _migrationId)
            .unsafeToInlineFuture();
    });

    return recipientConnectionString;
}

ExecutorFuture<void>
ShardSplitDonorService::DonorStateMachine::_enterAbortIndexBuildsOrAbortedState(
    const ScopedTaskExecutorPtr& executor,
    const CancellationToken& primaryToken,
    const CancellationToken& abortToken) {
    ShardSplitDonorStateEnum nextState;
    {
        stdx::lock_guard<Latch> lg(_mutex);
        if (_stateDoc.getState() == ShardSplitDonorStateEnum::kAborted || _abortReason) {
            if (isAbortedDocumentPersistent(lg, _stateDoc)) {
                // Node has step up and created an instance using a document in abort state. No
                // need to write the document as it already exists.
                _abortReason = mongo::resharding::getStatusFromAbortReason(_stateDoc);

                return ExecutorFuture(**executor);
            }

            if (!_abortReason) {
                _abortReason =
                    Status(ErrorCodes::TenantMigrationAborted, "Aborted due to 'abortShardSplit'.");
            }
            BSONObjBuilder bob;
            _abortReason->serializeErrorToBSON(&bob);
            _stateDoc.setAbortReason(bob.obj());
            _stateDoc.setExpireAt(_serviceContext->getFastClockSource()->now() +
                                  Milliseconds{repl::shardSplitGarbageCollectionDelayMS.load()});
            nextState = ShardSplitDonorStateEnum::kAborted;

            LOGV2(6670500, "Entering 'aborted' state.", "id"_attr = _stateDoc.getId());
        } else {
            // Always set up acceptance monitoring.
            auto recipientConnectionString = _setupAcceptanceMonitoring(lg, abortToken);

            if (_stateDoc.getState() > ShardSplitDonorStateEnum::kUninitialized) {
                // Node has stepped up and resumed a shard split. No need to write the document as
                // it already exists.
                return ExecutorFuture(**executor);
            }

            _stateDoc.setRecipientConnectionString(recipientConnectionString);
            nextState = ShardSplitDonorStateEnum::kAbortingIndexBuilds;

            LOGV2(
                6670501, "Entering 'aborting index builds' state.", "id"_attr = _stateDoc.getId());
        }
    }

    return _updateStateDocument(executor, primaryToken, nextState)
        .then([this, executor, primaryToken](repl::OpTime opTime) {
            return _waitForMajorityWriteConcern(executor, std::move(opTime), primaryToken);
        })
        .then([this, executor, nextState]() {
            uassert(ErrorCodes::TenantMigrationAborted,
                    "Shard split operation aborted.",
                    nextState != ShardSplitDonorStateEnum::kAborted);
        });
}

ExecutorFuture<void>
ShardSplitDonorService::DonorStateMachine::_abortIndexBuildsAndEnterBlockingState(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& abortToken) {
    checkForTokenInterrupt(abortToken);

    boost::optional<std::vector<TenantId>> tenantIds;
    {
        stdx::lock_guard<Latch> lg(_mutex);
        if (_stateDoc.getState() > ShardSplitDonorStateEnum::kAbortingIndexBuilds) {
            return ExecutorFuture(**executor);
        }

        tenantIds = _stateDoc.getTenantIds();
        invariant(tenantIds);
    }

    LOGV2(6436100, "Aborting index builds for shard split.", "id"_attr = _migrationId);

    // Abort any in-progress index builds. No new index builds can start while we are doing this
    // because the mtab prevents it.
    auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
    auto* indexBuildsCoordinator = IndexBuildsCoordinator::get(opCtx.get());
    for (const auto& tenantId : *tenantIds) {
        indexBuildsCoordinator->abortTenantIndexBuilds(
            opCtx.get(), MigrationProtocolEnum::kMultitenantMigrations, tenantId, "shard split");
    }

    if (MONGO_unlikely(pauseShardSplitBeforeBlockingState.shouldFail())) {
        pauseShardSplitBeforeBlockingState.pauseWhileSet();
    }

    {
        stdx::lock_guard<Latch> lg(_mutex);
        LOGV2(8423358, "Entering 'blocking' state.", "id"_attr = _stateDoc.getId());
    }

    return _updateStateDocument(executor, abortToken, ShardSplitDonorStateEnum::kBlocking)
        .then([this, self = shared_from_this(), executor, abortToken](repl::OpTime opTime) {
            return _waitForMajorityWriteConcern(executor, std::move(opTime), abortToken);
        });
}

ExecutorFuture<void> ShardSplitDonorService::DonorStateMachine::_waitForRecipientToReachBlockOpTime(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& abortToken) {
    checkForTokenInterrupt(abortToken);

    stdx::lock_guard<Latch> lg(_mutex);
    if (_stateDoc.getState() > ShardSplitDonorStateEnum::kBlocking ||
        _hasInstalledSplitConfig(lg)) {
        return ExecutorFuture(**executor);
    }

    auto replCoord = repl::ReplicationCoordinator::get(cc().getServiceContext());

    // It's possible that there has been an election since the blockOpTime was recorded, so we use
    // the blockOpTime's timestamp and the current configTerm when waiting for recipient nodes to
    // reach the blockTimestamp. This is okay because these timestamps are cluster times, and so are
    // guaranteed to increase even across terms.
    invariant(_stateDoc.getBlockOpTime());
    auto blockOpTime =
        repl::OpTime(_stateDoc.getBlockOpTime()->getTimestamp(), replCoord->getConfigTerm());

    invariant(_stateDoc.getRecipientTagName());
    auto recipientTagName = *_stateDoc.getRecipientTagName();
    auto recipientNodes = serverless::getRecipientMembers(replCoord->getConfig(), recipientTagName);

    WriteConcernOptions writeConcern;
    writeConcern.w = WTags{{recipientTagName.toString(), recipientNodes.size()}};

    LOGV2(
        6177201, "Waiting for recipient nodes to reach block timestamp.", "id"_attr = _migrationId);

    return ExecutorFuture(**executor).then([this, blockOpTime, writeConcern]() {
        auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
        auto replCoord = repl::ReplicationCoordinator::get(cc().getServiceContext());
        uassertStatusOK(replCoord->awaitReplication(opCtx.get(), blockOpTime, writeConcern).status);
    });
}

ExecutorFuture<void> ShardSplitDonorService::DonorStateMachine::_applySplitConfigToDonor(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& abortToken) {
    checkForTokenInterrupt(abortToken);

    {
        stdx::lock_guard<Latch> lg(_mutex);
        if (_stateDoc.getState() >= ShardSplitDonorStateEnum::kCommitted ||
            _hasInstalledSplitConfig(lg)) {
            return ExecutorFuture(**executor);
        }
    }

    auto splitConfig = [&]() {
        stdx::lock_guard<Latch> lg(_mutex);
        invariant(_stateDoc.getRecipientSetName());
        auto recipientSetName = _stateDoc.getRecipientSetName()->toString();
        invariant(_stateDoc.getRecipientTagName());
        auto recipientTagName = _stateDoc.getRecipientTagName()->toString();

        auto replCoord = repl::ReplicationCoordinator::get(cc().getServiceContext());
        invariant(replCoord);

        return serverless::makeSplitConfig(
            replCoord->getConfig(), recipientSetName, recipientTagName);
    }();

    LOGV2(6309100,
          "Applying the split config.",
          "id"_attr = _migrationId,
          "config"_attr = splitConfig);

    return AsyncTry([this, splitConfig] {
               auto opCtxHolder = _cancelableOpCtxFactory->makeOperationContext(&cc());
               DBDirectClient client(opCtxHolder.get());
               BSONObj result;
               const bool returnValue = client.runCommand(
                   DatabaseName::kAdmin, BSON("replSetReconfig" << splitConfig.toBSON()), result);
               uassert(ErrorCodes::BadValue,
                       "Invalid return value for 'replSetReconfig' command.",
                       returnValue);
               uassertStatusOK(getStatusFromCommandResult(result));
           })
        .until([](Status status) { return status.isOK(); })
        .withBackoffBetweenIterations(kExponentialBackoff)
        .on(**executor, abortToken);
}

ExecutorFuture<void> remoteAdminCommand(TaskExecutorPtr executor,
                                        const CancellationToken& token,
                                        const HostAndPort remoteNode,
                                        const BSONObj& command) {
    return AsyncTry([executor, token, remoteNode, command] {
               executor::RemoteCommandRequest request(remoteNode, "admin", command, nullptr);
               auto hasWriteConcern = command.hasField(WriteConcernOptions::kWriteConcernField);

               return executor->scheduleRemoteCommand(request, token)
                   .then([hasWriteConcern](const auto& response) {
                       auto status = getStatusFromCommandResult(response.data);
                       if (status.isOK() && hasWriteConcern) {
                           return getWriteConcernStatusFromCommandResult(response.data);
                       }

                       return status;
                   });
           })
        .until([](Status status) { return status.isOK(); })
        .on(executor, token);
}

ExecutorFuture<void> sendStepUpToRecipient(TaskExecutorPtr executor,
                                           const CancellationToken& token,
                                           const HostAndPort recipientPrimary) {
    return remoteAdminCommand(
        executor, token, recipientPrimary, BSON("replSetStepUp" << 1 << "skipDryRun" << true));
}

ExecutorFuture<void> waitForMajorityWriteOnRecipient(TaskExecutorPtr executor,
                                                     const CancellationToken& token,
                                                     const HostAndPort recipientPrimary) {
    return remoteAdminCommand(
        executor,
        token,
        recipientPrimary,
        BSON("appendOplogNote" << 1 << "data"
                               << BSON("noop write for shard split recipient primary election" << 1)
                               << WriteConcernOptions::kWriteConcernField
                               << BSON("w" << WriteConcernOptions::kMajority)));
}

ExecutorFuture<void>
ShardSplitDonorService::DonorStateMachine::_waitForSplitAcceptanceAndEnterCommittedState(
    const ScopedTaskExecutorPtr& executor,
    const CancellationToken& primaryToken,
    const CancellationToken& abortToken) {

    checkForTokenInterrupt(abortToken);
    {
        stdx::lock_guard<Latch> lg(_mutex);
        if (_stateDoc.getState() > ShardSplitDonorStateEnum::kBlocking) {
            return ExecutorFuture(**executor);
        }
    }

    LOGV2(6142501, "Waiting for recipient to accept the split.", "id"_attr = _migrationId);

    return ExecutorFuture(**executor)
        .then([&]() { return _splitAcceptancePromise.getFuture(); })
        .then([this, executor, abortToken](const HostAndPort& recipientPrimary) {
            auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
            if (MONGO_unlikely(pauseShardSplitBeforeLeavingBlockingState.shouldFail())) {
                pauseShardSplitBeforeLeavingBlockingState.execute([&](const BSONObj& data) {
                    if (!data.hasField("blockTimeMS")) {
                        pauseShardSplitBeforeLeavingBlockingState.pauseWhileSet(opCtx.get());
                    } else {
                        const auto blockTime = Milliseconds{data.getIntField("blockTimeMS")};
                        LOGV2(8423359,
                              "Keeping shard split in blocking state.",
                              "blockTime"_attr = blockTime);
                        opCtx->sleepFor(blockTime);
                    }
                });
            }

            if (MONGO_unlikely(abortShardSplitBeforeLeavingBlockingState.shouldFail())) {
                uasserted(ErrorCodes::InternalError, "simulate a shard split error");
            }

            // If the split acceptance step was cancelled, its future will produce a default
            // constructed HostAndPort. Skipping split acceptance implies skipping triggering an
            // election.
            if (recipientPrimary.empty()) {
                return ExecutorFuture(**executor);
            }

            LOGV2(6493901,
                  "Triggering an election after recipient has accepted the split.",
                  "id"_attr = _migrationId);

            auto remoteCommandExecutor = _splitAcceptanceTaskExecutorForTest
                ? *_splitAcceptanceTaskExecutorForTest
                : **executor;

            return sendStepUpToRecipient(remoteCommandExecutor, abortToken, recipientPrimary)
                .then([this, remoteCommandExecutor, abortToken, recipientPrimary]() {
                    LOGV2(8423365,
                          "Waiting for majority commit on recipient primary",
                          "id"_attr = _migrationId);

                    return waitForMajorityWriteOnRecipient(
                        remoteCommandExecutor, abortToken, recipientPrimary);
                });
        })
        .thenRunOn(**executor)
        .then([this, executor, primaryToken]() {
            // only cancel operations on stepdown from here out
            _cancelableOpCtxFactory.emplace(primaryToken, _markKilledExecutor);

            LOGV2(6142503, "Entering 'committed' state.", "id"_attr = _stateDoc.getId());
            auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
            pauseShardSplitAfterUpdatingToCommittedState.pauseWhileSet(opCtx.get());

            return _updateStateDocument(
                       executor, primaryToken, ShardSplitDonorStateEnum::kCommitted)
                .then([this, executor, primaryToken](repl::OpTime opTime) {
                    return _waitForMajorityWriteConcern(executor, std::move(opTime), primaryToken);
                });
        });
}

ExecutorFuture<repl::OpTime> ShardSplitDonorService::DonorStateMachine::_updateStateDocument(
    const ScopedTaskExecutorPtr& executor,
    const CancellationToken& token,
    ShardSplitDonorStateEnum nextState) {
    auto [isInsert, originalStateDocBson] = [&]() {
        stdx::lock_guard<Latch> lg(_mutex);
        auto currentState = _stateDoc.getState();
        auto isInsert = currentState == ShardSplitDonorStateEnum::kUninitialized ||
            currentState == ShardSplitDonorStateEnum::kAborted;
        return std::make_tuple(isInsert, _stateDoc.toBSON());
    }();

    return AsyncTry([this,
                     isInsert = isInsert,
                     originalStateDocBson = originalStateDocBson,
                     uuid = _migrationId,
                     nextState] {
               auto opCtxHolder = _cancelableOpCtxFactory->makeOperationContext(&cc());
               auto opCtx = opCtxHolder.get();

               auto collection =
                   acquireCollection(opCtx,
                                     CollectionAcquisitionRequest(
                                         _stateDocumentsNS,
                                         PlacementConcern{boost::none, ShardVersion::UNSHARDED()},
                                         repl::ReadConcernArgs::get(opCtx),
                                         AcquisitionPrerequisites::kWrite),
                                     MODE_IX);

               if (!isInsert) {
                   uassert(ErrorCodes::NamespaceNotFound,
                           str::stream()
                               << _stateDocumentsNS.toStringForErrorMsg() << " does not exist",
                           collection.exists());
               }

               writeConflictRetry(
                   opCtx, "ShardSplitDonorUpdateStateDoc", _stateDocumentsNS.ns(), [&]() {
                       WriteUnitOfWork wuow(opCtx);

                       if (nextState == ShardSplitDonorStateEnum::kBlocking) {
                           // Start blocking writes before getting an oplog slot to guarantee no
                           // writes to the tenant's data can commit with a timestamp after the
                           // block timestamp.
                           auto mtabVector =
                               TenantMigrationAccessBlockerRegistry::get(opCtx->getServiceContext())
                                   .getDonorAccessBlockersForMigration(uuid);
                           invariant(!mtabVector.empty());

                           for (auto& mtab : mtabVector) {
                               invariant(mtab);
                               mtab->startBlockingWrites();

                               opCtx->recoveryUnit()->onRollback(
                                   [mtab](OperationContext*) { mtab->rollBackStartBlocking(); });
                           }
                       }

                       // Reserve an opTime for the write.
                       auto oplogSlot = LocalOplogInfo::get(opCtx)->getNextOpTimes(opCtx, 1U)[0];
                       auto updatedStateDocBson = [&]() {
                           stdx::lock_guard<Latch> lg(_mutex);
                           _stateDoc.setState(nextState);
                           switch (nextState) {
                               case ShardSplitDonorStateEnum::kUninitialized:
                               case ShardSplitDonorStateEnum::kAbortingIndexBuilds:
                                   break;
                               case ShardSplitDonorStateEnum::kBlocking:
                                   _stateDoc.setBlockOpTime(oplogSlot);
                                   break;
                               case ShardSplitDonorStateEnum::kCommitted:
                                   _stateDoc.setCommitOrAbortOpTime(oplogSlot);
                                   break;
                               case ShardSplitDonorStateEnum::kAborted: {
                                   _stateDoc.setCommitOrAbortOpTime(oplogSlot);

                                   invariant(_abortReason);
                                   BSONObjBuilder bob;
                                   _abortReason.value().serializeErrorToBSON(&bob);
                                   _stateDoc.setAbortReason(bob.obj());
                                   break;
                               }
                               default:
                                   MONGO_UNREACHABLE;
                           }
                           if (isInsert) {
                               return BSON("$setOnInsert" << _stateDoc.toBSON());
                           }

                           return _stateDoc.toBSON();
                       }();

                       auto updateOpTime = [&]() {
                           if (isInsert) {
                               const auto filter =
                                   BSON(ShardSplitDonorDocument::kIdFieldName << uuid);
                               auto updateResult = Helpers::upsert(opCtx,
                                                                   collection,
                                                                   filter,
                                                                   updatedStateDocBson,
                                                                   /*fromMigrate=*/false);

                               // '$setOnInsert' update operator can never modify an existing
                               // on-disk state doc.
                               invariant(!updateResult.existing);
                               invariant(!updateResult.numDocsModified);

                               return repl::ReplClientInfo::forClient(opCtx->getClient())
                                   .getLastOp();
                           }

                           const auto originalRecordId =
                               Helpers::findOne(opCtx,
                                                collection.getCollectionPtr(),
                                                BSON("_id" << originalStateDocBson["_id"]));
                           const auto originalSnapshot = Snapshotted<BSONObj>(
                               opCtx->recoveryUnit()->getSnapshotId(), originalStateDocBson);
                           invariant(!originalRecordId.isNull());

                           CollectionUpdateArgs args{originalSnapshot.value()};
                           args.criteria = BSON("_id" << uuid);
                           args.oplogSlots = {oplogSlot};
                           args.update = updatedStateDocBson;

                           collection_internal::updateDocument(
                               opCtx,
                               collection.getCollectionPtr(),
                               originalRecordId,
                               originalSnapshot,
                               updatedStateDocBson,
                               collection_internal::kUpdateNoIndexes,
                               nullptr /* indexesAffected */,
                               nullptr /* OpDebug* */,
                               &args);

                           return oplogSlot;
                       }();

                       wuow.commit();
                       return updateOpTime;
                   });

               return repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
           })
        .until([&](StatusWith<repl::OpTime> swOpTime) {
            if (swOpTime.getStatus().code() == ErrorCodes::ConflictingServerlessOperation) {
                LOGV2(6531509,
                      "Shard split completed due to serverless lock error",
                      "id"_attr = _migrationId,
                      "status"_attr = swOpTime.getStatus());
                stdx::lock_guard<Latch> lg(_mutex);

                uassertStatusOK(swOpTime);
            }
            return swOpTime.getStatus().isOK();
        })
        .withBackoffBetweenIterations(kExponentialBackoff)
        .on(**executor, token);
}

ExecutorFuture<void> ShardSplitDonorService::DonorStateMachine::_waitForMajorityWriteConcern(
    const ScopedTaskExecutorPtr& executor, repl::OpTime opTime, const CancellationToken& token) {
    return WaitForMajorityService::get(_serviceContext)
        .waitUntilMajority(std::move(opTime), token)
        .thenRunOn(**executor);
}

void ShardSplitDonorService::DonorStateMachine::_initiateTimeout(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& abortToken) {
    auto timeoutFuture =
        (*executor)->sleepFor(Milliseconds(repl::shardSplitTimeoutMS.load()), abortToken);

    auto timeoutOrCompletionFuture =
        whenAny(std::move(timeoutFuture),
                decisionFuture().semi().ignoreValue().thenRunOn(**executor))
            .thenRunOn(**executor)
            .then([this, executor, abortToken, anchor = shared_from_this()](auto result) {
                stdx::lock_guard<Latch> lg(_mutex);
                if (_stateDoc.getState() != ShardSplitDonorStateEnum::kCommitted &&
                    _stateDoc.getState() != ShardSplitDonorStateEnum::kAborted &&
                    !abortToken.isCanceled()) {
                    LOGV2(6236500,
                          "Timeout expired, aborting shard split.",
                          "id"_attr = _migrationId,
                          "timeout"_attr = repl::shardSplitTimeoutMS.load());
                    _abortReason = Status(ErrorCodes::ExceededTimeLimit,
                                          "Aborting shard split as it exceeded its time limit.");
                    _abortSource->cancel();
                }
            })
            .semi();
}

ExecutorFuture<ShardSplitDonorService::DonorStateMachine::DurableState>
ShardSplitDonorService::DonorStateMachine::_handleErrorOrEnterAbortedState(
    Status status,
    const ScopedTaskExecutorPtr& executor,
    const CancellationToken& primaryToken,
    const CancellationToken& abortToken) {
    ON_BLOCK_EXIT([&] {
        stdx::lock_guard<Latch> lg(_mutex);
        if (_abortSource) {
            // Cancel source to ensure all child threads (RSM monitor, etc) terminate.
            _abortSource->cancel();
        }
    });

    {
        stdx::lock_guard<Latch> lg(_mutex);
        if (isAbortedDocumentPersistent(lg, _stateDoc)) {
            // The document is already in aborted state. No need to write it.
            LOGV2(8423376,
                  "Shard split already aborted.",
                  "id"_attr = _migrationId,
                  "abortReason"_attr = _abortReason.value());

            return ExecutorFuture(**executor,
                                  DurableState{ShardSplitDonorStateEnum::kAborted,
                                               _abortReason,
                                               _stateDoc.getBlockOpTime()});
        }
    }

    if (ErrorCodes::isNotPrimaryError(status) || ErrorCodes::isShutdownError(status) ||
        status.code() == ErrorCodes::ConflictingServerlessOperation) {
        // Don't abort the split on retriable errors that may have been generated by the local
        // server shutting/stepping down because it can be resumed when the client retries.
        return ExecutorFuture(**executor, StatusWith<DurableState>{status});
    }

    // Make sure we don't change the status if the abortToken is cancelled due to a POS instance
    // interruption.
    if (abortToken.isCanceled() && !primaryToken.isCanceled()) {
        status =
            Status(ErrorCodes::TenantMigrationAborted, "Aborted due to 'abortShardSplit' command.");
    }

    {
        stdx::lock_guard<Latch> lg(_mutex);
        if (!_abortReason) {
            _abortReason = status;
        }

        BSONObjBuilder bob;
        _abortReason->serializeErrorToBSON(&bob);
        _stateDoc.setAbortReason(bob.obj());

        LOGV2(6086508,
              "Entering 'aborted' state.",
              "id"_attr = _migrationId,
              "abortReason"_attr = _abortReason.value());
    }

    return ExecutorFuture<void>(**executor)
        .then([this, executor, primaryToken] {
            return _updateStateDocument(executor, primaryToken, ShardSplitDonorStateEnum::kAborted);
        })
        .then([this, executor, primaryToken](repl::OpTime opTime) {
            return _waitForMajorityWriteConcern(executor, std::move(opTime), primaryToken);
        })
        .then([this, executor] {
            stdx::lock_guard<Latch> lg(_mutex);
            return DurableState{_stateDoc.getState(), _abortReason, _stateDoc.getBlockOpTime()};
        });
}

ExecutorFuture<void>
ShardSplitDonorService::DonorStateMachine::_waitForForgetCmdThenMarkGarbageCollectable(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& primaryToken) {
    stdx::lock_guard<Latch> lg(_mutex);
    if (_stateDoc.getExpireAt()) {
        return ExecutorFuture(**executor);
    }

    LOGV2(6236603, "Waiting to receive 'forgetShardSplit' command.", "id"_attr = _migrationId);

    return future_util::withCancellation(_forgetShardSplitReceivedPromise.getFuture(), primaryToken)
        .thenRunOn(**executor)
        .then([this, self = shared_from_this(), executor, primaryToken] {
            LOGV2(6236606, "Marking shard split as garbage-collectable.", "id"_attr = _migrationId);

            stdx::lock_guard<Latch> lg(_mutex);
            _stateDoc.setExpireAt(_serviceContext->getFastClockSource()->now() +
                                  Milliseconds{repl::shardSplitGarbageCollectionDelayMS.load()});

            return AsyncTry([this, self = shared_from_this()] {
                       auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
                       uassertStatusOK(serverless::updateStateDoc(opCtx.get(), _stateDoc));
                       return repl::ReplClientInfo::forClient(opCtx->getClient()).getLastOp();
                   })
                .until(
                    [](StatusWith<repl::OpTime> swOpTime) { return swOpTime.getStatus().isOK(); })
                .withBackoffBetweenIterations(kExponentialBackoff)
                .on(**executor, primaryToken);
        })
        .then([this, self = shared_from_this(), executor, primaryToken](repl::OpTime opTime) {
            return _waitForMajorityWriteConcern(executor, std::move(opTime), primaryToken);
        })
        .then([this, self = shared_from_this()] {
            pauseShardSplitAfterMarkingStateGarbageCollectable.pauseWhileSet();
        });
}

ExecutorFuture<void>
ShardSplitDonorService::DonorStateMachine::_waitForGarbageCollectionTimeoutThenDeleteStateDoc(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& primaryToken) {
    auto expireAt = [&]() {
        stdx::lock_guard<Latch> lg(_mutex);
        return _stateDoc.getExpireAt();
    }();

    if (!expireAt) {
        return ExecutorFuture(**executor);
    }

    if (skipShardSplitGarbageCollectionTimeout.shouldFail()) {
        LOGV2(673701, "Skipping shard split garbage collection timeout");
        return ExecutorFuture(**executor);
    }

    LOGV2(6737300,
          "Waiting until the garbage collection timeout expires",
          "id"_attr = _migrationId,
          "expireAt"_attr = *expireAt);
    return (*executor)->sleepUntil(*expireAt, primaryToken).then([this, executor, primaryToken] {
        return AsyncTry([this, executor, primaryToken] {
                   auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
                   auto deleted =
                       uassertStatusOK(serverless::deleteStateDoc(opCtx.get(), _migrationId));
                   uassert(ErrorCodes::ConflictingOperationInProgress,
                           str::stream() << "Did not find active shard split with migration id "
                                         << _migrationId,
                           deleted);
                   return repl::ReplClientInfo::forClient(opCtx.get()->getClient()).getLastOp();
               })
            .until([](StatusWith<repl::OpTime> swOpTime) { return swOpTime.getStatus().isOK(); })
            .withBackoffBetweenIterations(kExponentialBackoff)
            .on(**executor, primaryToken)
            .then([](StatusWith<repl::OpTime> swOpTime) { return swOpTime.getStatus(); });
    });
}

ExecutorFuture<void> ShardSplitDonorService::DonorStateMachine::_removeSplitConfigFromDonor(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& token) {
    checkForTokenInterrupt(token);

    auto replCoord = repl::ReplicationCoordinator::get(cc().getServiceContext());
    invariant(replCoord);

    return AsyncTry([this, replCoord] {
               auto config = replCoord->getConfig();
               if (!config.isSplitConfig()) {
                   return;
               }

               LOGV2(6573000,
                     "Reconfiguring the donor to remove the split config.",
                     "id"_attr = _migrationId,
                     "config"_attr = config);

               BSONObjBuilder newConfigBob(
                   config.toBSON().removeField("recipientConfig").removeField("version"));
               newConfigBob.append("version", config.getConfigVersion() + 1);

               auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
               DBDirectClient client(opCtx.get());

               BSONObj result;
               const bool returnValue = client.runCommand(
                   DatabaseName::kAdmin, BSON("replSetReconfig" << newConfigBob.obj()), result);
               uassert(
                   ErrorCodes::BadValue, "Invalid return value for replSetReconfig", returnValue);
               uassertStatusOK(getStatusFromCommandResult(result));
           })
        .until([](Status status) { return status.isOK(); })
        .withBackoffBetweenIterations(kExponentialBackoff)
        .on(**executor, token);
}

ExecutorFuture<void> ShardSplitDonorService::DonorStateMachine::_cleanRecipientStateDoc(
    const ScopedTaskExecutorPtr& executor, const CancellationToken& primaryToken) {
    LOGV2(6309000, "Cleaning up shard split operation on recipient.", "id"_attr = _migrationId);
    return AsyncTry([this, self = shared_from_this()] {
               auto opCtx = _cancelableOpCtxFactory->makeOperationContext(&cc());
               auto deleted =
                   uassertStatusOK(serverless::deleteStateDoc(opCtx.get(), _migrationId));
               uassert(ErrorCodes::ConflictingOperationInProgress,
                       str::stream()
                           << "Did not find active shard split with migration id " << _migrationId,
                       deleted);
               return repl::ReplClientInfo::forClient(opCtx.get()->getClient()).getLastOp();
           })
        .until([](StatusWith<repl::OpTime> swOpTime) { return swOpTime.getStatus().isOK(); })
        .withBackoffBetweenIterations(kExponentialBackoff)
        .on(**executor, primaryToken)
        .ignoreValue();
}
}  // namespace mongo