diff options
Diffstat (limited to 'src/mongo/db/commands/plan_cache_commands_test.cpp')
-rw-r--r-- | src/mongo/db/commands/plan_cache_commands_test.cpp | 79 |
1 files changed, 49 insertions, 30 deletions
diff --git a/src/mongo/db/commands/plan_cache_commands_test.cpp b/src/mongo/db/commands/plan_cache_commands_test.cpp index 43d0f7d3830..89f0cc8b0f0 100644 --- a/src/mongo/db/commands/plan_cache_commands_test.cpp +++ b/src/mongo/db/commands/plan_cache_commands_test.cpp @@ -112,17 +112,18 @@ SolutionCacheData* createSolutionCacheData() { /** * Utility function to create a PlanRankingDecision */ -PlanRankingDecision* createDecision(size_t numPlans) { +std::unique_ptr<PlanRankingDecision> createDecision(size_t numPlans, size_t works = 0) { unique_ptr<PlanRankingDecision> why(new PlanRankingDecision()); for (size_t i = 0; i < numPlans; ++i) { CommonStats common("COLLSCAN"); auto stats = stdx::make_unique<PlanStageStats>(common, STAGE_COLLSCAN); stats->specific.reset(new CollectionScanStats()); why->stats.push_back(std::move(stats)); + why->stats[i]->common.works = works; why->scores.push_back(0U); why->candidateOrder.push_back(i); } - return why.release(); + return why; } TEST(PlanCacheCommandsTest, planCacheListQueryShapesEmpty) { @@ -151,7 +152,7 @@ TEST(PlanCacheCommandsTest, planCacheListQueryShapesOneKey) { qs.cacheData.reset(createSolutionCacheData()); std::vector<QuerySolution*> solns; solns.push_back(&qs); - ASSERT_OK(planCache.add(*cq, + ASSERT_OK(planCache.set(*cq, solns, createDecision(1U), opCtx->getServiceContext()->getPreciseClockSource()->now())); @@ -186,7 +187,7 @@ TEST(PlanCacheCommandsTest, planCacheClearAllShapes) { qs.cacheData.reset(createSolutionCacheData()); std::vector<QuerySolution*> solns; solns.push_back(&qs); - ASSERT_OK(planCache.add(*cq, + ASSERT_OK(planCache.set(*cq, solns, createDecision(1U), opCtx->getServiceContext()->getPreciseClockSource()->now())); @@ -327,11 +328,11 @@ TEST(PlanCacheCommandsTest, planCacheClearOneKey) { qs.cacheData.reset(createSolutionCacheData()); std::vector<QuerySolution*> solns; solns.push_back(&qs); - ASSERT_OK(planCache.add(*cqA, + ASSERT_OK(planCache.set(*cqA, solns, createDecision(1U), opCtx->getServiceContext()->getPreciseClockSource()->now())); - ASSERT_OK(planCache.add(*cqB, + ASSERT_OK(planCache.set(*cqB, solns, createDecision(1U), opCtx->getServiceContext()->getPreciseClockSource()->now())); @@ -390,11 +391,11 @@ TEST(PlanCacheCommandsTest, planCacheClearOneKeyCollation) { qs.cacheData.reset(createSolutionCacheData()); std::vector<QuerySolution*> solns; solns.push_back(&qs); - ASSERT_OK(planCache.add(*cq, + ASSERT_OK(planCache.set(*cq, solns, createDecision(1U), opCtx->getServiceContext()->getPreciseClockSource()->now())); - ASSERT_OK(planCache.add(*cqCollation, + ASSERT_OK(planCache.set(*cqCollation, solns, createDecision(1U), opCtx->getServiceContext()->getPreciseClockSource()->now())); @@ -467,14 +468,12 @@ BSONObj getPlan(const BSONElement& elt) { return obj.getOwned(); } -/** - * Utility function to get list of plan IDs for a query in the cache. - */ -vector<BSONObj> getPlans(const PlanCache& planCache, - const BSONObj& query, - const BSONObj& sort, - const BSONObj& projection, - const BSONObj& collation) { +BSONObj getCmdResult(const PlanCache& planCache, + const BSONObj& query, + const BSONObj& sort, + const BSONObj& projection, + const BSONObj& collation) { + QueryTestServiceContext serviceContext; auto opCtx = serviceContext.makeOperationContext(); @@ -489,6 +488,22 @@ vector<BSONObj> getPlans(const PlanCache& planCache, BSONObj cmdObj = cmdObjBuilder.obj(); ASSERT_OK(PlanCacheListPlans::list(opCtx.get(), planCache, nss.ns(), cmdObj, &bob)); BSONObj resultObj = bob.obj(); + + return resultObj; +} + +/** + * Utility function to get list of plan IDs for a query in the cache. + */ +vector<BSONObj> getPlans(const PlanCache& planCache, + const BSONObj& query, + const BSONObj& sort, + const BSONObj& projection, + const BSONObj& collation) { + BSONObj resultObj = getCmdResult(planCache, query, sort, projection, collation); + ASSERT_TRUE(resultObj.hasField("isActive")); + ASSERT_TRUE(resultObj.hasField("works")); + BSONElement plansElt = resultObj.getField("plans"); ASSERT_EQUALS(plansElt.type(), mongo::Array); vector<BSONElement> planEltArray = plansElt.Array(); @@ -539,17 +554,20 @@ TEST(PlanCacheCommandsTest, planCacheListPlansOnlyOneSolutionTrue) { qs.cacheData.reset(createSolutionCacheData()); std::vector<QuerySolution*> solns; solns.push_back(&qs); - ASSERT_OK(planCache.add(*cq, + ASSERT_OK(planCache.set(*cq, solns, - createDecision(1U), + createDecision(1U, 123), opCtx->getServiceContext()->getPreciseClockSource()->now())); - vector<BSONObj> plans = getPlans(planCache, + BSONObj resultObj = getCmdResult(planCache, cq->getQueryObj(), cq->getQueryRequest().getSort(), cq->getQueryRequest().getProj(), cq->getQueryRequest().getCollation()); - ASSERT_EQUALS(plans.size(), 1U); + + ASSERT_EQ(resultObj["plans"].Array().size(), 1u); + ASSERT_EQ(resultObj.getBoolField("isActive"), false); + ASSERT_EQ(resultObj.getIntField("works"), 123L); } TEST(PlanCacheCommandsTest, planCacheListPlansOnlyOneSolutionFalse) { @@ -571,17 +589,20 @@ TEST(PlanCacheCommandsTest, planCacheListPlansOnlyOneSolutionFalse) { std::vector<QuerySolution*> solns; solns.push_back(&qs); solns.push_back(&qs); - ASSERT_OK(planCache.add(*cq, + ASSERT_OK(planCache.set(*cq, solns, - createDecision(2U), + createDecision(2U, 333), opCtx->getServiceContext()->getPreciseClockSource()->now())); - vector<BSONObj> plans = getPlans(planCache, + BSONObj resultObj = getCmdResult(planCache, cq->getQueryObj(), cq->getQueryRequest().getSort(), cq->getQueryRequest().getProj(), cq->getQueryRequest().getCollation()); - ASSERT_EQUALS(plans.size(), 2U); + + ASSERT_EQ(resultObj["plans"].Array().size(), 2u); + ASSERT_EQ(resultObj.getBoolField("isActive"), false); + ASSERT_EQ(resultObj.getIntField("works"), 333); } @@ -611,14 +632,14 @@ TEST(PlanCacheCommandsTest, planCacheListPlansCollation) { qs.cacheData.reset(createSolutionCacheData()); std::vector<QuerySolution*> solns; solns.push_back(&qs); - ASSERT_OK(planCache.add(*cq, + ASSERT_OK(planCache.set(*cq, solns, createDecision(1U), opCtx->getServiceContext()->getPreciseClockSource()->now())); std::vector<QuerySolution*> twoSolns; twoSolns.push_back(&qs); twoSolns.push_back(&qs); - ASSERT_OK(planCache.add(*cqCollation, + ASSERT_OK(planCache.set(*cqCollation, twoSolns, createDecision(2U), opCtx->getServiceContext()->getPreciseClockSource()->now())); @@ -658,11 +679,9 @@ TEST(PlanCacheCommandsTest, planCacheListPlansTimeOfCreationIsCorrect) { std::vector<QuerySolution*> solns; solns.push_back(&qs); auto now = opCtx->getServiceContext()->getPreciseClockSource()->now(); - ASSERT_OK(planCache.add(*cq, solns, createDecision(1U), now)); + ASSERT_OK(planCache.set(*cq, solns, createDecision(1U), now)); - PlanCacheEntry* out; - ASSERT_OK(planCache.getEntry(*cq, &out)); - unique_ptr<PlanCacheEntry> entry(out); + auto entry = unittest::assertGet(planCache.getEntry(*cq)); ASSERT_EQ(entry->timeOfCreation, now); } |