diff options
| author | Yuhong Zhang <yuhong.zhang@mongodb.com> | 2022-09-19 19:08:16 +0000 |
|---|---|---|
| committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-09-19 22:25:49 +0000 |
| commit | f5324f56bfd410822c1ddad76515fa4cc9ea21e0 (patch) | |
| tree | 44dc5c32d1192f7a65e3200e21d6b59c872aae8d /src/mongo | |
| parent | 4d2bfbe7ca935e5d689679400e44920021725bd8 (diff) | |
| download | mongo-f5324f56bfd410822c1ddad76515fa4cc9ea21e0.tar.gz | |
SERVER-68689 Temporarily disable background validation to use checkpoint cursors
Reverts the part of the implementation of the checkpoint cursors
Diffstat (limited to 'src/mongo')
| -rw-r--r-- | src/mongo/db/catalog/collection_validation.cpp | 12 | ||||
| -rw-r--r-- | src/mongo/db/catalog/collection_validation_test.cpp | 17 | ||||
| -rw-r--r-- | src/mongo/db/catalog/validate_state.cpp | 149 | ||||
| -rw-r--r-- | src/mongo/db/catalog/validate_state.h | 6 | ||||
| -rw-r--r-- | src/mongo/db/catalog/validate_state_test.cpp | 77 | ||||
| -rw-r--r-- | src/mongo/db/storage/durable_catalog.h | 4 | ||||
| -rw-r--r-- | src/mongo/db/storage/durable_catalog_impl.cpp | 15 | ||||
| -rw-r--r-- | src/mongo/db/storage/durable_catalog_impl.h | 2 | ||||
| -rw-r--r-- | src/mongo/db/storage/recovery_unit.h | 5 | ||||
| -rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_cursor.cpp | 7 | ||||
| -rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp | 4 | ||||
| -rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.cpp | 15 | ||||
| -rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h | 2 | ||||
| -rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit_test.cpp | 11 | ||||
| -rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_session_cache.cpp | 4 | ||||
| -rw-r--r-- | src/mongo/dbtests/validate_tests.cpp | 53 |
16 files changed, 97 insertions, 286 deletions
diff --git a/src/mongo/db/catalog/collection_validation.cpp b/src/mongo/db/catalog/collection_validation.cpp index 4645e18aece..7b39f823443 100644 --- a/src/mongo/db/catalog/collection_validation.cpp +++ b/src/mongo/db/catalog/collection_validation.cpp @@ -625,18 +625,6 @@ Status validate(OperationContext* opCtx, "corruption found", logAttrs(validateState.nss()), logAttrs(validateState.uuid())); - } catch (ExceptionFor<ErrorCodes::CursorNotFound>&) { - invariant(validateState.isBackground()); - string warning = str::stream() - << "Collection validation with {background: true} validates" - << " the latest checkpoint (data in a snapshot written to disk in a consistent" - << " way across all data files). During this validation, some tables have not yet been" - << " checkpointed."; - results->warnings.push_back(warning); - - // Nothing to validate, so it must be valid. - results->valid = true; - return Status::OK(); } catch (const DBException& e) { if (opCtx->isKillPending() || e.code() == ErrorCodes::Interrupted) { LOGV2_OPTIONS(5160301, diff --git a/src/mongo/db/catalog/collection_validation_test.cpp b/src/mongo/db/catalog/collection_validation_test.cpp index 15f8f475aec..f0cfa7842c8 100644 --- a/src/mongo/db/catalog/collection_validation_test.cpp +++ b/src/mongo/db/catalog/collection_validation_test.cpp @@ -45,9 +45,6 @@ namespace { const NamespaceString kNss = NamespaceString("test.t"); class CollectionValidationTest : public CatalogTestFixture { -protected: - CollectionValidationTest(Options options = {}) : CatalogTestFixture(std::move(options)) {} - private: void setUp() override { CatalogTestFixture::setUp(); @@ -59,12 +56,6 @@ private: }; }; -// Background validation opens checkpoint cursors which requires reading from the disk. -class CollectionValidationDiskTest : public CollectionValidationTest { -protected: - CollectionValidationDiskTest() : CollectionValidationTest(Options{}.ephemeral(false)) {} -}; - /** * Calls validate on collection kNss with both kValidateFull and kValidateNormal validation levels * and verifies the results. @@ -218,7 +209,7 @@ TEST_F(CollectionValidationTest, ValidateEmpty) { /*numInvalidDocuments*/ 0, /*numErrors*/ 0); } -TEST_F(CollectionValidationDiskTest, BackgroundValidateEmpty) { +TEST_F(CollectionValidationTest, BackgroundValidateEmpty) { backgroundValidate(operationContext(), /*valid*/ true, /*numRecords*/ 0, @@ -236,7 +227,7 @@ TEST_F(CollectionValidationTest, Validate) { /*numInvalidDocuments*/ 0, /*numErrors*/ 0); } -TEST_F(CollectionValidationDiskTest, BackgroundValidate) { +TEST_F(CollectionValidationTest, BackgroundValidate) { auto opCtx = operationContext(); backgroundValidate(opCtx, /*valid*/ true, @@ -255,7 +246,7 @@ TEST_F(CollectionValidationTest, ValidateError) { /*numInvalidDocuments*/ 1, /*numErrors*/ 1); } -TEST_F(CollectionValidationDiskTest, BackgroundValidateError) { +TEST_F(CollectionValidationTest, BackgroundValidateError) { auto opCtx = operationContext(); backgroundValidate(opCtx, /*valid*/ false, @@ -289,7 +280,7 @@ void waitUntilValidateFailpointHasBeenReached() { ASSERT(CollectionValidation::getIsValidationPausedForTest()); } -TEST_F(CollectionValidationDiskTest, BackgroundValidateRunsConcurrentlyWithWrites) { +TEST_F(CollectionValidationTest, BackgroundValidateRunsConcurrentlyWithWrites) { auto opCtx = operationContext(); auto serviceContext = opCtx->getServiceContext(); diff --git a/src/mongo/db/catalog/validate_state.cpp b/src/mongo/db/catalog/validate_state.cpp index 0f9301c31f8..2fb6505ce1b 100644 --- a/src/mongo/db/catalog/validate_state.cpp +++ b/src/mongo/db/catalog/validate_state.cpp @@ -40,7 +40,6 @@ #include "mongo/db/db_raii.h" #include "mongo/db/index/index_access_method.h" #include "mongo/db/operation_context.h" -#include "mongo/db/storage/durable_catalog.h" #include "mongo/logv2/log.h" #include "mongo/util/fail_point.h" @@ -70,6 +69,7 @@ ValidateState::ValidateState(OperationContext* opCtx, // being validated. _noPBWM.emplace(opCtx->lockState()); + _globalLock.emplace(opCtx, MODE_IS); _databaseLock.emplace(opCtx, _nss.db(), MODE_IS); _collectionLock.emplace(opCtx, _nss, MODE_IS); } else { @@ -196,6 +196,11 @@ void ValidateState::_yieldCursors(OperationContext* opCtx) { _traverseRecordStoreCursor->save(); _seekRecordStoreCursor->save(); + if (isBackground() && _validateTs) { + // Reset snapshot to help ameliorate WiredTiger cache pressure. + opCtx->recoveryUnit()->refreshSnapshot(); + } + // Restore all the cursors. for (const auto& indexCursor : _indexCursors) { indexCursor.second->restore(); @@ -213,12 +218,25 @@ void ValidateState::initializeCursors(OperationContext* opCtx) { invariant(!_traverseRecordStoreCursor && !_seekRecordStoreCursor && _indexCursors.size() == 0 && _indexes.size() == 0); - // Background validation reads from the last stable checkpoint instead of the latest data. This - // allows concurrent writes to go ahead without interfering with validation's view of the data. + // Background validation (on replica sets) will read from a snapshot opened on the kNoOverlap + // read source, which is the minimum of the last applied and all durable timestamps, instead of + // the latest data. Using the kNoOverlap read source prevents us from having to take the PBWM + // lock, which blocks replication. We cannot solely rely on the all durable timestamp as it can + // be set while we're in the middle of applying a batch on secondary nodes. + // Background validation on standalones uses the kNoTimestamp read source because standalones + // have no timestamps to use for maintaining a consistent snapshot. RecoveryUnit::ReadSource rs = RecoveryUnit::ReadSource::kNoTimestamp; if (isBackground()) { opCtx->recoveryUnit()->abandonSnapshot(); - rs = RecoveryUnit::ReadSource::kCheckpoint; + // Background validation is expecting to read from the no overlap timestamp, but + // standalones do not support timestamps. Therefore, if this process is currently running as + // a standalone, don't use a timestamp. + + if (repl::ReplicationCoordinator::get(opCtx)->isReplEnabled()) { + rs = RecoveryUnit::ReadSource::kNoOverlap; + } else { + rs = RecoveryUnit::ReadSource::kNoTimestamp; + } opCtx->recoveryUnit()->setTimestampReadSource(rs); } @@ -229,111 +247,34 @@ void ValidateState::initializeCursors(OperationContext* opCtx) { _dataThrottle.turnThrottlingOff(); } - // Capture the checkpointTimestamp before and after opening the cursors. If it has moved, the - // cursors are out of sync. - auto storageEngine = opCtx->getServiceContext()->getStorageEngine(); - boost::optional<Timestamp> checkpointTimestamp = boost::none; - boost::optional<Timestamp> currCheckpointTimestamp = boost::none; - do { - _indexCursors.clear(); - _indexes.clear(); - checkpointTimestamp = storageEngine->getLastStableRecoveryTimestamp(); - StringSet readyDurableIndexes; - try { - _traverseRecordStoreCursor = std::make_unique<SeekableRecordThrottleCursor>( - opCtx, _collection->getRecordStore(), &_dataThrottle); - _seekRecordStoreCursor = std::make_unique<SeekableRecordThrottleCursor>( - opCtx, _collection->getRecordStore(), &_dataThrottle); - DurableCatalog::get(opCtx)->getReadyIndexes( - opCtx, _collection->getCatalogId(), &readyDurableIndexes); - } catch (const ExceptionFor<ErrorCodes::CursorNotFound>& ex) { - invariant(isBackground()); - // End the validation if we can't open a checkpoint cursor on the collection. - LOGV2( - 6868900, - "Skipping background validation because the collection is not yet in a checkpoint", - "nss"_attr = _nss, - "ex"_attr = ex); - throw; - } + _traverseRecordStoreCursor = std::make_unique<SeekableRecordThrottleCursor>( + opCtx, _collection->getRecordStore(), &_dataThrottle); + _seekRecordStoreCursor = std::make_unique<SeekableRecordThrottleCursor>( + opCtx, _collection->getRecordStore(), &_dataThrottle); - const IndexCatalog* indexCatalog = _collection->getIndexCatalog(); - // The index iterator for ready indexes is timestamp-aware and will only return indexes that - // are visible at our read time. - const auto it = - indexCatalog->getIndexIterator(opCtx, IndexCatalog::InclusionPolicy::kReady); - while (it->more()) { - const IndexCatalogEntry* entry = it->next(); - const IndexDescriptor* desc = entry->descriptor(); - - // Filter out any in-memory index in the collection that is not in our PIT view of the - // MDB catalog. This is only important when background:true because we are then reading - // from the checkpoint's view of the MDB catalog and data. - if (isBackground() && - readyDurableIndexes.find(desc->indexName()) == readyDurableIndexes.end()) { - LOGV2( - 6868901, - "Skipping background validation on the index because the index is not yet in a " - "checkpoint.", - "desc_indexName"_attr = desc->indexName(), - "nss"_attr = _nss); - continue; - } + if (rs != RecoveryUnit::ReadSource::kNoTimestamp) { + invariant(rs == RecoveryUnit::ReadSource::kNoOverlap); + invariant(isBackground()); + _validateTs = opCtx->recoveryUnit()->getPointInTimeReadTimestamp(opCtx); + } - // Read the index's ident from disk (the checkpoint if background:true). If it does not - // match the in-memory ident saved in the IndexCatalogEntry, then our PIT view of the - // index is old and the index has been dropped and recreated. In this case we will skip - // it since there is no utility in checking a dropped index (we also cannot currently - // access it because its in-memory representation is gone). - auto diskIndexIdent = - opCtx->getServiceContext()->getStorageEngine()->getCatalog()->getIndexIdent( - opCtx, _collection->getCatalogId(), desc->indexName()); - if (entry->getIdent() != diskIndexIdent) { - LOGV2(6868902, - "Skipping validation on the index because the index was recreated and is not " - "yet in a checkpoint.", - "desc_indexName"_attr = desc->indexName(), - "nss"_attr = _nss); - continue; - } + const IndexCatalog* indexCatalog = _collection->getIndexCatalog(); + // The index iterator for ready indexes is timestamp-aware and will only return indexes that + // are visible at our read time. + const auto it = indexCatalog->getIndexIterator(opCtx, IndexCatalog::InclusionPolicy::kReady); + while (it->more()) { + const IndexCatalogEntry* entry = it->next(); + const IndexDescriptor* desc = entry->descriptor(); - auto iam = entry->accessMethod()->asSortedData(); - if (!iam) - continue; - - _indexCursors.emplace( - desc->indexName(), - std::make_unique<SortedDataInterfaceThrottleCursor>(opCtx, iam, &_dataThrottle)); - - // Skip any newly created indexes that, because they were built with a WT bulk loader, - // are checkpoint'ed but not yet consistent with the rest of checkpoint's PIT view of - // the data. - if (isBackground() && - opCtx->getServiceContext()->getStorageEngine()->isInIndividuallyCheckpointedIndexes( - diskIndexIdent)) { - _indexCursors.erase(desc->indexName()); - LOGV2( - 6868903, - "Skipping background validation on the index because the index data is not yet " - "consistent in the checkpoint.", - "desc_indexName"_attr = desc->indexName(), - "nss"_attr = _nss); - continue; - } + auto iam = entry->accessMethod()->asSortedData(); + if (!iam) + continue; - _indexes.push_back(indexCatalog->getEntryShared(desc)); - } - currCheckpointTimestamp = - isBackground() ? storageEngine->getLastStableRecoveryTimestamp() : checkpointTimestamp; - // We will retry if a checkpoint happens during opening the cursors or break out of the loop - // for foreground validation. Due to the limited number of indexes a collection can have, it - // is expected to have at most one retry. - } while (currCheckpointTimestamp != checkpointTimestamp); + _indexCursors.emplace( + desc->indexName(), + std::make_unique<SortedDataInterfaceThrottleCursor>(opCtx, iam, &_dataThrottle)); - if (rs != RecoveryUnit::ReadSource::kNoTimestamp) { - invariant(rs == RecoveryUnit::ReadSource::kCheckpoint); - invariant(isBackground()); - _validateTs = checkpointTimestamp; + _indexes.push_back(indexCatalog->getEntryShared(desc)); } // Because SeekableRecordCursors don't have a method to reset to the start, we save and then diff --git a/src/mongo/db/catalog/validate_state.h b/src/mongo/db/catalog/validate_state.h index c508a6b5850..140a4a24fb1 100644 --- a/src/mongo/db/catalog/validate_state.h +++ b/src/mongo/db/catalog/validate_state.h @@ -177,7 +177,8 @@ public: /** * Initializes all the cursors to be used during validation and moves the traversal record - * store cursor to the first record. + * store cursor to the first record. For background validation, this should be called while + * holding the checkpoint lock when performing a background validation. */ void initializeCursors(OperationContext* opCtx); @@ -223,7 +224,8 @@ private: /** * Saves and restores the open cursors to release snapshots and minimize cache pressure for - * validation. + * validation. For background validation, also refreshes the snapshot by starting a new storage + * transaction. */ void _yieldCursors(OperationContext* opCtx); diff --git a/src/mongo/db/catalog/validate_state_test.cpp b/src/mongo/db/catalog/validate_state_test.cpp index caababd402a..a3ccd77ff58 100644 --- a/src/mongo/db/catalog/validate_state_test.cpp +++ b/src/mongo/db/catalog/validate_state_test.cpp @@ -36,7 +36,6 @@ #include "mongo/db/op_observer/op_observer_registry.h" #include "mongo/db/op_observer/oplog_writer_mock.h" #include "mongo/db/repl/replication_coordinator.h" -#include "mongo/db/storage/durable_catalog.h" #include "mongo/db/storage/snapshot_manager.h" #include "mongo/db/storage/wiredtiger/wiredtiger_global_options.h" #include "mongo/util/fail_point.h" @@ -51,9 +50,7 @@ namespace { const NamespaceString kNss = NamespaceString("fooDB.fooColl"); class ValidateStateTest : public CatalogTestFixture { -protected: - ValidateStateTest(Options options = {}) : CatalogTestFixture(std::move(options)) {} - +public: /** * Create collection 'nss'. It will possess a default _id index. */ @@ -68,12 +65,6 @@ private: void setUp() override; }; -// Background validation opens checkpoint cursors which requires reading from the disk. -class ValidateStateDiskTest : public ValidateStateTest { -protected: - ValidateStateDiskTest() : ValidateStateTest(Options{}.ephemeral(false)) {} -}; - void ValidateStateTest::createCollection(OperationContext* opCtx, const NamespaceString& nss) { // Create collection. CollectionOptions defaultCollectionOptions; @@ -179,8 +170,7 @@ TEST_F(ValidateStateTest, NonExistentCollectionShouldThrowNamespaceNotFoundError ErrorCodes::NamespaceNotFound); } -// Validate with {background:true} should fail to find an uncheckpoint'ed collection. -TEST_F(ValidateStateDiskTest, UncheckpointedCollectionShouldThrowCursorNotFoundError) { +TEST_F(ValidateStateTest, UncheckpointedCollectionShouldBeAbleToInitializeCursors) { auto opCtx = operationContext(); // Disable periodic checkpoint'ing thread so we can control when checkpoints occur. @@ -189,16 +179,16 @@ TEST_F(ValidateStateDiskTest, UncheckpointedCollectionShouldThrowCursorNotFoundE // Checkpoint of all of the data. opCtx->recoveryUnit()->waitUntilUnjournaledWritesDurable(opCtx, /*stableCheckpoint*/ false); - // Create the collection, which will not be in the checkpoint, and check that a CursorNotFound - // error is thrown when attempting to open cursors. createCollectionAndPopulateIt(opCtx, kNss); CollectionValidation::ValidateState validateState( opCtx, kNss, CollectionValidation::ValidateMode::kBackground, CollectionValidation::RepairMode::kNone); - ASSERT_THROWS_CODE( - validateState.initializeCursors(opCtx), AssertionException, ErrorCodes::CursorNotFound); + // Assert that cursors are able to created on the new collection. + validateState.initializeCursors(opCtx); + // There should only be a first record id if cursors were initialized successfully. + ASSERT(!validateState.getFirstRecordId().isNull()); } // Basic test with {background:false} to open cursors against all collection indexes. @@ -244,8 +234,8 @@ TEST_F(ValidateStateTest, OpenCursorsOnAllIndexes) { ASSERT_EQ(validateState.getIndexes().size(), 5); } -// Open cursors against checkpoint'ed indexes with {background:true}. -TEST_F(ValidateStateDiskTest, OpenCursorsOnCheckpointedIndexes) { +// Open cursors against all indexes with {background:true}. +TEST_F(ValidateStateTest, OpenCursorsOnAllIndexesWithBackground) { auto opCtx = operationContext(); createCollectionAndPopulateIt(opCtx, kNss); @@ -269,57 +259,14 @@ TEST_F(ValidateStateDiskTest, OpenCursorsOnCheckpointedIndexes) { CollectionValidation::RepairMode::kNone); validateState.initializeCursors(opCtx); - // Make sure the uncheckpoint'ed indexes are not found. - // (Note the _id index was create with collection creation, so we have 3 indexes.) - ASSERT_EQ(validateState.getIndexes().size(), 3); -} - -// Only open cursors against indexes that are consistent with the rest of the checkpoint'ed data. -TEST_F(ValidateStateDiskTest, OpenCursorsOnConsistentlyCheckpointedIndexes) { - auto opCtx = operationContext(); - createCollectionAndPopulateIt(opCtx, kNss); - - // Disable periodic checkpoint'ing thread so we can control when checkpoints occur. - FailPointEnableBlock failPoint("pauseCheckpointThread"); - - // Create several indexes. - createIndex(opCtx, kNss, BSON("a" << 1)); - createIndex(opCtx, kNss, BSON("b" << 1)); - createIndex(opCtx, kNss, BSON("c" << 1)); - createIndex(opCtx, kNss, BSON("d" << 1)); - - // Checkpoint the indexes. - opCtx->recoveryUnit()->waitUntilUnjournaledWritesDurable(opCtx, /*stableCheckpoint*/ false); - - { - // Artificially set two indexes as inconsistent with the checkpoint. - AutoGetCollection autoColl(opCtx, kNss, MODE_IS); - auto indexIdentA = - opCtx->getServiceContext()->getStorageEngine()->getCatalog()->getIndexIdent( - opCtx, autoColl.getCollection()->getCatalogId(), "a_1"); - auto indexIdentB = - opCtx->getServiceContext()->getStorageEngine()->getCatalog()->getIndexIdent( - opCtx, autoColl.getCollection()->getCatalogId(), "b_1"); - opCtx->getServiceContext()->getStorageEngine()->addIndividuallyCheckpointedIndex( - indexIdentA); - opCtx->getServiceContext()->getStorageEngine()->addIndividuallyCheckpointedIndex( - indexIdentB); - } - - // The two inconsistent indexes should not be found. - // (Note the _id index was create with collection creation, so we have 3 indexes.) - CollectionValidation::ValidateState validateState( - opCtx, - kNss, - CollectionValidation::ValidateMode::kBackground, - CollectionValidation::RepairMode::kNone); - validateState.initializeCursors(opCtx); - ASSERT_EQ(validateState.getIndexes().size(), 3); + // We should be able to open a cursor on each index. + // (Note the _id index was create with collection creation, so we have 5 indexes.) + ASSERT_EQ(validateState.getIndexes().size(), 5); } // Indexes in the checkpoint that were dropped in the present should not have cursors opened against // them. -TEST_F(ValidateStateDiskTest, CursorsAreNotOpenedAgainstCheckpointedIndexesThatWereLaterDropped) { +TEST_F(ValidateStateTest, CursorsAreNotOpenedAgainstCheckpointedIndexesThatWereLaterDropped) { auto opCtx = operationContext(); createCollectionAndPopulateIt(opCtx, kNss); diff --git a/src/mongo/db/storage/durable_catalog.h b/src/mongo/db/storage/durable_catalog.h index b9109819256..68cedb89f32 100644 --- a/src/mongo/db/storage/durable_catalog.h +++ b/src/mongo/db/storage/durable_catalog.h @@ -212,10 +212,6 @@ public: virtual int getTotalIndexCount(OperationContext* opCtx, const RecordId& catalogId) const = 0; - virtual void getReadyIndexes(OperationContext* opCtx, - RecordId catalogId, - StringSet* names) const = 0; - virtual bool isIndexPresent(OperationContext* opCtx, const RecordId& catalogId, StringData indexName) const = 0; diff --git a/src/mongo/db/storage/durable_catalog_impl.cpp b/src/mongo/db/storage/durable_catalog_impl.cpp index 6f138457995..7d4f23d88b5 100644 --- a/src/mongo/db/storage/durable_catalog_impl.cpp +++ b/src/mongo/db/storage/durable_catalog_impl.cpp @@ -821,21 +821,6 @@ int DurableCatalogImpl::getTotalIndexCount(OperationContext* opCtx, return md->getTotalIndexCount(); } -void DurableCatalogImpl::getReadyIndexes(OperationContext* opCtx, - RecordId catalogId, - StringSet* names) const { - auto md = getMetaData(opCtx, catalogId); - - if (!md) { - return; - } - - for (const auto& index : md->indexes) { - if (index.ready) - names->insert(index.spec["name"].String()); - } -} - bool DurableCatalogImpl::isIndexPresent(OperationContext* opCtx, const RecordId& catalogId, StringData indexName) const { diff --git a/src/mongo/db/storage/durable_catalog_impl.h b/src/mongo/db/storage/durable_catalog_impl.h index 0bfc3f37a7f..30e7af5918c 100644 --- a/src/mongo/db/storage/durable_catalog_impl.h +++ b/src/mongo/db/storage/durable_catalog_impl.h @@ -146,8 +146,6 @@ public: int getTotalIndexCount(OperationContext* opCtx, const RecordId& catalogId) const; - void getReadyIndexes(OperationContext* opCtx, RecordId catalogId, StringSet* names) const; - bool isIndexPresent(OperationContext* opCtx, const RecordId& catalogId, StringData indexName) const; diff --git a/src/mongo/db/storage/recovery_unit.h b/src/mongo/db/storage/recovery_unit.h index 9464bab4fb1..8135aea2d22 100644 --- a/src/mongo/db/storage/recovery_unit.h +++ b/src/mongo/db/storage/recovery_unit.h @@ -425,6 +425,11 @@ public: } /** + * Refreshes a read transaction by resetting the snapshot in use + */ + virtual void refreshSnapshot() {} + + /** * The ReadSource indicates which external or provided timestamp to read from for future * transactions. */ diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_cursor.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_cursor.cpp index 49346bfc671..a1c6924a4cd 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_cursor.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_cursor.cpp @@ -93,12 +93,7 @@ WiredTigerCursor::WiredTigerCursor(const std::string& uri, } WiredTigerCursor::~WiredTigerCursor() { - if (_isCheckpoint) { - // Closes the checkpoint cursor to avoid outdated data view when opening a new one. - _session->closeCursor(_cursor); - } else { - _session->releaseCursor(_tableID, _cursor, _config); - } + _session->releaseCursor(_tableID, _cursor, _config); } void WiredTigerCursor::reset() { diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp index a4909b310a7..4b02fe86417 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp @@ -1927,12 +1927,12 @@ void WiredTigerKVEngine::_checkpoint(WT_SESSION* session) { // Third, stableTimestamp >= initialDataTimestamp: Take stable checkpoint. Steady state // case. if (initialDataTimestamp.asULL() <= 1) { + clearIndividuallyCheckpointedIndexes(); invariantWTOK(session->checkpoint(session, "use_timestamp=false"), session); LOGV2_FOR_RECOVERY(5576602, 2, "Completed unstable checkpoint.", "initialDataTimestamp"_attr = initialDataTimestamp.toString()); - clearIndividuallyCheckpointedIndexes(); } else if (stableTimestamp < initialDataTimestamp) { LOGV2_FOR_RECOVERY( 23985, @@ -1950,8 +1950,8 @@ void WiredTigerKVEngine::_checkpoint(WT_SESSION* session) { "oplogNeededForRollback"_attr = toString(oplogNeededForRollback)); { - invariantWTOK(session->checkpoint(session, "use_timestamp=true"), session); clearIndividuallyCheckpointedIndexes(); + invariantWTOK(session->checkpoint(session, "use_timestamp=true"), session); } if (oplogNeededForRollback.isOK()) { diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.cpp index 9d3995562c1..e9888b40e0e 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.cpp @@ -372,6 +372,21 @@ void WiredTigerRecoveryUnit::preallocateSnapshotForOplogRead() { preallocateSnapshot(); } +void WiredTigerRecoveryUnit::refreshSnapshot() { + // Currently, this code only works for kNoOverlap or kNoTimestamp. + invariant(_timestampReadSource == ReadSource::kNoOverlap || + _timestampReadSource == ReadSource::kNoTimestamp); + invariant(_isActive()); + invariant(!_inUnitOfWork()); + invariant(!_noEvictionAfterRollback); + invariant(_abandonSnapshotMode == AbandonSnapshotMode::kAbort); + + auto session = _session->getSession(); + invariantWTOK(session->reset_snapshot(session), session); + LOGV2_DEBUG( + 6235000, 3, "WT refreshed snapshot", "snapshotId"_attr = getSnapshotId().toNumber()); +} + void WiredTigerRecoveryUnit::_txnClose(bool commit) { invariant(_isActive(), toString(_getState())); diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h b/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h index 26102686dbd..5397b91fb91 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h @@ -181,6 +181,8 @@ public: std::shared_ptr<StorageStats> getOperationStatistics() const override; + void refreshSnapshot() override; + void ignoreAllMultiTimestampConstraints() { _multiTimestampConstraintTracker.ignoreAllMultiTimestampConstraints = true; } diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit_test.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit_test.cpp index 9757ca278a2..d6bf0e62721 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit_test.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_recovery_unit_test.cpp @@ -623,7 +623,7 @@ TEST_F(WiredTigerRecoveryUnitTestFixture, CommitTimestampAfterSetTimestampOnAbor ASSERT(!commitTs); } -TEST_F(WiredTigerRecoveryUnitTestFixture, CheckpointCursorsAreNotCached) { +TEST_F(WiredTigerRecoveryUnitTestFixture, CheckpointCursorsCached) { auto opCtx = clientAndCtx1.second.get(); // Hold the global lock throughout the test to avoid having the global lock destructor @@ -663,8 +663,7 @@ TEST_F(WiredTigerRecoveryUnitTestFixture, CheckpointCursorsAreNotCached) { // Force a checkpoint. engine->flushAllFiles(opCtx, /*callerHoldsReadLock*/ false); - // Test 2: Checkpoint cursors are not expected to be cached, they - // should be immediately closed when destructed. + // Test 2: Checkpoint cursors will be released into the cache. ru->setTimestampReadSource(WiredTigerRecoveryUnit::ReadSource::kCheckpoint); // Close any cached cursors to establish a new 'before' state. @@ -674,9 +673,9 @@ TEST_F(WiredTigerRecoveryUnitTestFixture, CheckpointCursorsAreNotCached) { // Will search the checkpoint cursor for the record, then release the checkpoint cursor. ASSERT_TRUE(rs->findRecord(opCtx, s.getValue(), &rd)); - // No new cursors should have been released into the cache, with the exception of a metadata - // cursor that is opened to determine if the table is LSM. Metadata cursors are cached. - ASSERT_EQ(ru->getSession()->cachedCursors(), cachedCursorsBefore + 1); + // A new cursor should have been released into the cache, along with a metadata cursor that is + // opened to determine if the table is LSM. Metadata cursors are cached. + ASSERT_GT(ru->getSession()->cachedCursors(), cachedCursorsBefore + 1); // All opened cursors are closed. ASSERT_EQ(0, ru->getSession()->cursorsOut()); diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_session_cache.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_session_cache.cpp index cf35fca7041..22a30da3cb5 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_session_cache.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_session_cache.cpp @@ -295,8 +295,8 @@ void WiredTigerSessionCache::waitUntilDurable(OperationContext* opCtx, auto config = syncType == Fsync::kCheckpointStableTimestamp ? "use_timestamp=true" : "use_timestamp=false"; { - invariantWTOK(s->checkpoint(s, config), s); _engine->clearIndividuallyCheckpointedIndexes(); + invariantWTOK(s->checkpoint(s, config), s); } if (token) { @@ -349,10 +349,10 @@ void WiredTigerSessionCache::waitUntilDurable(OperationContext* opCtx, _waitUntilDurableSession); LOGV2_DEBUG(22419, 4, "flushed journal"); } else { + _engine->clearIndividuallyCheckpointedIndexes(); invariantWTOK(_waitUntilDurableSession->checkpoint(_waitUntilDurableSession, nullptr), _waitUntilDurableSession); LOGV2_DEBUG(22420, 4, "created checkpoint"); - _engine->clearIndividuallyCheckpointedIndexes(); } if (token) { diff --git a/src/mongo/dbtests/validate_tests.cpp b/src/mongo/dbtests/validate_tests.cpp index 20cbdae9fa7..4674a21de29 100644 --- a/src/mongo/dbtests/validate_tests.cpp +++ b/src/mongo/dbtests/validate_tests.cpp @@ -122,13 +122,6 @@ public: } protected: - void forceCheckpoint(bool background) { - if (background) { - // Checkpoint all of the data. - _opCtx.getServiceContext()->getStorageEngine()->checkpoint(); - } - } - ValidateResults runValidate() { // validate() will set a kCheckpoint read source. Callers continue to do operations after // running validate, so we must reset the read source back to normal before returning. @@ -148,7 +141,6 @@ protected: ValidateResults results; BSONObjBuilder output; - forceCheckpoint(_background); ASSERT_OK(CollectionValidation::validate( &_opCtx, _nss, mode, repairMode, &results, &output, kTurnOnExtraLoggingForTest)); @@ -3365,15 +3357,6 @@ public: ValidateResults results; BSONObjBuilder output; - // validate() will set a kCheckpoint read source. Callers continue to do operations - // after running validate, so we must reset the read source back to normal before - // returning. - auto originalReadSource = _opCtx.recoveryUnit()->getTimestampReadSource(); - ON_BLOCK_EXIT([&] { - _opCtx.recoveryUnit()->abandonSnapshot(); - _opCtx.recoveryUnit()->setTimestampReadSource(originalReadSource); - }); - forceCheckpoint(_background); ASSERT_OK(CollectionValidation::validate(&_opCtx, _nss, mode, @@ -4176,15 +4159,6 @@ public: ValidateResults results; BSONObjBuilder output; - // validate() will set a kCheckpoint read source. Callers continue to do operations - // after running validate, so we must reset the read source back to normal before - // returning. - auto originalReadSource = _opCtx.recoveryUnit()->getTimestampReadSource(); - ON_BLOCK_EXIT([&] { - _opCtx.recoveryUnit()->abandonSnapshot(); - _opCtx.recoveryUnit()->setTimestampReadSource(originalReadSource); - }); - forceCheckpoint(_background); ASSERT_OK(CollectionValidation::validate(&_opCtx, _nss, mode, @@ -4290,15 +4264,6 @@ public: ValidateResults results; BSONObjBuilder output; - // validate() will set a kCheckpoint read source. Callers continue to do operations - // after running validate, so we must reset the read source back to normal before - // returning. - auto originalReadSource = _opCtx.recoveryUnit()->getTimestampReadSource(); - ON_BLOCK_EXIT([&] { - _opCtx.recoveryUnit()->abandonSnapshot(); - _opCtx.recoveryUnit()->setTimestampReadSource(originalReadSource); - }); - forceCheckpoint(_background); ASSERT_OK(CollectionValidation::validate(&_opCtx, _nss, mode, @@ -4401,15 +4366,6 @@ public: ValidateResults results; BSONObjBuilder output; - // validate() will set a kCheckpoint read source. Callers continue to do operations - // after running validate, so we must reset the read source back to normal before - // returning. - auto originalReadSource = _opCtx.recoveryUnit()->getTimestampReadSource(); - ON_BLOCK_EXIT([&] { - _opCtx.recoveryUnit()->abandonSnapshot(); - _opCtx.recoveryUnit()->setTimestampReadSource(originalReadSource); - }); - forceCheckpoint(_background); ASSERT_OK(CollectionValidation::validate(&_opCtx, _nss, mode, @@ -4441,15 +4397,6 @@ public: ValidateResults results; BSONObjBuilder output; - // validate() will set a kCheckpoint read source. Callers continue to do operations - // after running validate, so we must reset the read source back to normal before - // returning. - auto originalReadSource = _opCtx.recoveryUnit()->getTimestampReadSource(); - ON_BLOCK_EXIT([&] { - _opCtx.recoveryUnit()->abandonSnapshot(); - _opCtx.recoveryUnit()->setTimestampReadSource(originalReadSource); - }); - forceCheckpoint(_background); ASSERT_OK(CollectionValidation::validate(&_opCtx, _nss, mode, |
