Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions packages/mongodb-runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Options:
--arbiters number of arbiter nodes for each replica set [number]
--secondaries number of secondaries for each replica set [number]
--shards number of shards for sharded clusters [number]
--configShard for `start` with the sharded topology: turn the
config server into a config shard (requires MongoDB
8.0+) [boolean]
--version MongoDB server version to use [string]
--logDir Directory to store server log files in [string]
--binDir Directory containing mongod/mongos binaries [string]
Expand Down Expand Up @@ -111,6 +114,24 @@ You can specify a config file for the CLI using `--config`:
$ npx @mongodb-js/mongodb-runner start --config <path/to/config.json>
```

## Config shards

By default the runner starts a sharded cluster with a dedicated config server.
Pass `--configShard` (or `configShard: true` in a config file / programmatic
options) to turn the config server replica set into a config shard, a config
server that also holds data, once the cluster is up. This requires MongoDB 8.0
or newer (the underlying `transitionFromDedicatedConfigServer` command is new
in 8.0) and is only supported for the `sharded` topology. On clusters that
enforce authentication (keyfile or `--auth`), at least one user must be
configured, since the transition closes the localhost exception and dropping
the temporary user it uses would otherwise leave the cluster unusable. `start`
waits until the transition has completed, and `ls --json` (and `start --json`)
report `configShard: true` for such clusters:

```sh
$ npx mongodb-runner start -t sharded --shards 1 --configShard
```

## Machine-readable output

By default `start` prints diagnostics to stderr and the bare connection string
Expand Down
37 changes: 37 additions & 0 deletions packages/mongodb-runner/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,43 @@ describe('cli', function () {
await runCli(['prune']);
});

it('starts a config shard cluster and reports it in start and ls --json', async function () {
this.timeout(300_000);
const startStdout = await runCli([
'start',
'--topology',
'sharded',
'--shards',
'1',
'--configShard',
'--json',
]);
const started = JSON.parse(startStdout) as {
id: string;
connectionString: string;
configShard?: boolean;
};

expect(started.configShard, 'start --json should report the config shard')
.to.be.true;

const client = new MongoClient(started.connectionString);
const listShards = await client.db('admin').command({ listShards: 1 });
await client.close();
expect(
listShards.shards.map((s: any) => s._id),
'the config server should be a shard of the cluster',
).to.include('config');

const lsStdout = await runCli(['ls', '--json']);
const entry = JSON.parse(lsStdout).find((e: any) => e.id === started.id);
expect(entry.configShard, 'ls --json should report the config shard').to.be
.true;

await runCli(['stop', '--all']);
await runCli(['prune']);
});

it('emits an empty JSON array for ls --json with no instances', async function () {
const emptyDir = path.join(tmpDir, 'empty-runner');
await fs.mkdir(emptyDir, { recursive: true });
Expand Down
5 changes: 5 additions & 0 deletions packages/mongodb-runner/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import type { MongoClientOptions } from 'mongodb';
}, // Can also be an array when coming from the config file
describe: 'number of shards for sharded clusters',
})
.option('configShard', {
type: 'boolean',
describe:
'for `start` with the sharded topology: turn the config server into a config shard (requires MongoDB 8.0+)',
})
.option('version', {
type: 'string',
describe: 'MongoDB server version to use',
Expand Down
105 changes: 105 additions & 0 deletions packages/mongodb-runner/src/mongocluster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,111 @@ describe('MongoCluster', function () {
expect(cluster.connectionString).to.include('localhost');
});

it('can start a sharded cluster with a config shard', async function () {
cluster = await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
shards: 1,
secondaries: twoIfNotWindowsCI,
configShard: true,
});
const listShards = await cluster.withClient((client) =>
client.db('admin').command({ listShards: 1 }),
);
const shardIds = listShards.shards.map((s: any) => s._id);
expect(
shardIds,
'the config server should be listed as a shard',
).to.include('config');
expect(shardIds).to.have.lengthOf(2);

const serialized = cluster.serialize();
expect(serialized.configShard).to.be.true;
const roundTripped = await MongoCluster.deserialize(serialized);
expect(
roundTripped.serialize().configShard,
'the config shard flag should survive a serialize round trip',
).to.be.true;
});

it('can start a sharded cluster with a config shard and keyfile auth', async function () {
const keyFile = path.join(tmpDir, 'keyFileConfigShard');
await fs.writeFile(keyFile, 'csauthsecret', { mode: 0o400 });
cluster = await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
shards: 1,
secondaries: 0,
users: [
{
username: 'testuser',
password: 'testpass',
roles: ['root'],
},
],
args: ['--keyFile', keyFile],
configShard: true,
});
const listShards = await cluster.withClient((client) =>
client.db('admin').command({ listShards: 1 }),
);
expect(
listShards.shards.map((s: any) => s._id),
'the config server should be listed as a shard',
).to.include('config');

// The temporary user created for the transition must have been removed.
const usersInfo = await cluster.withClient((client) =>
client.db('admin').command({ usersInfo: 1 }),
);
expect(
usersInfo.users.map((u: any) => u.user),
'the temporary transition user should be gone',
).to.deep.equal(['testuser']);
});

it('rejects configShard with authentication but no configured users', async function () {
const keyFile = path.join(tmpDir, 'keyFileConfigShardNoUsers');
await fs.writeFile(keyFile, 'csauthsecret', { mode: 0o400 });
let err: unknown;
try {
await MongoCluster.start({
version: '8.x',
topology: 'sharded',
tmpDir,
shards: 1,
secondaries: 0,
args: ['--keyFile', keyFile],
configShard: true,
});
} catch (e) {
err = e;
}
expect(err, 'start should have failed').to.be.instanceOf(Error);
expect((err as Error).message).to.include(
'configShard requires at least one user',
);
});

it('rejects configShard for non-sharded topologies', async function () {
let err: unknown;
try {
await MongoCluster.start({
topology: 'replset',
tmpDir,
configShard: true,
});
} catch (e) {
err = e;
}
expect(err, 'start should have failed').to.be.instanceOf(Error);
expect((err as Error).message).to.include(
'configShard requires the sharded topology',
);
});

it('can use localhost as the host for a sharded cluster', async function () {
cluster = await MongoCluster.start({
host: 'localhost',
Expand Down
Loading
Loading