summaryrefslogtreecommitdiff
path: root/jstests/sharding/reshard_collection_basic.js
blob: c536503c82a23f8bd1cb70178267160924f2a879 (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
//
// Basic tests for reshardCollection.
// @tags: [requires_fcv_47]
//

(function() {
'use strict';

const st = new ShardingTest({mongos: 1, shards: 2});
const kDbName = 'db';
const collName = '.foo';
const ns = kDbName + collName;
const mongos = st.s0;

let removeAllReshardingCollections = () => {
    mongos.getDB(kDbName).foo.drop();
    mongos.getDB('config').reshardingOperations.remove({nss: ns});
    mongos.getDB('config').collections.remove({reshardingFields: {$exists: true}});
    st.rs0.getPrimary().getDB('config').localReshardingOperations.donor.remove({nss: ns});
    st.rs0.getPrimary().getDB('config').localReshardingOperations.recipient.remove({nss: ns});
    st.rs1.getPrimary().getDB('config').localReshardingOperations.donor.remove({nss: ns});
    st.rs1.getPrimary().getDB('config').localReshardingOperations.recipient.remove({nss: ns});
};

// Fail if sharding is disabled.
assert.commandFailedWithCode(mongos.adminCommand({reshardCollection: ns, key: {_id: 1}}),
                             ErrorCodes.NamespaceNotFound);

assert.commandWorked(mongos.adminCommand({enableSharding: kDbName}));

// Fail if collection is unsharded.
assert.commandFailedWithCode(mongos.adminCommand({reshardCollection: ns, key: {_id: 1}}),
                             ErrorCodes.NamespaceNotSharded);

assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: {_id: 1}}));

// Fail if missing required key.
assert.commandFailedWithCode(mongos.adminCommand({reshardCollection: ns}), 40414);

// Fail if collation is specified and is not {locale: 'simple'}.
assert.commandFailedWithCode(
    mongos.adminCommand({reshardCollection: ns, key: {_id: 1}, collation: {locale: 'en_US'}}),
    ErrorCodes.BadValue);

// Succeed when correct locale is provided.
assert.commandWorked(
    mongos.adminCommand({reshardCollection: ns, key: {_id: 1}, collation: {locale: 'simple'}}));

removeAllReshardingCollections();

assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: {_id: 1}}));

// Fail if unique is specified and is true.
assert.commandFailedWithCode(
    mongos.adminCommand({reshardCollection: ns, key: {_id: 1}, unique: true}), ErrorCodes.BadValue);
// Succeed if unique is specified and is false.
assert.commandWorked(mongos.adminCommand({reshardCollection: ns, key: {_id: 1}, unique: false}));

removeAllReshardingCollections();

// Succeed if _presetReshardedChunks is provided and test commands are enabled (default).
assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: {_id: 1}}));
assert.commandWorked(mongos.adminCommand({
    reshardCollection: ns,
    key: {_id: 1},
    _presetReshardedChunks:
        [{recipientShardId: st.shard1.shardName, min: {_id: MinKey}, max: {_id: MaxKey}}]
}));

removeAllReshardingCollections();

assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: {_id: 1}}));

// Fail if both numInitialChunks and _presetReshardedChunks are provided.
assert.commandFailedWithCode(mongos.adminCommand({
    reshardCollection: ns,
    key: {_id: 1},
    unique: false,
    collation: {locale: 'simple'},
    numInitialChunks: 2,
    _presetReshardedChunks: [
        {recipientShardId: st.shard0.shardName, min: {_id: MinKey}, max: {_id: 0}},
        {recipientShardId: st.shard1.shardName, min: {_id: 0}, max: {_id: MaxKey}}
    ]
}),
                             ErrorCodes.BadValue);

// Succeed if all optional fields and numInitialChunks are provided with correct values.
assert.commandWorked(mongos.adminCommand({
    reshardCollection: ns,
    key: {_id: 1},
    unique: false,
    collation: {locale: 'simple'},
    numInitialChunks: 2,
}));

removeAllReshardingCollections();

// Succeed if all optional fields and _presetReshardedChunks are provided with correct values and
// test commands are enabled (default).
assert.commandWorked(mongos.adminCommand({shardCollection: ns, key: {_id: 1}}));
assert.commandWorked(mongos.adminCommand({
    reshardCollection: ns,
    key: {_id: 1},
    unique: false,
    collation: {locale: 'simple'},
    _presetReshardedChunks: [
        {recipientShardId: st.shard1.shardName, min: {_id: 0}, max: {_id: MaxKey}},
        {recipientShardId: st.shard0.shardName, min: {_id: MinKey}, max: {_id: 0}}
    ]
}));

removeAllReshardingCollections();

const existingZoneName = 'x1';

// Fail if authoritative tags exist in config.tags collection and zones are not provided.
assert.commandWorked(
    st.s.adminCommand({addShardToZone: st.shard1.shardName, zone: existingZoneName}));
assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));
assert.commandWorked(st.s.adminCommand(
    {updateZoneKeyRange: ns, min: {_id: 0}, max: {_id: 5}, zone: existingZoneName}));

assert.commandFailedWithCode(mongos.adminCommand({
    reshardCollection: ns,
    key: {_id: 1},
    unique: false,
    collation: {locale: 'simple'},
    numInitialChunks: 2,
}),
                             ErrorCodes.BadValue);

// Fail if authoritative tags exist in config.tags collection and zones are provided and use a name
// which does not exist in authoritative tags.
assert.commandFailedWithCode(mongos.adminCommand({
    reshardCollection: ns,
    key: {_id: 1},
    unique: false,
    collation: {locale: 'simple'},
    zones: [{tag: 'x', min: {_id: 5}, max: {_id: 10}, ns: ns}],
    numInitialChunks: 2,
}),
                             ErrorCodes.BadValue);

// Succeed if authoritative tags exist in config.tags collection and zones are provided and use an
// existing zone's name.
assert.commandWorked(mongos.adminCommand({
    reshardCollection: ns,
    key: {_id: 1},
    unique: false,
    collation: {locale: 'simple'},
    zones: [{tag: existingZoneName, min: {_id: 5}, max: {_id: 10}, ns: ns}],
    _presetReshardedChunks: [
        {recipientShardId: st.shard1.shardName, min: {_id: 0}, max: {_id: MaxKey}},
        {recipientShardId: st.shard0.shardName, min: {_id: MinKey}, max: {_id: 0}}
    ]
}));

removeAllReshardingCollections();

st.stop();
})();