summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/insert_ttl_timeseries.js
blob: eadbb937cc943cdb6ed2f0a44e18421f6f10b567 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';

/**
 * Creates a time-series collection with a short expireAfterSeconds value. Each thread does an
 * insert on each iteration with a time, a metadata field, 'tid', and random measurement, 'data'. At
 * the end, we wait until the first set of documents has been deleted.
 *
 * @tags: [
 *   assumes_no_implicit_collection_creation_after_drop,
 *   does_not_support_stepdowns,
 *   requires_timeseries,
 *   uses_ttl,
 * ]
 */

var $config = (function() {
    const initData = {
        getCollectionName: function(collName) {
            return "insert_ttl_timeseries_" + collName;
        },

        getCollection: function(db, collName) {
            return db.getCollection(this.getCollectionName(collName));
        },
    };

    const timeFieldName = "time";
    const metaFieldName = "tid";
    const ttlSeconds = 3;
    const defaultBucketMaxRangeMs = 3600 * 1000;
    const batchSize = 10;

    // Generates a time in the past that will be expired soon. TTL for time-series collections only
    // expires buckets once the bucket minimum is past the maximum range of the bucket size, in this
    // case one hour.
    const getTime = function() {
        const now = new Date();
        return new Date(now.getTime() - defaultBucketMaxRangeMs);
    };

    const states = {
        init: function init(db, collName) {
            const coll = this.getCollection(db, collName);
            const res = coll.insert({
                [metaFieldName]: this.tid,
                [timeFieldName]: getTime(),
                first: true,
            });
            assertAlways.commandWorked(res);
            assertAlways.eq(1, res.nInserted, tojson(res));
        },

        /**
         * Insert a single measurement for the current thread id.
         */
        insertOne: function insertOne(db, collName) {
            const coll = this.getCollection(db, collName);
            const res = coll.insert({
                [metaFieldName]: this.tid,
                [timeFieldName]: getTime(),
                data: Random.rand(),
            });
            assertAlways.commandWorked(res);
            assertAlways.eq(1, res.nInserted, tojson(res));
        },

        /**
         * Insert an ordered batch for the current thread id. All measurements should end up in the
         * same bucket.
         */
        insertManyOrdered: function insertManyOrdered(db, collName) {
            const coll = this.getCollection(db, collName);
            const docs = [];
            for (let i = 0; i < batchSize; i++) {
                docs.push({
                    [metaFieldName]: this.tid,
                    [timeFieldName]: getTime(),
                    data: Random.rand(),
                });
            }
            const res = coll.insertMany(docs, {ordered: true});
            assertAlways.commandWorked(res);
            assertAlways.eq(res.insertedIds.length, batchSize);
        },

        /**
         * Insert an unordered batch for a specific thread id. All measurements should end up in
         * the same bucket.
         */
        insertManyUnordered: function insertManyUnordered(db, collName) {
            const coll = this.getCollection(db, collName);
            const docs = [];
            for (let i = 0; i < batchSize; i++) {
                docs.push({
                    [metaFieldName]: this.tid,
                    [timeFieldName]: getTime(),
                    data: Random.rand(),
                });
            }
            const res = coll.insertMany(docs, {ordered: false});
            assertAlways.commandWorked(res);
            assertAlways.eq(res.insertedIds.length, batchSize);
        },

        /**
         * Writers are not restricted to insert documents for their thread id. Insert a batch with
         * randomized thread ids to exercise the case where a batch insert results in writes to
         * several different buckets.
         */
        insertManyRandTid: function insertManyRandTid(db, collName) {
            const coll = this.getCollection(db, collName);
            const docs = [];
            for (let i = 0; i < batchSize; i++) {
                docs.push({
                    [metaFieldName]: Random.randInt(this.threadCount),
                    [timeFieldName]: getTime(),
                    data: Random.rand(),
                });
            }
            const res = coll.insertMany(docs, {ordered: false});
            assertAlways.commandWorked(res);
            assertAlways.eq(res.insertedIds.length, batchSize);
        },

        /**
         * Insert a batch for the current thread id but with older times that should be inserted in
         * different buckets.
         */
        insertManyOld: function insertManyOld(db, collName) {
            const coll = this.getCollection(db, collName);
            const docs = [];
            const start = getTime();
            for (let i = 0; i < batchSize; i++) {
                let time = new Date(start.getTime() - ((batchSize - i) * defaultBucketMaxRangeMs));
                docs.push({
                    [metaFieldName]: this.tid,
                    [timeFieldName]: time,
                    data: Random.rand(),
                });
            }
            const res = coll.insertMany(docs, {ordered: false});
            assertAlways.commandWorked(res);
            assertAlways.eq(res.insertedIds.length, batchSize);
        }
    };

    function setup(db, collName, cluster) {
        collName = this.getCollectionName(collName);
        assertAlways.commandWorked(db.createCollection(collName, {
            timeseries: {
                timeField: timeFieldName,
                metaField: metaFieldName,
            },
            expireAfterSeconds: ttlSeconds,
        }));
    }

    function teardown(db, collName, cluster) {
        // Default TTL monitor period
        const ttlMonitorSleepSecs = 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 another
        // period for it to wake up and delete the expired documents. We wait at least another
        // period just to avoid race-prone tests on overloaded test hosts.
        const timeoutMS =
            (TestData.inEvergreen ? 10 : 2) * Math.max(ttlMonitorSleepSecs, ttlSeconds) * 1000;

        print("Waiting for data to be deleted by TTL monitor");
        collName = this.getCollectionName(collName);
        assertAlways.soon(() => {
            return db[collName].find({first: true}).itcount() == 0;
        }, 'Expected oldest documents to be removed', timeoutMS);
    }

    const standardTransition = {
        insertOne: 0.4,
        insertManyOrdered: 0.1,
        insertManyUnordered: 0.1,
        insertManyRandTid: 0.1,
        insertManyOld: 0.1
    };

    const transitions = {
        init: standardTransition,
        insertOne: standardTransition,
        insertManyOrdered: standardTransition,
        insertManyUnordered: standardTransition,
        insertManyRandTid: standardTransition,
        insertManyOld: standardTransition,
    };

    return {
        threadCount: 20,
        iterations: 150,
        startState: 'init',
        states: states,
        data: initData,
        transitions: transitions,
        setup: setup,
        teardown: teardown
    };
})();