summaryrefslogtreecommitdiff
path: root/jstests/sharding/initial_split_validate_shard_collections.js
blob: 537b03a87837698978f24c5ed39ed186a80903c9 (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
/**
 * Explicitly validates that all shards' collections have the correct UUIDs after an initial split
 * which spreads the collection across all available shards.
 */

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

(function() {
'use strict';

let st = new ShardingTest({shards: 2});
let mongos = st.s0;

assert.commandWorked(mongos.adminCommand({enableSharding: 'test'}));
st.ensurePrimaryShard('test', st.shard1.shardName);

assert.commandWorked(
    mongos.adminCommand({shardCollection: 'test.user', key: {x: 'hashed'}, numInitialChunks: 2}));

// Ensure that all the pending (received chunks) have been incorporated in the shard's filtering
// metadata so they will show up in the getShardVersion command
assert.eq(0, mongos.getDB('test').user.find({}).itcount());

st.printShardingStatus();

function checkMetadata(metadata) {
    jsTestLog(tojson(metadata));

    assert.eq(1, metadata.chunks.length);
    assert.eq(0, metadata.pending.length);

    // Check that the single chunk on the shard's metadata is a valid chunk (getShardVersion
    // represents chunks as an array of [min, max])
    let chunks = metadata.chunks;
    assert(bsonWoCompare(chunks[0][0], chunks[0][1]) < 0);
}

// Check that the shards' in-memory catalog caches were refreshed
checkMetadata(assert
                  .commandWorked(st.rs0.getPrimary().adminCommand(
                      {getShardVersion: 'test.user', fullMetadata: true}))
                  .metadata);
checkMetadata(assert
                  .commandWorked(st.rs1.getPrimary().adminCommand(
                      {getShardVersion: 'test.user', fullMetadata: true}))
                  .metadata);

// Check that the shards' catalogs have the correct UUIDs
const configUUID = getUUIDFromConfigCollections(mongos, 'test.user');
const shard0UUID = getUUIDFromListCollections(st.shard0.getDB('test'), 'user');
const shard1UUID = getUUIDFromListCollections(st.shard1.getDB('test'), 'user');
assert.eq(configUUID, shard0UUID);
assert.eq(configUUID, shard1UUID);

// Check that the shards' on-disk caches have the correct number of chunks
assert.commandWorked(
    st.shard0.adminCommand({_flushRoutingTableCacheUpdates: 'test.user', syncFromConfig: false}));
assert.commandWorked(
    st.shard1.adminCommand({_flushRoutingTableCacheUpdates: 'test.user', syncFromConfig: false}));

const chunksOnConfigCount = st.config.chunks.count({ns: 'test.user'});
assert.eq(2, chunksOnConfigCount);

const cacheChunksOnShard0 =
    st.shard0.getDB("config").getCollection("cache.chunks.test.user").find().toArray();
const cacheChunksOnShard1 =
    st.shard1.getDB("config").getCollection("cache.chunks.test.user").find().toArray();
assert.eq(chunksOnConfigCount, cacheChunksOnShard0.length);
assert.eq(chunksOnConfigCount, cacheChunksOnShard1.length);
assert.eq(cacheChunksOnShard0, cacheChunksOnShard1);

st.stop();
})();