From 91958f623b194a19fa50d18958b24d73726ba488 Mon Sep 17 00:00:00 2001 From: Dan Larkin-York Date: Tue, 7 Mar 2023 17:20:55 +0000 Subject: SERVER-74631 Log index spec when validation detects index key inconsistencies (cherry picked from commit 354b3b2ff1a72bef452b0037a707ecdc6879bff1) --- jstests/noPassthrough/validate_memory_limit.js | 7 +++++++ src/mongo/db/catalog/collection_validation.cpp | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/jstests/noPassthrough/validate_memory_limit.js b/jstests/noPassthrough/validate_memory_limit.js index 5a576743177..63f55a0ed43 100644 --- a/jstests/noPassthrough/validate_memory_limit.js +++ b/jstests/noPassthrough/validate_memory_limit.js @@ -37,6 +37,11 @@ function checkValidate(maxMemoryUsage, {minMissingKeys, maxMissingKeys}) { assert.lte(res.missingIndexEntries.length, maxMissingKeys, tojson(res)); } +function checkValidateLogs() { + assert(checkLog.checkContainsWithAtLeastCountJson( + conn, 7463100, {"spec": {"v": 2, "key": {"_id": 1}, "name": "_id_"}}, 1)); +} + function checkValidateRepair() { const res = coll.validate({repair: true}); assert.commandWorked(res); @@ -50,6 +55,7 @@ const indexKey = "a".repeat(kIndexKeyLength); assert.commandWorked(coll.insert({_id: indexKey})); corruptIndex(); checkValidate(1, {minMissingKeys: 1, maxMissingKeys: 1}); +checkValidateLogs(); // Can't repair successfully if there aren't any index inconsistencies reported. checkValidateRepair(); @@ -69,6 +75,7 @@ corruptIndex(); // each key is counted twice, so realistically we only expect to track 2 of them. However, there's // a small chance we could get hash collisions that would lead to us reporting only 1. checkValidate(1, {minMissingKeys: 1, maxMissingKeys: 2}); +checkValidateLogs(); // Repair, but incompletely if only some inconsistencies are reported. checkValidateRepair(); diff --git a/src/mongo/db/catalog/collection_validation.cpp b/src/mongo/db/catalog/collection_validation.cpp index 31ee797b02c..517c18aaeaf 100644 --- a/src/mongo/db/catalog/collection_validation.cpp +++ b/src/mongo/db/catalog/collection_validation.cpp @@ -242,6 +242,20 @@ void _validateIndexKeyCount(OperationContext* opCtx, } } +void _printIndexSpec(const ValidateState* validateState, StringData indexName) { + auto& indexes = validateState->getIndexes(); + auto indexEntry = + std::find_if(indexes.begin(), + indexes.end(), + [&](const std::shared_ptr indexEntry) -> bool { + return indexEntry->descriptor()->indexName() == indexName; + }); + if (indexEntry != indexes.end()) { + auto indexSpec = (*indexEntry)->descriptor()->infoObj(); + LOGV2_ERROR(7463100, "Index failed validation", "spec"_attr = indexSpec); + } +} + void _reportValidationResults(OperationContext* opCtx, ValidateState* validateState, ValidateResults* results, @@ -262,6 +276,7 @@ void _reportValidationResults(OperationContext* opCtx, for (const auto& [indexName, vr] : results->indexResultsMap) { if (!vr.valid) { results->valid = false; + _printIndexSpec(validateState, indexName); } if (validateState->getSkippedIndexes().contains(indexName)) { -- cgit v1.2.1