Skip to content

Commit

Permalink
fix: default to server-defined numInitialChunks MONGOSH-1949 (#2381)
Browse files Browse the repository at this point in the history
fix: default to server-defined numInitialChunks in shardAndDistributeCollection
  • Loading branch information
nirinchev authored Feb 24, 2025
1 parent 30203ee commit 30ec9a9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
77 changes: 76 additions & 1 deletion packages/shell-api/src/shard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ describe('Shard', function () {
expect(reshardCollectionStub.firstCall.args).to.deep.equal([
'db.coll',
{ key: 1 },
{ numInitialChunks: 1000, forceRedistribution: true },
{ forceRedistribution: true },
]);
});

Expand Down Expand Up @@ -1860,6 +1860,81 @@ describe('Shard', function () {
{ numInitialChunks: 1, forceRedistribution: true },
]);
});

it('allows user to pass collation', 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, {
collation: { locale: 'simple' },
});

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

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

it('allows user to pass shard-specific options and ignores them when resharding', 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, {
presplitHashedZones: true,
timeseries: {
timeField: 'ts',
},
});

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

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

it('returns whatever shard.reshardCollection returns', async function () {
const expectedResult = { ok: 1 };
sinon.stub(shard, 'reshardCollection').resolves(expectedResult);
Expand Down
26 changes: 17 additions & 9 deletions packages/shell-api/src/shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,16 +710,24 @@ export default class Shard extends ShellApiWithMongoClass {
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,

if (typeof unique === 'object') {
options = unique;
}

const reshardOptions: Document = {
forceRedistribution: true,
});
};

if (options?.numInitialChunks !== undefined) {
reshardOptions.numInitialChunks = options.numInitialChunks;
}

if (options?.collation !== undefined) {
reshardOptions.collation = options.collation;
}

return await this.reshardCollection(ns, key, reshardOptions);
}

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

0 comments on commit 30ec9a9

Please sign in to comment.