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

/**
 * Tests killing time-series inserts.
 *
 * @tags: [
 *   requires_timeseries,
 *   # killOp does not support stepdowns.
 *   does_not_support_stepdowns,
 *   # Timeseries do not support multi-document transactions with inserts.
 *   does_not_support_transactions,
 *   # Kill operations do not propagate for writes on mongos.
 *   assumes_unsharded_collection
 * ]
 */

var $config = (function() {
    const timeFieldName = 'time';
    const metaFieldName = 'tag';

    function getCollectionName(collName) {
        return jsTestName() + "_" + collName;
    }

    const insert = function(db, collName, ordered) {
        const docs = [];
        for (let i = 0; i < 1000; ++i) {
            docs.push({[timeFieldName]: ISODate(), [metaFieldName]: i % 10});
        }
        // TODO (SERVER-44673): Use assert.commandWorkedOrFailedWithCode without special handling.
        const res = db.runCommand({insert: collName, documents: docs, ordered: ordered});
        if (res.hasOwnProperty('writeErrors') && res.writeErrors.length > 0) {
            assert.eq(res.writeErrors[0].code, ErrorCodes.Interrupted);
        } else if (res.hasOwnProperty('writeConcernError')) {
            assert.eq(res.writeConcernError.code, ErrorCodes.Interrupted);
        } else {
            assert.commandWorkedOrFailedWithCode(res, ErrorCodes.Interrupted);
        }
    };

    const states = {
        init: function(db, collName) {},

        insertOrdered: function(db, collNameSuffix) {
            let collName = getCollectionName(collNameSuffix);
            insert(db, collName, true);
        },

        insertUnordered: function(db, collNameSuffix) {
            let collName = getCollectionName(collNameSuffix);
            insert(db, collName, false);
        },

        killInsert: function(db, collNameSuffix) {
            let collName = getCollectionName(collNameSuffix);
            const inprog =
                assert.commandWorked(db.currentOp({ns: db[collName].getFullName(), op: 'insert'}))
                    .inprog;
            if (inprog.length) {
                assert.commandWorked(
                    db.adminCommand({killOp: 1, op: inprog[Random.randInt(inprog.length)].opid}));
            }
        },
    };

    const setup = function(db, collNameSuffix) {
        let collName = getCollectionName(collNameSuffix);
        assert.commandWorked(db.createCollection(
            collName, {timeseries: {timeField: timeFieldName, metaField: metaFieldName}}));
    };

    const standardTransition = {
        insertOrdered: 0.4,
        insertUnordered: 0.4,
        killInsert: 0.2,
    };

    const transitions = {
        init: standardTransition,
        insertOrdered: standardTransition,
        insertUnordered: standardTransition,
        killInsert: standardTransition,
    };

    return {
        threadCount: 10,
        iterations: 100,
        setup: setup,
        states: states,
        transitions: transitions,
    };
})();