summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/plan_cache_index_create.js
blob: 7a37e49bfc4657010d15d90b101afd29d53d68a1 (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
/**
 * Confirms that the query plan cache is cleared on index build completion, making the newly created
 * index available to queries that had a cached plan prior to the build.
 * @tags: [
 *   requires_replication,
 *   # TODO SERVER-67607: Test plan cache with CQF enabled.
 *   cqf_incompatible,
 * ]
 */
(function() {
"use strict";

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

const dbName = "test";
const collName = "coll";

// Returns whether there is an active index build.
function indexBuildIsRunning(testDB, indexName) {
    const indexBuildFilter = {
        "command.createIndexes": collName,
        "command.indexes.0.name": indexName,
        "msg": /^Index Build/
    };
    const curOp =
        testDB.getSiblingDB("admin").aggregate([{$currentOp: {}}, {$match: indexBuildFilter}]);
    return curOp.hasNext();
}

// Returns whether a cached plan exists for 'query'.
function assertDoesNotHaveCachedPlan(coll, query) {
    const match = {"createdFromQuery.query": query};
    assert.eq([], coll.getPlanCache().list([{$match: match}]), coll.getPlanCache().list());
}

// Returns the cached plan for 'query'.
function getIndexNameForCachedPlan(coll, query) {
    const match = {"createdFromQuery.query": query};
    const plans = coll.getPlanCache().list([{$match: match}]);
    assert.eq(plans.length, 1, coll.getPlanCache().list());
    assert(plans[0].hasOwnProperty("cachedPlan"), plans);

    const cachedPlan = getCachedPlan(plans[0].cachedPlan);
    assert(cachedPlan.hasOwnProperty("inputStage"), plans);
    assert(cachedPlan.inputStage.hasOwnProperty("indexName"), plans);

    return cachedPlan.inputStage.indexName;
}

function runTest({rst, readDB, writeDB}) {
    const readColl = readDB.getCollection(collName);
    const writeColl = writeDB.getCollection(collName);

    assert.commandWorked(writeDB.runCommand({dropDatabase: 1, writeConcern: {w: "majority"}}));

    const bulk = writeColl.initializeUnorderedBulkOp();
    for (let i = 0; i < 100; ++i) {
        bulk.insert({x: i, y: i % 10, z: 0});
    }
    assert.commandWorked(bulk.execute({w: "majority"}));
    // We start with a baseline of 2 existing indexes as we will not cache plans when only a
    // single plan exists.
    assert.commandWorked(writeDB.runCommand({
        createIndexes: collName,
        indexes: [
            {key: {y: 1}, name: "less_selective", background: false},
            {key: {z: 1}, name: "least_selective", background: false}
        ],
        writeConcern: {w: "majority"}
    }));

    rst.awaitReplication();

    //
    // Confirm that the plan cache is reset on start and completion of a background index build.
    //

    // Execute a find and confirm that a cached plan exists for an existing index.
    const filter = {x: 50, y: 0, z: 0};
    assert.eq(readColl.find(filter).itcount(), 1);
    assert.eq("less_selective", getIndexNameForCachedPlan(readColl, filter));

    // Enable a failpoint that will cause an index build to block just after start. This will
    // allow us to examine PlanCache contents while index creation is in flight.
    assert.commandWorked(
        readDB.adminCommand({configureFailPoint: 'hangAfterStartingIndexBuild', mode: 'alwaysOn'}));

    // The commitIndexBuild oplog entry may block $planCacheStats on the secondary during oplog
    // application because it will hold the PBWM while waiting for the index build to complete in
    // the backgroud. Therefore, we get the primary to hold off on writing the commitIndexBuild
    // oplog entry until we are ready to resume index builds on the secondary.
    if (writeDB.getMongo().host != readDB.getMongo().host) {
        assert.commandWorked(writeDB.adminCommand(
            {configureFailPoint: 'hangAfterStartingIndexBuild', mode: 'alwaysOn'}));
    }

    // Build a "most selective" index in the background.
    TestData.dbName = dbName;
    TestData.collName = collName;
    const createIdxShell = startParallelShell(function() {
        const testDB = db.getSiblingDB(TestData.dbName);
        assert.commandWorked(testDB.runCommand({
            createIndexes: TestData.collName,
            indexes: [{key: {x: 1}, name: "most_selective", background: true}],
            writeConcern: {w: "majority"}
        }));
    }, writeDB.getMongo().port);

    // Confirm that the index build has started.
    assert.soon(() => indexBuildIsRunning(readDB, "most_selective"),
                "Index build operation not found after starting via parallelShell");

    // Confirm that there are no cached plans post index build start.
    assertDoesNotHaveCachedPlan(readColl, filter);

    // Execute a find and confirm that a previously built index is the cached plan.
    assert.eq(readColl.find(filter).itcount(), 1);
    assert.eq("less_selective", getIndexNameForCachedPlan(readColl, filter));

    // Disable the hang and wait for the index build to complete.
    assert.commandWorked(
        readDB.adminCommand({configureFailPoint: 'hangAfterStartingIndexBuild', mode: 'off'}));

    // No effect if the fail point is already disabled.
    assert.commandWorked(
        writeDB.adminCommand({configureFailPoint: 'hangAfterStartingIndexBuild', mode: 'off'}));

    assert.soon(() => !indexBuildIsRunning(readDB, "most_selective"));
    createIdxShell({checkExitSuccess: true});

    rst.awaitReplication();

    // Confirm that there are no cached plans post index build.
    assertDoesNotHaveCachedPlan(readColl, filter);

    // Now that the index has been built, execute another find and confirm that the newly
    // created index is used.
    assert.eq(readColl.find(filter).itcount(), 1);
    assert.eq("most_selective", getIndexNameForCachedPlan(readColl, filter));

    // Drop the newly created index and confirm that the plan cache has been cleared.
    assert.commandWorked(
        writeDB.runCommand({dropIndexes: collName, index: {x: 1}, writeConcern: {w: "majority"}}));
    assertDoesNotHaveCachedPlan(readColl, filter);

    //
    // Confirm that the plan cache is reset post foreground index build.
    //

    // Execute a find and confirm that an existing index is in the cache.
    assert.eq(readColl.find(filter).itcount(), 1);
    assert.eq("less_selective", getIndexNameForCachedPlan(readColl, filter));

    // Build a "most selective" index in the foreground.
    assert.commandWorked(writeDB.runCommand({
        createIndexes: collName,
        indexes: [{key: {x: 1}, name: "most_selective", background: false}],
        writeConcern: {w: "majority"}
    }));

    rst.awaitReplication();

    // Confirm that there are no cached plans post index build.
    assertDoesNotHaveCachedPlan(readColl, filter);

    // Execute a find and confirm that the newly created index is used.
    assert.eq(readColl.find(filter).itcount(), 1);
    assert.eq("most_selective", getIndexNameForCachedPlan(readColl, filter));

    // Drop the newly created index and confirm that the plan cache has been cleared.
    assert.commandWorked(
        writeDB.runCommand({dropIndexes: collName, index: {x: 1}, writeConcern: {w: "majority"}}));
    assertDoesNotHaveCachedPlan(readColl, filter);
}

const rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();
const primaryDB = rst.getPrimary().getDB(dbName);
const secondaryDB = rst.getSecondary().getDB(dbName);

if (checkSBEEnabled(primaryDB)) {
    jsTest.log("Skipping test because SBE is enabled");
    rst.stopSet();
    return;
}

runTest({rst: rst, readDB: primaryDB, writeDB: primaryDB});
runTest({rst: rst, readDB: secondaryDB, writeDB: primaryDB});

rst.stopSet();
})();