summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_existing_coll_chunk_count.js
blob: 46e2cdba748840e682fad4fa524b13edf0de0c97 (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
/**
 * This test confirms that after sharding a collection with some pre-existing data,
 * the resulting chunks aren't auto-split too aggressively.
 *
 * @tags: [requires_persistence]
 */
(function() {
'use strict';
load('jstests/sharding/autosplit_include.js');

var s = new ShardingTest({
    name: "shard_existing_coll_chunk_count",
    shards: 1,
    mongos: 1,
    other: {enableAutoSplit: true},
});

assert.commandWorked(s.s.adminCommand({enablesharding: "test"}));

var collNum = 0;
var overhead = Object.bsonsize({_id: ObjectId(), i: 1, pad: ""});

var getNumberChunks = function(ns) {
    return s.getDB("config").getCollection("chunks").count({ns});
};

var runCase = function(opts) {
    // Expected options.
    assert.gte(opts.docSize, 0);
    assert.gte(opts.stages.length, 2);

    // Compute padding.
    if (opts.docSize < overhead) {
        var pad = "";
    } else {
        var pad = (new Array(opts.docSize - overhead + 1)).join(' ');
    }

    collNum++;
    var db = s.getDB("test");
    var collName = "coll" + collNum;
    var coll = db.getCollection(collName);
    var i = 0;
    var limit = 0;
    var stageNum = 0;
    var stage = opts.stages[stageNum];

    // Insert initial docs.
    var bulk = coll.initializeUnorderedBulkOp();
    limit += stage.numDocsToInsert;
    for (; i < limit; i++) {
        bulk.insert({i, pad});
    }
    assert.commandWorked(bulk.execute());

    // Create shard key index.
    assert.commandWorked(coll.createIndex({i: 1}));

    // Shard collection.
    assert.commandWorked(s.s.adminCommand({shardcollection: coll.getFullName(), key: {i: 1}}));

    // Confirm initial number of chunks.
    var numChunks = getNumberChunks(coll.getFullName());
    assert.eq(numChunks,
              stage.expectedNumChunks,
              'in ' + coll.getFullName() + ' expected ' + stage.expectedNumChunks +
                  ' initial chunks, but found ' + numChunks + '\nopts: ' + tojson(opts) +
                  '\nchunks:\n' + s.getChunksString(coll.getFullName()));

    // Do the rest of the stages.
    for (stageNum = 1; stageNum < opts.stages.length; stageNum++) {
        stage = opts.stages[stageNum];

        // Insert the later docs (one at a time, to maximise the autosplit effects).
        limit += stage.numDocsToInsert;
        for (; i < limit; i++) {
            coll.insert({i, pad});

            waitForOngoingChunkSplits(s);
        }

        // Confirm number of chunks for this stage.
        var numChunks = getNumberChunks(coll.getFullName());
        assert.gte(numChunks,
                   stage.expectedNumChunks,
                   'in ' + coll.getFullName() + ' expected ' + stage.expectedNumChunks +
                       ' chunks for stage ' + stageNum + ', but found ' + numChunks + '\nopts: ' +
                       tojson(opts) + '\nchunks:\n' + s.getChunksString(coll.getFullName()));
    }
};

// Original problematic case.
runCase({
    docSize: 0,
    stages: [
        {numDocsToInsert: 20000, expectedNumChunks: 1},
        {numDocsToInsert: 7, expectedNumChunks: 1},
        {numDocsToInsert: 1000, expectedNumChunks: 1},
    ],
});

// Original problematic case (worse).
runCase({
    docSize: 0,
    stages: [
        {numDocsToInsert: 90000, expectedNumChunks: 1},
        {numDocsToInsert: 7, expectedNumChunks: 1},
        {numDocsToInsert: 1000, expectedNumChunks: 1},
    ],
});

// Pathological case #1.
runCase({
    docSize: 522,
    stages: [
        {numDocsToInsert: 8191, expectedNumChunks: 1},
        {numDocsToInsert: 2, expectedNumChunks: 1},
        {numDocsToInsert: 1000, expectedNumChunks: 1},
    ],
});

// Pathological case #2.
runCase({
    docSize: 522,
    stages: [
        {numDocsToInsert: 8192, expectedNumChunks: 1},
        {numDocsToInsert: 8192, expectedNumChunks: 1},
    ],
});

// Lower chunksize to 1MB, and restart the mongod for it to take. We also
// need to restart mongos for the case of the last-lts suite where the
// shard is also last-lts.
assert.commandWorked(
    s.getDB("config").getCollection("settings").update({_id: "chunksize"}, {$set: {value: 1}}, {
        upsert: true
    }));

s.restartMongos(0);
s.restartShardRS(0);

// Original problematic case, scaled down to smaller chunksize.
runCase({
    docSize: 0,
    stages: [
        {numDocsToInsert: 10000, expectedNumChunks: 1},
        {numDocsToInsert: 10, expectedNumChunks: 1},
        {numDocsToInsert: 20, expectedNumChunks: 1},
        {numDocsToInsert: 40, expectedNumChunks: 1},
        {numDocsToInsert: 1000, expectedNumChunks: 1},
    ],
});

// Docs just smaller than half chunk size.
runCase({
    docSize: 510 * 1024,
    stages: [
        {numDocsToInsert: 10, expectedNumChunks: 6},
        {numDocsToInsert: 10, expectedNumChunks: 10},
    ],
});

// Docs just larger than half chunk size.
runCase({
    docSize: 514 * 1024,
    stages: [
        {numDocsToInsert: 10, expectedNumChunks: 10},
        {numDocsToInsert: 10, expectedNumChunks: 18},
    ],
});

s.stop();
})();