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
|
/**
* Confirms that explode for sort plans are properly cached and recovered from the plan cache,
* yielding correct results after the query is auto-parameterized.
*
* @tags: [
* # Since the plan cache is per-node state, this test assumes that all operations are happening
* # against the same mongod.
* assumes_read_preference_unchanged,
* assumes_read_concern_unchanged,
* does_not_support_stepdowns,
* # If all chunks are moved off of a shard, it can cause the plan cache to miss commands.
* assumes_balancer_off,
* assumes_unsharded_collection,
* requires_fcv_62,
* # Plan cache state is node-local and will not get migrated alongside tenant data.
* tenant_migration_incompatible,
* # Part of this test does different checks depending on the engine used. If an implicit index
* # is created, the engine use depends on what index is implicitly created. E.g. if a column
* # index is implicitly created, the engine used may be different in that passthrough.
* assumes_no_implicit_index_creation
* ]
*/
(function() {
"use strict";
load("jstests/libs/analyze_plan.js");
load("jstests/libs/sbe_util.js");
const coll = db.explode_for_sort_plan_cache;
coll.drop();
// Create two indexes to ensure the multi-planner kicks in and the query plan gets cached.
assert.commandWorked(coll.createIndex({a: 1, b: 1, c: 1}));
assert.commandWorked(coll.createIndex({a: 1, b: 1, c: 1, d: 1}));
assert.commandWorked(coll.insert({a: 2, b: 3}));
// A helper function to look up a cache entry in the plan cache based on the given filter
// and sort specs.
function getPlanForCacheEntry(query, sort) {
const keyHash = getPlanCacheKeyFromShape({query: query, sort: sort, collection: coll, db: db});
const res =
coll.aggregate([{$planCacheStats: {}}, {$match: {planCacheKey: keyHash}}]).toArray();
// We expect exactly one matching cache entry.
assert.eq(1, res.length, () => tojson(coll.aggregate([{$planCacheStats: {}}]).toArray()));
return res[0];
}
// A helper function to assert that a cache entry doesn't exist in the plan cache based on the
// given filter and sort specs.
function assertCacheEntryDoesNotExist(query, sort) {
const keyHash = getPlanCacheKeyFromShape({query: query, sort: sort, collection: coll, db: db});
const res =
coll.aggregate([{$planCacheStats: {}}, {$match: {planCacheKey: keyHash}}]).toArray();
assert.eq(0, res.length, () => tojson(coll.aggregate([{$planCacheStats: {}}]).toArray()));
}
let querySpec = {a: {$eq: 2}, b: {$in: [99, 4]}};
const sortSpec = {
c: 1
};
// TODO SERVER-67576: remove this branch once explode for sort plans are supported by the SBE plan
// cache.
if (checkSBEEnabled(db, ["featureFlagSbeFull"])) {
// Run the query for the first time and make sure the plan hasn't been cached.
assert.eq(0, coll.find(querySpec).sort(sortSpec).itcount());
assertCacheEntryDoesNotExist(querySpec, sortSpec);
// Run the query again and make sure it's still not cached.
assert.eq(0, coll.find(querySpec).sort(sortSpec).itcount());
assertCacheEntryDoesNotExist(querySpec, sortSpec);
// Run a query that returns one document in the collection, but the plan is still not cached.
querySpec = {a: {$eq: 2}, b: {$in: [3, 4]}};
assert.eq(1, coll.find(querySpec).sort(sortSpec).itcount());
assertCacheEntryDoesNotExist(querySpec, sortSpec);
} else {
// Run the query for the first time to create an inactive plan cache entry.
assert.eq(0, coll.find(querySpec).sort(sortSpec).itcount());
const inactiveEntry = getPlanForCacheEntry(querySpec, sortSpec);
assert.eq(inactiveEntry.isActive, false, inactiveEntry);
// Run the query again to activate the cache entry.
assert.eq(0, coll.find(querySpec).sort(sortSpec).itcount());
const activeEntry = getPlanForCacheEntry(querySpec, sortSpec);
assert.eq(activeEntry.isActive, true, activeEntry);
assert.eq(inactiveEntry.queryHash,
activeEntry.queryHash,
`inactive=${tojson(inactiveEntry)}, active=${tojson(activeEntry)}`);
assert.eq(inactiveEntry.planCacheKey,
activeEntry.planCacheKey,
`inactive=${tojson(inactiveEntry)}, active=${tojson(activeEntry)}`);
// Run a query that reuses the cache entry and should return one document in the collection.
querySpec = {a: {$eq: 2}, b: {$in: [3, 4]}};
assert.eq(1, coll.find(querySpec).sort(sortSpec).itcount());
const reusedEntry = getPlanForCacheEntry(querySpec, sortSpec);
assert.eq(reusedEntry.isActive, true, reusedEntry);
assert.eq(activeEntry.queryHash,
reusedEntry.queryHash,
`active=${tojson(activeEntry)}, reused=${tojson(reusedEntry)}`);
assert.eq(activeEntry.planCacheKey,
reusedEntry.planCacheKey,
`active=${tojson(activeEntry)}, reused=${tojson(reusedEntry)}`);
}
}());
|