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

Addressing #74 - Wait until primary is transitioned from secondary for replicaSet #203

Merged
merged 1 commit into from
Jul 23, 2019

Conversation

jardakotesovec
Copy link
Contributor

My attempt to has deterministic implementation for waitUntilRunning for replicaSet, which is discussed in #74 .

Was testing if there is way to tell if its ready from replSetGetStatus, but my testing shows it rather shows the intention (whats going to be master) rather than actual real time status. With debug info from mongod - you can see that mongod instance is transitioning from secondary state to primary state (after voting) which takes about 2s. Also my testing shows that you get connect error until this transition is finished. And only reliable approach I found was using serverStatus which indicates in realtime whether its master or secondary.

Obviously serverStatus gives me status for the instance I am connect to, which is first server. Therefore this strategy would fail if some other instance would decide to be master initially. But in practice (testing 3 instances) always first server ended being primary, therefore this approach seems to be reliable from my testing.

Another small change was just rely on MongoMemoryServer.start to be resolved (which should resolve once instance starts listening) instead of waiting 1s. That shaved off about 800ms.

On my computer (current macbook pro) I am getting pretty good times. waitUntilRunning() gets resolved for single instance: 2100ms and multiple instance (3): 4100ms.

Let me know if you think I missed something. Thanks!

@jardakotesovec
Copy link
Contributor Author

Alternative approach would be to actually try to connect to replicaset until it does not return error. But this seems to work well.. have ran tests many times..

@nodkz nodkz merged commit 596fed8 into typegoose:master Jul 23, 2019
@nodkz
Copy link
Collaborator

nodkz commented Jul 23, 2019

@jardakotesovec brilliant 👍

You unlocked the ability for easy testing of transactions for the rest of the world 😉

@nodkz
Copy link
Collaborator

nodkz commented Jul 23, 2019

Published on npm as v5.1.8 🎉

@nodkz
Copy link
Collaborator

nodkz commented Jul 24, 2019

Just got some problems on circleCI test.

As fast fix I added a check for infinite loop in _waitForPrimary method with following error message No PRIMARY elected yet. Timeout expired. Exiting....

See last failed CircleCI builds:
https://circleci.com/gh/nodkz/mongodb-memory-server/tree/master

  1. Last test with infinite loop hotfix: https://circleci.com/gh/nodkz/mongodb-memory-server/143
  2. Infinite loop problem before hotfix https://circleci.com/gh/nodkz/mongodb-memory-server/138
  3. Failed test in repSet test suite https://circleci.com/gh/nodkz/mongodb-memory-server/136

@jardakotesovec will be cool if you have enough time to investigate this problem. Thanks.

@jardakotesovec
Copy link
Contributor Author

jardakotesovec commented Jul 24, 2019

@nodkz I can check it out tomorrow - but it seems that you have circleci running only on master? That will make difficult to investigate properly. I can only guess that the first server did not win voting (which can't replicate locally).. so I could probably fallback to strategy really just trying to connect to replicaset until it succeed.

@jardakotesovec
Copy link
Contributor Author

jardakotesovec commented Jul 24, 2019

@nodkz actually I could detect that primary is ready&present from stdout message 'transition to primary complete; database writes are now permitted'
Will try that tomorrow. For detecting that mongod is ready is used very much the same strategy and that works well :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants