Skip to content

Commit 3752c72

Browse files
nhuanhoangducNhua Nhuahasezoey
authored
feat(MongoMemoryReplSet): add replicaMemberConfig to replica instance (#508)
Co-authored-by: Nhua Nhua <[email protected]> Co-authored-by: hasezoey <[email protected]>
1 parent bf7fb5c commit 3752c72

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
272272
if (baseOpts.storageEngine) {
273273
opts.storageEngine = baseOpts.storageEngine;
274274
}
275+
if (baseOpts.replicaMemberConfig) {
276+
opts.replicaMemberConfig = baseOpts.replicaMemberConfig;
277+
}
275278

276279
log('getInstanceOpts: instance opts:', opts);
277280

@@ -506,7 +509,11 @@ export class MongoMemoryReplSet extends EventEmitter implements ManagerAdvanced
506509
try {
507510
let adminDb = con.db('admin');
508511

509-
const members = uris.map((uri, index) => ({ _id: index, host: getHost(uri) }));
512+
const members = uris.map((uri, index) => ({
513+
_id: index,
514+
host: getHost(uri),
515+
...(this.servers[index].opts.instance?.replicaMemberConfig || {}), // Overwrite replica member config
516+
}));
510517
const rsConfig = {
511518
_id: this._replSetOpts.name,
512519
members,

packages/mongodb-memory-server-core/src/__tests__/MongoMemoryReplSet.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,22 @@ describe('single server replset', () => {
143143
});
144144

145145
it('start an replset with instanceOpts', async () => {
146-
const replSet = new MongoMemoryReplSet({ instanceOpts: [{ args: ['--quiet'] }] });
146+
const replSet = new MongoMemoryReplSet({
147+
instanceOpts: [
148+
{
149+
args: ['--quiet'],
150+
replicaMemberConfig: {
151+
priority: 2,
152+
},
153+
},
154+
],
155+
});
147156
await replSet.start();
148157

149158
expect(
150159
replSet.servers[0].opts.instance!.args!.findIndex((x) => x === '--quiet') > -1
151160
).toBeTruthy();
161+
expect(replSet.servers[0].opts.instance?.replicaMemberConfig?.priority).toBe(2);
152162

153163
await replSet.stop();
154164
});

packages/mongodb-memory-server-core/src/util/MongoInstance.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,85 @@ const log = debug('MongoMS:MongoInstance');
1616

1717
export type StorageEngine = 'devnull' | 'ephemeralForTest' | 'mmapv1' | 'wiredTiger';
1818

19+
/**
20+
* Overwrite replica member-specific configuration
21+
*
22+
* @see {@link https://docs.mongodb.com/manual/reference/replica-configuration/#replica-set-configuration-document-example}
23+
*
24+
* @example
25+
* ```ts
26+
* {
27+
* priority: 2,
28+
* buildIndexes: false,
29+
* votes: 2,
30+
* }
31+
* ```
32+
*/
33+
export interface ReplicaMemberConfig {
34+
/**
35+
* A boolean that identifies an arbiter.
36+
* @defaultValue `false` - A value of `true` indicates that the member is an arbiter.
37+
*/
38+
arbiterOnly?: boolean;
39+
40+
/**
41+
* A boolean that indicates whether the mongod builds indexes on this member.
42+
* You can only set this value when adding a member to a replica set.
43+
* @defaultValue `true`
44+
*/
45+
buildIndexes?: boolean;
46+
47+
/**
48+
* The replica set hides this instance and does not include the member in the output of `db.hello()` or `hello`.
49+
* @defaultValue `true`
50+
*/
51+
hidden?: boolean;
52+
53+
/**
54+
* A number that indicates the relative eligibility of a member to become a primary.
55+
* Specify higher values to make a member more eligible to become primary, and lower values to make the member less eligible.
56+
* @defaultValue 1.0 for primary/secondary; 0 for arbiters.
57+
*/
58+
priority?: number;
59+
60+
/**
61+
* A tags document contains user-defined tag field and value pairs for the replica set member.
62+
* @defaultValue `null`
63+
* @example
64+
* ```ts
65+
* { "<tag1>": "<string1>", "<tag2>": "<string2>",... }
66+
* ```
67+
*/
68+
tags?: any;
69+
70+
/**
71+
* Mongodb 4.x only - The number of seconds "behind" the primary that this replica set member should "lag".
72+
* For mongodb 5.x, use `secondaryDelaySecs` instead.
73+
* @see {@link https://docs.mongodb.com/v4.4/tutorial/configure-a-delayed-replica-set-member/}
74+
* @defaultValue 0
75+
*/
76+
slaveDelay?: number;
77+
78+
/**
79+
* Mongodb 5.x only - The number of seconds "behind" the primary that this replica set member should "lag".
80+
* @defaultValue 0
81+
*/
82+
secondaryDelaySecs?: number;
83+
84+
/**
85+
* The number of votes a server will cast in a replica set election.
86+
* The number of votes each member has is either 1 or 0, and arbiters always have exactly 1 vote.
87+
* @defaultValue 1
88+
*/
89+
votes?: number;
90+
}
91+
1992
export interface MongoMemoryInstanceOptsBase {
2093
args?: string[];
2194
port?: number;
2295
dbPath?: string;
2396
storageEngine?: StorageEngine;
97+
replicaMemberConfig?: ReplicaMemberConfig;
2498
}
2599

26100
export interface MongoMemoryInstanceOpts extends MongoMemoryInstanceOptsBase {

0 commit comments

Comments
 (0)