An optimization method for the pre-election process in the Raft consensus algorithm
By using random values in the pre-election process of the Raft consensus algorithm to compare and select the appropriate Leader nodes, the problem of multiple alternative nodes being divided into ballots caused by election timeouts is solved, and the usability of the system is improved.
Patent Information
- Application Number
- CN202210058252.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-19
- Publication Date
- 2025-04-15
- Estimated Expiration
- 2042-01-19
AI Technical Summary
In the Raft consensus algorithm, multiple alternative nodes may appear in the pre-election process to divide the votes, resulting in the inability to select the Leader node, causing the election to time out and affect the availability of the distributed system.
During the pre-election process, the node that initiates the pre-election calculates a random value and places the random value in the pre-election message to broadcast. When receiving the pre-election message from other nodes, the logic of comparing the random value is added to prevent multiple pre-election nodes from causing the election to time out.
It effectively reduces the probability of election timeout caused by the partition of votes in the pre-election stage and improves the availability of the system.
Smart Images

Figure CN114490125B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Raft consensus algorithm, and in particular to an optimization method for a pre-election process in a Raft consensus algorithm. Background Art
[0002] In a distributed system, distributed consistency is a very important concept, which means that each server in the distributed system maintains a unified state (data). However, in a distributed system, some services are usually unavailable or unreliable due to network and system status reasons. This requires a consensus algorithm to ensure that the system is still available as a whole when some services fail.
[0003] The Raft consensus algorithm is a consensus algorithm that ensures data consistency among multiple server nodes. It was created under the influence of Paxos. Compared with Paxos, the Raft consensus algorithm is simpler and easier to understand. Follower and Leader are two common role states in the Raft consensus algorithm: the Follower state is a follower that passively receives data; the Leader state is a leader that handles all client interactions, log replication, etc.
[0004] At present, in the Raft consensus algorithm, there is a small probability that the leader election process will have multiple timeouts. Through the analysis of the election mechanism of the Raft consensus algorithm, it can be seen that a starting random time is set on each node in order to avoid the possibility of multiple nodes expiring at the same time and initiating elections. However, in a real production environment, due to the existence of network communication overhead, this situation of multiple nodes initiating elections at the same time cannot be completely avoided. At the same time, multiple candidate nodes divide the votes, resulting in the inability to select a leader node, causing the election process to time out multiple times, which seriously affects the availability of the distributed system. Summary of the invention
[0005] The technical task of the present invention is to address the above shortcomings and provide an optimization method for the pre-election process in the Raft consensus algorithm, which can effectively reduce the probability of election timeout caused by the division of votes in the pre-election stage and improve the availability of the system.
[0006] The technical solution adopted by the present invention to solve its technical problem is:
[0007] An optimization method for the pre-election process in the Raft consensus algorithm. During the pre-election process, the node initiating the pre-election calculates a random value and puts the random value in the pre-election message so that it is broadcast to other nodes along with the pre-election message.
[0008] During the pre-election process, if a pre-election message is received from another node initiating the pre-election, the logic of comparing node random values is added to prevent election timeouts caused by multiple pre-election nodes.
[0009] This method optimizes the pre-election process in the Raft consensus algorithm, reduces the probability of election timeout caused by vote division in the situation described in the above background technology, and improves the availability of the system.
[0010] Furthermore, the pre-election node receives a pre-election message from another pre-election node, and compares the index value of the last log record and the corresponding term value in its own record with the corresponding values in the pre-election message. If it is less than the corresponding term value in the pre-election message, or equal to the corresponding term value but less than or equal to the corresponding index value in the message, then it checks whether the index value of the last log record and the corresponding term value in its own record are equal to the corresponding values in the pre-election message.
[0011] If they are not equal, a response message of approval for voting is sent; if they are equal, the random value recorded by itself is further compared with the random value carried in the message to determine whether to send a response message of approval for voting.
[0012] Preferably, the random value recorded by itself is compared with the random value carried in the message. If the random value is smaller than the random value carried in the message, a response message of agreeing to vote is sent; otherwise, the voting message is continued to be waited.
[0013] Preferably, when sending a response message of approval for voting, the pre-election state node switches to a Follower node and sends a response message of approval for voting to another pre-election state node.
[0014] Preferably, the specific steps of implementing the method are as follows:
[0015] S1, Follower node checks whether its election timer has timed out;
[0016] S2. If the election timer has not timed out, go to step S1; otherwise, switch its own state to the pre-election state, that is, the PreCandidate state;
[0017] S3. Obtain and record a random value, add the random value to a pre-election message, and broadcast the pre-election message;
[0018] S4. If a pre-election response message, MsgPreVoteResp, is received from another Follower node, the term value recorded by the PreCandidate node is compared with the term value carried in MsgPreVoteResp:
[0019] If it is less than the term value carried in MsgPreVoteResp, it is a rejection vote, and the PreCandidate node switches to a Follower node;
[0020] If it is greater than the term value carried in MsgPreVoteResp, go to step S5;
[0021] S5. The number of votes recorded by the PreCandidate node itself is increased by 1;
[0022] Check whether the number of votes exceeds half of the number of nodes in the Raft group:
[0023] If it exceeds, it will switch to Candidate node and start the formal election process;
[0024] If not, wait for voting message;
[0025] S6. If a pre-election message is received from PreCandidate node X, the term value recorded in the pre-election message is compared with the term value recorded in the pre-election message:
[0026] If it is greater than the term value recorded in the pre-election message, a response message of rejecting the vote is sent to the PreCandidate node X; otherwise, go to step S7;
[0027] S7. Compare the last log index value and the corresponding term value recorded by itself with the corresponding values in the pre-election message:
[0028] If it is greater than the corresponding term value in the pre-election message, or equal to the corresponding term value but greater than the corresponding index value in the message, a response message of rejecting the vote is sent to the PreCandidate node X; otherwise, go to step S8;
[0029] S8. Check whether the last log index value and the corresponding term value recorded in the system are equal to the corresponding values in the pre-election message:
[0030] If they are not equal, the current PreCandidate node switches to a Follower node and sends a response message of approving the vote to the PreCandidate node X; otherwise, go to step S9;
[0031] S9. Compare the random value recorded by itself with the random value carried in the message. If it is smaller than the random value carried in the message, the current PreCandidate node switches to the Follower node and sends a response message of agreeing to vote to the PreCandidate node X; otherwise, continue to wait for the voting message.
[0032] Furthermore, in step S2, the term value recorded by itself does not make any change.
[0033] Furthermore, while waiting for the voting message, if the election timer has timed out, the pre-election is re-initiated.
[0034] The present invention also claims protection for a system for optimizing the pre-election process in the Raft consensus algorithm, which implements the above-mentioned method for optimizing the pre-election process in the Raft consensus algorithm.
[0035] The present invention also claims protection for an optimization device for a pre-election process in a Raft consensus algorithm, comprising: at least one memory and at least one processor;
[0036] The at least one memory is used to store a machine-readable program;
[0037] The at least one processor is used to call the machine-readable program to execute the above-mentioned optimization method for the pre-election process in the Raft consensus algorithm.
[0038] The present invention also requires protection of a computer-readable medium having computer instructions stored thereon, which, when executed by a processor, causes the processor to execute the above-mentioned optimization method for the pre-election process in the Raft consensus algorithm.
[0039] Compared with the prior art, the optimization method for the pre-election process in the Raft consensus algorithm of the present invention has the following beneficial effects:
[0040] This method can effectively reduce the probability of election timeouts caused by vote division in the pre-election stage and improve the availability of the system by optimizing the pre-election process in the Raft consensus algorithm. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Figure 1 It is a Raft consensus algorithm pre-election flow chart after optimization of the optimization method for the pre-election process in the Raft consensus algorithm provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0042] The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments.
[0043] In the Raft consensus algorithm, all data changes are recorded in the service node in the form of log records. The log includes information such as index value and term value. Among them, the index value represents the serial number of the log, and the term value represents the term value corresponding to the Raft Leader when the index value is generated. The service node will continuously read the log records and update the log records to the data of the service node. Among them, Follower and Leader are two common role states in the Raft consensus algorithm: Follower state is a follower, passively receiving data; Leader state is a leader, handling all client interactions, log replication, etc.
[0044] In a Raft group, there can generally be only one Leader node, which is elected by multiple Follower nodes through voting. When the election timer of a Follower node times out, it will switch to another state, the PreCandidate state. In the PreCandidate state, a pre-election vote will be sent to other member nodes in the Raft group. If it receives more than half of the approval votes from the Raft group members, it will switch to the Candidate state and enter the formal election voting. In the formal election voting process, if the node receives more than half of the approval votes from the Raft group members, it will become the Leader node.
[0045] In the Raft consensus algorithm, a pre-election phase is conducted before the formal election.
[0046] An embodiment of the present invention provides an optimization method for the pre-election process in the Raft consensus algorithm. During the pre-election process, the node initiating the pre-election calculates a random value and puts the random value in the pre-election message so that it is broadcast to other nodes along with the pre-election message.
[0047] During the pre-election process, if a pre-election message is received from another node initiating the pre-election, the logic of comparing node random values is added to prevent election timeouts caused by multiple pre-election nodes.
[0048] like Figure 1 As shown, the specific implementation steps of this method are as follows:
[0049] S1, Follower node checks whether its election timer has timed out;
[0050] S2. If the election timer has not timed out, go to step S1; otherwise, switch its own state to the pre-election state, i.e., the PreCandidate state; and do not make any changes to the term value recorded by itself;
[0051] S3. Obtain and record a random value, add the random value to a pre-election message, and broadcast the pre-election message;
[0052] S4. If a pre-election response message, MsgPreVoteResp, is received from another Follower node, the term value recorded by the PreCandidate node is compared with the term value carried in MsgPreVoteResp:
[0053] If it is less than the term value carried in MsgPreVoteResp, it is a rejection vote, and the PreCandidate node switches to a Follower node;
[0054] If it is greater than the term value carried in MsgPreVoteResp, go to step S5;
[0055] S5. The number of votes recorded by the PreCandidate node itself is increased by 1;
[0056] Check whether the number of votes exceeds half of the number of nodes in the Raft group:
[0057] If it exceeds, it will switch to Candidate node and start the formal election process;
[0058] If it has not exceeded, the system will wait for the voting message. If the election timer has timed out during the waiting process, the pre-election will be initiated again.
[0059] S6. If a pre-election message is received from PreCandidate node X, the term value recorded in the pre-election message is compared with the term value recorded in the pre-election message:
[0060] If it is greater than the term value recorded in the pre-election message, a response message of rejecting the vote is sent to the PreCandidate node X; otherwise, go to step S7;
[0061] S7. Compare the last log index value (and the corresponding term value) recorded by itself with the corresponding value in the pre-election message:
[0062] If it is greater than the corresponding term value in the pre-election message, or equal to the corresponding term value but greater than the corresponding index value in the message, a response message of rejecting the vote is sent to the PreCandidate node X; otherwise, go to step S8;
[0063] S8. Check whether the last log index value (and corresponding term value) recorded by itself is equal to the corresponding value in the pre-election message:
[0064] If they are not equal, the current PreCandidate node switches to a Follower node and sends a response message of approving the vote to the PreCandidate node X; otherwise, go to step S9;
[0065] S9. Compare the random value recorded by itself with the random value carried in the message. If it is smaller than the random value carried in the message, the current PreCandidate node switches to the Follower node and sends a response message of agreeing to vote to the PreCandidate node X; otherwise, continue to wait for the voting message.
[0066] This method optimizes the pre-election process in the Raft consensus algorithm, reduces the probability of election timeout due to vote division in the situation described in the background technology, and improves the availability of the system.
[0067] An embodiment of the present invention further provides an optimization system for the pre-election process in the Raft consensus algorithm, which implements the optimization method for the pre-election process in the Raft consensus algorithm described in the above embodiment of the present invention.
[0068] The embodiment of the present invention further provides an optimization device for the pre-election process in the Raft consensus algorithm, comprising: at least one memory and at least one processor;
[0069] The at least one memory is used to store a machine-readable program;
[0070] The at least one processor is used to call the machine-readable program to execute the optimization method for the pre-election process in the Raft consensus algorithm described in the above embodiment of the present invention.
[0071] The embodiment of the present invention further provides a computer-readable medium, on which computer instructions are stored, and when the computer instructions are executed by a processor, the processor executes the optimization method for the pre-election process in the Raft consensus algorithm described in the above embodiment of the present invention. Specifically, a system or device equipped with a storage medium can be provided, on which a software program code that implements the functions of any of the above embodiments is stored, and a computer (or CPU or MPU) of the system or device reads and executes the program code stored in the storage medium.
[0072] In this case, the program code itself read from the storage medium can realize the function of any one of the above-mentioned embodiments, and thus the program code and the storage medium storing the program code constitute a part of the present invention.
[0073] The storage medium embodiments for providing the program code include a floppy disk, a hard disk, a magneto-optical disk, an optical disk (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RAM, DVD-RW, DVD+RW), a magnetic tape, a non-volatile memory card, and a ROM. Alternatively, the program code can be downloaded from a server computer by a communication network.
[0074] In addition, it should be clear that the functions of any of the above embodiments can be implemented not only by executing the program code read by the computer, but also by enabling an operating system operating on the computer to complete part or all of the actual operations based on instructions from the program code.
[0075] In addition, it can be understood that the program code read from the storage medium is written to a memory provided in an expansion board inserted into the computer or written to a memory provided in an expansion unit connected to the computer, and then based on the instructions of the program code, a CPU installed on the expansion board or the expansion unit is enabled to perform part or all of the actual operations, thereby realizing the functions of any of the above-mentioned embodiments.
[0076] The present invention is shown and described in detail above through the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above multiple embodiments, those skilled in the art can know that the code review methods in the above different embodiments can be combined to obtain more embodiments of the present invention, and these embodiments are also within the protection scope of the present invention.
Claims
1. A method for optimizing the pre-election process in the Raft consensus algorithm, characterized in that: During the pre-election process, the node initiating the pre-election calculates a random value and puts the random value in the pre-election message so that it is broadcast to other nodes along with the pre-election message. During the pre-election process, if a pre-election message is received from another node that initiated the pre-election, the logic of comparing node random values is added to prevent the election timeout caused by multiple pre-election nodes. When the pre-election node receives a pre-election message from another pre-election node, it compares the index value of the last log record and the corresponding term value with the corresponding value in the pre-election message. If it is less than the corresponding term value in the pre-election message, or equal to the corresponding term value but less than or equal to the corresponding index value in the message, it checks whether the index value of the last log record and the corresponding term value are equal to the corresponding value in the pre-election message: If they are not equal, a response message of agreeing to vote is sent; if they are equal, the random value recorded by itself is further compared with the random value carried in the message to determine whether to send a response message of agreeing to vote; the random value recorded by itself is compared with the random value carried in the message. If it is less than the random value carried in the message, a response message of agreeing to vote is sent; otherwise, continue to wait for the voting message.
2. According to claim 1, a method for optimizing the pre-election process in the Raft consensus algorithm is characterized in that: The response message of voting approval is sent, the pre-election state node switches to a Follower node, and sends a response message of voting approval to another pre-election state node.
3. According to claim 1, a method for optimizing the pre-election process in the Raft consensus algorithm is characterized in that: The specific steps to implement this method are as follows: S1, Follower node checks whether its election timer has timed out; S2. If the election timer has not timed out, go to step S1; otherwise, switch its own state to the pre-election state, that is, the PreCandidate state; S3. Obtain and record a random value, add the random value to a pre-election message, and broadcast the pre-election message; S4. If a pre-election response message, MsgPreVoteResp, is received from another Follower node, the term value recorded by the PreCandidate node is compared with the term value carried in MsgPreVoteResp: If it is less than the term value carried in MsgPreVoteResp, it is a rejection vote, and the current PreCandidate node switches to a Follower node; If it is greater than the term value carried in MsgPreVoteResp, go to step S5; S5. The number of votes recorded by the PreCandidate node itself is increased by 1; Check whether the number of votes exceeds half of the number of nodes in the Raft group: If it exceeds, it will switch to Candidate node and start the formal election process; If not, wait for voting message; S6. If a pre-election message is received from PreCandidate node X, the term value recorded in the pre-election message is compared with the term value recorded in the pre-election message: If it is greater than the term value recorded in the pre-election message, a response message of rejecting the vote is sent to the PreCandidate node X; otherwise, go to step S7; S7. Compare the last log index value and the corresponding term value recorded by itself with the corresponding values in the pre-election message: If it is greater than the corresponding term value in the pre-election message, or equal to the corresponding term value but greater than the corresponding index value in the message, a response message of rejecting the vote is sent to PreCandidate node X; Otherwise go to step S8; S8. Check whether the last log index value and the corresponding term value recorded by itself are equal to the corresponding values in the pre-election message: If they are not equal, the current PreCandidate node switches to the Follower node and sends a response message of agreeing to vote to the PreCandidate node X; Otherwise go to step S9; S9. Compare the random value recorded by itself with the random value carried in the message. If it is smaller than the random value carried in the message, the current PreCandidate node switches to the Follower node and sends a response message of agreeing to vote to the PreCandidate node X; otherwise, continue to wait for the voting message.
4. According to claim 3, a method for optimizing the pre-election process in the Raft consensus algorithm is characterized in that: In step S2, the term value recorded by itself does not make any changes.
5. According to claim 3, a method for optimizing the pre-election process in the Raft consensus algorithm is characterized in that: While waiting for the voting message, if the election timer has timed out, the pre-election will be reinitiated.
6. An optimization system for the pre-election process in the Raft consensus algorithm, characterized in that: The system implements the optimization method for the pre-election process in the Raft consensus algorithm as described in any one of claims 1 to 5.
7. An optimization device for the pre-election process in the Raft consensus algorithm, characterized in that: include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is used to call the machine-readable program to execute the optimization method for the pre-election process in the Raft consensus algorithm described in any one of claims 1 to 5.
8. A computer readable medium, characterized in that The computer-readable medium stores computer instructions, and when the computer instructions are executed by the processor, the processor executes the optimization method for the pre-election process in the Raft consensus algorithm as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Leader electing method, device and equipment
CN108810046A
A block chain consensus method and device based on a Raft algorithm
CN109714404A