summaryrefslogtreecommitdiff
path: root/jstests/sharding/list_catalog.js
blob: 6de0aa35af357d60dcdddced05986ec1800df093 (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
/**
 * Tests that $listCatalog only returns entries from chunk-owning shards.
 *
 * TODO (SERVER-64980): Extend test for collectionless $listCatalog once it only returns entries
 * from chunk-owning shards.
 *
 * @tags: [
 *   requires_fcv_60,
 * ]
 */
(function() {
'use strict';

const st = new ShardingTest({shards: 2});

const db = st.s.getDB(jsTestName());
assert.commandWorked(st.s.adminCommand({enableSharding: db.getName()}));
st.ensurePrimaryShard(db.getName(), st.shard0.shardName);

const coll = db.coll;
assert.commandWorked(st.s.adminCommand({shardCollection: coll.getFullName(), key: {_id: 1}}));

// Split at {_id: 1}, moving {_id: 0} to shard0 and {_id: 1} to shard1.
assert.commandWorked(st.splitAt(coll.getFullName(), {_id: 1}));
assert.commandWorked(
    st.s.adminCommand({moveChunk: coll.getFullName(), find: {_id: 0}, to: st.shard0.shardName}));
assert.commandWorked(
    st.s.adminCommand({moveChunk: coll.getFullName(), find: {_id: 1}, to: st.shard1.shardName}));

const checkResult = function(res) {
    for (const entry of res) {
        assert.eq(entry.db, db.getName());
        assert.eq(entry.name, coll.getName());
        assert.eq(entry.ns, coll.getFullName());
        assert.eq(entry.type, "collection");
        assert.eq(entry.md.indexes.length, entry.md.options.clusteredIndex ? 0 : 1);
    }
};

let res = coll.aggregate([{$listCatalog: {}}]).toArray();
jsTestLog('$listCatalog with multiple chunk-owning shards: ' + tojson(res));
assert.eq(res.length, 2);
assert(res.some((entry) => entry.shard === st.shard0.shardName));
assert(res.some((entry) => entry.shard === st.shard1.shardName));
checkResult(res);

// Move {_id: 0} to shard1 so that shard0 does not own any chunks for the collection.
assert.commandWorked(
    st.s.adminCommand({moveChunk: coll.getFullName(), find: {_id: 0}, to: st.shard1.shardName}));

res = coll.aggregate([{$listCatalog: {}}]).toArray();
jsTestLog('$listCatalog with one chunk-owning shard: ' + tojson(res));
assert.eq(res.length, 1);
assert.eq(res[0].shard, st.shard1.shardName);
checkResult(res);

st.stop();
})();