Skip to content

Commit

Permalink
feat(MongoMemoryServer): use storage engine "wiredTiger" if version i…
Browse files Browse the repository at this point in the history
…s 7.0.0 or higher

fixes #742
  • Loading branch information
hasezoey committed Aug 22, 2023
1 parent f099597 commit 952609b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const mongod = new MongoMemoryServer({
ip?: string, // by default '127.0.0.1', for binding to all IP addresses set it to `::,0.0.0.0`,
dbName?: string, // by default '' (empty string)
dbPath?: string, // by default create in temp directory
storageEngine?: string, // by default `ephemeralForTest`, available engines: [ 'ephemeralForTest', 'wiredTiger' ]
storageEngine?: string, // by default `ephemeralForTest`(unless mongodb 7.0.0, where its `wiredTiger`), available engines: [ 'ephemeralForTest', 'wiredTiger' ]
replSet?: string, // by default no replica set, replica set name
args?: string[], // by default no additional arguments, any additional command line arguments for `mongod` `mongod` (ex. ['--notablescan'])
},
Expand Down
4 changes: 3 additions & 1 deletion docs/api/interfaces/mongo-memory-instance-opts.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ This option will automatically be set with a directory generated by [`mkdtemp`](
### storageEngine

Typings: `storageEngine?: StorageEngine`
Default: `ephemeralForTest`
Default: `ephemeralForTest` (unless mongodb version is `7.0.0`, where its `wiredTiger`)

Set which storage engine to use, uses [`StorageEngine`](#helper-type-storageengine).

Expand All @@ -98,4 +98,6 @@ Custom Explanation:
MongoDB has stated that storage-engine `ephemeralForTest` is unstable and has been deprecated, it will be removed with mongodb 7.

In MMS there has been no observation of `ephemeralForTest` being unstable (aside from some missing features) and will continue to be the default until mongodb-memory-server changes to a version where `ephemeralForTest` is not present anymore.

With mongodb-memory-server 9.0.0, if mongodb 7.0.0 or higher is used, `wiredTiger` is the default engine.
:::
2 changes: 1 addition & 1 deletion docs/api/interfaces/replset-opts.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Also see [`MongoMemoryInstanceOpts.spawn`](./mongo-memory-instance-opts.md#spawn
### storageEngine

Typings: `storageEngine?: StorageEngine`
Default: `"ephemeralForTest"`
Default: `"ephemeralForTest"` (unless mongodb version is `7.0.0`, where its `wiredTiger`)

Set which Storage Engine to use, uses [`StorageEngine`](./mongo-memory-instance-opts.md#helper-type-storageengine).

Expand Down
7 changes: 7 additions & 0 deletions docs/guides/migration/migrate9.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,10 @@ This *should* help with non-closed instances not exiting the nodejs process.
Previously MMS used `get-port`, but it caused some big memory-leakage across big projects, so it has been replaced with one that uses less maps.

It also has been replaced because newer versions were ESM only, but we couldnt switch to ESM yet (and using ESM in CommonJS is not a great experience)

## Mongodb version 7.0.0 is now supported

Mongob version `7.0.0` removed storage engine `ephemeralForTest`, with mongodb-memory-server 9.0.0 storage engine `wiredTiger` is the default for binary versions `7.0.0` and higher.
Older versions (before `7.0.0`) will still continue to use `ephemeralForTest` by default.

It is recommended to run those instances with a db path which is equivalent to [`tmpfs`](https://wiki.archlinux.org/title/tmpfs).
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface ReplSetOpts {
spawn?: SpawnOptions;
/**
*`mongod` storage engine type
* @default 'ephemeralForTest'
* @default 'ephemeralForTest' unless mongodb version is `7.0.0`, where its `wiredTiger`
*/
storageEngine?: StorageEngine;
/**
Expand Down
25 changes: 24 additions & 1 deletion packages/mongodb-memory-server-core/src/MongoMemoryServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { promises as fspromises } from 'fs';
import { AddUserOptions, MongoClient } from 'mongodb';
import { InstanceInfoError, StateError } from './util/errors';
import * as os from 'os';
import { DryMongoBinary } from './util/DryMongoBinary';
import * as semver from 'semver';

const log = debug('MongoMS:MongoMemoryServer');

Expand Down Expand Up @@ -375,12 +377,33 @@ export class MongoMemoryServer extends EventEmitter implements ManagerAdvanced {
port = await this.getNewPort(port);
}

const opts = await DryMongoBinary.generateOptions(this.opts.binary);
let storageEngine = instOpts.storageEngine;

// warn when storage engine "ephemeralForTest" is explicitly used and switch to "wiredTiger"
if (storageEngine === 'ephemeralForTest' && semver.gte(opts.version, '7.0.0')) {
console.warn(
'Storage Engine "ephemeralForTest" is removed since mongodb 7.0.0, automatically using "wiredTiger"!\n' +
'This warning is because the mentioned storage engine is explicitly used and mongodb version is 7.0.0 or higher'
);

storageEngine = 'wiredTiger';
}

if (isNullOrUndefined(storageEngine)) {
if (semver.gte(opts.version, '7.0.0')) {
storageEngine = 'wiredTiger';
} else {
storageEngine = 'ephemeralForTest';
}
}

// consider directly using "this.opts.instance", to pass through all options, even if not defined in "StartupInstanceData"
const data: StartupInstanceData = {
port: port,
dbName: generateDbName(instOpts.dbName),
ip: instOpts.ip ?? '127.0.0.1',
storageEngine: instOpts.storageEngine ?? 'ephemeralForTest',
storageEngine: storageEngine,
replSet: instOpts.replSet,
dbPath: instOpts.dbPath,
tmpDir: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1113,4 +1113,28 @@ describe('MongoMemoryServer', () => {
mongoServer._instanceInfo.tmpDir!
); // manual cleanup
});

describe('server version specific', () => {
// should use default options that are supported for 7.0 (like not using "ephemeralForTest" by default)
it('should allow mongodb by default 7.0', async () => {
const server = await MongoMemoryServer.create({ binary: { version: '7.0.0' } });

await server.stop();
});

it('should warn if "ephemeralForTest" is used explicitly in mongodb 7.0', async () => {
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
const server = await MongoMemoryServer.create({
binary: { version: '7.0.0' },
instance: { storageEngine: 'ephemeralForTest' },
});

expect(console.warn).toHaveBeenCalledTimes(1);
expect(spy.mock.calls).toMatchSnapshot();

expect(server.instanceInfo?.storageEngine).toStrictEqual('wiredTiger');

await server.stop();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ This may be because of using a v6.x way of calling functions, look at the follow
https://nodkz.github.io/mongodb-memory-server/docs/guides/migration/migrate7#no-function-other-than-start-create-ensureinstance-will-be-starting-anything"
`;

exports[`MongoMemoryServer server version specific should warn if "ephemeralForTest" is used explicitly in mongodb 7.0 1`] = `
Array [
Array [
"Storage Engine \\"ephemeralForTest\\" is removed since mongodb 7.0.0, automatically using \\"wiredTiger\\"!
This warning is because the mentioned storage engine is explicitly used and mongodb version is 7.0.0 or higher",
],
]
`;

exports[`MongoMemoryServer start() should throw an error if state is not "new" or "stopped" 1`] = `
"Incorrect State for operation: \\"starting\\", allowed States: \\"[new,stopped]\\"
This may be because of using a v6.x way of calling functions, look at the following guide if anything applies:
Expand Down

0 comments on commit 952609b

Please sign in to comment.