summaryrefslogtreecommitdiff
path: root/src/mongo/db/query
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/query')
-rw-r--r--src/mongo/db/query/SConscript1
-rw-r--r--src/mongo/db/query/index_entry.h20
-rw-r--r--src/mongo/db/query/plan_cache.cpp136
-rw-r--r--src/mongo/db/query/plan_cache.h86
-rw-r--r--src/mongo/db/query/plan_cache_test.cpp202
-rw-r--r--src/mongo/db/query/plan_ranker.h13
6 files changed, 394 insertions, 64 deletions
diff --git a/src/mongo/db/query/SConscript b/src/mongo/db/query/SConscript
index 486dda916b9..f4a37ab200d 100644
--- a/src/mongo/db/query/SConscript
+++ b/src/mongo/db/query/SConscript
@@ -35,6 +35,7 @@ env.Library(
LIBDEPS=[
"$BUILD_DIR/mongo/base",
"$BUILD_DIR/mongo/db/bson/dotted_path_support",
+ '$BUILD_DIR/mongo/db/commands/server_status_core',
"$BUILD_DIR/mongo/db/index/expression_params",
"$BUILD_DIR/mongo/db/index_names",
"$BUILD_DIR/mongo/db/matcher/expressions",
diff --git a/src/mongo/db/query/index_entry.h b/src/mongo/db/query/index_entry.h
index 5f6786640b9..18978d53f70 100644
--- a/src/mongo/db/query/index_entry.h
+++ b/src/mongo/db/query/index_entry.h
@@ -35,6 +35,7 @@
#include "mongo/db/index/multikey_paths.h"
#include "mongo/db/index_names.h"
#include "mongo/db/jsobj.h"
+#include "mongo/util/container_size_helper.h"
#include "mongo/util/mongoutils/str.h"
namespace mongo {
@@ -112,6 +113,25 @@ struct IndexEntry {
std::string toString() const;
+ uint64_t estimateObjectSizeInBytes() const {
+
+ return // For each element in 'multikeyPaths' add the 'length of the vector * size of the
+ // vector element'.
+ container_size_helper::estimateObjectSizeInBytes(
+ multikeyPaths,
+ [](const auto& keyPath) {
+ // Calculate the size of each std::set in 'multiKeyPaths'.
+ return container_size_helper::estimateObjectSizeInBytes(keyPath);
+ },
+ true) +
+ // Add the runtime BSONObj size of 'keyPattern' and capacity of 'name'.
+ keyPattern.objsize() + name.capacity() +
+ // The BSON size of the 'infoObj' is purposefully excluded since its ownership is shared
+ // with the index catalog.
+ // Add size of the object.
+ sizeof(*this);
+ }
+
BSONObj keyPattern;
bool multikey;
diff --git a/src/mongo/db/query/plan_cache.cpp b/src/mongo/db/query/plan_cache.cpp
index 29f8d603976..9be8931f52d 100644
--- a/src/mongo/db/query/plan_cache.cpp
+++ b/src/mongo/db/query/plan_cache.cpp
@@ -43,6 +43,7 @@
#include "mongo/base/owned_pointer_vector.h"
#include "mongo/client/dbclientinterface.h" // For QueryOption_foobar
+#include "mongo/db/commands/server_status_metric.h"
#include "mongo/db/matcher/expression_array.h"
#include "mongo/db/matcher/expression_geo.h"
#include "mongo/db/query/collation/collator_interface.h"
@@ -56,8 +57,13 @@
#include "mongo/util/transitional_tools_do_not_use/vector_spooling.h"
namespace mongo {
+
+Counter64 PlanCacheEntry::planCacheTotalSizeEstimateBytes;
namespace {
+ServerStatusMetricField<Counter64> totalPlanCacheSizeEstimateBytesMetric(
+ "query.planCacheTotalSizeEstimateBytes", &PlanCacheEntry::planCacheTotalSizeEstimateBytes);
+
// Delimiters for cache key encoding.
const char kEncodeChildrenBegin = '[';
const char kEncodeChildrenEnd = ']';
@@ -436,47 +442,89 @@ CachedSolution::~CachedSolution() {
// PlanCacheEntry
//
-PlanCacheEntry::PlanCacheEntry(const std::vector<QuerySolution*>& solutions,
- PlanRankingDecision* why)
- : plannerData(solutions.size()), decision(why) {
- invariant(why);
+std::unique_ptr<PlanCacheEntry> PlanCacheEntry::create(
+ const std::vector<QuerySolution*>& solutions,
+ std::unique_ptr<const PlanRankingDecision> decision,
+ const CanonicalQuery& query,
+ Date_t timeOfCreation) {
+ invariant(decision);
// The caller of this constructor is responsible for ensuring
// that the QuerySolution 's' has valid cacheData. If there's no
// data to cache you shouldn't be trying to construct a PlanCacheEntry.
// Copy the solution's cache data into the plan cache entry.
+ std::vector<std::unique_ptr<const SolutionCacheData>> solutionCacheData(solutions.size());
for (size_t i = 0; i < solutions.size(); ++i) {
invariant(solutions[i]->cacheData.get());
- plannerData[i] = solutions[i]->cacheData->clone();
+ solutionCacheData[i] =
+ std::unique_ptr<const SolutionCacheData>(solutions[i]->cacheData->clone());
}
+
+ const QueryRequest& qr = query.getQueryRequest();
+ BSONObjBuilder projBuilder;
+ for (auto elem : qr.getProj()) {
+ if (elem.fieldName()[0] == '$') {
+ continue;
+ }
+ projBuilder.append(elem);
+ }
+
+ return std::unique_ptr<PlanCacheEntry>(new PlanCacheEntry(
+ std::move(solutionCacheData),
+ qr.getFilter(),
+ qr.getSort(),
+ projBuilder.obj(),
+ query.getCollator() ? query.getCollator()->getSpec().toBSON() : BSONObj(),
+ timeOfCreation,
+ std::move(decision),
+ {}));
+}
+
+PlanCacheEntry::PlanCacheEntry(std::vector<std::unique_ptr<const SolutionCacheData>> plannerData,
+ const BSONObj& query,
+ const BSONObj& sort,
+ const BSONObj& projection,
+ const BSONObj& collation,
+ const Date_t timeOfCreation,
+ std::unique_ptr<const PlanRankingDecision> decision,
+ std::vector<PlanCacheEntryFeedback*> feedback)
+ : plannerData(std::move(plannerData)),
+ query(query),
+ sort(sort),
+ projection(projection),
+ collation(collation),
+ timeOfCreation(timeOfCreation),
+ decision(std::move(decision)),
+ feedback(std::move(feedback)),
+ _entireObjectSize(_estimateObjectSizeInBytes()) {
+ // Account for the object in the global metric for estimating the server's total plan cache
+ // memory consumption.
+ planCacheTotalSizeEstimateBytes.increment(_entireObjectSize);
}
PlanCacheEntry::~PlanCacheEntry() {
for (size_t i = 0; i < feedback.size(); ++i) {
delete feedback[i];
}
- for (size_t i = 0; i < plannerData.size(); ++i) {
- delete plannerData[i];
- }
+ planCacheTotalSizeEstimateBytes.decrement(_entireObjectSize);
}
PlanCacheEntry* PlanCacheEntry::clone() const {
- std::vector<std::unique_ptr<QuerySolution>> solutions;
+ std::vector<std::unique_ptr<const SolutionCacheData>> solutionCacheData(plannerData.size());
for (size_t i = 0; i < plannerData.size(); ++i) {
- auto qs = stdx::make_unique<QuerySolution>();
- qs->cacheData.reset(plannerData[i]->clone());
- solutions.push_back(std::move(qs));
- }
- PlanCacheEntry* entry = new PlanCacheEntry(
- transitional_tools_do_not_use::unspool_vector(solutions), decision->clone());
-
- // Copy query shape.
- entry->query = query.getOwned();
- entry->sort = sort.getOwned();
- entry->projection = projection.getOwned();
- entry->collation = collation.getOwned();
- entry->timeOfCreation = timeOfCreation;
+ invariant(plannerData[i]);
+ solutionCacheData[i] = std::unique_ptr<const SolutionCacheData>(plannerData[i]->clone());
+ }
+ auto decisionPtr = std::unique_ptr<PlanRankingDecision>(decision->clone());
+ PlanCacheEntry* entry = new PlanCacheEntry(std::move(solutionCacheData),
+ query,
+ sort,
+ projection,
+ collation,
+ timeOfCreation,
+ std::move(decisionPtr),
+ {});
// Copy performance stats.
for (size_t i = 0; i < feedback.size(); ++i) {
@@ -488,6 +536,25 @@ PlanCacheEntry* PlanCacheEntry::clone() const {
return entry;
}
+uint64_t PlanCacheEntry::_estimateObjectSizeInBytes() const {
+ return // Add the size of each entry in 'plannerData' vector.
+ container_size_helper::estimateObjectSizeInBytes(
+ plannerData,
+ [](const auto& cacheData) { return cacheData->estimateObjectSizeInBytes(); },
+ true) +
+ // Add the size of each entry in 'feedback' vector.
+ container_size_helper::estimateObjectSizeInBytes(
+ feedback,
+ [](const auto& feedbackEntry) { return feedbackEntry->estimateObjectSizeInBytes(); },
+ true) +
+ // Add the entire size of 'decision' object.
+ (decision ? decision->estimateObjectSizeInBytes() : 0) +
+ // Add the size of all the owned BSON objects.
+ query.objsize() + sort.objsize() + projection.objsize() + collation.objsize() +
+ // Add size of the object.
+ sizeof(*this);
+}
+
std::string PlanCacheEntry::toString() const {
return str::stream() << "(query: " << query.toString() << ";sort: " << sort.toString()
<< ";projection: " << projection.toString()
@@ -760,7 +827,7 @@ void PlanCache::encodeKeyForProj(const BSONObj& projObj, StringBuilder* keyBuild
Status PlanCache::add(const CanonicalQuery& query,
const std::vector<QuerySolution*>& solns,
- PlanRankingDecision* why,
+ std::unique_ptr<PlanRankingDecision> why,
Date_t now) {
invariant(why);
@@ -781,29 +848,10 @@ Status PlanCache::add(const CanonicalQuery& query,
"candidate ordering entries in decision must match solutions");
}
- PlanCacheEntry* entry = new PlanCacheEntry(solns, why);
- const QueryRequest& qr = query.getQueryRequest();
- entry->query = qr.getFilter().getOwned();
- entry->sort = qr.getSort().getOwned();
- if (query.getCollator()) {
- entry->collation = query.getCollator()->getSpec().toBSON();
- }
- entry->timeOfCreation = now;
-
-
- // Strip projections on $-prefixed fields, as these are added by internal callers of the query
- // system and are not considered part of the user projection.
- BSONObjBuilder projBuilder;
- for (auto elem : qr.getProj()) {
- if (elem.fieldName()[0] == '$') {
- continue;
- }
- projBuilder.append(elem);
- }
- entry->projection = projBuilder.obj();
+ auto entry(PlanCacheEntry::create(solns, std::move(why), query, now));
stdx::lock_guard<stdx::mutex> cacheLock(_cacheMutex);
- std::unique_ptr<PlanCacheEntry> evictedEntry = _cache.add(computeKey(query), entry);
+ std::unique_ptr<PlanCacheEntry> evictedEntry = _cache.add(computeKey(query), entry.release());
if (NULL != evictedEntry.get()) {
LOG(1) << _ns << ": plan cache maximum size exceeded - "
diff --git a/src/mongo/db/query/plan_cache.h b/src/mongo/db/query/plan_cache.h
index 7c38e94ccbb..e1e529d198f 100644
--- a/src/mongo/db/query/plan_cache.h
+++ b/src/mongo/db/query/plan_cache.h
@@ -33,6 +33,7 @@
#include <boost/optional/optional.hpp>
#include <set>
+#include "mongo/base/counter.h"
#include "mongo/db/exec/plan_stats.h"
#include "mongo/db/query/canonical_query.h"
#include "mongo/db/query/index_tag.h"
@@ -41,6 +42,7 @@
#include "mongo/db/query/query_planner_params.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/stdx/mutex.h"
+#include "mongo/util/container_size_helper.h"
namespace mongo {
@@ -62,6 +64,10 @@ struct PlanCacheEntryFeedback {
// The "goodness" score produced by the plan ranker
// corresponding to 'stats'.
double score;
+
+ uint64_t estimateObjectSizeInBytes() const {
+ return stats->estimateObjectSizeInBytes() + sizeof(*this);
+ }
};
// TODO: Replace with opaque type.
@@ -96,6 +102,14 @@ struct PlanCacheIndexTree {
*/
struct OrPushdown {
std::string indexName;
+ uint64_t estimateObjectSizeInBytes() const {
+ return // Add size of each element in 'route' vector.
+ container_size_helper::estimateObjectSizeInBytes(route) +
+ // Add size of each element in 'route' vector.
+ indexName.size() +
+ // Add size of the object.
+ sizeof(*this);
+ }
size_t position;
bool canCombineBounds;
std::deque<size_t> route;
@@ -126,6 +140,22 @@ struct PlanCacheIndexTree {
*/
std::string toString(int indents = 0) const;
+ uint64_t estimateObjectSizeInBytes() const {
+ return // Recursively add size of each element in 'children' vector.
+ container_size_helper::estimateObjectSizeInBytes(
+ children,
+ [](const auto& child) { return child->estimateObjectSizeInBytes(); },
+ true) +
+ // Add size of each element in 'orPushdowns' vector.
+ container_size_helper::estimateObjectSizeInBytes(
+ orPushdowns,
+ [](const auto& orPushdown) { return orPushdown.estimateObjectSizeInBytes(); },
+ false) +
+ // Add size of 'entry' if present.
+ (entry ? entry->estimateObjectSizeInBytes() : 0) +
+ // Add size of the object.
+ sizeof(*this);
+ }
// Children owned here.
std::vector<PlanCacheIndexTree*> children;
@@ -161,6 +191,10 @@ struct SolutionCacheData {
// For debugging.
std::string toString() const;
+ uint64_t estimateObjectSizeInBytes() const {
+ return (tree ? tree->estimateObjectSizeInBytes() : 0) + sizeof(*this);
+ }
+
// Owned here. If 'wholeIXSoln' is false, then 'tree'
// can be used to tag an isomorphic match expression. If 'wholeIXSoln'
// is true, then 'tree' is used to store the relevant IndexEntry.
@@ -237,9 +271,12 @@ public:
/**
* Create a new PlanCacheEntry.
* Grabs any planner-specific data required from the solutions.
- * Takes ownership of the PlanRankingDecision that placed the plan in the cache.
*/
- PlanCacheEntry(const std::vector<QuerySolution*>& solutions, PlanRankingDecision* why);
+ static std::unique_ptr<PlanCacheEntry> create(
+ const std::vector<QuerySolution*>& solutions,
+ std::unique_ptr<const PlanRankingDecision> decision,
+ const CanonicalQuery& query,
+ Date_t timeOfCreation);
~PlanCacheEntry();
@@ -258,30 +295,53 @@ public:
// Data provided to the planner to allow it to recreate the solutions this entry
// represents. Each SolutionCacheData is fully owned here, so in order to return
// it from the cache a deep copy is made and returned inside CachedSolution.
- std::vector<SolutionCacheData*> plannerData;
+ const std::vector<std::unique_ptr<const SolutionCacheData>> plannerData;
// TODO: Do we really want to just hold a copy of the CanonicalQuery? For now we just
// extract the data we need.
//
// Used by the plan cache commands to display an example query
// of the appropriate shape.
- BSONObj query;
- BSONObj sort;
- BSONObj projection;
- BSONObj collation;
- Date_t timeOfCreation;
+ const BSONObj query;
+ const BSONObj sort;
+ const BSONObj projection;
+ const BSONObj collation;
+ const Date_t timeOfCreation;
//
// Performance stats
//
- // Information that went into picking the winning plan and also why
- // the other plans lost.
- std::unique_ptr<PlanRankingDecision> decision;
+ // Information that went into picking the winning plan and also why the other plans lost.
+ const std::unique_ptr<const PlanRankingDecision> decision;
// Annotations from cached runs. The CachedPlanStage provides these stats about its
// runs when they complete.
std::vector<PlanCacheEntryFeedback*> feedback;
+
+ /**
+ * Tracks the approximate cumulative size of the plan cache entries across all the collections.
+ */
+ static Counter64 planCacheTotalSizeEstimateBytes;
+
+private:
+ /**
+ * All arguments constructor.
+ */
+ PlanCacheEntry(std::vector<std::unique_ptr<const SolutionCacheData>> plannerData,
+ const BSONObj& query,
+ const BSONObj& sort,
+ const BSONObj& projection,
+ const BSONObj& collation,
+ Date_t timeOfCreation,
+ std::unique_ptr<const PlanRankingDecision> decision,
+ std::vector<PlanCacheEntryFeedback*> feedback);
+
+ uint64_t _estimateObjectSizeInBytes() const;
+
+ // The total runtime size of the current object in bytes. This is the deep size, obtained by
+ // recursively following references to all owned objects.
+ const uint64_t _entireObjectSize;
};
/**
@@ -326,7 +386,7 @@ public:
*/
Status add(const CanonicalQuery& query,
const std::vector<QuerySolution*>& solns,
- PlanRankingDecision* why,
+ std::unique_ptr<PlanRankingDecision> why,
Date_t now);
/**
@@ -380,7 +440,7 @@ public:
/**
* Returns a copy of a cache entry.
* Used by planCacheListPlans to display plan details.
- *
+ *
* If there is no entry in the cache for the 'query', returns an error Status.
*
* If there is an entry in the cache, populates 'entryOut' and returns Status::OK(). Caller
diff --git a/src/mongo/db/query/plan_cache_test.cpp b/src/mongo/db/query/plan_cache_test.cpp
index c79341deaba..ab8cc1421fd 100644
--- a/src/mongo/db/query/plan_cache_test.cpp
+++ b/src/mongo/db/query/plan_cache_test.cpp
@@ -86,8 +86,8 @@ unique_ptr<CanonicalQuery> canonicalize(const BSONObj& queryObj) {
return std::move(statusWithCQ.getValue());
}
-unique_ptr<CanonicalQuery> canonicalize(const char* queryStr) {
- BSONObj queryObj = fromjson(queryStr);
+unique_ptr<CanonicalQuery> canonicalize(StringData queryStr) {
+ BSONObj queryObj = fromjson(queryStr.toString());
return canonicalize(queryObj);
}
@@ -247,7 +247,7 @@ struct GenerateQuerySolution {
/**
* Utility function to create a PlanRankingDecision
*/
-PlanRankingDecision* createDecision(size_t numPlans) {
+std::unique_ptr<PlanRankingDecision> createDecision(size_t numPlans) {
unique_ptr<PlanRankingDecision> why(new PlanRankingDecision());
for (size_t i = 0; i < numPlans; ++i) {
CommonStats common("COLLSCAN");
@@ -257,7 +257,7 @@ PlanRankingDecision* createDecision(size_t numPlans) {
why->scores.push_back(0U);
why->candidateOrder.push_back(i);
}
- return why.release();
+ return why;
}
/**
@@ -294,6 +294,13 @@ void assertShouldNotCacheQuery(const char* queryStr) {
assertShouldNotCacheQuery(*cq);
}
+std::unique_ptr<QuerySolution> getQuerySolutionForCaching() {
+ std::unique_ptr<QuerySolution> qs = std::make_unique<QuerySolution>();
+ qs->cacheData = std::make_unique<SolutionCacheData>();
+ qs->cacheData->tree = std::make_unique<PlanCacheIndexTree>();
+ return qs;
+}
+
/**
* Cacheable queries
* These queries will be added to the cache with run-time statistics
@@ -435,7 +442,7 @@ TEST(PlanCacheTest, AddEmptySolutions) {
std::vector<QuerySolution*> solns;
unique_ptr<PlanRankingDecision> decision(createDecision(1U));
QueryTestServiceContext serviceContext;
- ASSERT_NOT_OK(planCache.add(*cq, solns, decision.get(), Date_t{}));
+ ASSERT_NOT_OK(planCache.add(*cq, solns, std::move(decision), Date_t{}));
}
TEST(PlanCacheTest, AddValidSolution) {
@@ -695,8 +702,9 @@ protected:
qs.cacheData.reset(soln.cacheData->clone());
std::vector<QuerySolution*> solutions;
solutions.push_back(&qs);
- PlanCacheEntry entry(solutions, createDecision(1U));
- CachedSolution cachedSoln(ck, entry);
+
+ auto entry = PlanCacheEntry::create(solutions, createDecision(1U), *scopedCq, Date_t());
+ CachedSolution cachedSoln(ck, *entry);
auto statusWithQs = QueryPlanner::planFromCache(*scopedCq, params, cachedSoln);
ASSERT_OK(statusWithQs.getStatus());
@@ -1759,4 +1767,184 @@ TEST(PlanCacheTest, ComputeNeArrayInOrStagePreserversOrder) {
}));
}
+TEST(PlanCacheTest, PlanCacheSizeWithCRUDOperations) {
+ PlanCache planCache;
+ unique_ptr<CanonicalQuery> cq(canonicalize("{a: 1, b: 1}"));
+ auto qs = getQuerySolutionForCaching();
+ std::vector<QuerySolution*> solns = {qs.get()};
+ long long previousSize, originalSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+
+ // Verify that the plan cache size increases after adding new entry to cache.
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(1U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that trying to set the same entry won't change the plan cache size.
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(1U), Date_t{}));
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that the plan cache size increases after updating the same entry with more solutions.
+ solns.push_back(qs.get());
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(2U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that the plan cache size decreases after updating the same entry with fewer solutions.
+ solns.erase(solns.end() - 1);
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(1U), Date_t{}));
+ ASSERT_LT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), originalSize);
+
+ // Verify that adding multiple entries will increasing the cache size.
+ long long sizeWithOneEntry = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ std::string queryString = "{a: 1, c: 1}";
+ for (int i = 0; i < 5; ++i) {
+ // Update the field name in the query string so that plan cache creates a new entry.
+ queryString[1] = 'b' + i;
+ unique_ptr<CanonicalQuery> query(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.add(*query, solns, createDecision(1U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+ }
+
+ // Verify that removing multiple entries will decreasing the cache size.
+ for (int i = 0; i < 5; ++i) {
+ // Update the field name in the query to match the previously created plan cache entry key.
+ queryString[1] = 'b' + i;
+ unique_ptr<CanonicalQuery> query(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.remove(*query));
+ ASSERT_LT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+ }
+ // Verify that size is reset to the size when there is only entry.
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), sizeWithOneEntry);
+
+ // Verify that trying to remove a non-existing key won't change the plan cache size.
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ unique_ptr<CanonicalQuery> newQuery(canonicalize("{a: 1}"));
+ ASSERT_NOT_OK(planCache.remove(*newQuery));
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that the plan cache size goes back to original size when the entry is removed.
+ ASSERT_OK(planCache.remove(*cq));
+ ASSERT_EQ(planCache.size(), 0U);
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), originalSize);
+}
+
+TEST(PlanCacheTest, PlanCacheSizeWithEviction) {
+ const size_t kCacheSize = 5;
+ internalQueryCacheSize.store(kCacheSize);
+ PlanCache planCache;
+ unique_ptr<CanonicalQuery> cq(canonicalize("{a: 1, b: 1}"));
+ auto qs = getQuerySolutionForCaching();
+ std::vector<QuerySolution*> solns = {qs.get(), qs.get()};
+ long long originalSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ long long previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+
+ // Add entries until plan cache is full and verify that the size keeps increasing.
+ std::string queryString = "{a: 1, c: 1}";
+ for (size_t i = 0; i < kCacheSize; ++i) {
+ // Update the field name in the query string so that plan cache creates a new entry.
+ queryString[1]++;
+ unique_ptr<CanonicalQuery> query(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.add(*query, solns, createDecision(2U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+ }
+
+ // Verify that adding entry of same size as evicted entry wouldn't change the plan cache size.
+ queryString = "{k: 1, c: 1}";
+ cq = unique_ptr<CanonicalQuery>(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_EQ(planCache.size(), kCacheSize);
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(2U), Date_t{}));
+ ASSERT_EQ(planCache.size(), kCacheSize);
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that adding entry with query bigger than the evicted entry's key should change the
+ // plan cache size.
+ queryString = "{k: 1, c: 1, extraField: 1}";
+ unique_ptr<CanonicalQuery> queryBiggerKey(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.add(*queryBiggerKey, solns, createDecision(2U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that adding entry with query solutions larger than the evicted entry's query solutions
+ // should increase the plan cache size.
+ queryString = "{l: 1, c: 1}";
+ cq = unique_ptr<CanonicalQuery>(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ solns.push_back(qs.get());
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(3U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that adding entry with query solutions smaller than the evicted entry's query
+ // solutions should decrease the plan cache size.
+ queryString = "{m: 1, c: 1}";
+ cq = unique_ptr<CanonicalQuery>(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ solns = {qs.get()};
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(1U), Date_t{}));
+ ASSERT_LT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // clear() should reset the size.
+ planCache.clear();
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), originalSize);
+}
+
+TEST(PlanCacheTest, PlanCacheSizeWithMultiplePlanCaches) {
+ PlanCache planCache1;
+ PlanCache planCache2;
+ unique_ptr<CanonicalQuery> cq(canonicalize("{a: 1, b: 1}"));
+ auto qs = getQuerySolutionForCaching();
+ std::vector<QuerySolution*> solns = {qs.get()};
+ long long previousSize, originalSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+
+ // Verify that adding entries to both plan caches will keep increasing the cache size.
+ std::string queryString = "{a: 1, c: 1}";
+ for (int i = 0; i < 5; ++i) {
+ // Update the field name in the query string so that plan cache creates a new entry.
+ queryString[1] = 'b' + i;
+ unique_ptr<CanonicalQuery> query(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache1.add(*query, solns, createDecision(1U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache2.add(*query, solns, createDecision(1U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+ }
+
+ // Verify that removing entries from one plan caches will keep decreasing the cache size.
+ for (int i = 0; i < 5; ++i) {
+ // Update the field name in the query to match the previously created plan cache entry key.
+ queryString[1] = 'b' + i;
+ unique_ptr<CanonicalQuery> query(canonicalize(queryString));
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache1.remove(*query));
+ ASSERT_LT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+ }
+
+ // Verify for scoped PlanCache object.
+ long long sizeBeforeScopedPlanCache = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ {
+ PlanCache planCache;
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ ASSERT_OK(planCache.add(*cq, solns, createDecision(1U), Date_t{}));
+ ASSERT_GT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+ }
+
+ // Verify that size is reset to 'sizeBeforeScopedPlanCache' after the destructor of 'planCache'
+ // is called.
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), sizeBeforeScopedPlanCache);
+
+ // Clear 'planCache2' to remove all entries.
+ previousSize = PlanCacheEntry::planCacheTotalSizeEstimateBytes.get();
+ planCache2.clear();
+ ASSERT_LT(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), previousSize);
+
+ // Verify that size is reset to the original size after removing all entries.
+ ASSERT_EQ(PlanCacheEntry::planCacheTotalSizeEstimateBytes.get(), originalSize);
+}
} // namespace
diff --git a/src/mongo/db/query/plan_ranker.h b/src/mongo/db/query/plan_ranker.h
index 698f92d0cb9..217c21f8744 100644
--- a/src/mongo/db/query/plan_ranker.h
+++ b/src/mongo/db/query/plan_ranker.h
@@ -39,6 +39,7 @@
#include "mongo/db/exec/plan_stats.h"
#include "mongo/db/exec/working_set.h"
#include "mongo/db/query/query_solution.h"
+#include "mongo/util/container_size_helper.h"
namespace mongo {
@@ -106,6 +107,18 @@ struct PlanRankingDecision {
return decision;
}
+ uint64_t estimateObjectSizeInBytes() const {
+ return // Add size of each element in 'stats' vector.
+ container_size_helper::estimateObjectSizeInBytes(
+ stats, [](const auto& stat) { return stat->estimateObjectSizeInBytes(); }, true) +
+ // Add size of each element in 'candidateOrder' vector.
+ container_size_helper::estimateObjectSizeInBytes(candidateOrder) +
+ // Add size of each element in 'scores' vector.
+ container_size_helper::estimateObjectSizeInBytes(scores) +
+ // Add size of the object.
+ sizeof(*this);
+ }
+
// Stats of all plans sorted in descending order by score.
// Owned by us.
std::vector<std::unique_ptr<PlanStageStats>> stats;