summaryrefslogtreecommitdiff
path: root/jstests/sharding/move_chunk_basic.js
blob: 8ca26547c184d9ed2e9bd6919783e6886623c405 (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
//
// Basic tests for moveChunk.
//

(function() {
    'use strict';

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

    var mongos = st.s0;
    var shard0 = st.shard0.shardName;
    var shard1 = st.shard1.shardName;

    assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));
    st.ensurePrimaryShard(kDbName, shard0);

    // Fail if invalid namespace.
    assert.commandFailed(mongos.adminCommand({moveChunk: '', find: {_id: 1}, to: shard1}));

    // Fail if database does not exist.
    assert.commandFailed(mongos.adminCommand({moveChunk: 'a.b', find: {_id: 1}, to: shard1}));

    // Fail if collection is unsharded.
    assert.commandFailed(
        mongos.adminCommand({moveChunk: kDbName + '.xxx', find: {_id: 1}, to: shard1}));

    function testHashed() {
        var ns = kDbName + '.fooHashed';
        assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: {_id: 'hashed'}}));

        var aChunk = mongos.getDB('config').chunks.findOne({_id: RegExp(ns), shard: shard0});
        assert(aChunk);

        // Error if either of the bounds is not a valid shard key (BSON object - 1 yields a NaN)
        assert.commandFailed(
            mongos.adminCommand({moveChunk: ns, bounds: [aChunk.min - 1, aChunk.max], to: shard1}));
        assert.commandFailed(
            mongos.adminCommand({moveChunk: ns, bounds: [aChunk.min, aChunk.max - 1], to: shard1}));

        // Fail if find and bounds are both set.
        assert.commandFailed(mongos.adminCommand(
            {moveChunk: ns, find: {_id: 1}, bounds: [aChunk.min, aChunk.max], to: shard1}));

        assert.commandWorked(
            mongos.adminCommand({moveChunk: ns, bounds: [aChunk.min, aChunk.max], to: shard1}));

        assert.eq(0, mongos.getDB('config').chunks.count({_id: aChunk._id, shard: shard0}));
        assert.eq(1, mongos.getDB('config').chunks.count({_id: aChunk._id, shard: shard1}));

        mongos.getDB(kDbName).fooHashed.drop();
    }

    function testNotHashed(keyDoc) {
        var ns = kDbName + '.foo';

        // Fail if find is not a valid shard key.
        assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: keyDoc}));

        var chunkId = mongos.getDB('config').chunks.findOne({_id: RegExp(ns), shard: shard0})._id;

        assert.commandFailed(mongos.adminCommand({moveChunk: ns, find: {xxx: 1}, to: shard1}));
        assert.eq(shard0, mongos.getDB('config').chunks.findOne({_id: chunkId}).shard);

        assert.commandWorked(mongos.adminCommand({moveChunk: ns, find: keyDoc, to: shard1}));
        assert.eq(shard1, mongos.getDB('config').chunks.findOne({_id: chunkId}).shard);

        // Fail if to shard does not exists
        assert.commandFailed(mongos.adminCommand({moveChunk: ns, find: keyDoc, to: 'WrongShard'}));

        // Fail if chunk is already at shard
        assert.eq(shard1, mongos.getDB('config').chunks.findOne({_id: chunkId}).shard);

        mongos.getDB(kDbName).foo.drop();
    }

    testHashed();

    testNotHashed({a: 1});

    testNotHashed({a: 1, b: 1});

    st.stop();

})();