summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/clustered_coll_mod.js
blob: 997b452436dabae7069e656471495594f2dd92df (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
/**
 * Test collMod command on a clustered collection.
 *
 * @tags: [
 *   requires_fcv_53,
 * ]
 */

(function() {
"use strict";

load("jstests/libs/clustered_collections/clustered_collection_util.js");
load("jstests/libs/ttl_util.js");

// Run TTL monitor constantly to speed up this test.
const conn = MongoRunner.runMongod({setParameter: 'ttlMonitorSleepSecs=1'});

function testCollMod(coll, clusterKey, clusterKeyName) {
    const collName = coll.getName();
    const clusterKeyFieldName = Object.keys(clusterKey)[0];
    // Set the original expireAfterSeconds to a day.
    const expireAfterSeconds = 60 * 60 * 24;

    assertDropCollection(coll.getDB(), collName);
    assert.commandWorked(coll.getDB().createCollection(
        coll.getName(), {clusteredIndex: {key: clusterKey, unique: true}, expireAfterSeconds}));

    // Insert documents less than a day old so they don't automatically expire.
    const batchSize = 10;
    const now = new Date();
    let docs = [];
    for (let i = 0; i < batchSize; i++) {
        // Make them 5 minutes expired.
        const fiveMinutesPastMS = 5 * 60 * 1000;
        const recentDate = new Date(now - fiveMinutesPastMS - i);
        docs.push({
            [clusterKeyFieldName]: recentDate,
            info: "unexpired",
        });
    }
    assert.commandWorked(coll.insertMany(docs, {ordered: false}));
    assert.eq(coll.find().itcount(), batchSize);

    TTLUtil.waitForPass(coll.getDB());
    assert.eq(coll.find().itcount(), batchSize);

    // Shorten the expireAfterSeconds so all the documents in the collection are expired.
    assert.commandWorked(coll.getDB().runCommand({collMod: collName, expireAfterSeconds: 1}));

    TTLUtil.waitForPass(coll.getDB());

    // Confirm all documents were deleted once the expireAfterSeconds was shortened.
    assert.eq(coll.find().itcount(), 0);

    // Turn TTL off.
    assert.commandWorked(coll.getDB().runCommand({collMod: collName, expireAfterSeconds: "off"}));

    // Ensure there is no outstanding TTL pass in progress that will still remove entries.
    TTLUtil.waitForPass(coll.getDB());

    assert.commandWorked(coll.insert({[clusterKeyFieldName]: now, info: "unexpired"}));

    TTLUtil.waitForPass(coll.getDB());

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

    assert.commandFailedWithCode(
        coll.getDB().runCommand(
            {collMod: collName, index: {keyPattern: {[clusterKeyFieldName]: 1}, hidden: true}}),
        6011800);

    assert.commandFailedWithCode(
        coll.getDB().runCommand({collMod: collName, index: {name: clusterKeyName, hidden: true}}),
        6011800);
}

testCollMod(conn.getDB(jsTestName())["coll"], {_id: 1}, "_id_");

MongoRunner.stopMongod(conn);
})();