summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/convert_to_capped_collection.js
blob: 79b9934077bb909ee2392a1ac51ab9e5af9ef1b8 (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
/**
 * convert_to_capped_collection.js
 *
 * Creates a non-capped collection. Converts it to a
 * capped collection. After each iteration, truncates the
 * collection, ensuring that the storage size of the
 * collection is still a multiple of 256.
 *
 * MongoDB raises the storage size of a capped collection
 * to an integer multiple of 256.
 */
load('jstests/concurrency/fsm_workload_helpers/drop_utils.js');

var $config = (function() {
    // TODO: This workload may fail if an iteration multiplier is specified.
    var data = {
        prefix: 'convert_to_capped_collection'
    };

    var states = (function() {

        function uniqueCollectionName(prefix, tid) {
            return prefix + '_' + tid;
        }

        function isMultiple256(num) {
            return num % 256 === 0;
        }

        function init(db, collName) {
            this.threadCollName = uniqueCollectionName(this.prefix, this.tid);

            var bulk = db[this.threadCollName].initializeUnorderedBulkOp();
            for (var i = 0; i < (this.tid + 1) * 200; i++) {
                bulk.insert({i: i, rand: Random.rand()});
            }

            var res = bulk.execute();
            assertAlways.writeOK(res);
            assertAlways.eq((this.tid + 1) * 200, res.nInserted);

            assertWhenOwnDB(!db[this.threadCollName].isCapped());
            assertWhenOwnDB.commandWorked(db[this.threadCollName].convertToCapped(this.size));
            assertWhenOwnDB(db[this.threadCollName].isCapped());
            assertWhenOwnDB(isMultiple256(db[this.threadCollName].stats().maxSize));
        }

        function convertToCapped(db, collName) {
            // divide size by 1.5 so that the resulting size
            // is not a multiple of 256
            this.size /= 1.5;

            assertWhenOwnDB.commandWorked(db[this.threadCollName].convertToCapped(this.size));
            assertWhenOwnDB(db[this.threadCollName].isCapped());
            assertWhenOwnDB(isMultiple256(db[this.threadCollName].stats().maxSize));

            // only the _id index should remain after running convertToCapped
            var indexKeys = db[this.threadCollName].getIndexKeys();
            assertWhenOwnDB.eq(1, indexKeys.length);
            assertWhenOwnDB(function() {
                assertWhenOwnDB.docEq({_id: 1}, indexKeys[0]);
            });
        }

        return {
            init: init,
            convertToCapped: convertToCapped
        };
    })();

    var transitions = {
        init: {convertToCapped: 1},
        convertToCapped: {convertToCapped: 1}
    };

    function setup(db, collName, cluster) {
        // Initial size should not be a power of 256.
        this.size = Math.pow(2, this.iterations + 5) + 1;
    }

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

    return {
        threadCount: 10,
        iterations: 20,
        data: data,
        states: states,
        transitions: transitions,
        setup: setup,
        teardown: teardown
    };

})();