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

Possible more robust wait for primary for replica set #206

Closed
wants to merge 3 commits into from
Closed
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
30 changes: 7 additions & 23 deletions packages/mongodb-memory-server-core/src/MongoMemoryReplSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,12 @@ export default class MongoMemoryReplSet extends EventEmitter {
return server;
}

async _waitForPrimary(timeout: number = 30000): Promise<boolean> {
if (!this.admin) {
return false;
}
const replStatus: ReplStatusResultT = await this.admin.command({
serverStatus: 1,
metrics: 0,
locks: 0,
});
this.debug(' replStatus:', replStatus);
const hasPrimary = replStatus.repl.ismaster;
if (!hasPrimary) {
const restTimeout = timeout - 500;
if (restTimeout <= 0) {
throw new Error(`No PRIMARY elected yet. Timeout expired. Exiting...`);
}
this.debug(`No PRIMARY yet. Waiting... ${restTimeout}ms`);
return new Promise((resolve) =>
setTimeout(() => resolve(this._waitForPrimary(restTimeout)), 500)
);
}

return true;
async _waitForPrimary(): Promise<boolean> {
return Promise.race(
this.servers.map((server) => {
const instanceInfo: any = server.getInstanceInfo();
return instanceInfo.instance.waitPrimaryReady();
})
);
}
}
15 changes: 14 additions & 1 deletion packages/mongodb-memory-server-core/src/util/MongoInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default class MongoInstance {
// @ts-ignore: Need to initialize this function
instanceReady: Function;
// @ts-ignore: Need to initialize this function
primaryReady: Function;
// @ts-ignore: Need to initialize this function
instanceFailed: Function;
isInstanceReady: boolean;

Expand Down Expand Up @@ -90,7 +92,6 @@ export default class MongoInstance {
if (!auth) result.push('--noauth');
else if (auth) result.push('--auth');
if (replSet) result.push('--replSet', replSet);

return result.concat(args || []);
}

Expand All @@ -116,6 +117,14 @@ export default class MongoInstance {
return this;
}

async waitPrimaryReady(): Promise<boolean> {
return new Promise((resolve) => {
this.primaryReady = () => {
resolve(true);
};
});
}

async kill(): Promise<MongoInstance> {
this.debug('Called MongoInstance.kill():');
if (this.childProcess && !this.childProcess.killed) {
Expand Down Expand Up @@ -228,6 +237,10 @@ export default class MongoInstance {
}
} else if (/\*\*\*aborting after/i.test(log)) {
this.instanceFailed('Mongod internal error');
} else if (/transition to primary complete; database writes are now permitted/.test(log)) {
if (this.primaryReady) {
this.primaryReady();
}
}
}
}