Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell-api): add shardedDataDistribution to sh.status() MONGOSH-1326 #2214

Merged
merged 26 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d082248
WIP: test if shardDistribution works
gagik Oct 11, 2024
fc93cbd
Fix helper tests
gagik Oct 14, 2024
6fc82eb
Add unit test
gagik Oct 15, 2024
fa4f17f
Merge branch 'main' into gagik/status-6.0.3-clusters
gagik Oct 15, 2024
e7f0eef
Merge branch 'main' into gagik/status-6.0.3-clusters
gagik Oct 15, 2024
7ae0ac7
Merge branch 'main' into gagik/status-6.0.3-clusters
gagik Oct 17, 2024
9fb806d
Remove log
gagik Oct 22, 2024
4842194
Merge branch 'gagik/status-6.0.3-clusters' of github.com:mongodb-js/m…
gagik Oct 22, 2024
f71faa0
Add integration test
gagik Oct 23, 2024
789ba83
Merge branch 'main' of github.com:mongodb-js/mongosh into gagik/statu…
gagik Oct 23, 2024
80ac653
Clear previous shards
gagik Oct 23, 2024
05f6b33
Use a different db name
gagik Oct 23, 2024
2ef07db
and correct ns
gagik Oct 23, 2024
631c92f
Remove unneeded import
gagik Oct 23, 2024
0083086
Move test before others
gagik Oct 24, 2024
7c606db
less strict check of shards
gagik Oct 24, 2024
46adbb2
Apply suggestions from code review
gagik Oct 24, 2024
118574c
Update packages/shell-api/src/helpers.ts
gagik Oct 24, 2024
c6f5f86
Add changes
gagik Oct 24, 2024
f7bc2b5
Merge branch 'main' of github.com:mongodb-js/mongosh into gagik/statu…
gagik Oct 24, 2024
9bf0251
Align with main
gagik Oct 24, 2024
f12bf83
Merge branch 'main' of github.com:mongodb-js/mongosh into gagik/statu…
gagik Oct 24, 2024
f25c329
Fix accidental change
gagik Oct 24, 2024
c4f06b9
Only mock for sharding information
gagik Oct 24, 2024
178b616
Fix lint
gagik Oct 24, 2024
9f26347
Merge branch 'main' into gagik/status-6.0.3-clusters
gagik Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions packages/shell-api/src/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ShardedDataDistribution } from './helpers';
import {
assertArgsDefinedType,
coerceToJSNumber,
Expand All @@ -19,6 +20,7 @@ import sinon from 'ts-sinon';
import chai, { expect } from 'chai';
import { EventEmitter } from 'events';
import sinonChai from 'sinon-chai';
import { stub } from 'sinon';
chai.use(sinonChai);

const fakeConfigDb = makeFakeConfigDatabase(
Expand Down Expand Up @@ -129,10 +131,26 @@ describe('getPrintableShardStatus', function () {
const testServer = startSharedTestServer();

let mongo: Mongo;
let db: Database;
let configDatabase: Database;
let serviceProvider: ServiceProvider;
let inBalancerRound = false;

const mockedShardedDataDistribution: ShardedDataDistribution = [
{
ns: 'test.ns',
shards: [
{
shardName: 'test',
numOrphanedDocs: 1,
numOwnedDocuments: 5,
orphanedSizeBytes: 20,
ownedSizeBytes: 80,
},
],
},
];

beforeEach(async function () {
serviceProvider = await CliServiceProvider.connect(
await testServer.connectionString(),
Expand All @@ -150,6 +168,22 @@ describe('getPrintableShardStatus', function () {
configDatabase = new Database(mongo, 'config_test');
expect(configDatabase.getName()).to.equal('config_test');

const mockedAdminDb = {
aggregate: stub()
.withArgs([{ $shardedDataDistribution: {} }])
.resolves({
toArray: stub().resolves(mockedShardedDataDistribution),
}),
};
const getSiblingDB = stub();
getSiblingDB.withArgs('admin').returns(mockedAdminDb);
getSiblingDB.withArgs('config').returns(configDatabase);

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

const origRunCommandWithCheck = serviceProvider.runCommandWithCheck;
serviceProvider.runCommandWithCheck = async (db, cmd) => {
if (cmd.hello) {
Expand Down Expand Up @@ -186,7 +220,7 @@ describe('getPrintableShardStatus', function () {
});

it('returns an object with sharding information', async function () {
const status = await getPrintableShardStatus(configDatabase, false);
const status = await getPrintableShardStatus(db, 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 All @@ -202,6 +236,10 @@ describe('getPrintableShardStatus', function () {
);
expect(status.databases).to.have.lengthOf(1);
expect(status.databases[0].database._id).to.equal('config');

expect(status.shardedDataDistribution).to.equal(
mockedShardedDataDistribution
);
});

describe('hides all internal deprecated fields in shardingVersion', function () {
Expand All @@ -213,30 +251,34 @@ describe('getPrintableShardStatus', function () {
'upgradeState',
]) {
it(`does not show ${hiddenField} in shardingVersion`, async function () {
const status = await getPrintableShardStatus(configDatabase, false);
expect(status.shardingVersion[hiddenField]).to.equal(undefined);
const status = await getPrintableShardStatus(db, false);
expect((status.shardingVersion as any)[hiddenField]).to.equal(
undefined
);
});
}
});

it('returns whether the balancer is currently running', async function () {
{
inBalancerRound = true;
const status = await getPrintableShardStatus(configDatabase, true);
const status = await getPrintableShardStatus(db, true);
expect(status.balancer['Currently running']).to.equal('yes');
}

{
inBalancerRound = false;
const status = await getPrintableShardStatus(configDatabase, true);
const status = await getPrintableShardStatus(db, 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(configDatabase, true);
expect(status['most recently active mongoses'][0].up).to.be.a('number');
expect(status['most recently active mongoses'][0].waiting).to.be.a(
const status = await getPrintableShardStatus(db, true);
expect((status['most recently active mongoses'][0] as any).up).to.be.a(
'number'
);
expect((status['most recently active mongoses'][0] as any).waiting).to.be.a(
'boolean'
);
});
Expand All @@ -246,7 +288,7 @@ describe('getPrintableShardStatus', function () {
_id: 'balancer',
activeWindow: { start: '00:00', stop: '23:59' },
});
const status = await getPrintableShardStatus(configDatabase, false);
const status = await getPrintableShardStatus(db, false);
expect(status.balancer['Balancer active window is set between']).to.equal(
'00:00 and 23:59 server local time'
);
Expand All @@ -262,7 +304,7 @@ describe('getPrintableShardStatus', function () {
what: 'balancer.round',
ns: '',
});
const status = await getPrintableShardStatus(configDatabase, false);
const status = await getPrintableShardStatus(db, false);
expect(
status.balancer['Failed balancer rounds in last 5 attempts']
).to.equal(1);
Expand All @@ -276,12 +318,12 @@ describe('getPrintableShardStatus', function () {
ts: new bson.ObjectId('5fce116c579db766a198a176'),
when: new Date('2020-12-07T11:26:36.803Z'),
});
const status = await getPrintableShardStatus(configDatabase, false);
const status = await getPrintableShardStatus(db, false);
expect(
status.balancer['Collections with active migrations']
).to.have.lengthOf(1);
expect(
status.balancer['Collections with active migrations'].join('')
status.balancer['Collections with active migrations']?.join('')
).to.include('asdf');
});

Expand All @@ -291,7 +333,7 @@ describe('getPrintableShardStatus', function () {
what: 'moveChunk.from',
details: { from: 'shard0', to: 'shard1', note: 'success' },
});
const status = await getPrintableShardStatus(configDatabase, false);
const status = await getPrintableShardStatus(db, false);
expect(
status.balancer['Migration Results for the last 24 hours']
).to.deep.equal({ 1: 'Success' });
Expand All @@ -303,7 +345,7 @@ describe('getPrintableShardStatus', function () {
what: 'moveChunk.from',
details: { from: 'shard0', to: 'shard1', errmsg: 'oopsie' },
});
const status = await getPrintableShardStatus(configDatabase, false);
const status = await getPrintableShardStatus(db, false);

expect(
status.balancer['Migration Results for the last 24 hours']
Expand All @@ -313,7 +355,7 @@ describe('getPrintableShardStatus', function () {
it('fails when config.version is empty', async function () {
await configDatabase.getCollection('version').drop();
try {
await getPrintableShardStatus(configDatabase, false);
await getPrintableShardStatus(db, false);
} catch (err: any) {
expect(err.name).to.equal('MongoshInvalidInputError');
return;
Expand Down
Loading
Loading