summaryrefslogtreecommitdiff
path: root/jstests/sharding/refine_shardkey_config_cache_refresh.js
blob: 5418215c4cce12b264cc0774770e7976edf6b854 (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
/**
 * Refine shard key currently only changes the epoch and does not change the major/minor versions
 * in the chunks. This test simulates a stepDown in the middle of making changes to the
 * config.cache collections by the ShardServerCatalogCacheLoader after a refine shard key and makes
 * sure that the shard will be able to eventually reach the valid state on config.cache.
 *
 * @tags: [requires_fcv_47, disabled_due_to_server_58295]
 */

load("jstests/libs/uuid_util.js");

(function() {
'use strict';

let st = new ShardingTest({shards: 1});

let testDB = st.s.getDB('test');

assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
st.ensurePrimaryShard('test', st.shard0.shardName);
assert.commandWorked(st.s.adminCommand({shardCollection: 'test.user', key: {x: 1}}));

assert.commandWorked(st.s.adminCommand({split: 'test.user', middle: {x: 0}}));
assert.commandWorked(st.s.adminCommand({split: 'test.user', middle: {x: 10}}));

assert.commandWorked(testDB.user.insert({x: 1, y: 1}));
assert.commandWorked(testDB.user.insert({x: 10, y: 1}));
testDB.user.createIndex({x: 1, y: 1});

let priConn = st.rs0.getPrimary();
assert.commandWorked(
    priConn.adminCommand({_flushRoutingTableCacheUpdates: 'test.user', syncFromConfig: true}));

let collEntry = st.config.collections.findOne({_id: 'test.user'});
let chunksCollName = "cache.chunks.test.user";
let chunkCache = priConn.getDB('config').getCollection(chunksCollName);
let preRefineChunks = chunkCache.find().toArray();
assert.eq(3, preRefineChunks.length);

assert.commandWorked(st.s.adminCommand({refineCollectionShardKey: 'test.user', key: {x: 1, y: 1}}));

// Force refresh the config.cache.collections and config.cache.chunks, then manually revert
// the config.cache.chunks to simulate crash before updating config.cache.chunks.
assert.commandWorked(priConn.adminCommand({_flushRoutingTableCacheUpdates: 'test.user'}));

assert.commandWorked(chunkCache.remove({}, false /* justOne */));
preRefineChunks.forEach((chunk) => {
    assert.commandWorked(chunkCache.insert(chunk));
});

let collCache = priConn.getDB('config').cache.collections;
assert.commandWorked(collCache.update({_id: 'test.user'}, {$set: {refreshing: true}}));

// Force refresh should be able to detect anomaly and fix the cache collections.
assert.commandWorked(priConn.adminCommand({_flushRoutingTableCacheUpdates: 'test.user'}));

let fixedChunks = chunkCache.find().sort({min: 1}).toArray();
assert.eq(3, fixedChunks.length);

assert.eq({x: MinKey, y: MinKey}, fixedChunks[0]._id);
assert.eq({x: 0, y: MinKey}, fixedChunks[1]._id);
assert.eq({x: 10, y: MinKey}, fixedChunks[2]._id);

let collDoc = collCache.findOne({_id: 'test.user'});
assert.eq(false, collDoc.refreshing);

st.stop();
}());