summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/plan_cache_drop_database.js
blob: abd1312b7c3c4158ddd66138b30dcf11459ca172 (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';

/**
 * plan_cache_drop_database.js
 *
 * Repeatedly executes count queries with limits against a collection that
 * is periodically dropped (as part of a database drop).  This combination of
 * events triggers the concurrent destruction of a Collection object and
 * the updating of said object's PlanCache (SERVER-17117).
 */

var $config = (function() {

    var data = {
        // Use the workload name as the database name because the workload name
        // is assumed to be unique and we'll be dropping the database as part
        // of our workload.
        dbName: 'plan_cache_drop_database'
    };

    function populateData(db, collName) {
        var coll = db[collName];

        var bulk = coll.initializeUnorderedBulkOp();
        for (var i = 0; i < 1000; ++i) {
            bulk.insert({a: 1, b: Random.rand()});
        }
        var res = bulk.execute();
        assertAlways.writeOK(res);

        // Create two indexes to force plan caching: The {a: 1} index is
        // cached by the query planner because we query on a single value
        // of 'a' and a range of 'b' values.
        assertAlways.commandWorked(coll.ensureIndex({a: 1}));
        assertAlways.commandWorked(coll.ensureIndex({b: 1}));
    }

    var states = (function() {

        function count(db, collName) {
            var coll = db.getSiblingDB(this.dbName)[collName];

            var cmdObj = {
                query: {a: 1, b: {$gt: Random.rand()}},
                limit: Random.randInt(10)
            };

            // We can't use assertAlways.commandWorked here because the plan
            // executor can be killed during the count.
            coll.runCommand('count', cmdObj);
        }

        function dropDB(db, collName) {
            var myDB = db.getSiblingDB(this.dbName);
            // We can't assert anything about the dropDatabase return value
            // because the database might not exist due to other threads
            // calling dropDB.
            myDB.dropDatabase();

            // Re-populate the data to make plan caching possible.
            populateData(myDB, collName);
        }

        return {
            count: count,
            dropDB: dropDB
        };

    })();

    var transitions = {
        count: {count: 0.95, dropDB: 0.05},
        dropDB: {count: 0.95, dropDB: 0.05}
    };

    function setup(db, collName, cluster) {
        var myDB = db.getSiblingDB(this.dbName);
        populateData(myDB, collName);
    }

    function teardown(db, collName, cluster) {
        var myDB = db.getSiblingDB(this.dbName);

        // We can't assert anything about the dropDatabase return value because
        // the database won't exist if the dropDB state is executed last.
        myDB.dropDatabase();
    }

    return {
        threadCount: 10,
        iterations: 50,
        data: data,
        states: states,
        startState: 'count',
        transitions: transitions,
        setup: setup,
        teardown: teardown
    };

})();