summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/find_cmd_with_indexes_timeseries.js
blob: 59b98f859878c1c83d79d7a8fb6f4baf451c2493 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'use strict';

/**
 * This test verifies that neither index creation nor find cmd operation on a time-series collection
 * leads to incorrect data results.
 *
 * Creates a time-series collection and populates some data in it. Then creates and drops indexes on
 * the time and meta fields concurrently with queries. Confirms that queries on the time and meta
 * fields return the expected document results. The queries will exercise the find cmd on indexes or
 * collection scans (if no index at the moment of execution). The query results should be the same
 * regardless, the indexes should return the same data as collection scans.
 *
 * @tags: [
 *   requires_timeseries,
 * ]
 */

load("jstests/core/timeseries/libs/timeseries.js");

var $config = (function() {
    // Hardcode time-series collection information so that the threads can all obtain it and run on
    // the same fields and indexes.
    const timeFieldName = "tm";
    const metaFieldName = "mm";
    const metaIndexKey = "mm.a";
    const timeIndexSpec = {"tm": 1};
    const metaIndexSpec = {"mm.a": 1};

    const numDocs = 10;
    // Hardcode 10 time values so that the times are known for the queries to use.
    // Note: times-series collection implementation batches writes by hour into the same bucket. The
    // timestamps selected are deliberately provoking the use of multiple buckets.
    const docTimes = [
        ISODate("2021-01-20T00:00:00.000Z"),
        ISODate("2021-01-20T00:10:00.000Z"),
        ISODate("2021-01-20T00:20:00.000Z"),
        ISODate("2021-01-20T00:30:00.000Z"),
        ISODate("2021-01-20T00:40:00.000Z"),
        ISODate("2021-01-20T00:50:00.000Z"),
        ISODate("2021-01-20T01:00:00.000Z"),
        ISODate("2021-01-20T01:10:00.000Z"),
        ISODate("2021-01-20T01:20:00.000Z"),
        ISODate("2021-01-20T02:40:00.000Z"),
    ];

    // Support for time-series collections may or may not be turned on in the server. This value
    // will be initialized during the 'init' state so that the rest of the states can opt to do
    // nothing when disabled, since testing an inactive feature is pointless.
    let data = {
        supportsTimeseriesCollections: false,
    };

    function getCollectionName(collName) {
        return "find_cmd_with_indexes_timeseries_" + collName;
    }

    /**
     * Checks that the dropIndex cmd result either succeeded or failed in an acceptible manner.
     */
    function processDropIndex(dropIndexRes, indexSpec) {
        assertAlways(dropIndexRes.ok == 1 || dropIndexRes.code == ErrorCodes.IndexNotFound,
                     "Drop index for spec '" + indexSpec + "' failed: " + tojson(dropIndexRes));
    }

    /**
     * Checks that the createIndex cmd result either succeeded or failed in an acceptible manner.
     */
    function processCreateIndex(createIndexRes, indexSpec) {
        assertAlways(createIndexRes.ok == 1 ||
                         createIndexRes.code == ErrorCodes.IndexAlreadyExists ||
                         createIndexRes.code == ErrorCodes.IndexBuildAborted ||
                         createIndexRes.code == ErrorCodes.NoMatchingDocument,
                     "Create index for spec '" + indexSpec + "'failed: " + tojson(createIndexRes));
    }

    const states = {
        /**
         * Figures out whether the server supports time-series collections and sets
         * 'supportsTimeseriesCollections' accordingly.
         */
        init: function init(db, collName) {
            if (!TimeseriesTest.timeseriesCollectionsEnabled(db.getMongo())) {
                return;
            }
            this.supportsTimeseriesCollections = true;
        },

        /**
         * Creates an index 'timeIndexSpec' on the time-series collection.
         */
        createTimeIndex: function createTimeIndex(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            const createIndexRes = coll.createIndex(timeIndexSpec);
            processCreateIndex(createIndexRes, timeIndexSpec);
        },

        /**
         * Drops the index 'timeIndexSpec' on the time-series collection.
         */
        dropTimeIndex: function dropTimeIndex(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            const dropIndexRes = coll.dropIndex(timeIndexSpec);
            processDropIndex(dropIndexRes, timeIndexSpec);
        },

        /**
         * Creates an index 'metaIndexSpec' on the time-series collection.
         */
        createMetaIndex: function createMetaIndex(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            const createIndexRes = coll.createIndex(metaIndexSpec);
            processCreateIndex(createIndexRes, metaIndexSpec);
        },

        /**
         * Drops the index 'metaIndexSpec' on the time-series collection.
         */
        dropMetaIndex: function dropMetaIndex(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            const dropIndexRes = coll.dropIndex(metaIndexSpec);
            processDropIndex(dropIndexRes, metaIndexSpec);
        },

        /**
         * Queries all of the time-series collection documents by time field 'timeFieldName'. This
         * query will either provoke a collection scan or use the time field index (if present).
         */
        queryTimeAll: function queryTimeAll(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            try {
                const queryDocs = coll.find({[timeFieldName]: {$lte: docTimes[9]}}).toArray();
                assertAlways.eq(numDocs,
                                queryDocs.length,
                                "Failed to find " + numDocs +
                                    " documents with time field greater than '" + docTimes[0] +
                                    "'. Query results: " + tojson(queryDocs));
            } catch (e) {
                // The query may fail because the index got dropped out from under it.
                assertAlways(e.code == ErrorCodes.QueryPlanKilled,
                             "Expected a QueryPlanKilled error, but encountered: " + e.message);
            }
        },

        /**
         * Query a subset of the time-series collection documents by time field. This query will
         * either provoke a collection scan or use the time field index (if present).
         */
        queryTimeSubset: function queryTimeSubset(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            try {
                const queryDocs = coll.find({[timeFieldName]: {$lte: docTimes[4]}}).toArray();
                assertAlways.eq(numDocs / 2,
                                queryDocs.length,
                                "Failed to find " + (numDocs / 2) +
                                    " documents with time field greater than '" + docTimes[5] +
                                    "'. Query results: " + tojson(queryDocs));
            } catch (e) {
                // The query may fail because the index got dropped out from under it.
                assertAlways(e.code == ErrorCodes.QueryPlanKilled,
                             "Expected a QueryPlanKilled error, but encountered: " + e.message);
            }
        },

        /**
         * Query all of the time-series collection documents by meta field. This query will either
         * scan the collection or use the meta field index (if present).
         */
        queryMetaAll: function queryMetaAll(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            try {
                const queryDocs = coll.find({[metaIndexKey]: {$gte: 0}}).toArray();
                assertAlways.eq(
                    numDocs,
                    queryDocs.length,
                    "Failed to find " + numDocs +
                        " documents with meta field greater than or equal to 0. Query results: " +
                        tojson(queryDocs));
            } catch (e) {
                // The query may fail because the index got dropped out from under it.
                assertAlways(e.code == ErrorCodes.QueryPlanKilled,
                             "Expected a QueryPlanKilled error, but encountered: " + e.message);
            }
        },

        /**
         * Query a subset of the time-series collection documents by meta field. This query will
         * either scan the collection or use the meta field index (if present).
         */
        queryMetaSubset: function queryMetaSubset(db, collName) {
            if (!this.supportsTimeseriesCollections) {
                return;
            }

            const coll = db.getCollection(getCollectionName(collName));
            try {
                const queryDocs = coll.find({[metaIndexKey]: {$gt: 4}}).toArray();
                assertAlways.eq(numDocs / 2,
                                queryDocs.length,
                                "Failed to find " + (numDocs / 2) +
                                    " documents with meta field greater than 4. Query results: " +
                                    tojson(queryDocs));
            } catch (e) {
                // The query may fail because the index got dropped out from under it.
                assertAlways(e.code == ErrorCodes.QueryPlanKilled,
                             "Expected a QueryPlanKilled error, but encountered: " + e.message);
            }
        },

    };

    /**
     * Creates a time-series collection and pre-populates it with 'numDocs' documents.
     */
    function setup(db, collName, cluster) {
        if (!TimeseriesTest.timeseriesCollectionsEnabled(db.getMongo())) {
            jsTestLog("Skipping test because the time-series collection feature flag is disabled");
            return;
        }

        // Create the collection.
        assertAlways.commandWorked(db.createCollection(getCollectionName(collName), {
            timeseries: {
                timeField: timeFieldName,
                metaField: metaFieldName,
            }
        }));

        // Populate numDocs documents to query.
        const coll = db.getCollection(getCollectionName(collName));
        for (let i = 0; i < numDocs; ++i) {
            // Insert a document with the current time.
            const res =
                coll.insert({_id: i, [timeFieldName]: docTimes[i], [metaFieldName]: {a: i}});
            assertAlways.commandWorked(res);
            assertAlways.eq(1, res.nInserted, tojson(res));
        }
    }

    const standardTransition = {
        createTimeIndex: 0.2,
        dropTimeIndex: 0.05,
        createMetaIndex: 0.2,
        dropMetaIndex: 0.05,
        queryTimeAll: 0.125,
        queryTimeSubset: 0.125,
        queryMetaAll: 0.125,
        queryMetaSubset: 0.125,
    };

    const transitions = {
        init: standardTransition,
        createTimeIndex: standardTransition,
        dropTimeIndex: standardTransition,
        createMetaIndex: standardTransition,
        dropMetaIndex: standardTransition,
        queryTimeAll: standardTransition,
        queryTimeSubset: standardTransition,
        queryMetaAll: standardTransition,
        queryMetaSubset: standardTransition,
    };

    return {
        threadCount: 15,
        iterations: 700,
        states: states,
        data: data,
        transitions: transitions,
        setup: setup,
    };
})();