summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/drop_sharded_timeseries_collection.js
blob: 7a411309b74fa4c5a772790c26a4b13ad354de38 (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
/**
 * Repeatedly creates and drops a sharded time-series collection.
 *
 * @tags: [
 *   requires_sharding,
 * ]
 */
'use strict';

const dbPrefix = 'fsm_db_for_sharded_timeseries_collection_';
const dbCount = 2;
const collPrefix = 'sharded_timeseries_collection_';
const collCount = 2;
const timeField = 'time';
const metaField = 'hostId';

load("jstests/core/timeseries/libs/timeseries.js");  // For 'TimeseriesTest' helpers.

function getRandomDb(db) {
    return db.getSiblingDB(dbPrefix + Random.randInt(dbCount));
}

function getRandomTimeseriesView(db) {
    return getRandomDb(db)[collPrefix + Random.randInt(collCount)];
}

var $config = (function() {
    const setup = function(db, collName, cluster) {
        // Check that necessary feature flags are enabled on each of the mongods.
        let isEnabled = true;
        cluster.executeOnMongodNodes(function(db) {
            if (!TimeseriesTest.timeseriesCollectionsEnabled(db) ||
                !TimeseriesTest.shardedtimeseriesCollectionsEnabled(db)) {
                isEnabled = false;
            }
        });
        this.isShardedTimeseriesEnabled = isEnabled;

        if (!this.isShardedTimeseriesEnabled) {
            jsTestLog(
                "Feature flags for sharded time-series collections are not enabled. This test will do nothing.");
            return;
        }

        // Enable sharding for the test databases.
        for (var i = 0; i < dbCount; i++) {
            const dbName = dbPrefix + i;
            db.adminCommand({enablesharding: dbName});
        }
    };

    const states = {
        init: function(db, collName) {},
        create: function(db, collName) {
            if (!this.isShardedTimeseriesEnabled) {
                return;
            }

            const coll = getRandomTimeseriesView(db);
            jsTestLog("Executing create state on: " + coll.getFullName());
            assertAlways.commandWorked(db.adminCommand({
                shardCollection: coll.getFullName(),
                key: {[metaField]: 1, [timeField]: 1},
                timeseries: {timeField: timeField, metaField: metaField}
            }));
        },
        dropView: function(db, collName) {
            if (!this.isShardedTimeseriesEnabled) {
                return;
            }

            const coll = getRandomTimeseriesView(db);
            jsTestLog("Executing dropView state on: " + coll.getFullName());
            assertAlways.commandWorked(coll.getDB().runCommand({drop: coll.getName()}));
        },
    };

    const transitions = {
        init: {create: 0.33, dropView: 0.33},
        create: {create: 0.33, dropView: 0.33},
        dropView: {create: 0.33, dropView: 0.33},
    };

    return {
        threadCount: 12,
        iterations: 64,
        startState: 'init',
        data: {},
        states: states,
        setup: setup,
        transitions: transitions
    };
})();