-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathplan_cache_utils.js
134 lines (119 loc) · 4.39 KB
/
plan_cache_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Utility methods for reading planCache counters
*/
import {getLatestProfilerEntry} from "jstests/libs/profiler.js";
import {
getCachedPlan,
getEngine,
getPlanCacheShapeHashFromObject,
getPlanStage
} from "jstests/libs/query/analyze_plan.js";
export function getPlanCacheSize(db) {
return db.serverStatus().metrics.query.planCache.totalSizeEstimateBytes;
}
export function getPlanCacheNumEntries(db) {
return db.serverStatus().metrics.query.planCache.totalQueryShapes;
}
function getPlansForCacheEntry(coll, match) {
const matchingCacheEntries = coll.getPlanCache().list([{$match: match}]);
assert.eq(matchingCacheEntries.length,
1,
() => tojson(match) + " Contents of cache: " + tojson(coll.getPlanCache().list()));
return matchingCacheEntries[0];
}
function planHasIxScanStageForIndex(planStats, indexName) {
const stage = getPlanStage(planStats, "IXSCAN");
return (stage === null) ? false : indexName === stage.indexName;
}
/**
* Checks the profiler to ensure that the given latest aggregate command ('pipeline') used
* the plan cache as expected. 'queryColl' and 'planCacheColl' are separate arguments to
* support queries on views, where the query runs on 'queryColl' but the plan cache entry is
* associated with 'planCacheColl.' For non-views, 'planCacheColl' can be omitted.
*/
export function assertCacheUsage({
queryColl,
planCacheColl,
pipeline,
fromMultiPlanning,
cacheEntryVersion,
cacheEntryIsActive,
cachedIndexName,
aggOptions = {}
}) {
if (!planCacheColl) {
planCacheColl = queryColl;
}
const profileObj = getLatestProfilerEntry(
queryColl.getDB(), {op: {$in: ["command", "getmore"]}, ns: queryColl.getFullName()});
const planCacheShapeHash = getPlanCacheShapeHashFromObject(profileObj);
const planCacheKey = profileObj.planCacheKey;
assert.eq(fromMultiPlanning, !!profileObj.fromMultiPlanner, profileObj);
const entry = getPlansForCacheEntry(planCacheColl, {planCacheShapeHash: planCacheShapeHash});
assert.eq(cacheEntryVersion, entry.version, entry);
assert.eq(cacheEntryIsActive, entry.isActive, entry);
const explain = queryColl.explain().aggregate(pipeline, aggOptions);
if (entry.version === "2") {
// SBE plan cache always tracks "reads."
assert.eq(entry.worksType, "reads");
} else if (entry.version == "1") {
if (getEngine(explain) == "sbe") {
assert.eq(entry.worksType, "reads");
} else {
assert.eq(entry.worksType, "works");
}
}
// If the entry is active, we should have a plan cache key.
if (entry.isActive) {
assert(entry.planCacheKey);
}
if (planCacheKey) {
assert.eq(entry.planCacheKey, planCacheKey);
const explainKey = explain.hasOwnProperty("queryPlanner")
? explain.queryPlanner.planCacheKey
: explain.stages[0].$cursor.queryPlanner.planCacheKey;
assert.eq(explainKey, entry.planCacheKey, {explain: explain, cacheEntry: entry});
}
if (cachedIndexName) {
if (cacheEntryVersion === 2) {
assert(entry.cachedPlan.stages.includes(cachedIndexName), entry);
} else {
assert(planHasIxScanStageForIndex(getCachedPlan(entry.cachedPlan), cachedIndexName),
entry);
}
}
return entry;
}
export function setUpActiveCacheEntry(
coll, pipeline, cacheEntryVersion, cachedIndexName, assertFn) {
// For the first run, the query should go through multiplanning and create inactive cache entry.
assertFn(coll.aggregate(pipeline));
assertCacheUsage({
queryColl: coll,
pipeline,
fromMultiPlanning: true,
cacheEntryVersion,
cacheEntryIsActive: false,
cachedIndexName
});
// After the second run, the inactive cache entry should be promoted to an active entry.
assertFn(coll.aggregate(pipeline));
assertCacheUsage({
queryColl: coll,
pipeline,
fromMultiPlanning: true,
cacheEntryVersion,
cacheEntryIsActive: true,
cachedIndexName
});
// For the third run, the active cached query should be used.
assertFn(coll.aggregate(pipeline));
assertCacheUsage({
queryColl: coll,
pipeline,
fromMultiPlanning: false,
cacheEntryVersion,
cacheEntryIsActive: true,
cachedIndexName
});
}