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

Explain the election mechanism of MongoDB in an easy-to-understand way

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

If the primary node goes down or fails, the other nodes will choose a new primary node. The election process can be initiated by any non-master node, and then a new master node can be elected according to the priority and Bully algorithm. Until the primary node is elected, the entire cluster service is read-only and cannot perform write operations.

All non-arbitration nodes have a priority setting in the range of 0 Murray 100, and the higher the value, the more priority they can become the master node. By default, it is 1, and a value of 0 cannot be the primary node.

Bully algorithm is a coordinator (master node) election algorithm, the main idea is that each member of the cluster can declare that it is the master node and notify other nodes. The algorithm is introduced as follows:

When any process finds that the coordinator does not respond to the request, he initiates an election, the election process is as follows:

(1) .p process sends an election message to all processes with larger numbers.

(2)。 If no one responds, P wins and becomes the coordinator.

(3)。 If a process with a larger number than him responds, the responder takes over the election and the work of P is completed.

At any one time, a process can only receive an election message from a process with a smaller number. When the message arrives, the receiver sends an OK message to the sender indicating that it is running

Take over the job.

In the end, all but one process is abandoned, and that process is the new coordinator.

He sends the victory message to all other processes, notifying them of the new coordinator.

When a previously broken process is restored, it will host an election. If the process happens to be the most numbered process of the currently running process, it will win, so it becomes

Bullying algorithm.

For example, when the master node fails or goes down, the slave node that is eligible to be the master node will initiate an election proposal to the other nodes, which basically means "I think I can be the master node, don't you think?"

Other nodes will make the following judgments (three conditions) after receiving the election proposal:

(1)。 Is there another node in the replica set that is already the master node?

(2)。 Is your own data newer than the data on the slave node that is requested to become the master node?

(3)。 Is the data of other nodes in the replica set newer than the data of the slave node that is requested to become the master node?

If only one of the three conditions is true, then each will think that the other party's proposal is not feasible, so a message will be returned to the requesting node saying, "I don't think it is appropriate for you to be the master node, so withdraw from the election!"

As soon as the requesting node receives an inappropriate return from any other node, it will immediately withdraw from the election and keep itself in the role of the slave node; but if the above three conditions are negative, it will reply in the return package, "I think you can be the master node," that is, vote for the requesting node, and the voting process will enter the second stage of the election. The approved request node sends an acknowledgement request packet to other nodes, which basically means "I'm going to declare myself the master node. Is there any objection?" If no other nodes object during this confirmation process, the requesting node will promote itself to the master node, and all nodes will not make other election decisions within 30 seconds. if a node objects to the request node as the master node in the confirmation process, then the requesting node will keep its node role as the slave node after receiving the opposition reply, and then wait for the next election.

So how does priority affect the election? The election mechanism will try its best to make the node with the highest priority become the primary node, even if the primary node with relatively stable but low priority has been elected in the replica set.

A node with a lower priority will run as the master for a short period of time, but it cannot always be the master. In other words, if the higher priority node does not win in the Bully algorithm vote, the replica set will continue to initiate the election after running for a period of time until the highest priority node becomes the primary node.

Thus it can be seen that the priority setting parameters are very important in the election process, either do not set, maintain the fair state of priority 1, or several Server with better performance can be set higher priority.

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