A consensus method and distributed system with high consensus efficiency
By introducing preliminary candidate nodes and qualification review mechanisms into the distributed system, outdated nodes are screened out, the problem of election extension caused by network isolation partitions is solved, and efficient consensus and stable operation in the distributed system are achieved.
Patent Information
- Application Number
- CN202210674762.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-15
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2042-06-15
AI Technical Summary
In distributed systems, network isolation and partitioning cause nodes to lose connection, affecting consensus efficiency and system stability. Especially when the isolated node wins the election, it will increase the system's consensus time and disrupt the normal operation of the cluster.
Introduce the role of the preliminary candidate node, carry the term value and log index through the qualification review request message, screen out outdated nodes, prevent them from participating in the leader election, and keep the term unchanged before the follower node is converted into a preliminary candidate, saving the election cycle.
When the cluster is running normally, the election time is basically the same as the Raft algorithm, but when outdated nodes occur, the election time is greatly reduced, the consensus efficiency is improved, and the security and stability of the system are enhanced.
Smart Images

Figure CN115102967B
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of distributed systems, and in particular relates to a consensus method and a distributed system with high consensus efficiency. Background Art
[0002] In a distributed system, how to ensure that the data in all nodes in the cluster are exactly the same and can reach a consensus on a proposal is the core issue for the normal operation of the distributed system, and the consensus algorithm is a method used to ensure the consistency of the distributed system.
[0003] In 1998, Lamport Leslie first formalized the discussion and proof of the distributed consistency problem under the non-Byzantine Generals Problem model, and proposed the nearly axiomatic classic Paxos algorithm. This was the beginning of the formalization of the distributed consistency problem. After several generations of improvements, the Paxos theory was further improved and republished. The introduction of the distributed replica state machine (State Machine Replication) model has enabled the research model of the distributed consistency problem to transition from a formal model to an application field. Diego Ongaro and Dr. John Ousterhout of Stanford proposed the Raft algorithm in 2014 based on the distributed replica state machine model to address the problem that the classic Paxos is difficult to understand, read, and implement in engineering. The Raft algorithm is currently a commonly used consensus algorithm. All servers in the Raft cluster have three node roles: leader node, follower node, and candidate node. State transitions can occur between these three node roles.
[0004] In the Raft algorithm, if network isolation and partitioning occurs due to network problems, that is, a node is "isolated" from the entire cluster and "lost connection", re-election will inevitably occur. The Raft algorithm ensures that all nodes always use the highest term value they observe from other servers. This property is the key to ensuring the robustness of the Raft algorithm, and elections become certain. The consistency property of log replication also depends on this property. However, if the isolated and "lost connection" nodes are allowed to participate in the leader election, the normal operation of the cluster will be disrupted and the consensus efficiency of the entire system will be seriously affected. If the isolated and "lost connection" nodes win the election, the consensus time of the system will be increased. Summary of the invention
[0005] The purpose of the embodiments of the present invention is to provide a consensus method and distributed system with high consensus efficiency, so as to ensure the normal operation of the distributed cluster when network isolation and partitioning occurs due to network problems, save election time and improve consensus efficiency. The specific technical solution is as follows:
[0006] In a first aspect, an embodiment of the present invention provides a consensus method with high consensus efficiency, which is applied to a distributed system, wherein the distributed system includes four node roles, namely, a leader node, a follower node, a candidate node, and a reserve candidate node; the method includes:
[0007] When any follower node meets the timeout status, it will change its node role to a candidate node while keeping its term unchanged;
[0008] The preliminary candidate node sends a qualification review request message to the remaining nodes in the distributed system; wherein the qualification review request message carries the term value and log index of the preliminary candidate node;
[0009] Each node that receives the qualification review request message determines whether the preliminary candidate node meets the conditions for being updated to a candidate node based on its own term value, log index and the qualification review request message, determines qualification voting information according to the judgment result, and generates a response message based on the qualification voting information and returns it to the preliminary candidate node; wherein, if it is determined that the preliminary candidate node meets the conditions for being updated to a candidate node, the qualification voting information is a yes vote, otherwise it is a no vote;
[0010] The candidate preparation node counts whether the number of affirmative votes in the received response message exceeds a preset number;
[0011] If more than that, the candidate node changes its node role to a candidate node to initiate the leader election process;
[0012] If not, the candidate node changes its node role to a follower node, so that the distributed system waits for the new follower node to meet the timeout state.
[0013] In one embodiment of the present invention, the preliminary candidate node sends a qualification review request message to the remaining nodes in the distributed system, including:
[0014] The preliminary candidate node broadcasts a qualification review request message to other nodes in the distributed system.
[0015] In one embodiment of the present invention, each node that receives the qualification review request message determines whether the preliminary candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, including:
[0016] Each node receiving the qualification review request message parses the qualification review request message to obtain the term value and log index of the reserve candidate node;
[0017] Determine whether the term value of the reserve candidate node is less than its own term value;
[0018] If so, determining that the preliminary candidate node does not meet the conditions for being updated to a candidate node;
[0019] If not, determine whether the log index of the candidate node is smaller than its own log index;
[0020] If the log index of the preliminary candidate node is smaller than the log index of the preliminary candidate node, it is determined that the preliminary candidate node does not meet the conditions for being updated to a candidate node;
[0021] If the log index of the preliminary candidate node is greater than or equal to its own log index, it is determined that the preliminary candidate node meets the condition for being updated to a candidate node.
[0022] In one embodiment of the present invention, the content of the response message includes:
[0023] Eligibility voting information and response term values;
[0024] Among them, if the qualification voting information is an affirmative vote, the response term value is the term value of the reserve candidate node; otherwise, the response term value is the term value of the node that receives the qualification review request message.
[0025] In one embodiment of the present invention, the preliminary candidate node counts whether the number of affirmative votes in the received response message exceeds a preset number, including:
[0026] The preliminary candidate node counts whether the number of affirmative votes in the received response message exceeds half of the total number of nodes in the distributed system.
[0027] In a second aspect, an embodiment of the present invention provides a distributed system with high consensus efficiency, including four node roles: a leader node, a follower node, a candidate node, and a reserve candidate node, wherein:
[0028] The follower node is used to change its node role to a reserve candidate node while maintaining its term unchanged when the timeout state is met;
[0029] The preliminary candidate node is used to send a qualification review request message to the remaining nodes in the distributed system; wherein the qualification review request message carries the term value and log index of the preliminary candidate node;
[0030] Each node other than the preparatory candidate node that receives the qualification review request message is used to determine whether the preparatory candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, determine qualification voting information according to the judgment result, and generate a response message based on the qualification voting information and return it to the preparatory candidate node; wherein, if it is determined that the preparatory candidate node meets the conditions for updating to a candidate node, the qualification voting information is a positive vote, otherwise it is a negative vote;
[0031] The reserve candidate node is also used to count whether the number of affirmative votes in the received response message exceeds a preset number; if so, its own node role is changed to a candidate node to initiate a leader election process; if not, its own node role is changed to a follower node so that the distributed system waits for the new follower node to meet the timeout state.
[0032] In one embodiment of the present invention, when the preliminary candidate node sends a qualification review request message to the remaining nodes in the distributed system, it is specifically used to:
[0033] Broadcasting a qualification review request message to other nodes in the distributed system.
[0034] In one embodiment of the present invention, each node that receives the qualification review request message, when judging whether the preliminary candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, is specifically used to:
[0035] Parsing the qualification review request message to obtain the term value and log index of the reserve candidate node;
[0036] Determine whether the term value of the reserve candidate node is less than its own term value;
[0037] If so, determining that the preliminary candidate node does not meet the conditions for being updated to a candidate node;
[0038] If not, determine whether the log index of the candidate node is smaller than its own log index;
[0039] If the log index of the preliminary candidate node is smaller than the log index of the preliminary candidate node, it is determined that the preliminary candidate node does not meet the conditions for being updated to a candidate node;
[0040] If the log index of the preliminary candidate node is greater than or equal to its own log index, it is determined that the preliminary candidate node meets the condition for being updated to a candidate node.
[0041] In one embodiment of the present invention, the content of the response message includes:
[0042] Eligibility voting information and response term values;
[0043] Among them, if the qualification voting information is an affirmative vote, the response term value is the term value of the reserve candidate node; otherwise, the response term value is the term value of the node that receives the qualification review request message.
[0044] In one embodiment of the present invention, when counting whether the number of affirmative votes in the received response message exceeds a preset number, the preliminary candidate node is specifically configured to:
[0045] It is counted whether the number of affirmative votes in the received response messages exceeds half of the total number of nodes in the distributed system.
[0046] Beneficial effects of the present invention:
[0047] In view of the phenomenon of network isolation and partition, the embodiment of the present invention introduces the concept of obsolete nodes, and the nodes that are "lost" in the cluster are called obsolete nodes. A new role, namely the reserve candidate node, is introduced on the basis of the three roles of the original Raft algorithm. In addition, the proposed method also adds a qualification review request message initiated by the reserve candidate node and carrying the term value and log index of the reserve candidate node, which is used to perform the qualification review in the stage of electing formal candidates. The node that receives the qualification review request message "screens" out the obsolete nodes by using its own term value, log index and the corresponding content in the qualification review request message, so that it cannot participate in the formal leader election. And the method of the embodiment of the present invention can save an election cycle by not changing the setting of the term value of the node before the follower node is converted to the reserve candidate role.
[0048] Under normal operation of the cluster, the election time required by the method of the embodiment of the present invention is basically the same as the election time required by the Raft algorithm. However, when outdated nodes appear in the cluster, the time overhead of the method of the embodiment of the present invention is greatly reduced compared with the original Raft algorithm, thereby being able to save election time, improve consensus efficiency, and significantly improve the performance of the distributed system, and enhance its security and stability while ensuring the normal operation of the distributed cluster. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] Figure 1(a) is a schematic diagram illustrating a situation where a Leader node loses connection according to an embodiment of the present invention;
[0050] Figure 1 (b) is a schematic diagram illustrating a situation where a Follower node and a Candidate node lose connection according to an embodiment of the present invention;
[0051] Figure 2 A schematic diagram of a process flow of a consensus method with high consensus efficiency provided by an embodiment of the present invention;
[0052] Figure 3 A schematic diagram of the state transition of each node role in the consensus method provided by an embodiment of the present invention;
[0053] Figure 4 An algorithm logic flow chart of the PreC-Raft method provided in an embodiment of the present invention;
[0054] Figure 5 This is a comparison diagram of the cluster election time consumption when there are outdated nodes in the experiment of the embodiment of the present invention;
[0055] Figure 6 This is a comparison chart of the time consumption for elections in a normal environment in an experiment of an embodiment of the present invention;
[0056] Figure 7 This is a comparison chart of consensus delay in the experiment of the embodiment of the present invention. DETAILED DESCRIPTION
[0057] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of 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.
[0058] In order to facilitate understanding of the solution, the scenarios and problems targeted by the present invention are briefly introduced.
[0059] The Raft algorithm is a distributed consistency algorithm designed by D. Ongaro and J. K. Ousterhout in 2014 for easy understanding and application. Since the Raft algorithm was proposed, its popularity has been rising. It has been widely studied and applied in scientific research and industrial fields, and has become the preferred distributed consistency solution. Compared with the bottom-up solution of the Paxos algorithm, the Raft algorithm adopts a top-down solution of a replicated state machine, which fully describes how multiple individuals reach a decision on a series of values. Therefore, it is easier to understand and easier to practice in engineering.
[0060] The Raft algorithm is a consensus algorithm based on a strong leader. It is essentially a complete variant of the Paxos algorithm. The Raft algorithm solves the shortcomings of the Paxos algorithm, which is difficult to understand and implement, in a concise way. By simplifying the Paxos algorithm model, setting three node roles: leader, candidate, and follower, and restricting them on the basis of the Paxos algorithm, the Raft algorithm decomposes the consistency problem into three relatively independent sub-problems from the perspective of simplifying logic and facilitating implementation.
[0061] 1) Leader Election
[0062] When the cluster is first created or the leader goes down, a new leader needs to be elected. At the beginning, the initial state of each server node is a follower, and it will wait until it receives a heartbeat message from the leader or its local clock reaches a timeout. The server that completes the timeout countdown will first try to become the leader, and the role state will be converted to the candidate state. Candidates participate in the leader election by initiating RequestVote. Once a candidate receives more than half of the votes, the election is successful and the candidate becomes the leader.
[0063] 2) Log Replication
[0064] The leader receives requests from clients to append commands to its own log and replicates them to all its followers in the cluster in the form of log entries, requiring the logs of other nodes to be consistent with its own. Once more than half of the servers have appended the command and responded affirmatively to the leader, the command is committed and the leader can apply the command to its state machine. The committed command will eventually be executed on all servers. At any time, a follower must check whether its previous log entry is consistent with the leader before appending a new entry from the leader. If inconsistent, the follower will delete all inconsistent entries until the most recent overlapping entry, and then gradually ask for the missing logs and append them. By doing this, the follower will eventually have the same log entries as the leader. Each log entry contains the term number it was appended to the leader's log and an increasing index number to identify its position.
[0065] 3) Safety
[0066] In order to maintain consistency of committed commands in servers and clients, it is necessary to prevent a server that does not contain all committed log entries from winning. Therefore, followers only vote for candidates that are consistent with all their committed log entries. With these restrictions, it can be proven that no committed entry will be deleted again, and eventually all committed entries will be executed on every server. If a server node has applied a certain log entry to its state machine, then other server nodes cannot apply a different instruction at the same log index position.
[0067] In the traditional Raft consensus algorithm, if a node is "isolated" from the entire cluster due to network problems, that is, network isolation partition occurs, the "isolated" node will be "disconnected", which will inevitably lead to the re-election of the distributed system. However, considering the different roles of the "isolated" nodes, other remaining nodes may be affected in different ways. The following is a detailed analysis of the disconnection of different node roles:
[0068] (1) Leader node loss of connection
[0069] like Figure 1 As shown in (a), Figure 1 (a) is a schematic diagram illustrating the situation where the leader node loses connection according to an embodiment of the present invention. Each node is represented by a circle, L represents the leader node, F1 to F4 represent the follower nodes, the dotted circle around the leader node represents that it is "isolated", and the cross "×" represents that the leader node loses connection with the follower node.
[0070] Figure 1 Take the cluster of 5 existing nodes as an example. When the Leader node is "isolated", the remaining 4 Follower nodes will initiate a re-election to generate a new Leader node, and the term values of these Follower nodes will also be updated. However, the role of the original Leader node will remain unchanged, and its term value will also remain unchanged. That is, there will be 2 Leader nodes and corresponding term values in these 5 nodes at the same time. When the network connection is restored, since the term value of the original Leader node is smaller than the term value of the new Leader node, the original Leader node will become the Follower node of the new Leader node, and the cluster will be stable again.
[0071] (2) Disconnection between Follower and Candidate nodes
[0072] like Figure 1 (b) Figure 1(b) is a schematic diagram illustrating the disconnection of a Follower node and a Candidate node according to an embodiment of the present invention. Still taking the cluster having 5 existing nodes as an example, the disconnection of a Follower node is used as an example for specific description. The Follower node F4 loses connection with other nodes in the cluster due to a network problem.
[0073] Since Follower node F4 has not received any AppendEntry messages from the Leader node (messages responsible for log replication between the leader and its followers), it will time out repeatedly and will continuously initiate new leader elections. However, since it can only get its own vote and cannot obtain a majority of votes, the election will fail. Even if the election fails, it will continue to vote, which will cause its election term value to increase continuously until it reconnects with the lost cluster. Since the term value of this node has been increasing during the "isolation" process, its term value is generally larger than the term values of other nodes in the cluster, resulting in a high probability that the node will become the Leader node after the connection is restored. It will force other nodes to update their terms, and other nodes will become the Follower nodes of this node under this term.
[0074] For the Candidate node, when the Candidate node loses connection with the cluster, it cannot get the votes of the majority of nodes, so the node role status will automatically be downgraded to the Follower node. The situation is exactly the same as the above Follower node loss of connection, which can be seen above and Figure 1 (b) Understanding.
[0075] Since the Raft algorithm ensures that all nodes always use the highest term value they observe from other servers, this property is the key to ensuring the robustness of the algorithm, and elections become certain as a result, and the consistency property of log replication also depends on this. However, combined with the above analysis of the disconnection of different node roles, the "disconnected" nodes in the cluster will directly participate in the leader election in the Raft algorithm, but since such nodes have just resumed communication from the "isolated" state, they have not updated the log in time and the term value is too large. If these nodes are allowed to participate in the leader election, the consensus efficiency of the entire system will be seriously affected. If such nodes win the election, the consensus time of the entire system will increase.
[0076] The embodiment of the present invention is aimed at a problem that may occur in a distributed system - the phenomenon of network isolation and partitioning due to network problems causes the node to "lose connection". In response to this problem, the embodiment of the present invention introduces the concept of outdated nodes, that is, the nodes that appear "lost connection" in the above cluster are called outdated nodes, and a new consensus method is proposed to ensure that the distributed cluster can operate normally when this problem occurs, and to achieve the purpose of saving election time and improving consensus efficiency. Based on this, the embodiment of the present invention provides a consensus method and distributed system with high consensus efficiency.
[0077] Below, firstly, a consensus method with high consensus efficiency provided by an embodiment of the present invention is introduced. A consensus method with high consensus efficiency provided by an embodiment of the present invention is applied to a distributed system, wherein the distributed system includes four node roles, namely, a leader node, a follower node, a candidate node, and a reserve candidate node. Figure 2 As shown, the method may include the following steps:
[0078] S1: When any follower node meets the timeout status, it will change its node role to a reserve candidate node while keeping its term unchanged.
[0079] Based on the Raft algorithm, the embodiment of the present invention adds a node role to the distributed system, namely, the preparatory candidate node. Therefore, the consensus method provided by the embodiment of the present invention can also be referred to as the PreC-Raft method.
[0080] In the distributed system of the embodiment of the present invention, the leader node, follower node, candidate node and candidate node have their own division of labor and jointly maintain the stability of the consensus of the distributed system. For the conversion process of the node role status in the method of the embodiment of the present invention, please refer to Figure 3 , Figure 3 A schematic diagram of the state transition of each node role in the consensus method provided in an embodiment of the present invention.
[0081] The method of the embodiment of the present invention starts with a follower node meeting the timeout state. Following the timeout mechanism of the Raft algorithm, it can be understood that if a follower node does not receive a heartbeat message from the leader node within its random timeout period, the follower node reaches the timeout state. According to the Raft algorithm, the follower node will increase the term value and directly convert to a candidate node, and then initiate the leader election process. Specifically, the candidate node will send RPCs requesting votes to other nodes in the cluster in parallel to vote for itself. When more than half of the votes in favor are obtained, the candidate node will be transformed into a leader node, and so on. For the processing process after the candidate node initiates the leader election process, please refer to the Raft algorithm understanding, which will not be explained in detail here.
[0082] In the embodiment of the present invention, a follower node that meets the timeout state will not increase the term value and directly transform into a candidate node, but will keep the term unchanged and first transform into a provisional candidate node and then enter the qualification review stage to determine whether it is qualified to transform into a formal candidate node.
[0083] S2, the preliminary candidate node sends a qualification review request message to the remaining nodes in the distributed system.
[0084] After adding the node role of the preliminary candidate node, the embodiment of the present invention further adds a qualification review request initiated by the preliminary candidate node, which can be referred to as initiating a PreC PRC request, that is, the preliminary candidate node sends a qualification review request message to the remaining nodes in the distributed system, which is referred to as a PreC PRC message.
[0085] The embodiment of the present invention uses PreC RPC messages to complete the qualification review from the preliminary candidate to the formal candidate election stage, which is used to detect whether the preliminary candidate node becomes an outdated node due to network isolation to prevent it from disrupting the normal operation of the cluster. The qualification review request message carries the term value and log index of the preliminary candidate node. See Table 1 for details.
[0086] Table 1 Parameters and meanings in the PreC RPC message
[0087]
[0088] The candidate preparation node may send a qualification review request message to each of the remaining nodes in the distributed system individually.
[0089] Alternatively, in an optional implementation manner, the preliminary candidate node sending a qualification review request message to the remaining nodes in the distributed system may include:
[0090] The preliminary candidate node broadcasts a qualification review request message to other nodes in the distributed system.
[0091] It can be understood that, through broadcasting, the remaining nodes can receive the qualification review request message in parallel, which can save communication resources and improve communication efficiency.
[0092] S3, each node that receives the qualification review request message determines whether the preliminary candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, determines the qualification voting information according to the judgment result, and generates a response message based on the qualification voting information and returns it to the preliminary candidate node.
[0093] Among them, the eligibility voting information is determined according to the judgment result. If it is determined that the candidate node to be updated meets the conditions for being updated to a candidate node, the eligibility voting information is a yes vote; otherwise, it is a no vote.
[0094] For each node that receives the eligibility review request message, the node compares the corresponding items of the eligibility review request message based on its own term value and log index to determine whether the candidate node to be updated meets the conditions for being updated to a candidate node. If it is determined that the candidate node to be updated meets the conditions for being updated to a candidate node, the eligibility voting information given by the node is a yes vote; if it is determined that the candidate node to be updated does not meet the conditions for being updated to a candidate node, the eligibility voting information given by the node is a no vote.
[0095] In an optional implementation manner, for each node that receives the eligibility review request message, determining whether the candidate node to be updated meets the conditions for being updated to a candidate node based on its own term value, log index, and the eligibility review request message includes:
[0096] a), Each node that receives the eligibility review request message parses the eligibility review request message to obtain the term value and log index of the candidate node to be updated.
[0097] The following takes any node that receives the eligibility review request message as an example. For simplicity, this node is referred to as the receiving node. Specifically, the receiving node parses and obtains the PreC Term and PreC Commit Index in the PreC PRC message shown in Table 1.
[0098] b), Determine whether the term value of the candidate node to be updated is less than its own term value.
[0099] It can be understood that the receiving node itself has a term value, simply referred to as Current Term. The receiving node first determines whether the term value of the candidate node to be updated is less than its own term value, that is, whether there is: PreC Term < Current Term.
[0100] If so, execute c), and determine that the candidate node to be updated does not meet the conditions for being updated to a candidate node.
[0101] Specifically, if PreC Term < Current Term is satisfied, the receiving node determines that the candidate node to be updated does not meet the conditions for being updated to a candidate node, and the eligibility voting information given by the receiving node is a no vote, which can be a False message.
[0102] Otherwise, execute d), and determine whether the log index of the preliminary candidate node is less than its own log index.
[0103] It can be understood that the receiving node itself has a log index, simply referred to as the Commit Index.
[0104] If PreC Term ≥ Current Term, the receiving node needs to further determine whether the log index of the preliminary candidate node is less than its own log index, that is, whether it satisfies: PreC Commit Index < Commit Index.
[0105] e), if the log index of the preliminary candidate node is less than its own log index, determine that the preliminary candidate node does not meet the conditions for updating to a candidate node.
[0106] If PreC Commit Index < Commit Index, the receiving node determines that the preliminary candidate node does not meet the conditions for updating to a candidate node, and the eligibility voting information given by the receiving node is a negative vote.
[0107] f), if the log index of the preliminary candidate node is greater than or equal to its own log index, determine that the preliminary candidate node meets the conditions for updating to a candidate node.
[0108] If PreC Commit Index ≥ Commit Index, the receiving node determines that the preliminary candidate node meets the conditions for updating to a candidate node, and the eligibility voting information given by the receiving node is a positive vote, which can be a True message.
[0109] After determining the eligibility voting information according to the judgment result, the receiving node also needs to generate a response message based on the eligibility voting information and return the response message to the preliminary candidate node.
[0110] In an optional implementation manner, the content of the response message includes:
[0111] Eligibility voting information and response term value;
[0112] Specifically, the response message can be called a PreC RPC response message, and the specific content can be seen in Table 2.
[0113] Table 2 Parameter and meaning description in the PreC RPC response message
[0114]
[0115] Among them, if the qualification voting information is an affirmative vote, the response term value is the term value of the reserve candidate node; otherwise, the response term value is the term value of the node that receives the qualification review request message.
[0116] Specifically, for the above steps c) and e), the receiving node determines that the preliminary candidate node does not meet the conditions for updating to a candidate node, and after giving a negative vote, the response term value of the receiving node can be determined to be its own term value, that is, Term = Current Term.
[0117] With respect to the above step f), the receiving node determines that the pre-candidate node meets the conditions for being updated to a candidate node, and after giving an affirmative vote, the response term value of the receiving node can be determined to be the term value of the pre-candidate node, that is, Term=PreC Term.
[0118] Afterwards, the receiving node sends a response message to the preliminary candidate node. It can be understood that each receiving node sends a response message generated by itself to the preliminary candidate node. Each response message carries the qualification voting information of the corresponding receiving node, which may be a vote in favor or a vote against.
[0119] S4, the candidate preparation node counts whether the number of affirmative votes in the received response message exceeds a preset number.
[0120] In the embodiment of the present invention, the preliminary candidate node may cumulatively count the number of affirmative votes in the received response message, and determine how to change the state of its own node role according to whether the number of affirmative votes exceeds a preset number.
[0121] A universal fixed preset number may be pre-set according to the usual number range of servers in different distributed systems. For example, if the number of servers in the distributed system is between 100 and 200, a preset number of 60 may be set.
[0122] Of course, the preset number may also be determined according to the specific number of servers in the current distributed system. For example, in an optional implementation manner, the candidate node counts whether the number of affirmative votes in the received response message exceeds the preset number, including:
[0123] The preliminary candidate node counts whether the number of affirmative votes in the received response message exceeds half of the total number of nodes in the distributed system.
[0124] It will be understood by those skilled in the art that, if the number of affirmative votes of any node in a distributed system exceeds half of the total number of nodes in the distributed system, it indicates that most nodes believe that the reserve candidate node is qualified to be updated to a candidate node; if the number of affirmative votes does not exceed half of the total number of nodes in the distributed system, it indicates that most nodes believe that the reserve candidate node is not qualified to be updated to a candidate node. Then, according to different situations, the reserve candidate node needs to change the status of the node role accordingly.
[0125] If it exceeds, S5 is executed, and the candidate node changes its node role to a candidate node to initiate a leader election process.
[0126] If the number of affirmative votes exceeds half of the total number of nodes in the distributed system, it means that the candidate node is not an "outdated node". The candidate node will change its node role to a candidate node, and then the candidate node will perform subsequent consensus operations according to the Raft algorithm and process according to the relevant process of initiating the leader election process. Figure 3 Understand, no further explanation is given here.
[0127] If not, executing S6, the candidate node changes its node role to a follower node, so that the distributed system waits for the new follower node to meet the timeout state.
[0128] If the number of affirmative votes does not exceed half of the total number of nodes in the distributed system, it means that the candidate node is an "outdated node". The candidate node changes its node role to a follower node, and then the distributed system waits for the new follower node to perform processing according to the method of the embodiment of the present invention again when the timeout state is met, and the specific description will not be repeated.
[0129] In view of the phenomenon of network isolation and partition, the embodiment of the present invention introduces the concept of obsolete nodes, and the nodes that are "lost" in the cluster are called obsolete nodes. A new role, namely the reserve candidate node, is introduced on the basis of the three roles of the original Raft algorithm. In addition, the proposed method also adds a qualification review request message initiated by the reserve candidate node and carrying the term value and log index of the reserve candidate node, which is used to perform the qualification review in the stage of electing formal candidates. The node that receives the qualification review request message "screens" out the obsolete nodes by using its own term value, log index and the corresponding content in the qualification review request message, so that it cannot participate in the formal leader election. And the method of the embodiment of the present invention can save an election cycle by not changing the setting of the term value of the node before the follower node is converted to the reserve candidate role.
[0130] Under normal operation of the cluster, the election time required by the method of the embodiment of the present invention is basically the same as that required by the Raft algorithm. However, when there are stale nodes in the cluster, in terms of time overhead, the method of the embodiment of the present invention is significantly reduced compared to the original Raft algorithm, so as to save election time, improve consensus efficiency, and significantly improve the performance of the distributed system, enhance its security and stability while ensuring the normal operation of the distributed cluster.
[0131] For the convenience of understanding the algorithm logic flow of the embodiment of the present invention, please refer to Figure 4 as shown in Figure 4 which is the algorithm logic flow chart of the PreC-Raft method provided by the embodiment of the present invention.
[0132] Specifically, first, if the follower node F (Follower) does not receive a heartbeat message from the leader node L (Leader) within its random timeout duration, and the follower node F (Follower) determines that state = timeout_state (timeout state) is satisfied, then the follower node F (Follower) updates its node role to a pre-candidate node P (PreCandidate).
[0133] Next, the pre-candidate node P (PreCandidate) broadcasts a qualification review request (PreC PRC request) in the distributed system, that is, broadcasts and sends a qualification review request message (PreC PRC message) to the other nodes.
[0134] For each receiving node that receives the PreC PRC message, the following processing is performed: first, it is judged whether there is a PreC Term < Current Term. If there is a PreC Term < Current Term, the receiving node generates a PreC PRC response message with Term = Current Term; Success = False and sends it to the pre-candidate node P (PreCandidate) to notify it that it does not meet the condition for updating to a candidate node.
[0135] If there is no PreC Term < Current Term, that is, there is a PreC Term ≥ Current Term, the receiving node further judges whether there is a PreC Commit Index < Commit Index;
[0136] If PreC Commit Index < Commit Index exists, the receiving node generates a PreC PRC response message carrying Term = Current Term; Success = False and sends it to the pre-candidate node P (PreCandidate) to notify that it does not meet the conditions for updating to a candidate node.
[0137] If PreC Commit Index < Commit Index does not exist, that is, PreC Commit Index ≥ Commit Index exists, the receiving node generates a PreC PRC response message carrying Term = PreC Term; Success = True and sends it to the pre-candidate node P (PreCandidate) to notify that it meets the conditions for updating to a candidate node.
[0138] The pre-candidate node P (PreCandidate) parses the Success value in the received PreC PRC response message, then counts the number of True. And it judges whether the number of True, True num, satisfies True num ≥ |n / 2| + 1, where n is the total number of nodes in the distributed system.
[0139] If True num ≥ |n / 2| + 1 is satisfied, the pre-candidate node P (PreCandidate) transforms into a candidate node C (Candidate), and the candidate node C (Candidate) initiates a leader election process to end the consensus process.
[0140] If True num ≥ |n / 2| + 1 is not satisfied, the pre-candidate node P (PreCandidate) transforms into a follower node F (Follower). At this time, the distributed system waits for a new follower node F (Follower) to meet the timeout state, and when a new follower node F (Follower) meets the timeout state, the above process will be executed again.
[0141] The following uses experimental data to illustrate the effectiveness of the method of the embodiment of the present invention.
[0142] The experiment of the embodiment of the present invention runs on a Windows 10 system configured with an i7-1065G7 processor and 16GB of memory. The experimental code is implemented in the Java language. For the two cases of whether there are obsolete nodes in the cluster, the influence of the original Raft algorithm and the proposed PreC-Raft algorithm on the election time consumption is respectively tested.
[0143] ① Election time consumption when there are outdated nodes
[0144] In order to solve the problem of outdated nodes disrupting the normal operation of the cluster, an experiment was designed in the embodiment of the present invention to test the impact of outdated nodes on the cluster election time under two algorithms. Four groups of control experiments were designed specifically. Each group selected a different number of nodes, and each conducted 10 leader elections. At the same time, network isolation was artificially created to simulate outdated nodes. The outdated nodes were then added back to the cluster to test the cumulative election time of the cluster, and expressed in segments according to the term of office. In order to ensure the consistency of the experimental variables, the logs of the outdated nodes were set to lag behind the log sequence in the cluster, that is, the cluster reached a new consensus during the isolation period. The experimental results are as follows: Figure 5 As shown, Figure 5 This is a comparison chart of the election time of the cluster when there are outdated nodes in the experiment of the embodiment of the present invention. The horizontal axis is the number of nodes, and the vertical axis is the election time. PreC-Raft represents the method of the embodiment of the present invention. Raft-term1, Raft-term2 and Raft-term3 respectively represent the existing Raft algorithm for different terms.
[0145] Depend on Figure 5 It can be seen from the experimental results that as the number of nodes increases, the impact of outdated nodes on election time increases. Under the same number of nodes, when an outdated node appears in the Raft algorithm, the incumbent leader node will step down because the outdated node has a higher term, and a second leader election will be conducted, and the new node will be waited for to reach the timeout state again, which increases the time of a new round of elections. However, the outdated node formed by the PreC-Raft method of the embodiment of the present invention does not increase the term value and will not affect the running cluster state. Subsequently, the outdated node is transformed into a follower state and the log synchronization between it and the leader node is detected. Therefore, there is only one leader election process, which is significantly less than the unoptimized Raft algorithm version, thereby greatly saving the election time.
[0146] ②Normal elections take time
[0147] In order to verify the influence of the PreC-Raft method of the embodiment of the present invention on the election time under normal circumstances, five control experiments were designed with clusters consisting of different numbers of nodes. Each group conducted 100 leader elections, measured the election time and obtained the average value. The experimental results are as follows: Figure 6 shown. Figure 6 This is a comparison chart of the time consumption for elections in a normal environment in the experiment of the embodiment of the present invention.
[0148] observe Figure 6The experimental results show that the election time of the PreC-Raft method and the Raft algorithm in the embodiment of the present invention increases with the increase in the number of nodes. This is because when the number of nodes in the cluster increases, the difference in election time depends on the delay of the last affirmative vote. At the same time, the increase in network complexity also increases the message propagation delay. When the number of nodes is fixed, the election time of the PreC-Raft method in the embodiment of the present invention is slightly higher than that of the Raft algorithm. This is because the qualification review request message added by the algorithm is equivalent to one more voting request. Figure 5 and Figure 6 According to the measured data, when the number of nodes is 3, 7, 11 and 15 respectively, the increased time consumption of the qualification review request message accounts for 1.5%, 4.76%, 6.18% and 6.94% of the time overhead introduced by the outdated nodes participating in the election, and since the time units are all in milliseconds, they are negligible compared to the overall time consumption.
[0149] ③Consensus delay
[0150] When an outdated node joins the cluster during a consensus process, the consensus process will be blocked due to the outdated node's disruption of the cluster state until a leader node is selected to resume the consensus process. The embodiment of the present invention experimentally tests the impact of the PreC-Raft method on the consensus delay when an outdated node appears. Create network isolation for a single node to make it an outdated node. Assuming that the log is not behind the cluster, after the experiment starts, the outdated node and the log addition request are added to the cluster at the same time. The consensus delay is tested when the number of cluster nodes is different (3, 5, 7, 9, and 11 nodes respectively). The average delay of each group for 100 consensuses is tested and the average value is obtained. The experimental results are as follows. Figure 7 shown. Figure 7 This is a comparison chart of consensus delay in the experiment of the embodiment of the present invention.
[0151] The experimental results are in line with expectations. When an outdated node appears in the Raft algorithm, the original leader node in the cluster is converted to a follower because it has a higher term. The cluster waits for a new election timeout. At this time, only the outdated node and the original leader node in the cluster have the largest term value and the outdated node enters the election countdown first, so the outdated node reaches the election timeout first. Because the outdated node log is synchronized with the cluster, it can obtain the approval vote of the nodes in the cluster and become the leader. However, this process still disrupts the original operation state of the cluster and increases the term number of the cluster (possibly a nonlinear increase). At the same time, due to the resignation of the original leader, the ongoing consensus process is blocked and the consensus delay is prolonged. The outdated node in the PreC-Raft method of the embodiment of the present invention is first converted to the PreCandidate identity before becoming a candidate and increasing the term. The term remains unchanged, and the qualification review request message is broadcast to the follower nodes. The outdated node cannot receive any reply due to network isolation, so it will not be converted into a real candidate, and the term value does not change. When it rejoins the cluster, it will not affect the normal operation of the cluster, the consensus process is not hindered, and the consensus delay is at a normal level.
[0152] In summary, the method of the embodiment of the present invention can be used in the normal operation state of the distributed system. When the cluster is operating normally, the election time required by the PreC-Raft method is basically the same as the election time required by the Raft algorithm, and will not affect the performance of the distributed system. More importantly, the method of the embodiment of the present invention can deal with the problem of outdated nodes caused by network isolation and partitioning, and can significantly reduce the time overhead compared to the original Raft algorithm, save election time, and improve the security and stability of the distributed system during operation.
[0153] In a second aspect, corresponding to the above method embodiment, an embodiment of the present invention further provides a distributed system with high consensus efficiency, including four node roles, namely, a leader node, a follower node, a candidate node, and a reserve candidate node, wherein:
[0154] The follower node is used to change its node role to a reserve candidate node while maintaining its term unchanged when the timeout state is met;
[0155] The preliminary candidate node is used to send a qualification review request message to the remaining nodes in the distributed system; wherein the qualification review request message carries the term value and log index of the preliminary candidate node;
[0156] Each node other than the preparatory candidate node that receives the qualification review request message is used to determine whether the preparatory candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, determine qualification voting information according to the judgment result, and generate a response message based on the qualification voting information and return it to the preparatory candidate node; wherein, if it is determined that the preparatory candidate node meets the conditions for updating to a candidate node, the qualification voting information is a positive vote, otherwise it is a negative vote;
[0157] The reserve candidate node is also used to count whether the number of affirmative votes in the received response message exceeds a preset number; if so, its own node role is changed to a candidate node to initiate a leader election process; if not, its own node role is changed to a follower node so that the distributed system waits for the new follower node to meet the timeout state.
[0158] Optionally, when the candidate preparation node sends a qualification review request message to other nodes in the distributed system, it is specifically used to:
[0159] Broadcasting a qualification review request message to other nodes in the distributed system.
[0160] Optionally, each node that receives the qualification review request message, when judging whether the preliminary candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, is specifically used to:
[0161] Parsing the qualification review request message to obtain the term value and log index of the reserve candidate node;
[0162] Determine whether the term value of the reserve candidate node is less than its own term value;
[0163] If so, determining that the preliminary candidate node does not meet the conditions for being updated to a candidate node;
[0164] If not, determine whether the log index of the candidate node is smaller than its own log index;
[0165] If the log index of the preliminary candidate node is smaller than the log index of the preliminary candidate node, it is determined that the preliminary candidate node does not meet the conditions for being updated to a candidate node;
[0166] If the log index of the preliminary candidate node is greater than or equal to its own log index, it is determined that the preliminary candidate node meets the condition for being updated to a candidate node.
[0167] Optionally, the content of the response message includes:
[0168] Eligibility voting information and response term values;
[0169] Among them, if the qualification voting information is an affirmative vote, the response term value is the term value of the reserve candidate node; otherwise, the response term value is the term value of the node that receives the qualification review request message.
[0170] Optionally, when counting whether the number of affirmative votes in the received response message exceeds a preset number, the preliminary candidate node is specifically configured to:
[0171] It is counted whether the number of affirmative votes in the received response messages exceeds half of the total number of nodes in the distributed system.
[0172] For specific details, please refer to the consensus method with high consensus efficiency provided in the first aspect, which will not be elaborated here.
[0173] The distributed system with high consensus efficiency provided by the embodiment of the present invention introduces the concept of outdated nodes in response to the phenomenon of network isolation and partitioning, and the nodes that are "out of connection" in the cluster are called outdated nodes. A new role, namely the reserve candidate node, is introduced on the basis of the three roles of the original Raft algorithm. In addition, a qualification review request message initiated by the reserve candidate node and carrying the term value and log index of the reserve candidate node is added to perform the qualification review in the stage of electing formal candidates. The node that receives the qualification review request message "screens" out the outdated node by using its own term value, log index and the corresponding content in the qualification review request message, so that it cannot participate in the formal leader election. And the embodiment of the present invention can save an election cycle by not changing the setting of the node's term value before the follower node is converted to the reserve candidate role.
[0174] Under normal operation of the cluster, the election time required by the embodiment of the present invention is basically the same as the election time required by the Raft algorithm. However, when outdated nodes appear in the cluster, the time overhead of the embodiment of the present invention is significantly reduced compared with the original Raft algorithm, thereby being able to save election time, improve consensus efficiency, and significantly improve the performance of the distributed system, and enhance its security and stability while ensuring the normal operation of the distributed cluster.
[0175] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. A consensus method with high consensus efficiency, characterized in that: Applied in a distributed system, the distributed system includes four node roles, namely, a leader node, a follower node, a candidate node, and a reserve candidate node; the method includes: When any follower node first meets the timeout state, it changes its node role to the only candidate node in the distributed system while keeping its term unchanged; The preliminary candidate node sends a qualification review request message to the remaining nodes in the distributed system; wherein the qualification review request message carries the term value and log index of the preliminary candidate node; Each node that receives the qualification review request message determines whether the preliminary candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, and determines the qualification voting information based on the judgment result, and generates a response message based on the qualification voting information and returns it to the preliminary candidate node; wherein, if it is determined that the preliminary candidate node meets the conditions for updating to a candidate node, the qualification voting information is a positive vote, otherwise it is a negative vote; wherein, each node that receives the qualification review request message determines whether the preliminary candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, including: each node that receives the qualification review request message parses the qualification review request message to obtain the term of the preliminary candidate node value and log index; determine whether the term value of the preparatory candidate node is less than its own term value; if so, determine that the preparatory candidate node does not meet the conditions for updating to a candidate node; if not, determine whether the log index of the preparatory candidate node is less than its own log index; if the log index of the preparatory candidate node is less than its own log index, determine that the preparatory candidate node does not meet the conditions for updating to a candidate node; if the log index of the preparatory candidate node is greater than or equal to its own log index, determine that the preparatory candidate node meets the conditions for updating to a candidate node; the content of the response message includes: qualification voting information and response term value; wherein, if the qualification voting information is an affirmative vote, the response term value is the term value of the preparatory candidate node; otherwise, the response term value is the term value of the node that receives the qualification review request message; The candidate preparation node counts whether the number of affirmative votes in the received response message exceeds a preset number; If more than that, the candidate node changes its node role to a candidate node to initiate the leader election process; If not, it means that the reserve candidate node is an outdated node; the reserve candidate node changes its own node role to a follower node, so that the distributed system waits for a new follower node to meet the timeout status; wherein, the outdated node refers to a node in the cluster that has lost connection due to network isolation and partitioning caused by network problems.
2. The consensus method with high consensus efficiency according to claim 1, characterized in that: The provisional candidate node sends a qualification review request message to other nodes in the distributed system, including: The preliminary candidate node broadcasts a qualification review request message to other nodes in the distributed system.
3. The consensus method with high consensus efficiency according to claim 1 or 2, characterized in that: The candidate node counting whether the number of affirmative votes in the received response message exceeds a preset number includes: The preliminary candidate node counts whether the number of affirmative votes in the received response message exceeds half of the total number of nodes in the distributed system.
4. A distributed system with high consensus efficiency, characterized in that: There are four types of node roles, including leader node, follower node, candidate node and candidate node. The follower node is used to change its own node role to the only reserve candidate node in the distributed system while keeping the term unchanged when the timeout state is met first; The preliminary candidate node is used to send a qualification review request message to the remaining nodes in the distributed system; wherein the qualification review request message carries the term value and log index of the preliminary candidate node; Each node other than the preparatory candidate node that receives the qualification review request message is used to determine whether the preparatory candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, and determine the qualification voting information based on the judgment result, and generate a response message based on the qualification voting information and return it to the preparatory candidate node; wherein, if it is determined that the preparatory candidate node meets the conditions for updating to a candidate node, the qualification voting information is a positive vote, otherwise it is a negative vote; wherein, each node that receives the qualification review request message, when determining whether the preparatory candidate node meets the conditions for updating to a candidate node based on its own term value, log index and the qualification review request message, is specifically used to: parse the qualification review request message, obtain the qualification voting information of the preparatory candidate node term value and log index; determine whether the term value of the preparatory candidate node is less than its own term value; if so, determine that the preparatory candidate node does not meet the conditions for updating to a candidate node; if not, determine whether the log index of the preparatory candidate node is less than its own log index; if the log index of the preparatory candidate node is less than its own log index, determine that the preparatory candidate node does not meet the conditions for updating to a candidate node; if the log index of the preparatory candidate node is greater than or equal to its own log index, determine that the preparatory candidate node meets the conditions for updating to a candidate node; the content of the response message includes: qualification voting information and response term value; wherein, if the qualification voting information is an affirmative vote, the response term value is the term value of the preparatory candidate node; otherwise, the response term value is the term value of the node that receives the qualification review request message; The reserve candidate node is also used to count whether the number of affirmative votes in the received response message exceeds a preset number; if so, its own node role is changed to a candidate node to initiate a leader election process; if not, it means that the reserve candidate node is an outdated node; its own node role is changed to a follower node so that the distributed system waits for a new follower node to meet the timeout status; wherein the outdated node refers to a node in the cluster that has lost connection due to network isolation and partitioning caused by network problems.
5. The distributed system according to claim 4, characterized in that: When the candidate preparation node sends a qualification review request message to other nodes in the distributed system, it is specifically used to: Broadcasting a qualification review request message to other nodes in the distributed system.
6. The distributed system according to claim 4 or 5, characterized in that: When the preliminary candidate node counts whether the number of affirmative votes in the received response message exceeds a preset number, it is specifically used to: It is counted whether the number of affirmative votes in the received response messages exceeds half of the total number of nodes in the distributed system.
