Skip to content

Commit

Permalink
fix(shell-api): expand fallback condition for $collstats MONGOSH-1425 (
Browse files Browse the repository at this point in the history
  • Loading branch information
paula-stacho authored Aug 15, 2024
1 parent ea12bee commit e61b174
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 56 deletions.
129 changes: 74 additions & 55 deletions packages/shell-api/src/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1467,63 +1467,82 @@ describe('Collection', function () {
context(
'when the aggregation fails with error code `13388`',
function () {
beforeEach(function () {
const tryNext = sinon.stub();
const mockError: any = new Error('test error');
mockError.code = 13388;
tryNext.onCall(0).rejects(mockError);
serviceProvider.aggregate.returns({ tryNext } as any);
});

it('runs the deprecated collStats command with the default scale', async function () {
await collection.stats();

expect(
serviceProvider.runCommandWithCheck
).to.have.been.calledWith(database._name, {
collStats: collection._name,
scale: 1,
});
});

it('runs the deprecated collStats command with a custom scale', async function () {
await collection.stats({
scale: 1024, // Scale to kilobytes.
});

expect(
serviceProvider.runCommandWithCheck
).to.have.been.calledWith(database._name, {
collStats: collection._name,
scale: 1024,
});
});

it('runs the deprecated collStats command with the legacy scale parameter', async function () {
await collection.stats(2);

expect(
serviceProvider.runCommandWithCheck
).to.have.been.calledWith(database._name, {
collStats: collection._name,
scale: 2,
});
});

context('when the fallback collStats command fails', function () {
beforeEach(function () {
serviceProvider.runCommandWithCheck.rejects(
new Error('not our error')
for (const mockError of [
{
...new Error('Code 13388'),
code: 13388,
},
{
...new Error('Stale Config'),
codeName: 'StaleConfig',
},
{
...new Error('Failed to Parse'),
codeName: 'FailedToParse',
},
]) {
context(`in case of ${mockError.name} error`, function () {
beforeEach(function () {
const tryNext = sinon.stub();
tryNext.onCall(0).rejects(mockError);
serviceProvider.aggregate.returns({ tryNext } as any);
});

it('runs the deprecated collStats command with the default scale', async function () {
await collection.stats();

expect(
serviceProvider.runCommandWithCheck
).to.have.been.calledWith(database._name, {
collStats: collection._name,
scale: 1,
});
});

it('runs the deprecated collStats command with a custom scale', async function () {
await collection.stats({
scale: 1024, // Scale to kilobytes.
});

expect(
serviceProvider.runCommandWithCheck
).to.have.been.calledWith(database._name, {
collStats: collection._name,
scale: 1024,
});
});

it('runs the deprecated collStats command with the legacy scale parameter', async function () {
await collection.stats(2);

expect(
serviceProvider.runCommandWithCheck
).to.have.been.calledWith(database._name, {
collStats: collection._name,
scale: 2,
});
});

context(
'when the fallback collStats command fails',
function () {
beforeEach(function () {
serviceProvider.runCommandWithCheck.rejects(
new Error('not our error')
);
});

it('surfaces the original aggregation error', async function () {
const error = await collection.stats().catch((e) => e);

expect(serviceProvider.runCommandWithCheck).to.have.been
.called;
expect(error.message).to.equal(mockError.message);
});
}
);
});

it('surfaces the original aggregation error', async function () {
const error = await collection.stats().catch((e) => e);

expect(serviceProvider.runCommandWithCheck).to.have.been.called;
expect(error.message).to.equal('test error');
});
});
}
}
);
});
Expand Down
7 changes: 6 additions & 1 deletion packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1863,9 +1863,14 @@ export default class Collection extends ShellApiWithMongoClass {

return await this._aggregateAndScaleCollStats(collStats, scale);
} catch (e: any) {
if (e?.codeName === 'StaleConfig' || e?.code === 13388) {
if (
e?.codeName === 'StaleConfig' ||
e?.code === 13388 ||
e?.codeName === 'FailedToParse'
) {
// Fallback to the deprecated way of fetching that folks can still
// fetch the stats of sharded timeseries collections. SERVER-72686
// and atlas data federation (MONGOSH-1425)
try {
return await this._getLegacyCollStats(scale);
} catch (legacyCollStatsError) {
Expand Down

0 comments on commit e61b174

Please sign in to comment.