Distributed election methods, devices, systems and media

CN113760468BActive Publication Date: 2026-08-14BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 3 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-01-19
Publication Date
2026-08-14

AI Technical Summary

Technical Problem

[0003]在实现本发明过程中,发明人发现现有分布式选举方法存在例如大量的RPC(远程过程调用)通信、选票被瓜分导致选举失败或者选举过程十分复杂等诸多缺陷

Benefits of technology

[0028]上述发明中的一个实施例具有如下优点或有益效果:通过在集群中设置子节点,在主节点宕机后,使集群快速选举出主节点,从而能够克服现有的选举方法中的大量的RPC通信以及由于选票被瓜分而导致的选举失败这样的缺陷,从而能够提高分布式系统的工作效率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113760468B_ABST
    Figure CN113760468B_ABST
Patent Text Reader

Abstract

This invention discloses a distributed election method, apparatus, system, and medium. The method includes: when election triggering conditions are met, the only existing child node in the cluster receives an election message, which is sent from a slave node to the child node to elect the child node as the new master node; determining whether the number of election messages received by the child node exceeds a predetermined threshold; when the number of election messages exceeds the predetermined threshold, the child node switches to become the new master node; and the new master node selects a new child node from the slave nodes. This invention enables the cluster to quickly elect a master node by establishing child node roles, thereby avoiding a large amount of RPC communication and avoiding election failures due to vote splitting, thus improving the working efficiency of the distributed system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a distributed election method, apparatus, system, and medium. Background Technology

[0002] A core problem addressed by distributed systems is improving the system's concurrent access capabilities. When a system experiences a large number of requests, adding more nodes increases the overall service capacity. However, data consistency across all nodes is crucial to ensure the system provides consistent data to the outside world. To guarantee data consistency across nodes, one solution is to have one node receive all write requests and then synchronize them to the other nodes—a master-slave model. This shifts the focus of this solution to how to elect a master node.

[0003] In the process of developing this invention, the inventors discovered that existing distributed election methods have many drawbacks, such as a large amount of RPC (Remote Procedure Call) communication, votes being divided up leading to election failure, or the election process being very complex. Summary of the Invention

[0004] In view of this, embodiments of the present invention provide a distributed election method, apparatus, and medium that can enable a cluster to quickly elect a master node by establishing child node roles, thereby avoiding a large amount of RPC communication and avoiding election failure due to vote splitting, thus improving the working efficiency of the distributed system.

[0005] To achieve the above objectives, according to one aspect of the present invention, a distributed election method is provided, comprising:

[0006] When the election conditions are met, the only existing child node in the cluster receives an election message, which is sent from the slave node to the child node to elect the child node as the new master node.

[0007] Determine whether the number of election messages received by the child node exceeds a predetermined threshold;

[0008] When the number of election messages exceeds the predetermined threshold, the child node switches to become the new master node; and

[0009] The new master node selects a new child node from the slave nodes.

[0010] Furthermore, after becoming the new master node, the child node broadcasts to the cluster and suggests heartbeat communication with other nodes in the cluster.

[0011] Furthermore, the new master node randomly selects the new child node from the slave nodes.

[0012] Furthermore, the election triggering conditions include the failure of the master node in the cluster or the loss of heartbeat communication between the master node and more than half of the slave nodes and child nodes in the cluster.

[0013] Furthermore, when both the master node and the child node fail simultaneously, or when the number of election messages does not exceed half the number of slave nodes in the cluster, all slave nodes in the cluster elect a new master node using the Raft algorithm.

[0014] Furthermore, after a new master node is elected using the Raft algorithm, the new master node randomly selects new child nodes.

[0015] Furthermore, the predetermined threshold is half the number of slave nodes in the cluster.

[0016] According to another aspect of the present invention, a distributed election device is provided, comprising:

[0017] A receiving unit is used to receive an election message when the election triggering conditions are met. The election message is sent from the slave node to the only existing child node in the cluster to elect the child node as the new master node.

[0018] A determining unit is configured to determine whether the number of election messages received by the child node exceeds a predetermined threshold.

[0019] A switching unit, configured to switch the child node to a new master node when the number of election messages exceeds a predetermined threshold; and

[0020] The first selection unit is used to select a new child node from the slave node.

[0021] Furthermore, the distributed election device also includes:

[0022] A broadcast unit is used to broadcast to the cluster after the child node becomes the new master node.

[0023] According to another aspect of the present invention, a distributed election system is provided, comprising:

[0024] One or more processors;

[0025] Storage device for storing one or more programs.

[0026] When the one or more programs are executed by the one or more processors, the one or more processors perform the method described in one aspect above.

[0027] According to another aspect of the present invention, a computer-readable medium is provided having a computer program stored thereon, characterized in that the program, when executed by a processor, implements the method described in the preceding aspect.

[0028] One embodiment of the above invention has the following advantages or beneficial effects: by setting child nodes in the cluster, the cluster can quickly elect a new master node after the master node fails, thereby overcoming the shortcomings of existing election methods such as a large amount of RPC communication and election failure due to the splitting of votes, thus improving the working efficiency of the distributed system.

[0029] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description

[0030] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein:

[0031] Figure 1 This is a schematic diagram of the main process of the distributed election method according to an embodiment of the present invention;

[0032] Figure 2 This is a schematic diagram of the election process of each node involved in the distributed election method according to the present invention;

[0033] Figure 3 This is a schematic diagram of the main modules of a distributed election device according to an embodiment of the present invention;

[0034] Figure 4 This is a schematic diagram of the structure of a computer system suitable for implementing terminal devices or servers of the present invention.

[0035] Figure 5 This is a schematic diagram used to illustrate the election process of the ZAB algorithm. Detailed Implementation

[0036] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.

[0037] Before providing a detailed description of the present invention, a brief description of several improved distributed election methods will be given to help understand the technical problems to be solved and the technical effects that the present invention can achieve.

[0038] Among the existing distributed election methods, Raft and ZAB (Zookeeper Atomic Broadcast) are the most widely used. Zookeeper uses the ZAB algorithm for election, while the Raft algorithm can be said to have become the preferred algorithm for current distributed systems, such as Consul and Etcd.

[0039] The Raft algorithm is relatively simple to implement. Voting is based on a "first-come, first-served" principle; the first node to receive a vote request casts its vote, and the node that receives more than half the votes is promoted to master node. The drawback of the Raft algorithm is that votes may be split, resulting in no node receiving more than half the votes, leading to election failure and the need for a second round of elections.

[0040] The ZAB algorithm's process of electing a master node is relatively complex. It requires comparing candidates and ultimately electing the candidate with the most complete data as the leader. In addition, the election process involves a large amount of PRC communication. For details, refer to... Figure 5 As shown, the voting information is 2, 1, 10, where B represents the node sequence number, term of office, data transaction ID (reflecting the data update status), and voting object. After the master node A fails, nodes B and C each cast their own vote and then broadcast it to other followers. Node C receives two votes, and based on the principle of the larger term of office and transaction ID, the vote is ultimately cast for C, meaning node C becomes the new master node.

[0041] As mentioned above, the Raft algorithm suffers from issues such as vote splitting and excessive RPC communication, while the ZAB algorithm's election process is time-consuming and complex. This invention addresses these shortcomings by creating child nodes within the cluster, thereby avoiding the aforementioned problems under normal circumstances and improving the efficiency of distributed systems.

[0042] The distributed election method of the present invention will be described in detail below with reference to the accompanying drawings.

[0043] Figure 1 This is the main flow of the distributed election method according to an embodiment of the present invention, such as... Figure 1 As shown, the distributed election method according to the present invention includes a receiving step S101, a determining step S102, a switching step S103, a switching step S104, a selecting step S105, and a broadcasting step S106.

[0044] Before describing each of the above steps in detail, the identities of each node in the cluster involved in the invention will first be explained.

[0045] Master node: There is only one master node in the cluster, and all write requests are executed on the master node.

[0046] Child node: There is only one child node in the cluster. When the master node fails, the child node can be promoted to master node.

[0047] Slave nodes: All nodes in the cluster except for the master node and child nodes are slave nodes. Slave nodes only accept read requests from clients, and write requests need to be forwarded to the master node.

[0048] Having clarified the identities of each of the aforementioned nodes, the following text will refer to... Figure 2 The above-described steps of the distributed election method of the present invention are described in detail.

[0049] Step S101, receiving steps

[0050] When the election conditions are met, the only existing child node in the cluster receives an election message, which is sent from the slave node to the child node to elect the child node as the new master node.

[0051] Specifically, the election condition is met when the master node in the cluster fails or the master node loses heartbeat communication with more than half of the other nodes (slave nodes and child nodes) in the cluster. At this time, slave nodes communicate with child nodes to send election messages, thereby electing the child node as the master node, and the child node receives the election messages from the slave nodes.

[0052] Step S102, determine the steps

[0053] Determine whether the number of election messages received by the child node exceeds a predetermined threshold, such as half the number of slave nodes in the cluster.

[0054] Specifically, the child node counts the election messages received from the slave nodes and determines that the number of election messages received recommending itself as the master node is more than half the number of slave nodes in the cluster.

[0055] Step S103, switch steps

[0056] When the number of received election messages exceeds a predetermined threshold, such as half the number of slave nodes in the cluster, the child node is switched to become the new master node.

[0057] Step S104, select step

[0058] The new master node selects a new child node from the slave nodes.

[0059] Specifically, a new master node can select new child nodes from the slave nodes based on preset conditions. For example, a new master node can randomly select new child nodes from the slave nodes.

[0060] Step S105, Broadcast Step

[0061] After becoming the new master node, the child node broadcasts to the cluster (a broadcast of successful master node switch) to notify all nodes in the cluster that it has become the new master node and establishes heartbeat communication with other nodes.

[0062] It should be noted that the following anomalies exist:

[0063] If a child node also fails at the same time as the master node, all remaining slave nodes use the Raft algorithm to elect a new master node. The elected master node then executes steps S104 and S105 to select new child nodes and send a broadcast.

[0064] If the number of election messages received by the child nodes after several rounds of voting does not exceed half the number of slave nodes, then all remaining slave nodes use the Raft algorithm to elect a new master node. The newly elected master node then executes steps S104 and S105 to select new child nodes and send a broadcast.

[0065] When master node A crashes or communicates with more than half of its child and slave nodes via heartbeat, the election conditions are met. At this time, slave nodes C1, C2, C3...Cn send election messages to child node B to elect child node B. When child node B receives election messages from more than half of its slave nodes C1, C2, C3...Cn, child node B becomes the new master node B', and a new child node C' is randomly selected from the slave nodes C1, C2, C3...Cn.

[0066] Therefore, the distributed election method according to the present invention can enable the cluster to quickly elect a master node by establishing child node roles, thereby avoiding a large amount of RPC communication and avoiding election failure due to the splitting of votes, thus improving the working efficiency of the distributed system.

[0067] According to another aspect of the present invention, a distributed device is provided. Reference will now be made to... Figure 3 A distributed election device 200 according to an embodiment of the present invention is described. The distributed election device 200 according to an embodiment of the present invention includes: a receiving unit 201, a determining unit 202, a switching unit 203, a selecting unit 204, and a broadcasting unit 205. The above-described units will be described in detail below.

[0068] Receiving unit 201

[0069] When the conditions for triggering an election are met, the receiving unit 201 receives an election message, which is sent from the slave node to the child node to elect the child node as the new master node.

[0070] Specifically, when the master node in the cluster crashes or the master node loses heartbeat communication with more than half of the other nodes (slave nodes and child nodes) in the cluster, the election conditions are met. At this time, the slave nodes communicate with the child nodes to send election messages, thereby electing the child nodes as the master node, and the receiving unit 201 receives the election messages from the slave nodes.

[0071] Determine Unit 202

[0072] The determining unit 202 is used to determine whether the number of election messages received by the child node is within a predetermined threshold, such as exceeding half the number of slave nodes in the cluster.

[0073] Specifically, the determining unit 202 counts the election messages received from the slave nodes and determines whether the number of election messages received recommending itself as the master node exceeds half the number of slave nodes in the cluster.

[0074] Switching unit 203

[0075] When the number of received election messages exceeds a predetermined threshold, for example, half the number of slave nodes in the cluster, the switching unit 203 switches the child node to the new master node.

[0076] Select unit 204

[0077] Select unit 204 selects a new child node from the slave nodes.

[0078] Specifically, the selection unit 204 can select a new child node from the slave nodes according to preset conditions. Alternatively, the selection unit 204 can randomly select a new child node from the slave nodes.

[0079] Broadcast Unit 205

[0080] After the child node becomes the new master node, the broadcast unit 205 broadcasts to the cluster (broadcast of successful master node switch) to notify all nodes in the cluster that the original child node has become the new master node and establishes heartbeat communication with other nodes.

[0081] It should be noted that if the child nodes also fail at the same time as the master node, or if the number of election messages received by the child nodes does not exceed half the number of slave nodes, then all remaining slave nodes will use the Raft algorithm to elect a new master node. The newly elected master node will then use selection units and broadcast units to select new child nodes and send broadcasts.

[0082] The above describes a distributed election device according to an embodiment of the present invention. The distributed election device according to an embodiment of the present invention can enable the cluster to quickly elect a master node by establishing child node roles, thereby avoiding a large amount of RPC communication and avoiding election failure due to the splitting of votes, thus improving the working efficiency of the distributed system.

[0083] The following is for reference. Figure 4 It shows a schematic diagram of the structure of a computer system 400 suitable for implementing a terminal device of the present invention. Figure 4 The terminal device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of the present invention.

[0084] like Figure 4 As shown, the computer system 400 includes a central processing unit (CPU) 401, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 402 or programs loaded from storage section 408 into random access memory (RAM) 403. The RAM 403 also stores various programs and data required for the operation of the system 400. The CPU 401, ROM 402, and RAM 403 are interconnected via a bus 404. An input / output (I / O) interface 405 is also connected to the bus 404.

[0085] The following components are connected to I / O interface 405: an input section 406 including a keyboard, mouse, etc.; an output section 407 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 408 including a hard disk, etc.; and a communication section 409 including a network interface card such as a LAN card, modem, etc. The communication section 409 performs communication processing via a network such as the Internet. A drive 410 is also connected to I / O interface 405 as needed. A removable medium 411, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 410 as needed so that computer programs read from it can be installed into storage section 408 as needed.

[0086] In particular, according to the embodiments disclosed in this invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 409, and / or installed from removable medium 411. When the computer program is executed by central processing unit (CPU) 401, it performs the functions defined above in the system of this invention.

[0087] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media can also be any computer-readable medium other than computer-readable storage media, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.

[0088] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0089] The units described in the embodiments of the present invention can be implemented in software or hardware. The described units can also be housed in a processor; for example, a processor can be described as including a receiving unit, a determining unit, a switching unit, a selecting unit, and a broadcasting unit. The names of these units do not necessarily limit the specific unit; for example, the determining unit can also be described as "a unit that determines whether the number of election messages exceeds half."

[0090] In another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs, which, when executed by the device, cause the device to include:

[0091] When the election conditions are met, the only existing child node in the cluster receives an election message, which is sent from the slave node to the child node to elect the child node as the new master node.

[0092] Determine whether the number of election messages received by the child node exceeds a predetermined threshold;

[0093] When the number of election messages exceeds the predetermined threshold, the child node switches to become the new master node; and

[0094] The new master node selects a new child node from the slave nodes.

[0095] According to the technical solution of the present invention, by establishing child node roles, the cluster can quickly elect a master node, thereby avoiding a large amount of RPC communication and avoiding election failure due to the splitting of votes, thus improving the working efficiency of the distributed system.

[0096] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.

Claims

1. A distributed election method, characterized in that, include: When the election conditions are met, the only existing child node in the cluster receives an election message. The election message is sent from the slave node to the child node to elect the child node as the new master node. The election conditions include the master node in the cluster losing heartbeat communication with more than half of the slave nodes and child nodes in the cluster. Determine whether the number of election messages received by the child node exceeds a predetermined threshold; When the number of election messages exceeds the predetermined threshold, the child node is switched to a new master node; If, after several rounds of voting, the number of election messages received by the child nodes does not exceed a preset threshold, a new master node is elected from all remaining slave nodes in the cluster; and The new master node randomly selects a new child node from the slave nodes according to preset conditions.

2. The method according to claim 1, characterized in that, Also includes: After becoming the new master node, the child node broadcasts to the cluster and establishes heartbeat communication with other nodes in the cluster.

3. The method according to claim 1, characterized in that, in, The election triggering condition includes the failure of the master node in the cluster.

4. The method according to claim 3, characterized in that, in, When both the master node and the child node fail simultaneously, all slave nodes in the cluster elect a new master node using the Raft algorithm.

5. The method according to claim 4, characterized in that, in, After a new master node is elected using the Raft algorithm, the new master node randomly selects new child nodes.

6. The method according to claim 5, characterized in that, in, The predetermined threshold is half the number of slave nodes in the cluster.

7. A distributed election device, characterized in that, include: A receiving unit is configured to receive an election message when the election triggering conditions are met. The election message is sent from a slave node to the only existing child node in the cluster to elect the child node as the new master node. The election triggering conditions include the master node in the cluster losing heartbeat communication with more than half of the slave nodes and child nodes in the cluster. A determining unit is configured to determine whether the number of election messages received by the child node exceeds a predetermined threshold. A switching unit is configured to switch the child node to a new master node when the number of election messages exceeds a predetermined threshold; and to elect a new master node from all remaining slave nodes in the cluster when the number of election messages received by the child node after several rounds of voting has not exceeded the predetermined threshold. The selection unit is used to randomly select a new child node from the slave nodes according to preset conditions by the new master node.

8. The apparatus according to claim 7, characterized in that, Also includes: A broadcast unit is used to broadcast to the cluster after the child node becomes the new master node.

9. A distributed election system, characterized in that, include: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-6.

10. A computer-readable medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-6.

Citation Information

Patent Citations

  • Improved distributed consistency algorithm

    CN106559263A

  • A blockchain leader election method and a blockchain leader election device

    CN109728941A

  • Method and apparatus for configuring nodes as masters or slaves

    US7587465B1