A cluster leader election method with version comparison

By defining node election and connectivity states in a distributed system, and utilizing election round checks and timer management, the problems of data version conflicts and inaccurate elections in leader elections in distributed systems are solved, thereby improving the reliability and efficiency of leader elections.

CN117714337BActive Publication Date: 2026-11-17SICHUAN UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311714312.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-12-13
Publication Date
2026-11-17
Estimated Expiration
2043-12-13

AI Technical Summary

Technical Problem

In existing technologies, leader election methods in distributed systems cannot effectively avoid data version conflicts and data loss, and the election results are not accurate enough. In particular, in Paxos-like consensus methods, a leader with an outdated data version may be elected.

Method used

By defining the election and connectivity states of nodes, utilizing election state transition relationships and election round checks, the system ensures that the node with the latest data version is elected as the leader. The election process is synchronized using TCP connections and broadcast messages, and the leader state is managed through heartbeats and timers.

Benefits of technology

This effectively avoids data version conflicts, improves the reliability and accuracy of leader election, ensures data version consistency, adapts to the real operating environment of the cluster, and improves election efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117714337B_ABST
    Figure CN117714337B_ABST
Patent Text Reader

Abstract

This invention belongs to the field of distributed systems technology and relates to a cluster leader election method with version comparison. The method includes: defining the election states of nodes; defining the transition relationships between various election states; defining the connectivity states of nodes; starting each node in the cluster and connecting them; initializing the election states; nodes initiating election state notifications; detecting node election states; each node synchronously initiating and synchronizing the election process; checking the election round; selecting the node with the largest instance as the leader, and the remaining nodes as followers; the newly elected leader sending a heartbeat and broadcasting the heartbeat message, maintaining the leader's connectivity state. This invention effectively avoids the risk of data loss by checking the election state and election round, preventing data version conflicts and ensuring data version consistency; it solves the problem of different version information in leader election in Paxos-like clusters and is better adapted to real-world cluster operating environments.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of distributed systems technology, and specifically relates to a cluster leader election method with version comparison. Background Technology

[0002] Election methods have wide applications in the field of distributed systems, especially in Paxos-like consensus methods with a strong leader model. The elected leader will dominate all subsequent data reads and writes. However, if the leader's own data version is outdated, data version conflicts may occur, leading to data loss. This invention addresses this problem by collecting global data version information in the distributed cluster, rather than the more common majority version information, while also limiting potential accidents such as mis-elections.

[0003] Paxos-like methods include Multi-Paxos, Raft, and Zab methods. These methods either lack an election mechanism or rely on a majority of nodes for election, which may result in the selected node not being the most recently updated.

[0004] In the research and design of distributed systems, consensus methods are a crucial element in maintaining data consistency among nodes and ensuring the accuracy of data received by system callers. Common distributed consensus methods in the industry include Paxos, Raft, and Zab. In practical engineering, consensus methods are increasingly being implemented as independent reliability services, enabling more efficient distributed deployment. All of the aforementioned consensus methods are leader-based, introducing a stable leader whose data is determined over a period of time. Paxos avoids livelock risks through its leader, while Raft explicitly states that all node data is determined or aligned by its leader.

[0005] Therefore, a reliable leader election method is essential for implementing consensus mechanisms. Since the data of other nodes is determined by the leader's data version, if the leader's chosen data version lags behind that of other non-leader nodes, these nodes will be forced to abandon their decided data, leading to severe data loss. Current mainstream election methods, such as the Paxos lease method and the Fast Election method used in ZooKeeper, all suffer from the possibility of a suboptimal node being elected as the leader, failing to yield accurate and effective election results. Summary of the Invention

[0006] To address the aforementioned technical problems, this invention provides a cluster leader election method with version comparison, comprising:

[0007] Define the election states of a node, including: the first election state, which indicates that the node is allowed to receive votes; when the election of a new leader begins, the node's election state is initialized to the first election state; the second election state, which indicates that the node's followers are currently prohibited from receiving votes; and the third election state, which indicates that the node's leader is currently prohibited from receiving votes.

[0008] Define the transition relationships between various election states, including: when the data version of a node in the first election state is lower than a first set threshold, the node transitions to the second election state; when the data version of a node in the first election state is greater than a second set threshold, the node transitions to the third election state.

[0009] Define the connectivity state of a node, including: a first connectivity state, which indicates that the current leader is online and the system is running; and a second connectivity state, which indicates that the local node has detected a loss of connection with the current leader.

[0010] Each node in the cluster starts up, and the nodes are connected via TCP.

[0011] Load the currently decided instance and initialize the election state to the first election state, while also initializing the current leader state to the second connectivity state;

[0012] The node initiates an election status report;

[0013] Set the conditions for a node to pass the election state, and detect the node's election state;

[0014] Once the election status check is passed, each node is notified to initiate an election, and each node initiates and processes the election synchronously.

[0015] After receiving broadcast votes from nodes other than itself, each node first determines whether it is in the first election state, and then checks whether the election state of the nodes in the received votes is also in the first election state. If the election state of each node is in the first election state, the election round is checked; otherwise, the election state of the node is initialized.

[0016] Each node sets an election logic clock to mark the current election round. During the election, the election round is checked, and only votes from the same election round are considered valid votes.

[0017] The global state table records all valid votes received during the current election round. When the number of valid votes collected meets the number of valid nodes in the current cluster, the global state table is traversed, and the node with the largest instance is selected as the leader, with the remaining nodes as followers. The election state of the local machine is modified synchronously. When the election ends, the node resets the election timeout timer, and the newly elected leader node starts the heartbeat sending timer and broadcasts heartbeat messages to continue the leader's first connectivity state.

[0018] Based on the above technical solution, the present invention can be further improved as follows.

[0019] Furthermore, the conditions for a node to pass the election state include all nodes in the cluster being in the first election state, or more than half of the nodes in the cluster being in the first election state and the time after the majority has exceeded the threshold.

[0020] Furthermore, each node synchronizes the election process by sending and receiving broadcast messages.

[0021] Furthermore, each node sets an election logic clock to mark the current election round. During the election, only votes from the same election round are considered valid votes.

[0022] Furthermore, each node sets an election logic clock to mark the current election round. The election round increments each time an election is initiated, and then each node broadcasts its votes.

[0023] Furthermore, each node sets an election logic clock to mark the current election round. The election round increments each time an election is initiated, and then each node broadcasts its votes.

[0024] Furthermore, examine the election rounds, including:

[0025] If the election round in the ballot is greater than the local election round, then the local machine failed to participate in the previous election and is lagging behind in the election round. The local machine's election round is updated to the election round in the ballot, and the ballot is rebroadcast. If the election round in the ballot is less than the local election round, then the local machine's current ballot is ignored. If the election round in the ballot is equal to the local election round, then the global state table is updated.

[0026] Furthermore, a third connectivity state is defined to characterize the state in which more than half of the nodes in the cluster detect a loss of connection with the current leader. When the leader fails or there is network latency, the node is in the second connectivity state, causing followers to fail to receive heartbeat packets and triggering an election timeout timer. The timed-out followers first mark the current leader as being in the first offline state, which characterizes the follower's subjective offline status. Then, they broadcast the leader status information they perceive. After receiving this leader status information, other nodes update their own connectivity tables between each node and the current leader, and check their own connectivity with the leader. If more than half of the valid nodes in the cluster have marked the leader offline, the node is in the third connectivity state, so it changes its own connectivity with the leader to offline and initiates an election request to enter a new round of elections. If less than half of the nodes mark the leader offline, the node continues to broadcast the leader status information it perceives and continues to collect global connectivity information.

[0027] Furthermore, a maximum number of times the leader's status information can be broadcast is set; if the maximum number of broadcasts is exceeded, the broadcasting stops.

[0028] Furthermore, a heartbeat sending timer is used to extend the first connectivity state of the leader, which is triggered by the leader of the current cluster node; an election timeout timer is used to detect leader failure and re-election when a single election fails; and an election status check timer is used to check the election status of each node when the cluster node starts up, and the election status check timer is turned off after the election begins.

[0029] The beneficial effects of this invention are: it can effectively avoid the risk of data loss by checking the election status and election rounds, thus preventing data version conflicts caused by outdated leader data versions and ensuring data version consistency; it can improve the reliability of leader election, ensure the accuracy of election results, solve the problem of different version information in leader election in Paxos-like clusters, and solve the problem of low efficiency when electing the node with the latest data version as the leader, and can better adapt to the real operating environment of the cluster. Attached Figure Description

[0030] Figure 1 This is a schematic diagram of a cluster leader election method with version comparison provided in Embodiment 1 of the present invention;

[0031] Figure 2 A schematic diagram illustrating the transition relationships between different election states;

[0032] Figure 3 A flowchart of the election process;

[0033] Figure 4 A flowchart for detecting the node election status;

[0034] Figure 5 A flowchart of the election process when a leader fails. Detailed Implementation

[0035] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.

[0036] As an example, see the attached document. Figure 1 As shown, to solve the above-mentioned technical problems, this embodiment provides a cluster leader election method with version comparison, including:

[0037] Define the election states of a node, including: the first election state, which indicates that the node is allowed to receive votes; when the election of a new leader begins, the node's election state is initialized to the first election state; the second election state, which indicates that the node's followers are currently prohibited from receiving votes; and the third election state, which indicates that the node's leader is currently prohibited from receiving votes.

[0038] As attached Figure 2 As shown, the transition relationships between various election states are defined, including: when the data version of a node in the first election state is lower than a first set threshold, the node transitions to the second election state; when the data version of a node in the first election state is greater than a second set threshold, the node transitions to the third election state; the second set threshold is greater than the first set threshold.

[0039] Define the connectivity state of a node, including: a first connectivity state, which indicates that the current leader is online and the system is running; and a second connectivity state, which indicates that the local node has detected a loss of connection with the current leader.

[0040] In practical applications, the first election state is the initial state after startup. It automatically enters the first election state when a new leader election begins, and nodes can only receive votes in this state. After the election, the node corresponding to the second election state is called a follower, and in this state, the node is prohibited from receiving votes. After the election, the first election state indicates that this node is the leader, and in this state, the node is prohibited from receiving votes.

[0041] The first connectivity state is defined as representing that the current leader is online and the system is running; the second connectivity state is defined as representing that the local node detects that it has lost contact with the current leader. When the leader's corresponding node crashes or the network is interrupted, the local node loses contact with the current leader.

[0042] As attached Figure 3 As shown, the election process is as follows:

[0043] When each node in the cluster starts up, the TCP connection between the nodes ensures the reliability of data transmission.

[0044] After each node in the cluster is powered on and started, it waits for the nodes to connect and then loads the currently decided instance number.

[0045] The initial election state is set to the first election state, and the current leader state is set to the second election state.

[0046] Nodes initiate election status notifications to monitor node election status. (See attached image) Figure 4 As shown, when detecting the election status of a node, the election round is checked. If the election round in the ballot is greater than the local election round, the local node failed to participate in the previous election and is lagging behind in the election round. The local election round is updated to the election round in the ballot, and the ballot is rebroadcast. If the election round in the ballot is less than the local election round, the local current ballot is ignored. If the election round in the ballot is equal to the local election round, the global state table is updated.

[0047] Initiating an election after checking the election status helps synchronize the election process across nodes and prevents the omission of votes from better nodes due to inconsistent election initiation times among nodes.

[0048] Each node sets an auto-incrementing logical clock to mark the current election round. During the election, the election round is checked, and only votes from the same election round are considered valid. Each time an election is initiated, the election round is incremented, and then each node broadcasts its votes.

[0049] After receiving broadcast votes from nodes other than itself, each node first determines whether it is in the first election state, and then checks whether the election state of the nodes in the received votes is also in the first election state. If the election state of each node is in the first election state, the election round is checked; otherwise, the election state of the node is initialized.

[0050] The global state table records all valid votes received during the current election round. When the number of valid votes collected meets the number of valid nodes in the current cluster, the global state table is traversed, and the node with the largest instance is selected as the leader and its election state is set to the third election state. The remaining nodes are set as followers and their election state is set to the second election state. When the election ends, the nodes reset the election timeout timer, and the newly elected leader node starts a heartbeat sending timer and broadcasts a heartbeat message to continue the leader's first connectivity state.

[0051] Optionally, when detecting the node election status, the conditions for a node to pass the election status include all nodes in the cluster being in the first election state, or more than half of the nodes in the cluster being in the first election state and the time after the majority has exceeded the threshold.

[0052] Under two different conditions, the number of valid nodes in the cluster will differ during subsequent elections. Condition one requires that the number of valid nodes in the cluster equals the total number of nodes in the cluster, which is the number of nodes set in the configuration file. Condition two only requires that the number of valid nodes in the cluster equals more than half of the nodes that currently meet the first election state. Valid nodes are those that are currently running normally and maintaining connections with other nodes. Condition two can prevent some nodes from failing to start due to hardware or software problems after the cluster starts, which would cause partial blocking of the consensus method and prevent external services. It ensures that the number of nodes that start normally at this time meets the requirements for external services.

[0053] Optionally, each node can synchronize the election process by sending and receiving broadcast messages.

[0054] Optionally, each node can set an election logic clock to mark the current election round. Only votes from the same election round are considered valid during the election.

[0055] Optionally, each node sets an election logic clock to mark the current election round. The election round increments each time an election is initiated, and then each node broadcasts its votes.

[0056] Optionally, a third connectivity state can be defined to characterize a situation where more than half of the nodes in the cluster have detected a loss of connection with the current leader. (See attached...) Figure 5 As shown, when the leader fails or there is network latency, the node is in the second connectivity state. At this time, the leader is inactive, causing followers to fail to receive heartbeat packets and triggering the election timeout timer. The timed-out followers first mark the current leader as the first offline state, which represents the followers' subjective offline state of the leader. Then, each node broadcasts the leader's perceived state information. After receiving the leader's state information, each node updates its own connectivity table between each node and the current leader, and checks its own connectivity with the leader. If more than half of the cluster's valid nodes have marked the leader offline, the node is in the third connectivity state, so it modifies its own connectivity with the leader to the offline state and initiates an election request to enter a new round of election. If less than half of the nodes have marked the leader offline, the node continues to broadcast the leader's perceived state information and continues to collect global connectivity information.

[0057] The solution described above for leader failure, rather than immediately triggering a new election when the election timeout timer is triggered, is adopted because there may be network partitions or other issues. For example, if the network between the cluster and node A is unexpectedly interrupted, node A will trigger the election timeout timer and initiate a single-node election, causing its election rounds to increase indefinitely. If node A rejoins the cluster as the network recovers, this election round will disrupt the original cluster's election rounds, leading to reduced election efficiency or even destroying the election results.

[0058] Optionally, a maximum number of times the leader status information is broadcast can be detected. If the maximum number of broadcasts is exceeded, the broadcasting will stop.

[0059] Optionally, a heartbeat sending timer can be used to extend the first connectivity state of the leader, which is triggered by the leader of the current cluster node; an election timeout timer can be used to detect leader failure and re-elect when a single election fails; and an election status check timer can be used to check the election status of each node when the cluster node starts, and the election status check timer is turned off when the election begins.

[0060] Optionally, a heartbeat sending timer can be used to extend the first connectivity state of the leader, which is triggered by the leader of the current cluster node; an election timeout timer can be used to detect leader failure and re-elect when a single election fails; and an election status check timer can be used to check the election status of each node when the cluster node starts, and the election status check timer is turned off when the election begins.

[0061] In practical applications, the duration requirements for each timer are: the heartbeat interval is less than the election timeout interval, and the election status check timer occurs before the election. Therefore, the interval can be freely adjusted according to the duration required for the connection between each node.

[0062] The Paxos-like method can be broadly divided into three phases: election, data synchronization, and broadcasting. First, a node is elected as the leader. When the leader and other nodes in the cluster form a majority, the nodes communicate with each other to synchronize their data with the node holding the latest data version. Then, the leader can receive external read and write requests. When a write request is received, the leader needs to broadcast the written content to the majority of nodes. It can be seen that in the election phase, if the elected node can be guaranteed to have the highest data version, then during data synchronization, the time spent comparing versions can be saved, and data synchronization can proceed directly. Therefore, this invention is more suitable for implementing distributed server sets using the Paxos-like consensus method, solving the problem of low efficiency in electing the node with the latest data version as the leader.

[0063] This invention effectively avoids the risk of data loss by checking the election status and election rounds, preventing data version conflicts caused by outdated leader data versions and ensuring data version consistency. This invention improves the reliability of leader election, ensures the accuracy of election results, solves the problem of different version information in leader election in Paxos-like clusters, and is better adapted to the real-world operating environment of the cluster.

[0064] The above are merely preferred embodiments of the present invention and are not intended to limit the present invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A cluster leader election method with band version comparison, characterized in that, include: Define the election states of a node, including: the first election state, which indicates that the node is allowed to receive votes; when the election of a new leader begins, the node's election state is initialized to the first election state; the second election state, which indicates that the node's followers are currently prohibited from receiving votes; and the third election state, which indicates that the node's leader is currently prohibited from receiving votes. Define the transition relationships between various election states, including: when the data version of a node in the first election state is lower than a first set threshold, the node transitions to the second election state; when the data version of a node in the first election state is greater than a second set threshold, the node transitions to the third election state. Define the connectivity state of a node, including: a first connectivity state, which indicates that the current leader is online and the system is running; and a second connectivity state, which indicates that the local node has detected a loss of connection with the current leader. Each node in the cluster starts up, and the nodes are connected via TCP. Load the currently decided instance and initialize the election state to the first election state, while also initializing the current leader state to the second connectivity state; The node initiates an election status report; Set the conditions for a node to pass the election state, and detect the node's election state; Once the election status check is passed, each node is notified to initiate an election, and each node initiates and processes the election synchronously. After receiving broadcast votes from nodes other than itself, each node first determines whether it is in the first election state, and then checks whether the election state of the nodes in the received votes is also in the first election state. If the election state of each node is in the first election state, the election round is checked; otherwise, the election state of the node is initialized. Each node sets an election logic clock to mark the current election round. During the election, the election round is checked, and only votes from the same election round are considered valid votes. The global state table records all valid votes received during the current election round. When the number of valid votes collected meets the number of valid nodes in the current cluster, the global state table is traversed, and the node with the largest instance is selected as the leader, with the remaining nodes as followers. The election state of the local machine is modified synchronously. When the election ends, the node resets the election timeout timer, and the newly elected leader node starts the heartbeat sending timer and broadcasts heartbeat messages to continue the leader's first connectivity state.

2. The cluster leader election method with version comparison of claim 1, wherein, The conditions for a node to pass the election state include all nodes in the cluster being in the first election state, or more than half of the nodes in the cluster being in the first election state and the time after the majority has passed has exceeded the threshold.

3. The cluster leader election method with version comparison of claim 1, wherein, Each node synchronizes the election process by sending and receiving broadcast messages.

4. The cluster leader election method with version comparison according to claim 1, characterized in that, Each node sets an election logic clock to mark the current election round. During the election, only votes from the same election round are considered valid votes.

5. The cluster leader election method with version comparison according to claim 1, characterized in that, Each node sets up an election logic clock to mark the current election round. The election round increments each time an election is initiated, and then each node broadcasts its votes.

6. The cluster leader election method with version comparison according to claim 1, characterized in that, Examine the election rounds, including: If the election round in the ballot is greater than the local election round, then the local machine failed to participate in the previous election and is lagging behind in the election round. The local machine's election round is updated to the election round in the ballot, and the ballot is rebroadcast. If the election round in the ballot is less than the local election round, then the local machine's current ballot is ignored. If the election round in the ballot is equal to the local election round, then the global state table is updated.

7. The cluster leader election method with version comparison according to claim 1, characterized in that, Define a third connectivity state to characterize the state in which more than half of the nodes in the cluster have detected a loss of connection with the current leader; When the leader malfunctions or experiences network latency, and the node is in the second connectivity state, causing followers to fail to receive heartbeats and triggering an election timeout, the timed-out followers first mark the current leader as being in the first offline state. The first offline state represents the follower's subjective perception of the leader's offline status. Then, they broadcast the leader's perceived status information. After receiving this leader status information, other nodes update their own connectivity tables between each node and the current leader, and simultaneously check their own connectivity with the leader. If more than half of the cluster's valid nodes have marked the leader offline, and the node is in the third connectivity state, it modifies its own connectivity with the leader to offline and initiates an election request to enter a new round of elections. If less than half of the nodes have marked the leader offline, the node continues to broadcast the leader's perceived status information and continues to collect global connectivity information.

8. The cluster leader election method with version comparison according to claim 7, characterized in that, Set the upper limit on the number of times the leader's status information can be broadcast; if the upper limit is exceeded, the broadcasting will stop.

9. The cluster leader election method with version comparison according to claim 1, characterized in that, The leader's initial connectivity state is maintained using a heartbeat sending timer, triggered by the leader of the current cluster node; Utilize an election timeout timer to detect leader failure and perform re-election when a single election fails; The election status check timer is used to check the election status of each node when the cluster nodes start up. The election status check timer is turned off after the election begins.