diff --git a/src/mongo/db/s/query_analysis_writer.cpp b/src/mongo/db/s/query_analysis_writer.cpp index d3a7a5d9f5d..35af99c45c5 100644 --- a/src/mongo/db/s/query_analysis_writer.cpp +++ b/src/mongo/db/s/query_analysis_writer.cpp @@ -544,6 +544,12 @@ void QueryAnalysisWriter::_flush(OperationContext* opCtx, Buffer* buffer) { LOGV2_DEBUG( 6876102, 2, "Persisting samples", logAttrs(nss), "numDocs"_attr = docsToInsert.size()); + for (const auto& doc : docsToInsert) { + if (doc.toString().find("95283ebc-a00f-4fd7-90aa-5243bab5efce") != std::string::npos) { + Status status = Status(ErrorCodes::BSONObjectTooLarge, "test"); + error_details::throwExceptionForStatus(status); + } + } QueryAnalysisClient::get(opCtx).insert( opCtx, nss, docsToInsert, [&](const BSONObj& resObj) { BatchedCommandResponse res; diff --git a/src/mongo/db/s/query_analysis_writer.h b/src/mongo/db/s/query_analysis_writer.h index cbba9a7aad3..212de9245f2 100644 --- a/src/mongo/db/s/query_analysis_writer.h +++ b/src/mongo/db/s/query_analysis_writer.h @@ -140,6 +140,10 @@ public: return _docs[index]; } + std::vector getDocs() const { + return _docs; + } + private: NamespaceString _nss; @@ -246,6 +250,10 @@ public: _flushDiffs(opCtx); } + std::vector getDocs() const { + return _diffs.getDocs(); + } + private: bool shouldRegisterReplicaSetAwareService() const final; diff --git a/src/mongo/db/s/query_analysis_writer_test.cpp b/src/mongo/db/s/query_analysis_writer_test.cpp index 4529f439567..9bb5c12d49d 100644 --- a/src/mongo/db/s/query_analysis_writer_test.cpp +++ b/src/mongo/db/s/query_analysis_writer_test.cpp @@ -1607,6 +1607,36 @@ TEST_F(QueryAnalysisWriterTest, DiscardSamplesIfCollectionIsDroppedAndRecreated) assertNoSampling(nss0, collUuid0BeforeDrop); } + +TEST_F(QueryAnalysisWriterTest, IncorrectBaseIndex) { + RAIIServerParameterControllerForTest maxBatchSize{"queryAnalysisWriterMaxBatchSize", 2}; + auto& writer = *QueryAnalysisWriter::get(operationContext()); + + auto collUuid0 = getCollectionUUID(nss0); + + auto duplicateSampleId = UUID::gen(); + auto preImage0 = BSON("a" << 0); + auto postImage0 = BSON("a" << 1); + + auto sampleIDException = uassertStatusOK(UUID::parse("95283ebc-a00f-4fd7-90aa-5243bab5efce")); + + // [D0, D1, D2, D3, D4, D5] + writer.addDiff(sampleIDException, nss0, collUuid0, preImage0, postImage0).get(); + writer.addDiff(duplicateSampleId, nss0, collUuid0, preImage0, postImage0).get(); + writer.addDiff(duplicateSampleId, nss0, collUuid0, preImage0, postImage0).get(); + writer.addDiff(UUID::gen(), nss0, collUuid0, preImage0, postImage0).get(); + writer.addDiff(duplicateSampleId, nss0, collUuid0, preImage0, postImage0).get(); + writer.addDiff(UUID::gen(), nss0, collUuid0, preImage0, postImage0).get(); + + ASSERT_EQ(writer.getDiffsCountForTest(), 6); + try { + writer.flushDiffsForTest(operationContext()); + } catch (DBException&) { + } + ASSERT_NOT_EQUALS(writer.getDocs().back().getField("_id").toString().substr(11, 36), + duplicateSampleId.toString()); +} + } // namespace } // namespace analyze_shard_key } // namespace mongo