From 2d38d38e76fb0d35521de414e9b749436f9b409d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 18 Jul 2024 12:51:21 +0200 Subject: [PATCH] fix(shell-api): apply readPreference to admin read commands MONGOSH-1837 (#2084) This mirrors 949c2b5ee87, apart from applying corresponding changes to `_runAdminCommand()` instead of `_runCommand()`. --- packages/shell-api/src/collection.ts | 4 ++-- packages/shell-api/src/database.ts | 32 +++++++++++++++------------ packages/shell-api/src/replica-set.ts | 4 ++-- packages/shell-api/src/shard.ts | 6 ++--- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/shell-api/src/collection.ts b/packages/shell-api/src/collection.ts index 38dbc3861..4603cc81b 100644 --- a/packages/shell-api/src/collection.ts +++ b/packages/shell-api/src/collection.ts @@ -2054,7 +2054,7 @@ export default class Collection extends ShellApiWithMongoClass { @apiVersions([]) async getShardVersion(): Promise { this._emitCollectionApiCall('getShardVersion', {}); - return await this._database._runAdminCommand({ + return await this._database._runAdminReadCommand({ getShardVersion: `${this._database._name}.${this._name}`, }); } @@ -2258,7 +2258,7 @@ export default class Collection extends ShellApiWithMongoClass { ): Promise { assertArgsDefinedType([key], [true], 'Collection.analyzeShardKey'); this._emitCollectionApiCall('analyzeShardKey', { key }); - return await this._database._runAdminCommand({ + return await this._database._runAdminReadCommand({ analyzeShardKey: this.getFullName(), key, ...options, diff --git a/packages/shell-api/src/database.ts b/packages/shell-api/src/database.ts index 9f0f94373..58751a3a0 100644 --- a/packages/shell-api/src/database.ts +++ b/packages/shell-api/src/database.ts @@ -185,10 +185,14 @@ export default class Database extends ShellApiWithMongoClass { cmd: Document, options: CommandOperationOptions = {} ): Promise { - return this.getSiblingDB('admin')._runCommand(cmd, { - ...(await this._baseOptions()), - ...options, - }); + return this.getSiblingDB('admin')._runCommand(cmd, options); + } + + public async _runAdminReadCommand( + cmd: Document, + options: CommandOperationOptions = {} + ): Promise { + return this.getSiblingDB('admin')._runReadCommand(cmd, options); } public async _runCursorCommand( @@ -1126,10 +1130,10 @@ export default class Database extends ShellApiWithMongoClass { } @returnsPromise - @apiVersions([]) // TODO: Update this after https://jira.mongodb.org/browse/SPM-2327 + @apiVersions([]) async version(): Promise { this._emitDatabaseApiCall('version', {}); - const info: Document = await this._runAdminCommand({ + const info: Document = await this._runAdminReadCommand({ buildInfo: 1, }); if (!info || info.version === undefined) { @@ -1144,10 +1148,10 @@ export default class Database extends ShellApiWithMongoClass { } @returnsPromise - @apiVersions([]) // TODO: Maybe update this after https://jira.mongodb.org/browse/SPM-2327 + @apiVersions([]) async serverBits(): Promise { this._emitDatabaseApiCall('serverBits', {}); - const info: Document = await this._runAdminCommand({ + const info: Document = await this._runAdminReadCommand({ buildInfo: 1, }); if (!info || info.bits === undefined) { @@ -1197,7 +1201,7 @@ export default class Database extends ShellApiWithMongoClass { @apiVersions([]) async serverBuildInfo(): Promise { this._emitDatabaseApiCall('serverBuildInfo', {}); - return await this._runAdminCommand({ + return await this._runAdminReadCommand({ buildInfo: 1, }); } @@ -1206,7 +1210,7 @@ export default class Database extends ShellApiWithMongoClass { @apiVersions([]) async serverStatus(opts = {}): Promise { this._emitDatabaseApiCall('serverStatus', { options: opts }); - return await this._runAdminCommand({ + return await this._runAdminReadCommand({ serverStatus: 1, ...opts, }); @@ -1235,7 +1239,7 @@ export default class Database extends ShellApiWithMongoClass { @apiVersions([]) async hostInfo(): Promise { this._emitDatabaseApiCall('hostInfo', {}); - return await this._runAdminCommand({ + return await this._runAdminReadCommand({ hostInfo: 1, }); } @@ -1244,7 +1248,7 @@ export default class Database extends ShellApiWithMongoClass { @apiVersions([]) async serverCmdLineOpts(): Promise { this._emitDatabaseApiCall('serverCmdLineOpts', {}); - return await this._runAdminCommand({ + return await this._runAdminReadCommand({ getCmdLineOpts: 1, }); } @@ -1353,7 +1357,7 @@ export default class Database extends ShellApiWithMongoClass { this._emitDatabaseApiCall('getLogComponents', {}); const cmdObj = { getParameter: 1, logComponentVerbosity: 1 }; - const result = await this._runAdminCommand(cmdObj); + const result = await this._runAdminReadCommand(cmdObj); if (!result || result.logComponentVerbosity === undefined) { throw new MongoshRuntimeError( `Error running command ${result ? result.errmsg || '' : ''}`, @@ -1493,7 +1497,7 @@ export default class Database extends ShellApiWithMongoClass { if ( (await local.getCollection('system.replset').countDocuments({})) !== 0 ) { - const status = await this._runAdminCommand({ replSetGetStatus: 1 }); + const status = await this._runAdminReadCommand({ replSetGetStatus: 1 }); // get primary let primary = null; for (const member of status.members) { diff --git a/packages/shell-api/src/replica-set.ts b/packages/shell-api/src/replica-set.ts index f510a2bd1..cb0b0872d 100644 --- a/packages/shell-api/src/replica-set.ts +++ b/packages/shell-api/src/replica-set.ts @@ -60,7 +60,7 @@ export default class ReplicaSet extends ShellApiWithMongoClass { async _getConfig(): Promise { try { - const result = await this._database._runAdminCommand({ + const result = await this._database._runAdminReadCommand({ replSetGetConfig: 1, }); if (result.config === undefined) { @@ -242,7 +242,7 @@ export default class ReplicaSet extends ShellApiWithMongoClass { @apiVersions([]) async status(): Promise { this._emitReplicaSetApiCall('status', {}); - return this._database._runAdminCommand({ + return this._database._runAdminReadCommand({ replSetGetStatus: 1, }); } diff --git a/packages/shell-api/src/shard.ts b/packages/shell-api/src/shard.ts index 9ee7ffa1f..9a700c9ff 100644 --- a/packages/shell-api/src/shard.ts +++ b/packages/shell-api/src/shard.ts @@ -486,7 +486,7 @@ export default class Shard extends ShellApiWithMongoClass { async balancerCollectionStatus(ns: string): Promise { assertArgsDefinedType([ns], ['string'], 'Shard.balancerCollectionStatus'); this._emitShardApiCall('balancerCollectionStatus', { ns }); - return this._database._runAdminCommand({ + return this._database._runAdminReadCommand({ balancerCollectionStatus: ns, }); } @@ -540,7 +540,7 @@ export default class Shard extends ShellApiWithMongoClass { async isBalancerRunning(): Promise { this._emitShardApiCall('isBalancerRunning', {}); await getConfigDB(this._database); - return this._database._runAdminCommand({ + return this._database._runAdminReadCommand({ balancerStatus: 1, }); } @@ -550,7 +550,7 @@ export default class Shard extends ShellApiWithMongoClass { async startBalancer(timeout = 60000): Promise { assertArgsDefinedType([timeout], ['number'], 'Shard.startBalancer'); this._emitShardApiCall('startBalancer', { timeout }); - return this._database._runAdminCommand({ + return this._database._runAdminReadCommand({ balancerStart: 1, maxTimeMS: timeout, });