Skip to content

Commit 30ec9a9

Browse files
authored
fix: default to server-defined numInitialChunks MONGOSH-1949 (#2381)
fix: default to server-defined numInitialChunks in shardAndDistributeCollection
1 parent 30203ee commit 30ec9a9

File tree

2 files changed

+93
-10
lines changed

2 files changed

+93
-10
lines changed

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ describe('Shard', function () {
18231823
expect(reshardCollectionStub.firstCall.args).to.deep.equal([
18241824
'db.coll',
18251825
{ key: 1 },
1826-
{ numInitialChunks: 1000, forceRedistribution: true },
1826+
{ forceRedistribution: true },
18271827
]);
18281828
});
18291829

@@ -1860,6 +1860,81 @@ describe('Shard', function () {
18601860
{ numInitialChunks: 1, forceRedistribution: true },
18611861
]);
18621862
});
1863+
1864+
it('allows user to pass collation', async function () {
1865+
const expectedResult = { ok: 1 };
1866+
1867+
const shardCollectionStub = sinon
1868+
.stub(shard, 'shardCollection')
1869+
.resolves(expectedResult);
1870+
const reshardCollectionStub = sinon
1871+
.stub(shard, 'reshardCollection')
1872+
.resolves(expectedResult);
1873+
1874+
await shard.shardAndDistributeCollection('db.coll', { key: 1 }, true, {
1875+
collation: { locale: 'simple' },
1876+
});
1877+
1878+
expect(shardCollectionStub.calledOnce).to.equal(true);
1879+
expect(shardCollectionStub.firstCall.args).to.deep.equal([
1880+
'db.coll',
1881+
{
1882+
key: 1,
1883+
},
1884+
true,
1885+
{
1886+
collation: { locale: 'simple' },
1887+
},
1888+
]);
1889+
1890+
expect(reshardCollectionStub.calledOnce).to.equal(true);
1891+
expect(reshardCollectionStub.firstCall.args).to.deep.equal([
1892+
'db.coll',
1893+
{ key: 1 },
1894+
{ collation: { locale: 'simple' }, forceRedistribution: true },
1895+
]);
1896+
});
1897+
1898+
it('allows user to pass shard-specific options and ignores them when resharding', async function () {
1899+
const expectedResult = { ok: 1 };
1900+
1901+
const shardCollectionStub = sinon
1902+
.stub(shard, 'shardCollection')
1903+
.resolves(expectedResult);
1904+
const reshardCollectionStub = sinon
1905+
.stub(shard, 'reshardCollection')
1906+
.resolves(expectedResult);
1907+
1908+
await shard.shardAndDistributeCollection('db.coll', { key: 1 }, true, {
1909+
presplitHashedZones: true,
1910+
timeseries: {
1911+
timeField: 'ts',
1912+
},
1913+
});
1914+
1915+
expect(shardCollectionStub.calledOnce).to.equal(true);
1916+
expect(shardCollectionStub.firstCall.args).to.deep.equal([
1917+
'db.coll',
1918+
{
1919+
key: 1,
1920+
},
1921+
true,
1922+
{
1923+
presplitHashedZones: true,
1924+
timeseries: {
1925+
timeField: 'ts',
1926+
},
1927+
},
1928+
]);
1929+
1930+
expect(reshardCollectionStub.calledOnce).to.equal(true);
1931+
expect(reshardCollectionStub.firstCall.args).to.deep.equal([
1932+
'db.coll',
1933+
{ key: 1 },
1934+
{ forceRedistribution: true },
1935+
]);
1936+
});
1937+
18631938
it('returns whatever shard.reshardCollection returns', async function () {
18641939
const expectedResult = { ok: 1 };
18651940
sinon.stub(shard, 'reshardCollection').resolves(expectedResult);

packages/shell-api/src/shard.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -710,16 +710,24 @@ export default class Shard extends ShellApiWithMongoClass {
710710
options,
711711
});
712712
await this.shardCollection(ns, key, unique, options);
713-
// SERVER-92762: Prevent unequal data distribution by setting
714-
// numInitialChunks to 1000.
715-
const numInitialChunks =
716-
typeof unique === 'object'
717-
? unique.numInitialChunks
718-
: options?.numInitialChunks;
719-
return await this.reshardCollection(ns, key, {
720-
numInitialChunks: numInitialChunks ?? 1000,
713+
714+
if (typeof unique === 'object') {
715+
options = unique;
716+
}
717+
718+
const reshardOptions: Document = {
721719
forceRedistribution: true,
722-
});
720+
};
721+
722+
if (options?.numInitialChunks !== undefined) {
723+
reshardOptions.numInitialChunks = options.numInitialChunks;
724+
}
725+
726+
if (options?.collation !== undefined) {
727+
reshardOptions.collation = options.collation;
728+
}
729+
730+
return await this.reshardCollection(ns, key, reshardOptions);
723731
}
724732

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

0 commit comments

Comments
 (0)