Skip to content

Commit

Permalink
fix(shell-api): apply readPreference to admin read commands MONGOSH-1837
Browse files Browse the repository at this point in the history
 (#2084)

This mirrors 949c2b5, apart from applying corresponding changes to
`_runAdminCommand()` instead of `_runCommand()`.
  • Loading branch information
addaleax authored Jul 18, 2024
1 parent 2294160 commit 2d38d38
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,7 @@ export default class Collection extends ShellApiWithMongoClass {
@apiVersions([])
async getShardVersion(): Promise<Document> {
this._emitCollectionApiCall('getShardVersion', {});
return await this._database._runAdminCommand({
return await this._database._runAdminReadCommand({
getShardVersion: `${this._database._name}.${this._name}`,
});
}
Expand Down Expand Up @@ -2258,7 +2258,7 @@ export default class Collection extends ShellApiWithMongoClass {
): Promise<Document> {
assertArgsDefinedType([key], [true], 'Collection.analyzeShardKey');
this._emitCollectionApiCall('analyzeShardKey', { key });
return await this._database._runAdminCommand({
return await this._database._runAdminReadCommand({
analyzeShardKey: this.getFullName(),
key,
...options,
Expand Down
32 changes: 18 additions & 14 deletions packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ export default class Database extends ShellApiWithMongoClass {
cmd: Document,
options: CommandOperationOptions = {}
): Promise<Document> {
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<Document> {
return this.getSiblingDB('admin')._runReadCommand(cmd, options);
}

public async _runCursorCommand(
Expand Down Expand Up @@ -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<string> {
this._emitDatabaseApiCall('version', {});
const info: Document = await this._runAdminCommand({
const info: Document = await this._runAdminReadCommand({
buildInfo: 1,
});
if (!info || info.version === undefined) {
Expand All @@ -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<Document> {
this._emitDatabaseApiCall('serverBits', {});
const info: Document = await this._runAdminCommand({
const info: Document = await this._runAdminReadCommand({
buildInfo: 1,
});
if (!info || info.bits === undefined) {
Expand Down Expand Up @@ -1197,7 +1201,7 @@ export default class Database extends ShellApiWithMongoClass {
@apiVersions([])
async serverBuildInfo(): Promise<Document> {
this._emitDatabaseApiCall('serverBuildInfo', {});
return await this._runAdminCommand({
return await this._runAdminReadCommand({
buildInfo: 1,
});
}
Expand All @@ -1206,7 +1210,7 @@ export default class Database extends ShellApiWithMongoClass {
@apiVersions([])
async serverStatus(opts = {}): Promise<Document> {
this._emitDatabaseApiCall('serverStatus', { options: opts });
return await this._runAdminCommand({
return await this._runAdminReadCommand({
serverStatus: 1,
...opts,
});
Expand Down Expand Up @@ -1235,7 +1239,7 @@ export default class Database extends ShellApiWithMongoClass {
@apiVersions([])
async hostInfo(): Promise<Document> {
this._emitDatabaseApiCall('hostInfo', {});
return await this._runAdminCommand({
return await this._runAdminReadCommand({
hostInfo: 1,
});
}
Expand All @@ -1244,7 +1248,7 @@ export default class Database extends ShellApiWithMongoClass {
@apiVersions([])
async serverCmdLineOpts(): Promise<Document> {
this._emitDatabaseApiCall('serverCmdLineOpts', {});
return await this._runAdminCommand({
return await this._runAdminReadCommand({
getCmdLineOpts: 1,
});
}
Expand Down Expand Up @@ -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 || '' : ''}`,
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/shell-api/src/replica-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class ReplicaSet extends ShellApiWithMongoClass {

async _getConfig(): Promise<ReplSetConfig> {
try {
const result = await this._database._runAdminCommand({
const result = await this._database._runAdminReadCommand({
replSetGetConfig: 1,
});
if (result.config === undefined) {
Expand Down Expand Up @@ -242,7 +242,7 @@ export default class ReplicaSet extends ShellApiWithMongoClass {
@apiVersions([])
async status(): Promise<Document> {
this._emitReplicaSetApiCall('status', {});
return this._database._runAdminCommand({
return this._database._runAdminReadCommand({
replSetGetStatus: 1,
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/shell-api/src/shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ export default class Shard extends ShellApiWithMongoClass {
async balancerCollectionStatus(ns: string): Promise<Document> {
assertArgsDefinedType([ns], ['string'], 'Shard.balancerCollectionStatus');
this._emitShardApiCall('balancerCollectionStatus', { ns });
return this._database._runAdminCommand({
return this._database._runAdminReadCommand({
balancerCollectionStatus: ns,
});
}
Expand Down Expand Up @@ -540,7 +540,7 @@ export default class Shard extends ShellApiWithMongoClass {
async isBalancerRunning(): Promise<Document> {
this._emitShardApiCall('isBalancerRunning', {});
await getConfigDB(this._database);
return this._database._runAdminCommand({
return this._database._runAdminReadCommand({
balancerStatus: 1,
});
}
Expand All @@ -550,7 +550,7 @@ export default class Shard extends ShellApiWithMongoClass {
async startBalancer(timeout = 60000): Promise<Document> {
assertArgsDefinedType([timeout], ['number'], 'Shard.startBalancer');
this._emitShardApiCall('startBalancer', { timeout });
return this._database._runAdminCommand({
return this._database._runAdminReadCommand({
balancerStart: 1,
maxTimeMS: timeout,
});
Expand Down

0 comments on commit 2d38d38

Please sign in to comment.