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

/**
 * collmod.js
 *
 * Base workload for collMod command.
 * Generates some random data and inserts it into a collection with a
 * TTL index. Runs a collMod command to change the value of the
 * expireAfterSeconds setting to a random integer.
 *
 * All threads update the same TTL index on the same collection.
 */
var $config = (function() {
    var data = {
        numDocs: 1000,
        maxTTL: 5000  // max time to live
    };

    var states = (function() {
        function collMod(db, collName) {
            var newTTL = Random.randInt(this.maxTTL);
            var res = db.runCommand({
                collMod: this.threadCollName,
                index: {keyPattern: {createdAt: 1}, expireAfterSeconds: newTTL}
            });
            assertAlways.commandWorkedOrFailedWithCode(res,
                                                       [ErrorCodes.ConflictingOperationInProgress]);
            // only assert if new expireAfterSeconds differs from old one
            if (res.ok === 1 && res.hasOwnProperty('expireAfterSeconds_new')) {
                assertWhenOwnDB.eq(res.expireAfterSeconds_new, newTTL);
            }

            // Attempt an invalid collMod which should always fail regardless of whether a WCE
            // occurred. This is meant to reproduce SERVER-56772.
            const encryptSchema = {$jsonSchema: {properties: {_id: {encrypt: {}}}}};
            assertAlways.commandFailedWithCode(
                db.runCommand({
                    collMod: this.threadCollName,
                    validator: encryptSchema,
                    validationAction: "warn"
                }),
                [ErrorCodes.ConflictingOperationInProgress, ErrorCodes.QueryFeatureNotAllowed]);
        }

        return {collMod: collMod};
    })();

    var transitions = {collMod: {collMod: 1}};

    function setup(db, collName, cluster) {
        // other workloads that extend this one might have set 'this.threadCollName'
        this.threadCollName = this.threadCollName || collName;
        var bulk = db[this.threadCollName].initializeUnorderedBulkOp();
        for (var i = 0; i < this.numDocs; ++i) {
            bulk.insert({createdAt: new Date()});
        }

        var res = bulk.execute();
        assertAlways.commandWorked(res);
        assertAlways.eq(this.numDocs, res.nInserted);

        // create TTL index
        res = db[this.threadCollName].createIndex({createdAt: 1}, {expireAfterSeconds: 3600});
        assertAlways.commandWorked(res);
    }

    return {
        threadCount: 10,
        iterations: 20,
        data: data,
        startState: 'collMod',
        states: states,
        transitions: transitions,
        setup: setup
    };
})();