Skip to content

Commit 13ae8de

Browse files
authored
feat(shell-api): added a new shell helper 'getShardedDataInformation' MONGOSH-1254 (#1334)
1 parent b6aad4d commit 13ae8de

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

packages/i18n/src/locales/en_US.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,10 @@ const translations: Catalog = {
16351635
link: 'https://docs.mongodb.com/manual/reference/method/sh.setBalancerState',
16361636
description: 'Calls sh.startBalancer if state is true, otherwise calls sh.stopBalancer',
16371637
example: 'sh.setBalancerState(state)',
1638+
},
1639+
getShardedDataDistribution: {
1640+
description: 'Returns data-size distribution information for all existing sharded collections',
1641+
example: 'sh.getShardedDataDistribution()',
16381642
}
16391643
}
16401644
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Shard from './shard';
44
import { ADMIN_DB, ALL_PLATFORMS, ALL_SERVER_VERSIONS, ALL_TOPOLOGIES } from './enums';
55
import { signatures, toShellResult } from './index';
66
import Mongo from './mongo';
7-
import { bson, ServiceProvider, FindCursor as ServiceProviderCursor } from '@mongosh/service-provider-core';
7+
import { bson, ServiceProvider, FindCursor as ServiceProviderCursor, AggregationCursor as ServiceProviderAggCursor } from '@mongosh/service-provider-core';
88
import { EventEmitter } from 'events';
99
import ShellInstanceState from './shell-instance-state';
1010
import { UpdateResult } from './result';
@@ -1184,6 +1184,21 @@ describe('Shard', () => {
11841184
expect(caughtError).to.equal(expectedError);
11851185
});
11861186
});
1187+
describe('getShardedDataDistribution', () => {
1188+
it('throws if aggregateDb fails', async() => {
1189+
serviceProvider.aggregateDb.throws(new Error('err'));
1190+
const error: any = await shard.getShardedDataDistribution().catch(err => err);
1191+
expect(error.message).to.be.equal('err');
1192+
});
1193+
1194+
it('throws if not mongos', async() => {
1195+
const serviceProviderCursor = stubInterface<ServiceProviderAggCursor>();
1196+
serviceProvider.aggregateDb.returns(serviceProviderCursor as any);
1197+
serviceProviderCursor.hasNext.throws(Object.assign(new Error(), { code: 40324 }));
1198+
const error: any = await shard.getShardedDataDistribution().catch(err => err);
1199+
expect(error.message).to.match(/sh\.getShardedDataDistribution only works on mongos/);
1200+
});
1201+
});
11871202
});
11881203

11891204
describe('integration', () => {

packages/shell-api/src/shard.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import Database from './database';
22
import {
33
shellApiClassDefault,
4-
returnsPromise, serverVersions, apiVersions, ShellApiWithMongoClass
4+
returnsPromise, serverVersions, apiVersions, ShellApiWithMongoClass, returnType
55
} from './decorators';
66

77
import type { Document } from '@mongosh/service-provider-core';
88
import { assertArgsDefinedType, getConfigDB, getPrintableShardStatus } from './helpers';
99
import { ServerVersions, asPrintable } from './enums';
1010
import { CommandResult, UpdateResult } from './result';
1111
import { redactURICredentials } from '@mongosh/history';
12+
import { CommonErrors, MongoshRuntimeError } from '@mongosh/errors';
1213
import Mongo from './mongo';
14+
import AggregationCursor from './aggregation-cursor';
1315

1416
@shellApiClassDefault
1517
export default class Shard extends ShellApiWithMongoClass {
@@ -424,4 +426,29 @@ export default class Shard extends ShellApiWithMongoClass {
424426
}
425427
return this.stopBalancer();
426428
}
429+
430+
@returnsPromise
431+
@apiVersions([])
432+
@serverVersions(['6.1.0', ServerVersions.latest])
433+
@returnType('AggregationCursor')
434+
async getShardedDataDistribution(options = {}): Promise<AggregationCursor> {
435+
this._emitShardApiCall('getShardedDataDistribution', {});
436+
437+
const cursor = await this._database.getSiblingDB('admin').aggregate([{ $shardedDataDistribution: options }]);
438+
439+
try {
440+
await cursor.hasNext();
441+
} catch (err: any) {
442+
if (err.code?.valueOf() === 40324) { // unrecognized stage error
443+
throw new MongoshRuntimeError(
444+
'sh.getShardedDataDistribution only works on mongos',
445+
CommonErrors.CommandFailed
446+
);
447+
}
448+
449+
throw err;
450+
}
451+
452+
return cursor;
453+
}
427454
}

0 commit comments

Comments
 (0)