Skip to content

Commit

Permalink
Add changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Oct 24, 2024
1 parent 118574c commit c6f5f86
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
3 changes: 1 addition & 2 deletions packages/shell-api/src/change-stream-cursor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from './enums';
import type { ChangeStream, Document } from '@mongosh/service-provider-core';
import { startTestCluster } from '../../../testing/integration-testing-hooks';
import { CliServiceProvider } from '../../service-provider-server/lib';
import ShellInstanceState from './shell-instance-state';
import Mongo from './mongo';
import { ensureMaster, ensureResult } from '../test/helpers';
Expand All @@ -22,6 +21,7 @@ import type Collection from './collection';
import { MongoshUnimplementedError } from '@mongosh/errors';
import { EventEmitter } from 'events';
import { dummyOptions } from './helpers.spec';
import CliServiceProvider from '../../service-provider-server/src/cli-service-provider';

describe('ChangeStreamCursor', function () {
describe('help', function () {
Expand Down Expand Up @@ -342,7 +342,6 @@ describe('ChangeStreamCursor', function () {
it('isExhausted fails', function () {
try {
cursor.isExhausted();
expect.fail('missed exception');
} catch (err: any) {
expect(err.name).to.equal('MongoshInvalidInputError');
}
Expand Down
40 changes: 21 additions & 19 deletions packages/shell-api/src/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ describe('getPrintableShardStatus', function () {
const testServer = startSharedTestServer();

let mongo: Mongo;
let db: Database;
let configDatabase: Database;
let serviceProvider: ServiceProvider;
let inBalancerRound = false;
Expand Down Expand Up @@ -179,20 +178,23 @@ describe('getPrintableShardStatus', function () {
getSiblingDB.withArgs('admin').returns(mockedAdminDb);
getSiblingDB.withArgs('config').returns(configDatabase);

db = {
_maybeCachedHello: stub().returns({ msg: 'isdbgrid' }),
getSiblingDB,
} as unknown as Database;
configDatabase.getSiblingDB = getSiblingDB;
configDatabase._maybeCachedHello = stub().returns({ msg: 'isdbgrid' });

const origRunCommandWithCheck = serviceProvider.runCommandWithCheck;
serviceProvider.runCommandWithCheck = async (db, cmd) => {
serviceProvider.runCommandWithCheck = async (configDatabase, cmd) => {
if (cmd.hello) {
return { ok: 1, msg: 'isdbgrid' };
}
if (db === 'admin' && cmd.balancerStatus) {
if (configDatabase === 'admin' && cmd.balancerStatus) {
return { ok: 1, inBalancerRound };
}
return origRunCommandWithCheck.call(serviceProvider, db, cmd, {});
return origRunCommandWithCheck.call(
serviceProvider,
configDatabase,
cmd,
{}
);
};

await Promise.all(
Expand Down Expand Up @@ -220,7 +222,7 @@ describe('getPrintableShardStatus', function () {
});

it('returns an object with sharding information', async function () {
const status = await getPrintableShardStatus(db, false);
const status = await getPrintableShardStatus(configDatabase, false);
expect(status.shardingVersion.clusterId).to.be.instanceOf(bson.ObjectId);
expect(status.shards.map(({ host }: { host: string }) => host)).to.include(
'shard01/localhost:27018,localhost:27019,localhost:27020'
Expand Down Expand Up @@ -251,7 +253,7 @@ describe('getPrintableShardStatus', function () {
'upgradeState',
]) {
it(`does not show ${hiddenField} in shardingVersion`, async function () {
const status = await getPrintableShardStatus(db, false);
const status = await getPrintableShardStatus(configDatabase, false);
expect((status.shardingVersion as any)[hiddenField]).to.equal(
undefined
);
Expand All @@ -262,19 +264,19 @@ describe('getPrintableShardStatus', function () {
it('returns whether the balancer is currently running', async function () {
{
inBalancerRound = true;
const status = await getPrintableShardStatus(db, true);
const status = await getPrintableShardStatus(configDatabase, true);
expect(status.balancer['Currently running']).to.equal('yes');
}

{
inBalancerRound = false;
const status = await getPrintableShardStatus(db, true);
const status = await getPrintableShardStatus(configDatabase, true);
expect(status.balancer['Currently running']).to.equal('no');
}
});

it('returns an object with verbose sharding information if requested', async function () {
const status = await getPrintableShardStatus(db, true);
const status = await getPrintableShardStatus(configDatabase, true);
expect((status['most recently active mongoses'][0] as any).up).to.be.a(
'number'
);
Expand All @@ -288,7 +290,7 @@ describe('getPrintableShardStatus', function () {
_id: 'balancer',
activeWindow: { start: '00:00', stop: '23:59' },
});
const status = await getPrintableShardStatus(db, false);
const status = await getPrintableShardStatus(configDatabase, false);
expect(status.balancer['Balancer active window is set between']).to.equal(
'00:00 and 23:59 server local time'
);
Expand All @@ -304,7 +306,7 @@ describe('getPrintableShardStatus', function () {
what: 'balancer.round',
ns: '',
});
const status = await getPrintableShardStatus(db, false);
const status = await getPrintableShardStatus(configDatabase, false);
expect(
status.balancer['Failed balancer rounds in last 5 attempts']
).to.equal(1);
Expand All @@ -318,7 +320,7 @@ describe('getPrintableShardStatus', function () {
ts: new bson.ObjectId('5fce116c579db766a198a176'),
when: new Date('2020-12-07T11:26:36.803Z'),
});
const status = await getPrintableShardStatus(db, false);
const status = await getPrintableShardStatus(configDatabase, false);
expect(
status.balancer['Collections with active migrations']
).to.have.lengthOf(1);
Expand All @@ -333,7 +335,7 @@ describe('getPrintableShardStatus', function () {
what: 'moveChunk.from',
details: { from: 'shard0', to: 'shard1', note: 'success' },
});
const status = await getPrintableShardStatus(db, false);
const status = await getPrintableShardStatus(configDatabase, false);
expect(
status.balancer['Migration Results for the last 24 hours']
).to.deep.equal({ 1: 'Success' });
Expand All @@ -345,7 +347,7 @@ describe('getPrintableShardStatus', function () {
what: 'moveChunk.from',
details: { from: 'shard0', to: 'shard1', errmsg: 'oopsie' },
});
const status = await getPrintableShardStatus(db, false);
const status = await getPrintableShardStatus(configDatabase, false);

expect(
status.balancer['Migration Results for the last 24 hours']
Expand All @@ -355,7 +357,7 @@ describe('getPrintableShardStatus', function () {
it('fails when config.version is empty', async function () {
await configDatabase.getCollection('version').drop();
try {
await getPrintableShardStatus(db, false);
await getPrintableShardStatus(configDatabase, false);
} catch (err: any) {
expect(err.name).to.equal('MongoshInvalidInputError');
return;
Expand Down
35 changes: 17 additions & 18 deletions packages/shell-api/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,11 @@ export function processDigestPassword(
* @param verbose
*/
export async function getPrintableShardStatus(
db: Database,
configDB: Database,
verbose: boolean
): Promise<ShardingStatusResult> {
const result = {} as ShardingStatusResult;

const configDB = await getConfigDB(db);

// configDB is a DB object that contains the sharding metadata of interest.
const mongosColl = configDB.getCollection('mongos');
const versionColl = configDB.getCollection('version');
Expand Down Expand Up @@ -522,7 +520,7 @@ export async function getPrintableShardStatus(
// All databases in config.databases + those implicitly referenced
// by a sharded collection in config.collections
// (could become a single pipeline using $unionWith when we drop 4.2 server support)
const [databases, collections] = await Promise.all([
const [databases, collections, shardedDataDistribution] = await Promise.all([
(async () =>
await (await configDB.getCollection('databases').find())
.sort({ _id: 1 })
Expand All @@ -535,7 +533,22 @@ export async function getPrintableShardStatus(
)
.sort({ _id: 1 })
.toArray())(),
(async () => {
try {
// $shardedDataDistribution is available since >= 6.0.3
const adminDB = configDB.getSiblingDB('admin');
return (await (
await adminDB.aggregate([{ $shardedDataDistribution: {} }])
).toArray()) as ShardedDataDistribution;
} catch {
// Pass, most likely an older version.
return undefined;
}
})(),
]);

result.shardedDataDistribution = shardedDataDistribution;

// Special case the config db, since it doesn't have a record in config.databases.
databases.push({ _id: 'config', primary: 'config', partitioned: true });

Expand Down Expand Up @@ -666,20 +679,6 @@ export async function getPrintableShardStatus(
)
).filter((dbEntry) => !!dbEntry);

let shardedDataDistribution: ShardedDataDistribution | undefined;

try {
// Available since >= 6.0.3
const adminDb = db.getSiblingDB('admin');
shardedDataDistribution = (await (
await adminDb.aggregate([{ $shardedDataDistribution: {} }])
).toArray()) as ShardedDataDistribution;
} catch {
// Pass, most likely an older version.
}

result.shardedDataDistribution = shardedDataDistribution;

delete result.shardingVersion.currentVersion;
return result;
}
Expand Down

0 comments on commit c6f5f86

Please sign in to comment.