summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/rename_collection.cpp
blob: 8c171c2532f5da1229e4a8ec15c7102d6abc398d (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
/**
 *    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/db/catalog/rename_collection.h"

#include "mongo/bson/unordered_fields_bsonobj_comparator.h"
#include "mongo/db/catalog/collection_catalog.h"
#include "mongo/db/catalog/collection_uuid_mismatch.h"
#include "mongo/db/catalog/collection_write_path.h"
#include "mongo/db/catalog/database_holder.h"
#include "mongo/db/catalog/document_validation.h"
#include "mongo/db/catalog/drop_collection.h"
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/catalog/list_indexes.h"
#include "mongo/db/catalog/local_oplog_info.h"
#include "mongo/db/catalog/unique_collection_name.h"
#include "mongo/db/client.h"
#include "mongo/db/concurrency/exception_util.h"
#include "mongo/db/curop.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/index_builds_coordinator.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/op_observer/batched_write_policy.h"
#include "mongo/db/op_observer/op_observer.h"
#include "mongo/db/ops/insert.h"
#include "mongo/db/query/query_knobs_gen.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/db/server_options.h"
#include "mongo/db/service_context.h"
#include "mongo/db/shard_role.h"
#include "mongo/db/storage/storage_parameters_gen.h"
#include "mongo/logv2/log.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/namespace_string_util.h"
#include "mongo/util/scopeguard.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand

namespace mongo {
namespace {

MONGO_FAIL_POINT_DEFINE(writeConflictInRenameCollCopyToTmp);

boost::optional<NamespaceString> getNamespaceFromUUID(OperationContext* opCtx, const UUID& uuid) {
    return CollectionCatalog::get(opCtx)->lookupNSSByUUID(opCtx, uuid);
}

// From a replicated to an unreplicated collection or vice versa.
bool isReplicatedChanged(OperationContext* opCtx,
                         const NamespaceString& source,
                         const NamespaceString& target) {
    auto replCoord = repl::ReplicationCoordinator::get(opCtx);
    auto sourceIsUnreplicated = replCoord->isOplogDisabledFor(opCtx, source);
    auto targetIsUnreplicated = replCoord->isOplogDisabledFor(opCtx, target);
    return (sourceIsUnreplicated != targetIsUnreplicated);
}

Status checkSourceAndTargetNamespaces(OperationContext* opCtx,
                                      const NamespaceString& source,
                                      const NamespaceString& target,
                                      RenameCollectionOptions options,
                                      bool targetExistsAllowed) {

    auto replCoord = repl::ReplicationCoordinator::get(opCtx);
    if (opCtx->writesAreReplicated() && !replCoord->canAcceptWritesFor(opCtx, source))
        return Status(ErrorCodes::NotWritablePrimary,
                      str::stream() << "Not primary while renaming collection "
                                    << source.toStringForErrorMsg() << " to "
                                    << target.toStringForErrorMsg());

    if (isReplicatedChanged(opCtx, source, target))
        return {ErrorCodes::IllegalOperation,
                "Cannot rename collections between a replicated and an unreplicated database"};

    auto db = DatabaseHolder::get(opCtx)->getDb(opCtx, source.dbName());
    if (!db || db->isDropPending(opCtx))
        return Status(ErrorCodes::NamespaceNotFound,
                      str::stream() << "Database " << source.dbName().toStringForErrorMsg()
                                    << " does not exist or is drop pending");

    auto catalog = CollectionCatalog::get(opCtx);
    const auto sourceColl = catalog->lookupCollectionByNamespace(opCtx, source);
    if (!sourceColl) {
        if (CollectionCatalog::get(opCtx)->lookupView(opCtx, source))
            return Status(ErrorCodes::CommandNotSupportedOnView,
                          str::stream() << "cannot rename view: " << source.toStringForErrorMsg());
        return Status(ErrorCodes::NamespaceNotFound,
                      str::stream() << "Source collection " << source.toStringForErrorMsg()
                                    << " does not exist");
    }

    if (sourceColl->getCollectionOptions().encryptedFieldConfig &&
        !AuthorizationSession::get(opCtx->getClient())
             ->isAuthorizedForActionsOnResource(ResourcePattern::forClusterResource(),
                                                ActionType::setUserWriteBlockMode)) {
        return Status(ErrorCodes::IllegalOperation, "Cannot rename an encrypted collection");
    }

    IndexBuildsCoordinator::get(opCtx)->assertNoIndexBuildInProgForCollection(sourceColl->uuid());

    const auto targetColl = catalog->lookupCollectionByNamespace(opCtx, target);

    if (!targetColl) {
        if (CollectionCatalog::get(opCtx)->lookupView(opCtx, target))
            return Status(ErrorCodes::NamespaceExists,
                          str::stream() << "a view already exists with that name: "
                                        << target.toStringForErrorMsg());
    } else {
        if (targetColl->getCollectionOptions().encryptedFieldConfig &&
            !AuthorizationSession::get(opCtx->getClient())
                 ->isAuthorizedForActionsOnResource(ResourcePattern::forClusterResource(),
                                                    ActionType::setUserWriteBlockMode)) {
            return Status(ErrorCodes::IllegalOperation,
                          "Cannot rename to an existing encrypted collection");
        }

        if (!targetExistsAllowed && !options.dropTarget)
            return Status(ErrorCodes::NamespaceExists, "target namespace exists");
    }

    return Status::OK();
}

Status renameTargetCollectionToTmp(OperationContext* opCtx,
                                   const NamespaceString& sourceNs,
                                   const UUID& sourceUUID,
                                   Database* const targetDB,
                                   const NamespaceString& targetNs,
                                   const UUID& targetUUID) {
    repl::UnreplicatedWritesBlock uwb(opCtx);

    // The generated unique collection name is only guaranteed to exist if the database is
    // exclusively locked.
    invariant(opCtx->lockState()->isDbLockedForMode(targetDB->name(), LockMode::MODE_X));
    auto tmpNameResult = makeUniqueCollectionName(opCtx, targetDB->name(), "tmp%%%%%.rename");
    if (!tmpNameResult.isOK()) {
        return tmpNameResult.getStatus().withContext(
            str::stream() << "Cannot generate a temporary collection name for the target "
                          << targetNs.toStringForErrorMsg() << " (" << targetUUID
                          << ") so that the source" << sourceNs.toStringForErrorMsg() << " ("
                          << sourceUUID << ") could be renamed to "
                          << targetNs.toStringForErrorMsg());
    }
    const auto& tmpName = tmpNameResult.getValue();
    const bool stayTemp = true;
    return writeConflictRetry(opCtx, "renameCollection", targetNs, [&] {
        WriteUnitOfWork wunit(opCtx);
        auto status = targetDB->renameCollection(opCtx, targetNs, tmpName, stayTemp);
        if (!status.isOK())
            return status;

        wunit.commit();

        LOGV2(20397,
              "Successfully renamed the target {targetNs} ({targetUUID}) to {tmpName} so that the "
              "source {sourceNs} ({sourceUUID}) could be renamed to {targetNs2}",
              "Successfully renamed the target so that the source could be renamed",
              "existingTargetNamespace"_attr = targetNs,
              "existingTargetUUID"_attr = targetUUID,
              "renamedExistingTarget"_attr = tmpName,
              "sourceNamespace"_attr = sourceNs,
              "sourceUUID"_attr = sourceUUID,
              "newTargetNamespace"_attr = targetNs);

        return Status::OK();
    });
}

Status renameCollectionDirectly(OperationContext* opCtx,
                                Database* db,
                                const UUID& uuid,
                                NamespaceString source,
                                NamespaceString target,
                                RenameCollectionOptions options) {
    return writeConflictRetry(opCtx, "renameCollection", target, [&] {
        WriteUnitOfWork wunit(opCtx);

        {
            // No logOp necessary because the entire renameCollection command is one logOp.
            repl::UnreplicatedWritesBlock uwb(opCtx);
            auto status = db->renameCollection(opCtx, source, target, options.stayTemp);
            if (!status.isOK())
                return status;
        }

        // We have to override the provided 'dropTarget' setting for idempotency reasons to
        // avoid unintentionally removing a collection on a secondary with the same name as
        // the target.
        auto opObserver = opCtx->getServiceContext()->getOpObserver();
        opObserver->onRenameCollection(
            opCtx, source, target, uuid, {}, 0U, options.stayTemp, options.markFromMigrate);

        wunit.commit();
        return Status::OK();
    });
}

Status renameCollectionAndDropTarget(OperationContext* opCtx,
                                     Database* db,
                                     const UUID& uuid,
                                     NamespaceString source,
                                     NamespaceString target,
                                     const CollectionPtr& targetColl,
                                     RenameCollectionOptions options,
                                     repl::OpTime renameOpTimeFromApplyOps) {
    return writeConflictRetry(opCtx, "renameCollection", target, [&] {
        WriteUnitOfWork wunit(opCtx);

        // Target collection exists - drop it.
        invariant(options.dropTarget);

        auto replCoord = repl::ReplicationCoordinator::get(opCtx);
        auto isOplogDisabledForNamespace = replCoord->isOplogDisabledFor(opCtx, target);
        if (!isOplogDisabledForNamespace) {
            invariant(opCtx->writesAreReplicated());
            invariant(renameOpTimeFromApplyOps.isNull());
        }

        IndexBuildsCoordinator::get(opCtx)->assertNoIndexBuildInProgForCollection(
            targetColl->uuid());

        auto numRecords = targetColl->numRecords(opCtx);
        auto opObserver = opCtx->getServiceContext()->getOpObserver();

        auto renameOpTime = opObserver->preRenameCollection(opCtx,
                                                            source,
                                                            target,
                                                            uuid,
                                                            targetColl->uuid(),
                                                            numRecords,
                                                            options.stayTemp,
                                                            options.markFromMigrate);

        if (!renameOpTimeFromApplyOps.isNull()) {
            // 'renameOpTime' must be null because a valid 'renameOpTimeFromApplyOps' implies
            // replicated writes are not enabled.
            if (!renameOpTime.isNull()) {
                LOGV2_FATAL(
                    40616,
                    "renameCollection: {from} to {to} (with dropTarget=true) - unexpected "
                    "renameCollection oplog entry written to the oplog with optime {renameOpTime}",
                    "renameCollection (with dropTarget=true): unexpected renameCollection oplog "
                    "entry written to the oplog",
                    "from"_attr = source,
                    "to"_attr = target,
                    "renameOpTime"_attr = renameOpTime);
            }
            renameOpTime = renameOpTimeFromApplyOps;
        }

        // No logOp necessary because the entire renameCollection command is one logOp.
        repl::UnreplicatedWritesBlock uwb(opCtx);

        auto status = db->dropCollection(opCtx, targetColl->ns(), renameOpTime);
        if (!status.isOK())
            return status;

        status = db->renameCollection(opCtx, source, target, options.stayTemp);
        if (!status.isOK())
            return status;

        opObserver->postRenameCollection(
            opCtx, source, target, uuid, targetColl->uuid(), options.stayTemp);
        wunit.commit();
        return Status::OK();
    });
}

Status renameCollectionWithinDB(OperationContext* opCtx,
                                const NamespaceString& source,
                                const NamespaceString& target,
                                RenameCollectionOptions options) {
    invariant(source.db() == target.db());
    DisableDocumentValidation validationDisabler(opCtx);

    AutoGetDb autoDb(opCtx, source.dbName(), MODE_IX);

    boost::optional<Lock::CollectionLock> sourceLock;
    boost::optional<Lock::CollectionLock> targetLock;
    // To prevent deadlock, always lock system.views collection in the end because concurrent
    // view-related operations always lock system.views in the end.
    if (!source.isSystemDotViews() &&
        (target.isSystemDotViews() ||
         ResourceId(RESOURCE_COLLECTION, source) < ResourceId(RESOURCE_COLLECTION, target))) {
        // To prevent deadlock, always lock source and target in ascending resourceId order.
        sourceLock.emplace(opCtx, source, MODE_X);
        targetLock.emplace(opCtx, target, MODE_X);
    } else {
        targetLock.emplace(opCtx, target, MODE_X);
        sourceLock.emplace(opCtx, source, MODE_X);
    }

    auto db = DatabaseHolder::get(opCtx)->getDb(opCtx, source.dbName());
    auto catalog = CollectionCatalog::get(opCtx);
    const auto sourceColl = catalog->lookupCollectionByNamespace(opCtx, source);
    const auto targetColl = catalog->lookupCollectionByNamespace(opCtx, target);

    checkCollectionUUIDMismatch(opCtx, source, sourceColl, options.expectedSourceUUID);
    checkCollectionUUIDMismatch(opCtx, target, targetColl, options.expectedTargetUUID);

    auto status = checkSourceAndTargetNamespaces(
        opCtx, source, target, options, /* targetExistsAllowed */ false);
    if (!status.isOK())
        return status;

    AutoStatsTracker statsTracker(
        opCtx,
        source,
        Top::LockType::NotLocked,
        AutoStatsTracker::LogMode::kUpdateCurOp,
        CollectionCatalog::get(opCtx)->getDatabaseProfileLevel(source.dbName()));

    if (!targetColl) {
        return renameCollectionDirectly(opCtx, db, sourceColl->uuid(), source, target, options);
    } else {
        return renameCollectionAndDropTarget(
            opCtx, db, sourceColl->uuid(), source, target, CollectionPtr(targetColl), options, {});
    }
}

Status renameCollectionWithinDBForApplyOps(OperationContext* opCtx,
                                           const NamespaceString& source,
                                           const NamespaceString& target,
                                           const boost::optional<UUID>& uuidToDrop,
                                           repl::OpTime renameOpTimeFromApplyOps,
                                           const RenameCollectionOptions& options) {
    invariant(source.db() == target.db());
    DisableDocumentValidation validationDisabler(opCtx);

    AutoGetDb autoDb(opCtx, source.dbName(), MODE_X);

    auto status = checkSourceAndTargetNamespaces(
        opCtx, source, target, options, /* targetExistsAllowed */ true);
    if (!status.isOK())
        return status;

    auto db = DatabaseHolder::get(opCtx)->getDb(opCtx, source.dbName());
    const auto sourceColl =
        CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, source);

    AutoStatsTracker statsTracker(
        opCtx,
        source,
        Top::LockType::NotLocked,
        AutoStatsTracker::LogMode::kUpdateCurOp,
        CollectionCatalog::get(opCtx)->getDatabaseProfileLevel(source.dbName()));

    return writeConflictRetry(opCtx, "renameCollection", target, [&] {
        auto targetColl = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, target);
        WriteUnitOfWork wuow(opCtx);
        if (targetColl) {
            if (sourceColl->uuid() == targetColl->uuid()) {
                if (!uuidToDrop || uuidToDrop == targetColl->uuid()) {
                    wuow.commit();
                    return Status::OK();
                }

                // During initial sync, it is possible that the collection already
                // got renamed to the target, so there is not much left to do other
                // than drop the dropTarget. See SERVER-40861 for more details.
                auto collToDropBasedOnUUID = getNamespaceFromUUID(opCtx, *uuidToDrop);
                if (!collToDropBasedOnUUID) {
                    wuow.commit();
                    return Status::OK();
                }
                repl::UnreplicatedWritesBlock uwb(opCtx);
                Status status =
                    db->dropCollection(opCtx, *collToDropBasedOnUUID, renameOpTimeFromApplyOps);
                if (!status.isOK())
                    return status;
                wuow.commit();
                return Status::OK();
            }

            if (!uuidToDrop || uuidToDrop != targetColl->uuid()) {
                // We need to rename the targetColl to a temporary name.
                auto status = renameTargetCollectionToTmp(
                    opCtx, source, sourceColl->uuid(), db, target, targetColl->uuid());
                if (!status.isOK())
                    return status;
                targetColl = nullptr;
            }
        }

        // When reapplying oplog entries (such as in the case of initial sync) we need
        // to identify the collection to drop by UUID, as otherwise we might end up
        // dropping the wrong collection.
        if (!targetColl && uuidToDrop) {
            invariant(options.dropTarget);
            auto collToDropBasedOnUUID = getNamespaceFromUUID(opCtx, uuidToDrop.value());
            if (collToDropBasedOnUUID && !collToDropBasedOnUUID->isDropPendingNamespace()) {
                invariant(collToDropBasedOnUUID->db() == target.db());
                targetColl = CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(
                    opCtx, *collToDropBasedOnUUID);
            }
        }

        Status ret = Status::OK();
        if (!targetColl) {
            ret = renameCollectionDirectly(opCtx, db, sourceColl->uuid(), source, target, options);
        } else {
            if (sourceColl == targetColl) {
                wuow.commit();
                return Status::OK();
            }

            ret = renameCollectionAndDropTarget(opCtx,
                                                db,
                                                sourceColl->uuid(),
                                                source,
                                                target,
                                                CollectionPtr(targetColl),
                                                options,
                                                renameOpTimeFromApplyOps);
        }

        if (ret.isOK()) {
            wuow.commit();
        }

        return ret;
    });
}

Status renameCollectionAcrossDatabases(OperationContext* opCtx,
                                       const NamespaceString& source,
                                       const NamespaceString& target,
                                       const RenameCollectionOptions& options) {
    invariant(
        source.db() != target.db(),
        str::stream()
            << "cannot rename within same database (use renameCollectionWithinDB instead): source: "
            << source.toStringForErrorMsg() << "; target: " << target.toStringForErrorMsg());

    // Refer to txnCmdAllowlist in commands.cpp.
    invariant(!opCtx->inMultiDocumentTransaction(),
              str::stream() << "renameCollectionAcrossDatabases not supported in multi-document "
                               "transaction: source: "
                            << source.toStringForErrorMsg()
                            << "; target: " << target.toStringForErrorMsg());

    uassert(ErrorCodes::InvalidOptions,
            "Cannot provide an expected collection UUID when renaming across databases",
            !options.expectedSourceUUID && !options.expectedTargetUUID);

    boost::optional<Lock::DBLock> sourceDbLock;
    boost::optional<Lock::CollectionLock> sourceCollLock;
    if (!opCtx->lockState()->isCollectionLockedForMode(source, MODE_S)) {
        // Lock the DB using MODE_IX to ensure we have the global lock in that mode, as to prevent
        // upgrade from MODE_IS to MODE_IX, which caused deadlock on systems not supporting Database
        // locking and should be avoided in general.
        sourceDbLock.emplace(opCtx, source.dbName(), MODE_IX);
        sourceCollLock.emplace(opCtx, source, MODE_S);
    }

    boost::optional<Lock::DBLock> targetDBLock;
    if (!opCtx->lockState()->isDbLockedForMode(target.dbName(), MODE_X)) {
        targetDBLock.emplace(opCtx, target.dbName(), MODE_X);
    }

    DatabaseShardingState::assertMatchingDbVersion(opCtx, source.dbName());

    DisableDocumentValidation validationDisabler(opCtx);

    auto sourceDB = DatabaseHolder::get(opCtx)->getDb(opCtx, source.dbName());
    if (!sourceDB)
        return Status(ErrorCodes::NamespaceNotFound, "source namespace does not exist");

    boost::optional<AutoStatsTracker> statsTracker(
        boost::in_place_init,
        opCtx,
        source,
        Top::LockType::NotLocked,
        AutoStatsTracker::LogMode::kUpdateCurOp,
        CollectionCatalog::get(opCtx)->getDatabaseProfileLevel(source.dbName()));

    auto catalog = CollectionCatalog::get(opCtx);
    const auto sourceColl = catalog->lookupCollectionByNamespace(opCtx, source);
    if (!sourceColl) {
        if (CollectionCatalog::get(opCtx)->lookupView(opCtx, source))
            return Status(ErrorCodes::CommandNotSupportedOnView,
                          str::stream() << "cannot rename view: " << source.toStringForErrorMsg());
        return Status(ErrorCodes::NamespaceNotFound, "source namespace does not exist");
    }

    if (isReplicatedChanged(opCtx, source, target))
        return {ErrorCodes::IllegalOperation,
                "Cannot rename collections across a replicated and an unreplicated database"};

    IndexBuildsCoordinator::get(opCtx)->assertNoIndexBuildInProgForCollection(sourceColl->uuid());

    auto targetDB = DatabaseHolder::get(opCtx)->getDb(opCtx, target.dbName());

    // Check if the target namespace exists and if dropTarget is true.
    // Return a non-OK status if target exists and dropTarget is not true or if the collection
    // is sharded.
    const auto targetColl =
        targetDB ? catalog->lookupCollectionByNamespace(opCtx, target) : nullptr;
    if (targetColl) {
        if (sourceColl->uuid() == targetColl->uuid()) {
            invariant(source == target);
            return Status::OK();
        }

        if (!options.dropTarget) {
            return Status(ErrorCodes::NamespaceExists, "target namespace exists");
        }

    } else if (CollectionCatalog::get(opCtx)->lookupView(opCtx, target)) {
        return Status(ErrorCodes::NamespaceExists,
                      str::stream() << "a view already exists with that name: "
                                    << target.toStringForErrorMsg());
    }

    // Create a temporary collection in the target database. It will be removed if we fail to
    // copy the collection, or on restart, so there is no need to replicate these writes.
    if (!targetDB) {
        targetDB = DatabaseHolder::get(opCtx)->openDb(opCtx, target.dbName());
    }

    // The generated unique collection name is only guaranteed to exist if the database is
    // exclusively locked.
    invariant(opCtx->lockState()->isDbLockedForMode(targetDB->name(), LockMode::MODE_X));

    // Note that this temporary collection name is used by MongoMirror and thus must not be changed
    // without consultation.
    auto tmpNameResult =
        makeUniqueCollectionName(opCtx, target.dbName(), "tmp%%%%%.renameCollection");
    if (!tmpNameResult.isOK()) {
        return tmpNameResult.getStatus().withContext(
            str::stream() << "Cannot generate temporary collection name to rename "
                          << source.toStringForErrorMsg() << " to "
                          << target.toStringForErrorMsg());
    }
    const auto& tmpName = tmpNameResult.getValue();

    LOGV2(705520,
          "Attempting to create temporary collection",
          "temporaryCollection"_attr = tmpName,
          "sourceCollection"_attr = source);

    // Renaming across databases will result in a new UUID.
    NamespaceStringOrUUID tmpCollUUID{tmpName.dbName(), UUID::gen()};

    {
        auto collectionOptions = sourceColl->getCollectionOptions();
        collectionOptions.uuid = tmpCollUUID.uuid();

        writeConflictRetry(opCtx, "renameCollection", tmpName, [&] {
            WriteUnitOfWork wunit(opCtx);
            targetDB->createCollection(opCtx, tmpName, collectionOptions);
            wunit.commit();
        });
    }

    // Dismissed on success
    ScopeGuard tmpCollectionDropper([&] {
        Status status = Status::OK();
        try {
            status = dropCollectionForApplyOps(
                opCtx,
                tmpName,
                {},
                DropCollectionSystemCollectionMode::kAllowSystemCollectionDrops);
        } catch (...) {
            status = exceptionToStatus();
        }
        if (!status.isOK()) {
            // Ignoring failure case when dropping the temporary collection during cleanup because
            // the rename operation has already failed for another reason.
            LOGV2(705521,
                  "Unable to drop temporary collection {tmpName} while renaming from {source} to "
                  "{target}: {error}",
                  "Unable to drop temporary collection while renaming",
                  "tempCollection"_attr = tmpName,
                  "source"_attr = source,
                  "target"_attr = target,
                  "error"_attr = status);
        }
    });

    // Copy the index descriptions from the source collection.
    std::vector<BSONObj> indexesToCopy;
    for (auto sourceIndIt = sourceColl->getIndexCatalog()->getIndexIterator(
             opCtx,
             IndexCatalog::InclusionPolicy::kReady | IndexCatalog::InclusionPolicy::kUnfinished |
                 IndexCatalog::InclusionPolicy::kFrozen);
         sourceIndIt->more();) {
        auto descriptor = sourceIndIt->next()->descriptor();
        if (descriptor->isIdIndex()) {
            continue;
        }

        indexesToCopy.push_back(descriptor->infoObj());
    }

    // Create indexes using the index specs on the empty temporary collection that was just created.
    // Since each index build is possibly replicated to downstream nodes, each createIndex oplog
    // entry must have a distinct timestamp to support correct rollback operation. This is achieved
    // by writing the createIndexes oplog entry *before* creating the index. Using
    // IndexCatalog::createIndexOnEmptyCollection() for the index creation allows us to add and
    // commit the index within a single WriteUnitOfWork and avoids the possibility of seeing the
    // index in an unfinished state. For more information on assigning timestamps to multiple index
    // builds, please see SERVER-35780 and SERVER-35070.
    if (!indexesToCopy.empty()) {
        Status status = writeConflictRetry(opCtx, "renameCollection", tmpName, [&] {
            WriteUnitOfWork wunit(opCtx);
            auto fromMigrate = false;
            try {
                CollectionWriter tmpCollWriter(opCtx, *tmpCollUUID.uuid());
                IndexBuildsCoordinator::get(opCtx)->createIndexesOnEmptyCollection(
                    opCtx, tmpCollWriter, indexesToCopy, fromMigrate);
            } catch (DBException& ex) {
                return ex.toStatus();
            }
            wunit.commit();
            return Status::OK();
        });
        if (!status.isOK()) {
            return status;
        }
    }

    {
        statsTracker.reset();

        // Copy over all the data from source collection to temporary collection. For this we can
        // drop the exclusive database lock on the target and grab an intent lock on the temporary
        // collection.
        targetDBLock.reset();

        AutoGetCollection autoTmpColl(opCtx, tmpCollUUID, MODE_IX);
        if (!autoTmpColl) {
            return Status(ErrorCodes::NamespaceNotFound,
                          str::stream() << "Temporary collection '" << tmpName.toStringForErrorMsg()
                                        << "' was removed while renaming collection across DBs");
        }

        auto replCoord = repl::ReplicationCoordinator::get(opCtx);
        auto isOplogDisabledForTmpColl = replCoord->isOplogDisabledFor(opCtx, tmpName);
        bool canBeBatched =
            !(autoTmpColl->isCapped() && autoTmpColl->getIndexCatalog()->haveAnyIndexes());

        auto cursor = sourceColl->getCursor(opCtx);
        auto record = cursor->next();
        auto batchedWriteMaxSizeBytes =
            gMaxSizeOfBatchedInsertsForRenameAcrossDatabasesBytes.load();
        auto batchedWriteMaxNumberOfInserts =
            gMaxNumberOfInsertsBatchInsertsForRenameAcrossDatabases.load();
        while (record) {
            opCtx->checkForInterrupt();
            // Cursor is left one past the end of the batch inside writeConflictRetry.
            auto beginBatchId = record->id;
            Status status = writeConflictRetry(opCtx, "renameCollection", tmpName, [&] {
                // Always reposition cursor in case it gets a WCE midway through.
                record = cursor->seekExact(beginBatchId);

                std::vector<InsertStatement> stmts;
                // Inserts to indexed capped collections cannot be batched.
                // Otherwise, CollectionImpl::_insertDocuments() will fail with
                // OperationCannotBeBatched. See SERVER-21512.
                buildBatchedWritesWithPolicy(
                    batchedWriteMaxSizeBytes,
                    batchedWriteMaxNumberOfInserts,
                    [&cursor]() { return cursor->next(); },
                    record,
                    stmts,
                    canBeBatched);

                bool isGroupedOplogEntries = stmts.size() > 1U;
                WriteUnitOfWork wunit(opCtx, isGroupedOplogEntries);

                if (!isOplogDisabledForTmpColl && !isGroupedOplogEntries) {
                    auto oplogInfo = LocalOplogInfo::get(opCtx);
                    auto slots = oplogInfo->getNextOpTimes(opCtx, 1U);
                    stmts[0].oplogSlot = slots[0];
                }

                OpDebug* const opDebug = nullptr;
                auto status = collection_internal::insertDocuments(opCtx,
                                                                   *autoTmpColl,
                                                                   stmts.begin(),
                                                                   stmts.end(),
                                                                   opDebug,
                                                                   false /* fromMigrate */);
                if (!status.isOK()) {
                    return status;
                }

                // Used to make sure that a WCE can be handled by this logic without data loss.
                if (MONGO_unlikely(writeConflictInRenameCollCopyToTmp.shouldFail())) {
                    throwWriteConflictException(
                        str::stream() << "Hit failpoint '"
                                      << writeConflictInRenameCollCopyToTmp.getName() << "'.");
                }

                wunit.commit();

                // Time to yield; make a safe copy of the current record before releasing our
                // cursor.
                if (record)
                    record->data.makeOwned();

                cursor->save();
                // When this exits via success or WCE, we need to restore the cursor.
                ON_BLOCK_EXIT([opCtx, ns = tmpName, &cursor]() {
                    writeConflictRetry(
                        opCtx, "retryRestoreCursor", ns, [&cursor] { cursor->restore(); });
                });
                return Status::OK();
            });
            if (!status.isOK())
                return status;
        }
    }
    sourceCollLock.reset();
    sourceDbLock.reset();

    // Getting here means we successfully built the target copy. We now do the final
    // in-place rename and remove the source collection.
    invariant(tmpName.db() == target.db());
    RenameCollectionOptions tempOptions(options);
    Status status = renameCollectionWithinDB(opCtx, tmpName, target, tempOptions);
    if (!status.isOK())
        return status;

    tmpCollectionDropper.dismiss();
    return dropCollectionForApplyOps(
        opCtx, source, {}, DropCollectionSystemCollectionMode::kAllowSystemCollectionDrops);
}

}  // namespace

void doLocalRenameIfOptionsAndIndexesHaveNotChanged(OperationContext* opCtx,
                                                    const NamespaceString& sourceNs,
                                                    const NamespaceString& targetNs,
                                                    const RenameCollectionOptions& options,
                                                    std::list<BSONObj> originalIndexes,
                                                    BSONObj originalCollectionOptions) {
    AutoGetDb dbLock(opCtx, targetNs.dbName(), MODE_X);
    auto collection = dbLock.getDb()
        ? CollectionCatalog::get(opCtx)->lookupCollectionByNamespace(opCtx, targetNs)
        : nullptr;
    BSONObj collectionOptions = {};
    if (collection) {
        // We do not include the UUID field in the options comparison. It is ok if the target
        // collection was dropped and recreated, as long as the new target collection has the same
        // options and indexes as the original one did. This is mainly to support concurrent $out
        // to the same collection.
        collectionOptions = collection->getCollectionOptions().toBSON().removeField("uuid");
    }

    uassert(ErrorCodes::CommandFailed,
            str::stream() << "collection options of target collection "
                          << targetNs.toStringForErrorMsg()
                          << " changed during processing. Original options: "
                          << originalCollectionOptions << ", new options: " << collectionOptions,
            SimpleBSONObjComparator::kInstance.evaluate(
                originalCollectionOptions.removeField("uuid") == collectionOptions));

    auto currentIndexes =
        listIndexesEmptyListIfMissing(opCtx, targetNs, ListIndexesInclude::Nothing);

    UnorderedFieldsBSONObjComparator comparator;
    uassert(
        ErrorCodes::CommandFailed,
        str::stream() << "indexes of target collection " << targetNs.toStringForErrorMsg()
                      << " changed during processing.",
        originalIndexes.size() == currentIndexes.size() &&
            std::equal(originalIndexes.begin(),
                       originalIndexes.end(),
                       currentIndexes.begin(),
                       [&](auto& lhs, auto& rhs) { return comparator.compare(lhs, rhs) == 0; }));

    validateAndRunRenameCollection(opCtx, sourceNs, targetNs, options);
}

void validateNamespacesForRenameCollection(OperationContext* opCtx,
                                           const NamespaceString& source,
                                           const NamespaceString& target,
                                           const RenameCollectionOptions& options) {
    uassert(ErrorCodes::InvalidNamespace,
            str::stream() << "Invalid source namespace: " << source.toStringForErrorMsg(),
            source.isValid());
    uassert(ErrorCodes::InvalidNamespace,
            str::stream() << "Invalid target namespace: " << target.toStringForErrorMsg(),
            target.isValid());

    if ((repl::ReplicationCoordinator::get(opCtx)->getReplicationMode() !=
         repl::ReplicationCoordinator::modeNone)) {
        uassert(ErrorCodes::IllegalOperation,
                "can't rename live oplog while replicating",
                !source.isOplog());
        uassert(ErrorCodes::IllegalOperation,
                "can't rename to live oplog while replicating",
                !target.isOplog());
    }

    uassert(ErrorCodes::IllegalOperation,
            "If either the source or target of a rename is an oplog name, both must be",
            source.isOplog() == target.isOplog());

    Status sourceStatus = userAllowedWriteNS(opCtx, source);
    uassert(ErrorCodes::IllegalOperation,
            "error with source namespace: " + sourceStatus.reason(),
            sourceStatus.isOK());
    Status targetStatus = userAllowedWriteNS(opCtx, target);
    uassert(ErrorCodes::IllegalOperation,
            "error with target namespace: " + targetStatus.reason(),
            targetStatus.isOK());

    if (source.isServerConfigurationCollection()) {
        uasserted(ErrorCodes::IllegalOperation,
                  "renaming the server configuration "
                  "collection (admin.system.version) is not "
                  "allowed");
    }

    uassert(ErrorCodes::NamespaceNotFound,
            str::stream() << "renameCollection cannot accept a source collection that is in a "
                             "drop-pending state: "
                          << source.toStringForErrorMsg(),
            !source.isDropPendingNamespace());

    uassert(ErrorCodes::IllegalOperation,
            "renaming system.views collection or renaming to system.views is not allowed",
            !source.isSystemDotViews() && !target.isSystemDotViews());

    uassert(ErrorCodes::IllegalOperation,
            "renaming system.js collection or renaming to system.js is not allowed",
            !source.isSystemDotJavascript() && !target.isSystemDotJavascript());

    uassert(ErrorCodes::IllegalOperation,
            "Renaming system.buckets collections is not allowed",
            source.isOutTmpBucketsCollection() || !source.isTimeseriesBucketsCollection());
}

void validateAndRunRenameCollection(OperationContext* opCtx,
                                    const NamespaceString& source,
                                    const NamespaceString& target,
                                    const RenameCollectionOptions& options) {
    invariant(source != target, "Can't rename a collection to itself");

    validateNamespacesForRenameCollection(opCtx, source, target, options);

    OperationShardingState::ScopedAllowImplicitCollectionCreate_UNSAFE unsafeCreateCollection(
        opCtx);
    uassertStatusOK(renameCollection(opCtx, source, target, options));
}

Status renameCollection(OperationContext* opCtx,
                        const NamespaceString& source,
                        const NamespaceString& target,
                        const RenameCollectionOptions& options) {
    if (source.isDropPendingNamespace()) {
        return Status(ErrorCodes::NamespaceNotFound,
                      str::stream() << "renameCollection() cannot accept a source "
                                       "collection that is in a drop-pending state: "
                                    << source.toStringForErrorMsg());
    }

    if (source.isSystemDotViews() || target.isSystemDotViews()) {
        return Status(
            ErrorCodes::IllegalOperation,
            "renaming system.views collection or renaming to system.views is not allowed");
    }

    if (source.isSystemDotJavascript() || target.isSystemDotJavascript()) {
        return Status(ErrorCodes::IllegalOperation,
                      "renaming system.js collection or renaming to system.js is not allowed");
    }

    if (source.tenantId() != target.tenantId()) {
        return Status(ErrorCodes::IllegalOperation,
                      "renaming a collection across tenants is not allowed");
    }

    StringData dropTargetMsg = options.dropTarget ? "yes"_sd : "no"_sd;
    LOGV2(20400,
          "renameCollectionForCommand: rename {source} to {target}{dropTargetMsg}",
          "renameCollectionForCommand",
          "sourceNamespace"_attr = source,
          "targetNamespace"_attr = target,
          "dropTarget"_attr = dropTargetMsg);

    if (source.db() == target.db())
        return renameCollectionWithinDB(opCtx, source, target, options);
    else {
        return renameCollectionAcrossDatabases(opCtx, source, target, options);
    }
}

Status renameCollectionForApplyOps(OperationContext* opCtx,
                                   const boost::optional<UUID>& uuidToRename,
                                   const boost::optional<TenantId>& tid,
                                   const BSONObj& cmd,
                                   const repl::OpTime& renameOpTime) {

    // A valid 'renameOpTime' is not allowed when writes are replicated.
    if (!renameOpTime.isNull() && opCtx->writesAreReplicated()) {
        return Status(
            ErrorCodes::BadValue,
            "renameCollection() cannot accept a rename optime when writes are replicated.");
    }

    const auto sourceNsElt = cmd["renameCollection"];
    const auto targetNsElt = cmd["to"];

    NamespaceString sourceNss{NamespaceStringUtil::deserialize(tid, sourceNsElt.valueStringData())};
    NamespaceString targetNss{NamespaceStringUtil::deserialize(tid, targetNsElt.valueStringData())};

    // TODO: not needed once we are no longer parsing for prefixed tenantIds
    uassert(ErrorCodes::IllegalOperation,
            "moving a collection between tenants is not allowed",
            sourceNss.tenantId() == targetNss.tenantId());

    if (uuidToRename) {
        auto nss = CollectionCatalog::get(opCtx)->lookupNSSByUUID(opCtx, uuidToRename.value());
        if (nss)
            sourceNss = *nss;
    }

    RenameCollectionOptions options;
    options.dropTarget = cmd["dropTarget"].trueValue();
    options.stayTemp = cmd["stayTemp"].trueValue();

    boost::optional<UUID> uuidToDrop;
    if (cmd["dropTarget"].type() == BinData) {
        auto uuid = uassertStatusOK(UUID::parse(cmd["dropTarget"]));
        uuidToDrop = uuid;
    }

    // Check that the target namespace is in the correct form, "database.collection".
    auto targetStatus = userAllowedCreateNS(opCtx, targetNss);
    if (!targetStatus.isOK()) {
        return Status(targetStatus.code(),
                      str::stream() << "error with target namespace: " << targetStatus.reason());
    }

    if ((repl::ReplicationCoordinator::get(opCtx)->getReplicationMode() ==
         repl::ReplicationCoordinator::modeNone) &&
        targetNss.isOplog()) {
        return Status(ErrorCodes::IllegalOperation,
                      str::stream() << "Cannot rename collection to the oplog");
    }

    // Take global IX lock explicitly to avoid upgrading from IS later
    Lock::GlobalLock globalLock(opCtx, MODE_IX);
    AutoGetCollectionForRead sourceColl(
        opCtx,
        sourceNss,
        AutoGetCollection::Options{}.viewMode(auto_get_collection::ViewMode::kViewsPermitted));

    if (sourceNss.isDropPendingNamespace() || !sourceColl) {
        boost::optional<NamespaceString> dropTargetNss;

        if (options.dropTarget)
            dropTargetNss = targetNss;

        if (uuidToDrop)
            dropTargetNss = getNamespaceFromUUID(opCtx, uuidToDrop.value());

        // Downgrade renameCollection to dropCollection.
        if (dropTargetNss) {
            return dropCollectionForApplyOps(
                opCtx,
                *dropTargetNss,
                renameOpTime,
                DropCollectionSystemCollectionMode::kAllowSystemCollectionDrops);
        }

        return Status(ErrorCodes::NamespaceNotFound,
                      str::stream()
                          << "renameCollection() cannot accept a source "
                             "collection that does not exist or is in a drop-pending state: "
                          << sourceNss.toStringForErrorMsg());
    }

    const std::string uuidToDropString = uuidToDrop ? uuidToDrop->toString() : "<none>";
    const std::string uuidString = uuidToRename ? uuidToRename->toString() : "UUID unknown";
    LOGV2(20401,
          "renameCollectionForApplyOps: rename {sourceNss} ({uuidString}) to "
          "{targetNss}{dropTargetMsg}",
          "renameCollectionForApplyOps",
          "sourceNamespace"_attr = sourceNss,
          "uuid"_attr = uuidString,
          "targetNamespace"_attr = targetNss,
          "uuidToDrop"_attr = uuidToDropString);

    if (sourceNss.db() == targetNss.db()) {
        return renameCollectionWithinDBForApplyOps(
            opCtx, sourceNss, targetNss, uuidToDrop, renameOpTime, options);
    } else {
        return renameCollectionAcrossDatabases(opCtx, sourceNss, targetNss, options);
    }
}

Status renameCollectionForRollback(OperationContext* opCtx,
                                   const NamespaceString& target,
                                   const UUID& uuid) {
    // If the UUID we're targeting already exists, rename from there no matter what.
    auto source = getNamespaceFromUUID(opCtx, uuid);
    invariant(source);
    invariant(source->db() == target.db(),
              str::stream() << "renameCollectionForRollback: source and target namespaces must "
                               "have the same database. source: "
                            << (*source).toStringForErrorMsg()
                            << ". target: " << target.toStringForErrorMsg());

    LOGV2(20402,
          "renameCollectionForRollback: rename {source} ({uuid}) to {target}.",
          "renameCollectionForRollback",
          "source"_attr = *source,
          "uuid"_attr = uuid,
          "target"_attr = target);

    return renameCollectionWithinDB(opCtx, *source, target, {});
}

}  // namespace mongo