Skip to content

Commit 489ffdb

Browse files
authored
feat(shell-api): add reshard collection helpers MONGOSH-754 (#899)
Integration tests are omitted, because it seems like they would take a fairly long time (see the conversation in the ticket).
1 parent c305532 commit 489ffdb

File tree

3 files changed

+151
-5
lines changed

3 files changed

+151
-5
lines changed

packages/i18n/src/locales/en_US.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,22 @@ const translations: Catalog = {
14741474
shardCollection: {
14751475
link: 'https://docs.mongodb.com/manual/reference/method/sh.shardCollection',
14761476
description: 'Enables sharding for a collection. Uses the shardCollection command',
1477-
example: 'sh.shardCollection(namesapce, key, unique?, options?)',
1477+
example: 'sh.shardCollection(namespace, key, unique?, options?)',
1478+
},
1479+
reshardCollection: {
1480+
link: 'https://docs.mongodb.com/manual/reference/method/sh.reshardCollection',
1481+
description: 'Enables sharding for a collection. Uses the reshardCollection command',
1482+
example: 'sh.reshardCollection(namespace, key, unique?, options?)',
1483+
},
1484+
commitReshardCollection: {
1485+
link: 'https://docs.mongodb.com/manual/reference/method/sh.commitReshardCollection',
1486+
description: 'Commits the current reshardCollection on a given collection',
1487+
example: 'sh.commitReshardCollection(namespace)',
1488+
},
1489+
abortReshardCollection: {
1490+
link: 'https://docs.mongodb.com/manual/reference/method/sh.abortReshardCollection',
1491+
description: 'Abort the current reshardCollection on a given collection',
1492+
example: 'sh.abortReshardCollection(namespace)',
14781493
},
14791494
status: {
14801495
link: 'https://docs.mongodb.com/manual/reference/method/sh.status',

packages/shell-api/src/shard.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,98 @@ describe('Shard', () => {
155155
expect(catchedError).to.equal(expectedError);
156156
});
157157
});
158+
describe('reshardCollection', () => {
159+
it('calls serviceProvider.runCommandWithCheck without optional args', async() => {
160+
await shard.reshardCollection('db.coll', { key: 1 } );
161+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
162+
ADMIN_DB,
163+
{
164+
reshardCollection: 'db.coll',
165+
key: { key: 1 }
166+
}
167+
);
168+
});
169+
170+
it('calls serviceProvider.runCommandWithCheck with optional args', async() => {
171+
await shard.reshardCollection('db.coll', { key: 1 }, true, { option1: 1 });
172+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
173+
ADMIN_DB,
174+
{
175+
reshardCollection: 'db.coll',
176+
key: { key: 1 },
177+
unique: true,
178+
option1: 1
179+
}
180+
);
181+
});
182+
183+
it('returns whatever serviceProvider.runCommandWithCheck returns', async() => {
184+
const expectedResult = { ok: 1 };
185+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
186+
const result = await shard.reshardCollection('db.coll', { key: 1 });
187+
expect(result).to.deep.equal(expectedResult);
188+
});
189+
190+
it('throws if serviceProvider.runCommandWithCheck rejects', async() => {
191+
const expectedError = new Error();
192+
serviceProvider.runCommandWithCheck.rejects(expectedError);
193+
const catchedError = await shard.reshardCollection('db.coll', { key: 1 })
194+
.catch(e => e);
195+
expect(catchedError).to.equal(expectedError);
196+
});
197+
});
198+
describe('commitReshardCollection', () => {
199+
it('calls serviceProvider.runCommandWithCheck', async() => {
200+
await shard.commitReshardCollection('db.coll');
201+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
202+
ADMIN_DB,
203+
{
204+
commitReshardCollection: 'db.coll'
205+
}
206+
);
207+
});
208+
209+
it('returns whatever serviceProvider.runCommandWithCheck returns', async() => {
210+
const expectedResult = { ok: 1 };
211+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
212+
const result = await shard.commitReshardCollection('db.coll');
213+
expect(result).to.deep.equal(expectedResult);
214+
});
215+
216+
it('throws if serviceProvider.runCommandWithCheck rejects', async() => {
217+
const expectedError = new Error();
218+
serviceProvider.runCommandWithCheck.rejects(expectedError);
219+
const catchedError = await shard.commitReshardCollection('db.coll')
220+
.catch(e => e);
221+
expect(catchedError).to.equal(expectedError);
222+
});
223+
});
224+
describe('abortReshardCollection', () => {
225+
it('calls serviceProvider.runCommandWithCheck', async() => {
226+
await shard.abortReshardCollection('db.coll');
227+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
228+
ADMIN_DB,
229+
{
230+
abortReshardCollection: 'db.coll'
231+
}
232+
);
233+
});
234+
235+
it('returns whatever serviceProvider.runCommandWithCheck returns', async() => {
236+
const expectedResult = { ok: 1 };
237+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
238+
const result = await shard.abortReshardCollection('db.coll');
239+
expect(result).to.deep.equal(expectedResult);
240+
});
241+
242+
it('throws if serviceProvider.runCommandWithCheck rejects', async() => {
243+
const expectedError = new Error();
244+
serviceProvider.runCommandWithCheck.rejects(expectedError);
245+
const catchedError = await shard.abortReshardCollection('db.coll')
246+
.catch(e => e);
247+
expect(catchedError).to.equal(expectedError);
248+
});
249+
});
158250
describe('addShard', () => {
159251
it('calls serviceProvider.runCommandWithCheck with arg', async() => {
160252
serviceProvider.runCommandWithCheck.resolves({ ok: 1, msg: 'isdbgrid' });

packages/shell-api/src/shard.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,51 @@ export default class Shard extends ShellApiWithMongoClass {
6868
}
6969

7070
@returnsPromise
71-
async shardCollection(namespace: string, key: Document, unique?: boolean, options?: Document): Promise<Document> {
72-
assertArgsDefinedType([namespace, key, unique, options], ['string', 'object', [undefined, 'boolean'], [undefined, 'object']], 'Shard.shardCollection');
73-
this._emitShardApiCall('shardCollection', { namespace, key, unique, options });
71+
@serverVersions(['5.0.0', ServerVersions.latest])
72+
async commitReshardCollection(namespace: string): Promise<Document> {
73+
assertArgsDefinedType([namespace], ['string'], 'Shard.commitReshardCollection');
74+
this._emitShardApiCall('commitReshardCollection', { namespace });
75+
return await this._database._runAdminCommand({
76+
commitReshardCollection: namespace
77+
});
78+
}
79+
80+
@returnsPromise
81+
@serverVersions(['5.0.0', ServerVersions.latest])
82+
async abortReshardCollection(namespace: string): Promise<Document> {
83+
assertArgsDefinedType([namespace], ['string'], 'Shard.abortReshardCollection');
84+
this._emitShardApiCall('abortReshardCollection', { namespace });
85+
return await this._database._runAdminCommand({
86+
abortReshardCollection: namespace
87+
});
88+
}
89+
90+
@returnsPromise
91+
async shardCollection(namespace: string, key: Document, unique?: boolean | Document, options?: Document): Promise<Document> {
92+
return await this._runShardCollection('shardCollection', namespace, key, unique, options);
93+
}
94+
95+
@returnsPromise
96+
@serverVersions(['5.0.0', ServerVersions.latest])
97+
async reshardCollection(namespace: string, key: Document, unique?: boolean | Document, options?: Document): Promise<Document> {
98+
return await this._runShardCollection('reshardCollection', namespace, key, unique, options);
99+
}
100+
101+
async _runShardCollection(
102+
command: 'shardCollection' | 'reshardCollection',
103+
namespace: string,
104+
key: Document,
105+
unique?: boolean | Document,
106+
options?: Document): Promise<Document> {
107+
assertArgsDefinedType([namespace, key, unique, options], ['string', 'object', [undefined, 'boolean', 'object'], [undefined, 'object']], `Shard.${command}`);
108+
this._emitShardApiCall(command, { namespace, key, unique, options });
109+
if (typeof unique === 'object' && unique !== null) {
110+
options = unique;
111+
unique = undefined;
112+
}
74113

75114
const cmd = {
76-
shardCollection: namespace,
115+
[command]: namespace,
77116
key: key
78117
} as any;
79118
if (unique !== undefined) {

0 commit comments

Comments
 (0)