summaryrefslogtreecommitdiff
path: root/jstests/core/plan_cache_shell_helpers.js
blob: b663e09e90b0eb18d1a56e6d3af65751d8515977 (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
// Test the shell helpers which wrap the plan cache commands.
//
// @tags: [
//   assumes_balancer_off,
//   assumes_read_concern_unchanged,
//   # This test attempts to perform queries and introspect 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. If all chunks are moved off of a shard, it can cause the plan cache to
//   # miss commands.
//   assumes_read_preference_unchanged,
//   assumes_unsharded_collection,
//   does_not_support_stepdowns,
//   # 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/aggregation/extras/utils.js');  // For assertArrayEq.
load("jstests/libs/analyze_plan.js");         // For getPlanCacheKeyFromShape.
load("jstests/libs/sbe_util.js");             // For checkSBEEnabled.

const isSbeEnabled = checkSBEEnabled(db, ["featureFlagSbeFull"]);
var coll = db.jstests_plan_cache_shell_helpers;
coll.drop();

function assertCacheLength(length) {
    const cacheContents = coll.getPlanCache().list();
    assert.eq(length, cacheContents.length, cacheContents);
}

// Add data and indices.
var n = 200;
for (var i = 0; i < n; i++) {
    assert.commandWorked(coll.insert({a: i, b: -1, c: 1}));
}
assert.commandWorked(coll.createIndex({a: 1}));
assert.commandWorked(coll.createIndex({b: 1}));

// Populate plan cache.
var queryB = {a: {$gte: 199}, b: -1};
var projectionB = {_id: 0, b: 1};
var sortC = {c: -1};
assert.eq(1, coll.find(queryB, projectionB).sort(sortC).itcount(), 'unexpected document count');
assert.eq(1, coll.find(queryB, projectionB).itcount(), 'unexpected document count');
assert.eq(1, coll.find(queryB).sort(sortC).itcount(), 'unexpected document count');
assert.eq(1, coll.find(queryB).itcount(), 'unexpected document count');
assertCacheLength(4);

//
// PlanCache.getName
//

var planCache = coll.getPlanCache();
assert.eq(coll.getName(), planCache.getName(), 'name of plan cache should match collection');

//
// PlanCache.help
//
planCache.help();

//
// shellPrint
//

print('plan cache:');
print(planCache);

//
// collection.getPlanCache().list
//

var missingCollection = db.jstests_plan_cache_missing;
missingCollection.drop();
// Listing the cache for a non-existing collection is expected to fail by throwing.
assert.throws(() => missingCollection.getPlanCache().list());

// Test that we can use $group and $count with the list() helper.
assert.eq([{_id: null, count: 4}],
          planCache.list([{$group: {_id: null, count: {$sum: 1}}}]),
          planCache.list());
assert.eq([{count: 4}], planCache.list([{$count: "count"}]), planCache.list());

// Test that we can collect descriptions of all the queries that created cache entries using the
// list() helper.
if (isSbeEnabled) {
    assertArrayEq({
        expected: [
            {planCacheKey: getPlanCacheKeyFromShape({query: queryB, collection: coll, db: db})},
            {
                planCacheKey:
                    getPlanCacheKeyFromShape({query: queryB, sort: sortC, collection: coll, db: db})
            },
            {
                planCacheKey: getPlanCacheKeyFromShape(
                    {query: queryB, projection: projectionB, collection: coll, db: db})
            },
            {
                planCacheKey: getPlanCacheKeyFromShape(
                    {query: queryB, projection: projectionB, sort: sortC, collection: coll, db: db})
            },
        ],
        actual: planCache.list([{$project: {planCacheKey: 1}}]),
        extraErrorMsg: planCache.list()
    });
} else {
    assertArrayEq({
        expected: [
            {query: queryB, sort: {}, projection: {}},
            {query: queryB, sort: sortC, projection: {}},
            {query: queryB, sort: {}, projection: projectionB},
            {query: queryB, sort: sortC, projection: projectionB}
        ],
        actual: planCache.list([{$replaceWith: "$createdFromQuery"}]),
        extraErrorMsg: planCache.list()
    });
}

//
// collection.getPlanCache().clearPlansByQuery
//

// should not error on non-existent query shape.
planCache.clearPlansByQuery({unknownfield: 1});
// should error on missing required field query.
assert.throws(function() {
    planCache.clearPlansByQuery();
});

// Invoke with various permutations of required (query) and optional (projection, sort) arguments.
planCache.clearPlansByQuery(queryB, projectionB);
assertCacheLength(3);

planCache.clearPlansByQuery(queryB, undefined, sortC);
assertCacheLength(2);

planCache.clearPlansByQuery(queryB);
assertCacheLength(1);

planCache.clear();
assertCacheLength(0);

// clearPlansByQuery() will also accept a single argument with the query shape object
// as an alternative to specifying the query, sort and projection parameters separately.
// Format of query shape object:
// {
//     query: <query>,
//     projection: <projection>,
//     sort: <sort>
// }

// Repopulate cache
assert.eq(1, coll.find(queryB).sort(sortC).itcount(), 'unexpected document count');

// Clear using query shape object.
planCache.clearPlansByQuery({query: queryB, projection: {}, sort: sortC});
assertCacheLength(0);

// Should not error on missing or extra fields in query shape object.
planCache.clearPlansByQuery({query: queryB});
planCache.clearPlansByQuery(
    {query: queryB, sort: sortC, projection: projectionB, unknown_field: 1});

//
// collection.getPlanCache().clear
//

// Should not error on non-existent collection.
missingCollection.getPlanCache().clear();
// Re-populate plan cache with 1 query shape.
assert.eq(1, coll.find(queryB, projectionB).sort(sortC).itcount(), 'unexpected document count');
assertCacheLength(1);
// Clear cache.
planCache.clear();
assertCacheLength(0);

// Verify that explaining a find command does not write to the plan cache.
planCache.clear();
const explain = coll.find(queryB, projectionB).sort(sortC).explain(true);
assertCacheLength(0);
}());