summaryrefslogtreecommitdiff
path: root/jstests/sharding/dump_coll_metadata.js
blob: eb60af37cb4c62083b3f1ecb6c6706f4b4cb6c25 (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
//
// Tests that we can dump collection metadata via getShardVersion()
//
(function() {
    'use strict';

    var st = new ShardingTest({shards: 2, mongos: 1});

    var mongos = st.s0;
    var coll = mongos.getCollection("foo.bar");
    var admin = mongos.getDB("admin");
    var shards = mongos.getCollection("config.shards").find().toArray();
    var shardAdmin = st.shard0.getDB("admin");

    assert.commandWorked(admin.runCommand({enableSharding: coll.getDB() + ""}));
    st.ensurePrimaryShard(coll.getDB() + "", shards[0]._id);
    assert.commandWorked(admin.runCommand({shardCollection: coll + "", key: {_id: 1}}));

    assert.commandWorked(shardAdmin.runCommand({getShardVersion: coll + ""}));

    // Make sure we have chunks information on the shard after the shard collection call
    var result = assert.commandWorked(
        shardAdmin.runCommand({getShardVersion: coll + "", fullMetadata: true}));
    printjson(result);
    var metadata = result.metadata;

    assert.eq(metadata.chunks.length, 1);
    assert.eq(metadata.pending.length, 0);
    assert(metadata.chunks[0][0]._id + "" == MinKey + "");
    assert(metadata.chunks[0][1]._id + "" == MaxKey + "");
    assert(metadata.shardVersion + "" == result.global + "");

    // Make sure a collection with no metadata still returns the metadata field
    assert(shardAdmin.runCommand({getShardVersion: coll + "xyz", fullMetadata: true}).metadata !=
           undefined);

    // Make sure we get multiple chunks after a split
    assert(admin.runCommand({split: coll + "", middle: {_id: 0}}).ok);

    assert(shardAdmin.runCommand({getShardVersion: coll + ""}).ok);
    printjson(shardAdmin.runCommand({getShardVersion: coll + "", fullMetadata: true}));

    // Make sure we have chunks info
    result = shardAdmin.runCommand({getShardVersion: coll + "", fullMetadata: true});
    metadata = result.metadata;

    assert.eq(metadata.chunks.length, 2);
    assert.eq(metadata.pending.length, 0);
    assert(metadata.chunks[0][0]._id + "" == MinKey + "");
    assert(metadata.chunks[0][1]._id == 0);
    assert(metadata.chunks[1][0]._id == 0);
    assert(metadata.chunks[1][1]._id + "" == MaxKey + "");
    assert(metadata.shardVersion + "" == result.global + "");

    st.stop();

})();