summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/ttl_batch_deletes.js
blob: c91293bb6d9d056fd42d51bd76879f677edab718 (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
/**
 * Verify the behavior of TTL batched deletes and ttlMonitorBatchDeletes parameter.
 *
 * @tags: [
 *   requires_replication,
 *   requires_sharding,
 *   requires_timeseries,
 *   uses_ttl,
 * ]
 */
(function() {
'use strict';
load("jstests/libs/clustered_collections/clustered_collection_util.js");
load('jstests/replsets/rslib.js');
load("jstests/libs/fixture_helpers.js");  // For mapOnEachShardNode().

const docCount = 50;

const runTestCase = function(fn, isSharded = false) {
    if (!isSharded) {
        const replTest = new ReplSetTest({
            nodes: 1,
            nodeOptions: {setParameter: {ttlMonitorSleepSecs: 1}},
        });
        replTest.startSet();
        replTest.initiate();

        fn(replTest.getPrimary());

        replTest.stopSet();
    } else {
        const st = new ShardingTest({
            shards: 1,
            mongos: 1,
            config: 1,
            other: {
                shardOptions: {setParameter: {ttlMonitorSleepSecs: 1}},
            }
        });
        const conn = st.s0;

        fn(conn);

        st.stop();
    }
};

const disableTTLBatchDeletes = function(conn) {
    // Disable TTL batch deletion.
    assert.commandWorked(
        conn.getDB('admin').runCommand({setParameter: 1, ttlMonitorBatchDeletes: false}));
};

const triggerIndexScanTTL = function(db, doShardCollection = false) {
    const coll = db.ttl_coll;
    coll.drop();

    if (doShardCollection) {
        assert.commandWorked(db.adminCommand({enableSharding: db.getName()}));
        assert.commandWorked(
            db.adminCommand({shardCollection: coll.getFullName(), key: {_id: 'hashed'}}));
    }

    // Insert 50 docs with timestamp 'now - 24h'.
    const now = (new Date()).getTime();
    const past = new Date(now - (3600 * 1000 * 24));
    for (let i = 0; i < docCount; i++) {
        assert.commandWorked(db.runCommand({insert: 'ttl_coll', documents: [{x: past}]}));
    }

    assert.eq(coll.find().itcount(), docCount);

    // Create TTL index: expire docs older than 20000 seconds (~5.5h).
    coll.createIndex({x: 1}, {expireAfterSeconds: 20000});

    assert.soon(function() {
        return coll.find().itcount() == 0;
    }, 'TTL index on x didn\'t delete');
};

const testTTLDeleteWithIndexScanBatched = function(conn) {
    const db = conn.getDB('test');
    triggerIndexScanTTL(db);

    // Verify batchedDeletes status to verify ttl deletions have been batched.
    assert.eq(db.serverStatus()["batchedDeletes"]["docs"], docCount);
};

const testTTLDeleteWithIndexScanDocByDoc = function(conn) {
    const db = conn.getDB('test');

    disableTTLBatchDeletes(conn);

    triggerIndexScanTTL(db);

    // Verify batchedDeletes status to verify ttl deletions batch doc count is 0.
    assert.eq(db.serverStatus()["batchedDeletes"]["docs"], 0);
};

const triggerCollectionScanTTL = function(testDB, doShardCollection = false) {
    const timeFieldName = 'time';
    const metaFieldName = 'host';
    const expireAfterSeconds = 5;
    // Default maximum range of time for a bucket.
    const defaultBucketMaxRange = 3600;

    const coll = testDB.getCollection('ts');
    const bucketsColl = testDB.getCollection('system.buckets.' + coll.getName());
    assert.commandWorked(testDB.createCollection(coll.getName(), {
        timeseries: {
            timeField: timeFieldName,
            metaField: metaFieldName,
        },
        expireAfterSeconds: expireAfterSeconds,
    }));

    if (doShardCollection) {
        assert.commandWorked(
            testDB.adminCommand({shardCollection: coll.getFullName(), key: {[metaFieldName]: 1}}));
    }

    const maxTime = new Date((new Date()).getTime() - (1000 * defaultBucketMaxRange));
    const minTime = new Date(maxTime.getTime() - (1000 * 5 * 60));
    for (let i = 0; i < docCount; i++) {
        const time = new Date(minTime.getTime() + i);
        assert.commandWorked(coll.insert({[timeFieldName]: time, [metaFieldName]: "localhost"}));
    }

    assert.soon(function() {
        return coll.find().itcount() == 0;
    }, 'TTL index on the cluster key didn\'t delete');

    assert.eq(0, coll.find().itcount());
    assert.eq(0, bucketsColl.find().itcount());
};

const testTTLDeleteWithCollectionScanBatched = function(conn) {
    const testDB = conn.getDB('test');

    triggerCollectionScanTTL(testDB);

    // For time series the "docs" count is related to buckets instead of documents. Check that the
    // number of batches is greater than 0 instead.
    assert.gt(testDB.serverStatus()["batchedDeletes"]["batches"], 0);
};

const testTTLDeleteWithCollectionScanDocByDoc = function(conn) {
    const testDB = conn.getDB('test');

    disableTTLBatchDeletes(conn);

    triggerCollectionScanTTL(testDB);

    assert.eq(testDB.serverStatus()["batchedDeletes"]["batches"], 0);
};

const verifyTTLOnShardedCluster = function(conn, clustered = false) {
    const db = conn.getDB('test');
    if (clustered) {
        triggerCollectionScanTTL(db, true /* doShardCollection */);
    } else {
        triggerIndexScanTTL(db, true /* doShardCollection */);
    }

    // Verify batchedDeletes status to verify ttl deletions have been batched.
    var sum = 0;
    FixtureHelpers.mapOnEachShardNode({
        db: db,
        func: (db) => {
            sum += db.serverStatus()["batchedDeletes"]["docs"];
        }
    });

    if (clustered) {
        // For time series the "docs" count is related to buckets instead of documents. Check that
        // the number of batches is greater than 0 instead.
        assert.gte(sum, 0);
    } else {
        assert.eq(sum, docCount);
    }
};

const testTTLDeleteWithIndexScanBatchedOnShardedCollection = function(conn) {
    verifyTTLOnShardedCluster(conn, false);
};

const testTTLDeleteWithCollectionScanBatchedOnShardedCollection = function(conn) {
    verifyTTLOnShardedCluster(conn, true);
};

runTestCase(testTTLDeleteWithIndexScanBatched);
runTestCase(testTTLDeleteWithIndexScanDocByDoc);
runTestCase(testTTLDeleteWithCollectionScanBatched);
runTestCase(testTTLDeleteWithCollectionScanDocByDoc);
runTestCase(testTTLDeleteWithIndexScanBatchedOnShardedCollection, true);
runTestCase(testTTLDeleteWithCollectionScanBatchedOnShardedCollection, true);
})();