Skip to content

Commit

Permalink
chore(shell-api): unified shard distribution helper MONGOSH-1694 (#2098)
Browse files Browse the repository at this point in the history
* unified shard helper

* allow user option to pass through

* fix i8n test
  • Loading branch information
mabaasit authored Jul 29, 2024
1 parent 69f8e9c commit 235c047
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
6 changes: 6 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,12 @@ const translations: Catalog = {
'Returns a cursor with information about metadata inconsistencies',
example: 'sh.checkMetadataConsistency(<options>)',
},
shardAndDistributeCollection: {
description:
'Shards a collection and then immediately reshards the collection to the same shard key.',
example:
'sh.shardAndDistributeCollection(ns, key, unique?, options?)',
},
moveCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.moveCollection',
description:
Expand Down
79 changes: 79 additions & 0 deletions packages/shell-api/src/shard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,85 @@ describe('Shard', function () {
});
});

describe('shardAndDistributeCollection', function () {
it('calls shardCollection and then reshardCollection with correct parameters', async function () {
const expectedResult = { ok: 1 };

const shardCollectionStub = sinon
.stub(shard, 'shardCollection')
.resolves(expectedResult);
const reshardCollectionStub = sinon
.stub(shard, 'reshardCollection')
.resolves(expectedResult);

await shard.shardAndDistributeCollection(
'db.coll',
{ key: 1 },
true,
{}
);

expect(shardCollectionStub.calledOnce).to.equal(true);
expect(shardCollectionStub.firstCall.args).to.deep.equal([
'db.coll',
{
key: 1,
},
true,
{},
]);

expect(reshardCollectionStub.calledOnce).to.equal(true);
expect(reshardCollectionStub.firstCall.args).to.deep.equal([
'db.coll',
{ key: 1 },
{ numInitialChunks: 1000, forceRedistribution: true },
]);
});

it('allows user to pass numInitialChunks', async function () {
const expectedResult = { ok: 1 };

const shardCollectionStub = sinon
.stub(shard, 'shardCollection')
.resolves(expectedResult);
const reshardCollectionStub = sinon
.stub(shard, 'reshardCollection')
.resolves(expectedResult);

await shard.shardAndDistributeCollection('db.coll', { key: 1 }, true, {
numInitialChunks: 1,
});

expect(shardCollectionStub.calledOnce).to.equal(true);
expect(shardCollectionStub.firstCall.args).to.deep.equal([
'db.coll',
{
key: 1,
},
true,
{
numInitialChunks: 1,
},
]);

expect(reshardCollectionStub.calledOnce).to.equal(true);
expect(reshardCollectionStub.firstCall.args).to.deep.equal([
'db.coll',
{ key: 1 },
{ numInitialChunks: 1, forceRedistribution: true },
]);
});
it('returns whatever shard.reshardCollection returns', async function () {
const expectedResult = { ok: 1 };
sinon.stub(shard, 'reshardCollection').resolves(expectedResult);
const result = await shard.shardAndDistributeCollection('db.coll', {
key: 1,
});
expect(result).to.deep.equal(expectedResult);
});
});

describe('moveCollection', function () {
it('calls serviceProvider.runCommandWithCheck', async function () {
await shard.moveCollection('db.coll', 'shard1');
Expand Down
27 changes: 27 additions & 0 deletions packages/shell-api/src/shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,33 @@ export default class Shard extends ShellApiWithMongoClass {
});
}

@returnsPromise
@serverVersions(['5.0.0', ServerVersions.latest])
async shardAndDistributeCollection(
ns: string,
key: Document,
unique?: boolean | Document,
options?: Document
): Promise<Document> {
this._emitShardApiCall('shardAndDistributeCollection', {
ns,
key,
unique,
options,
});
await this.shardCollection(ns, key, unique, options);
// SERVER-92762: Prevent unequal data distribution by setting
// numInitialChunks to 1000.
const numInitialChunks =
typeof unique === 'object'
? unique.numInitialChunks
: options?.numInitialChunks;
return await this.reshardCollection(ns, key, {
numInitialChunks: numInitialChunks ?? 1000,
forceRedistribution: true,
});
}

@serverVersions(['8.0.0', ServerVersions.latest])
@apiVersions([])
@returnsPromise
Expand Down

0 comments on commit 235c047

Please sign in to comment.