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

/**
 * map_reduce_merge_nonatomic.js
 *
 * Generates some random data and inserts it into a collection. Runs a
 * map-reduce command over the collection that computes the frequency
 * counts of the 'value' field and stores the results in an existing
 * collection on a separate database.
 *
 * Uses the "merge" action to combine the results with the contents
 * of the output collection.
 *
 * Specifies nonAtomic=true.
 */
load('jstests/concurrency/fsm_libs/extend_workload.js'); // for extendWorkload
load('jstests/concurrency/fsm_workloads/map_reduce_inline.js'); // for $config
load('jstests/concurrency/fsm_workload_helpers/drop_utils.js'); // for dropDatabases

var $config = extendWorkload($config, function($config, $super) {

    // Use the workload name as a prefix for the database name,
    // since the workload name is assumed to be unique.
    var prefix = 'map_reduce_merge_nonatomic';

    function uniqueDBName(prefix, tid) {
        return prefix + tid;
    }

    $config.states.init = function init(db, collName) {
        $super.states.init.apply(this, arguments);

        this.outDBName = db.getName() + uniqueDBName(prefix, this.tid);
        var outDB = db.getSiblingDB(this.outDBName);
        assertAlways.commandWorked(outDB.createCollection(collName));
    };

    $config.states.mapReduce = function mapReduce(db, collName) {
        var outDB = db.getSiblingDB(this.outDBName);
        var fullName = outDB[collName].getFullName();
        assertAlways(outDB[collName].exists() !== null,
                     "output collection '" + fullName + "' should exist");

        var options = {
            finalize: this.finalizer,
            out: {
                merge: collName,
                db: this.outDBName,
                nonAtomic: true
            }
        };

        var res = db[collName].mapReduce(this.mapper, this.reducer, options);
        assertAlways.commandWorked(res);
    };

    $config.teardown = function teardown(db, collName, cluster) {
        var pattern = new RegExp('^' + db.getName() + prefix + '\\d+$');
        dropDatabases(db, pattern);
    };

    return $config;
});