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

/**
 * view_catalog.js
 *
 * Creates, modifies and drops view namespaces concurrently. Each worker operates on their own view,
 * built on a shared underlying collection.
 */

var $config = (function() {
    var data = {
        // Use the workload name as a prefix for the view name, since the workload name is assumed
        // to be unique.
        prefix: 'view_catalog',

    };

    var states = (function() {
        function init(db, collName) {
            this.threadCollName = db[collName].getName();
            this.threadViewName = this.prefix + '_' + this.tid;
            this.counter = 0;
            this.confirmViewDefinition = function confirmViewDefinition(
                db, viewName, collName, pipeline, counter) {
                assert.eq({_id: counter}, db[collName].aggregate(pipeline).toArray()[0]);
                assert.eq({_id: counter}, db[viewName].findOne());
                const res = db.runCommand({listCollections: 1, filter: {name: viewName}});
                assertAlways.commandWorked(res);
                assertAlways.eq(1, res.cursor.firstBatch.length, tojson(res));
                assertAlways.eq({
                    name: viewName,
                    type: "view",
                    options: {viewOn: collName, pipeline: pipeline},
                    info: {readOnly: true}
                },
                                res.cursor.firstBatch[0],
                                tojson(res));
            };
        }

        function create(db, collName) {
            this.counter++;
            let pipeline = [{$match: {_id: this.counter}}];
            assertAlways.commandWorked(
                db.createView(this.threadViewName, this.threadCollName, pipeline));
            this.confirmViewDefinition(db, this.threadViewName, collName, pipeline, this.counter);
        }

        function modify(db, collName) {
            this.counter++;
            let pipeline = [{$match: {_id: this.counter}}];
            assertAlways.commandWorked(db.runCommand(
                {collMod: this.threadViewName, viewOn: this.threadCollName, pipeline: pipeline}));
            this.confirmViewDefinition(db, this.threadViewName, collName, pipeline, this.counter);
        }

        function drop(db, collName) {
            assertAlways.commandWorked(db.runCommand({drop: this.threadViewName}));

            let res = db.runCommand({listCollections: 1, filter: {name: this.threadViewName}});
            assertAlways.commandWorked(res);
            assertAlways.eq(0, res.cursor.firstBatch.length, tojson(res));
        }

        return {init: init, create: create, modify: modify, drop: drop};
    })();

    var transitions = {
        init: {create: 1},
        create: {modify: 0.75, drop: 0.25},
        modify: {modify: 0.5, drop: 0.5},
        drop: {create: 1}
    };

    var setup = function setup(db, collName, cluster) {
        let bulk = db[collName].initializeOrderedBulkOp();
        for (let i = 0; i < this.iterations; i++) {
            bulk.insert({_id: i});
        }
        assertAlways.commandWorked(bulk.execute());
    };

    // This test performs createCollection concurrently from many threads, and createCollection on a
    // sharded cluster takes a distributed lock. Since a distributed lock is acquired by repeatedly
    // attempting to grab the lock every half second for 20 seconds (a max of 40 attempts), it's
    // possible that some thread will be starved by the other threads and fail to grab the lock
    // after 40 attempts. To reduce the likelihood of this, we choose threadCount and iterations so
    // that threadCount * iterations < 40.
    // The threadCount and iterations can be increased once PM-697 ("Remove all usages of
    // distributed lock") is complete.

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