Distributed system election method and device, electronic equipment and storage medium
By monitoring the leader node's heartbeat and evaluating the election broadcast messages of candidate nodes, the problems of slow fault recovery, low election efficiency, and poor data consistency in leader node election in distributed systems are solved, achieving more efficient node election and system stability.
Patent Information
- Application Number
- CN202511071175.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-31
- Publication Date
- 2025-11-07
AI Technical Summary
Existing leader election algorithms for distributed systems suffer from slow fault recovery, low election efficiency, poor data consistency, and insufficient adaptability to network environments.
By monitoring the heartbeat information of the leader node, the election broadcast messages of candidate nodes are obtained. Based on the preset weight information, the candidate nodes are evaluated and voted on to determine the target leader node, and the leader node is controlled to send election messages to other nodes to confirm its leadership.
It improves the speed of node failure recovery and election efficiency in distributed systems, and enhances data consistency and adaptability to network environments.
Smart Images

Figure CN120915787A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of server, and particularly relates to a method and device for distributed system election, electronic equipment and storage medium. BACKGROUND
[0002] In a distributed system, the election of a leader node is a core link for guaranteeing high availability and data consistency of the system, and the core goal is to quickly and accurately select a coordinator among multiple nodes to cope with node failures, network fluctuations and other abnormal situations. At present, the mainstream election algorithm in the industry has its own advantages in different scenarios, but there is still obvious optimization space. Related election algorithms still have room for improvement in terms of fault recovery speed, election efficiency, data consistency and network environment adaptability. Therefore, there is an urgent need for a node election method that can comprehensively optimize election speed, data consistency and system stability. SUMMARY
[0003] The present disclosure provides a method and device for distributed system election, electronic equipment and storage medium. The main purpose is to solve the problems of slow fault recovery speed, low election efficiency, poor data consistency and network environment adaptability in related technologies.
[0004] According to a first aspect of the present disclosure, a method for distributed system election is provided, comprising:
[0005] Monitoring the heartbeat of the current leader node, and determining whether the leader node needs to be re-elected based on the heartbeat information of the current leader node;
[0006] Obtaining the election broadcast message of each candidate node, the election broadcast message containing a term number, a transaction identifier, a load score and a node unique identifier;
[0007] Based on the election broadcast information and the preset weight information, performing node evaluation on each candidate node, and voting for the candidate node based on the node evaluation result;
[0008] According to the voting data of each candidate node, determining the target leader node for re-election, controlling the target leader node to send an election message to the remaining nodes, and determining the target leader node as the re-elected leader node in response to the remaining nodes turning into follower nodes.
[0009] Optionally, monitoring the heartbeat of the current leader node, and determining whether the leader node needs to be re-elected based on the heartbeat information of the current leader node, comprises:
[0010] Obtaining the heartbeat signal of the current leader node, and determining whether the number of lost heartbeats of the current leader node reaches a preset number;
[0011] If the preset number of times is reached, it is determined that the leader node needs to be re-elected;
[0012] If the preset number of times is not reached and it is determined that the heartbeat signal is in timeout, it is determined that the leader node needs to be re-elected.
[0013] Optionally, based on the election broadcast information and the preset weight information, node evaluation is performed on each candidate node, and voting is performed on the candidate nodes based on the node evaluation result, including:
[0014] Based on the preset weight information and the term number, the transaction identifier, the load score, and the node unique identifier, priority scores of each candidate node are calculated to perform node evaluation on each candidate node;
[0015] According to the priority scores of each candidate node, voting processing is performed on each candidate node.
[0016] Optionally, according to the priority score results of each candidate node, voting processing is performed on each candidate node, including:
[0017] Based on the preset weight information, a current priority score of the current follower node is calculated;
[0018] The priority scores of each candidate node are compared with the current priority score;
[0019] If the priority score result of the candidate node exceeds the current priority score, voting processing is performed on the candidate node whose priority score exceeds the current follower node.
[0020] Optionally, according to the voting data of each candidate node, a target leader node for re-election is determined, including:
[0021] The voting data of each candidate node is obtained, and the candidate node with the highest priority score is taken as the target leader node;
[0022] If there are multiple candidate nodes with equal priority scores, the election broadcast messages of the multiple candidate nodes are compared to determine the target leader node for re-election.
[0023] Optionally, the election broadcast messages of the multiple candidate nodes are compared to determine the target leader node for re-election, including:
[0024] The transaction identifiers, the term numbers, the load scores, and the node unique identifiers of the multiple candidate nodes are sequentially compared to determine the target leader node for re-election.
[0025] Optionally, the method for electing a distributed system further includes:
[0026] According to the cluster size of the distributed system, node health information and historical election information, preset weight information corresponding to the term number, the transaction identifier, the load score and the node unique identifier is dynamically adjusted.
[0027] According to a second aspect of the present disclosure, a device for distributed system election is provided, comprising:
[0028] A monitoring unit is configured to monitor a heartbeat of a current leader node, and determine whether a leader node needs to be re-elected based on heartbeat information of the current leader node.
[0029] An obtaining unit is configured to obtain an election broadcast message of each candidate node, the election broadcast message containing a term number, a transaction identifier, a load score and a node unique identifier.
[0030] An evaluation unit is configured to perform node evaluation on each candidate node based on the election broadcast information and preset weight information, and vote for the candidate node based on a node evaluation result.
[0031] A determination unit is configured to determine a target leader node for re-election according to voting data of each candidate node, control the target leader node to send an election message to remaining nodes, and determine the target leader node as a re-elected leader node in response to the remaining nodes turning into follower nodes.
[0032] Optionally, the monitoring unit comprises:
[0033] A judgment module is configured to obtain a heartbeat signal of the current leader node, and determine whether a number of lost heartbeats of the current leader node reaches a preset number.
[0034] A first determination module is configured to determine that the leader node needs to be re-elected if the preset number is reached.
[0035] A second determination module is configured to determine that the leader node needs to be re-elected if the preset number is not reached and it is determined that the heartbeat signal is in timeout.
[0036] Optionally, the evaluation unit comprises:
[0037] An evaluation module is configured to perform priority scoring on each candidate node based on the preset weight information and the term number, the transaction identifier, the load score and the node unique identifier, so as to realize node evaluation on each candidate node.
[0038] A voting module is configured to perform voting processing on each candidate node according to the priority score of each candidate node.
[0039] Optionally, the voting module is further configured to:
[0040] The preset weight information is used to calculate a current priority score of the current follower node.
[0041] The priority score of each candidate node is compared with the current priority score.
[0042] If the priority score of the candidate node exceeds the current priority score, the candidate node with the priority score exceeding the current follower node is voted.
[0043] Optionally, the determining unit comprises:
[0044] The obtaining module is configured to obtain voting data of each candidate node, and take the candidate node with the highest priority score as a target leader node.
[0045] The comparison module is configured to compare the election broadcast messages of the multiple candidate nodes with equal priority scores, and determine a target leader node for re-election.
[0046] Optionally, the comparison module is further configured to:
[0047] The transaction identifiers, the term numbers, the load scores and the node unique identifiers of the multiple candidate nodes are sequentially compared to determine the target leader node for re-election.
[0048] Optionally, the distributed system election device further comprises:
[0049] The adjusting unit is configured to dynamically adjust the preset weight information corresponding to the term number, the transaction identifier, the load score and the node unique identifier according to the cluster size, the node health information and the historical election information of the distributed system.
[0050] According to a third aspect of the present disclosure, an electronic device is provided, comprising:
[0051] at least one processor; and
[0052] a memory connected with the at least one processor; wherein
[0053] The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the distributed system election method of the first aspect.
[0054] According to a fourth aspect of the present disclosure, a non-transitory computer readable storage medium storing computer instructions is provided, wherein the computer instructions are used to enable the computer to execute the distributed system election method of the first aspect.
[0055] According to a fifth aspect of the present disclosure, a computer program product is provided, comprising a computer program which, when executed by a processor, implements the method of distributed system election as described in the first aspect above.
[0056] The present disclosure provides a method and device for distributed system election, an electronic device and a storage medium, which determines whether to re-election by monitoring the heartbeat of a leader node, acquires a broadcast message containing a term number, a transaction identifier, a load score and a node unique identifier of a candidate node, evaluates the candidate node based on a preset weight and votes, and then determines a target leader node according to the voting data, and confirms the target leader node as a leader node after it sends a message to make the remaining nodes become follower nodes. Therefore, the problems of slow fault recovery speed, low election efficiency, poor data consistency and network environment adaptability in the prior art can be solved, and the technical effects of improving the fault recovery speed of nodes in a distributed system and improving the election efficiency are achieved.
[0057] It should be understood that the content described in this part is not intended to identify key or important features of the embodiments of the present disclosure, nor to limit the scope of the present disclosure. Other features of the present disclosure will become apparent from the following description. BRIEF DESCRIPTION OF DRAWINGS
[0058] The accompanying drawings are used to better understand the present scheme and do not limit the present disclosure. Among them:
[0059] Figure 1 A flowchart of a method of distributed system election provided by an embodiment of the present disclosure;
[0060] Figure 2 A structural diagram of a device for distributed system election provided by an embodiment of the present disclosure;
[0061] Figure 3 A structural diagram of another device for distributed system election provided by an embodiment of the present disclosure. DETAILED DESCRIPTION
[0062] Exemplary embodiments of the present disclosure are described below with reference to the accompanying drawings, which include various details of the embodiments of the present disclosure to help understanding, and should be considered as merely exemplary. Therefore, those skilled in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Also, in order to be clear and concise, the description below omits the description of well-known functions and structures.
[0063] The method and device for distributed system election, the electronic device and the storage medium of the embodiments of the present disclosure are described below with reference to the accompanying drawings.
[0064] Figure 1A flowchart of a method for distributed system election provided by an embodiment of the present disclosure is shown.
[0065] As shown in the figure, the method comprises the following steps: Figure 1
[0066] Step 101: Monitor the heartbeat of the current leader node, and determine whether the leader node needs to be re-elected based on the heartbeat information of the current leader node.
[0067] In this embodiment, in the distributed system, the leader node (Leader) serves as the core coordinator of the system, and needs to prove its survival state and normal working ability to other nodes (mainly follower nodes, Follower) in the cluster by periodically sending heartbeat messages. The so-called heartbeat information usually refers to a short state notification broadcast by the leader node to all follower nodes at fixed time intervals (such as tens to hundreds of milliseconds), which contains the identification of the leader node, the current term information and other key contents, and is used to maintain the stable operation of the cluster and the state synchronization between nodes. To ensure that the abnormality of the leader node can be discovered in time, each follower node will monitor the heartbeat information from the current leader node in real time, and maintain a special heartbeat loss counter. Specifically, when the follower node successfully receives the heartbeat message of the leader node within the set time window, it will clear the counter, indicating that the current state of the leader node is normal; and if the heartbeat message is not received within the corresponding time window, the counter will automatically increase by 1, recording a heartbeat loss event.
[0068] Based on the above heartbeat monitoring mechanism, the system will determine whether to start the process of re-electing the leader node according to the value of the heartbeat loss counter. When the cumulative value of the counter reaches a preset threshold (for example, 3 consecutive heartbeat losses are detected), the follower node will determine that the current leader node may have failed (such as node downtime, network interruption, etc.), and cannot continue to perform the coordination function. At this time, the operation of re-electing the leader node will be triggered immediately to ensure that the system can quickly recover the normal coordination ability and avoid the entire distributed system being paralyzed or in a state of data inconsistency due to the failure of the leader node. This monitoring and judgment mechanism based on heartbeat information is an important basis for ensuring the high availability and fault tolerance of the distributed system, and can quickly respond when the leader node is abnormal, providing a trigger signal for the subsequent election process.
[0069] Step 102: Obtain the election broadcast message of each candidate node, which contains the term number, transaction identifier, load score and node unique identifier.
[0070] In the present embodiment, in the election process of the distributed system, when the re-election mechanism is triggered, the candidate nodes (Candidates) in the cluster actively broadcast election messages (ELECTION messages) to other nodes in the cluster to participate in the election of the leader node. Each follower node (Follower) is responsible for receiving the election broadcast messages from the candidate nodes, and provides a basis for subsequent voting decisions by analyzing and processing the key information contained in the messages. Among them, the content contained in the election broadcast message has a clear and key role: the term number (Term) is an important parameter for identifying the current election period, and only one valid leader node can be generated in each term. The increment of the value ensures the orderliness and authority of the election process, avoiding the interference of expired election information; the transaction identifier (ZXID, i.e. transaction ID) can intuitively reflect the freshness of the candidate node processing transactions. The larger the ZXID value, the newer the data saved by the node, which is crucial to ensure the data consistency of the newly elected leader node and can reduce the subsequent data synchronization overhead; the load score (Load) is a quantitative result obtained by comprehensively calculating the current CPU usage, memory occupation, network delay and connection number of the candidate node. It directly reflects the current workload of the node, avoiding the selection of high-load nodes as leader nodes and affecting the running efficiency of the entire system; the node unique identifier (NodeID) is the unique identity of each node in the cluster, which is used as one of the final judgment bases when other information conflicts or needs further differentiation, ensuring that each node can be accurately identified. By obtaining the election broadcast message containing these key information, the follower node can fully understand the basic state and qualifications of each candidate node, lay a foundation for subsequent multi-factor based priority evaluation and voting decision, and make the entire election process more scientific, reasonable and meet the requirements of the system for consistency and stability.
[0071] In step 103, based on the election broadcast information and the preset weight information, the node evaluation of each candidate node is performed, and the voting to the candidate node is performed based on the node evaluation result.
[0072] In the present embodiment, in the process of node evaluation and voting of each candidate node based on the election broadcast information and the preset weight information, the follower node first parses the election broadcast message received from the candidate node to extract key information such as the term number (Term), the transaction identifier (ZXID), the load score (Load) and the node unique identifier (NodeID). Subsequently, according to the preset weight system (i.e. the weight coefficients w1, w2, w3, w4 set for Term, ZXID, Load, NodeID respectively, and satisfy w1+w2+w3+w4=1), the information is normalized - specifically, the Term value is divided by the maximum Term value known in the current cluster to achieve normalization, thereby reflecting the relative level of the candidate node in the term authority; the ZXID value is divided by the maximum ZXID value in the cluster to complete the normalization, which is used to reflect the freshness of the node processing transactions; for the load score, the normalization is performed by subtracting the ratio of Load to the preset threshold from 1, thereby quantifying the current load of the node, and the higher the value, the lighter the load; and the normalization of NodeID is to divide (the NodeID minus the smallest NodeID in the cluster) by (the difference between the largest NodeID and the smallest NodeID in the cluster), thereby eliminating the influence of different node ID value differences. After normalizing each item of information, the follower node multiplies the normalized Term value by w1, the ZXID value by w2, the Load normalized value by w3, and the NodeID normalized value by w4, then adds the four results and multiplies by 100 to obtain the comprehensive priority score of the candidate node, which is used as the core basis for evaluating whether the node is suitable to become a leader node. In the voting stage, the follower node compares the comprehensive priority scores of each candidate node calculated, and preferentially sends a voting request to the candidate node with the highest score; if there are multiple candidate nodes with the same comprehensive priority score, they are further compared in turn according to their ZXID (preferentially selecting the node with a larger ZXID to ensure fresh data), Term (selecting the node with a larger Term to reflect the authority of the term), Load (selecting the node with a lower load score to avoid high-load nodes becoming leader nodes) and NodeID (in the case where the above conditions are the same, selecting the node with a larger NodeID to ensure the distinguishing effect of the unique identifier), and determining the final voting object according to this progressive comparison rule to complete the voting operation. This process converts multi-dimensional node information into a quantifiable comprehensive score for comparison, and combines with a clear voting rule, which not only ensures the comprehensiveness and objectivity of the evaluation, but also ensures the rationality of the voting decision, laying a foundation for subsequent selection of the optimal leader node.
[0073] In step 104, the target leader node for re-election is determined according to the voting data of each candidate node, and the target leader node is controlled to send an election message to the remaining nodes, and the target leader node is determined to be the leader node for re-election in response to the remaining nodes turning into follower nodes.
[0074] In the election process of the distributed system in the embodiment, after each candidate node completes the collection of the voting information, the system performs statistics and analysis on the voting data of all candidate nodes to determine whether a candidate node has obtained more than half of the total number of cluster nodes. This "more than half" determination criterion is the key to ensuring the uniqueness and effectiveness of the election result, which can effectively avoid the coexistence of multiple leader nodes (i.e., "split brain") caused by network partition and other abnormal conditions, and ensure the consistency of the system. When the number of votes obtained by a candidate node reaches this threshold, the node is preliminarily determined as the target leader node for re-election. Subsequently, the system controls the target leader node to broadcast a victory message (Victory message) containing the identification of the target leader node, the current term information, the latest transaction state, and the like to all the remaining nodes in the cluster, to announce its election status. After receiving the Victory message, the remaining nodes verify the qualifications of the target leader node according to the preset rules, mainly including comparing the priority score of the target leader node with the priority score of the remaining nodes, and if the score of the target leader node is not lower than that of the remaining nodes, the remaining nodes will automatically turn into follower (Follower) state, accept the coordination and instructions of the new leader node, and follow the data synchronization and task allocation arrangement of the new leader node. When the system monitors that the remaining nodes in the cluster have successfully turned into follower state and can normally respond to the heartbeat message and instructions of the target leader node, it is confirmed that the target leader node has obtained the universal recognition of the cluster, and the target leader node is officially determined as the leader node generated by re-election, and the entire election process is completed. This process ensures that the new leader node can smoothly take over the system coordination responsibilities and maintain the continuous and stable operation of the distributed system through strict voting effectiveness determination, clear victory message transmission, and orderly node state conversion.
[0075] The present disclosure provides a method for election of a distributed system, which determines whether to re-elect by monitoring the heartbeat of a leader node, acquires a broadcast message containing a term number, a transaction identifier, a load score, and a unique identifier of a candidate node, evaluates the candidate node based on a preset weight and votes, and then determines a target leader node according to the voting data, and confirms the target leader node as a leader node after the target leader node sends a message to make the remaining nodes turn into follower nodes. Therefore, the problems of slow fault recovery speed, low election efficiency, poor data consistency, and network environment adaptability in the prior art can be solved, and the technical effects of improving the speed of node fault recovery and improving the election efficiency in the distributed system are achieved.
[0076] Further, in some embodiments of the present disclosure, for the operation of "monitoring the heartbeat of the current leader node, determining whether the leader node needs to be re-elected based on the heartbeat information of the current leader node", the specific implementation is various, and for clear description, the following enumerated implementation modes include but are not limited to part of the implementation modes: obtaining the heartbeat signal of the current leader node, judging whether the number of lost heartbeats of the current leader node reaches a preset number; if the preset number is reached, it is determined that the leader node needs to be re-elected; if the preset number is not reached and it is determined that the heartbeat signal exists timeout, it is determined that the leader node needs to be re-elected.
[0077] Specifically, in the distributed system, the heartbeat monitoring of the current leader node is a key link to ensure that the system responds to node failure in time and maintains high availability. Specifically, each follower node in the cluster continuously listens to the periodic heartbeat signal from the leader node. This heartbeat signal usually contains the identification of the leader node, the current term number (Term) and the basic state information of the system running, which is used to prove that the leader node is in a normal working state. In order to accurately track the reception of the heartbeat, each follower node maintains an independent heartbeat loss counter, which is initially set to 0. When the follower node fails to successfully receive the heartbeat signal from the leader node within a set time window (such as a time length matching the heartbeat sending interval), the counter will automatically increase by 1, recording a heartbeat loss event. Once the heartbeat signal is successfully received, the counter will be immediately cleared and reset to the initial state. Based on the value of the heartbeat loss counter, the system will determine whether to start the re-election process according to the preset rules. When the cumulative value of the counter reaches a preset trigger number (for example, 3 consecutive heartbeat losses are detected), the follower node will directly determine that the current leader node may have failed (such as node downtime, network connection interruption, etc.), at which time it will no longer wait for additional timeout time and immediately trigger the operation of re-electing the leader node. This mechanism aims to ensure a quick response to leader node failure and minimize the service interruption time caused by the system being leaderless. If the value of the heartbeat loss counter has not reached the above-mentioned preset number, but the follower node has not received the heartbeat signal from the leader node within a randomly set timeout time (which is usually set in the range of 150-300 ms, and the specific value is randomly generated by the system within this range), the bottom election trigger mechanism will be started, and it will also be determined that the leader node needs to be re-elected. The design of this random timeout bottom mechanism is mainly to cope with the scenario of high network delay or large fluctuation. By introducing a random time difference, it avoids the simultaneous triggering of election by multiple follower nodes due to the simultaneous detection of heartbeat abnormalities, thereby reducing unnecessary election conflicts and resource consumption and ensuring the orderliness of the election process.
[0078] Through the two heartbeat information-based determination manners, i.e., the immediate triggering when the heartbeat loss times reaches the preset number, and the bottom triggering when the heartbeat is not received after the timeout, the system can flexibly respond in different network environments and failure scenarios, which not only ensures that the re-election can be quickly started when the leader node actually fails, but also effectively avoids the misjudgment caused by the transient fluctuation of the network, and provides accurate and reliable triggering basis for the subsequent re-election of the leader node.
[0079] Further, in some embodiments of the present disclosure, for the operation of "conducting node evaluation on each candidate node based on the election broadcast information and the preset weight information, and voting for the candidate node based on the node evaluation result", the specific implementation manners are various. For clear illustration, the following enumerated implementation manners include but are not limited to part of the implementation manners: priority scoring of each candidate node based on the preset weight information and the term number, the transaction identifier, the load score and the node unique identifier, so as to realize the node evaluation on each candidate node; voting processing of each candidate node according to the priority score of each candidate node.
[0080] Specifically, in the process of node evaluation and voting of each candidate node based on the election broadcast information and the preset weight information, firstly, the meanings and roles of the term number (Term), transaction identifier (ZXID), load score (Load) and node unique identifier (NodeID) contained in the election broadcast information need to be clarified: Term represents the term in which the node currently locates, the larger the value is, the newer the election cycle that the node participates in is, and the higher the term authority is; ZXID is the latest transaction ID processed by the node, and the value size directly reflects the freshness of the node data, the larger the ZXID is, the newer the data saved by the node is; Load is a comprehensive load index calculated by weighting the CPU usage rate, network delay, current connection number and other indicators of the node, which is used to reflect the current workload of the node, and the lower the value is, the lighter the load of the node is; NodeID is the unique identifier of the node in the cluster, which is used as the final basis for judgment when other information cannot be distinguished. Next, the priority score of each candidate node is calculated based on the preset weight information (i.e. the weight coefficients w1, w2, w3 and w4 set for Term, ZXID, Load and NodeID respectively, and w1+w2+w3+w4=1) to complete the node evaluation. When calculating, the normalization processing of each item of information is needed: divide the Term value by the maximum Term value known in the cluster to obtain the normalized result of Term, so as to eliminate the influence of the absolute value of different terms and highlight the relative authority; divide the ZXID value by the maximum ZXID value in the cluster to obtain the normalized result of ZXID, which quantifies the relative level of node data freshness; for Load, normalize it by subtracting the ratio of Load to the preset threshold from 1, so that the result can intuitively reflect the lightness and heaviness of the node load, and the higher the normalized value is, the more suitable the load is as a leader node; normalize NodeID by the formula (NodeID-cluster minimum NodeID) / (cluster maximum NodeID-cluster minimum NodeID) to eliminate the interference caused by the difference of different node ID values and ensure that the influence of NodeID in scoring conforms to the preset weight. After normalization, multiply each normalized result by the corresponding weight coefficient, add the products and multiply by 100, and the comprehensive priority score of the candidate node can be obtained, the formula is: Priority=(Term normalized value×w1+ZXID normalized value×w2+Load normalized value×w3+NodeID normalized value×w4)×100. This score comprehensively considers the term authority, data freshness, load condition and unique identifier of the node, and can fully reflect the adaptation degree of the candidate node as a leader node.
[0081] When voting based on the evaluation results of the nodes, clear rules need to be followed: each follower node compares the comprehensive priority scores of all candidate nodes, and preferentially sends a voting request to the candidate node with the highest score; if there are multiple candidate nodes with the same comprehensive priority score, comparison is performed in the order of ZXID, Term, Load, and NodeID, that is, the node with a larger ZXID is selected first (to ensure that the data is fresh), if the ZXIDs are the same, the node with a larger Term is selected (to reflect the authority of the term), if the Terms are the same, the node with a lower Load is selected (to avoid high-load nodes affecting system performance), and if the above information is the same, the node with a larger NodeID is selected (to finally determine through a unique identifier).
[0082] Further, in some embodiments of the present disclosure, for the operation of “voting for each candidate node according to the priority score results of each candidate node”, the specific implementation is various, and for clear illustration, the following enumerated implementation modes include but are not limited to part of the implementation modes: calculating the current priority score of the current follower node based on preset weight information; comparing the priority scores of each candidate node with the current priority score; if the priority score result of the candidate node exceeds the current priority score, voting processing is performed on the candidate node whose priority score exceeds the current follower node.
[0083] Specifically, in the voting process according to the priority score results of each candidate node, first, as a follower, the current priority score of itself needs to be calculated based on the preset weight information (that is, the weight coefficients w1, w2, w3, w4 configured respectively for the term number Term, the transaction identifier ZXID, the load score Load, and the node unique identifier NodeID, and w1+w2+w3+w4=1 is satisfied). The calculation process is consistent with the scoring calculation logic of the candidate node: first, the Term, ZXID, Load, and NodeID of itself are normalized— the Term normalized value is the Term of itself divided by the maximum Term value known in the cluster, the ZXID normalized value is the ZXID of itself divided by the maximum ZXID value in the cluster, the Load normalized value is 1 minus the ratio of the Load of itself to the preset threshold, and the NodeID normalized value is (the NodeID of itself minus the minimum NodeID of the cluster) divided by (the difference between the maximum NodeID and the minimum NodeID of the cluster); then the normalized values are multiplied by the corresponding weight coefficients respectively, and the product is added and multiplied by 100 to obtain the current priority score of itself, which is used as a quantitative reflection of the qualification of itself participating in the election. Subsequently, the follower compares the priority score of each candidate node (which is obtained by the follower according to the Term, ZXID, Load, and NodeID contained in the election message broadcast by the candidate node, in the same normalization and weighting calculation manner) with the current priority score of itself. In the comparison process, if the priority score of a certain candidate node is higher than the current priority score of the follower itself, it means that the candidate node is superior to the follower itself in comprehensive qualifications (including term authority, data freshness, load condition, etc.), and at this time the follower sends a voting request to the candidate node to complete the voting process; if the priority score of the candidate node is equal to or lower than the current priority score of the follower itself, the follower will not vote for the candidate node, so as to ensure that the voting resources are concentrated on the better candidate node.
[0084] Further, in some embodiments of the present disclosure, for the operation of "determining the target leader node for re-election according to the voting data of each candidate node", the specific implementation manners are various. For clear illustration, the following listed implementation manners include but are not limited to part of the implementation manners: obtaining the voting data of each candidate node, and taking the candidate node with the highest priority score as the target leader node; if there are multiple candidate nodes with equal priority scores, comparing the election broadcast messages of the multiple candidate nodes to determine the target leader node for re-election.
[0085] Specifically, when determining the target leader node for re-election according to the voting data of each candidate node, it is first necessary to summarize and count the voting of all candidate nodes in the cluster. The voting data here includes not only the number of support votes obtained by each candidate node, but also the comprehensive priority score of each candidate node calculated previously. The system will first compare the comprehensive priority scores of all candidate nodes, and select the candidate node with the highest priority score. Since this node performs best in multi-dimensional comprehensive evaluation of term authority, data freshness, load condition, etc., it will be preliminarily determined as the target leader node for re-election. The core logic of this process is to select the node that is theoretically most suitable for assuming leadership through quantitative priority score, to ensure that the target leader node can efficiently coordinate the operation of the cluster. If it is found during the comparison process that there are multiple candidate nodes with completely equal comprehensive priority scores, further progressive comparison based on the specific information contained in the election broadcast message of these candidate nodes is needed to determine the unique target leader node. Specifically, the transaction identifier (ZXID), term number (Term), load score (Load), and node unique identifier (NodeID) of each node will be compared in the preset priority order: first compare ZXID, the node with a larger ZXID value means that the transaction is fresher and the data consistency is more guaranteed, so it is preferred to be selected as the target leader node; if the ZXID is also the same, compare the term number (Term), the node with a larger Term represents a later election cycle and has higher term authority, and is preferred as the target; if the Term is still the same, compare the load score (Load), and select the node with a lower load score to avoid high-load nodes becoming leader nodes and affecting the overall performance of the system; if the above information is the same, finally compare the node unique identifier (NodeID), and select the node with a larger NodeID to distinguish the target leader node through unique identification. Through this progressive comparison mechanism, even in the case of equal priority scores, a unique target leader node can be selected based on clear rules, ensuring the uniqueness and effectiveness of the election result, and laying a foundation for the confirmation of the subsequent leader node and the coordination of the cluster.
[0086] Further, in some embodiments of the present disclosure, for the operation of "comparing the election broadcast messages of multiple candidate nodes to determine the target leader node for re-election", the specific implementation methods are various. For clear illustration, the following listed implementation methods include but are not limited to some of the implementation methods: sequentially comparing the transaction identifier, term number, load score, and node unique identifier of multiple candidate nodes to determine the target leader node for re-election.
[0087] Specifically, when the comprehensive priority scores of multiple candidate nodes are equal, the target leader node needs to be determined by comparing the transaction identifiers (ZXIDs), term numbers (Terms), load scores (Loads), and node unique identifiers (NodeIDs) contained in the election broadcast messages of the candidate nodes in turn. Specifically, first, the ZXIDs of the candidate nodes are compared. The ZXID is a unique identifier of a transaction processed by a node, and the greater the value, the newer the transaction processed by the node, and the closer the data state to the latest state of the system. Therefore, the candidate node with a greater ZXID is preferentially selected. If the ZXIDs are the same, the term numbers (Terms) are further compared. The Term represents an election cycle in which a node is located, and the greater the value, the newer the election stage in which the node participates, and the higher the term authority. Therefore, the node with a greater Term is preferentially considered. If the Terms are still the same, the load scores (Loads) are compared. The Load is calculated through indexes such as CPU utilization, network delay, and connection number. The lower the value, the lighter the current workload of the node, and the more suitable the node is to undertake the coordination responsibilities of the leader node. Therefore, the node with a lower Load is selected. If the above information is the same, the node unique identifiers (NodeIDs) are finally compared. The NodeID is a unique identity of each node in the cluster. At this time, the node with a greater NodeID is selected as the target leader node. Through this progressive comparison mechanism, the unique target leader node can be finally determined based on the order of data freshness, term authority, load rationality, and unique identity in the case of equal priority scores, ensuring the uniqueness and effectiveness of the election result, avoiding the election stalemate caused by information conflicts, and ensuring the stable operation of the distributed system.
[0088] Further, in the embodiments of the present disclosure, the method for electing a distributed system further includes other specific implementation steps in addition to the foregoing. To clearly present these components, the related specific implementation modes are described in detail as follows: according to the cluster size, node health information, and historical election information of the distributed system, the preset weight information corresponding to the term number, transaction identifier, load score, and node unique identifier is dynamically adjusted.
[0089] Specifically, in the distributed system election process, the dynamic adjustment of the preset weight information is the key link to ensure that the election mechanism adapts to different running scenarios. The core is to flexibly adjust the weight coefficients (w1, w2, w3, w4) corresponding to the term number (Term), the transaction identifier (ZXID), the load score (Load), and the node unique identifier (NodeID) according to the cluster size, the node health information, and the historical election information. Specifically, when the cluster size changes, the adjustment logic will trigger accordingly: for example, in a small-scale cluster, the weight of the node unique identifier (NodeID) can be appropriately increased to quickly determine the leader node by using its uniqueness; when the cluster size expands (such as the number of nodes exceeds a preset threshold), the weight of NodeID is reduced (such as from 0.1 to 0.05), avoiding that the static node identifier dominates the election and ensuring that more nodes have the opportunity to participate in the election based on comprehensive qualifications. For the node health information, if a node frequently fails (such as downtime, response delay, etc.) in historical operation, the system will reduce the weight coefficient of the load score (Load) of the node, reducing the possibility of the node being selected as the leader node due to the temporary low load, preventing the faulty node from frequently dominating the system, and improving the stability of the cluster. The adjustment combined with the historical election information is more targeted: for example, if the data synchronization efficiency is low due to high network delay in multiple past elections, the system will increase the weight of the transaction identifier (ZXID) to preferentially select a node with fresh data as the leader, reducing the data coordination overhead in the recovery phase; if the historical election results show that the authority of the term number (Term) is crucial to avoiding election conflicts, the weight of w1 will be increased to strengthen the role of the term mechanism in regulating the election order. Through this dynamic weight adjustment based on the actual running state, the influence degree of each scoring factor can be accurately matched with the current needs of the cluster, so that the election process can quickly respond to failures while taking into account multiple dimensions such as data consistency, load balancing, and other multi-dimensional goals, significantly improving the adaptive ability and overall reliability of the distributed system.
[0090] It should be noted that the embodiments of the present disclosure can include multiple steps, which are numbered for ease of description, but these numbers are not a limitation on the execution time slots and execution order between the steps; the steps can be implemented in any order, and the embodiments of the present disclosure do not limit this.
[0091] Corresponding to the above-mentioned method of distributed system election, the present disclosure also proposes a device for distributed system election. Since the device embodiment of the present disclosure corresponds to the above-mentioned method embodiment, for the details not disclosed in the device embodiment, please refer to the above-mentioned method embodiment, which will not be described in detail in the present disclosure.
[0092] Figure 2 The structure diagram of a device for distributed system election provided by the embodiments of the present disclosure is as follows:Figure 2 As shown in the figure, comprising:
[0093] The monitoring unit 21 is configured to monitor the current leader node heartbeat, determine whether the leader node needs to be re-elected based on the heartbeat information of the current leader node;
[0094] The acquisition unit 22 is configured to acquire the election broadcast message of each candidate node, and the election broadcast message contains the term number, transaction identifier, load score and node unique identifier;
[0095] The evaluation unit 23 is configured to evaluate each candidate node based on the election broadcast information and the preset weight information, and vote for the candidate node based on the node evaluation result;
[0096] The determination unit 24 is configured to determine the target leader node of re-election according to the voting data of each candidate node, control the target leader node to send the election message to the remaining nodes, and determine the target leader node as the leader node of re-election in response to the remaining nodes turning into follower nodes.
[0097] The present disclosure provides a kind of distributed system election device, whether re-election is determined by monitoring leader node heartbeat, the broadcast message of candidate node containing term number, transaction identifier, load score and node unique identifier is acquired, candidate node is evaluated based on preset weight and votes, and target leader node is determined according to voting data, after its sending message makes the remaining nodes turn into follower node, it is confirmed as leader node.Therefore, it can solve the problems of slow fault recovery speed, low election efficiency, poor data consistency and network environment adaptability when node election is carried out in prior art, reach the technical effect of improving the speed of node fault recovery in distributed system, improve the election efficiency.
[0098] Further, in a possible implementation manner of the embodiment, as shown in the figure, Figure 3 The monitoring unit 21 comprises:
[0099] The judging module 211 is configured to acquire the heartbeat signal of the current leader node, and judge whether the loss times of the heartbeat of the current leader node reach the preset times;
[0100] The first determination module 212 is configured to determine that the leader node needs to be re-elected if the preset times are reached;
[0101] The second determination module 213 is configured to determine that the leader node needs to be re-elected if the preset times are not reached and it is determined that the heartbeat signal exists timeout.
[0102] Further, in a possible implementation manner of the embodiment, as shown in the figure, Figure 3 The evaluation unit 23 comprises:
[0103] The evaluation module 231 is configured to perform priority scoring on each candidate node based on the preset weight information and the term number, the transaction identifier, the load score, and the node unique identifier, so as to perform node evaluation on each candidate node.
[0104] The voting module 232 is configured to perform voting processing on each candidate node according to the priority score of each candidate node.
[0105] Further, in a possible implementation manner of the embodiment, the voting module 232 is further configured to:
[0106] calculate the current priority score of the current follower node based on the preset weight information;
[0107] compare the priority score of each candidate node with the current priority score;
[0108] if the priority score of the candidate node exceeds the current priority score, perform voting processing on the candidate node whose priority score exceeds the current follower node.
[0109] Further, in a possible implementation manner of the embodiment, as shown in Figure 3 The determination unit 24 includes:
[0110] The acquisition module 241 is configured to acquire voting data of each candidate node, and take the candidate node with the highest priority score as the target leader node.
[0111] The comparison module 242 is configured to, if there are multiple candidate nodes with equal priority scores, compare the election broadcast messages of the multiple candidate nodes, and determine the target leader node for re-election.
[0112] Further, in a possible implementation manner of the embodiment, the comparison module 242 is further configured to:
[0113] sequentially compare the transaction identifier, the term number, the load score, and the node unique identifier of the multiple candidate nodes, and determine the target leader node for re-election.
[0114] Further, in a possible implementation manner of the embodiment, as shown in Figure 3 The apparatus for distributed system election further includes:
[0115] The adjustment unit 25 is configured to dynamically adjust the preset weight information corresponding to the term number, the transaction identifier, the load score, and the node unique identifier according to the cluster size of the distributed system, the node health information, and the historical election information.
[0116] It should be noted that the foregoing explanation of the method embodiments also applies to the device of the present embodiment, and the principles are the same, which will not be limited herein.
[0117] The features of the device corresponding to the embodiments of the distributed system election can be referred to the related description of the method corresponding to the embodiments of the distributed system election, which will not be described herein.
[0118] The embodiments of the present application also provide an electronic device, comprising a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform the steps in any of the above-mentioned method embodiments of the distributed system election.
[0119] The embodiments of the present application also provide a computer readable storage medium, which stores a computer program, wherein the computer program is configured to perform the steps in any of the above-mentioned method embodiments of the distributed system election when running.
[0120] In an exemplary embodiment, the above-mentioned computer readable storage medium can include, but is not limited to, a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and various media that can store computer programs.
[0121] The embodiments of the present application also provide a computer program product, which comprises a computer program, and the computer program is executed by a processor to implement the steps in any of the above-mentioned method embodiments of the distributed system election.
[0122] The embodiments of the present application also provide another computer program product, which comprises a non-volatile computer readable storage medium, and the non-volatile computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement the steps in any of the above-mentioned method embodiments of the distributed system election.
[0123] The skilled person can further realize that the units and algorithm steps of the examples described in conjunction with the embodiments disclosed herein can be realized by electronic hardware, computer software or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the components and steps of the examples have been described in a general manner in the foregoing description. Whether the functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. The skilled person can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.
[0124] The above describes in detail the method and device for distributed system election, the electronic device and the storage medium provided by the present application. The principles and implementation modes of the present application are described by applying specific examples, and the above description of the embodiments is only used to help understand the method of the present application and its core idea. It should be pointed out that, for those skilled in the art, some improvements and modifications can be made to the present application without departing from the principles of the present application, and these improvements and modifications also fall within the protection scope of the claims of the present application.
Claims
1. A method for distributed system election, the method comprising: The method comprises the following steps: monitoring heartbeat of a current leader node, determining whether a leader node needs to be re-elected based on heartbeat information of the current leader node; obtaining election broadcast messages of each candidate node, wherein the election broadcast messages comprise a term number, a transaction identifier, a load score and a node unique identifier; based on the election broadcast information and preset weight information, performing node evaluation on each candidate node, and voting for the candidate nodes based on the node evaluation result; determining a target leader node to be re-elected according to voting data of each candidate node, controlling the target leader node to send an election message to the remaining nodes, and determining the target leader node as a re-elected leader node in response to the remaining nodes turning into follower nodes.
2. The method of distributed system election according to claim 1, wherein, The monitoring of the heartbeat of the current leader node and the determination of whether the leader node needs to be re-elected based on the heartbeat information of the current leader node comprise the following steps: obtaining a heartbeat signal of the current leader node, and determining whether the number of lost heartbeats of the current leader node reaches a preset number; if the preset number is reached, it is determined that the leader node needs to be re-elected; if the preset number is not reached and it is determined that the heartbeat signal is timed out, it is determined that the leader node needs to be re-elected.
3. The method of claim 1, wherein, The node evaluation on each candidate node based on the election broadcast information and the preset weight information, and the voting for the candidate nodes based on the node evaluation result comprise the following steps: based on the preset weight information and the term number, the transaction identifier, the load score and the node unique identifier, priority scores of each candidate node are calculated to realize the node evaluation on each candidate node; voting processing is performed on each candidate node according to the priority scores of each candidate node.
4. The method of distributed system election according to claim 3, wherein, The voting processing on each candidate node according to the priority score results of each candidate node comprises the following steps: based on the preset weight information, a current priority score of a current follower node is calculated; the priority scores of each candidate node are compared with the current priority score; if the priority score result of a candidate node exceeds the current priority score, the candidate node whose priority score exceeds the current follower node is voted for.
5. The method of claim 1, wherein, The determination of the target leader node to be re-elected according to the voting data of each candidate node comprises the following steps: obtaining the voting data of each candidate node, and taking the candidate node with the highest priority score as the target leader node to be re-elected; if there are multiple candidate nodes with equal priority scores, the election broadcast messages of the multiple candidate nodes are compared to determine the target leader node to be re-elected.
6. The method of distributed system election according to claim 5, wherein, The comparison of the election broadcast messages of the multiple candidate nodes to determine the target leader node to be re-elected comprises the following steps: the transaction identifiers, the term numbers, the load scores and the node unique identifiers of the multiple candidate nodes are compared in sequence to determine the target leader node to be re-elected.
7. The method of claim 1, wherein, The method further comprises the following steps: based on the cluster size, node health information and historical election information of the distributed system, the preset weight information corresponding to the term number, the transaction identifier, the load score and the node unique identifier is dynamically adjusted.
8. An apparatus for a Byzantine system election, the apparatus comprising: The method comprises the following steps: A monitoring unit configured to monitor a current leader node heartbeat, determine whether a leader node needs to be re-elected based on heartbeat information of the current leader node; An obtaining unit configured to obtain an election broadcast message of each candidate node, the election broadcast message containing a term number, a transaction identifier, a load score, and a node unique identifier; An evaluating unit configured to perform node evaluation on each candidate node based on the election broadcast information and preset weight information, and vote for a candidate node based on a node evaluation result; A determining unit configured to determine a target leader node for re-election according to voting data of each candidate node, control the target leader node to send an election message to remaining nodes, and determine the target leader node as a leader node for re-election in response to the remaining nodes turning into follower nodes.
9. An electronic device, comprising: comprise: at least one processor; and a memory connected with the at least one processor in communication; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method for distributed system election of any one of claims 1-7.
10. A non-transitory computer-readable storage medium having stored thereon computer instructions, wherein, The computer instructions are used to enable the computer to perform the method for distributed system election of any one of claims 1-7.
Citation Information
Cited By
High-availability VRS differential data broadcasting method and system
CN121442270A