Skip to content

Commit 389a9fd

Browse files
fracasulanodkz
authored andcommitted
docs: support for replSetInitiate settings and great electionTimeoutMillis: 100 option
* Support for replSetInitiate settings * replSet.configSettings docs * Typo
1 parent bcb1904 commit 389a9fd

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,35 @@ const replSet = new MongoMemoryReplSet({
146146
oplogSize, // size (in MB) for the oplog; (default: 1)
147147
spawn, // spawn options when creating the child processes
148148
storageEngine, // default storage engine for instance. (Can be overridden per instance)
149+
configSettings: { // Optional settings for replSetInitiate command. See https://docs.mongodb.com/manual/reference/command/replSetInitiate/
150+
chainingAllowed: boolean, // When true it allows secondary members to replicate from other secondary members. When false, secondaries can replicate only from the primary.
151+
heartbeatTimeoutSecs: number, // Number of seconds that the replica set members wait for a successful heartbeat from each other. If a member does not respond in time, other members mark the delinquent member as inaccessible.
152+
heartbeatIntervalMillis: number, // The frequency in milliseconds of the heartbeats.
153+
electionTimeoutMillis: number, // The time limit in milliseconds for detecting when a replica set’s primary is unreachable.
154+
catchUpTimeoutMillis: number, // Time limit for a newly elected primary to sync (catch up) with the other replica set members that may have more recent writes.
155+
}
149156
},
150157
});
151158
```
152159
160+
The `replSet.configSettings` can be used to tune the initialization of the replica set. By default one of the nodes has
161+
to be promoted to Primary before the replica set can be successfully used but that happens only after a timeout of 10 seconds
162+
(see [electionTimeoutMillis](https://docs.mongodb.com/manual/reference/replica-configuration/#rsconf.settings.electionTimeoutMillis)).
163+
164+
It is possible to force the replica set to elect a node as Primary before the default 10 seconds by changing the `electionTimeoutMillis`
165+
parameter as shown below:
166+
167+
```js
168+
const replSet = new MongoMemoryReplSet({
169+
replSet: {
170+
configSettings: {
171+
electionTimeoutMillis: 100,
172+
},
173+
},
174+
})
175+
await replSet.waitUntilRunning()
176+
```
177+
153178
### Simple test with MongoClient
154179
155180
Take a look at this [test file](https://github.com/nodkz/mongodb-memory-server/blob/master/src/__tests__/singleDB-test.ts).

src/MongoMemoryReplSet.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export interface ReplSetOpts {
3535
oplogSize: number;
3636
spawn: SpawnOptions;
3737
storageEngine: StorageEngineT;
38+
configSettings?: MongoMemoryReplSetConfigSettingsT;
39+
}
40+
41+
export interface MongoMemoryReplSetConfigSettingsT {
42+
chainingAllowed?: boolean;
43+
heartbeatTimeoutSecs?: number;
44+
heartbeatIntervalMillis?: number;
45+
electionTimeoutMillis?: number;
46+
catchUpTimeoutMillis?: number;
3847
}
3948

4049
export interface MongoMemoryReplSetOptsT {
@@ -215,6 +224,7 @@ export default class MongoMemoryReplSet extends EventEmitter {
215224
const rsConfig = {
216225
_id: this.opts.replSet.name,
217226
members,
227+
settings: this.opts.replSet.configSettings || {},
218228
};
219229
await this.admin.command({ replSetInitiate: rsConfig });
220230
this.debug('Waiting for replica set to have a PRIMARY member.');

0 commit comments

Comments
 (0)