summaryrefslogtreecommitdiff
path: root/jstests/sharding/timeseries_coll_mod_bucketing_parameters.js
blob: 93806f782789ee7cef6f93d0582af917bff2bb44 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
 * Test $collMod command on a sharded timeseries collection. This test specifically targets the
 * manipulation of the bucketing parameters which are made up of: granularity, bucketMaxSpanSeconds,
 * and bucketRoundingSeconds.
 *
 * @tags: [
 *   requires_fcv_60,
 * ]
 */

(function() {
"use strict";

load("jstests/core/timeseries/libs/timeseries.js");
load("jstests/libs/fail_point_util.js");
load("jstests/libs/parallel_shell_helpers.js");

const dbName = 'testDB';
const collName = 'testColl';
const timeField = 'tm';
const metaField = 'mt';
const indexName = 'index';
const viewNss = `${dbName}.${collName}`;
const bucketNss = `${dbName}.system.buckets.${collName}`;
const controlTimeField = `control.min.${timeField}`;

const getConfigGranularity = function(db) {
    return db.getSiblingDB('config')
        .collections.findOne({_id: bucketNss})
        .timeseriesFields.granularity;
};
const getConfigMaxSpanSeconds = function(db) {
    return db.getSiblingDB('config')
        .collections.findOne({_id: bucketNss})
        .timeseriesFields.bucketMaxSpanSeconds;
};
const getConfigRoundingSeconds = function(db) {
    return db.getSiblingDB('config')
        .collections.findOne({_id: bucketNss})
        .timeseriesFields.bucketRoundingSeconds;
};

const checkConfigParametersAfterCollMod = function() {
    jsTestLog("Entering checkConfigParametersAfterCollMod...");

    const st = new ShardingTest({shards: 1, rs: {nodes: 1}});
    const mongos = st.s0;
    const db = mongos.getDB(dbName);

    if (!TimeseriesTest.shardedtimeseriesCollectionsEnabled(st.shard0)) {
        jsTestLog(
            "Skipping test because the sharded time-series collection feature flag is disabled");
        st.stop();
        return;
    }

    if (!TimeseriesTest.timeseriesScalabilityImprovementsEnabled(st.shard0)) {
        jsTestLog("Skipping test because the timeseries scalability feature flag is disabled");
        st.stop();
        return;
    }

    // Create and shard the time-series collection.
    assert.commandWorked(
        db.createCollection(collName, {timeseries: {timeField: timeField, metaField: metaField}}));
    assert.commandWorked(mongos.adminCommand({enableSharding: dbName}));
    assert.commandWorked(mongos.adminCommand({
        shardCollection: viewNss,
        key: {[metaField]: 1},
    }));

    // The default granularity for a time-series collection is 'seconds', so verify that alligns
    // with the information on the config server.
    let currentMaxSpan = TimeseriesTest.getBucketMaxSpanSecondsFromGranularity('seconds');
    assert.eq(getConfigGranularity(db), 'seconds');
    assert.eq(getConfigMaxSpanSeconds(db), currentMaxSpan);
    assert.isnull(getConfigRoundingSeconds(db));

    // 1. Modify the bucketMaxSpanSeconds and bucketRoundingSeconds to the maxSpanSeconds for
    // seconds. We expect this to succeed and we should see that the granularity is null and the new
    // values should be present on the config server.
    assert.commandWorked(db.runCommand({
        collMod: collName,
        timeseries: {bucketMaxSpanSeconds: currentMaxSpan, bucketRoundingSeconds: currentMaxSpan}
    }));
    assert.isnull(getConfigGranularity(db));
    assert.eq(getConfigMaxSpanSeconds(db), currentMaxSpan);
    assert.eq(getConfigRoundingSeconds(db), currentMaxSpan);

    // 2. Change the granularity to 'minutes'. We expect this to succeed and the values on the
    // config server should reflect the values which correspond to the 'minutes' granularity.
    // Note: when specifying a granularity, the bucketRoundingSeconds parameter should NOT be
    // present on the config server (it is calculated based off of the granularity).
    currentMaxSpan = TimeseriesTest.getBucketMaxSpanSecondsFromGranularity('minutes');
    assert.commandWorked(db.runCommand({collMod: collName, timeseries: {granularity: 'minutes'}}));
    assert.eq(getConfigGranularity(db), 'minutes');
    assert.eq(getConfigMaxSpanSeconds(db), currentMaxSpan);
    assert.isnull(getConfigRoundingSeconds(db));

    // 3. Modify the bucketMaxSpanSeconds and bucketRoundingSeconds to a custom value and check the
    // config server for consistency.
    currentMaxSpan = TimeseriesTest.getBucketMaxSpanSecondsFromGranularity('minutes');
    assert.commandWorked(db.runCommand({
        collMod: collName,
        timeseries: {bucketMaxSpanSeconds: currentMaxSpan, bucketRoundingSeconds: currentMaxSpan}
    }));
    assert.isnull(getConfigGranularity(db));
    assert.eq(getConfigMaxSpanSeconds(db), currentMaxSpan);
    assert.eq(getConfigRoundingSeconds(db), currentMaxSpan);

    // 4. Change the granularity to 'hours' and check the config server for consistency.
    currentMaxSpan = TimeseriesTest.getBucketMaxSpanSecondsFromGranularity('hours');
    assert.commandWorked(db.runCommand({collMod: collName, timeseries: {granularity: 'hours'}}));
    assert.eq(getConfigGranularity(db), 'hours');
    assert.eq(getConfigMaxSpanSeconds(db), currentMaxSpan);
    assert.isnull(getConfigRoundingSeconds(db));

    // 5. We expect changing the granularity to a value lower than 'hours' will fail.
    assert.commandFailedWithCode(
        db.runCommand({collMod: collName, timeseries: {granularity: 'minutes'}}),
        ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(
        db.runCommand({collMod: collName, timeseries: {granularity: 'seconds'}}),
        ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(db.runCommand({
        collMod: collName,
        timeseries:
            {bucketMaxSpanSeconds: currentMaxSpan - 1, bucketRoundingSeconds: currentMaxSpan - 1}
    }),
                                 ErrorCodes.InvalidOptions);
    // Make sure that the bucketing parameters are unchanged.
    assert.eq(getConfigGranularity(db), 'hours');
    assert.eq(getConfigMaxSpanSeconds(db), currentMaxSpan);
    assert.isnull(getConfigRoundingSeconds(db));

    // 6. Modify the bucketMaxSpanSeconds and bucketRoundingSeconds to a custom value and check the
    // config server for consistency.
    assert.commandWorked(db.runCommand({
        collMod: collName,
        timeseries: {bucketMaxSpanSeconds: currentMaxSpan, bucketRoundingSeconds: currentMaxSpan}
    }));
    assert.isnull(getConfigGranularity(db));
    assert.eq(getConfigMaxSpanSeconds(db), currentMaxSpan);
    assert.eq(getConfigRoundingSeconds(db), currentMaxSpan);

    st.stop();
    jsTestLog("Exiting checkConfigParametersAfterCollMod.");
};

const checkShardRoutingAfterCollMod = function() {
    jsTestLog("Entering checkShardRoutingAfterCollMod...");

    const st = new ShardingTest({shards: 2, rs: {nodes: 2}, mongos: 2});
    const mongos0 = st.s0;
    const mongos1 = st.s1;
    const shard0 = st.shard0;
    const shard1 = st.shard1;
    const db = mongos0.getDB(dbName);

    if (!TimeseriesTest.timeseriesScalabilityImprovementsEnabled(mongos0)) {
        jsTestLog(
            "Skipped test as the featureFlagTimeseriesScalabilityImprovements feature flag is not enabled.");
        st.stop();
        return;
    }

    // Create and shard a time-series collection using custom bucketing parameters.
    assert.commandWorked(db.createCollection(collName, {
        timeseries: {
            timeField: timeField,
            metaField: metaField,
            bucketMaxSpanSeconds: 60,
            bucketRoundingSeconds: 60
        }
    }));
    st.ensurePrimaryShard(db.getName(), shard0.shardName);
    assert.commandWorked(mongos0.adminCommand({enableSharding: dbName}));
    assert.commandWorked(mongos0.adminCommand({
        shardCollection: viewNss,
        key: {[timeField]: 1},
    }));

    // Minkey --- 2022-01-01 09:00:00 --- MaxKey
    //       shard0                  shard1
    const splitChunk = {[controlTimeField]: ISODate('2022-01-01 09:00:00')};
    assert.commandWorked(mongos0.adminCommand({split: bucketNss, middle: splitChunk}));
    assert.commandWorked(mongos0.adminCommand(
        {moveChunk: bucketNss, find: splitChunk, to: shard1.name, _waitForDelete: true}));

    function assertDocumentOnShard(shard, _id) {
        const buckets =
            shard.getDB(dbName).getCollection(`system.buckets.${collName}`).find().toArray();
        const _ids = [];
        buckets.forEach(bucket => {
            for (let key in bucket.data._id) {
                _ids.push(bucket.data._id[key]);
            }
        });
        assert(_ids.some(x => x === _id));
    }

    // Based on a bucketMaxSpan of 60, the time document will be routed to shard1 through mongos0.
    const time = ISODate('2022-01-01 10:30:50');
    assert.commandWorked(
        mongos0.getDB(dbName).getCollection(collName).insert({_id: 1, [timeField]: time}));
    assertDocumentOnShard(shard1, 1);

    const failPoint = configureFailPoint(shard0.getDB(dbName), "collModBeforeConfigServerUpdate");
    const parallelGranularityUpdate = startParallelShell(
        funWithArgs(function(dbName, collName) {
            assert.commandWorked(db.getSiblingDB(dbName).runCommand({
                collMod: collName,
                timeseries: {bucketMaxSpanSeconds: 86400, bucketRoundingSeconds: 86400}
            }));
        }, dbName, collName), mongos0.port);

    failPoint.wait();

    // While the collMod command on the config server is still being processed, inserts on the
    // collection should be blocked.
    assert.commandFailedWithCode(
        mongos0.getDB(dbName).runCommand(
            {insert: collName, documents: [{[timeField]: ISODate()}], maxTimeMS: 2000}),
        ErrorCodes.MaxTimeMSExpired);
    assert.commandFailedWithCode(
        mongos0.getDB(dbName).runCommand({find: collName, maxTimeMS: 2000}),
        ErrorCodes.MaxTimeMSExpired);

    failPoint.off();
    parallelGranularityUpdate();

    // Based on a bucketMaxSpan of 86400, the time document will be routed to shard1 through
    // mongos0.
    assert.commandWorked(
        mongos0.getDB(dbName).getCollection(collName).insert({_id: 2, [timeField]: time}));
    assertDocumentOnShard(shard0, 2);

    // Assert that a collection with a bucketMaxSpan of 86400 will find both documents through
    // mongos1.
    assert.eq(mongos1.getDB(dbName).getCollection(collName).countDocuments({[timeField]: time}), 2);
    assert.eq(0, st.config.collections.countDocuments({allowMigrations: {$exists: true}}));

    st.stop();
    jsTestLog("Exiting checkShardRoutingAfterCollMod.");
};

checkConfigParametersAfterCollMod();

checkShardRoutingAfterCollMod();
})();