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

/**
 * indexed_insert_ttl.js
 *
 * Creates a TTL index with a short expireAfterSeconds value (5 seconds). Each
 * thread does an insert on each iteration. The first insert done by each
 * thread is marked with an extra field. At the end, we assert that the first
 * doc inserted by each thread is no longer in the collection.
 */
var $config = (function() {

    var states = {
        init: function init(db, collName) {
            var res = db[collName].insert({indexed_insert_ttl: new ISODate(), first: true});
            assertAlways.writeOK(res);
            assertWhenOwnColl.eq(1, res.nInserted, tojson(res));
        },

        insert: function insert(db, collName) {
            var res = db[collName].insert({indexed_insert_ttl: new ISODate()});
            assertAlways.writeOK(res);
            assertWhenOwnColl.eq(1, res.nInserted, tojson(res));
        }
    };

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

    function setup(db, collName, cluster) {
        var res = db[collName].ensureIndex({indexed_insert_ttl: 1},
                                           {expireAfterSeconds: this.ttlSeconds});
        assertAlways.commandWorked(res);
    }

    function teardown(db, collName, cluster) {
        // By default, the TTL monitor thread runs every 60 seconds.
        var defaultTTLSecs = 60;

        // We need to wait for the initial documents to expire. It's possible for this code to run
        // right after the TTL thread has started to sleep, which requires us to wait at least ~60
        // seconds for it to wake up and delete the expired documents. We wait at least another
        // minute just to avoid race-prone tests on overloaded test hosts.
        var timeoutMS = 2 * Math.max(defaultTTLSecs, this.ttlSeconds) * 1000;

        assertWhenOwnColl.soon(function checkTTLCount() {
            // All initial documents should be removed by the end of the workload.
            var count = db[collName].find({first: true}).itcount();
            return count === 0;
        }, 'Expected oldest documents with TTL fields to be removed', timeoutMS);
    }

    return {
        threadCount: 20,
        iterations: 200,
        states: states,
        transitions: transitions,
        setup: setup,
        data: {ttlSeconds: 5, ttlIndexExists: true},
        teardown: teardown
    };
})();