summaryrefslogtreecommitdiff
path: root/jstests/sharding/timeseries_sharding_admin_commands.js
blob: 0a0f0da2d0f2c8bc75ae68b45312d4eb95ac8c64 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
 * Test sharding admin commands on sharded time-series collection.
 *
 * @tags: [
 *   requires_fcv_51
 * ]
 */

(function() {
"use strict";

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

// Connections.
const mongo = new ShardingTest({shards: 2, rs: {nodes: 3}});

// Sanity checks.
if (!TimeseriesTest.timeseriesCollectionsEnabled(mongo.shard0)) {
    jsTestLog("Skipping test because the time-series collection feature flag is disabled");
    mongo.stop();
    return;
}

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

const dbName = 'testDB';
const collName = 'testColl';
const timeField = 'time';
const metaField = 'meta';
const viewNss = `${dbName}.${collName}`;
const bucketNss = `${dbName}.system.buckets.${collName}`;
const controlTimeField = `control.min.${timeField}`;
const numDocsInserted = 20;
const zone = 'Z';
assert.commandWorked(mongo.s0.adminCommand({enableSharding: dbName}));
assert.commandWorked(mongo.s0.adminCommand({addShardToZone: mongo.shard0.shardName, zone: zone}));

function createTimeSeriesColl({index, shardKey}) {
    const db = mongo.s0.getDB(dbName);
    assert.commandWorked(
        db.createCollection(collName, {timeseries: {timeField: timeField, metaField: metaField}}));
    assert.commandWorked(db[collName].createIndex(index));
    for (let i = 0; i < numDocsInserted; i++) {
        assert.commandWorked(db[collName].insert({[metaField]: i, [timeField]: ISODate()}));
    }
    if (shardKey) {
        assert.commandWorked(mongo.s0.adminCommand({
            shardCollection: viewNss,
            key: shardKey,
            timeseries: {timeField: timeField, metaField: metaField}
        }));
    }
}

function dropTimeSeriesColl() {
    const db = mongo.s0.getDB(dbName);
    const coll = db.getCollection(collName);
    assert(coll.drop());
}

// Check the zone range against the extended range saved in config.tags collection.
function assertRangeMatch(savedRange, paramRange) {
    // Non-extended range key must match.
    Object.keys(paramRange).forEach(key => {
        assert.docEq(savedRange[key], paramRange[key]);
    });
    // Extended range key must be MinKey.
    Object.keys(savedRange).forEach(key => {
        if (!paramRange.hasOwnProperty(key)) {
            assert.docEq(savedRange[key], MinKey);
        }
    });
}

// Check updateZoneKeyRange with range other than MinKey -> MinKey on the time field prevents
// sharding. The last successful call will shard the collection.
(function checkUpdateZoneKeyRangeCommandBeforeSharding() {
    function check(min, max, success) {
        createTimeSeriesColl({index: {[metaField]: 1, [timeField]: 1}});
        assert.commandWorked(
            mongo.s0.adminCommand({updateZoneKeyRange: bucketNss, min: min, max: max, zone: zone}));
        const tag = mongo.s0.getDB('config').tags.findOne({ns: bucketNss});
        assertRangeMatch(tag.min, min);
        assertRangeMatch(tag.max, max);
        const result = mongo.s0.adminCommand({
            shardCollection: viewNss,
            key: {[metaField]: 1, [timeField]: 1},
            timeseries: {timeField: timeField, metaField: metaField}
        });
        if (success) {
            assert.commandWorked(result);
        } else {
            assert.commandFailedWithCode(result, ErrorCodes.InvalidOptions);
        }
        assert.commandWorked(
            mongo.s0.adminCommand({updateZoneKeyRange: bucketNss, min: min, max: max, zone: null}));
        assert.eq(0, mongo.s0.getDB('config').tags.find({ns: bucketNss}).count());
        dropTimeSeriesColl();
    }
    check(
        {[metaField]: 1, [controlTimeField]: 1}, {[metaField]: 10, [controlTimeField]: 10}, false);
    check({[metaField]: 1, [controlTimeField]: 1},
          {[metaField]: 10, [controlTimeField]: MaxKey},
          false);
    check({[metaField]: 1, [controlTimeField]: MinKey},
          {[metaField]: 10, [controlTimeField]: 10},
          false);
    check({[metaField]: 1, [controlTimeField]: MinKey},
          {[metaField]: 10, [controlTimeField]: MaxKey},
          false);
    check({[metaField]: 1, [controlTimeField]: MinKey},
          {[metaField]: 10, [controlTimeField]: MinKey},
          true);
    check({[metaField]: 1}, {[metaField]: 10}, false);
})();

// Check updateZoneKeyRange rejects range other than MinKey -> MinKey on the time field after
// sharding.
(function checkUpdateZoneKeyRangeAfterSharding() {
    function check(min, max, success) {
        createTimeSeriesColl(
            {index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1, [timeField]: 1}});
        const result =
            mongo.s0.adminCommand({updateZoneKeyRange: bucketNss, min: min, max: max, zone: zone});
        if (success) {
            assert.commandWorked(result);
            const tag = mongo.s0.getDB('config').tags.findOne({ns: bucketNss});
            assertRangeMatch(tag.min, min);
            assertRangeMatch(tag.max, max);
            assert.commandWorked(mongo.s0.adminCommand(
                {updateZoneKeyRange: bucketNss, min: min, max: max, zone: null}));
            assert.eq(0, mongo.s0.getDB('config').tags.find({ns: bucketNss}).count());
        } else {
            assert.commandFailedWithCode(result, ErrorCodes.InvalidOptions);
        }
        dropTimeSeriesColl();
    }
    check(
        {[metaField]: 1, [controlTimeField]: 1}, {[metaField]: 10, [controlTimeField]: 10}, false);
    check({[metaField]: 1, [controlTimeField]: 1},
          {[metaField]: 10, [controlTimeField]: MaxKey},
          false);
    check({[metaField]: 1, [controlTimeField]: MinKey},
          {[metaField]: 10, [controlTimeField]: 10},
          false);
    check({[metaField]: 1, [controlTimeField]: MinKey},
          {[metaField]: 10, [controlTimeField]: MaxKey},
          false);
    check({[metaField]: 1, [controlTimeField]: MinKey},
          {[metaField]: 10, [controlTimeField]: MinKey},
          true);
    check({[metaField]: 1}, {[metaField]: 10}, true);
})();

// Check shardingState commands will return collection info in bucket namespace.
(function checkShardingStateCommand() {
    createTimeSeriesColl(
        {index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1, [timeField]: 1}});
    const shardingStateRes = mongo.getPrimaryShard(dbName).adminCommand({shardingState: 1});
    const shardingStateColls = Object.keys(shardingStateRes.versions);
    assert(shardingStateColls.includes(bucketNss) && !shardingStateColls.includes(viewNss));
    dropTimeSeriesColl();
})();

// Check reshardCollection commands are disabled for time-series collection.
(function checkReshardCollectionCommand() {
    createTimeSeriesColl({index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1}});
    // Command is not supported on the time-series view namespace.
    assert.commandFailedWithCode(
        mongo.s0.adminCommand(
            {reshardCollection: viewNss, key: {[metaField]: 1, [controlTimeField]: 1}}),
        ErrorCodes.NamespaceNotSharded);
    assert.commandFailedWithCode(
        mongo.s0.adminCommand(
            {reshardCollection: bucketNss, key: {[metaField]: 1, [controlTimeField]: 1}}),
        ErrorCodes.NotImplemented);
    dropTimeSeriesColl();
})();

// Check checkShardingIndex works for the correct key pattern on the bucket namespace,
// but not on the view namespace or in correct key pattern.
(function checkCheckShardingIndexCommand() {
    createTimeSeriesColl(
        {index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1, [timeField]: 1}});
    const primaryShard = mongo.getPrimaryShard(dbName);
    assert.commandWorked(primaryShard.getDB(dbName).runCommand(
        {checkShardingIndex: bucketNss, keyPattern: {[metaField]: 1, [controlTimeField]: 1}}));
    assert.commandFailedWithCode(
        primaryShard.getDB(dbName).runCommand(
            {checkShardingIndex: viewNss, keyPattern: {[metaField]: 1, [controlTimeField]: 1}}),
        ErrorCodes.CommandNotSupportedOnView);
    assert.commandFailed(primaryShard.getDB(dbName).runCommand(
        {checkShardingIndex: bucketNss, keyPattern: {[controlTimeField]: 1}}));
    dropTimeSeriesColl();
})();

// Check we can split/move/merge chunks between shards through bucket namespace but not view
// namespace.
(function checkSplitMoveMergeChunksCommand() {
    createTimeSeriesColl(
        {index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1, [timeField]: 1}});
    const primaryShard = mongo.getPrimaryShard(dbName);
    const otherShard = mongo.getOther(primaryShard);
    const minChunk = {[metaField]: MinKey, [controlTimeField]: MinKey};
    const splitChunk = {[metaField]: numDocsInserted / 2, [controlTimeField]: MinKey};
    const maxChunk = {[metaField]: MaxKey, [controlTimeField]: MaxKey};
    function checkChunkCount(expectedCounts) {
        const counts = mongo.chunkCounts(`system.buckets.${collName}`, dbName);
        assert.docEq(expectedCounts, counts);
    }
    // Command is not supported on the time-series view namespace.
    assert.commandFailedWithCode(mongo.s.adminCommand({split: viewNss, middle: splitChunk}),
                                 ErrorCodes.NamespaceNotSharded);
    assert.commandFailedWithCode(
        mongo.s.adminCommand(
            {moveChunk: viewNss, find: splitChunk, to: otherShard.name, _waitForDelete: true}),
        ErrorCodes.NamespaceNotSharded);
    assert.commandFailedWithCode(
        mongo.s.adminCommand({mergeChunks: viewNss, bounds: [minChunk, maxChunk]}),
        ErrorCodes.NamespaceNotSharded);

    assert.commandWorked(mongo.s.adminCommand({split: bucketNss, middle: splitChunk}));
    checkChunkCount({[primaryShard.shardName]: 2, [otherShard.shardName]: 0});
    assert.commandWorked(mongo.s.adminCommand(
        {moveChunk: bucketNss, find: splitChunk, to: otherShard.name, _waitForDelete: true}));
    checkChunkCount({[primaryShard.shardName]: 1, [otherShard.shardName]: 1});
    assert.commandWorked(mongo.s.adminCommand(
        {moveChunk: bucketNss, find: minChunk, to: otherShard.name, _waitForDelete: true}));
    checkChunkCount({[primaryShard.shardName]: 0, [otherShard.shardName]: 2});
    assert.commandWorked(
        mongo.s.adminCommand({mergeChunks: bucketNss, bounds: [minChunk, maxChunk]}));
    checkChunkCount({[primaryShard.shardName]: 0, [otherShard.shardName]: 1});
    dropTimeSeriesColl();
})();

// Can add control.min.time as the last shard key component on bucket namespace but not view
// namespace.
(function checkRefineCollectionShardKeyCommand() {
    createTimeSeriesColl({index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1}});
    assert.commandFailedWithCode(
        mongo.s0.adminCommand(
            {refineCollectionShardKey: viewNss, key: {[metaField]: 1, [controlTimeField]: 1}}),
        ErrorCodes.NamespaceNotSharded);
    assert.commandWorked(mongo.s0.adminCommand(
        {refineCollectionShardKey: bucketNss, key: {[metaField]: 1, [controlTimeField]: 1}}));
    const coll = mongo.s0.getDB(dbName)[collName];
    for (let i = 0; i < numDocsInserted; i++) {
        assert.commandWorked(coll.insert({[metaField]: i, [timeField]: ISODate()}));
    }
    assert.eq(numDocsInserted * 2, coll.find({}).count());
    dropTimeSeriesColl();
})();

// Check clearJumboFlag command can clear bucket chunk jumbo flag on bucket namespace but not view
// namespace.
(function checkClearJumboFlagCommand() {
    createTimeSeriesColl(
        {index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1, [timeField]: 1}});
    const configDB = mongo.s0.getDB('config');
    const collDoc = configDB.collections.findOne({_id: bucketNss});
    let chunkDoc = configDB.chunks.findOne({uuid: collDoc.uuid});
    configDB.chunks.update({_id: chunkDoc._id}, {$set: {jumbo: true}});
    chunkDoc = configDB.chunks.findOne({_id: chunkDoc._id});
    assert(chunkDoc.jumbo);
    assert.commandWorked(
        mongo.s.adminCommand({clearJumboFlag: bucketNss, bounds: [chunkDoc.min, chunkDoc.max]}));
    chunkDoc = configDB.chunks.findOne({_id: chunkDoc._id});
    assert(!chunkDoc.jumbo);
    // Command is not supported on the time-series view namespace.
    assert.commandFailedWithCode(
        mongo.s.adminCommand({clearJumboFlag: viewNss, bounds: [chunkDoc.min, chunkDoc.max]}),
        ErrorCodes.NamespaceNotSharded);
    dropTimeSeriesColl();
})();

// Check renameCollection command cannot modify name through the view namespace.
(function checkRenameCollectionCommand() {
    createTimeSeriesColl(
        {index: {[metaField]: 1, [timeField]: 1}, shardKey: {[metaField]: 1, [timeField]: 1}});
    const newCollName = `${collName}New`;
    const newViewNss = `${dbName}.${newCollName}`;
    // Rename collection is not supported through view namespace.
    assert.commandFailedWithCode(mongo.s.adminCommand({renameCollection: viewNss, to: newViewNss}),
                                 ErrorCodes.NamespaceNotFound);
    dropTimeSeriesColl();
})();

mongo.stop();
})();