summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/reindex.js
blob: 890d52e78ccd2e61885ea769bd5a88d6a9bfb3cd (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
'use strict';

/**
 * reindex.js
 *
 * Bulk inserts 1000 documents and builds indexes. Then alternates between reindexing and querying
 * against the collection. Operates on a separate collection for each thread.
 *
 * @tags: [SERVER-40561]
 */

var $config = (function() {
    var data = {
        nIndexes: 4 + 1,  // 4 created and 1 for _id.
        nDocumentsToInsert: 1000,
        maxInteger: 100,   // Used for document values. Must be a factor of nDocumentsToInsert.
        prefix: 'reindex'  // Use filename for prefix because filename is assumed unique.
    };

    var states = (function() {
        function insertDocuments(db, collName) {
            var bulk = db[collName].initializeUnorderedBulkOp();
            for (var i = 0; i < this.nDocumentsToInsert; ++i) {
                bulk.insert({
                    text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do' +
                        ' eiusmod tempor incididunt ut labore et dolore magna aliqua.',
                    geo: {type: 'Point', coordinates: [(i % 50) - 25, (i % 50) - 25]},
                    integer: i % this.maxInteger
                });
            }
            var res = bulk.execute();
            assertAlways.commandWorked(res);
            assertAlways.eq(this.nDocumentsToInsert, res.nInserted);
        }

        function createIndexes(db, collName) {
            // The number of indexes created here is also stored in data.nIndexes.
            const coll = db[this.threadCollName];
            assertAlways.commandWorked(coll.createIndex({text: 'text'}));
            assertAlways.commandWorked(coll.createIndex({geo: '2dsphere'}));
            assertAlways.commandWorked(coll.createIndex({integer: 1}));
            assertAlways.commandWorked(coll.createIndex({"$**": 1}));
        }

        function init(db, collName) {
            this.threadCollName = this.prefix + '_' + this.tid;
            insertDocuments.call(this, db, this.threadCollName);
        }

        function query(db, collName) {
            var coll = db[this.threadCollName];
            var nInsertedDocuments = this.nDocumentsToInsert;
            var count = coll.find({integer: Random.randInt(this.maxInteger)}).itcount();
            assertWhenOwnColl.eq(
                nInsertedDocuments / this.maxInteger,
                count,
                'number of ' +
                    'documents returned by integer query should match the number ' +
                    'inserted');

            var coords = [[[-26, -26], [-26, 26], [26, 26], [26, -26], [-26, -26]]];
            var geoQuery = {geo: {$geoWithin: {$geometry: {type: 'Polygon', coordinates: coords}}}};

            // We can only perform a geo query when we own the collection and are sure a geo index
            // is present. The same is true of text queries.
            assertWhenOwnColl(function() {
                count = coll.find(geoQuery).itcount();
                assertWhenOwnColl.eq(count,
                                     nInsertedDocuments,
                                     'number of documents returned by' +
                                         ' geospatial query should match number inserted');

                count = coll.find({$text: {$search: 'ipsum'}}).itcount();
                assertWhenOwnColl.eq(count,
                                     nInsertedDocuments,
                                     'number of documents returned by' +
                                         ' text query should match number inserted');
            });

            var indexCount = db[this.threadCollName].getIndexes().length;
            assertWhenOwnColl.eq(indexCount, this.nIndexes);
        }

        function reIndex(db, collName) {
            var res = db[this.threadCollName].reIndex();
            assertAlways.commandWorked(res);
        }

        return {init: init, createIndexes: createIndexes, reIndex: reIndex, query: query};
    })();

    var transitions = {
        init: {createIndexes: 1},
        createIndexes: {reIndex: 0.5, query: 0.5},
        reIndex: {reIndex: 0.5, query: 0.5},
        query: {reIndex: 0.5, query: 0.5}
    };

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