A campaign method, distributed system and storage medium based on Raft protocol
By comparing the latest log submission time of candidate nodes and voting nodes in the Raft protocol, the meaningless election process is terminated, the election extension problem caused by 'defective nodes' is solved, and the availability of the Redis cluster and the efficiency of the master node election are improved.
Patent Information
- Application Number
- CN202111409153.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-24
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2041-11-24
AI Technical Summary
In a distributed fault-tolerant system, the existence of 'defective nodes' causes the Raft protocol election process to be prolonged, algorithm calculation time to increase, and cluster availability to decrease.
By comparing the latest log submission time of the candidate node and the voting node, the meaningless election process is terminated, the 'defective node' is deprived of its candidacy, and the voting node is converted into a new candidate node until the master node is elected.
It reduces the amount of campaign data calculation and time, and improves the availability of the cluster and the probability of master node election.
Smart Images

Figure CN114268532B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of distributed cluster management, and in particular to a campaign method, a distributed system and a storage medium based on the Raft protocol. Background Art
[0002] Raft is an algorithm for managing replicated logs and has important applications in distributed fault-tolerant systems. In a Redis cluster, each server will only be in one of the three states of a master node, a candidate node, or a slave node at any time. The master node receives log entries from the client, replicates them to other servers, and controls other servers to apply log entries to their own state machines; slave nodes are passive, they will not issue requests, and will only respond to requests from the master node and candidates; candidates are randomly selected from the servers by the cluster to elect a master node.
[0003] Nowadays, when a candidate asks other slave nodes to vote for itself, the term and log length of the candidate and the voting node are compared. The condition for obtaining votes is that the term and log length of the candidate are greater than the term and log length of the voting node. In this way, a new master node is elected. In some cases, the votes may be divided, resulting in no master node being generated. This term will end with no master node, and a new term will be generated soon. The Raft protocol ensures that each term has at most one master node. In extreme cases, the cluster will go through multiple rounds of voting to elect a master node.
[0004] Since distributed fault-tolerant systems may experience a variety of complex abnormal conditions such as network delays, partitions, packet loss, duplication, and disorder, "defective nodes" may appear with log lengths shorter than other nodes but longer terms than other nodes. If such "defective nodes" are randomly identified as candidates by the cluster at the beginning, a round of elections will be initiated that will inevitably fail to produce a master node, resulting in meaningless time consumption, increased data calculations, prolonged algorithm operation time, and greatly reduced cluster availability. Summary of the invention
[0005] The present invention provides an election method, a distributed system and a storage medium based on the Raft protocol to solve the technical problem that the existing Redis cluster will cause the algorithm operation time to be prolonged and the cluster availability to be reduced due to "defective nodes". By comparing the important parameter of the latest log submission time, the meaningless election initiated by the "defective node" is ended in advance, thereby speeding up the process of electing the master node and improving the availability of the cluster.
[0006] In order to solve the above technical problems, an embodiment of the present invention provides a campaign method based on the Raft protocol, including:
[0007] When the Redis cluster is in election mode, multiple slave nodes are converted into candidate nodes;
[0008] Each candidate node initiates election requests in turn, and the current candidate node in each round of election will request other nodes except itself to vote for itself one by one;
[0009] When the current voting node votes for the current candidate node, the latest log submission time of the current candidate node is compared with the latest log submission time of the current voting node;
[0010] If the latest log submission time of the current candidate node is later than the latest log submission time of the current voting node, terminate this round of election and convert the current voting node into an update candidate node;
[0011] Control the update candidate node to continue to initiate election requests until the master node is elected.
[0012] As one of the preferred solutions, the election method based on the Raft protocol also includes:
[0013] If it is detected that there is no master node in the Redis cluster, control the Redis cluster to enter the election mode; or,
[0014] If the term of the current master node is lower than the term of any slave node, the current master node is controlled to be converted into a slave node, and the Redis cluster is controlled to enter the election mode.
[0015] As one of the preferred solutions, before converting the multiple slave nodes into candidate nodes, the election method based on the Raft protocol further includes:
[0016] Each slave node is controlled to enter a standby state, wherein the standby state lasts for 150ms to 300ms.
[0017] As one of the preferred schemes, if the latest log submission time of the current candidate node is earlier than the latest log submission time of the current voting node, the current voting node whose term and log length are smaller than the current candidate node is controlled to vote for the current candidate node.
[0018] As one of the preferred solutions, if the number of votes of the current candidate node satisfies the following relationship, it will be elected as the master node:
[0019]
[0020] Among them, a is the number of votes of the current candidate node, and b is the number of nodes in the Redis cluster.
[0021] As one of the preferred solutions, the election method based on the Raft protocol also includes:
[0022] If there are at least two update candidate nodes in the Redis cluster, each of the update candidate nodes generates a special election request with a time flag in turn, wherein the time flag reflects the conversion time of the update candidate node;
[0023] Compare the time flags of the two update candidate nodes, control the update candidate node with a later time flag to vote for the update candidate node with an earlier time flag, and,
[0024] All votes of the update candidate node with a later time marker are forwarded to the update candidate node with an earlier time marker.
[0025] As one of the preferred solutions, after the master node is elected, the election mode is ended and the Redis cluster is controlled to enter the normal mode.
[0026] Another embodiment of the present invention provides a distributed system, comprising a plurality of nodes, wherein the nodes are Redis servers running in a cluster mode, and the nodes are divided into a master node and a slave node;
[0027] The distributed system is used to execute the above-mentioned election method based on the Raft protocol when the master node is abnormal.
[0028] As one of the preferred solutions, the nodes include N master nodes and N×n slave nodes, and any master node corresponds to n slave nodes.
[0029] Yet another embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein when the computer program is executed, the device where the computer-readable storage medium is located is controlled to execute the above-mentioned Raft protocol-based election method.
[0030] Compared with the prior art, the beneficial effect of the embodiments of the present invention lies in at least one of the following: when the Redis cluster is in election mode, in addition to requiring the provision of the two parameters of term and log length, each round of elections initiated by the candidate nodes will require the candidate nodes and voting nodes to provide the latest log submission time owned by their own nodes. After the voting node finds that it has the latest updated log submission time, the cluster will immediately end this round of elections and convert the voting node to the updated candidate node to continue to initiate election requests. In this way, meaningless elections initiated by "defective nodes" whose log lengths are shorter than other nodes but whose terms are longer than other nodes are eliminated, greatly reducing the amount of data calculations and computing time of the elections, increasing the probability of electing a master node in each round, and ensuring the availability of the cluster. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 It is a flowchart of a campaign method based on the Raft protocol in one embodiment of the present invention;
[0032] Figure 2 It is a flowchart of a special campaign phase in one embodiment of the present invention. DETAILED DESCRIPTION
[0033] 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.
[0034] In the description of this application, the terms "first", "second", "third", etc. are used for descriptive purposes only and are not to be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first", "second", "third", etc. may explicitly or implicitly include one or more of the feature. In the description of this application, unless otherwise specified, "plurality" means two or more.
[0035] In the description of this application, it should be noted that, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in this application can be understood according to specific circumstances.
[0036] In the description of this application, it should be noted that, unless otherwise defined, all technical and scientific terms used in the present invention have the same meaning as those commonly understood by those skilled in the art. The terms used in the specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. For those of ordinary skill in the art, the specific meanings of the above terms in this application can be understood by specific circumstances.
[0037] An embodiment of the present invention provides a campaign method based on the Raft protocol. For details, see Figure 1 , Figure 1 The figure shows a flow chart of a campaign method based on the Raft protocol in one embodiment of the present invention, which includes steps S1 to S5:
[0038] S1. When the Redis cluster is in election mode, multiple slave nodes are converted into candidate nodes;
[0039] S2. Each candidate node initiates election requests in turn. In each round of election, the current candidate node will request other nodes except itself to vote for itself one by one;
[0040] S3. When the current voting node votes for the current candidate node, compare the latest log submission time of the current candidate node with the latest log submission time of the current voting node;
[0041] S4. If the latest log submission time of the current candidate node is later than the latest log submission time of the current voting node, terminate this round of election and convert the current voting node into an update candidate node;
[0042] S5. Control the update candidate node to continue to initiate election requests until a master node is elected.
[0043] The Raft protocol uses a heartbeat mechanism to trigger leader (master node) elections. For example, a server is in follower (slave node) state when it starts, and it will maintain follower status when it can receive valid RPC requests from the leader or candidate. The leader sends periodic heartbeats (AppendEntries RPC without logs) to all followers to ensure its authority. When an exception occurs, it will assume that the leader has failed and start a new election.
[0044] To start a new round of elections, the cluster will randomly select multiple slave nodes from many servers and convert their states. The selected follower will increase its current term and turn to the candidate state. It will first vote for itself and then send election requests to other servers in the cluster in parallel. The candidate will maintain this state until it wins the election, another server is established as the leader, or no winner is generated.
[0045] It should be noted that each server can only vote for one candidate at most in a given term, on a first-come, first-served basis. Receiving votes from the majority of nodes ensures that at most one leader can be elected in a term. Once a candidate wins the election, it becomes the leader. It will then send heartbeat messages to establish its authority and prevent new elections. If the candidate does not succeed or fails in the election, the candidate will time out and trigger a new round of elections, raising the term and sending new election requests. If the votes are split again, it will result in no server being established as the leader.
[0046] It can be seen that in extreme cases, the cluster will go through multiple rounds of voting to select the master node, and the probability of failing to select the master node in each round is close to 50%. In order to shorten the entire election time, the inventor has found through research that due to abnormal server status or unstable network, there are "defective nodes" in the cluster whose log length is less than other nodes but whose term of office is higher than other nodes. The election process of these defective nodes is undoubtedly meaningless. Therefore, in order to optimize the cluster election process for the master node, the embodiment of the present invention compares the important parameter of the latest log submission time. When it is found that the latest log submission time of the candidate node that initiates the voting request lags behind the latest log submission time of the voting node, the candidate node that initiates the voting request is immediately established as a "defective node". At this time, the election process of the "defective node" requiring other nodes to vote for it is no longer continued one by one, but this round of election process is directly ended, and the candidate node is deprived of its candidacy, and the voting node is upgraded to a candidate node, and the subsequent election process is continued. In this way, the amount of calculation of election data is reduced and the availability of the cluster is improved.
[0047] It should be noted that the leader can decide where to put the new log entry without asking other nodes, and data always flows simply from the leader to other nodes. Therefore, in the case of leader abnormality or disconnection, the cluster needs to elect a new leader. In this embodiment, leader abnormality includes the following two situations:
[0048] It is detected that there is no master node in the Redis cluster, or the term of the current master node is lower than the term of any slave node. In these two cases, the cluster will stop normal operations such as log sending, receiving, and replication, enter the election mode, and elect a new master node.
[0049] Furthermore, in the above embodiment, the election method based on the Raft protocol also includes:
[0050] Control each slave node to enter the standby state, wherein the standby state lasts for any value between 150ms and 300ms. Of course, in addition to the above range, the duration of the standby state can be set according to the actual server cluster performance and is not limited here. After the standby state, it is necessary to increase the free term of each node to ensure the timing of the cluster, which will not be repeated here.
[0051] Furthermore, in this embodiment, after the candidate node asks the voting node to vote for itself, it will determine whether the candidate node is eligible to vote. If the latest log submission time of the current candidate node is earlier than the latest log submission time of the current voting node, the current voting node whose term and log length are smaller than the current candidate node will be controlled to vote for the current candidate node.
[0052] In this embodiment, when the number of votes reaches a certain value, it is immediately converted to a master node and the election process ends. Specifically, if the number of votes of the current candidate node satisfies the following relationship, it will be elected as the master node:
[0053]
[0054] Among them, a is the number of votes of the current candidate node, and b is the number of nodes in the Redis cluster.
[0055] For details, see Figure 2 , Figure 2 The flowchart of the special election link in one embodiment of the present invention is shown. In this embodiment, after the voting node finds that it has the most detailed log submission time for the update, it immediately converts to updating the candidate node and initiates a campaign voting request. At this time, this request is called a "special election request" in this embodiment. The request is accompanied by a special flag (IrregularVote) to indicate that this is a special election request, as well as the sending time (RequestTime) information, that is, the "special election request" is a request with a time flag, wherein the time flag reflects the conversion time of the update candidate node.
[0056] The object nodes that receive the "special election request" include two major categories. One category is ordinary slave nodes. After receiving the above-mentioned "special election request", the ordinary slave nodes trigger the voting mechanism, vote for the candidate, and then wait for subsequent instructions; the other category is other candidate nodes. After receiving the "special election request", other candidate nodes will determine whether they have also issued the above-mentioned "special election request". If they have not issued the above-mentioned "special election request", the voting mechanism will be triggered and they will vote for the candidate. If other candidate nodes have also issued the above-mentioned "special election request", they can also be called update candidate nodes. The cluster will compare the time flags of the two update candidate nodes, control the update candidate node with a later time flag to vote for the update candidate node with an earlier time flag, and forward all votes of the update candidate node with a later time flag to the update candidate node with an earlier time flag. Of course, for the entire cluster, the node that sends the "special election request" earlier tends to be elected as the master node. Therefore, if the updated candidate node that receives the "special election request" finds that the time mark of the "special election request" it sent before is later, it will ignore the currently received "special election request" and send / wait for the signal from the master node. I will not go into details here.
[0057] Since the normal workflow will be interrupted when the cluster is in election mode, the cluster in election mode is unavailable. In this embodiment, after the master node is elected, the election mode is ended and the Redis cluster is controlled to enter normal mode, thereby optimizing the availability of the cluster.
[0058] Another embodiment of the present invention provides a distributed system, comprising a plurality of nodes, wherein the nodes are Redis servers running in a cluster mode, and the nodes are divided into a master node and a slave node;
[0059] The distributed system is used to execute the above-mentioned election method based on the Raft protocol when the master node is abnormal.
[0060] In the above embodiment, the nodes include N master nodes and N×n slave nodes, and any master node corresponds to n slave nodes. Of course, the number and correspondence of nodes need to be combined with the actual distributed system, considering factors such as disaster recovery, fault tolerance, remote / local computer room and other network security, which will not be repeated here.
[0061] Accordingly, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the steps in the election method based on the Raft protocol in the above embodiment, for example Figure 1 Steps S1 to S5 described in .
[0062] The Raft-based election method, distributed system, and storage medium provided in the embodiments of the present invention have the following beneficial effects:
[0063] When the Redis cluster is in election mode, in addition to requiring the term and log length, each round of elections initiated by candidate nodes will require candidate nodes and voting nodes to provide the latest log submission time of their own nodes. After the voting node finds that it has the latest updated log submission time, the cluster will immediately end this round of elections and convert the voting node to the updated candidate node to continue to initiate election requests. In this way, meaningless elections initiated by "defective nodes" with shorter log lengths than other nodes but longer terms than other nodes are eliminated, greatly reducing the amount of data calculation and computing time of the election, increasing the probability of electing a master node in each round, and ensuring the availability of the cluster.
[0064] The above is a preferred embodiment of the present invention. It should be pointed out that a person skilled in the art can make several improvements and modifications without departing from the principle of the present invention. These improvements and modifications are also considered to be within the scope of protection of the present invention.
Claims
1. A campaign method based on the Raft protocol, characterized in that: include: When the Redis cluster is in election mode, multiple slave nodes are converted into candidate nodes; Each candidate node initiates election requests in turn, and the current candidate node in each round of election will request other nodes except itself to vote for itself one by one; When the current voting node votes for the current candidate node, the latest log submission time of the current candidate node is compared with the latest log submission time of the current voting node; If the latest log submission time of the current candidate node is later than the latest log submission time of the current voting node, terminate this round of election and convert the current voting node into an update candidate node; Control the update candidate node to continue to initiate election requests until a master node is elected; The election method based on the Raft protocol also includes: If it is detected that there is no master node in the Redis cluster, control the Redis cluster to enter the election mode; or, If the term of the current master node is lower than the term of any slave node, the current master node is controlled to be converted into a slave node, and the Redis cluster is controlled to enter the election mode; The election method based on the Raft protocol also includes: If there are at least two update candidate nodes in the Redis cluster, each of the update candidate nodes generates a special election request with a time flag in turn, wherein the time flag reflects the conversion time of the update candidate node; Compare the time flags of the two update candidate nodes, control the update candidate node with a later time flag to vote for the update candidate node with an earlier time flag, and, All votes of the update candidate node with a later time marker are forwarded to the update candidate node with an earlier time marker.
2. The election method based on the Raft protocol as claimed in claim 1, characterized in that: Before converting the multiple slave nodes into candidate nodes, the election method based on the Raft protocol further includes: Each slave node is controlled to enter a standby state, wherein the standby state lasts for 150ms to 300ms.
3. The election method based on the Raft protocol as claimed in claim 1, characterized in that: If the latest log submission time of the current candidate node is earlier than the latest log submission time of the current voting node, the current voting node whose term and log length are smaller than that of the current candidate node is controlled to vote for the current candidate node.
4. The election method based on the Raft protocol as claimed in claim 1, characterized in that: If the number of votes of the current candidate node satisfies the following relationship, it will be elected as the primary node: Among them, a is the number of votes of the current candidate node, and b is the number of nodes in the Redis cluster.
5. The election method based on the Raft protocol as claimed in claim 1, characterized in that: After the master node is elected, the election mode ends and the Redis cluster is controlled to enter the normal mode.
6. A distributed system, characterized in that: It includes multiple nodes, each of which is a Redis server running in cluster mode, and each of which is divided into a master node and a slave node; The distributed system is used to execute the election method based on the Raft protocol according to any one of claims 1 to 5 when the master node is abnormal.
7. The distributed system according to claim 6, characterized in that: The nodes include N master nodes and N×n slave nodes, and any one master node corresponds to n slave nodes.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the election method based on the Raft protocol according to any one of claims 1 to 5.
Citation Information
Patent Citations
Node equipment operation method and node equipment
CN107105032A
