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
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
|
/**
* 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.
*/
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kStorage
#include "mongo/db/storage/storage_engine_impl.h"
#include <algorithm>
#include "mongo/db/audit.h"
#include "mongo/db/catalog/catalog_control.h"
#include "mongo/db/catalog/collection_catalog.h"
#include "mongo/db/catalog/collection_catalog_helper.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/client.h"
#include "mongo/db/concurrency/d_concurrency.h"
#include "mongo/db/index_builds_coordinator.h"
#include "mongo/db/multitenancy.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/server_options.h"
#include "mongo/db/storage/deferred_drop_record_store.h"
#include "mongo/db/storage/durable_catalog_impl.h"
#include "mongo/db/storage/durable_history_pin.h"
#include "mongo/db/storage/historical_ident_tracker.h"
#include "mongo/db/storage/kv/kv_engine.h"
#include "mongo/db/storage/storage_repair_observer.h"
#include "mongo/db/storage/storage_util.h"
#include "mongo/db/storage/two_phase_index_build_knobs_gen.h"
#include "mongo/logv2/log.h"
#include "mongo/stdx/unordered_map.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/scopeguard.h"
#include "mongo/util/str.h"
#define LOGV2_FOR_RECOVERY(ID, DLEVEL, MESSAGE, ...) \
LOGV2_DEBUG_OPTIONS(ID, DLEVEL, {logv2::LogComponent::kStorageRecovery}, MESSAGE, ##__VA_ARGS__)
namespace mongo {
using std::string;
using std::vector;
MONGO_FAIL_POINT_DEFINE(failToParseResumeIndexInfo);
MONGO_FAIL_POINT_DEFINE(pauseTimestampMonitor);
MONGO_FAIL_POINT_DEFINE(setMinVisibleForAllCollectionsToOldestOnStartup);
namespace {
const std::string kCatalogInfo = "_mdb_catalog";
const auto kCatalogLogLevel = logv2::LogSeverity::Debug(2);
} // namespace
StorageEngineImpl::StorageEngineImpl(OperationContext* opCtx,
std::unique_ptr<KVEngine> engine,
StorageEngineOptions options)
: _engine(std::move(engine)),
_options(std::move(options)),
_dropPendingIdentReaper(_engine.get()),
_minOfCheckpointAndOldestTimestampListener(
TimestampMonitor::TimestampType::kMinOfCheckpointAndOldest,
[this](Timestamp timestamp) { _onMinOfCheckpointAndOldestTimestampChanged(timestamp); }),
_historicalIdentTimestampListener(
TimestampMonitor::TimestampType::kCheckpoint,
[serviceContext = opCtx->getServiceContext()](Timestamp timestamp) {
HistoricalIdentTracker::get(serviceContext).removeEntriesOlderThan(timestamp);
}),
_supportsCappedCollections(_engine->supportsCappedCollections()) {
uassert(28601,
"Storage engine does not support --directoryperdb",
!(_options.directoryPerDB && !_engine->supportsDirectoryPerDB()));
// Replace the noop recovery unit for the startup operation context now that the storage engine
// has been initialized. This is needed because at the time of startup, when the operation
// context gets created, the storage engine initialization has not yet begun and so it gets
// assigned a noop recovery unit. See the StorageClientObserver class.
invariant(opCtx->recoveryUnit()->isNoop());
opCtx->setRecoveryUnit(std::unique_ptr<RecoveryUnit>(_engine->newRecoveryUnit()),
WriteUnitOfWork::RecoveryUnitState::kNotInUnitOfWork);
// If we are loading the catalog after an unclean shutdown, it's possible that there are
// collections in the catalog that are unknown to the storage engine. We should attempt to
// recover these orphaned idents.
// Allowing locking in write mode as reinitializeStorageEngine will be called while holding the
// global lock in exclusive mode.
invariant(!opCtx->lockState()->isLocked() || opCtx->lockState()->isW());
Lock::GlobalWrite globalLk(opCtx);
loadCatalog(opCtx,
_options.lockFileCreatedByUncleanShutdown ? LastShutdownState::kUnclean
: LastShutdownState::kClean);
}
void StorageEngineImpl::loadCatalog(OperationContext* opCtx, LastShutdownState lastShutdownState) {
bool catalogExists = _engine->hasIdent(opCtx, kCatalogInfo);
if (_options.forRepair && catalogExists) {
auto repairObserver = StorageRepairObserver::get(getGlobalServiceContext());
invariant(repairObserver->isIncomplete());
LOGV2(22246, "Repairing catalog metadata");
Status status = _engine->repairIdent(opCtx, kCatalogInfo);
if (status.code() == ErrorCodes::DataModifiedByRepair) {
LOGV2_WARNING(22264, "Catalog data modified by repair", "error"_attr = status);
repairObserver->invalidatingModification(str::stream() << "DurableCatalog repaired: "
<< status.reason());
} else {
fassertNoTrace(50926, status);
}
}
if (!catalogExists) {
WriteUnitOfWork uow(opCtx);
auto status = _engine->createRecordStore(
opCtx, NamespaceString(kCatalogInfo), kCatalogInfo, CollectionOptions());
// BadValue is usually caused by invalid configuration string.
// We still fassert() but without a stack trace.
if (status.code() == ErrorCodes::BadValue) {
fassertFailedNoTrace(28562);
}
fassert(28520, status);
uow.commit();
}
_catalogRecordStore = _engine->getRecordStore(
opCtx, NamespaceString(kCatalogInfo), kCatalogInfo, CollectionOptions());
if (shouldLog(::mongo::logv2::LogComponent::kStorageRecovery, kCatalogLogLevel)) {
LOGV2_FOR_RECOVERY(4615631, kCatalogLogLevel.toInt(), "loadCatalog:");
_dumpCatalog(opCtx);
}
_catalog.reset(new DurableCatalogImpl(
_catalogRecordStore.get(), _options.directoryPerDB, _options.directoryForIndexes, this));
_catalog->init(opCtx);
std::vector<std::string> identsKnownToStorageEngine = _engine->getAllIdents(opCtx);
std::sort(identsKnownToStorageEngine.begin(), identsKnownToStorageEngine.end());
std::vector<DurableCatalog::Entry> catalogEntries = _catalog->getAllCatalogEntries(opCtx);
// Perform a read on the catalog at the `oldestTimestamp` and record the record stores (via
// their catalogId) that existed.
std::set<RecordId> existedAtOldestTs;
if (!_engine->getOldestTimestamp().isNull()) {
ReadSourceScope snapshotScope(
opCtx, RecoveryUnit::ReadSource::kProvided, _engine->getOldestTimestamp());
auto entriesAtOldest = _catalog->getAllCatalogEntries(opCtx);
LOGV2_FOR_RECOVERY(5380110,
kCatalogLogLevel.toInt(),
"Catalog entries at the oldest timestamp",
"oldestTimestamp"_attr = _engine->getOldestTimestamp());
for (auto entry : entriesAtOldest) {
existedAtOldestTs.insert(entry.catalogId);
LOGV2_FOR_RECOVERY(5380109,
kCatalogLogLevel.toInt(),
"Historical entry",
"catalogId"_attr = entry.catalogId,
"ident"_attr = entry.ident,
logAttrs(entry.nss));
}
}
if (_options.forRepair) {
// It's possible that there are collection files on disk that are unknown to the catalog. In
// a repair context, if we can't find an ident in the catalog, we generate a catalog entry
// 'local.orphan.xxxxx' for it. However, in a nonrepair context, the orphaned idents
// will be dropped in reconcileCatalogAndIdents().
for (const auto& ident : identsKnownToStorageEngine) {
if (_catalog->isCollectionIdent(ident)) {
bool isOrphan = !std::any_of(
catalogEntries.begin(),
catalogEntries.end(),
[this, &ident](DurableCatalog::Entry entry) { return entry.ident == ident; });
if (isOrphan) {
// If the catalog does not have information about this
// collection, we create an new entry for it.
WriteUnitOfWork wuow(opCtx);
StatusWith<std::string> statusWithNs = _catalog->newOrphanedIdent(opCtx, ident);
if (statusWithNs.isOK()) {
wuow.commit();
auto orphanCollNs = statusWithNs.getValue();
LOGV2(22247,
"Successfully created an entry in the catalog for orphaned "
"collection",
"namespace"_attr = orphanCollNs);
LOGV2_WARNING(22265,
"Collection does not have an _id index. Please manually "
"build the index",
"namespace"_attr = orphanCollNs);
StorageRepairObserver::get(getGlobalServiceContext())
->benignModification(str::stream() << "Orphan collection created: "
<< statusWithNs.getValue());
} else {
// Log an error message if we cannot create the entry.
// reconcileCatalogAndIdents() will later drop this ident.
LOGV2_ERROR(
22268,
"Cannot create an entry in the catalog for the orphaned "
"collection ident: {ident} due to {statusWithNs_getStatus_reason}. "
"Restarting the server will remove this ident",
"Cannot create an entry in the catalog for orphaned ident. Restarting "
"the server will remove this ident",
"ident"_attr = ident,
"error"_attr = statusWithNs.getStatus());
}
}
}
}
}
const auto loadingFromUncleanShutdownOrRepair =
lastShutdownState == LastShutdownState::kUnclean || _options.forRepair;
BatchedCollectionCatalogWriter catalogBatchWriter{opCtx};
for (DurableCatalog::Entry entry : catalogEntries) {
if (_options.forRestore) {
// When restoring a subset of user collections from a backup, the collections not
// restored are in the catalog but are unknown to the storage engine. The catalog
// entries for these collections will be removed.
const auto collectionIdent = entry.ident;
bool restoredIdent = std::binary_search(identsKnownToStorageEngine.begin(),
identsKnownToStorageEngine.end(),
collectionIdent);
if (!restoredIdent) {
LOGV2(6260800,
"Removing catalog entry for collection not restored",
logAttrs(entry.nss),
"ident"_attr = collectionIdent);
WriteUnitOfWork wuow(opCtx);
fassert(6260801, _catalog->_removeEntry(opCtx, entry.catalogId));
wuow.commit();
continue;
}
// A collection being restored needs to also restore all of its indexes.
_checkForIndexFiles(opCtx, entry, identsKnownToStorageEngine);
}
if (loadingFromUncleanShutdownOrRepair) {
// If we are loading the catalog after an unclean shutdown or during repair, it's
// possible that there are collections in the catalog that are unknown to the storage
// engine. If we can't find a table in the list of storage engine idents, either
// attempt to recover the ident or drop it.
const auto collectionIdent = entry.ident;
bool orphan = !std::binary_search(identsKnownToStorageEngine.begin(),
identsKnownToStorageEngine.end(),
collectionIdent);
// If the storage engine is missing a collection and is unable to create a new record
// store, drop it from the catalog and skip initializing it by continuing past the
// following logic.
if (orphan) {
auto status =
_recoverOrphanedCollection(opCtx, entry.catalogId, entry.nss, collectionIdent);
if (!status.isOK()) {
LOGV2_WARNING(22266,
"Failed to recover orphaned data file for collection "
"'{namespace}': {error}",
"Failed to recover orphaned data file for collection",
"namespace"_attr = entry.nss,
"error"_attr = status);
WriteUnitOfWork wuow(opCtx);
fassert(50716, _catalog->_removeEntry(opCtx, entry.catalogId));
if (_options.forRepair) {
StorageRepairObserver::get(getGlobalServiceContext())
->invalidatingModification(str::stream()
<< "Collection " << entry.nss.ns()
<< " dropped: " << status.reason());
}
wuow.commit();
continue;
}
}
}
if (!entry.nss.isReplicated() &&
!std::binary_search(identsKnownToStorageEngine.begin(),
identsKnownToStorageEngine.end(),
entry.ident)) {
// All collection drops are non-transactional and unreplicated collections are dropped
// immediately as they do not use two-phase drops. It's possible to run into a situation
// where there are collections in the catalog that are unknown to the storage engine
// after restoring from backed up data files. See SERVER-55552.
WriteUnitOfWork wuow(opCtx);
fassert(5555200, _catalog->_removeEntry(opCtx, entry.catalogId));
wuow.commit();
LOGV2_INFO(5555201,
"Removed unknown unreplicated collection from the catalog",
"catalogId"_attr = entry.catalogId,
logAttrs(entry.nss),
"ident"_attr = entry.ident);
continue;
}
Timestamp minVisibleTs = Timestamp::min();
// If there's no recovery timestamp, every collection is available.
if (boost::optional<Timestamp> recoveryTs = _engine->getRecoveryTimestamp()) {
// Otherwise choose a minimum visible timestamp that's at least as large as the true
// value. For every collection we will choose either the `oldestTimestamp` or the
// `recoveryTimestamp`. Choose the `oldestTimestamp` for collections that existed at the
// `oldestTimestamp` and conservatively choose the `recoveryTimestamp` for everything
// else.
minVisibleTs = recoveryTs.get();
if (existedAtOldestTs.find(entry.catalogId) != existedAtOldestTs.end()) {
// Collections found at the `oldestTimestamp` on startup can have their minimum
// visible timestamp pulled back to that value.
minVisibleTs = _engine->getOldestTimestamp();
}
// This failpoint is useful for tests which want to exercise atClusterTime reads across
// server starts (e.g. resharding). It is likely only safe for tests which don't run
// operations that bump the minimum visible timestamp and can guarantee the collection
// always exists for the atClusterTime value(s).
setMinVisibleForAllCollectionsToOldestOnStartup.execute(
[this, &minVisibleTs](const BSONObj& data) {
minVisibleTs = _engine->getOldestTimestamp();
});
}
_initCollection(opCtx, entry.catalogId, entry.nss, _options.forRepair, minVisibleTs);
if (entry.nss.isOrphanCollection()) {
LOGV2(22248, "Orphaned collection found", logAttrs(entry.nss));
}
}
opCtx->recoveryUnit()->abandonSnapshot();
}
void StorageEngineImpl::_initCollection(OperationContext* opCtx,
RecordId catalogId,
const NamespaceString& nss,
bool forRepair,
Timestamp minVisibleTs) {
auto md = _catalog->getMetaData(opCtx, catalogId);
uassert(ErrorCodes::MustDowngrade,
str::stream() << "Collection does not have UUID in KVCatalog. Collection: " << nss,
md->options.uuid);
auto ident = _catalog->getEntry(catalogId).ident;
std::unique_ptr<RecordStore> rs;
if (forRepair) {
// Using a NULL rs since we don't want to open this record store before it has been
// repaired. This also ensures that if we try to use it, it will blow up.
rs = nullptr;
} else {
rs = _engine->getRecordStore(opCtx, nss, ident, md->options);
invariant(rs);
}
auto collectionFactory = Collection::Factory::get(getGlobalServiceContext());
auto collection = collectionFactory->make(opCtx, nss, catalogId, md, std::move(rs));
collection->setMinimumVisibleSnapshot(minVisibleTs);
CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) {
catalog.registerCollection(opCtx, md->options.uuid.get(), std::move(collection));
});
}
void StorageEngineImpl::closeCatalog(OperationContext* opCtx) {
dassert(opCtx->lockState()->isLocked());
if (shouldLog(::mongo::logv2::LogComponent::kStorageRecovery, kCatalogLogLevel)) {
LOGV2_FOR_RECOVERY(4615632, kCatalogLogLevel.toInt(), "loadCatalog:");
_dumpCatalog(opCtx);
}
CollectionCatalog::write(
opCtx, [&](CollectionCatalog& catalog) { catalog.deregisterAllCollectionsAndViews(); });
_catalog.reset();
_catalogRecordStore.reset();
}
Status StorageEngineImpl::_recoverOrphanedCollection(OperationContext* opCtx,
RecordId catalogId,
const NamespaceString& collectionName,
StringData collectionIdent) {
if (!_options.forRepair) {
return {ErrorCodes::IllegalOperation, "Orphan recovery only supported in repair"};
}
LOGV2(22249,
"Storage engine is missing collection from its metadata. Attempting to locate and "
"recover the data",
"namespace"_attr = collectionName,
"ident"_attr = collectionIdent);
WriteUnitOfWork wuow(opCtx);
const auto metadata = _catalog->getMetaData(opCtx, catalogId);
Status status =
_engine->recoverOrphanedIdent(opCtx, collectionName, collectionIdent, metadata->options);
bool dataModified = status.code() == ErrorCodes::DataModifiedByRepair;
if (!status.isOK() && !dataModified) {
return status;
}
if (dataModified) {
StorageRepairObserver::get(getGlobalServiceContext())
->invalidatingModification(str::stream() << "Collection " << collectionName
<< " recovered: " << status.reason());
}
wuow.commit();
return Status::OK();
}
void StorageEngineImpl::_checkForIndexFiles(
OperationContext* opCtx,
const DurableCatalog::Entry& entry,
std::vector<std::string>& identsKnownToStorageEngine) const {
std::vector<std::string> indexIdents = _catalog->getIndexIdents(opCtx, entry.catalogId);
for (const std::string& indexIdent : indexIdents) {
bool restoredIndexIdent = std::binary_search(
identsKnownToStorageEngine.begin(), identsKnownToStorageEngine.end(), indexIdent);
if (restoredIndexIdent) {
continue;
}
LOGV2_FATAL_NOTRACE(6261000,
"Collection is missing an index file",
logAttrs(entry.nss),
"collectionIdent"_attr = entry.ident,
"missingIndexIdent"_attr = indexIdent);
}
}
bool StorageEngineImpl::_handleInternalIdent(OperationContext* opCtx,
const std::string& ident,
LastShutdownState lastShutdownState,
ReconcileResult* reconcileResult,
std::set<std::string>* internalIdentsToDrop,
std::set<std::string>* allInternalIdents) {
if (!_catalog->isInternalIdent(ident)) {
return false;
}
allInternalIdents->insert(ident);
// When starting up after an unclean shutdown, we do not attempt to recover any state from the
// internal idents. Thus, we drop them in this case.
if (lastShutdownState == LastShutdownState::kUnclean || !supportsResumableIndexBuilds()) {
internalIdentsToDrop->insert(ident);
return true;
}
if (!_catalog->isResumableIndexBuildIdent(ident)) {
return false;
}
// When starting up after a clean shutdown and resumable index builds are supported, find the
// internal idents that contain the relevant information to resume each index build and recover
// the state.
auto rs = _engine->getRecordStore(opCtx, NamespaceString(""), ident, CollectionOptions());
auto cursor = rs->getCursor(opCtx);
auto record = cursor->next();
if (record) {
auto doc = record.get().data.toBson();
// Parse the documents here so that we can restart the build if the document doesn't
// contain all the necessary information to be able to resume building the index.
ResumeIndexInfo resumeInfo;
try {
if (MONGO_unlikely(failToParseResumeIndexInfo.shouldFail())) {
uasserted(ErrorCodes::FailPointEnabled,
"failToParseResumeIndexInfo fail point is enabled");
}
resumeInfo = ResumeIndexInfo::parse(IDLParserErrorContext("ResumeIndexInfo"), doc);
} catch (const DBException& e) {
LOGV2(4916300, "Failed to parse resumable index info", "error"_attr = e.toStatus());
// Ignore the error so that we can restart the index build instead of resume it. We
// should drop the internal ident if we failed to parse.
internalIdentsToDrop->insert(ident);
return true;
}
reconcileResult->indexBuildsToResume.push_back(resumeInfo);
// Once we have parsed the resume info, we can safely drop the internal ident.
internalIdentsToDrop->insert(ident);
LOGV2(4916301,
"Found unfinished index build to resume",
"buildUUID"_attr = resumeInfo.getBuildUUID(),
"collectionUUID"_attr = resumeInfo.getCollectionUUID(),
"phase"_attr = IndexBuildPhase_serializer(resumeInfo.getPhase()));
return true;
}
return false;
}
/**
* This method reconciles differences between idents the KVEngine is aware of and the
* DurableCatalog. There are three differences to consider:
*
* First, a KVEngine may know of an ident that the DurableCatalog does not. This method will drop
* the ident from the KVEngine.
*
* Second, a DurableCatalog may have a collection ident that the KVEngine does not. This is an
* illegal state and this method fasserts.
*
* Third, a DurableCatalog may have an index ident that the KVEngine does not. This method will
* rebuild the index.
*/
StatusWith<StorageEngine::ReconcileResult> StorageEngineImpl::reconcileCatalogAndIdents(
OperationContext* opCtx, LastShutdownState lastShutdownState) {
// Gather all tables known to the storage engine and drop those that aren't cross-referenced
// in the _mdb_catalog. This can happen for two reasons.
//
// First, collection creation and deletion happen in two steps. First the storage engine
// creates/deletes the table, followed by the change to the _mdb_catalog. It's not assumed a
// storage engine can make these steps atomic.
//
// Second, a replica set node in 3.6+ on supported storage engines will only persist "stable"
// data to disk. That is data which replication guarantees won't be rolled back. The
// _mdb_catalog will reflect the "stable" set of collections/indexes. However, it's not
// expected for a storage engine's ability to persist stable data to extend to "stable
// tables".
std::set<std::string> engineIdents;
{
std::vector<std::string> vec = _engine->getAllIdents(opCtx);
engineIdents.insert(vec.begin(), vec.end());
engineIdents.erase(kCatalogInfo);
}
LOGV2_FOR_RECOVERY(4615633, 2, "Reconciling collection and index idents.");
std::set<std::string> catalogIdents;
{
std::vector<std::string> vec = _catalog->getAllIdents(opCtx);
catalogIdents.insert(vec.begin(), vec.end());
}
std::set<std::string> internalIdentsToDrop;
std::set<std::string> allInternalIdents;
auto dropPendingIdents = _dropPendingIdentReaper.getAllIdentNames();
// Drop all idents in the storage engine that are not known to the catalog. This can happen in
// the case of a collection or index creation being rolled back.
StorageEngine::ReconcileResult reconcileResult;
for (const auto& it : engineIdents) {
if (catalogIdents.find(it) != catalogIdents.end()) {
continue;
}
if (_handleInternalIdent(opCtx,
it,
lastShutdownState,
&reconcileResult,
&internalIdentsToDrop,
&allInternalIdents)) {
continue;
}
if (!_catalog->isUserDataIdent(it)) {
continue;
}
// In repair context, any orphaned collection idents from the engine should already be
// recovered in the catalog in loadCatalog().
invariant(!(_catalog->isCollectionIdent(it) && _options.forRepair));
// Leave drop-pending idents alone.
// These idents have to be retained as long as the corresponding drops are not part of a
// checkpoint.
if (dropPendingIdents.find(it) != dropPendingIdents.cend()) {
LOGV2(22250,
"Not removing ident for uncheckpointed collection or index drop",
"ident"_attr = it);
continue;
}
const auto& toRemove = it;
LOGV2(22251, "Dropping unknown ident", "ident"_attr = toRemove);
WriteUnitOfWork wuow(opCtx);
fassert(40591, _engine->dropIdent(opCtx->recoveryUnit(), toRemove));
wuow.commit();
}
// Scan all collections in the catalog and make sure their ident is known to the storage
// engine. An omission here is fatal. A missing ident could mean a collection drop was rolled
// back. Note that startup already attempts to open tables; this should only catch errors in
// other contexts such as `recoverToStableTimestamp`.
std::vector<DurableCatalog::Entry> catalogEntries = _catalog->getAllCatalogEntries(opCtx);
if (!_options.forRepair) {
for (DurableCatalog::Entry entry : catalogEntries) {
if (engineIdents.find(entry.ident) == engineIdents.end()) {
return {ErrorCodes::UnrecoverableRollbackError,
str::stream() << "Expected collection does not exist. Collection: "
<< entry.nss.ns() << " Ident: " << entry.ident};
}
}
}
// Scan all indexes and return those in the catalog where the storage engine does not have the
// corresponding ident. The caller is expected to rebuild these indexes.
//
// Also, remove unfinished builds except those that were background index builds started on a
// secondary.
for (DurableCatalog::Entry entry : catalogEntries) {
std::shared_ptr<BSONCollectionCatalogEntry::MetaData> metaData =
_catalog->getMetaData(opCtx, entry.catalogId);
NamespaceString nss(metaData->ns);
// Batch up the indexes to remove them from `metaData` outside of the iterator.
std::vector<std::string> indexesToDrop;
for (const auto& indexMetaData : metaData->indexes) {
auto indexName = indexMetaData.nameStringData();
auto indexIdent = _catalog->getIndexIdent(opCtx, entry.catalogId, indexName);
// Warn in case of incorrect "multikeyPath" information in catalog documents. This is
// the result of a concurrency bug which has since been fixed, but may persist in
// certain catalog documents. See https://jira.mongodb.org/browse/SERVER-43074
const bool hasMultiKeyPaths =
std::any_of(indexMetaData.multikeyPaths.begin(),
indexMetaData.multikeyPaths.end(),
[](auto& pathSet) { return pathSet.size() > 0; });
if (!indexMetaData.multikey && hasMultiKeyPaths) {
LOGV2_WARNING(
22267,
"The 'multikey' field for index {index} on collection {namespace} was "
"false with non-empty 'multikeyPaths'. This indicates corruption of "
"the catalog. Consider either dropping and recreating the index, or "
"rerunning with the --repair option. See "
"http://dochub.mongodb.org/core/repair for more information.",
"The 'multikey' field for index was false with non-empty 'multikeyPaths'. This "
"indicates corruption of the catalog. Consider either dropping and recreating "
"the index, or rerunning with the --repair option. See "
"http://dochub.mongodb.org/core/repair for more information",
"index"_attr = indexName,
logAttrs(nss));
}
// Two-phase index drop ensures that the underlying data table for an index in the
// catalog is not dropped until the index removal from the catalog has been majority
// committed and become part of the latest checkpoint. Therefore, there should almost
// never be a case where the index catalog entry remains but the index table (identified
// by ident) has been removed.
//
// There is an exception to this due to the fact that we drop the index ident without a
// timestamp when restarting an index build for startup recovery. Then, if we experience
// an unclean shutdown before a checkpoint is taken, the subsequent startup recovery can
// see the now-dropped ident referenced by the old index catalog entry.
invariant(engineIdents.find(indexIdent) != engineIdents.end() ||
lastShutdownState == LastShutdownState::kUnclean,
str::stream() << "Failed to find an index data table matching " << indexIdent
<< " for durable index catalog entry " << indexMetaData.spec
<< " in collection " << nss.ns());
// Any index build with a UUID is an unfinished two-phase build and must be restarted.
// There are no special cases to handle on primaries or secondaries. An index build may
// be associated with multiple indexes. We should only restart an index build if we
// aren't going to resume it.
if (indexMetaData.buildUUID) {
invariant(!indexMetaData.ready);
auto collUUID = metaData->options.uuid;
invariant(collUUID);
auto buildUUID = *indexMetaData.buildUUID;
LOGV2(22253,
"Found index from unfinished build",
logAttrs(nss),
"uuid"_attr = *collUUID,
"index"_attr = indexName,
"buildUUID"_attr = buildUUID);
// Insert in the map if a build has not already been registered.
auto existingIt = reconcileResult.indexBuildsToRestart.find(buildUUID);
if (existingIt == reconcileResult.indexBuildsToRestart.end()) {
reconcileResult.indexBuildsToRestart.insert(
{buildUUID, IndexBuildDetails(*collUUID)});
existingIt = reconcileResult.indexBuildsToRestart.find(buildUUID);
}
existingIt->second.indexSpecs.emplace_back(indexMetaData.spec);
continue;
}
// If the index was kicked off as a background secondary index build, replication
// recovery will not run into the oplog entry to recreate the index. If the index build
// did not successfully complete, this code will return the index to be rebuilt.
if (indexMetaData.isBackgroundSecondaryBuild && !indexMetaData.ready) {
LOGV2(22255,
"Expected background index build did not complete, rebuilding in foreground "
"- see SERVER-43097",
logAttrs(nss),
"index"_attr = indexName);
reconcileResult.indexesToRebuild.push_back(
{entry.catalogId, nss, indexName.toString()});
continue;
}
// The last anomaly is when the index build did not complete, nor was the index build
// a secondary background index build. This implies the index build was on a primary
// and the `createIndexes` command never successfully returned, or the index build was
// a foreground secondary index build, meaning replication recovery will build the
// index when it replays the oplog. In these cases the index entry in the catalog
// should be dropped.
if (!indexMetaData.ready && !indexMetaData.isBackgroundSecondaryBuild) {
LOGV2(22256, "Dropping unfinished index", logAttrs(nss), "index"_attr = indexName);
// Ensure the `ident` is dropped while we have the `indexIdent` value.
fassert(50713, _engine->dropIdent(opCtx->recoveryUnit(), indexIdent));
indexesToDrop.push_back(indexName.toString());
continue;
}
}
for (auto&& indexName : indexesToDrop) {
invariant(metaData->eraseIndex(indexName),
str::stream() << "Index is missing. Collection: " << nss.ns()
<< " Index: " << indexName);
}
if (indexesToDrop.size() > 0) {
WriteUnitOfWork wuow(opCtx);
auto collection =
CollectionCatalog::get(opCtx)->lookupCollectionByNamespaceForMetadataWrite(
opCtx, entry.nss);
invariant(collection->getCatalogId() == entry.catalogId);
collection->replaceMetadata(opCtx, std::move(metaData));
wuow.commit();
}
}
// If there are no index builds to resume, we should drop all internal idents.
if (reconcileResult.indexBuildsToResume.empty()) {
internalIdentsToDrop.swap(allInternalIdents);
}
for (auto&& temp : internalIdentsToDrop) {
LOGV2(22257, "Dropping internal ident", "ident"_attr = temp);
WriteUnitOfWork wuow(opCtx);
fassert(51067, _engine->dropIdent(opCtx->recoveryUnit(), temp));
wuow.commit();
}
return reconcileResult;
}
std::string StorageEngineImpl::getFilesystemPathForDb(
const TenantDatabaseName& tenantDbName) const {
return _catalog->getFilesystemPathForDb(tenantDbName.dbName());
}
void StorageEngineImpl::cleanShutdown() {
if (_timestampMonitor) {
_timestampMonitor->clearListeners();
}
CollectionCatalog::write(getGlobalServiceContext(), [](CollectionCatalog& catalog) {
catalog.deregisterAllCollectionsAndViews();
});
_catalog.reset();
_catalogRecordStore.reset();
_timestampMonitor.reset();
_engine->cleanShutdown();
// intentionally not deleting _engine
}
StorageEngineImpl::~StorageEngineImpl() {}
void StorageEngineImpl::startTimestampMonitor() {
if (storageGlobalParams.readOnly) {
return;
}
// Unless explicitly disabled, all storage engines should create a TimestampMonitor for
// drop-pending internal idents, even if they do not support pending drops for collections
// and indexes.
_timestampMonitor = std::make_unique<TimestampMonitor>(
_engine.get(), getGlobalServiceContext()->getPeriodicRunner());
_timestampMonitor->addListener(&_minOfCheckpointAndOldestTimestampListener);
_timestampMonitor->addListener(&_historicalIdentTimestampListener);
}
void StorageEngineImpl::notifyStartupComplete() {
_engine->notifyStartupComplete();
}
RecoveryUnit* StorageEngineImpl::newRecoveryUnit() {
if (!_engine) {
// shutdown
return nullptr;
}
return _engine->newRecoveryUnit();
}
std::vector<TenantDatabaseName> StorageEngineImpl::listDatabases() const {
return CollectionCatalog::get(getGlobalServiceContext())->getAllDbNames();
}
Status StorageEngineImpl::closeDatabase(OperationContext* opCtx,
const TenantDatabaseName& tenantDbName) {
// This is ok to be a no-op as there is no database layer in kv.
return Status::OK();
}
Status StorageEngineImpl::dropDatabase(OperationContext* opCtx,
const TenantDatabaseName& tenantDbName) {
auto catalog = CollectionCatalog::get(opCtx);
{
auto tenantDbNames = catalog->getAllDbNames();
if (std::count(tenantDbNames.begin(), tenantDbNames.end(), tenantDbName) == 0) {
return Status(ErrorCodes::NamespaceNotFound, "db not found to drop");
}
}
std::vector<UUID> toDrop = catalog->getAllCollectionUUIDsFromDb(tenantDbName);
// Do not timestamp any of the following writes. This will remove entries from the catalog as
// well as drop any underlying tables. It's not expected for dropping tables to be reversible
// on crash/recoverToStableTimestamp.
return _dropCollectionsNoTimestamp(opCtx, toDrop);
}
/**
* Returns the first `dropCollection` error that this method encounters. This method will attempt
* to drop all collections, regardless of the error status.
*/
Status StorageEngineImpl::_dropCollectionsNoTimestamp(OperationContext* opCtx,
const std::vector<UUID>& toDrop) {
// On primaries, this method will be called outside of any `TimestampBlock` state meaning the
// "commit timestamp" will not be set. For this case, this method needs no special logic to
// avoid timestamping the upcoming writes.
//
// On secondaries, there will be a wrapping `TimestampBlock` and the "commit timestamp" will
// be set. Carefully save that to the side so the following writes can go through without that
// context.
const Timestamp commitTs = opCtx->recoveryUnit()->getCommitTimestamp();
if (!commitTs.isNull()) {
opCtx->recoveryUnit()->clearCommitTimestamp();
}
// Ensure the method exits with the same "commit timestamp" state that it was called with.
ScopeGuard addCommitTimestamp([&opCtx, commitTs] {
if (!commitTs.isNull()) {
opCtx->recoveryUnit()->setCommitTimestamp(commitTs);
}
});
Status firstError = Status::OK();
WriteUnitOfWork untimestampedDropWuow(opCtx);
auto collectionCatalog = CollectionCatalog::get(opCtx);
for (auto& uuid : toDrop) {
auto coll = collectionCatalog->lookupCollectionByUUIDForMetadataWrite(opCtx, uuid);
// No need to remove the indexes from the IndexCatalog because eliminating the Collection
// will have the same effect.
auto ii =
coll->getIndexCatalog()->getIndexIterator(opCtx, true /* includeUnfinishedIndexes */);
while (ii->more()) {
const IndexCatalogEntry* ice = ii->next();
audit::logDropIndex(opCtx->getClient(), ice->descriptor()->indexName(), coll->ns());
catalog::removeIndex(
opCtx, ice->descriptor()->indexName(), coll, ice->getSharedIdent());
}
audit::logDropCollection(opCtx->getClient(), coll->ns());
Status result = catalog::dropCollection(
opCtx, coll->ns(), coll->getCatalogId(), coll->getSharedIdent());
if (!result.isOK() && firstError.isOK()) {
firstError = result;
}
CollectionCatalog::get(opCtx)->dropCollection(opCtx, coll);
}
untimestampedDropWuow.commit();
return firstError;
}
void StorageEngineImpl::flushAllFiles(OperationContext* opCtx, bool callerHoldsReadLock) {
_engine->flushAllFiles(opCtx, callerHoldsReadLock);
}
Status StorageEngineImpl::beginBackup(OperationContext* opCtx) {
// We should not proceed if we are already in backup mode
if (_inBackupMode)
return Status(ErrorCodes::BadValue, "Already in Backup Mode");
Status status = _engine->beginBackup(opCtx);
if (status.isOK())
_inBackupMode = true;
return status;
}
void StorageEngineImpl::endBackup(OperationContext* opCtx) {
// We should never reach here if we aren't already in backup mode
invariant(_inBackupMode);
_engine->endBackup(opCtx);
_inBackupMode = false;
}
Status StorageEngineImpl::disableIncrementalBackup(OperationContext* opCtx) {
return _engine->disableIncrementalBackup(opCtx);
}
StatusWith<std::unique_ptr<StorageEngine::StreamingCursor>>
StorageEngineImpl::beginNonBlockingBackup(OperationContext* opCtx,
boost::optional<Timestamp> checkpointTimestamp,
const StorageEngine::BackupOptions& options) {
return _engine->beginNonBlockingBackup(opCtx, checkpointTimestamp, options);
}
void StorageEngineImpl::endNonBlockingBackup(OperationContext* opCtx) {
return _engine->endNonBlockingBackup(opCtx);
}
StatusWith<std::deque<std::string>> StorageEngineImpl::extendBackupCursor(OperationContext* opCtx) {
return _engine->extendBackupCursor(opCtx);
}
bool StorageEngineImpl::isDurable() const {
return _engine->isDurable();
}
bool StorageEngineImpl::isEphemeral() const {
return _engine->isEphemeral();
}
SnapshotManager* StorageEngineImpl::getSnapshotManager() const {
return _engine->getSnapshotManager();
}
Status StorageEngineImpl::repairRecordStore(OperationContext* opCtx,
RecordId catalogId,
const NamespaceString& nss) {
auto repairObserver = StorageRepairObserver::get(getGlobalServiceContext());
invariant(repairObserver->isIncomplete());
Status status = _engine->repairIdent(opCtx, _catalog->getEntry(catalogId).ident);
bool dataModified = status.code() == ErrorCodes::DataModifiedByRepair;
if (!status.isOK() && !dataModified) {
return status;
}
if (dataModified) {
repairObserver->invalidatingModification(str::stream() << "Collection " << nss << ": "
<< status.reason());
}
// After repairing, re-initialize the collection with a valid RecordStore.
CollectionCatalog::write(opCtx, [&](CollectionCatalog& catalog) {
auto uuid = catalog.lookupUUIDByNSS(opCtx, nss).get();
catalog.deregisterCollection(opCtx, uuid);
});
// When repairing a record store, keep the existing behavior of not installing a minimum visible
// timestamp.
_initCollection(opCtx, catalogId, nss, false, Timestamp::min());
return status;
}
std::unique_ptr<TemporaryRecordStore> StorageEngineImpl::makeTemporaryRecordStore(
OperationContext* opCtx, KeyFormat keyFormat) {
std::unique_ptr<RecordStore> rs =
_engine->makeTemporaryRecordStore(opCtx, _catalog->newInternalIdent(), keyFormat);
LOGV2_DEBUG(22258, 1, "Created temporary record store", "ident"_attr = rs->getIdent());
return std::make_unique<DeferredDropRecordStore>(std::move(rs), this);
}
std::unique_ptr<TemporaryRecordStore>
StorageEngineImpl::makeTemporaryRecordStoreForResumableIndexBuild(OperationContext* opCtx,
KeyFormat keyFormat) {
std::unique_ptr<RecordStore> rs = _engine->makeTemporaryRecordStore(
opCtx, _catalog->newInternalResumableIndexBuildIdent(), keyFormat);
LOGV2_DEBUG(4921500,
1,
"Created temporary record store for resumable index build",
"ident"_attr = rs->getIdent());
return std::make_unique<DeferredDropRecordStore>(std::move(rs), this);
}
std::unique_ptr<TemporaryRecordStore> StorageEngineImpl::makeTemporaryRecordStoreFromExistingIdent(
OperationContext* opCtx, StringData ident) {
auto rs = _engine->getRecordStore(opCtx, NamespaceString(""), ident, CollectionOptions());
return std::make_unique<DeferredDropRecordStore>(std::move(rs), this);
}
void StorageEngineImpl::setJournalListener(JournalListener* jl) {
_engine->setJournalListener(jl);
}
void StorageEngineImpl::setStableTimestamp(Timestamp stableTimestamp, bool force) {
_engine->setStableTimestamp(stableTimestamp, force);
}
Timestamp StorageEngineImpl::getStableTimestamp() const {
return _engine->getStableTimestamp();
}
void StorageEngineImpl::setInitialDataTimestamp(Timestamp initialDataTimestamp) {
_engine->setInitialDataTimestamp(initialDataTimestamp);
}
Timestamp StorageEngineImpl::getInitialDataTimestamp() const {
return _engine->getInitialDataTimestamp();
}
void StorageEngineImpl::setOldestTimestampFromStable() {
_engine->setOldestTimestampFromStable();
}
void StorageEngineImpl::setOldestTimestamp(Timestamp newOldestTimestamp) {
const bool force = true;
_engine->setOldestTimestamp(newOldestTimestamp, force);
}
Timestamp StorageEngineImpl::getOldestTimestamp() const {
return _engine->getOldestTimestamp();
};
void StorageEngineImpl::setOldestActiveTransactionTimestampCallback(
StorageEngine::OldestActiveTransactionTimestampCallback callback) {
_engine->setOldestActiveTransactionTimestampCallback(callback);
}
bool StorageEngineImpl::supportsRecoverToStableTimestamp() const {
return _engine->supportsRecoverToStableTimestamp();
}
bool StorageEngineImpl::supportsRecoveryTimestamp() const {
return _engine->supportsRecoveryTimestamp();
}
StatusWith<Timestamp> StorageEngineImpl::recoverToStableTimestamp(OperationContext* opCtx) {
invariant(opCtx->lockState()->isW());
auto state = catalog::closeCatalog(opCtx);
// SERVER-58311: Reset the recovery unit to unposition storage engine cursors. This allows WT to
// assert it has sole access when performing rollback_to_stable().
auto recovUnit = opCtx->releaseAndReplaceRecoveryUnit();
recovUnit.reset();
StatusWith<Timestamp> swTimestamp = _engine->recoverToStableTimestamp(opCtx);
if (!swTimestamp.isOK()) {
return swTimestamp;
}
catalog::openCatalog(opCtx, state, swTimestamp.getValue());
DurableHistoryRegistry::get(opCtx)->reconcilePins(opCtx);
LOGV2(22259,
"recoverToStableTimestamp successful",
"stableTimestamp"_attr = swTimestamp.getValue());
return {swTimestamp.getValue()};
}
boost::optional<Timestamp> StorageEngineImpl::getRecoveryTimestamp() const {
return _engine->getRecoveryTimestamp();
}
boost::optional<Timestamp> StorageEngineImpl::getLastStableRecoveryTimestamp() const {
return _engine->getLastStableRecoveryTimestamp();
}
bool StorageEngineImpl::supportsReadConcernSnapshot() const {
return _engine->supportsReadConcernSnapshot();
}
bool StorageEngineImpl::supportsReadConcernMajority() const {
return _engine->supportsReadConcernMajority();
}
bool StorageEngineImpl::supportsOplogStones() const {
return _engine->supportsOplogStones();
}
bool StorageEngineImpl::supportsResumableIndexBuilds() const {
return supportsReadConcernMajority() && !isEphemeral() &&
!repl::ReplSettings::shouldRecoverFromOplogAsStandalone();
}
bool StorageEngineImpl::supportsPendingDrops() const {
return supportsReadConcernMajority();
}
void StorageEngineImpl::clearDropPendingState() {
_dropPendingIdentReaper.clearDropPendingState();
}
Timestamp StorageEngineImpl::getAllDurableTimestamp() const {
return _engine->getAllDurableTimestamp();
}
boost::optional<Timestamp> StorageEngineImpl::getOplogNeededForCrashRecovery() const {
return _engine->getOplogNeededForCrashRecovery();
}
void StorageEngineImpl::_dumpCatalog(OperationContext* opCtx) {
auto catalogRs = _catalogRecordStore.get();
auto cursor = catalogRs->getCursor(opCtx);
boost::optional<Record> rec = cursor->next();
stdx::unordered_set<std::string> nsMap;
while (rec) {
// This should only be called by a parent that's done an appropriate `shouldLog` check. Do
// not duplicate the log level policy.
LOGV2_FOR_RECOVERY(4615634,
kCatalogLogLevel.toInt(),
"Catalog entry",
"catalogId"_attr = rec->id,
"value"_attr = rec->data.toBson());
auto valueBson = rec->data.toBson();
if (valueBson.hasField("md")) {
std::string ns = valueBson.getField("md").Obj().getField("ns").String();
invariant(!nsMap.count(ns), str::stream() << "Found duplicate namespace: " << ns);
nsMap.insert(ns);
}
rec = cursor->next();
}
opCtx->recoveryUnit()->abandonSnapshot();
}
void StorageEngineImpl::addDropPendingIdent(const Timestamp& dropTimestamp,
std::shared_ptr<Ident> ident,
DropIdentCallback&& onDrop) {
_dropPendingIdentReaper.addDropPendingIdent(dropTimestamp, ident, std::move(onDrop));
}
void StorageEngineImpl::checkpoint() {
_engine->checkpoint();
}
void StorageEngineImpl::_onMinOfCheckpointAndOldestTimestampChanged(const Timestamp& timestamp) {
// No drop-pending idents present if getEarliestDropTimestamp() returns boost::none.
if (auto earliestDropTimestamp = _dropPendingIdentReaper.getEarliestDropTimestamp()) {
if (timestamp >= *earliestDropTimestamp) {
LOGV2(22260,
"Removing drop-pending idents with drop timestamps before timestamp",
"timestamp"_attr = timestamp);
auto opCtx = cc().getOperationContext();
invariant(opCtx);
_dropPendingIdentReaper.dropIdentsOlderThan(opCtx, timestamp);
}
}
}
StorageEngineImpl::TimestampMonitor::TimestampMonitor(KVEngine* engine, PeriodicRunner* runner)
: _engine(engine), _running(false), _periodicRunner(runner) {
_startup();
}
StorageEngineImpl::TimestampMonitor::~TimestampMonitor() {
LOGV2(22261, "Timestamp monitor shutting down");
stdx::lock_guard<Latch> lock(_monitorMutex);
invariant(_listeners.empty());
}
void StorageEngineImpl::TimestampMonitor::_startup() {
invariant(!_running);
LOGV2(22262, "Timestamp monitor starting");
PeriodicRunner::PeriodicJob job(
"TimestampMonitor",
[&](Client* client) {
if (MONGO_unlikely(pauseTimestampMonitor.shouldFail())) {
LOGV2(6321800,
"Pausing the timestamp monitor due to the pauseTimestampMonitor fail point");
pauseTimestampMonitor.pauseWhileSet();
}
{
stdx::lock_guard<Latch> lock(_monitorMutex);
if (_listeners.empty()) {
return;
}
}
try {
auto opCtx = client->getOperationContext();
mongo::ServiceContext::UniqueOperationContext uOpCtx;
if (!opCtx) {
uOpCtx = client->makeOperationContext();
opCtx = uOpCtx.get();
}
Timestamp checkpoint;
Timestamp oldest;
Timestamp stable;
Timestamp minOfCheckpointAndOldest;
{
// Take a global lock in MODE_IS while fetching timestamps to guarantee that
// rollback-to-stable isn't running concurrently.
ShouldNotConflictWithSecondaryBatchApplicationBlock shouldNotConflictBlock(
opCtx->lockState());
Lock::GlobalLock lock(opCtx, MODE_IS);
// The checkpoint timestamp is not cached in mongod and needs to be fetched with
// a call into WiredTiger, all the other timestamps are cached in mongod.
checkpoint = _engine->getCheckpointTimestamp();
oldest = _engine->getOldestTimestamp();
stable = _engine->getStableTimestamp();
minOfCheckpointAndOldest =
(checkpoint.isNull() || (checkpoint > oldest)) ? oldest : checkpoint;
}
{
stdx::lock_guard<Latch> lock(_monitorMutex);
for (const auto& listener : _listeners) {
if (listener->getType() == TimestampType::kCheckpoint) {
listener->notify(checkpoint);
} else if (listener->getType() == TimestampType::kOldest) {
listener->notify(oldest);
} else if (listener->getType() == TimestampType::kStable) {
listener->notify(stable);
} else if (listener->getType() ==
TimestampType::kMinOfCheckpointAndOldest) {
listener->notify(minOfCheckpointAndOldest);
} else if (stable == Timestamp::min()) {
// Special case notification of all listeners when writes do not have
// timestamps. This handles standalone mode and storage engines that
// don't support timestamps.
listener->notify(Timestamp::min());
}
}
}
} catch (const ExceptionForCat<ErrorCategory::Interruption>& ex) {
if (ex.code() == ErrorCodes::Interrupted) {
LOGV2(6183600, "Timestamp monitor got interrupted, retrying");
return;
}
if (!ErrorCodes::isCancellationError(ex))
throw;
// If we're interrupted at shutdown or after PeriodicRunner's client has been
// killed, it's fine to give up on future notifications.
LOGV2(22263, "Timestamp monitor is stopping", "error"_attr = ex);
return;
} catch (const DBException& ex) {
// Logs and rethrows the exceptions of other types.
LOGV2_ERROR(5802500, "Timestamp monitor throws an exception", "error"_attr = ex);
throw;
}
},
Seconds(1));
_job = _periodicRunner->makeJob(std::move(job));
_job.start();
_running = true;
}
void StorageEngineImpl::TimestampMonitor::addListener(TimestampListener* listener) {
stdx::lock_guard<Latch> lock(_monitorMutex);
if (std::find(_listeners.begin(), _listeners.end(), listener) != _listeners.end()) {
bool listenerAlreadyRegistered = true;
invariant(!listenerAlreadyRegistered);
}
_listeners.push_back(listener);
}
void StorageEngineImpl::TimestampMonitor::removeListener_forTestOnly(TimestampListener* listener) {
stdx::lock_guard<Latch> lock(_monitorMutex);
if (auto it = std::find(_listeners.begin(), _listeners.end(), listener);
it != _listeners.end()) {
_listeners.erase(it);
}
}
void StorageEngineImpl::TimestampMonitor::clearListeners() {
stdx::lock_guard<Latch> lock(_monitorMutex);
_listeners.clear();
}
int64_t StorageEngineImpl::sizeOnDiskForDb(OperationContext* opCtx,
const TenantDatabaseName& tenantDbName) {
int64_t size = 0;
auto perCollectionWork = [&](const CollectionPtr& collection) {
size += collection->getRecordStore()->storageSize(opCtx);
auto it = collection->getIndexCatalog()->getIndexIterator(opCtx, true);
while (it->more()) {
size += _engine->getIdentSize(opCtx, it->next()->getIdent());
}
return true;
};
if (opCtx->isLockFreeReadsOp()) {
auto collectionCatalog = CollectionCatalog::get(opCtx);
for (auto it = collectionCatalog->begin(opCtx, tenantDbName);
it != collectionCatalog->end(opCtx);
++it) {
perCollectionWork(*it);
}
} else {
catalog::forEachCollectionFromDb(opCtx, tenantDbName, MODE_IS, perCollectionWork);
};
return size;
}
StatusWith<Timestamp> StorageEngineImpl::pinOldestTimestamp(
OperationContext* opCtx,
const std::string& requestingServiceName,
Timestamp requestedTimestamp,
bool roundUpIfTooOld) {
return _engine->pinOldestTimestamp(
opCtx, requestingServiceName, requestedTimestamp, roundUpIfTooOld);
}
void StorageEngineImpl::unpinOldestTimestamp(const std::string& requestingServiceName) {
_engine->unpinOldestTimestamp(requestingServiceName);
}
void StorageEngineImpl::setPinnedOplogTimestamp(const Timestamp& pinnedTimestamp) {
_engine->setPinnedOplogTimestamp(pinnedTimestamp);
}
DurableCatalog* StorageEngineImpl::getCatalog() {
return _catalog.get();
}
const DurableCatalog* StorageEngineImpl::getCatalog() const {
return _catalog.get();
}
void StorageEngineImpl::dump() const {
_engine->dump();
}
} // namespace mongo
|