summaryrefslogtreecommitdiff
path: root/jstests/sharding/refine_collection_shard_key_drops_chunks.js
blob: 9f89445fa6798e9bd18a2166a80e5e729f73cfa2 (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
//
// Tests that refineCollectionShardKey deletes all existing chunks in the persisted routing table
// cache.
//

(function() {
'use strict';
load('jstests/libs/fail_point_util.js');

const st = new ShardingTest({shards: 1});
const mongos = st.s0;
const shard = st.shard0;
const kDbName = 'db';
const kCollName = 'foo';
const kNsName = kDbName + '.' + kCollName;
const kConfigCacheChunks = 'config.cache.chunks.' + kNsName;
const oldKeyDoc = {
    a: 1,
    b: 1
};
const newKeyDoc = {
    a: 1,
    b: 1,
    c: 1,
    d: 1
};

assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
assert.commandWorked(mongos.adminCommand({shardCollection: kNsName, key: oldKeyDoc}));
assert.commandWorked(mongos.getCollection(kNsName).createIndex(newKeyDoc));

// Ensure that there exist three chunks belonging to 'db.foo' covering the entire key range.
//
// Chunk 1: {a: MinKey, b: MinKey} -->> {a: 0, b: 0}
// Chunk 2: {a: 0, b: 0} -->> {a: 5, b: 5}
// Chunk 3: {a: 5, b: 5} -->> {a: MaxKey, b: MaxKey}
assert.commandWorked(mongos.adminCommand({split: kNsName, middle: {a: 0, b: 0}}));
assert.commandWorked(mongos.adminCommand({split: kNsName, middle: {a: 5, b: 5}}));

// Flush the routing table cache and verify that 'config.cache.chunks.db.foo' is as expected
// before refineCollectionShardKey.
assert.commandWorked(shard.adminCommand({_flushRoutingTableCacheUpdates: kNsName}));
let chunkArr = shard.getCollection(kConfigCacheChunks).find({}).sort({min: 1}).toArray();
assert.eq(3, chunkArr.length);
assert.eq({a: MinKey, b: MinKey}, chunkArr[0]._id);
assert.eq({a: 0, b: 0}, chunkArr[0].max);
assert.eq({a: 0, b: 0}, chunkArr[1]._id);
assert.eq({a: 5, b: 5}, chunkArr[1].max);
assert.eq({a: 5, b: 5}, chunkArr[2]._id);
assert.eq({a: MaxKey, b: MaxKey}, chunkArr[2].max);

// Enable failpoint 'hangPersistCollectionAndChangedChunksAfterDropChunks' and flush the routing
// table cache.
let hangAfterDropChunksFailPoint =
    configureFailPoint(shard, 'hangPersistCollectionAndChangedChunksAfterDropChunks');

assert.commandWorked(mongos.adminCommand({refineCollectionShardKey: kNsName, key: newKeyDoc}));

// Verify that all chunks belonging to 'db.foo' have been deleted.
hangAfterDropChunksFailPoint.wait();
chunkArr = shard.getCollection(kConfigCacheChunks).find({}).sort({min: 1}).toArray();
assert.eq(0, chunkArr.length);

// Disable failpoint 'hangPersistCollectionAndChangedChunksAfterDropChunks' and continue
// flushing the routing table cache.
hangAfterDropChunksFailPoint.off();

// Verify that 'config.cache.chunks.db.foo' is as expected after refineCollectionShardKey. NOTE: We
// use assert.soon here because refineCollectionShardKey doesn't block for each shard to refresh.
assert.soon(() => {
    chunkArr = shard.getCollection(kConfigCacheChunks).find({}).sort({min: 1}).toArray();
    return (3 === chunkArr.length);
});
assert.eq({a: MinKey, b: MinKey, c: MinKey, d: MinKey}, chunkArr[0]._id);
assert.eq({a: 0, b: 0, c: MinKey, d: MinKey}, chunkArr[0].max);
assert.eq({a: 0, b: 0, c: MinKey, d: MinKey}, chunkArr[1]._id);
assert.eq({a: 5, b: 5, c: MinKey, d: MinKey}, chunkArr[1].max);
assert.eq({a: 5, b: 5, c: MinKey, d: MinKey}, chunkArr[2]._id);
assert.eq({a: MaxKey, b: MaxKey, c: MaxKey, d: MaxKey}, chunkArr[2].max);

st.stop();
})();