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

/**
 * Build indexes with different abort causes.
 * - Indexing error.
 * - Abort due to dropIndexes.
 * - Abort due to killOp on primary.
 *
 * Abort due to DiskSpaceMonitor is not tested as it would interfere with other concurrent tests
 * creating index builds. Similarly, killOp on secondary nodes is not tested as it can result in a
 * node crash, interfering with other tests.
 *
 * @tags: [
 *     creates_background_indexes,
 *     # The test uses $currentOp, which is not supported in transactions.
 *     does_not_support_transactions,
 *     requires_fcv_71,
 *     requires_replication
 * ]
 */

load("jstests/libs/fail_point_util.js");
load("jstests/noPassthrough/libs/index_build.js");

var $config = (function() {
    const data = {
        prefix: "index_build_abort_",
        nCollections: 3,
        nDocuments: 25000,
        expectedErrorCodes: [ErrorCodes.IndexBuildAborted, ErrorCodes.Interrupted, 13026],
        mutexColl: "index_build_abort_mutexes",
    };

    function randInt(max) {
        return Math.floor(Math.random() * max);
    }

    function getRandCollectionName() {
        return data.prefix + randInt(data.nCollections);
    }

    function getCollMutexName(collName) {
        return collName + "_mutex";
    }

    function mutexTryLock(db, collName) {
        const collMutex = getCollMutexName(collName);
        let doc = db[data.mutexColl].findAndModify(
            {query: {mutex: collMutex, locked: 0}, update: {$set: {locked: 1}}});
        if (doc === null) {
            return false;
        }
        return true;
    }

    function mutexUnlock(db, collName) {
        const collMutex = getCollMutexName(collName);
        db[data.mutexColl].update({mutex: collMutex}, {$set: {locked: 0}});
    }

    const states = {
        dropCollAndCreateIndexBuild: function dropCollAndCreateIndexBuild(db, collName) {
            const randomColl = getRandCollectionName();
            var coll = db[randomColl];
            if (mutexTryLock(db, randomColl)) {
                try {
                    // Having the collection drop outside the lock to allow a drop concurrent to an
                    // index build might be more interesting, but we'd also be allowing a drop in
                    // the middle of bulk insert, or before the createIndexes starts.
                    coll.drop();
                    var bulk = coll.initializeUnorderedBulkOp();
                    const failDocumentIndex = randInt(this.nDocuments);
                    for (var i = 0; i < this.nDocuments; ++i) {
                        if (failDocumentIndex == i) {
                            bulk.insert({a: [0, "a"]});
                        } else {
                            bulk.insert({a: [0, 0]});
                        }
                    }
                    let bulkRes = bulk.execute();
                    assertAlways.commandWorked(bulkRes);
                    assertAlways.eq(this.nDocuments, bulkRes.nInserted, tojson(bulkRes));
                    assertAlways.commandFailedWithCode(coll.createIndexes([{a: "2d"}]),
                                                       this.expectedErrorCodes);
                } finally {
                    mutexUnlock(db, randomColl);
                }
            }
        },
        dropIndexes: function dropIndexes(db, collName) {
            assertAlways.commandWorkedOrFailedWithCode(
                db.runCommand({dropIndexes: getRandCollectionName(), index: "*"}),
                ErrorCodes.NamespaceNotFound);
        },
        killOpIndexBuild: function killOpIndexBuild(db, collName) {
            let nTry = 0;
            while (nTry++ < 10) {
                try {
                    const opId = IndexBuildTest.getIndexBuildOpId(db, getRandCollectionName());
                    if (opId != -1) {
                        db.killOp(opId);
                        break;
                    }
                } catch (e) {
                    jsTestLog("Suppressed exception during killOp attempt: " + e);
                }
            }
        }
    };

    const transtitionToAllStates = {
        dropCollAndCreateIndexBuild: 1,
        dropIndexes: 1,
        killOpIndexBuild: 1,
    };
    const transitions = {
        dropCollAndCreateIndexBuild: transtitionToAllStates,
        dropIndexes: transtitionToAllStates,
        killOpIndexBuild: transtitionToAllStates
    };

    const setup = function(db, collName, cluster) {
        for (let coll = 0; coll < this.nCollections; ++coll) {
            const mutexName = getCollMutexName(data.prefix + coll);
            db[data.mutexColl].insert({mutex: mutexName, locked: 0});
        }
    };

    const teardown = function(db, collName, cluster) {
        for (let coll = 0; coll < this.nCollections; ++coll) {
            const collName = data.prefix + coll;
            db[collName].drop();
            db[getCollMutexName(collName)].drop();
        }
    };

    return {
        threadCount: 12,
        iterations: 200,
        startState: 'dropCollAndCreateIndexBuild',
        states: states,
        transitions: transitions,
        setup: setup,
        teardown: teardown,
        data: data,
    };
})();