summaryrefslogtreecommitdiff
path: root/jstests/sharding/merge_chunk_hashed.js
blob: cd3d184f47419e2f75ac94e32fce81a757dfd662 (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
/*
 * Test that merging chunks for hashed sharding via mongos works/doesn't work with
 * different chunk configurations.
 */
(function() {
'use strict';

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

let st = new ShardingTest({shards: 2, mongos: 2});
// , configOptions: {verbose: 3}
let mongos = st.s0;
let staleMongos = st.s1;

let dbName = "test";
let collName = "user";
let ns = dbName + "." + collName;
let configDB = mongos.getDB('config');
let admin = mongos.getDB("admin");
let coll = mongos.getCollection(ns);

assert.commandWorked(admin.runCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, st.shard0.shardName);
assert.commandWorked(admin.runCommand({shardCollection: ns, key: {x: 'hashed'}}));

// Default chunks:
// shard0: MIN                  -> -4611686018427387902,
//         -4611686018427387902 -> 0
// shard1: 0                    -> 4611686018427387902
//         4611686018427387902  -> MAX

// Get the chunk -4611686018427387902 -> 0 on shard0.
let chunkToSplit =
    configDB.chunks.findOne({ns: ns, min: {$ne: {x: MinKey}}, shard: st.shard0.shardName});

// Create chunks from that chunk and move some chunks to create holes.
// shard0: MIN                  -> chunkToSplit.min,
//         chunkToSplit.min     -> -4500000000000000000,
//         (hole),
//         -4000000000000000000 -> -3500000000000000000,
//         -3500000000000000000 -> -3000000000000000000,
//         -3000000000000000000 -> -2500000000000000000,
//         (hole),
//         -2000000000000000000 -> 0
// shard1: -4500000000000000000 ->  -4000000000000000000
//         -2500000000000000000 -> -2000000000000000000
//         0                    -> 4611686018427387902
//         4611686018427387902  -> MAX
let splitPoints = [
    {x: NumberLong(-4500000000000000000)},
    {x: NumberLong(-4000000000000000000)},
    {x: NumberLong(-3500000000000000000)},
    {x: NumberLong(-3000000000000000000)},
    {x: NumberLong(-2500000000000000000)},
    {x: NumberLong(-2000000000000000000)},
];
assert.gt(0, bsonWoCompare(chunkToSplit.min, splitPoints[0]));
assert.lt(0, bsonWoCompare(chunkToSplit.max, splitPoints[splitPoints.length - 1]));

jsTest.log("Creating additional chunks and holes...");
for (let splitPoint of splitPoints) {
    assert.commandWorked(admin.runCommand({split: ns, middle: splitPoint}));
}
assert.commandWorked(admin.runCommand({
    moveChunk: ns,
    bounds: [{x: NumberLong(-4500000000000000000)}, {x: NumberLong(-4000000000000000000)}],
    to: st.shard1.shardName,
    _waitForDelete: true
}));
assert.commandWorked(admin.runCommand({
    moveChunk: ns,
    bounds: [{x: NumberLong(-2500000000000000000)}, {x: NumberLong(-2000000000000000000)}],
    to: st.shard1.shardName,
    _waitForDelete: true
}));

let chunkDocs = configDB.chunks.find({ns: ns}).toArray();
let shardChunkBounds = chunkBoundsUtil.findShardChunkBounds(chunkDocs);

jsTest.log("Inserting docs...");
// Use docs that belong to different chunks and go on both shards.
let docs = [{x: -100}, {x: -60}, {x: -10}, {x: 10}];
let shards = [];
let docChunkBounds = [];
docs.forEach(function(doc) {
    let hash = convertShardKeyToHashed(doc.x);
    let {shard, bounds} =
        chunkBoundsUtil.findShardAndChunkBoundsForShardKey(st, shardChunkBounds, {x: hash});
    shards.push(shard);
    docChunkBounds.push(bounds);
});
assert.eq(2, (new Set(shards)).size);
assert.eq(docs.length, (new Set(docChunkBounds)).size);
assert.commandWorked(coll.insert(docs));

var staleCollection = staleMongos.getCollection(ns);

jsTest.log("Trying merges that should fail...");

// Make sure merging non-exact chunks is invalid.
assert.commandFailed(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: MinKey}, {x: NumberLong(-5000000000000000000)}]}));
assert.commandFailed(admin.runCommand({
    mergeChunks: ns,
    bounds: [{x: NumberLong(-5500000000000000000)}, {x: NumberLong(-4500000000000000000)}]
}));
assert.commandFailed(admin.runCommand({
    mergeChunks: ns,
    bounds: [{x: NumberLong(4500000000000000000)}, {x: NumberLong(5500000000000000000)}]
}));
assert.commandFailed(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: NumberLong(-1500000000000000000)}, {x: MaxKey}]}));

// Make sure merging single chunks is invalid.
assert.commandFailed(admin.runCommand({mergeChunks: ns, bounds: [{x: MinKey}, chunkToSplit.min]}));
assert.commandFailed(admin.runCommand({
    mergeChunks: ns,
    bounds: [{x: NumberLong(-4500000000000000000)}, {x: NumberLong(-4000000000000000000)}]
}));
assert.commandFailed(admin.runCommand({mergeChunks: ns, bounds: [{x: 110}, {x: MaxKey}]}));

// Make sure merging over holes is invalid.
assert.commandFailed(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: MinKey}, {x: NumberLong(-3500000000000000000)}]}));
assert.commandFailed(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: NumberLong(-3500000000000000000)}, {x: NumberLong(0)}]}));
assert.commandFailed(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: NumberLong(-3000000000000000000)}, {x: NumberLong(0)}]}));

// Make sure merging between shards is invalid.
assert.commandFailed(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: MinKey}, {x: NumberLong(-4000000000000000000)}]}));
assert.commandFailed(admin.runCommand({
    mergeChunks: ns,
    bounds: [{x: NumberLong(-3000000000000000000)}, {x: NumberLong(-2000000000000000000)}]
}));
assert.commandFailed(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: NumberLong(-2500000000000000000)}, {x: NumberLong(0)}]}));
assert.eq(4, staleCollection.find().itcount());

jsTest.log("Trying merges that should succeed...");

// Make sure merge including the MinKey works.
assert.commandWorked(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: MinKey}, {x: NumberLong(-4500000000000000000)}]}));
assert.eq(4, staleCollection.find().itcount());
// shard0: MIN                  -> -4500000000000000000,
//         (hole),
//         -4000000000000000000 -> -3500000000000000000,
//         -3500000000000000000 -> -3000000000000000000,
//         -3000000000000000000 -> -2500000000000000000,
//         (hole),
//         -2000000000000000000 -> 0
// shard1: -4500000000000000000 ->  -4000000000000000000
//         -2500000000000000000 -> -2000000000000000000
//         0                    -> 4611686018427387902
//         4611686018427387902  -> MAX

// Make sure merging three chunks in the middle works.
assert.commandWorked(admin.runCommand({
    mergeChunks: ns,
    bounds: [{x: NumberLong(-4000000000000000000)}, {x: NumberLong(-2500000000000000000)}]
}));
assert.eq(4, staleCollection.find().itcount());
// shard0: MIN                  -> -4500000000000000000,
//         (hole),
//         -4000000000000000000 -> -2500000000000000000,
//         (hole),
//         -2000000000000000000 -> 0
// shard1: -4500000000000000000 -> -4000000000000000000
//         -2500000000000000000 -> -2000000000000000000
//         0                    -> 4611686018427387902
//         4611686018427387902  -> MAX

// Make sure merge including the MaxKey works.
assert.commandWorked(
    admin.runCommand({mergeChunks: ns, bounds: [{x: NumberLong(0)}, {x: MaxKey}]}));
assert.eq(4, staleCollection.find().itcount());

// Make sure merging chunks after a chunk has been moved out of a shard succeeds
assert.commandWorked(admin.runCommand({
    moveChunk: ns,
    bounds: [{x: NumberLong(-2000000000000000000)}, {x: NumberLong(0)}],
    to: st.shard1.shardName,
    _waitForDelete: true
}));
assert.commandWorked(admin.runCommand({
    moveChunk: ns,
    bounds: [{x: NumberLong(-4500000000000000000)}, {x: NumberLong(-4000000000000000000)}],
    to: st.shard0.shardName,
    _waitForDelete: true
}));
assert.eq(4, staleCollection.find().itcount());
// shard0: MIN                  -> -4500000000000000000,
//         -4500000000000000000 -> -4000000000000000000
//         -4000000000000000000 -> -2500000000000000000,
// shard1: -2500000000000000000 -> -2000000000000000000
//         -2000000000000000000 -> 0
//         0                    -> 4611686018427387902
//         4611686018427387902  -> MAX

assert.commandWorked(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: NumberLong(-2500000000000000000)}, {x: MaxKey}]}));
assert.eq(4, staleCollection.find().itcount());
// shard0: MIN                  -> -4500000000000000000,
//         -4500000000000000000 -> -4000000000000000000
//         -4000000000000000000 -> -2500000000000000000,
// shard1: -2500000000000000000 -> MAX

// Make sure merge on the other shard after a chunk has been merged succeeds.
assert.commandWorked(admin.runCommand(
    {mergeChunks: ns, bounds: [{x: MinKey}, {x: NumberLong(-2500000000000000000)}]}));
// shard0: MIN                  -> -2500000000000000000,
// shard1: -2500000000000000000 -> MAX

assert.eq(2, configDB.chunks.find({ns: ns}).itcount());
assert.eq(1,
          configDB.chunks
              .find({
                  ns: ns,
                  min: {x: MinKey},
                  max: {x: NumberLong(-2500000000000000000)},
                  shard: st.shard0.shardName
              })
              .count());
assert.eq(1,
          configDB.chunks
              .find({
                  ns: ns,
                  min: {x: NumberLong(-2500000000000000000)},
                  max: {x: MaxKey},
                  shard: st.shard1.shardName
              })
              .count());

st.stop();
})();