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 | 109 |
1 files changed, 55 insertions, 54 deletions
diff --git a/src/mongo/db/commands/plan_cache_commands_test.cpp b/src/mongo/db/commands/plan_cache_commands_test.cpp index 4975a557443..1ec3611ccdf 100644 --- a/src/mongo/db/commands/plan_cache_commands_test.cpp +++ b/src/mongo/db/commands/plan_cache_commands_test.cpp @@ -133,7 +133,7 @@ TEST(PlanCacheCommandsTest, planCacheListQueryShapesEmpty) { TEST(PlanCacheCommandsTest, planCacheListQueryShapesOneKey) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Create a canonical query auto qr = stdx::make_unique<QueryRequest>(nss); @@ -142,7 +142,7 @@ TEST(PlanCacheCommandsTest, planCacheListQueryShapesOneKey) { qr->setProj(fromjson("{_id: 0}")); qr->setCollation(fromjson("{locale: 'mock_reverse_string'}")); auto statusWithCQ = CanonicalQuery::canonicalize( - txn.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> cq = std::move(statusWithCQ.getValue()); @@ -168,13 +168,13 @@ TEST(PlanCacheCommandsTest, planCacheListQueryShapesOneKey) { TEST(PlanCacheCommandsTest, planCacheClearAllShapes) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Create a canonical query auto qr = stdx::make_unique<QueryRequest>(nss); qr->setFilter(fromjson("{a: 1}")); auto statusWithCQ = CanonicalQuery::canonicalize( - txn.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> cq = std::move(statusWithCQ.getValue()); @@ -189,7 +189,7 @@ TEST(PlanCacheCommandsTest, planCacheClearAllShapes) { ASSERT_EQUALS(getShapes(planCache).size(), 1U); // Clear cache and confirm number of keys afterwards. - ASSERT_OK(PlanCacheClear::clear(txn.get(), &planCache, nss.ns(), BSONObj())); + ASSERT_OK(PlanCacheClear::clear(opCtx.get(), &planCache, nss.ns(), BSONObj())); ASSERT_EQUALS(getShapes(planCache).size(), 0U); } @@ -202,68 +202,69 @@ TEST(PlanCacheCommandsTest, Canonicalize) { // Invalid parameters PlanCache planCache; QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Missing query field - ASSERT_NOT_OK(PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{}")).getStatus()); + ASSERT_NOT_OK( + PlanCacheCommand::canonicalize(opCtx.get(), nss.ns(), fromjson("{}")).getStatus()); // Query needs to be an object ASSERT_NOT_OK( - PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{query: 1}")).getStatus()); + PlanCacheCommand::canonicalize(opCtx.get(), nss.ns(), fromjson("{query: 1}")).getStatus()); // Sort needs to be an object ASSERT_NOT_OK( - PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{query: {}, sort: 1}")) + PlanCacheCommand::canonicalize(opCtx.get(), nss.ns(), fromjson("{query: {}, sort: 1}")) .getStatus()); // Projection needs to be an object. - ASSERT_NOT_OK( - PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{query: {}, projection: 1}")) - .getStatus()); + ASSERT_NOT_OK(PlanCacheCommand::canonicalize( + opCtx.get(), nss.ns(), fromjson("{query: {}, projection: 1}")) + .getStatus()); // Collation needs to be an object. ASSERT_NOT_OK( - PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{query: {}, collation: 1}")) + PlanCacheCommand::canonicalize(opCtx.get(), nss.ns(), fromjson("{query: {}, collation: 1}")) .getStatus()); // Bad query (invalid sort order) ASSERT_NOT_OK( - PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{query: {}, sort: {a: 0}}")) + PlanCacheCommand::canonicalize(opCtx.get(), nss.ns(), fromjson("{query: {}, sort: {a: 0}}")) .getStatus()); // Valid parameters auto statusWithCQ = - PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}}")); + PlanCacheCommand::canonicalize(opCtx.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}}")); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> query = std::move(statusWithCQ.getValue()); // Equivalent query should generate same key. statusWithCQ = - PlanCacheCommand::canonicalize(txn.get(), nss.ns(), fromjson("{query: {b: 1, a: 1}}")); + PlanCacheCommand::canonicalize(opCtx.get(), nss.ns(), fromjson("{query: {b: 1, a: 1}}")); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> equivQuery = std::move(statusWithCQ.getValue()); ASSERT_EQUALS(planCache.computeKey(*query), planCache.computeKey(*equivQuery)); // Sort query should generate different key from unsorted query. statusWithCQ = PlanCacheCommand::canonicalize( - txn.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}, sort: {a: 1, b: 1}}")); + opCtx.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}, sort: {a: 1, b: 1}}")); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> sortQuery1 = std::move(statusWithCQ.getValue()); ASSERT_NOT_EQUALS(planCache.computeKey(*query), planCache.computeKey(*sortQuery1)); // Confirm sort arguments are properly delimited (SERVER-17158) statusWithCQ = PlanCacheCommand::canonicalize( - txn.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}, sort: {aab: 1}}")); + opCtx.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}, sort: {aab: 1}}")); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> sortQuery2 = std::move(statusWithCQ.getValue()); ASSERT_NOT_EQUALS(planCache.computeKey(*sortQuery1), planCache.computeKey(*sortQuery2)); // Changing order and/or value of predicates should not change key statusWithCQ = PlanCacheCommand::canonicalize( - txn.get(), nss.ns(), fromjson("{query: {b: 3, a: 3}, sort: {a: 1, b: 1}}")); + opCtx.get(), nss.ns(), fromjson("{query: {b: 3, a: 3}, sort: {a: 1, b: 1}}")); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> sortQuery3 = std::move(statusWithCQ.getValue()); ASSERT_EQUALS(planCache.computeKey(*sortQuery1), planCache.computeKey(*sortQuery3)); // Projected query should generate different key from unprojected query. statusWithCQ = PlanCacheCommand::canonicalize( - txn.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}, projection: {_id: 0, a: 1}}")); + opCtx.get(), nss.ns(), fromjson("{query: {a: 1, b: 1}, projection: {_id: 0, a: 1}}")); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> projectionQuery = std::move(statusWithCQ.getValue()); ASSERT_NOT_EQUALS(planCache.computeKey(*query), planCache.computeKey(*projectionQuery)); @@ -275,47 +276,47 @@ TEST(PlanCacheCommandsTest, Canonicalize) { TEST(PlanCacheCommandsTest, planCacheClearInvalidParameter) { PlanCache planCache; - OperationContextNoop txn; + OperationContextNoop opCtx; // Query field type must be BSON object. - ASSERT_NOT_OK(PlanCacheClear::clear(&txn, &planCache, nss.ns(), fromjson("{query: 12345}"))); + ASSERT_NOT_OK(PlanCacheClear::clear(&opCtx, &planCache, nss.ns(), fromjson("{query: 12345}"))); ASSERT_NOT_OK( - PlanCacheClear::clear(&txn, &planCache, nss.ns(), fromjson("{query: /keyisnotregex/}"))); + PlanCacheClear::clear(&opCtx, &planCache, nss.ns(), fromjson("{query: /keyisnotregex/}"))); // Query must pass canonicalization. ASSERT_NOT_OK(PlanCacheClear::clear( - &txn, &planCache, nss.ns(), fromjson("{query: {a: {$no_such_op: 1}}}"))); + &opCtx, &planCache, nss.ns(), fromjson("{query: {a: {$no_such_op: 1}}}"))); // Sort present without query is an error. - ASSERT_NOT_OK(PlanCacheClear::clear(&txn, &planCache, nss.ns(), fromjson("{sort: {a: 1}}"))); + ASSERT_NOT_OK(PlanCacheClear::clear(&opCtx, &planCache, nss.ns(), fromjson("{sort: {a: 1}}"))); // Projection present without query is an error. ASSERT_NOT_OK(PlanCacheClear::clear( - &txn, &planCache, nss.ns(), fromjson("{projection: {_id: 0, a: 1}}"))); + &opCtx, &planCache, nss.ns(), fromjson("{projection: {_id: 0, a: 1}}"))); // Collation present without query is an error. ASSERT_NOT_OK(PlanCacheClear::clear( - &txn, &planCache, nss.ns(), fromjson("{collation: {locale: 'en_US'}}"))); + &opCtx, &planCache, nss.ns(), fromjson("{collation: {locale: 'en_US'}}"))); } TEST(PlanCacheCommandsTest, planCacheClearUnknownKey) { PlanCache planCache; - OperationContextNoop txn; + OperationContextNoop opCtx; - ASSERT_OK(PlanCacheClear::clear(&txn, &planCache, nss.ns(), fromjson("{query: {a: 1}}"))); + ASSERT_OK(PlanCacheClear::clear(&opCtx, &planCache, nss.ns(), fromjson("{query: {a: 1}}"))); } TEST(PlanCacheCommandsTest, planCacheClearOneKey) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Create 2 canonical queries. auto qrA = stdx::make_unique<QueryRequest>(nss); qrA->setFilter(fromjson("{a: 1}")); auto statusWithCQA = CanonicalQuery::canonicalize( - txn.get(), std::move(qrA), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qrA), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQA.getStatus()); auto qrB = stdx::make_unique<QueryRequest>(nss); qrB->setFilter(fromjson("{b: 1}")); unique_ptr<CanonicalQuery> cqA = std::move(statusWithCQA.getValue()); auto statusWithCQB = CanonicalQuery::canonicalize( - txn.get(), std::move(qrB), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qrB), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQB.getStatus()); unique_ptr<CanonicalQuery> cqB = std::move(statusWithCQB.getValue()); @@ -350,7 +351,7 @@ TEST(PlanCacheCommandsTest, planCacheClearOneKey) { BSONObjBuilder bob; ASSERT_OK(PlanCacheClear::clear( - txn.get(), &planCache, nss.ns(), BSON("query" << cqB->getQueryObj()))); + opCtx.get(), &planCache, nss.ns(), BSON("query" << cqB->getQueryObj()))); vector<BSONObj> shapesAfter = getShapes(planCache); ASSERT_EQUALS(shapesAfter.size(), 1U); ASSERT_BSONOBJ_EQ(shapesAfter[0], shapeA); @@ -358,20 +359,20 @@ TEST(PlanCacheCommandsTest, planCacheClearOneKey) { TEST(PlanCacheCommandsTest, planCacheClearOneKeyCollation) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Create 2 canonical queries, one with collation. auto qr = stdx::make_unique<QueryRequest>(nss); qr->setFilter(fromjson("{a: 'foo'}")); auto statusWithCQ = CanonicalQuery::canonicalize( - txn.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> cq = std::move(statusWithCQ.getValue()); auto qrCollation = stdx::make_unique<QueryRequest>(nss); qrCollation->setFilter(fromjson("{a: 'foo'}")); qrCollation->setCollation(fromjson("{locale: 'mock_reverse_string'}")); auto statusWithCQCollation = CanonicalQuery::canonicalize( - txn.get(), std::move(qrCollation), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qrCollation), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQCollation.getStatus()); unique_ptr<CanonicalQuery> cqCollation = std::move(statusWithCQCollation.getValue()); @@ -412,7 +413,7 @@ TEST(PlanCacheCommandsTest, planCacheClearOneKeyCollation) { // Drop query with collation from cache. Make other query is still in cache afterwards. BSONObjBuilder bob; - ASSERT_OK(PlanCacheClear::clear(txn.get(), &planCache, nss.ns(), shapeWithCollation)); + ASSERT_OK(PlanCacheClear::clear(opCtx.get(), &planCache, nss.ns(), shapeWithCollation)); vector<BSONObj> shapesAfter = getShapes(planCache); ASSERT_EQUALS(shapesAfter.size(), 1U); ASSERT_BSONOBJ_EQ(shapesAfter[0], shape); @@ -464,7 +465,7 @@ vector<BSONObj> getPlans(const PlanCache& planCache, const BSONObj& projection, const BSONObj& collation) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); BSONObjBuilder bob; BSONObjBuilder cmdObjBuilder; @@ -475,7 +476,7 @@ vector<BSONObj> getPlans(const PlanCache& planCache, cmdObjBuilder.append("collation", collation); } BSONObj cmdObj = cmdObjBuilder.obj(); - ASSERT_OK(PlanCacheListPlans::list(txn.get(), planCache, nss.ns(), cmdObj, &bob)); + ASSERT_OK(PlanCacheListPlans::list(opCtx.get(), planCache, nss.ns(), cmdObj, &bob)); BSONObj resultObj = bob.obj(); BSONElement plansElt = resultObj.getField("plans"); ASSERT_EQUALS(plansElt.type(), mongo::Array); @@ -489,36 +490,36 @@ vector<BSONObj> getPlans(const PlanCache& planCache, TEST(PlanCacheCommandsTest, planCacheListPlansInvalidParameter) { PlanCache planCache; BSONObjBuilder ignored; - OperationContextNoop txn; + OperationContextNoop opCtx; // Missing query field is not ok. - ASSERT_NOT_OK(PlanCacheListPlans::list(&txn, planCache, nss.ns(), BSONObj(), &ignored)); + ASSERT_NOT_OK(PlanCacheListPlans::list(&opCtx, planCache, nss.ns(), BSONObj(), &ignored)); // Query field type must be BSON object. - ASSERT_NOT_OK( - PlanCacheListPlans::list(&txn, planCache, nss.ns(), fromjson("{query: 12345}"), &ignored)); ASSERT_NOT_OK(PlanCacheListPlans::list( - &txn, planCache, nss.ns(), fromjson("{query: /keyisnotregex/}"), &ignored)); + &opCtx, planCache, nss.ns(), fromjson("{query: 12345}"), &ignored)); + ASSERT_NOT_OK(PlanCacheListPlans::list( + &opCtx, planCache, nss.ns(), fromjson("{query: /keyisnotregex/}"), &ignored)); } TEST(PlanCacheCommandsTest, planCacheListPlansUnknownKey) { // Leave the plan cache empty. PlanCache planCache; - OperationContextNoop txn; + OperationContextNoop opCtx; BSONObjBuilder ignored; - ASSERT_OK( - PlanCacheListPlans::list(&txn, planCache, nss.ns(), fromjson("{query: {a: 1}}"), &ignored)); + ASSERT_OK(PlanCacheListPlans::list( + &opCtx, planCache, nss.ns(), fromjson("{query: {a: 1}}"), &ignored)); } TEST(PlanCacheCommandsTest, planCacheListPlansOnlyOneSolutionTrue) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Create a canonical query auto qr = stdx::make_unique<QueryRequest>(nss); qr->setFilter(fromjson("{a: 1}")); auto statusWithCQ = CanonicalQuery::canonicalize( - txn.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> cq = std::move(statusWithCQ.getValue()); @@ -540,13 +541,13 @@ TEST(PlanCacheCommandsTest, planCacheListPlansOnlyOneSolutionTrue) { TEST(PlanCacheCommandsTest, planCacheListPlansOnlyOneSolutionFalse) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Create a canonical query auto qr = stdx::make_unique<QueryRequest>(nss); qr->setFilter(fromjson("{a: 1}")); auto statusWithCQ = CanonicalQuery::canonicalize( - txn.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> cq = std::move(statusWithCQ.getValue()); @@ -571,20 +572,20 @@ TEST(PlanCacheCommandsTest, planCacheListPlansOnlyOneSolutionFalse) { TEST(PlanCacheCommandsTest, planCacheListPlansCollation) { QueryTestServiceContext serviceContext; - auto txn = serviceContext.makeOperationContext(); + auto opCtx = serviceContext.makeOperationContext(); // Create 2 canonical queries, one with collation. auto qr = stdx::make_unique<QueryRequest>(nss); qr->setFilter(fromjson("{a: 'foo'}")); auto statusWithCQ = CanonicalQuery::canonicalize( - txn.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qr), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQ.getStatus()); unique_ptr<CanonicalQuery> cq = std::move(statusWithCQ.getValue()); auto qrCollation = stdx::make_unique<QueryRequest>(nss); qrCollation->setFilter(fromjson("{a: 'foo'}")); qrCollation->setCollation(fromjson("{locale: 'mock_reverse_string'}")); auto statusWithCQCollation = CanonicalQuery::canonicalize( - txn.get(), std::move(qrCollation), ExtensionsCallbackDisallowExtensions()); + opCtx.get(), std::move(qrCollation), ExtensionsCallbackDisallowExtensions()); ASSERT_OK(statusWithCQCollation.getStatus()); unique_ptr<CanonicalQuery> cqCollation = std::move(statusWithCQCollation.getValue()); |