From ad2b7e93f1e05bc0512d93d03db45a4139e20bb2 Mon Sep 17 00:00:00 2001 From: Pol Pinol Castuera Date: Thu, 24 Nov 2022 07:35:51 +0000 Subject: SERVER-71477 Check '_internalAllCollectionStatsSpec.getStats()' exists before calling 'makeStatsForNs' --- etc/backports_required_for_multiversion_tests.yml | 4 ++++ jstests/sharding/all_collection_stats.js | 4 ++++ .../document_source_internal_all_collection_stats.cpp | 14 ++++++++------ .../document_source_internal_all_collection_stats.h | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/etc/backports_required_for_multiversion_tests.yml b/etc/backports_required_for_multiversion_tests.yml index bfb7da29e79..29c2514d579 100644 --- a/etc/backports_required_for_multiversion_tests.yml +++ b/etc/backports_required_for_multiversion_tests.yml @@ -274,6 +274,8 @@ last-continuous: ticket: SERVER-70436 - test_file: jstests/sharding/prepare_transaction_then_migrate.js ticket: SERVER-68361 + - test_file: jstests/sharding/all_collection_stats.js + ticket: SERVER-71477 suites: null last-lts: all: @@ -623,4 +625,6 @@ last-lts: ticket: SERVER-70436 - test_file: jstests/sharding/prepare_transaction_then_migrate.js ticket: SERVER-68361 + - test_file: jstests/sharding/all_collection_stats.js + ticket: SERVER-71477 suites: null diff --git a/jstests/sharding/all_collection_stats.js b/jstests/sharding/all_collection_stats.js index ac0207bda7b..62a725719cf 100644 --- a/jstests/sharding/all_collection_stats.js +++ b/jstests/sharding/all_collection_stats.js @@ -175,6 +175,10 @@ for (let i = numCollections / 2; i < numCollections; i++) { checkResults(outputData, checksToDo); })(); +// Test valid query with empty specification +assert.commandWorked( + adminDb.runCommand({aggregate: 1, pipeline: [{$_internalAllCollectionStats: {}}], cursor: {}})); + // Test invalid queries/values. assert.commandFailedWithCode( adminDb.runCommand({aggregate: 1, pipeline: [{$_internalAllCollectionStats: 3}], cursor: {}}), diff --git a/src/mongo/db/pipeline/document_source_internal_all_collection_stats.cpp b/src/mongo/db/pipeline/document_source_internal_all_collection_stats.cpp index 73782cd88c4..bbc07b66bc4 100644 --- a/src/mongo/db/pipeline/document_source_internal_all_collection_stats.cpp +++ b/src/mongo/db/pipeline/document_source_internal_all_collection_stats.cpp @@ -62,12 +62,14 @@ DocumentSource::GetNextResult DocumentSourceInternalAllCollectionStats::doGetNex continue; } - try { - return {Document{DocumentSourceCollStats::makeStatsForNs( - pExpCtx, nss, _internalAllCollectionStatsSpec.getStats().get(), _projectFilter)}}; - } catch (const ExceptionFor&) { - // We don't want to retrieve data for views, only for collections. - continue; + if (const auto& stats = _internalAllCollectionStatsSpec.getStats()) { + try { + return {Document{DocumentSourceCollStats::makeStatsForNs( + pExpCtx, nss, stats.get(), _projectFilter)}}; + } catch (const ExceptionFor&) { + // We don't want to retrieve data for views, only for collections. + continue; + } } } diff --git a/src/mongo/db/pipeline/document_source_internal_all_collection_stats.h b/src/mongo/db/pipeline/document_source_internal_all_collection_stats.h index 93b1556b94f..d9a081412f9 100644 --- a/src/mongo/db/pipeline/document_source_internal_all_collection_stats.h +++ b/src/mongo/db/pipeline/document_source_internal_all_collection_stats.h @@ -119,7 +119,7 @@ private: // The specification object given to $_internalAllCollectionStats containing user specified // options. - DocumentSourceInternalAllCollectionStatsSpec _internalAllCollectionStatsSpec; + const DocumentSourceInternalAllCollectionStatsSpec _internalAllCollectionStatsSpec; boost::optional> _catalogDocs; // A $match stage can be absorbed in order to avoid unnecessarily computing the stats for -- cgit v1.2.1