summaryrefslogtreecommitdiff
path: root/jstests/core/query/plan_cache/plan_cache_clear.js
blob: 7aee144bc89d96c6c1f1c9b7b00d67303d774da4 (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Test clearing of the plan cache, either manually through the planCacheClear command,
// or due to system events such as an index build.
//
// The test runs commands that are not allowed with security token: planCacheClear, reIndex.
// @tags: [
//   not_allowed_with_security_token,
//   # This test attempts to perform queries and introspect/manipulate the server's plan cache
//   # entries. The former operation may be routed to a secondary in the replica set, whereas the
//   # latter must be routed to the primary.
//   assumes_read_concern_unchanged,
//   assumes_read_preference_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,
//   # Plan cache state is node-local and will not get migrated alongside tenant data.
//   tenant_migration_incompatible,
//   # TODO SERVER-67607: Test plan cache with CQF enabled.
//   cqf_incompatible,
// ]

(function() {
'use strict';

load("jstests/libs/sbe_util.js");  // For checkSBEEnabled.

const coll = db.jstests_plan_cache_clear;
coll.drop();

function numPlanCacheEntries(coll) {
    return coll.aggregate([{$planCacheStats: {}}]).itcount();
}

function dumpPlanCacheState(coll) {
    return coll.aggregate([{$planCacheStats: {}}]).toArray();
}

assert.commandWorked(coll.insert({a: 1, b: 1}));
assert.commandWorked(coll.insert({a: 1, b: 2}));
assert.commandWorked(coll.insert({a: 1, b: 2}));
assert.commandWorked(coll.insert({a: 2, b: 2}));

// We need two indices so that the MultiPlanRunner is executed.
assert.commandWorked(coll.createIndex({a: 1}));
assert.commandWorked(coll.createIndex({a: 1, b: 1}));

// Run a query so that an entry is inserted into the cache.
assert.eq(1, coll.find({a: 1, b: 1}).itcount());

// Invalid key should be a no-op.
assert.commandWorked(coll.runCommand('planCacheClear', {query: {unknownfield: 1}}));
assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// Introduce a second plan cache entry.
assert.eq(0, coll.find({a: 1, b: 1, c: 1}).itcount());
assert.eq(2, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// Drop one of the two shapes from the cache.
assert.commandWorked(coll.runCommand('planCacheClear', {query: {a: 1, b: 1}}),
                     dumpPlanCacheState(coll));
assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// Drop the second shape from the cache.
assert.commandWorked(coll.runCommand('planCacheClear', {query: {a: 1, b: 1, c: 1}}),
                     dumpPlanCacheState(coll));
assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// planCacheClear can clear $expr queries.
assert.eq(1, coll.find({a: 1, b: 1, $expr: {$eq: ['$a', 1]}}).itcount());
assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
assert.commandWorked(
    coll.runCommand('planCacheClear', {query: {a: 1, b: 1, $expr: {$eq: ['$a', 1]}}}));
assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// planCacheClear fails with an $expr query with an unbound variable.
assert.commandFailed(
    coll.runCommand('planCacheClear', {query: {a: 1, b: 1, $expr: {$eq: ['$a', '$$unbound']}}}));

// Insert two more shapes into the cache.
assert.eq(1, coll.find({a: 1, b: 1}).itcount());
assert.eq(1, coll.find({a: 1, b: 1}, {_id: 0, a: 1}).itcount());
assert.eq(2, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// Error cases.
assert.commandFailedWithCode(coll.runCommand('planCacheClear', {query: 12345}),
                             ErrorCodes.BadValue);
assert.commandFailedWithCode(coll.runCommand('planCacheClear', {query: /regex/}),
                             ErrorCodes.BadValue);
assert.commandFailedWithCode(coll.runCommand('planCacheClear', {query: {a: {$no_such_op: 1}}}),
                             ErrorCodes.BadValue);
// 'sort' parameter is not allowed without 'query' parameter.
assert.commandFailedWithCode(coll.runCommand('planCacheClear', {sort: {a: 1}}),
                             ErrorCodes.BadValue);
// 'projection' parameter is not allowed with 'query' parameter.
assert.commandFailedWithCode(coll.runCommand('planCacheClear', {projection: {_id: 0, a: 1}}),
                             ErrorCodes.BadValue);

// Drop query cache. This clears all cached queries in the collection.
assert.commandWorked(coll.runCommand('planCacheClear'));
assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// Clearing the plan cache for a non-existent collection should succeed.
const nonExistentColl = db.plan_cache_clear_nonexistent;
nonExistentColl.drop();
assert.commandWorked(nonExistentColl.runCommand('planCacheClear'));

if (checkSBEEnabled(db, ["featureFlagSbeFull"], true /* checkAllNodes */)) {
    // Plan cache commands should work against the main collection only, not foreignColl
    // collections, when $lookup is pushed down into SBE.
    const foreignColl = db.plan_cache_clear_foreign;
    foreignColl.drop();

    // We need two indices so that the multi-planner is executed.
    assert.commandWorked(foreignColl.createIndex({b: 1}));
    assert.commandWorked(foreignColl.createIndex({b: 1, c: 1}));

    assert.commandWorked(foreignColl.insert([{b: 1}, {b: 3}]));

    const pipeline = [
        {$match: {a: 1}},
        {$lookup: {from: foreignColl.getName(), localField: "a", foreignField: "b", as: "matched"}}
    ];

    // Test case 1: clear plan cache on the main collection.
    //
    // Run a query against the 'foreignColl' and make sure it's cached.
    assert.eq(0, foreignColl.find({b: 1, c: 1}).itcount());
    assert.eq(1, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));

    // Run the '$lookup' query and make sure it's cached.
    let results = coll.aggregate(pipeline).toArray();
    assert.eq(3, results.length, results);
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

    // Drop query cache on the main collection. This clears all cached queries in the main
    // collection only.
    assert.commandWorked(coll.runCommand("planCacheClear"));
    assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
    assert.eq(1, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));

    // Test case 2: clear plan cache on the foreign collection.
    //
    // Run the '$lookup' query again and make sure it's cached.
    results = coll.aggregate(pipeline).toArray();
    assert.eq(3, results.length, results);
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

    // Drop query cache on the foreign collection. Make sure that the plan cache on the main
    // collection is not affected.
    assert.commandWorked(foreignColl.runCommand("planCacheClear"));
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
    assert.eq(0, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));

    // Test case 3: clear plan cache on the main collection by query shape.
    //
    // Run a query against the 'foreignColl' and make sure it's cached.
    assert.eq(0, foreignColl.find({b: 1, c: 1}).itcount());
    assert.eq(1, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));

    // Run the '$lookup' query and make sure it's cached.
    results = coll.aggregate(pipeline).toArray();
    assert.eq(3, results.length, results);
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

    // Drop query cache by the query shape. This clears all cached queries in the main
    // collection only.
    assert.commandWorked(coll.runCommand("planCacheClear", {query: {a: 1}}));
    assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
    assert.eq(1, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));

    // Test case 4: clear plan cache on the foreign collection by (empty) query shape.
    //
    // Run two queries against the 'foreignColl' and make sure they're cached.
    assert.eq(2, foreignColl.find({}).itcount());
    assert.eq(0, foreignColl.find({b: 1, c: 1}).itcount());
    assert.eq(2, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));

    // Run the '$lookup' query and make sure it's cached.
    results = coll.aggregate(pipeline).toArray();
    assert.eq(3, results.length, results);
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

    // Drop query cache on the foreign collection by the query shape. This clears one cached
    // query in the foreign collection only.
    assert.commandWorked(foreignColl.runCommand("planCacheClear", {query: {}}));
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
    assert.eq(1, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));

    // Test case 5: clear by query shape which matches $lookup and non-$lookup queries.
    //
    // Run the query on the main collection whose plan cache key command shape matches the shape of
    // the $lookup query.
    results = coll.aggregate({$match: {a: 1}}).toArray();
    assert.eq(3, results.length, results);
    assert.eq(2, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

    // Run another query on the main collection with a totally different shape.
    results = coll.aggregate({$match: {a: {$in: [1, 2]}}}).toArray();
    assert.eq(4, results.length, results);
    assert.eq(3, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

    // Drop query cache on the main collection by the query shape. This clears two cached queries in
    // the main collection which match the query shape.
    assert.commandWorked(coll.runCommand("planCacheClear", {query: {a: 1}}));
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
    assert.eq(1, numPlanCacheEntries(foreignColl), dumpPlanCacheState(foreignColl));
}

//
// Query Plan Revision
// http://docs.mongodb.org/manual/core/query-plans/#query-plan-revision
// As collections change over time, the query optimizer deletes the query plan and re-evaluates
// after any of the following events:
// - The reIndex rebuilds the index.
// - You add or drop an index.
// - The mongod process restarts.
//

// Make sure the cache is emtpy.
assert.commandWorked(coll.runCommand('planCacheClear'));
assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// Case 1: The reIndex rebuilds the index.
// Steps:
//     Populate the cache with 1 entry.
//     Run reIndex on the collection.
//     Confirm that cache is empty.
// (Only standalone mode supports the reIndex command.)
const isMongos = db.adminCommand({isdbgrid: 1}).isdbgrid;
const isStandalone = !isMongos && !db.runCommand({hello: 1}).hasOwnProperty('setName');
if (isStandalone) {
    assert.eq(1, coll.find({a: 1, b: 1}).itcount());
    assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
    assert.commandWorked(coll.reIndex());
    assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
}

// Case 2: You add or drop an index.
// Steps:
//     Populate the cache with 1 entry.
//     Add an index.
//     Confirm that cache is empty.
assert.eq(1, coll.find({a: 1, b: 1}).itcount());
assert.eq(1, numPlanCacheEntries(coll), dumpPlanCacheState(coll));
assert.commandWorked(coll.createIndex({b: 1}));
assert.eq(0, numPlanCacheEntries(coll), dumpPlanCacheState(coll));

// Case 3: The mongod process restarts
// Not applicable.
})();