summaryrefslogtreecommitdiff
path: root/jstests/multiVersion/libs/config_chunks_tags_shared.js
blob: 01355815401adfda4baccac236e9898e583f4e7a (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
/**
 * Functions and variables shared between multiversion/config_chunks_tags_upgrade_cluster.js and
 * multiversion/config_chunks_tags_downgrade_cluster.js.
 */

// Sets up a collection with chunks in the format expected by the testChunkOperations() helper.
function setUpCollectionForChunksTesting(st, ns) {
    assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: -50}}));
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 0}}));
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 1}, to: st.shard1.shardName}));
}

// Sets up zones and chunks for a collection to work with the testZoneOperations() helper.
function setUpCollectionForZoneTesting(st, ns) {
    assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));
    assert.commandWorked(st.s.adminCommand({addShardToZone: st.shard0.shardName, zone: "zone0"}));
    assert.commandWorked(st.s.adminCommand({addShardToZone: st.shard1.shardName, zone: "zone1"}));
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 0}}));
    assert.commandWorked(st.s.adminCommand(
        {updateZoneKeyRange: ns, min: {_id: MinKey}, max: {_id: 0}, zone: "zone0"}));
    assert.commandWorked(
        st.s.adminCommand({updateZoneKeyRange: ns, min: {_id: 0}, max: {_id: 50}, zone: "zone1"}));
}

// Sets up a sharded collection with the given number of chunks and zones.
function setUpCollectionWithManyChunksAndZones(st, ns, numChunks, numZones) {
    assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));

    for (let i = 0; i < numChunks - 1; i++) {
        assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: i}}));
    }

    for (let i = 0; i < numZones; i++) {
        assert.commandWorked(
            st.s.adminCommand({addShardToZone: st.shard0.shardName, zone: "many_zones-" + i}));
        assert.commandWorked(st.s.adminCommand(
            {updateZoneKeyRange: ns, min: {_id: i}, max: {_id: i + 1}, zone: "many_zones-" + i}));
    }
}

function setUpExtraShardedCollections(st, dbName) {
    assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
    st.ensurePrimaryShard(dbName, st.shard1.shardName);

    // Set up one zone with half the key range and two chunks split at {_id: 0}.
    const ns = dbName + ".extra_coll";
    assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));
    assert.commandWorked(
        st.s.adminCommand({addShardToZone: st.shard1.shardName, zone: "extra_zone0"}));
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 0}}));
    assert.commandWorked(st.s.adminCommand(
        {updateZoneKeyRange: ns, min: {_id: MinKey}, max: {_id: 0}, zone: "extra_zone0"}));
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard0.shardName}));

    // Set up a sharded collection with a hashed shard key.
    const hashedNs = dbName + ".extra_coll_hashed";
    assert.commandWorked(st.s.adminCommand({shardCollection: hashedNs, key: {_id: "hashed"}}));
}

function verifyChunksAndTags(st, dbName, chunkNs, zoneNs, {expectNewFormat}) {
    verifyChunks(st, {expectNewFormat});
    verifyTags(st, {expectNewFormat});

    testChunkOperations(st, chunkNs);
    testZoneOperations(st, zoneNs);
    verifyInitialChunks(st, dbName, {expectNewFormat});

    verifyChunks(st, {expectNewFormat});
    verifyTags(st, {expectNewFormat});
}

function getChunks(st, ns) {
    if (ns) {
        return st.s.getDB("config").chunks.find({ns}).sort({min: 1}).toArray();
    }
    return st.s.getDB("config").chunks.find().sort({min: 1}).toArray();
}

// Asserts all chunk documents have the expected format.
function verifyChunks(st, {ns, expectNewFormat}) {
    const chunks = getChunks(st, ns);
    assert.lte(1, chunks.length, tojson(chunks));
    chunks.forEach((chunk) => {
        if (expectNewFormat) {
            assert(chunk._id.isObjectId, tojson(chunk));
            assert.neq("string", typeof chunk._id, tojson(chunk));
        } else {
            assert(!chunk._id.isObjectId, tojson(chunk));
            assert.eq("string", typeof chunk._id, tojson(chunk));
        }

        let expectedChunkFields =
            ["_id", "ns", "min", "max", "shard", "lastmod", "lastmodEpoch", "history"];

        // Jumbo is an optional field.
        if (chunk.hasOwnProperty("jumbo")) {
            expectedChunkFields = expectedChunkFields.concat("jumbo");
        }

        assert.eq(Object.keys(chunk).length, expectedChunkFields.length, tojson(chunk));
        assert.hasFields(chunk, expectedChunkFields);
    });
}

function getTags(st) {
    return st.s.getDB("config").tags.find().sort({min: 1}).toArray();
}

// Asserts all tag documents have the expected format.
function verifyTags(st, {expectNewFormat}) {
    const tags = getTags(st);
    assert.lt(1, tags.length, tojson(tags));
    tags.forEach((tag) => {
        if (expectNewFormat) {
            assert(tag._id.isObjectId, tojson(tag));
            // ObjectId returns "object" from typeof...
            // assert.neq("object", typeof tag._id, tojson(tag));
        } else {
            assert(!tag._id.isObjectId, tojson(tag));
            assert.eq("object", typeof tag._id, tojson(tag));
        }

        const expectedTagFields = ["_id", "ns", "tag", "min", "max"];
        assert.eq(Object.keys(tag).length, expectedTagFields.length, tojson(tag));
        assert.hasFields(tag, expectedTagFields);
    });
}

// Runs basic crud operations against the given namespace.
function testCRUDOperations(st, ns) {
    const coll = st.s.getCollection(ns);
    assert.eq(0, coll.find().itcount());

    assert.commandWorked(coll.insert({_id: -5}));
    assert.commandWorked(coll.insert({_id: 5}));

    assert.commandWorked(coll.update({_id: -5}, {$set: {updated: true}}));
    assert.commandWorked(coll.update({_id: 5}, {$set: {updated: true}}));

    assert.docEq({_id: -5, updated: true}, coll.findOne({_id: -5}));
    assert.docEq({_id: 5, updated: true}, coll.findOne({_id: 5}));

    assert.commandWorked(coll.remove({_id: -5}, true /* justOne */));
    assert.commandWorked(coll.remove({_id: 5}, true /* justOne */));
    assert.eq(0, coll.find().itcount());
}

// Helper to verify chunks are owned by the expected shards.
function verifyChunkDistribution(st, ns, expectedChunkDistribution) {
    for (let i = 0; i < expectedChunkDistribution.length; i++) {
        assert.eq(expectedChunkDistribution[i],
                  st.s.getDB("config").chunks.count({ns: ns, shard: st["shard" + i].shardName}),
                  "unexpected number of chunks on shard " + i);
    }
}

// Assumes ns has the following chunk layout: [-inf, -50), [-50, 0) on shard0 and [0, inf) on
// shard 1.
function testChunkOperations(st, ns) {
    verifyChunkDistribution(st, ns, [2, 1]);

    // Split chunk should work.
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 50}}));
    verifyChunkDistribution(st, ns, [2, 2]);

    testCRUDOperations(st, ns);

    // Move chunk should work with a control chunk.
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard0.shardName}));
    verifyChunkDistribution(st, ns, [3, 1]);

    testCRUDOperations(st, ns);

    // Move chunk should work without a control chunk.
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 50}, to: st.shard0.shardName}));
    verifyChunkDistribution(st, ns, [4, 0]);

    testCRUDOperations(st, ns);

    // Merge chunk should work.
    assert.commandWorked(st.s.adminCommand({mergeChunks: ns, bounds: [{_id: -50}, {_id: 50}]}));
    verifyChunkDistribution(st, ns, [3, 0]);

    testCRUDOperations(st, ns);

    // Reset the chunks to their original state.
    assert.commandWorked(st.s.adminCommand({split: ns, middle: {_id: 0}}));
    verifyChunkDistribution(st, ns, [4, 0]);
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 50}, to: st.shard1.shardName}));
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard1.shardName}));
    verifyChunkDistribution(st, ns, [2, 2]);
    assert.commandWorked(st.s.adminCommand({mergeChunks: ns, bounds: [{_id: 0}, {_id: MaxKey}]}));
    verifyChunkDistribution(st, ns, [2, 1]);

    testCRUDOperations(st, ns);
}

// Assumes ns has two chunks: [-inf, 0), [0, inf), on shards 0 and 1, respectively and that shard0
// is in zone0 which contains [-inf, 0) and shard1 is in zone1 which contains [0, 50).
function testZoneOperations(st, ns) {
    // Verify conflicting zones can't be created.
    assert.commandFailedWithCode(
        st.s.adminCommand({updateZoneKeyRange: ns, min: {_id: -10}, max: {_id: 0}, zone: "zone1"}),
        ErrorCodes.RangeOverlapConflict);

    // Verify zone boundaries are still enforced.
    assert.commandFailedWithCode(
        st.s.adminCommand({moveChunk: ns, find: {_id: -1}, to: st.shard1.shardName}),
        ErrorCodes.IllegalOperation);

    //
    // Verify zone ranges can be updated.
    //

    assert.commandWorked(
        st.s.adminCommand({updateZoneKeyRange: ns, min: {_id: MinKey}, max: {_id: 0}, zone: null}));
    assert.commandWorked(st.s.adminCommand(
        {updateZoneKeyRange: ns, min: {_id: MinKey}, max: {_id: 0}, zone: "zone1"}));

    // Now the chunk can be moved to shard1, which is in zone1.
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard1.shardName}));

    // Reset the chunk and zones.
    assert.commandWorked(
        st.s.adminCommand({updateZoneKeyRange: ns, min: {_id: MinKey}, max: {_id: 0}, zone: null}));
    assert.commandWorked(st.s.adminCommand(
        {updateZoneKeyRange: ns, min: {_id: MinKey}, max: {_id: 0}, zone: "zone0"}));
    assert.commandWorked(
        st.s.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard0.shardName}));
}

let uniqueCollCounter = 0;

// Assumes ns is a non-existent namespace on a database that is sharding enabled.
function verifyInitialChunks(st, dbName, {expectNewFormat}) {
    const ns = dbName + ".unique_coll" + uniqueCollCounter++;
    assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));

    // Assert the chunks created for the new namespace are in the correct format.
    verifyChunks(st, {ns, expectNewFormat});

    // Clean up the new collection.
    assert.commandWorked(st.s.adminCommand({drop: ns}));
}