Leader node election method and system

By using the separation of election terms and work terms in the Raft consensus algorithm, the problem of frequently triggering leader elections and dual leaders or multiple leaders is solved, ensuring the stability and availability of the blockchain network.

CN118784544APending Publication Date: 2024-10-15LINGSHU TECH CO LTD
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202410905213.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-08
Publication Date
2024-10-15

AI Technical Summary

Technical Problem

In the Raft consensus algorithm, frequent triggering of the Leader election process and the occurrence of double or multiple Leaders lead to the unavailability of the blockchain network. Especially in insufficient resources and weak network environments, it may cause the entire blockchain system to paralyze.

Method used

The non-leader node broadcasts the election message based on whether the timer times out. The candidate node counts the total number of votes received and determines whether it can become the leader node. If successful, it broadcasts the heartbeat message to other nodes, causing the old leader node to step down and store the new leader node information. Use The separation of election term and work term maintains the stability of leadership nodes.

Benefits of technology

It effectively prevents the election of new leader nodes from interrupting the normal work of the current leader nodes, prevents the emergence of multiple leader nodes, and ensures the stable operation of the blockchain network.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118784544A_ABST
    Figure CN118784544A_ABST
Patent Text Reader

Abstract

The invention discloses a leader node election method and system. The method comprises the following steps: a non-leader node broadcasts an election message according to whether a timer is overtime or not; each node receives the election request, processes the election request and replies a response message; the response message comprises a rejection message and an agreement message; after receiving the response message, the candidate node counts the total number of acquired tickets according to the response message, and judges whether the candidate node can become a leader node according to the total number of acquired tickets; if yes, heartbeat messages are broadcasted to all other nodes; and after all other nodes receive the heartbeat message, the old leader node gets off the channel and stores the information of the new leader node according to the received time of the new leader node. According to the method provided by the invention, a mode of separating the election period from the working period is used, the keeping of the leader node period is realized, when the candidate node is locally triggered to elect, the leader position of the leader node cannot be impacted, the normal work of the current leader node is prevented from being interrupted, and the occurrence of a plurality of leader nodes can be prevented.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of blockchain consensus algorithms, and in particular to a method and system for electing a leader node for a blockchain Raft consensus algorithm in a complex network environment. Background Art

[0002] Raft is a consensus algorithm designed to replace Paxos. It is easier to understand than Paxos through logical separation, is also formally proven to be safe, and provides some additional features. Raft provides a general method for distributing state machines in a cluster of computing systems, ensuring that every node in the cluster agrees on the same series of state transitions.

[0003] In the Raft consensus algorithm, nodes have the following roles:

[0004] ● Leader: The leader is responsible for communicating with the client, receiving commands from the client and forwarding them to the followers;

[0005] ● Follower: A follower who meticulously executes the orders from the leader;

[0006] ● Candidate: When a follower does not receive any message from the leader for a long time, it will rise up and become a candidate to compete for the qualification of becoming the leader;

[0007] In the prior art, in a block system composed of a large number of nodes, especially for small and medium-sized enterprise users, due to the constraints of their own hardware conditions, some nodes may have insufficient system resources, network I / O limitations, or two of the nodes cannot communicate with each other P2P. This will cause the term of individual nodes to time out and trigger re-election, or even frequently trigger the Leader election process, especially in a weak network environment, which may cause the entire blockchain network to be unavailable; even worse, there may be a double leader or multiple leaders, which is called a brain split in the Raft consensus algorithm. Due to the immutability of the blockchain, in serious cases, it may gradually cause each node to become a faulty node, and it will never be able to reach a consensus with other nodes until the entire block system is paralyzed and cannot be used anymore, and it is irreversible and irreparable.

[0008] Currently, no effective solution has been proposed to the problem that the leader election process is frequently triggered in the existing technology, resulting in the unavailability of the entire blockchain network. In even worse cases, there are dual leaders or multiple leaders. In severe cases, the entire blockchain system may be paralyzed. Summary of the invention

[0009] The embodiments of the present invention provide a leader node election method and system to solve the problem in the prior art that the leader election process is frequently triggered, resulting in the unavailability of the entire blockchain network, and even worse, the occurrence of double leaders or multiple leaders, which in severe cases will cause the entire block system to be paralyzed.

[0010] To achieve the above-mentioned purpose, on the one hand, the present invention provides a method for electing a leader node, which method includes: S1, non-leader nodes broadcast election messages according to whether the timer has timed out; S2, each node processes the election request received and replies with a response message; the response message includes: a rejection message and an approval message; S3, after receiving the response message, the candidate node counts the total number of votes according to the response message, and determines whether it can become a leader node according to the total number of votes; if so, broadcasts a heartbeat message to all other nodes; S4, after receiving the heartbeat message, all other nodes cause the old leader node to step down according to the term of office of the new leader node received and store the new leader node information.

[0011] Optionally, the non-leader node includes: a follower node and a candidate node; the S1 includes: S11, if the current node is a follower node, check whether the heartbeat time of the current node from the leader node has timed out, if not, it remains a follower node and does not broadcast an election message; if it times out, switch the node state to a candidate node; and enter S12; if the current node is a candidate node, check whether the timer of the current node has timed out, if not, it remains a candidate node and does not broadcast an election message; if it times out, and no consent vote from the majority of nodes and no message from other nodes to become leader nodes is received, it remains a candidate node and enters S12; S12, update the election term to the current election term plus 1, and update the election time to the current system time; broadcast the election message, and conduct a new round of leader node elections.

[0012] Optionally, S2 includes: S21. If the current node is a leading node, directly reject the election request and reply with a rejection message; if the current node is a candidate node, and the election term of the current node is not less than the request term of the candidate node that broadcasts the election message, directly reject the election request and reply with a rejection message; otherwise, switch the node state to a follower node and enter S22; if the current node is a follower node and the current node normally receives the heartbeat of the leading node within the timer, directly reject the election request and reply with a rejection message; or if the election term of the current node is not less than the request term of the candidate node that broadcasts the election message, directly reject the election request and reply with a rejection message; otherwise, enter S22; S22. Update the election term to the current request term, and update the election time to the current system time; agree to the election and reply with an agreement message.

[0013] Optionally, the S3 includes: S31. If the response message is a rejection message, enter S32; if the response message is an approval message, enter S33; S32. When it is determined that the rejection message is sent by the leading node, the election is ended and the node state is switched to a follower node; when it is determined that the rejection message is sent by the candidate node, if the election term of the opposite node is higher than the election term of the current node, the election is ended, and the node state is switched to a follower node, and the election term of the current node is updated to the election term of the opposite node; if the election term of the opposite node is not higher than the election term of the current node, this message is not processed; S33. Add 1 to the number of votes; S34. After counting the total number of votes, determine whether the total number of votes is greater than or equal to N / 2+1, where N is the total number of nodes; if the conditions are not met, it remains a candidate node and continues to the next election; if the conditions are met, the node state is switched to a leading node, and the working term is updated to the election term of the winner at this time, and a heartbeat message is broadcast to all other nodes to interrupt the election process of all other nodes.

[0014] Optionally, S4 includes: S41. After all other nodes receive the heartbeat message, if the current node is a follower node and the term of the new leader node it receives is higher than the term of the old leader node that is still sending heartbeat messages to the current node, the old leader node is forced to step down and enter S42; if the current node is a leader node and its current node term is lower than the term of the new leader node received, it means that the current node is the old leader node, and the current node passively steps down, switches the node state to a follower node, and enters S42; if the current node is a candidate node, directly ends the election process, switches the node state to a follower node, and enters S42; S42. Stores the information of the new leader node; updates the term to the working term of the new leader node at this time.

[0015] On the other hand, the present invention provides a leadership node election system, which includes: a broadcast election unit, which is used for non-leader nodes to broadcast election messages according to whether the timer has timed out; an election request processing unit, which is used for each node to process the election request received and reply to the response message; the response message includes: a rejection message and an approval message; a response message processing unit, which is used for the candidate node to count the total number of votes according to the response message after receiving the response message, and judge whether it can become a leader node according to the total number of votes; if so, broadcast a heartbeat message to all other nodes; a heartbeat message processing unit, which is used for all other nodes to step down the old leader node according to the term of office of the new leader node received and store the new leader node information after receiving the heartbeat message.

[0016] Optionally, the non-leader node includes: a follower node and a candidate node; the broadcast election unit includes: a timeout judgment subunit, which is used to check whether the heartbeat time of the current node from the leader node has timed out if the current node is a follower node. If not, it remains a follower node and does not broadcast election messages; if it times out, switch the node state to a candidate node; and enter the broadcast election subunit; if the current node is a candidate node, check whether the timer of the current node has timed out. If not, it remains a candidate node and does not broadcast election messages; if it times out and does not receive a majority of nodes' consent votes and messages from other nodes becoming leader nodes, it remains a candidate node and enters the broadcast election subunit; the broadcast election subunit is used to update the election term to the current election term plus 1, and update the election time to the current system time; broadcast election messages to conduct a new round of leader node elections.

[0017] Optionally, the election request processing unit includes: an election request judgment processing subunit, which is used to directly reject the election request and reply with a rejection message if the current node is a leader node; if the current node is a candidate node, and the election term of the current node is not less than the request term of the candidate node that broadcasts the election message, directly reject the election request and reply with a rejection message; otherwise, switch the node state to a follower node, and enter the election consent subunit; if the current node is a follower node, and the current node normally receives the heartbeat of the leader node within the timer, directly reject the election request and reply with a rejection message; or if the election term of the current node is not less than the request term of the candidate node that broadcasts the election message, directly reject the election request and reply with a rejection message; otherwise, enter the election consent subunit; the election consent subunit is used to update the election term to the current request term, and update the election time to the current system time; agree to the election and reply with an agreement message.

[0018] Optionally, the response message processing unit includes: a response message judgment subunit, which is used to enter the node judgment processing subunit if the response message is a rejection message; if the response message is an approval message, it enters the vote counting subunit; a node judgment processing subunit, which is used to end the election and switch the node state to a follower node when it is judged to be a rejection message sent by the leader node; when it is judged to be a rejection message sent by the candidate node, if the election term of the opposite node is higher than the election term of the current node, the election is ended, the node state is switched to a follower node, and the election term of the current node is updated to the opposite node. The election term of the end node; if the election term of the peer node is not higher than that of the current node, this message will not be processed; the vote counting subunit is used to add 1 to the number of votes; the vote judgment subunit is used to judge whether the total number of votes is greater than or equal to N / 2+1 after counting the total number of votes, where N is the total number of nodes; if the conditions are not met, it will remain a candidate node and continue to the next election; if the conditions are met, the node state will be switched to a leader node, and the working term will be updated to the winning election term at this time, and a heartbeat message will be broadcast to all other nodes to interrupt the election process of all other nodes.

[0019] Optionally, the heartbeat message processing unit includes: a step-down sub-unit, which is used for, after all other nodes receive the heartbeat message, if the current node is a follower node, and the term of the new leader node it receives is higher than the term of the old leader node that is still sending heartbeat messages to the current node, then the old leader node is forced to step down and enter the information storage sub-unit; if the current node is a leader node, and its current node term is lower than the term of the new leader node received, it means that the current node is the old leader node, then the current node passively steps down, switches the node state to a follower node, and enters the information storage sub-unit; if the current node is a candidate node, directly ends the election process, switches the node state to a follower node, and enters the information storage sub-unit; the information storage sub-unit is used to store information of the new leader node; and updates the term to the working term of the new leader node at this time.

[0020] Beneficial effects of the present invention:

[0021] The present invention provides a method and system for electing a leader node, wherein the method comprises: S1, a non-leader node broadcasts an election message according to whether a timer has timed out; S2, each node processes the election request after receiving it and replies with a response message; the response message comprises: a rejection message and an approval message; S3, after receiving the response message, the candidate node counts the total number of votes obtained according to the response message, and judges whether it can become a leader node according to the total number of votes obtained; if so, broadcasts a heartbeat message to all other nodes; S4, after receiving the heartbeat message, all other nodes cause the old leader node to step down according to the term of the received new leader node and store the information of the new leader node. The method of the present invention uses a method of separating the election term from the working term to achieve the maintenance of the leader node term, and when a candidate node triggers an election locally, the leadership position of the leader node cannot be shaken. It can effectively prevent the election of a new leader node in a local node from interrupting the normal operation of the current leader node, and can prevent the emergence of multiple leader nodes. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] Figure 1 is a flow chart of a leader node election method provided by an embodiment of the present invention;

[0023] Figure 2 is a flow chart of broadcasting election messages provided by an embodiment of the present invention;

[0024] Figure 3 is a flow chart of election request processing provided by an embodiment of the present invention;

[0025] Figure 4 is a flow chart of response message processing provided by an embodiment of the present invention;

[0026] Figure 5 is a flowchart of heartbeat message processing provided by an embodiment of the present invention;

[0027] Figure 6 It is a structural diagram of a leadership node election system provided by an embodiment of the present invention;

[0028] Figure 7 is a schematic diagram of the structure of a broadcast election unit provided by an embodiment of the present invention;

[0029] Figure 8 is a schematic diagram of the structure of an election request processing unit provided by an embodiment of the present invention;

[0030] Fig. 9 is a structural diagram of a response message processing unit provided in an embodiment of the present invention;

[0031] Fig.10 It is a structural diagram of a heartbeat message processing unit provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0032] In order to make the purpose, technical scheme and advantages of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0033] In the Raft consensus algorithm, nodes have the following roles:

[0034] ● Leader: The leader is responsible for communicating with the client, receiving commands from the client and forwarding them to the followers;

[0035] ● Follower: A follower who meticulously executes the orders from the leader;

[0036] ● Candidate: When a follower does not receive any message from the leader for a long time, it will rise up and become a candidate to compete for the qualification of becoming the leader;

[0037] In the prior art, Raft uses a heartbeat mechanism to trigger Leader election. When the system starts, all nodes are initialized to the Follower state, the term is set to 0, and the timer is started. After the timer times out, the Follower node is converted to a Candidate node. Once converted to a Candidate node, it immediately starts to do the following things:

[0038] (1) Increase the number of terms you hold;

[0039] (2) Start a new timer;

[0040] (3) Vote for yourself;

[0041] (4) Send a RequestVote RPC request to all other nodes and wait for responses from other nodes.

[0042] If the majority of nodes vote before the timer times out, it will be converted to Leader. If it receives the AppendEntries heartbeat RPC from other nodes, it means that other nodes have been selected as Leaders, and it will be converted to Follower. If the timer times out and it has not received any of the above two messages, repeat steps 1-4 to conduct a new election.

[0043] After a node receives votes from the majority of nodes and becomes a Leader, it will immediately send an AppendEntries heartbeat RPC to all nodes. After all Candidates receive the heartbeat RPC, they are converted to Followers and the election is over.

[0044] Each Follower can only cast one vote in one term, and the first-come-first-served policy is adopted. Each Follower has a timer. When the timer times out and it still does not receive the heartbeat RPC from the Leader, it will be converted to a Candidate and start requesting a vote. That is, after the current Leader fails, a Follower will start to convert to a Candidate and start voting.

[0045] If multiple nodes initiate voting at the same time, and each node does not get a majority of votes (this situation is called SplitVote), the term number is increased and voting is repeated in the new term. Is it possible that Split Vote will occur repeatedly and the Leader will never be elected? No. Because Raft uses a random timeout, the Raft system has an election timeout configuration item. The timeout of the Follower and Candidate timers is recalculated each time, and a random selection between 1 and 2 times the configuration time is selected. Even if all nodes are started at the same time, due to the setting of the random timeout, each node will generally not be converted to a Candidate at the same time. The node that is converted to a Candidate first will initiate voting first and thus obtain the majority of votes. Therefore, in each term, the probability of multiple nodes requesting voting at the same time and only obtaining a minority of votes is very small, and the probability of this happening multiple times in a row is even smaller. In fact, it can be considered completely impossible. In general, the Leader is basically elected within 1-2 terms.

[0046] In the blockchain system, various organizations jointly maintain a consortium blockchain network, and each network node may be in a different network environment.

[0047] In a block system composed of a large number of nodes, especially for small and medium-sized enterprise users, due to the constraints of their own hardware conditions, some nodes may have insufficient system resources, network I / O restrictions, or two of the nodes cannot communicate with each other P2P. This will cause the term of individual nodes to time out and trigger re-election, or even frequently trigger the Leader election process, especially in a weak network environment, which may cause the entire blockchain network to be unavailable; even worse, there may be a double leader or multiple leaders, which is called a brain split in the Raft consensus algorithm. Due to the immutability of the blockchain, in serious cases, it may gradually cause each node to become a faulty node, and it will never be able to reach a consensus with other nodes, until the entire block system is paralyzed and cannot be used anymore, and it is irreversible and irreparable.

[0048] In view of the problems existing in the prior art, the present invention provides an optimized leader node election method. Figure 1 is a flow chart of a method for selecting a leader node according to an embodiment of the present invention. Figure 1 As shown, the method includes:

[0049] S1, non-leader nodes broadcast election messages based on whether the timer has timed out;

[0050] Specifically, the non-leader nodes include: follower nodes and candidate nodes;

[0051] Figure 2 FIG. 1 is a flowchart of broadcasting an election message provided by an embodiment of the present invention. Figure 2 As shown, the S1 includes:

[0052] S11, if the current node is a follower node, check whether the heartbeat time of the current node from the leader node has timed out. If not, it remains a follower node and does not broadcast election messages; if it has timed out, switch the node state to a candidate node; and enter S12; if the current node is a candidate node, check whether the timer of the current node has timed out. If not, it remains a candidate node and does not broadcast election messages; if it has timed out and has not received the consent vote of the majority of nodes and the message that other nodes have become leader nodes, it remains a candidate node and enters S12;

[0053] S12. Update the election term to the current election term plus 1, and update the election time to the current system time; broadcast the election message and conduct a new round of leadership node election.

[0054] For example, if the starting term is 0, and there are currently 10 follower nodes, and none of the 10 follower nodes receive a heartbeat when the timer times out, they are all converted to candidate nodes, and the election term is 1. The election message is broadcast to elect a leader node. It should be noted that the timers of the 10 follower nodes are different, and they are converted to candidate nodes one after another. The one that is converted to a candidate node first broadcasts the election message first. If it does not receive a majority vote or a message that another node has become a leader node when the timeout expires, the election for the second election term will begin.

[0055] S2. Each node receives the election request, processes it and replies with a response message; the response message includes: a rejection message and an approval message;

[0056] Figure 3 is a flowchart of an election request processing provided by an embodiment of the present invention, such as Figure 3 As shown, the S2 includes:

[0057] After receiving the election request, each node processes the election request as follows:

[0058] S21. If the current node is the leader node, directly reject the election request and reply with a rejection message;

[0059] If the current node is a candidate node, and the election term of the current node is not less than the requested term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, the node state is switched to a follower node, and S22 is entered;

[0060] For example, if the election term of the current candidate node is the second term, and the requested term (i.e., election term) of the candidate node that broadcasts the election message is also the second term, the election request is directly rejected; if the requested term of the candidate node that broadcasts the election message is the third term, the node state is directly switched to a follower node;

[0061] If the current node is a follower node and the current node normally receives the heartbeat of the leader node within the timer, the election request is directly rejected and a rejection message is replied; or if the election term of the current node is not less than the request term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, enter S22;

[0062] For example, if the election term of the current follower node is the second term (a vote has been cast for the candidate node of the second term), and the term of the candidate node broadcasting the election message is the first term, the election request is directly rejected.

[0063] S22. Update the election term to the current requested term, and update the election time to the current system time; agree to the election and reply with an agreement message.

[0064] For example, if the term of the current follower node is 1 and the requested term of the candidate node that broadcasts the election message is 2, the current follower node agrees to the election request of the candidate node that broadcasts the election message and updates the election term to 2.

[0065] S3. After receiving the response message, the candidate node counts the total number of votes according to the response message, and determines whether it can become the leader node according to the total number of votes; if so, broadcasts the heartbeat message to all other nodes;

[0066] Specifically, Figure 4 is a flow chart of response message processing provided by an embodiment of the present invention; Figure 4 As shown, the S3 includes:

[0067] After receiving the election response message, the candidate node that broadcasts the election message processes the election response. If the current node is not a candidate node, the message is discarded and the process ends.

[0068] S31. If the response message is a rejection message, enter S32; if the response message is an approval message, enter S33;

[0069] S32: When it is determined that the message is a rejection message sent by the leader node, the election is terminated and the node state is switched to a follower node;

[0070] When it is determined that the candidate node sends a rejection message, if the election term of the peer node (i.e. the candidate node that sends the rejection message) is higher than the election term of the current node, the election is terminated, and the node state is switched to a follower node, and the election term of the current node is updated to the election term of the peer node; if the election term of the peer node is not higher than the election term of the current node, the message is not processed;

[0071] S33, add 1 to the number of votes received;

[0072] S34. After counting the total number of votes, determine whether the total number of votes is greater than or equal to N / 2+1, where N is the total number of nodes; if the condition is not met, it remains a candidate node and continues to the next election; if the condition is met, switch the node state to the leader node, update the working term to the election term of the winner at this time, and broadcast heartbeat messages to all other nodes to interrupt the election process of all other nodes.

[0073] For example, if a candidate node that broadcasts an election message obtains votes greater than or equal to N / 2+1 in the third election term, it wins the election and updates its working term to the third term. At this time, it needs to broadcast a heartbeat message to all other nodes.

[0074] During the local election process of candidate nodes, the leadership position of the original leader node cannot be shaken, and the normal work of the current leader node will not be interrupted. That is, the leader node can send rejection messages to candidate nodes and send heartbeats to follower nodes.

[0075] S4. After receiving the heartbeat message, all other nodes step down the old leader node according to the term of the new leader node received and store the new leader node information.

[0076] Figure 5 : is a flowchart of heartbeat message processing provided by an embodiment of the present invention; Figure 5 As shown, the S4 includes:

[0077] S41, after all other nodes receive the heartbeat message, they first check the term of the new leader node contained in the heartbeat message; if the current node is a follower node, and the term of the new leader node it receives is higher than the term of the old leader node that still sends heartbeat messages to the current node, the old leader node is forced to step down and enter S42;

[0078] If the current node is a leader node, and its term of office is lower than the term of office of the new leader node received, it means that the current node is an old leader node, and the current node passively steps down, switches the node state to a follower node, and enters S42;

[0079] If the current node is a candidate node, the election process ends directly, and the node state is switched to a follower node, and then enters S42;

[0080] S42. Store the information of the new leader node; and update the term to be the working term of the new leader node at this time.

[0081] The method of the present invention can force one leader node to step down when two leader nodes appear, and the other leader node can work normally, thus preventing multiple leader nodes from appearing.

[0082] Figure 6 Schematic diagram of the structure of a leadership node election system provided by an embodiment of the present invention; Figure 6 As shown, the system includes:

[0083] A broadcast election unit 201, used for a non-leader node to broadcast an election message according to whether a timer has timed out;

[0084] Figure 7 is a schematic diagram of the structure of a broadcast election unit provided by an embodiment of the present invention. Figure 7 As shown, the non-leader nodes include: follower nodes and candidate nodes;

[0085] The broadcast election unit 201 includes:

[0086] The timeout judgment subunit 2011 is used to check whether the heartbeat time of the current node from the leader node has timed out if the current node is a follower node. If not, it remains a follower node and does not broadcast election messages; if it has timed out, the node state is switched to a candidate node and enters the broadcast election subunit; if the current node is a candidate node, check whether the timer of the current node has timed out. If not, it remains a candidate node and does not broadcast election messages; if it has timed out and has not received the consent vote of the majority of nodes and messages from other nodes to become leader nodes, it remains a candidate node and enters the broadcast election subunit;

[0087] The broadcast election subunit 2012 is used to update the election term to the current election term plus 1, and update the election time to the current system time; broadcast the election message to conduct a new round of leadership node election.

[0088] The election request processing unit 202 is used for each node to process the election request received and reply a response message; the response message includes: a rejection message and an approval message;

[0089] Figure 8 is a schematic diagram of the structure of an election request processing unit provided in an embodiment of the present invention. Figure 8 As shown, the election request processing unit 202 includes:

[0090] The election request judgment processing subunit 2021 is used to directly reject the election request and reply with a rejection message if the current node is a leader node;

[0091] If the current node is a candidate node, and the election term of the current node is not less than the requested term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, the node state is switched to a follower node, and the election consent subunit is entered;

[0092] If the current node is a follower node and the current node normally receives the heartbeat of the leader node within the timer, the election request is directly rejected and a rejection message is replied; or if the election term of the current node is not less than the requested term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, the election consent subunit is entered;

[0093] The election approval subunit 2022 is used to update the election term to the current requested term and update the election time to the current system time; approve the election and reply to the approval message.

[0094] The response message processing unit 203 is used for the candidate node to count the total number of votes according to the response message after receiving the response message, and determine whether it can become the leader node according to the total number of votes; if so, broadcast the heartbeat message to all other nodes;

[0095] Fig. 9 is a schematic diagram of the structure of a response message processing unit provided in an embodiment of the present invention, such as Fig. 9 As shown, the response message processing unit 203 includes:

[0096] The response message judgment subunit 2031 is used to enter the node judgment processing subunit if the response message is a rejection message; if the response message is an approval message, enter the vote counting subunit;

[0097] The node judgment processing subunit 2032 is used to terminate the election and switch the node state to a follower node when judging that the message is a rejection message sent by the leader node;

[0098] When it is determined that the candidate node sends a rejection message, if the election term of the peer node is higher than the election term of the current node, the election ends and the node status is switched to a follower node. At the same time, the election term of the current node is updated to the election term of the peer node. If the election term of the peer node is not higher than the election term of the current node, the message is not processed.

[0099] The vote counting subunit 2033 is used to add 1 to the number of votes received;

[0100] The vote judgment subunit 2034 is used to judge whether the total number of votes is greater than or equal to N / 2+1 after counting the total number of votes, where N is the total number of nodes; if the condition is not met, it remains a candidate node and continues to the next election; if the condition is met, the node state is switched to a leader node, and the working term is updated to the election term of the winner at this time, and a heartbeat message is broadcast to all other nodes to interrupt the election process of all other nodes.

[0101] The heartbeat message processing unit 204 is used to make the old leader node step down and store the new leader node information according to the received term of the new leader node after all other nodes receive the heartbeat message.

[0102] Fig.10 is a structural diagram of a heartbeat message processing unit provided in an embodiment of the present invention, such as Fig.10 As shown, the heartbeat message processing unit 204 includes:

[0103] The step-down subunit 2041 is used for forcing the old leader node to step down after all other nodes receive the heartbeat message, if the current node is a follower node and the term of the new leader node it receives is higher than the term of the old leader node that still sends heartbeat messages to the current node, and enters the information storage subunit;

[0104] If the current node is a leader node, and its term of office is lower than the term of office of the new leader node received, it means that the current node is the old leader node, and the current node passively steps down, switches the node state to a follower node, and enters the storage information subunit;

[0105] If the current node is a candidate node, the election process ends directly, and the node status is switched to a follower node, and the information storage subunit is entered;

[0106] The storage information subunit 2042 is used to store the information of the new leader node; the update term is the working term of the new leader node at this time.

[0107] Beneficial effects of the present invention:

[0108] The present invention provides a method and system for electing a leader node, wherein the method comprises: S1, a non-leader node broadcasts an election message according to whether a timer has timed out; S2, each node processes the election request after receiving it and replies with a response message; the response message comprises: a rejection message and an approval message; S3, after receiving the response message, the candidate node counts the total number of votes obtained according to the response message, and judges whether it can become a leader node according to the total number of votes obtained; if so, broadcasts a heartbeat message to all other nodes; S4, after receiving the heartbeat message, all other nodes cause the old leader node to step down according to the term of the received new leader node and store the information of the new leader node. The method of the present invention uses a method of separating the election term from the working term to achieve the maintenance of the leader node term, and when a candidate node triggers an election locally, the leadership position of the leader node cannot be shaken. It can effectively prevent the election of a new leader node in a local node from interrupting the normal operation of the current leader node, and can prevent the emergence of multiple leader nodes.

[0109] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for selecting a leader node, characterized in that: include: S1, non-leader nodes broadcast election messages based on whether the timer has timed out; S2. Each node receives the election request, processes it and replies with a response message; The response message includes: a rejection message and an approval message; S3. After receiving the response message, the candidate node counts the total number of votes according to the response message, and determines whether it can become the leader node according to the total number of votes; if so, broadcasts the heartbeat message to all other nodes; S4. After receiving the heartbeat message, all other nodes step down the old leader node according to the term of the new leader node received and store the new leader node information.

2. The method according to claim 1, characterized in that: The non-leader nodes include: follower nodes and candidate nodes; The S1 includes: S11, if the current node is a follower node, check whether the heartbeat time of the current node from the leader node has timed out. If not, it remains a follower node and does not broadcast election messages; if it has timed out, switch the node state to a candidate node; and enter S12; if the current node is a candidate node, check whether the timer of the current node has timed out. If not, it remains a candidate node and does not broadcast election messages; if it has timed out and has not received the consent vote of the majority of nodes and the message that other nodes have become leader nodes, it remains a candidate node and enters S12; S12. Update the election term to the current election term plus 1, and update the election time to the current system time; broadcast the election message and conduct a new round of leadership node election.

3. The method according to claim 1, characterized in that The S2 includes: S21. If the current node is the leader node, directly reject the election request and reply with a rejection message; If the current node is a candidate node, and the election term of the current node is not less than the requested term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, the node state is switched to a follower node, and S22 is entered; If the current node is a follower node and the current node normally receives the heartbeat of the leader node within the timer, the election request is directly rejected and a rejection message is replied; or if the election term of the current node is not less than the request term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, enter S22; S22. Update the election term to the current requested term, and update the election time to the current system time; agree to the election and reply with an agreement message.

4. The method according to claim 1, characterized in that: The S3 includes: S31. If the response message is a rejection message, enter S32; if the response message is an approval message, enter S33; S32: When it is determined that the message is a rejection message sent by the leader node, the election is terminated and the node state is switched to a follower node; When it is determined that the candidate node sends a rejection message, if the election term of the peer node is higher than the election term of the current node, the election ends and the node status is switched to a follower node. At the same time, the election term of the current node is updated to the election term of the peer node. If the election term of the peer node is not higher than the election term of the current node, the message is not processed. S33, add 1 to the number of votes received; S34. After counting the total number of votes, determine whether the total number of votes is greater than or equal to N / 2+1, where N is the total number of nodes; if the condition is not met, it remains a candidate node and continues to the next election; if the condition is met, switch the node state to the leader node, update the working term to the election term of the winner at this time, and broadcast heartbeat messages to all other nodes to interrupt the election process of all other nodes.

5. The method according to claim 1, characterized in that The S4 includes: S41, after all other nodes receive the heartbeat message, if the current node is a follower node and the term of the new leader node it receives is higher than the term of the old leader node that still sends heartbeat messages to the current node, the old leader node is forced to step down and enter S42; If the current node is a leader node, and its term of office is lower than the term of office of the new leader node received, it means that the current node is an old leader node, and the current node passively steps down, switches the node state to a follower node, and enters S42; If the current node is a candidate node, the election process ends directly, and the node state is switched to a follower node, and then enters S42; S42. Store the information of the new leader node; update the term to be the working term of the new leader node at this time.

6. A leadership node election system, characterized in that: include: Broadcast election unit, used for non-leader nodes to broadcast election messages based on whether the timer has timed out; An election request processing unit is used for each node to process the election request received and reply with a response message; The response message includes: a rejection message and an approval message; The response message processing unit is used to count the total number of votes after the candidate node receives the response message, and determine whether it can become the leader node according to the total number of votes; if so, broadcast the heartbeat message to all other nodes; The heartbeat message processing unit is used to make the old leader node step down and store the new leader node information according to the term of the new leader node received by all other nodes after receiving the heartbeat message.

7. The system according to claim 6, characterized in that: The non-leader nodes include: follower nodes and candidate nodes; The broadcast election unit comprises: The timeout judgment subunit is used to check whether the heartbeat time of the current node from the leader node has timed out if the current node is a follower node. If not, it remains a follower node and does not broadcast election messages; if it has timed out, the node state is switched to a candidate node and enters the broadcast election subunit; if the current node is a candidate node, check whether the timer of the current node has timed out. If not, it remains a candidate node and does not broadcast election messages; if it has timed out and has not received the consent vote of the majority of nodes and messages from other nodes to become leader nodes, it remains a candidate node and enters the broadcast election subunit; The broadcast election subunit is used to update the election term to the current election term plus 1, and update the election time to the current system time; broadcast the election message to conduct a new round of leadership node election.

8. The system according to claim 6, characterized in that The election request processing unit comprises: The election request judgment processing subunit is used to directly reject the election request and reply with a rejection message if the current node is the leader node; If the current node is a candidate node, and the election term of the current node is not less than the requested term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, the node state is switched to a follower node, and the election consent subunit is entered; If the current node is a follower node and the current node normally receives the heartbeat of the leader node within the timer, the election request is directly rejected and a rejection message is replied; or if the election term of the current node is not less than the requested term of the candidate node that broadcasts the election message, the election request is directly rejected and a rejection message is replied; otherwise, the election consent subunit is entered; The election approval subunit is used to update the election term to the current requested term and the election time to the current system time; approve the election and reply to the approval message.

9. The system according to claim 6, characterized in that The response message processing unit includes: The response message judgment subunit is used to enter the node judgment processing subunit if the response message is a rejection message; if the response message is an approval message, enter the vote counting subunit; The node judgment processing subunit is used to terminate the election and switch the node state to a follower node when judging that the message is a rejection message sent by the leader node; When it is determined that the candidate node sends a rejection message, if the election term of the peer node is higher than the election term of the current node, the election ends and the node status is switched to a follower node. At the same time, the election term of the current node is updated to the election term of the peer node. If the election term of the peer node is not higher than the election term of the current node, the message is not processed. The vote counting subunit is used to add 1 to the number of votes received; The vote judgment subunit is used to judge whether the total number of votes is greater than or equal to N / 2+1 after counting the total number of votes, where N is the total number of messages; if the condition is not met, it remains a candidate node and continues to the next election; if the condition is met, the node state is switched to a leader node, and the working term is updated to the election term of the winner at this time, and a heartbeat message is broadcast to all other nodes to interrupt the election process of all other nodes.

10. The system according to claim 6, characterized in that The heartbeat message processing unit comprises: The step-down sub-unit is used for forcing the old leader node to step down and enter the information storage sub-unit after all other nodes receive the heartbeat message. If the current node is a follower node and the term of the new leader node it receives is higher than the term of the old leader node that still sends heartbeat messages to the current node, the old leader node is forced to step down and enter the information storage sub-unit; If the current node is a leader node, and its term of office is lower than the term of office of the new leader node received, it means that the current node is the old leader node, and the current node passively steps down, switches the node state to a follower node, and enters the storage information subunit; If the current node is a candidate node, the election process ends directly, and the node status is switched to a follower node, and the information storage subunit is entered; The storage information subunit is used to store the information of the new leader node; the update term is the working term of the new leader node at this time.

Citation Information

Cited By

  • Data synchronization method and device, equipment and medium

    CN119537483A

  • Method and system for quickly electing leader node

    CN121261868A

  • Block chain consensus method and device, equipment, storage medium and product

    CN121333868A