summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_config_db_collections.js
blob: d666f3c8d439c0c59d0313c170ac262b81b78478 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(function() {
    'use strict';

    function assertNoChunksOnConfig() {
        assert.eq(0, config.chunks.count({"shard": "config"}));
    }

    // Database-level tests
    {
        var st = new ShardingTest({shards: 2});
        var config = st.s.getDB('config');
        var admin = st.s.getDB('admin');

        // At first, there should not be an entry for config
        assert.eq(0, config.databases.count({"_id": "config"}));

        // Test that we can enable sharding on the config db
        assert.commandWorked(admin.runCommand({enableSharding: 'config'}));

        // We should never have a metadata doc for config, it is generated in-mem
        assert.eq(0, config.databases.count({"_id": "config"}));

        // Test that you cannot set the primary shard for config (not even to 'config')
        assert.commandFailed(admin.runCommand({movePrimary: 'config', to: 'shard0000'}));
        assert.commandFailed(admin.runCommand({movePrimary: 'config', to: 'config'}));

        st.stop();
    }

    // Collection-level tests
    {
        var st = new ShardingTest({shards: 2});
        var config = st.s.getDB('config');
        var admin = st.s.getDB('admin');

        // Test that we can shard an empty collection in the config db
        assert.commandWorked(admin.runCommand({shardCollection: 'config.sharded', key: {_id: 1}}));
        assertNoChunksOnConfig();

        // Test that we cannot shard a collection in 'config' that holds data
        assert.writeOK(config.withdata.insert({a: 1}));
        assert.commandFailed(admin.runCommand({shardCollection: 'config.withdata', key: {_id: 1}}));

        // Test that we cannot specify an initial number of chunks,
        // because our shard key is not hashed.
        assert.commandFailed(admin.runCommand(
            {shardCollection: 'config.numchunks', key: {_id: 1}, numInitialChunks: 10}));
        assertNoChunksOnConfig();

        // Test that we cannot re-shard the same collection
        assert.commandFailed(admin.runCommand({shardCollection: 'config.sharded', key: {a: 1}}));
        assertNoChunksOnConfig();

        // Test that we can drop a sharded collection in config
        assert.commandWorked(admin.runCommand({shardCollection: 'config.todrop', key: {_id: 1}}));
        config.todrop.insert({a: 1});
        assertNoChunksOnConfig();
        assert(config.todrop.drop());
        assert.eq(0, config.chunks.count({"ns": "config.todrop"}));
        assert.eq(0, config.todrop.count());

        // Test that we can drop a non-sharded collection in config
        config.newcoll.insert({a: 1});
        assert(config.newcoll.drop());
        assert.eq(0, config.newcoll.count());
        assertNoChunksOnConfig();

        st.stop();
    }

    // CRUD operations on sharded collections in config
    {
        var st = new ShardingTest({shards: 2});
        var config = st.s.getDB('config');
        var admin = st.s.getDB('admin');

        assert.commandWorked(admin.runCommand({shardCollection: 'config.sharded', key: {_id: 1}}));
        assertNoChunksOnConfig();

        // Insertion and retrieval
        assert.commandWorked(st.splitAt("config.sharded", {_id: 0}));
        assert.commandWorked(
            admin.runCommand({moveChunk: "config.sharded", find: {_id: -10}, to: "shard0000"}));
        assert.commandWorked(
            admin.runCommand({moveChunk: "config.sharded", find: {_id: 10}, to: "shard0001"}));

        assert.writeOK(config.sharded.insert({_id: -10}));
        assert.writeOK(config.sharded.insert({_id: 10}));
        assert.eq({_id: -10}, config.sharded.findOne({_id: -10}));
        assert.eq({_id: 10}, config.sharded.findOne({_id: 10}));

        var shard0000 = st._connections[0].getDB("config");
        var shard0001 = st._connections[1].getDB("config");
        assert.eq({_id: -10}, shard0000.sharded.findOne());
        assert.eq({_id: 10}, shard0001.sharded.findOne());

        assert.eq(2, config.sharded.count());

        for (var i = 0; i < 100; i++) {
            assert.writeOK(config.sharded.insert({a: i}));
        }

        // Updates
        assert.writeOK(config.sharded.update({_id: 10}, {$set: {a: 15}}));
        assert.writeOK(config.sharded.update({_id: 10}, {$set: {a: 20}}));

        // Deletes
        assert.writeOK(config.sharded.remove({_id: 10}));
        assert.writeOK(config.sharded.remove({a: {$gt: 50}}));

        // Make an index
        assert.commandWorked(config.sharded.createIndex({a: 1}));

        st.stop();
    }

    // Chunk-level operations
    {
        var st = new ShardingTest({shards: 2});
        var config = st.s.getDB('config');
        var admin = st.s.getDB('admin');

        assert.commandWorked(admin.runCommand({shardCollection: 'config.sharded', key: {_id: 1}}));
        assertNoChunksOnConfig();

        // Test that we can split chunks
        assert.commandWorked(st.splitAt("config.sharded", {_id: 10}));
        assert.commandWorked(st.splitAt("config.sharded", {_id: 100}));
        assert.commandWorked(st.splitAt("config.sharded", {_id: 1000}));
        assertNoChunksOnConfig();

        // Try to move a chunk to config shard, should fail
        assert.commandFailed(
            admin.runCommand({moveChunk: "config.sharded", find: {_id: 40}, to: "config"}));
        assertNoChunksOnConfig();

        // Test that we can move chunks between two non-config shards
        assert.commandWorked(
            admin.runCommand({moveChunk: "config.sharded", find: {_id: 40}, to: "shard0001"}));
        assert.commandWorked(
            admin.runCommand({moveChunk: "config.sharded", find: {_id: 40}, to: "shard0000"}));
        assertNoChunksOnConfig();

        st.stop();
    }

    // When test commands are not enabled, only system.sessions may be sharded.
    {
        jsTest.setOption('enableTestCommands', false);

        var st = new ShardingTest({shards: 2});
        var admin = st.s.getDB('admin');

        assert.commandWorked(
            admin.runCommand({shardCollection: "config.system.sessions", key: {_id: 1}}));
        assert.commandFailed(
            admin.runCommand({shardCollection: "config.anythingelse", key: {_id: 1}}));

        st.stop();
    }

    // Cannot shard things in config without shards.
    {
        var st = new ShardingTest({shards: 0});
        var admin = st.s.getDB('admin');

        assert.commandFailed(
            admin.runCommand({shardCollection: "config.system.sessions", key: {_id: 1}}));

        st.stop();
    }

})();