summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuhong Zhang <yuhong.zhang@mongodb.com>2023-03-14 18:44:21 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-03-16 20:51:24 +0000
commit6cdf0c2a0f998000f963b91631e1e615ec2977aa (patch)
treeb38a8ee801b6440d12625349d34e4716b85a2ae9
parentbc451106f4be1c9f0f2265debb5a08354359d243 (diff)
downloadmongo-6cdf0c2a0f998000f963b91631e1e615ec2977aa.tar.gz
SERVER-72542 Avoid redundant index traversal during full validation
(cherry picked from commit 571cfa3561d657445ae694bf6a7b120f202b5250)
-rw-r--r--src/mongo/db/catalog/collection_validation.cpp32
-rw-r--r--src/mongo/db/catalog/validate_results.h1
-rw-r--r--src/mongo/db/index/index_access_method.cpp10
-rw-r--r--src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_sorted_impl.cpp8
-rw-r--r--src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp36
5 files changed, 32 insertions, 55 deletions
diff --git a/src/mongo/db/catalog/collection_validation.cpp b/src/mongo/db/catalog/collection_validation.cpp
index dbd119c9ade..1bf11131960 100644
--- a/src/mongo/db/catalog/collection_validation.cpp
+++ b/src/mongo/db/catalog/collection_validation.cpp
@@ -95,14 +95,11 @@ void _validateIndexesInternalStructure(OperationContext* opCtx,
auto& curIndexResults = (results->indexResultsMap)[descriptor->indexName()];
- int64_t numValidated;
- iam->validate(opCtx, &numValidated, &curIndexResults);
+ iam->validate(opCtx, nullptr, &curIndexResults);
if (!curIndexResults.valid) {
results->valid = false;
}
-
- curIndexResults.keysTraversedFromFullValidate = numValidated;
}
}
@@ -134,33 +131,6 @@ void _validateIndexes(OperationContext* opCtx,
auto& curIndexResults = (results->indexResultsMap)[descriptor->indexName()];
curIndexResults.keysTraversed = numTraversedKeys;
- // If we are performing a full index validation, we have information on the number of index
- // keys validated in _validateIndexesInternalStructure (when we validated the internal
- // structure of the index). Check if this is consistent with 'numTraversedKeys' from
- // traverseIndex above.
- if (validateState->isFullIndexValidation()) {
- invariant(opCtx->lockState()->isCollectionLockedForMode(validateState->nss(), MODE_X));
-
- // The number of keys counted in _validateIndexesInternalStructure, when checking the
- // internal structure of the index.
- const int64_t numIndexKeys = curIndexResults.keysTraversedFromFullValidate;
-
- // Check if currIndexResults is valid to ensure that this index is not corrupted or
- // comprised (which was set in _validateIndexesInternalStructure). If the index is
- // corrupted, there is no use in checking if the traversal yielded the same key count.
- if (curIndexResults.valid) {
- if (numIndexKeys != numTraversedKeys) {
- curIndexResults.valid = false;
- string msg = str::stream()
- << "number of traversed index entries (" << numTraversedKeys
- << ") does not match the number of expected index entries (" << numIndexKeys
- << ")";
- results->errors.push_back(msg);
- results->valid = false;
- }
- }
- }
-
if (!curIndexResults.valid) {
results->valid = false;
}
diff --git a/src/mongo/db/catalog/validate_results.h b/src/mongo/db/catalog/validate_results.h
index 83af552e4d9..03f7389d9ab 100644
--- a/src/mongo/db/catalog/validate_results.h
+++ b/src/mongo/db/catalog/validate_results.h
@@ -45,7 +45,6 @@ struct IndexValidateResults {
std::vector<std::string> errors;
std::vector<std::string> warnings;
int64_t keysTraversed = 0;
- int64_t keysTraversedFromFullValidate = 0;
};
using ValidateResultsMap = std::map<std::string, IndexValidateResults>;
diff --git a/src/mongo/db/index/index_access_method.cpp b/src/mongo/db/index/index_access_method.cpp
index ac4ba3a2671..cf276f8f6fc 100644
--- a/src/mongo/db/index/index_access_method.cpp
+++ b/src/mongo/db/index/index_access_method.cpp
@@ -415,9 +415,13 @@ RecordId AbstractIndexAccessMethod::findSingle(OperationContext* opCtx,
void AbstractIndexAccessMethod::validate(OperationContext* opCtx,
int64_t* numKeys,
IndexValidateResults* fullResults) const {
- long long keys = 0;
- _newInterface->fullValidate(opCtx, &keys, fullResults);
- *numKeys = keys;
+ if (numKeys) {
+ long long keys = 0;
+ _newInterface->fullValidate(opCtx, &keys, fullResults);
+ *numKeys = keys;
+ } else {
+ _newInterface->fullValidate(opCtx, nullptr, fullResults);
+ }
}
bool AbstractIndexAccessMethod::appendCustomStats(OperationContext* opCtx,
diff --git a/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_sorted_impl.cpp b/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_sorted_impl.cpp
index 52be28c2183..93839c1d4ab 100644
--- a/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_sorted_impl.cpp
+++ b/src/mongo/db/storage/ephemeral_for_test/ephemeral_for_test_sorted_impl.cpp
@@ -1459,7 +1459,9 @@ void SortedDataInterfaceUnique::fullValidate(OperationContext* opCtx,
numKeys += UniqueIndexData(it->second, _rsKeyFormat).size();
++it;
}
- *numKeysOut = numKeys;
+ if (numKeysOut) {
+ *numKeysOut = numKeys;
+ }
}
bool SortedDataInterfaceBase::appendCustomStats(OperationContext* opCtx,
@@ -1599,7 +1601,9 @@ void SortedDataInterfaceStandard::fullValidate(OperationContext* opCtx,
++numKeys;
++it;
}
- *numKeysOut = numKeys;
+ if (numKeysOut) {
+ *numKeysOut = numKeys;
+ }
}
std::unique_ptr<mongo::SortedDataInterface::Cursor> SortedDataInterfaceStandard::newCursor(
diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp
index 9d009390762..bae8e9888de 100644
--- a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp
+++ b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp
@@ -348,25 +348,25 @@ void WiredTigerIndex::fullValidate(OperationContext* opCtx,
}
}
- auto cursor = newCursor(opCtx);
- long long count = 0;
- LOGV2_TRACE_INDEX(20094, "fullValidate");
-
- const auto requestedInfo = TRACING_ENABLED ? Cursor::kKeyAndLoc : Cursor::kJustExistance;
-
- KeyString::Value keyStringForSeek =
- IndexEntryComparison::makeKeyStringFromBSONKeyForSeek(BSONObj(),
- getKeyStringVersion(),
- getOrdering(),
- true, /* forward */
- true /* inclusive */
- );
-
- for (auto kv = cursor->seek(keyStringForSeek, requestedInfo); kv; kv = cursor->next()) {
- LOGV2_TRACE_INDEX(20095, "fullValidate {kv}", "kv"_attr = kv);
- count++;
- }
if (numKeysOut) {
+ auto cursor = newCursor(opCtx);
+ long long count = 0;
+ LOGV2_TRACE_INDEX(20094, "fullValidate");
+
+ const auto requestedInfo = TRACING_ENABLED ? Cursor::kKeyAndLoc : Cursor::kJustExistance;
+
+ KeyString::Value keyStringForSeek =
+ IndexEntryComparison::makeKeyStringFromBSONKeyForSeek(BSONObj(),
+ getKeyStringVersion(),
+ getOrdering(),
+ true, /* forward */
+ true /* inclusive */
+ );
+
+ for (auto kv = cursor->seek(keyStringForSeek, requestedInfo); kv; kv = cursor->next()) {
+ LOGV2_TRACE_INDEX(20095, "fullValidate {kv}", "kv"_attr = kv);
+ count++;
+ }
*numKeysOut = count;
}
}