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

/**
 * explain_update.js
 *
 * Runs explain() and update() on a collection.
 */
load('jstests/concurrency/fsm_libs/extend_workload.js');           // for extendWorkload
load('jstests/concurrency/fsm_workloads/explain.js');              // for $config
load('jstests/concurrency/fsm_workload_helpers/server_types.js');  // for isMongos

var $config = extendWorkload($config, function($config, $super) {
    $config.states = Object.extend({
        explainBasicUpdate: function explainBasicUpdate(db, collName) {
            var res =
                db[collName].explain('executionStats').update({i: this.nInserted}, {$set: {j: 49}});
            assertAlways.commandWorked(res);
            assertWhenOwnColl(function() {
                assertWhenOwnColl.eq(1, explain.executionStats.totalDocsExamined);

                // document should not have been updated.
                var doc = db[collName].findOne({i: this.nInserted});
                assertWhenOwnColl.eq(2 * this.nInserted, doc.j);
            }.bind(this));
        },
        explainUpdateUpsert: function explainUpdateUpsert(db, collName) {
            var res = db[collName]
                          .explain('executionStats')
                          .update({i: 2 * this.nInserted + 1},
                                  {$set: {j: 81}},
                                  /* upsert */ true);
            assertAlways.commandWorked(res);
            var stage = res.executionStats.executionStages;

            // if explaining a write command through mongos
            if (isMongos(db)) {
                stage = stage.shards[0].executionStages;
            }
            assertAlways.eq(stage.stage, 'UPDATE');
            assertWhenOwnColl(stage.wouldInsert);

            // make sure that the insert didn't actually happen.
            assertWhenOwnColl.eq(this.nInserted, db[collName].find().itcount());
        },
        explainUpdateMulti: function explainUpdateMulti(db, collName) {
            var res = db[collName]
                          .explain('executionStats')
                          .update({i: {$lte: 2}},
                                  {$set: {b: 3}},
                                  /* upsert */ false,
                                  /* multi */ true);
            assertAlways.commandWorked(res);
            var stage = res.executionStats.executionStages;

            // if explaining a write command through mongos
            if (isMongos(db)) {
                stage = stage.shards[0].executionStages;
            }
            assertAlways.eq(stage.stage, 'UPDATE');
            assertWhenOwnColl(!stage.wouldInsert);
            assertWhenOwnColl.eq(3, stage.nMatched);
            assertWhenOwnColl.eq(3, stage.nWouldModify);
        }
    },
                                   $super.states);

    $config.transitions = Object.extend(
        {explain: $config.data.assignEqualProbsToTransitions($config.states)}, $super.transitions);

    return $config;
});