Method and device for calculating asymmetric weights of nodes of Raft leader election mechanism
Through the node asymmetric weight method, the leader election in the Raft algorithm is dynamically adjusted, which solves the problem that the leader election mechanism is easily affected by multiple parties, achieves stable and efficient leader election, and avoids election storms and waste of resources.
Patent Information
- Application Number
- CN202410795810.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-19
- Publication Date
- 2024-09-06
AI Technical Summary
The leader election mechanism of the Raft algorithm is easily affected by problems such as network partitions and frequent node disconnection, leading to problems such as election storms, difficulty in balancing election timeouts, and excessive resource consumption.
The node asymmetric weight method is used to dynamically adjust the probability of a node becoming the leader by calculating the node's basic weight, performance bonus and penalty factor, combined with exponential moving average smoothing to ensure the stability and efficiency of the leader.
It reduces the occurrence of election storms, improves the stability and efficiency of leaders, and avoids the insecurity caused by excessive concentration of leaders.
Smart Images

Figure CN118612232A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of distributed consensus algorithms, and more specifically, to a method and device for calculating asymmetric weights of nodes in a Raft leader election mechanism. Background Art
[0002] In a distributed system, due to network delays, node failures, and network partitions, the data and states on each node may be inconsistent. To solve this problem, a distributed consensus algorithm is designed to ensure that all nodes in the system can reach a consensus on a certain value or state.
[0003] As a typical distributed system, blockchain has a variety of consensus algorithms, such as proof of work (PoW), proof of stake (PoS), practical Byzantine fault tolerance (PBFT), etc. There is also the popular RAFT algorithm in recent years, which is a consistency algorithm suitable for non-Byzantine fault distributed systems. It can reach a consistent state in a limited time and is suitable for scenarios where consensus needs to be reached quickly.
[0004] However, although the Raft algorithm has the advantages of being simple to understand, easy to implement, and highly fault-tolerant, its leader election mechanism still has many shortcomings. It is easily affected by multiple parties and may cause problems during the election. For example, network partitions or frequent node disconnection may lead to frequent leader elections, forming an "election storm", or the election timeout is difficult to find a balance between avoiding misjudgment and rapid recovery. In addition, frequent leader elections in large-scale clusters will consume a lot of network and CPU resources, etc. Therefore, it is necessary to optimize the scalability of the leader election mechanism of the Raft consensus algorithm. Summary of the invention
[0005] The purpose of the present invention is to provide a method and device for calculating asymmetric weights of nodes in a Raft leader election mechanism, so as to overcome the defects existing in the prior art.
[0006] In order to achieve the above object, the technical solution adopted by the present invention is as follows:
[0007] A method for calculating asymmetric weights of nodes in a Raft leader election mechanism includes the following steps:
[0008] S1. Calculate the basic weights of different nodes;
[0009] S2. Dynamically calculate the performance bonus PS based on the node performance factor and the node performance weight;
[0010] S3. Dynamically increase the penalty factor PF according to the frequency of the node becoming a leader. This penalty factor is used to punish nodes that become leaders more frequently than the set frequency.
[0011] S4. Smoothing the performance bonus PS and the penalty factor PF by applying exponential moving average to obtain the node score.
[0012] Furthermore, the basic weight in step S1 is the initial weight of the node. The initial weight or priority is set during initialization, and the initial weight is allocated according to the inherent characteristics of the node.
[0013] Furthermore, the node performance in step S2 includes response time, processing capability, network delay, and available bandwidth.
[0014] Furthermore, the calculation formula of the performance bonus PS in step S2 is:
[0015] PS=∑ i PF i ×FW i
[0016] In the formula, PF i is the performance of the i-th node, FW i The performance weight of the i-th node.
[0017] Furthermore, the penalty factor PF in step S3 is adjusted according to the number of times L that the node becomes a leader and a preset growth rate α, and its calculation formula is:
[0018] PF = α × L.
[0019] Furthermore, the calculation formula of the node score f(Node) in step S4 is:
[0020] f(Node)=BW+EMAP S -EMAP F
[0021] Where BW is the static base weight, EMAP S and EMAP F They are the EMA values of performance bonus and penalty factor respectively;
[0022] EMAPS ,t =α ps *PS t +(1-α ps )*EMAP S,t-1
[0023] EMAP F,t =α pf *PF t +(1-αpf )*EMAP F,t-1
[0024] In the formula, α is the smoothing coefficient.
[0025] The present invention also provides a device for calculating the asymmetric weight of nodes in the Raft leader election mechanism according to the above method, comprising:
[0026] The calculation module is used to calculate the basic weights of different nodes;
[0027] A dynamic calculation module is used to dynamically calculate the performance bonus PS according to the node performance factor and the node performance weight;
[0028] Add a module to dynamically increase the penalty factor PF according to the frequency of nodes becoming leaders. This penalty factor is used to punish nodes that become leaders more frequently than the set frequency.
[0029] The smoothing processing module is used to smooth the performance bonus PS and the penalty factor PF by applying an exponential moving average to obtain a node score.
[0030] Compared with the prior art, the advantages of the present invention are as follows: the present invention aims at the problems that are easily affected by multiple parties during the leader election, such as election storms, difficulty in balancing the election timeout, performance waste, etc., by giving nodes with high performance or low network latency a higher weight, thereby increasing the probability of these nodes becoming leaders, making the leader more likely to be stable and efficient, thereby reducing election storms caused by frequent elections. At the same time, the penalty factor and smoothing processing are introduced to avoid the insecurity caused by excessive concentration of leaders. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0032] Figure 1 It is the weight factor graph in the present invention.
[0033] Figure 2 It is a flow chart of a method for calculating asymmetric weights of nodes in a Raft leader election mechanism according to the present invention.
[0034] Figure 3 It is the weight influence diagram in the present invention.
[0035] Figure 4It is a framework diagram of a device for calculating asymmetric weights of nodes of a Raft leader election mechanism according to the present invention. DETAILED DESCRIPTION
[0036] The preferred embodiments of the present invention are described in detail below in conjunction with the accompanying drawings so that the advantages and features of the present invention can be more easily understood by those skilled in the art, thereby making a clearer and more definite definition of the protection scope of the present invention.
[0037] See also Figure 1-Figure 3 As shown, this embodiment discloses a method for calculating asymmetric weights of nodes in a Raft leader election mechanism, including the following steps:
[0038] Step S1: Calculate the basic weights of different nodes.
[0039] Specifically, the basic weight is the initial weight of the node, which is usually used to set the initial weight or priority when the system is initialized. The initial weight is allocated according to the inherent characteristics of the node (such as node level, geographical location, etc.).
[0040] Mathematically, the base weight is represented as a constant value BW, and remains unchanged throughout the life cycle of the node. Its mathematical representation is BW=constant.
[0041] Step S2: dynamically calculate the performance bonus PS according to the node performance factor and the node performance weight.
[0042] Specifically, the performance bonus is composed of multiple node performance factors (PF), each node performance factor PF i There is a corresponding weight FW i The calculation formula of performance bonus is the weighted sum of all node performance factors and their weights. Node performance factors include response time (RT), processing capability (PC), network latency (NL), available bandwidth (AB), etc.
[0043] When calculating the performance weights (Factor Weights, FW), the weight FW of each performance factor i The importance is determined by statistical analysis. Its mathematical expression is PS = ∑ i PF i ×FW i , PF i is the performance of the i-th node, FW iThe performance weight of the i-th node. For example, if there are three performance factors: response time RT, processing power PC, and network delay NL, and their weights are FW1, FW2, and FW3 respectively, then the performance addition can be expressed as: PS = (RT × FW1) + (PC × FW2) + (NL × FW3).
[0044] Step S3: dynamically increase the penalty factor PF according to the frequency at which the node becomes a leader. The penalty factor is used to punish nodes that become leaders more frequently than a set frequency, so as to prevent excessive load on a single node and centralization of the system.
[0045] Specifically, the penalty factor is dynamically adjusted according to the frequency at which the node becomes a leader. The higher the frequency, the larger the penalty factor. At the same time, by using dynamic adjustment, the value of the penalty factor PF can be adjusted according to the number of times the node becomes a leader L and a preset growth rate α. Assuming that the penalty factor increases by α each time the node becomes a leader, the penalty factor calculation formula is: PF = α × L, where α is the growth rate of the penalty factor, which is usually a positive number less than 1. L is the number of times the node becomes a leader.
[0046] Step S4: Smoothing the performance bonus PS and the penalty factor PF by applying exponential moving average to obtain the score of the node.
[0047] Specifically, the calculation formula for the node score f(Node) is:
[0048] f(Node)=BW+EMAP S -EMAP F
[0049] Where BW is the static base weight, EMAP S and EMAP F They are the EMA values of performance bonus and penalty factor respectively;
[0050] EMAPS ,t =α ps *PS t +(1-α ps )*EMAP S,t-1
[0051] EMAP F,t =α pf *PF t +(1-α pf )*EMAP F,t-1
[0052] In the formula, α is the smoothing coefficient.
[0053] The present invention will be further described below by way of examples.
[0054] In this embodiment, three nodes are set, and the basic weight constant of node A is set to BW according to its geographical location, node level and other characteristics. A =10, BW of Node B B =12, BW of node C C =8.
[0055] The three performance factors in this embodiment are response time RT, processing capacity PC and network delay NL, and their weights are FW1=0.5, FW2=0.3 and FW3=0.2 respectively. The performance data collected at the current time t is as follows: RT in node A A =2, PC A =5, NL A =1, RT in Node B B =3, PC B =4, NL B =2, and node C: RT C = 1, PC C =6, NL C =1.5, so the final performance bonus PS calculation formula is:
[0056] PS=(R T ×FW1)+(PC×FW2)+(NL×FW3)
[0057] In this embodiment, a penalty factor PF is set to punish nodes that frequently become leaders. The growth rate α of the penalty factor is set to 0.1, and the number of times each node becomes a leader at the current moment is as follows:
[0058] Node A:L A =3
[0059] Node B:L B =1
[0060] Node C:L C =2
[0061] According to the penalty factor calculation formula PF = α × L, the penalty factor of each node is calculated as PF A =0.3, PF B =0.1, PF C =0.2.
[0062] In this embodiment, the smoothing coefficients αps=0.2 and αpf=0.3 are set.
[0063] Set the EMA value of the previous moment t-1 as follows:
[0064] Node A:EMA PS,At-1 =2.5, EMA PF,At-1 =0.25
[0065] Node B:EMA PS,Bt-1 =3.0,EMA PF,Bt-1 =0.15
[0066] Node C:EMAPS ,Ct-1 =2.7EMAPF,C t-1=0.2
[0067] Use the EMA formula to update the EMA value at the current time t:
[0068] EMAPS ,t =α ps *PS t +(1-α ps )*EMAP S,t-1
[0069] EMAPF ,t =α pf *PF t +(1-α pf )*EMAP F,t-1
[0070] Finally, calculate the EMA value of each node:
[0071] Node A:EMA PS,At =0.254, EMA PF,At =0.265,
[0072] Node B:EMA PS,Bt =3.02, EMA PF,Bt =0.135,
[0073] Node C:EMA PS,Ct =2.68,EMA PF,Ct =0.2,
[0074] The final node score function f can be expressed as: f(Node) = BW + EMAP S -EMAP F , calculate the final score of each node: f(A)=12.275, f(B)=12.275, f(B)=12.275.
[0075] See also Figure 4As shown, the present invention also provides a device for calculating the asymmetric weight of nodes in the Raft leader election mechanism according to the above-mentioned method, including: a calculation module 100, used to calculate the basic weights of different nodes; a dynamic calculation module 200, used to dynamically calculate the performance bonus PS according to the node performance factor and the node performance weight; an increase module 300, used to dynamically increase the penalty factor PF according to the frequency of the node becoming a leader, and the penalty factor is used to punish the node that becomes the leader with a frequency exceeding the set frequency; a smoothing processing module 400, used to smooth the performance bonus PS and the penalty factor PF by applying an exponential moving average to obtain the node score.
[0076] The present invention aims at the problems that are easily affected by multiple parties during leader election, such as election storms, difficulty in balancing election timeouts, performance waste, etc. By giving nodes with high performance or low network latency higher weights, the probability of these nodes becoming leaders is increased, making leaders more likely to be stable and efficient, thereby reducing election storms caused by frequent elections. At the same time, the penalty factor and smoothing processing are introduced to avoid the insecurity caused by excessive concentration of leaders.
[0077] Although the embodiments of the present invention are described in conjunction with the accompanying drawings, the patent owner may make various variations or modifications within the scope of the appended claims. As long as they do not exceed the protection scope described in the claims of the present invention, they should be within the protection scope of the present invention.
Claims
1. A method for calculating asymmetric weights of nodes in a Raft leader election mechanism, characterized in that: The following steps are involved: S1. Calculate the basic weights of different nodes; S2. Dynamically calculate the performance bonus PS based on the node performance factor and the node performance weight; S3. Dynamically increase the penalty factor PF according to the frequency of the node becoming a leader. This penalty factor is used to punish nodes that become leaders more frequently than the set frequency. S4. Smoothing the performance bonus PS and the penalty factor PF by applying exponential moving average to obtain the node score.
2. The method for calculating asymmetric weights of nodes in the Raft leader election mechanism according to claim 1, characterized in that: The basic weight in step S1 is the initial weight of the node. The initial weight or priority is set during initialization, and the initial weight is allocated according to the inherent characteristics of the node.
3. The method for calculating asymmetric weights of nodes in the Raft leader election mechanism according to claim 1, characterized in that: The node performance in step S2 includes response time, processing capacity, network delay, and available bandwidth.
4. The method for calculating asymmetric weights of nodes in the Raft leader election mechanism according to claim 1, characterized in that: The calculation formula of the performance bonus PS in step S2 is: PS=∑ i PF i ×FW i In the formula, PF i is the performance of the i-th node, FW i The performance weight of the i-th node.
5. The method for calculating asymmetric weights of nodes in the Raft leader election mechanism according to claim 1, characterized in that: The penalty factor PF in step S3 is adjusted according to the number of times L that the node becomes a leader and a preset growth rate α, and its calculation formula is: PF = α × L.
6. The method for calculating asymmetric weights of nodes in the Raft leader election mechanism according to claim 1, characterized in that: The calculation formula of the node score f(Node) in step S4 is: f(Node)=BW+EMAP S -EMAP F Where BW is the static basic weight, EMAP S and EMAP F They are the EMA values of the performance bonus and penalty factor, respectively; EMAPS ,t =α ps *PS t +(1-α ps )*EMAP S,t-1 EMAP F,t =a pf *PF t +(1-α pf )*EMAP F,t-1 In the formula, α is the smoothing coefficient.
7. A device for calculating the asymmetric weight of nodes in the Raft leader election mechanism according to any one of claims 1 to 6, characterized in that: include: The calculation module is used to calculate the basic weights of different nodes; A dynamic calculation module is used to dynamically calculate the performance bonus PS according to the node performance factor and the node performance weight; Add a module to dynamically increase the penalty factor PF according to the frequency of nodes becoming leaders. This penalty factor is used to punish nodes that become leaders more frequently than the set frequency. The smoothing processing module is used to smooth the performance bonus PS and the penalty factor PF by applying an exponential moving average to obtain a node score.
