Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the election process of MongoDB?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

What this article shares with you is about the election process of MongoDB. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, let's take a look at it.

The replication set of MongoDB can automatically tolerate the downtime of some nodes. When there is a problem with the replication set, it will trigger the election-related process and complete the automatic switching between master and slave nodes.

Each replication set member runs the heartbeat thread of all nodes of the replication set in the background, triggering the state detection process in two cases:

1)。 The heartbeat detection results of replica set members change, such as a node is down or a new node is added.

2)。 No status detection process is performed for more than 4 seconds.

The state detection process roughly includes the following steps:

1)。 Check whether you are in the election process, and if so, withdraw from the process.

2)。 Maintain a standby list of primary nodes, in which all nodes may be elected as primary nodes, and each node detects whether its own and global conditions are met:

a. See if there is a Majority online in the replication set.

b. Its own priority is greater than 0.

c. Itself is not arbiter.

d. Its own opTime can not lag more than 10 seconds behind the latest node.

e. The cluster program stored by itself is up-to-date according to the information.

If all conditions are met, add yourself to the primary node standby list, otherwise, remove yourself from the list.

1)。 If the following conditions are met, the master node will be a slave node (if the master node to be degraded is itself, call the demotion method directly; if not for itself, call the replSetStepDown command to demote the replication set master node to slave node.):

a. The master node exists in the cluster.

B. there are nodes higher than the priority of the current primary node in the standby list of primary nodes.

C. The node with the highest priority in the standby list of primary nodes has an opTime less than 10 seconds behind the latest opTime of all other nodes.

2)。 Check whether the Majority is dominant, if it is dominant, and you can't see the replication set online, downgrade yourself to slave.

3)。 If there is no primary node in the cluster, check whether it is in the "standby list of primary nodes". If not, print log and exit the process.

4)。 If you start to judge whether you can send a notification of electing your own primary node to the replication set in the "standby list of Primary Node", the judgment process includes:

a. Whether you can see the Majority online in the replication set.

b. Whether it is in the standby list of the primary node.

c. If the condition is satisfied, set the "oneself is already in the election process" identification bit to true, and enter the "elect itself master node" method.

5)。 Method verifies that it meets the following conditions:

a. This thread got the thread lock.

b. This node is not configured with the slaveDelay option or configured with a slaveDelay of 0. 0.

c. This node is not configured as arbiter.

If it is met, the environment detection will be called. If the following conditions are triggered, the "vote me Master" poll will not be sent:

d. The current time is less than the end freeze time of steppedDown (set time for execution of steppedDown + freeze time, internal call is 60s).

e. Your own opTime is not up to date on all nodes.

If any node opTime is newer than yourself, quit the process directly.

If other newest nodes are at most as new as themselves, each such node will be randomly sleep for a period of time, and then continue to judge.

f. Within 5 minutes of self-launch and not all nodes in the replication set are online.

6)。 If there are no other questions, try to get the number of votes you vote. in the process, you will judge whether you have voted within 30 seconds, and if so, withdraw from the whole process directly.

7)。 After all these complex tests, we can finally send a vote to elect me as the master node to the replica set.

8)。 After sending, it will receive votes from all nodes. If the number of votes is less than or equal to half, it will not become the master node. If it is more than half, set itself as the master node.

After voting closes, set the "you are already in the election process" flag to false.

It can be seen that some of the above judgment logic is repeated judgment, but it does not affect the final result, and may have something to do with the complexity of the judgment logic. Before each decision, it is necessary to verify whether all the conditions are met to prevent the conditions from being left out.

When a node in the replication set receives the "vote me Master" vote message sent by another node, the following judgment will be made:

9)。 If the configuration version of the replication set stored by yourself is too low, do not vote.

10)。 If the replication set configuration version stored by the requesting node is too low, vote against it.

11)。 If you are in a replica set that does not have a node to initiate a vote, vote against it.

12)。 There is a master node in the replication set and vote against it.

13)。 If the node whose priority is higher than the request exists, vote against it.

If all the conditions are passed, get your own number of votes (also judge whether you have participated in the vote within 30 seconds, if so, do not vote again), and cast the number of votes.

It should be noted that an objection will reduce the final number of votes by 10000, that is, in the vast majority of cases, as long as a node objects, the requesting node cannot become the primary node.

The election process is very complicated and can be summarized into two points in practical use:

a. Under normal circumstances, it takes about 5 seconds to choose the master.

b. If the newly elected master node dies immediately, it will take at least 30 seconds to re-elect the master.

The above is what the election process of MongoDB is like. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report