From 916ed8e152acf80c43c30efdab28cf417cd3c3da Mon Sep 17 00:00:00 2001 From: Pol Pinol Castuera Date: Tue, 2 Aug 2022 12:28:49 +0000 Subject: [PATCH 1/3] feat(shell-api): added a new shell helper 'getShardedDataInformation' MONGOSH-1254 --- packages/i18n/src/locales/en_US.ts | 4 ++++ packages/shell-api/src/shard.spec.ts | 17 +++++++++++++++- packages/shell-api/src/shard.ts | 29 +++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/i18n/src/locales/en_US.ts b/packages/i18n/src/locales/en_US.ts index e950eefc5b..8dbcf68c63 100644 --- a/packages/i18n/src/locales/en_US.ts +++ b/packages/i18n/src/locales/en_US.ts @@ -1634,6 +1634,10 @@ const translations: Catalog = { link: 'https://docs.mongodb.com/manual/reference/method/sh.setBalancerState', description: 'Calls sh.startBalancer if state is true, otherwise calls sh.stopBalancer', example: 'sh.setBalancerState(state)', + }, + getShardedDataDistribution: { + description: 'Returns data-size distribution information for all existing sharded collections', + example: 'sh.getShardedDataDistribution()', } } } diff --git a/packages/shell-api/src/shard.spec.ts b/packages/shell-api/src/shard.spec.ts index 34d3ca0117..cf55e9b5fd 100644 --- a/packages/shell-api/src/shard.spec.ts +++ b/packages/shell-api/src/shard.spec.ts @@ -4,7 +4,7 @@ import Shard from './shard'; import { ADMIN_DB, ALL_PLATFORMS, ALL_SERVER_VERSIONS, ALL_TOPOLOGIES } from './enums'; import { signatures, toShellResult } from './index'; import Mongo from './mongo'; -import { bson, ServiceProvider, FindCursor as ServiceProviderCursor } from '@mongosh/service-provider-core'; +import { bson, ServiceProvider, FindCursor as ServiceProviderCursor, AggregationCursor as ServiceProviderAggCursor } from '@mongosh/service-provider-core'; import { EventEmitter } from 'events'; import ShellInstanceState from './shell-instance-state'; import { UpdateResult } from './result'; @@ -1184,6 +1184,21 @@ describe('Shard', () => { expect(caughtError).to.equal(expectedError); }); }); + describe('getShardedDataDistribution', () => { + it('throws if aggregateDb fails', async() => { + serviceProvider.aggregateDb.throws(new Error('err')); + const error: any = await shard.getShardedDataDistribution().catch(err => err); + expect(error.message).to.be.equal('err'); + }); + + it('throws if not mongos', async() => { + const serviceProviderCursor = stubInterface(); + serviceProvider.aggregateDb.returns(serviceProviderCursor as any); + serviceProviderCursor.hasNext.throws(Object.assign(new Error(), { code: 40324 })); + const error: any = await shard.getShardedDataDistribution().catch(err => err); + expect(error.message).to.match(/sh\.getShardedDataDistribution only works on mongos/); + }); + }); }); describe('integration', () => { diff --git a/packages/shell-api/src/shard.ts b/packages/shell-api/src/shard.ts index 163e3c21ee..4875ea60f8 100644 --- a/packages/shell-api/src/shard.ts +++ b/packages/shell-api/src/shard.ts @@ -1,7 +1,7 @@ import Database from './database'; import { shellApiClassDefault, - returnsPromise, serverVersions, apiVersions, ShellApiWithMongoClass + returnsPromise, serverVersions, apiVersions, ShellApiWithMongoClass, returnType } from './decorators'; import type { Document } from '@mongosh/service-provider-core'; @@ -9,7 +9,9 @@ import { assertArgsDefinedType, getConfigDB, getPrintableShardStatus } from './h import { ServerVersions, asPrintable } from './enums'; import { CommandResult, UpdateResult } from './result'; import { redactURICredentials } from '@mongosh/history'; +import { CommonErrors, MongoshRuntimeError } from '@mongosh/errors'; import Mongo from './mongo'; +import AggregationCursor from './aggregation-cursor'; @shellApiClassDefault export default class Shard extends ShellApiWithMongoClass { @@ -424,4 +426,29 @@ export default class Shard extends ShellApiWithMongoClass { } return this.stopBalancer(); } + + @returnsPromise + @apiVersions([]) + @serverVersions(['6.1.0', ServerVersions.latest]) + @returnType('AggregationCursor') + async getShardedDataDistribution(): Promise { + this._emitShardApiCall('getShardedDataDistribution', {}); + + const cursor = await this._database.aggregate([{ $shardedDataDistribution: {} }]); + + try { + await cursor.hasNext(); + } catch (err: any) { + if (err.code?.valueOf() === 40324) { // unrecognized stage error + throw new MongoshRuntimeError( + 'sh.getShardedDataDistribution only works on mongos', + CommonErrors.CommandFailed + ); + } + + throw err; + } + + return cursor; + } } From 8225ebbfd2d7974196010fe56fa5e56011bd1763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Pi=C3=B1ol=20Castuera?= <67922619+PolPinol@users.noreply.github.com> Date: Mon, 5 Sep 2022 15:44:41 +0200 Subject: [PATCH 2/3] Update packages/shell-api/src/shard.ts Co-authored-by: Anna Henningsen --- packages/shell-api/src/shard.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shell-api/src/shard.ts b/packages/shell-api/src/shard.ts index 4875ea60f8..e654ed1c93 100644 --- a/packages/shell-api/src/shard.ts +++ b/packages/shell-api/src/shard.ts @@ -431,10 +431,10 @@ export default class Shard extends ShellApiWithMongoClass { @apiVersions([]) @serverVersions(['6.1.0', ServerVersions.latest]) @returnType('AggregationCursor') - async getShardedDataDistribution(): Promise { + async getShardedDataDistribution(options = {}): Promise { this._emitShardApiCall('getShardedDataDistribution', {}); - const cursor = await this._database.aggregate([{ $shardedDataDistribution: {} }]); + const cursor = await this._database.aggregate([{ $shardedDataDistribution: options }]); try { await cursor.hasNext(); From a84ce463881166c1f2f51453681bd3a3a4dd3728 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Pi=C3=B1ol=20Castuera?= <67922619+PolPinol@users.noreply.github.com> Date: Mon, 5 Sep 2022 15:53:19 +0200 Subject: [PATCH 3/3] Update packages/shell-api/src/shard.ts Co-authored-by: Anna Henningsen --- packages/shell-api/src/shard.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shell-api/src/shard.ts b/packages/shell-api/src/shard.ts index e654ed1c93..122589c059 100644 --- a/packages/shell-api/src/shard.ts +++ b/packages/shell-api/src/shard.ts @@ -434,7 +434,7 @@ export default class Shard extends ShellApiWithMongoClass { async getShardedDataDistribution(options = {}): Promise { this._emitShardApiCall('getShardedDataDistribution', {}); - const cursor = await this._database.aggregate([{ $shardedDataDistribution: options }]); + const cursor = await this._database.getSiblingDB('admin').aggregate([{ $shardedDataDistribution: options }]); try { await cursor.hasNext();