summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/refine_collection_shard_key_zone_ops.js
blob: 09d7fb8f32257d664c25a35550c847c2935a5c3f (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';

/**
 * Runs refineCollectionShardKey and zone operations concurrently.
 *
 * States:
 *  - sendZoneToOtherShard: Picks a random zone assigned to this thread, removes it from the
 *    current shard, and assigns it to the other shard. Verifies via querying the config server
 *    that the first shard no longer has said zone, and that the second shard now has said zone.
 *
 *  - swapZoneRange: Removes the ranges from each of the zones assigned to this thread, and swaps
 *    them, such that each range is now assigned to the opposite zone. Verifies that the zones
 *    have swapped ranges by querying the config server.
 *
 *  - refineCollectionShardKey - Refines the collection's shard key and decreases the latch count
 *    such that the next latch collection will be targeted by the test.
 *
 * @tags: [requires_persistence, requires_sharding, requires_fcv_44]
 */

load('jstests/libs/parallelTester.js');

var $config = (function() {
    var data = {
        oldShardKeyField: 'a',
        newShardKeyField: ['a', 'b'],
        oldShardKey: {a: 1},
        newShardKey: {a: 1, b: 1},
        docCount: 100,
        shardNames: [],
        zonesMappedToShardsForCollection: {},
        zonesMappedToRangesOwnedByThreadForCollection: {},
    };

    function getCurrentLatchCollName(collName, latch) {
        return collName + '_' + latch.getCount().toString();
    }

    function getCurrentOrPreviousLatchCollName(collName, latch, latchCount) {
        const latchNumber =
            (Math.random() < 0.5) ? latch.getCount() : Math.min(latch.getCount() + 1, latchCount);

        return collName + '_' + latchNumber.toString();
    }

    function getConfigTagsCollection(db) {
        return db.getSiblingDB('config').tags;
    }

    function getConfigShardsCollection(db) {
        return db.getSiblingDB('config').shards;
    }

    function populateTagRangesForThreadFromConfig(
        db, collName, threadId, currentZoneRangeMapForCollection) {
        const threadZoneRegexMatch = '.*tid-' + threadId + '.*';
        const tags = getConfigTagsCollection(db)
                         .find({ns: db + '.' + collName, tag: {$regex: threadZoneRegexMatch}})
                         .toArray();
        assertAlways.eq(2, tags.length);
        tags.forEach((tag) => {
            currentZoneRangeMapForCollection[tag.tag] = {'min': tag.min, 'max': tag.max};
        });
    }

    function initMapsForThread(db,
                               collName,
                               threadId,
                               shardNames,
                               zonesMappedToShardsForCollection,
                               zonesMappedToRangesOwnedByThreadForCollection) {
        const threadZoneStringMatch = 'tid-' + threadId;

        if (!zonesMappedToShardsForCollection[collName]) {
            zonesMappedToShardsForCollection[collName] = {};
        }
        let currentZoneShardMap = zonesMappedToShardsForCollection[collName];

        const shards = getConfigShardsCollection(db).find({});
        shards.forEach((shard) => {
            if (!shardNames.includes(shard._id)) {
                shardNames.push(shard._id);
            }

            shard.tags.forEach((tag) => {
                if (!tag.includes(threadZoneStringMatch)) {
                    return;
                }
                currentZoneShardMap[tag] = shard._id;
            });
        });

        if (!zonesMappedToRangesOwnedByThreadForCollection[collName]) {
            zonesMappedToRangesOwnedByThreadForCollection[collName] = {};
        }

        let currentZoneRangeMap = zonesMappedToRangesOwnedByThreadForCollection[collName];
        populateTagRangesForThreadFromConfig(db, collName, threadId, currentZoneRangeMap);
    }

    function attemptSwapZoneRange(db, collName, zonesMappedToRangesOwnedByThreadForCollection) {
        const fullCollName = db + '.' + collName;

        // Assume that we only have two zones owned by the thread for a given collection.
        const zoneKeys = Object.keys(zonesMappedToRangesOwnedByThreadForCollection);
        assertAlways.eq(2, zoneKeys.length);

        const firstZoneName = zoneKeys[0];
        const firstZoneRange = zonesMappedToRangesOwnedByThreadForCollection[zoneKeys[0]];
        const secondZoneName = zoneKeys[1];
        const secondZoneRange = zonesMappedToRangesOwnedByThreadForCollection[zoneKeys[1]];

        // Swap the zone ranges by first setting both to null, then reversing the values such that
        // the first zone's range will now be associated with the second zone, and vice versa.
        assertAlways.commandWorked(db.adminCommand({
            updateZoneKeyRange: fullCollName,
            min: firstZoneRange.min,
            max: firstZoneRange.max,
            zone: null
        }));

        assertAlways.commandWorked(db.adminCommand({
            updateZoneKeyRange: fullCollName,
            min: secondZoneRange.min,
            max: secondZoneRange.max,
            zone: null
        }));

        assertAlways.commandWorked(db.adminCommand({
            updateZoneKeyRange: fullCollName,
            min: secondZoneRange.min,
            max: secondZoneRange.max,
            zone: firstZoneName
        }));
        assertAlways.commandWorked(db.adminCommand({
            updateZoneKeyRange: fullCollName,
            min: firstZoneRange.min,
            max: firstZoneRange.max,
            zone: secondZoneName
        }));

        // Verify that the commands set the correct data on the config server.
        const firstTagRangeOnConfig = getConfigTagsCollection(db).findOne({tag: firstZoneName});
        const secondTagRangeOnConfig = getConfigTagsCollection(db).findOne({tag: secondZoneName});

        assertAlways.eq(secondZoneRange.min, firstTagRangeOnConfig.min);
        assertAlways.eq(secondZoneRange.max, firstTagRangeOnConfig.max);
        assertAlways.eq(firstZoneRange.min, secondTagRangeOnConfig.min);
        assertAlways.eq(firstZoneRange.max, secondTagRangeOnConfig.max);

        zonesMappedToRangesOwnedByThreadForCollection[firstZoneName] = secondZoneRange;
        zonesMappedToRangesOwnedByThreadForCollection[secondZoneName] = firstZoneRange;
    }

    const states = {
        init: function init(db, collName, connCache) {
            for (let i = this.latchCount; i >= 0; --i) {
                initMapsForThread(db,
                                  collName + '_' + i,
                                  this.tid,
                                  this.shardNames,
                                  this.zonesMappedToShardsForCollection,
                                  this.zonesMappedToRangesOwnedByThreadForCollection);
            }
        },

        sendZoneToOtherShard: function sendZoneToOtherShard(db, collName, connCache) {
            const configShardsCollection = getConfigShardsCollection(db);

            const latchCollName =
                getCurrentOrPreviousLatchCollName(collName, this.latch, this.latchCount);
            let currentZoneShardMap = this.zonesMappedToShardsForCollection[latchCollName];

            const zoneKeys = Object.keys(currentZoneShardMap);
            const randomZone = zoneKeys[Random.randInt(zoneKeys.length)];
            const formerShardForZone = currentZoneShardMap[randomZone];

            // We assume here that we only have two shards.
            let newShardForZone = this.shardNames.filter((shard) => {
                if (shard !== formerShardForZone) {
                    return shard;
                }
            })[0];

            // Move the zone to the other shard.
            assertAlways.commandWorked(
                db.adminCommand({addShardToZone: newShardForZone, zone: randomZone}));
            assertAlways.commandWorked(
                db.adminCommand({removeShardFromZone: formerShardForZone, zone: randomZone}));

            // Verify that the zone exists only on the new shard.
            const tagsForFormerShard =
                configShardsCollection.findOne({_id: formerShardForZone}).tags;
            const tagsForNewShard = configShardsCollection.findOne({_id: newShardForZone}).tags;

            assertAlways.eq(false, tagsForFormerShard.includes(randomZone));
            assertAlways.eq(true, tagsForNewShard.includes(randomZone));

            currentZoneShardMap[randomZone] = newShardForZone;
        },

        swapZoneRange: function swapZoneRange(db, collName, connCache) {
            const latchCollName =
                getCurrentOrPreviousLatchCollName(collName, this.latch, this.latchCount);

            let currentZoneRangeMap =
                this.zonesMappedToRangesOwnedByThreadForCollection[latchCollName];

            try {
                attemptSwapZoneRange(db, latchCollName, currentZoneRangeMap);
            } catch (e) {
                // During the process of attempting to swap the zone range, the collection may
                // become refined. Retrying swapping the zone range will allow us to target the
                // shard key in its refined state.
                if (e.message.includes('"b"') && e.message.includes('are not equal')) {
                    jsTestLog("Retrying swapZoneRange on collection " + latchCollName +
                              " due to refineCollectionShardKey conflict");
                    for (let zoneRange of Object.values(currentZoneRangeMap)) {
                        zoneRange.min.b = MinKey;
                        zoneRange.max.b = MinKey;
                    }
                    // Try swapping the zone range only once more.
                    attemptSwapZoneRange(db, latchCollName, currentZoneRangeMap);
                } else {
                    throw e;
                }
            }
        },

        refineCollectionShardKey: function refineCollectionShardKey(db, collName, connCache) {
            const latchCollName = getCurrentLatchCollName(collName, this.latch);
            const latchColl = db.getCollection(latchCollName);

            try {
                assertAlways.commandWorked(db.adminCommand(
                    {refineCollectionShardKey: latchColl.getFullName(), key: this.newShardKey}));
            } catch (e) {
                // There is a race that could occur where two threads run refineCollectionShardKey
                // concurrently on the same collection. Since the epoch of the collection changes,
                // the later thread may receive a StaleEpoch error, which is an acceptable error.
                if (e.code == ErrorCodes.StaleEpoch) {
                    print("Ignoring acceptable refineCollectionShardKey error: " + tojson(e));
                    return;
                }
                throw e;
            }

            this.latch.countDown();
        }
    };

    const transitions = {
        init: {sendZoneToOtherShard: 0.4, swapZoneRange: 0.4, refineCollectionShardKey: 0.2},
        sendZoneToOtherShard:
            {sendZoneToOtherShard: 0.4, swapZoneRange: 0.4, refineCollectionShardKey: 0.2},
        swapZoneRange:
            {sendZoneToOtherShard: 0.4, swapZoneRange: 0.4, refineCollectionShardKey: 0.2},
        refineCollectionShardKey:
            {sendZoneToOtherShard: 0.4, swapZoneRange: 0.4, refineCollectionShardKey: 0.2},
    };

    function setup(db, collName, cluster) {
        // Use a CountDownLatch as if it were a std::atomic<long long> shared between all of the
        // threads. The collection name is suffixed with the current this.latch.getCount() value
        // when concurrent operations are run against it. With every refineCollectionShardKey, call
        // this.latch.countDown() and run CRUD operations against the new collection suffixed with
        // this.latch.getCount(). This bypasses the need to drop and reshard the current collection
        // with every refineCollectionShardKey since it cannot be achieved in an atomic fashion
        // fashion under the FSM infrastructure (meaning operations could fail).
        this.latchCount = this.iterations;
        this.latch = new CountDownLatch(this.latchCount);
        this.zoneAndRangeCount = this.threadCount * 2;
        this.partitionSize = this.docCount / this.zoneAndRangeCount;

        const shardNames = Object.keys(cluster.getSerializedCluster().shards);

        // Proactively create and shard all possible collections suffixed with this.latch.getCount()
        // that could receive CRUD operations over the course of the FSM workload. This prevents the
        // race that could occur between sharding a collection and creating an index on the new
        // shard key (if this step were done after every refineCollectionShardKey).
        for (let i = this.latchCount; i >= 0; --i) {
            const latchCollName = collName + '_' + i;
            const latchColl = db.getCollection(latchCollName);

            let currentRangeLowerBound = 0;
            for (let j = 0; j < this.zoneAndRangeCount; ++j) {
                // Create a name for the zone that guarantees that the zone will never be touched
                // by other threads for this test.
                const currentThread = Math.floor(j / 2);
                const zoneName = latchCollName + '-tid-' + currentThread + '-' + j % 2;

                // Add the zone to one random shard.
                const randomShard = shardNames[Random.randInt(shardNames.length)];
                assertAlways.commandWorked(
                    db.adminCommand({addShardToZone: randomShard, zone: zoneName}));

                // Assign a range to the zone.
                const lowerZoneRange = {[this.oldShardKeyField]: currentRangeLowerBound};
                const uppperZoneRange =
                    {[this.oldShardKeyField]: currentRangeLowerBound + this.partitionSize};
                assertAlways.commandWorked(db.adminCommand({
                    updateZoneKeyRange: latchColl.getFullName(),
                    min: lowerZoneRange,
                    max: uppperZoneRange,
                    zone: zoneName
                }));

                currentRangeLowerBound += this.partitionSize;
            }

            // Shard the collection, implicitly creating chunks to match the created zones.
            assertAlways.commandWorked(
                db.adminCommand({shardCollection: latchColl.getFullName(), key: this.oldShardKey}));
            assertAlways.commandWorked(latchColl.createIndex(this.newShardKey));

            db.printShardingStatus();
        }
    }

    return {
        threadCount: 5,
        iterations: 25,
        data: data,
        startState: 'init',
        states: states,
        transitions: transitions,
        setup: setup,
        passConnectionCache: true,
    };
})();