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

/**
 * create_database.js
 *
 * Repeatedly creates and drops a database, with the focus on creation using different name casing.
 * Create using all different methods, implicitly by inserting, creating views/indexes etc.
 *
 * Each thread uses its own database, though sometimes threads may try to create databases with
 * names that only differ in case, expecting the appriopriate error code.
 *
 * @tags: [creates_background_indexes]
 */

load('jstests/concurrency/fsm_workload_helpers/server_types.js');  // for isEphemeralForTest
load("jstests/concurrency/fsm_workload_helpers/assert_handle_fail_in_transaction.js");

var $config = (function() {
    let data = {
        checkCommandResult: function checkCommandResult(mayFailWithDatabaseDifferCase, res) {
            if (mayFailWithDatabaseDifferCase && !res.ok)
                assertWorkedOrFailedHandleTxnErrors(
                    res,
                    [ErrorCodes.IndexBuildAlreadyInProgress, ErrorCodes.DatabaseDifferCase],
                    ErrorCodes.DatabaseDifferCase);
            else
                assertWorkedHandleTxnErrors(res, ErrorCodes.IndexBuildAlreadyInProgress);
            return res;
        },

        checkWriteResult: function checkWriteResult(mayFailWithDatabaseDifferCase, res) {
            if (mayFailWithDatabaseDifferCase && res.hasWriteError())
                assertAlways.writeErrorWithCode(res, ErrorCodes.DatabaseDifferCase);
            else
                assertAlways.commandWorked(res);
            return res;
        }
    };

    let states = {
        init: function init(db, collName) {
            let uniqueNr = this.tid;
            let semiUniqueNr = Math.floor(uniqueNr / 2);

            // The semiUniqueDBName may clash and result in a DatabaseDifferCas error on creation,
            // while the uniqueDBName does not clash. The unique and created variables track this.
            this.semiUniqueDBName =
                (this.tid % 2 ? 'create_database' : 'CREATE_DATABASE') + semiUniqueNr;
            this.uniqueDBName = 'CreateDatabase' + uniqueNr;
            this.myDB = db.getSiblingDB(this.uniqueDBName);
            this.created = false;
            this.unique = true;
        },

        useSemiUniqueDBName: function useSemiUniqueDBName(db, collName) {
            this.myDB = db.getSiblingDB(this.semiUniqueDBName);
            this.unique = false;
        },

        createView: function createView(db, collName) {
            this.created =
                this.checkCommandResult(!this.unique, this.myDB.createView(collName, "nil", [])).ok;
        },

        createCollection: function createCollection(db, collName) {
            this.created =
                this.checkCommandResult(!this.unique, this.myDB.createCollection(collName)).ok;
        },

        createIndex: function createIndex(db, collName) {
            let background = Math.random > 0.5;
            let res = this.myDB.getCollection(collName).createIndex({x: 1}, {background});
            this.created |=
                this.checkCommandResult(!this.unique, res).createdCollectionAutomatically;
        },

        insert: function insert(db, collName) {
            this.created |= this.checkWriteResult(!this.created && !this.unique,
                                                  this.myDB.getCollection(collName).insert({x: 1}))
                                .nInserted == 1;
        },

        upsert: function upsert(db, collName) {
            this.created |= this.checkWriteResult(!this.created && !this.unique,
                                                  this.myDB.getCollection(collName).update(
                                                      {x: 1}, {x: 2}, {upsert: 1}))
                                .nUpserted == 1;
        },

        drop: function drop(db, collName) {
            if (this.created)
                assertAlways(this.myDB.getCollection(collName).drop());
        },

        dropDatabase: function dropDatabase(db, collName) {
            if (this.created)
                assertAlways.commandWorked(this.myDB.dropDatabase());
        },

        listDatabases: function listDatabases(db, collName) {
            for (let database of db.adminCommand({listDatabases: 1}).databases) {
                let res = db.getSiblingDB(database.name).runCommand({listCollections: 1});
                assertAlways.commandWorked(res);
                assertAlways.neq(database.name, this.myDB.toString(), "this DB shouldn't exist");
            }
        },

        listDatabasesNameOnly: function listDatabases(db, collName) {
            for (let database of db.adminCommand({listDatabases: 1, nameOnly: 1}).databases) {
                let res = db.getSiblingDB(database.name).runCommand({listCollections: 1});
                assertAlways.commandWorked(res);
                assertAlways.neq(database.name, this.myDB.toString(), "this DB shouldn't exist");
            }
        },
    };

    var transitions = {
        init: {
            useSemiUniqueDBName: 0.25,
            createView: 0.25,
            createCollection: 0.125,
            createIndex: 0.125,
            insert: 0.125,
            upsert: 0.125
        },
        useSemiUniqueDBName: {createCollection: 0.75, createView: 0.25},
        createView: {dropDatabase: 0.5, drop: 0.5},
        createCollection: {dropDatabase: 0.25, createIndex: 0.25, insert: 0.25, upsert: 0.25},
        createIndex: {insert: 0.25, upsert: 0.25, dropDatabase: 0.5},
        insert: {dropDatabase: 0.2, drop: 0.05, insert: 0.5, upsert: 0.25},
        upsert: {dropDatabase: 0.2, drop: 0.05, insert: 0.25, upsert: 0.5},
        drop: {dropDatabase: 0.75, init: 0.25},  // OK to leave the empty database behind sometimes
        dropDatabase: {init: 0.75, listDatabases: 0.15, listDatabasesNameOnly: 0.10},
        listDatabases: {init: 0.75, listDatabases: 0.15, listDatabasesNameOnly: 0.10},
        listDatabasesNameOnly: {init: 0.75, listDatabases: 0.10, listDatabasesNameOnly: 0.15},
    };

    return {
        data,
        // We only run a few iterations to reduce the amount of data cumulatively
        // written to disk.
        threadCount: 10,
        iterations: 120,
        states,
        transitions,
    };
})();