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

/**
 * update_simple.js
 *
 * Creates several docs. On each iteration, each thread chooses:
 *  - a random doc
 *  - whether to $set or $unset its field
 *  - what value to $set the field to
 */

// For isMongod and supportsDocumentLevelConcurrency.
load('jstests/concurrency/fsm_workload_helpers/server_types.js');

var $config = (function() {

    var states = {
        set: function set(db, collName) {
            this.setOrUnset(db, collName, true, this.numDocs);
        },

        unset: function unset(db, collName) {
            this.setOrUnset(db, collName, false, this.numDocs);
        }
    };

    var transitions = {
        set: {set: 0.5, unset: 0.5},
        unset: {set: 0.5, unset: 0.5}
    };

    function setup(db, collName, cluster) {
        // index on 'value', the field being updated
        assertAlways.commandWorked(db[collName].ensureIndex({value: 1}));

        // numDocs should be much less than threadCount, to make more threads use the same docs.
        this.numDocs = Math.floor(this.threadCount / 5);
        assertAlways.gt(this.numDocs, 0, 'numDocs should be a positive number');

        for (var i = 0; i < this.numDocs; ++i) {
            // make sure the inserted docs have a 'value' field, so they won't need
            // to grow when this workload runs against a capped collection
            var res = db[collName].insert({_id: i, value: 0});
            assertWhenOwnColl.writeOK(res);
            assertWhenOwnColl.eq(1, res.nInserted);
        }
    }

    return {
        threadCount: 20,
        iterations: 20,
        startState: 'set',
        states: states,
        transitions: transitions,
        data: {
            // explicitly pass db to avoid accidentally using the global `db`
            assertResult: function assertResult(db, res) {
                assertAlways.eq(0, res.nUpserted, tojson(res));

                if (isMongod(db) && supportsDocumentLevelConcurrency(db)) {
                    // Storage engines which support document-level concurrency will automatically
                    // retry any operations when there are conflicts, so we should always see a
                    // matching document.
                    assertWhenOwnColl.eq(res.nMatched, 1, tojson(res));
                } else {
                    // On storage engines that do not support document-level concurrency, it is
                    // possible that the query will not find the document. This can happen if
                    // another thread updated the target document during a yield, triggering an
                    // invalidation.
                    assertWhenOwnColl.contains(res.nMatched, [0, 1], tojson(res));
                }

                // We can't be sure nModified will be non-zero because we may have just set a key to
                // its existing value
                if (db.getMongo().writeMode() === 'commands') {
                    assertWhenOwnColl.contains(res.nModified, [0, 1], tojson(res));
                }
            },

            setOrUnset: function setOrUnset(db, collName, set, numDocs) {
                // choose a doc and value to use in the update
                var docIndex = Random.randInt(numDocs);
                var value = Random.randInt(5);

                var updater = {};
                updater[set ? '$set' : '$unset'] = {
                    value: value
                };

                var query = {
                    _id: docIndex
                };
                var res = this.doUpdate(db, collName, query, updater);
                this.assertResult(db, res);
            },

            doUpdate: function doUpdate(db, collName, query, updater) {
                return db[collName].update(query, updater);
            },
        },
        setup: setup
    };

})();