summaryrefslogtreecommitdiff
path: root/jstests/sharding/resharding_large_number_of_initial_chunks.js
blob: 798131c0fb814277408f10d4ffabbeaec9c73ebc (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
/**
 * Tests that resharding can complete successfully when the original collection has a large number
 * of chunks.
 *
 * @tags: [
 *   uses_atclustertime,
 *   resource_intensive,
 *   exclude_from_large_txns
 * ]
 */
(function() {
"use strict";

load("jstests/sharding/libs/find_chunks_util.js");

const st = new ShardingTest({
    mongos: 1,
    shards: 2,
    config: 1,
    other: {
        configOptions: {
            setParameter:
                {reshardingCriticalSectionTimeoutMillis: 24 * 60 * 60 * 1000 /* 1 day */}
        }
    }
});

const kDbName = 'db';
const collName = 'foo';
const ns = kDbName + '.' + collName;
const mongos = st.s;

assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: {oldKey: 1}}));

let nZones = 10000;
let zones = [];
let shard0Zones = [];
let shard1Zones = [];
for (let i = 0; i < nZones; i++) {
    let zoneName = "zone" + i;
    zones.push({zone: zoneName, min: {"newKey": i}, max: {"newKey": i + 1}});

    if (i % 2 == 0) {
        shard0Zones.push(zoneName);
    } else {
        shard1Zones.push(zoneName);
    }
}

jsTestLog("Updating First Zone");
assert.commandWorked(
    mongos.getDB("config").shards.update({_id: st.shard0.shardName}, {$set: {tags: shard0Zones}}));
jsTestLog("Updating First Zone");
assert.commandWorked(
    mongos.getDB("config").shards.update({_id: st.shard1.shardName}, {$set: {tags: shard1Zones}}));

jsTestLog("Resharding Collection");
assert.commandWorked(mongos.adminCommand({reshardCollection: ns, key: {newKey: 1}, zones: zones}));

// Assert that the correct number of zones and chunks documents exist after resharding 'db.foo'.
// There should be two more chunks docs than zones docs created to cover the ranges
// {newKey: minKey -> newKey : 0} and {newKey: nZones -> newKey : maxKey} which are not associated
// with a zone.
assert.eq(mongos.getDB("config").tags.find({ns: ns}).itcount(), nZones);
assert.eq(findChunksUtil.countChunksForNs(mongos.getDB("config"), ns), nZones + 2);

st.stop();
})();