Skip to content

Commit

Permalink
feat(shell-api): add shard moveCollection and unshardCollection helpers
Browse files Browse the repository at this point in the history
COMPASS-7500 (#2092)

* add helpers

* cr fix
  • Loading branch information
mabaasit authored Jul 26, 2024
1 parent 3252c96 commit 5516373
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,30 @@ const translations: Catalog = {
'Returns a cursor with information about metadata inconsistencies',
example: 'sh.checkMetadataConsistency(<options>)',
},
moveCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.moveCollection',
description:
'Moves a single unsharded collection to a different shard.',
example: 'sh.moveCollection(ns, toShard)',
},
abortMoveCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.abortMoveCollection',
description:
'Abort the current moveCollection operation on a given collection',
example: 'sh.abortMoveCollection(ns)',
},
unshardCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.unshardCollection',
description:
'Unshard the given collection and move all data to the given shard.',
example: 'sh.unshardCollection(ns, toShard)',
},
abortUnshardCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.abortUnshardCollection',
description:
'Abort the current unshardCollection operation on a given collection',
example: 'sh.abortUnshardCollection(ns)',
},
},
},
},
Expand Down
114 changes: 114 additions & 0 deletions packages/shell-api/src/shard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,120 @@ describe('Shard', function () {
);
});
});

describe('moveCollection', function () {
it('calls serviceProvider.runCommandWithCheck', async function () {
await shard.moveCollection('db.coll', 'shard1');
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
moveCollection: 'db.coll',
toShard: 'shard1',
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
const expectedResult = { ok: 1 };
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.moveCollection('db.coll', 'shard1');
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
const expectedError = new Error();
serviceProvider.runCommandWithCheck.rejects(expectedError);
const caughtError = await shard
.moveCollection('db.coll', 'shard1')
.catch((e) => e);
expect(caughtError).to.equal(expectedError);
});
});

describe('abortMoveCollection', function () {
it('calls serviceProvider.runCommandWithCheck', async function () {
await shard.abortMoveCollection('db.coll');
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
abortMoveCollection: 'db.coll',
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
const expectedResult = { ok: 1 };
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.abortMoveCollection('db.coll');
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
const expectedError = new Error();
serviceProvider.runCommandWithCheck.rejects(expectedError);
const caughtError = await shard
.abortMoveCollection('db.coll')
.catch((e) => e);
expect(caughtError).to.equal(expectedError);
});
});

describe('unshardCollection', function () {
it('calls serviceProvider.runCommandWithCheck', async function () {
await shard.unshardCollection('db.coll', 'shard1');
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
unshardCollection: 'db.coll',
toShard: 'shard1',
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
const expectedResult = { ok: 1 };
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.unshardCollection('db.coll', 'shard1');
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
const expectedError = new Error();
serviceProvider.runCommandWithCheck.rejects(expectedError);
const caughtError = await shard
.unshardCollection('db.coll', 'shard1')
.catch((e) => e);
expect(caughtError).to.equal(expectedError);
});
});

describe('abortUnshardCollection', function () {
it('calls serviceProvider.runCommandWithCheck', async function () {
await shard.abortUnshardCollection('db.coll');
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
abortUnshardCollection: 'db.coll',
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
const expectedResult = { ok: 1 };
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.abortUnshardCollection('db.coll');
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
const expectedError = new Error();
serviceProvider.runCommandWithCheck.rejects(expectedError);
const caughtError = await shard
.abortUnshardCollection('db.coll')
.catch((e) => e);
expect(caughtError).to.equal(expectedError);
});
});
});

describe('integration', function () {
Expand Down
57 changes: 57 additions & 0 deletions packages/shell-api/src/shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,61 @@ export default class Shard extends ShellApiWithMongoClass {
checkMetadataConsistency: 1,
});
}

@serverVersions(['8.0.0', ServerVersions.latest])
@apiVersions([])
@returnsPromise
async moveCollection(ns: string, toShard: string): Promise<Document> {
assertArgsDefinedType(
[ns, toShard],
['string', 'string'],
'Shard.moveCollection'
);
this._emitShardApiCall('moveCollection', { moveCollection: ns, toShard });
return await this._database._runAdminCommand({
moveCollection: ns,
toShard,
});
}

@serverVersions(['8.0.0', ServerVersions.latest])
@apiVersions([])
@returnsPromise
async abortMoveCollection(ns: string): Promise<Document> {
assertArgsDefinedType([ns], ['string'], 'Shard.abortMoveCollection');
this._emitShardApiCall('abortMoveCollection', { abortMoveCollection: ns });
return await this._database._runAdminCommand({ abortMoveCollection: ns });
}

@serverVersions(['8.0.0', ServerVersions.latest])
@apiVersions([])
@returnsPromise
async unshardCollection(ns: string, toShard: string): Promise<Document> {
assertArgsDefinedType(
[ns, toShard],
['string', 'string'],
'Shard.unshardCollection'
);
this._emitShardApiCall('unshardCollection', {
unshardCollection: ns,
toShard,
});
return await this._database._runAdminCommand({
unshardCollection: ns,
toShard,
});
}

@serverVersions(['8.0.0', ServerVersions.latest])
@apiVersions([])
@returnsPromise
async abortUnshardCollection(ns: string): Promise<Document> {
assertArgsDefinedType([ns], ['string'], 'Shard.abortUnshardCollection');
this._emitShardApiCall('abortUnshardCollection', {
abortUnshardCollection: ns,
});
return await this._database._runAdminCommand({
abortUnshardCollection: ns,
});
}
}

0 comments on commit 5516373

Please sign in to comment.