Server cluster master node election method, device, server node and storage medium

By exchanging election requests among nodes in the server cluster and determining the master server node based on the sorting of vote data, the problem of low efficiency in master node election caused by dependence on third-party components is solved, and a more efficient master node election is achieved.

CN119363545BActive Publication Date: 2025-10-28MAIPU COMM TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411484129.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-10-23
Publication Date
2025-10-28
Estimated Expiration
2044-10-23

AI Technical Summary

Technical Problem

The existing master node election method relies on third-party components, resulting in low efficiency in master node election. This is especially true when the third-party components encounter problems or start up slowly, which affects business efficiency.

Method used

By exchanging election requests among nodes in the server cluster, sorting the identification information according to the ballot data, and determining the server node with the target identification information as the master server node, the system avoids dependence on third-party components and uses random numbers and preset rules to elect the master.

Benefits of technology

It improves the efficiency of master node election, reduces dependence on third-party components, reduces the difficulty and burden of software management, is suitable for single physical node or virtualization environments, and avoids master election problems caused by the same startup time.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119363545B_ABST
    Figure CN119363545B_ABST
Patent Text Reader

Abstract

The server cluster master election method, device, server node, and storage medium provided by an embodiment of the present invention relate to the field of distributed cluster technology. The method includes: generating a first election request based on the identification information of the server node, obtaining a second election request generated by the remaining server nodes in the server cluster, each second election request including the ballot data and identification information of the corresponding server node, sorting all the identification information based on all the ballot data to obtain a sorting result, determining the target identification information based on the sorting result and a preset rule, and determining the server node corresponding to the target identification information as the master server node selected by the server node. By selecting a master based on the ballot data included in the election request generated by each node, the problem of low master election efficiency caused by relying on a third-party component for master election is avoided when a problem occurs in the third-party component, thereby improving the master election efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of distributed cluster technology, and more specifically, to a method, apparatus, server node, and storage medium for electing a master node in a server cluster. Background Art

[0002] In the field of data communication, with the development and maturation of cluster technology and the exponential increase in the amount of data to be processed, more and more software systems are using cluster technology to support their business. In cluster systems, some critical services are multiple instances, which can achieve load balancing and synchronous data processing, improving the overall system response speed. Businesses using cluster systems can be divided into two types: stateless and stateful. Stateless businesses can be executed indiscriminately on all nodes, but stateful businesses require certain functions to be executed on a fixed node in the cluster to perform aggregated tasks. Most current businesses cannot truly be stateless and are still stateful, requiring a master server node in the cluster to complete some unified tasks.

[0003] The existing master node election method relies on third-party components. The identification information of the node is created in the specified file directory of the third-party component. The first node created becomes the master server node, and the node created later becomes the slave server node. If the third-party component has problems or starts slowly, it will affect the efficiency of the master node election of the business. Summary of the Invention

[0004] The purpose of this invention is to provide a method, apparatus, server node, and storage medium for electing a master in a server cluster, so as to solve the problem of slow master election efficiency.

[0005] In a first aspect, the present invention provides a method for electing a master node in a server cluster, applicable to any one server node in the server cluster. The method includes:

[0006] A first election request is generated based on the identification information of this server node, and the first election request is sent to the remaining server nodes in the server cluster.

[0007] Obtain the second election requests generated by the remaining server nodes in the server cluster; each second election request includes the vote data and identification information of the corresponding server node;

[0008] Sort all the identification information according to all the ballot data to obtain the sorting result;

[0009] The target identification information is determined based on the sorting results and preset rules, and the server node corresponding to the target identification information is determined as the main server node selected by this server node.

[0010] In an optional implementation, the identification information of the local server node generates a first election request, including:

[0011] Get the current leader election round of this server node and generate a random number of a preset number of bits;

[0012] Generate ballot data based on the random number and the identification information of this server node;

[0013] The first election request is generated based on the current election round, the identifier information of this server node, and the vote data of this server node.

[0014] In an optional implementation, generating the vote data for the server node based on the random number and the identifier information of the server node includes:

[0015] The random number is added to the identifier information of the local server node to generate vote data.

[0016] In an optional implementation, sorting all the identification information based on all the ballot data to obtain a sorting result includes:

[0017] Reverse the order of each of the ballot data;

[0018] Convert each of the vote data after reverse processing into a preset format;

[0019] The identification information is sorted according to all the ballot data converted to a preset format to obtain a sorting result.

[0020] In an optional implementation, before reversing the order of each ballot data, the method further includes:

[0021] Determine whether the length of each ballot data exceeds a preset character length;

[0022] If the length of the ballot data exceeds the preset character length, then data of the preset character length is selected from the corresponding ballot data, and the selected data is determined as the new ballot data.

[0023] In an optional implementation, after determining the server node corresponding to the target identification information as the primary server node selected by this server node, the method further includes:

[0024] Receive selection result confirmation requests sent by the other server nodes; each selection result confirmation request includes the identification information of the main server node selected by the corresponding server node;

[0025] If the identification information included in each selection result confirmation request is the same as the target identification information, then the selection of the main body is confirmed to be successful.

[0026] In an optional implementation, the method further includes;

[0027] Periodically send keep-alive information to the remaining server nodes;

[0028] Receive response information sent by the other servers based on the keep-alive information;

[0029] Based on the response information, check if there are any downtime server nodes;

[0030] If a server node is down, determine whether the downed server node is the primary server node selected by this server node.

[0031] If the server node that crashed is the master server node selected by this server node, then the server node corresponding to the next identifier information of the target identifier information in the sorting result will be determined as the newly selected master server node of this server node.

[0032] Secondly, the present invention provides a server cluster master node election device, applicable to any server node in a server cluster, the device comprising:

[0033] The election request generation module is used to generate a first election request based on the identification information of this server node, and send the first election request to the remaining server nodes in the server cluster.

[0034] The election request acquisition module is used to acquire the second election requests generated by the remaining server nodes in the server cluster; each second election request includes the vote data and identification information of the corresponding server node;

[0035] The sorting module is used to sort all the identification information according to all the ballot data to obtain a sorting result;

[0036] The master selection module is used to determine the target identification information based on the sorting results and preset rules, and to determine the server node corresponding to the target identification information as the master server node selected by this server node.

[0037] Thirdly, the present invention provides a server node including a processor and a memory, wherein the memory stores a computer program executable by the processor, and the processor-executable computer program implements the server cluster master node election method described in any of the foregoing embodiments.

[0038] Fourthly, the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the server cluster master node election method as described in any of the foregoing embodiments.

[0039] The server cluster master node election method, apparatus, server node, and storage medium provided in this invention are applied to any server node in a server cluster. The method includes: generating a first election request based on the server node's identification information and sending the first election request to the remaining server nodes in the server cluster; obtaining second election requests generated by the remaining server nodes in the server cluster, each second election request including vote data and identification information of the corresponding server node; sorting all identification information based on all vote data to obtain a sorting result; determining target identification information based on the sorting result and preset rules; and determining the server node corresponding to the target identification information as the master server node selected by this server node. By selecting the master node based on the vote data included in the election requests generated by each node, the problem of low efficiency in master node selection due to reliance on third-party components and issues with these components is avoided, thereby improving the efficiency of master node selection. Attached Figure Description

[0040] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as a limitation on the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0041] Figure 1 This diagram illustrates an application environment of the server cluster master node election method provided in an embodiment of the present invention.

[0042] Figure 2 This invention illustrates a flowchart of a server cluster master node election generation method provided in an embodiment of the present invention.

[0043] Figure 3 This illustration shows another flowchart of the server cluster master node election generation method provided in an embodiment of the present invention;

[0044] Figure 4This illustration shows another flowchart of the server cluster master node election generation method provided in an embodiment of the present invention;

[0045] Figure 5 This paper illustrates another flowchart of the server cluster master node election generation method provided in an embodiment of the present invention.

[0046] Figure 6 This illustration shows another flowchart of the server cluster master node election generation method provided in an embodiment of the present invention;

[0047] Figure 7 A block diagram of a server node device provided in an embodiment of the present invention is shown;

[0048] Figure 8 A block diagram of a server node provided in an embodiment of the present invention is shown.

[0049] Icons: 100 - Server node; 110 - Memory; 120 - Processor; 130 - Communication module; 200 - Server cluster master node election device; 210 - Election request generation module; 220 - Election request acquisition module; 230 - Sorting module; 240 - Master election module. Detailed Implementation

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

[0051] Therefore, the following detailed description of the embodiments of the invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the invention without inventive effort are within the scope of protection of the invention.

[0052] It should be noted that relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes the element.

[0053] Please see Figure 1 , Figure 1 This diagram illustrates an application environment of the server cluster master node election method provided in an embodiment of the present invention. Multiple server nodes 100 are located in a server cluster, and all server nodes 100 are communicatively connected to each other to enable data interaction. As one method, the multiple server nodes 100 can connect via WIFI, ZigBee, or 2G / 3G / 4G / 5G, and this is not limited to any particular method.

[0054] Please see Figure 2 , Figure 2 This diagram illustrates a flowchart of a server cluster master node election method provided by an embodiment of the present invention. This server cluster master node election method can be applied to any server node in a server cluster. The specific process of this embodiment is described below using any server node in the server cluster as an example. The following section focuses on... Figure 2 The process shown will be explained in detail. This method for electing the master node of a server cluster can specifically include the following steps:

[0055] Step 110: Generate a first election request based on the identification information of this server node, and send the first election request to the remaining server nodes in the server cluster.

[0056] The identification information may include IP information and HOST information, which can uniquely identify the server node.

[0057] It is understandable that this server node will send the first election request it generates to the other server nodes in the server cluster, and the other server nodes in the server cluster will all receive the first election request generated by this server node.

[0058] Step 120: Obtain the second election requests generated by the remaining server nodes in the server cluster.

[0059] Each second election request includes the vote data and identification information of the corresponding server node.

[0060] Step 130: Sort all identification information according to all ballot data to obtain the sorting result.

[0061] It should be noted that the first election request includes the vote data of this server node, so it is sorted according to the total vote data of all server nodes in the server cluster being equal to all the identification information.

[0062] Step 140: Determine the target identification information based on the sorting results and preset rules, and determine the server node corresponding to the target identification information as the main server node selected by this server node.

[0063] In some implementations, the server node corresponding to the target identification information can be the local server node, in which case the local server node is the master server node.

[0064] In one implementation, this server node can determine the first identifier in the sorting result as the target identifier.

[0065] As another implementation, this server node can also determine the last identifier in the sorting result as the target identifier.

[0066] In some implementations, if two votes are equal in the sorting results, the local server node regenerates the election request and sends the regenerated election request to the remaining server nodes in the server cluster to re-elect the leader.

[0067] In some implementations, after all server nodes in the server cluster have selected a master server node, the master server node selected by all server nodes is the same server node, and the task is executed with this server node as the master server node.

[0068] Existing leader election methods utilize third-party components. If the task itself does not depend on these components, introducing them solely for leader election creates difficulties and adds an extra burden to software management. These third-party components can include Redis and ZooKeeper, among others.

[0069] The server cluster master node election method provided in this embodiment of the invention generates a first election request based on the identification information of the current server node, and sends the first election request to the remaining server nodes in the server cluster. It then obtains second election requests generated by the remaining server nodes in the server cluster. Each second election request includes the voting data and identification information of the corresponding server node. All identification information is sorted based on all voting data to obtain a sorting result. Target identification information is determined according to the sorting result and preset rules, and the server node corresponding to the target identification information is determined as the master server node selected by the current server node. By selecting the master node based on the voting data included in the election request generated by each node, the method avoids the problem of low efficiency in master node election due to reliance on third-party components, especially when these components malfunction. This improves master node election efficiency and reduces the difficulty and burden of software management.

[0070] Existing master election methods can use node startup time as the basis for election. Each node has its own startup completion time, and each node informs other hosts of its startup completion time. Finally, the host with the earliest startup time becomes the master server node, and the rest become slave server nodes. However, in a single physical node or virtualization environment, multiple virtual machines (pods) may start at the same time because they share the resources of the same physical host. That is, the startup time of multiple pods may be exactly the same. Therefore, it is not possible to use the pod startup time as the basis for master election. However, the server cluster master election method provided in this embodiment uses voting data as the basis for master election, instead of using the node startup time. Therefore, master election can still be performed in a single physical node or virtualization environment, without limiting the master election scenario, and avoiding the problem of re-electing the master due to multiple nodes having the same startup completion time, thereby improving the efficiency of master election.

[0071] like Figure 3 As shown, step 110 further includes the following steps:

[0072] Step S111: Obtain the current master election round of this server node and generate a random number with a preset number of digits.

[0073] The current leader election round is the leader election round of this server node. For example, if this is the first leader election, then the current round of this server node is 1.

[0074] In some implementations, server nodes can generate random numbers based on a preset number of bits. The preset number of bits is the same for each server node. For example, if the preset number of bits is 5, the random number could be 12345; if the preset number of bits is 3, the random number could be 222.

[0075] Step S112: Generate ballot data based on the random number and the identification information of this server node.

[0076] In some implementations, a random number is appended to the identifier information of the local server node to generate the voting data for that server node. For example, if the identifier information of the local server node is hostA and the random number is 22122, then the voting data for the local server node is hostA22122.

[0077] In some implementations, a random number is appended before the identifier information of the local server node to generate the voting data for that server node. For example, if the identifier information of the local server node is hostB and the random number is 2325, then the voting data for the local server node is 2325hostB.

[0078] Step S113: Generate the first election request based on the current election round, the identifier information of this server node, and the vote data of this server node.

[0079] The election request of this server node also includes the information type of the election request, so that the server node receiving the election request can confirm the type of the election request.

[0080] To facilitate sorting of the ballot data, such as Figure 4 As shown, step 130 further includes the following steps:

[0081] Step 131: Reverse the order of each ballot data.

[0082] It should be noted that reversing the order of the ballot data means reversing the order of the characters in the ballot data, that is, rearranging the characters in the ballot data in the reverse order. For example, if the ballot data is IP4587, reversing the order of the ballot data will result in 7854PI.

[0083] Step 132: Convert each ballot data after reverse processing into a preset format.

[0084] In some implementations, the ballot data is converted into values ​​corresponding to a preset character encoding standard, and each digit of the converted value is multiplied by 10 raised to the power of the corresponding number of digits. All results are then summed to obtain ballot data converted to a preset format.

[0085] The default format can be ASCII, Unicode, or UTF encoding, etc., and there are no restrictions here.

[0086] Step 133: Sort all the identification information according to all the ballot data converted to the preset format, and obtain the sorting result.

[0087] In one implementation, the server node can sort all the identification information in descending order based on all the ballot data converted to a preset format to obtain the sorting result.

[0088] In another implementation, the server node can sort all the identification information in ascending order based on all the ballot data converted to a preset format to obtain the sorting result.

[0089] For example, the default format is the sum of the digits after conversion to ASCII code. Server node A's vote data is ipA1112, and server node B's vote data is ipB2693. Reversing the order of server node A's vote data ipA1112 results in 2111Api. Converting 2111Api to ASCII code (51 49 49 49 65 112 105), and multiplying each digit of the ASCII code (51 49 49 49 65 112 105) by 10 raised to the power of its corresponding digit, and summing all the results, we obtain server node A's vote data in the default format as 116904440 (50 × 10^44). 0 +49×10 1 +49×10 2 +49×10 3 +65×10 4 +112×10 5 +105×10 6= The vote data of server node B, ipB2693, is reversed to 3962Bpi. This 3962Bpi is then converted to ASCII code (51 57 54 5066 112105). Each digit of this ASCII code is multiplied by 10 raised to the power of its corresponding digit. All these results are summed to obtain the vote data of server node B converted to the preset format: 116916021 (51 × 10^4440). 0 +57×10 1 +54×10 2 +50×10 3 +66×10 4 +112×10 5 +105×10 6= (116916021) Sort ipA and ipB in descending order based on the vote data 116904440 of server node A converted to the preset format and the vote data 116916021 of server node B converted to the preset format. The sorted result is ipB and ipA.

[0090] To improve the efficiency of sorting ballot data, the following steps are included before step 131: determining whether the length of each ballot data exceeds the preset character length; if the length of the ballot data exceeds the preset character length, selecting data of the preset character length from the corresponding ballot data and determining the selected data as new ballot data.

[0091] In some implementations, the server node may first select the random number portion from the corresponding ballot data, and then select the remaining data of a preset character length from the ballot data.

[0092] For example, if the preset character length is 7, and the ballot data is hostA28465, then first select a 5-digit random number 28465, then select the remaining 2 digits tA, and the new ballot data will be tA28465; if the preset character length is 8, and the ballot data is 875952hostB, then first select a 6-digit random number 875952, then select the remaining 2 digits ho, and the new ballot data will be 875952ho.

[0093] To ensure that each server node selects the same master server node, such as Figure 5 As shown, after step 140, the following steps are also included:

[0094] Step 150: Receive confirmation requests for selection results from the other server nodes.

[0095] Each selection result confirmation should include the identification information of the primary server node selected for the corresponding server node.

[0096] In some implementations, after determining the server node corresponding to the target identification information as the primary server node selected by the server node, the server node can generate a selection result confirmation request based on the target identification information and send the selection result confirmation request to the remaining server nodes in the server cluster.

[0097] Step 160: If the identification information included in each selection result confirmation request is the same as the target identification information, then the selection of the main body is confirmed to be successful.

[0098] In some implementations, after confirming successful master election, the server node corresponding to the target identifier information is used as the master server node to execute the task.

[0099] To monitor the operational status of the server cluster, promptly identify faulty server nodes, and minimize system downtime, such as... Figure 6 As shown, the above-mentioned server cluster leader election method also includes the following steps:

[0100] Step 210: Periodically send keep-alive information to the remaining server nodes.

[0101] Step 220: Receive response information sent by the other servers based on the keep-alive information.

[0102] Step 230: Based on the response information, check if there are any downtime server nodes.

[0103] It should be noted that if a server node goes down, it will be unable to send a response based on the keep-alive information. In other words, a server node that does not send a response will be identified as a downed server node.

[0104] Step 240: If there is a downtime server node, determine whether the downtime server node is the primary server node selected by this server node.

[0105] In some implementations, if a server node fails, the identifier information of the failed server node is obtained, and it is determined whether the identifier information of the failed server node is the target identifier information. If the identifier information of the failed server node is the target identifier information, then the failed server node is the main server node selected by this server node.

[0106] Step 250: If the server node that crashed is the master server node selected by this server node, then the server node corresponding to the next identifier information of the target identifier information in the sorting result shall be determined as the newly selected master server node of this server node.

[0107] In some implementations, the local server node stores the sorting results. After the main server node crashes, the server node corresponding to the next identifier information of the target identifier information in the sorting results is determined as the new main server node selected by the local server node.

[0108] For example, the sorting results are the identification information of server node A, server node C, server node D, and server node B. The primary server node selected for this server node is server node C. After server node C goes down, server node D will be determined as the newly selected primary server node.

[0109] It is understandable that this server node stores the sorting results. When the selected master server node fails, there is no need to re-elect a master. The new master server node can be determined based on the sorting results, thereby improving the efficiency of master node election.

[0110] In some implementations, if the current server node is the master server node and is down, then the current server node will stop executing the current task, and the other server nodes in the server cluster will re-determine the master server node.

[0111] To perform the corresponding steps in the above embodiments and various possible methods, an implementation of a server cluster master election device is given below. Further, please refer to... Figure 7 The figure shows a functional block diagram of a server cluster master node election device provided in an embodiment of the present invention. It should be noted that the basic principle and technical effects of the server cluster master node election device provided in this embodiment are the same as those in the above embodiments. For the sake of brevity, any parts not mentioned in this embodiment can be referred to the corresponding content in the above embodiments. The server cluster master node election device 200 includes: an election request generation module 210, an election request acquisition module 220, a sorting module 230, and a master node election module 240, wherein:

[0112] The election request generation module 210 is used to generate a first election request based on the identification information of the server node and send the first election request to the remaining server nodes in the server cluster.

[0113] The election request acquisition module 220 is used to acquire the second election requests generated by the other server nodes in the server cluster; each second election request includes the ballot data and identification information of the corresponding server node.

[0114] The sorting module 230 is used to sort all the identification information based on all the ballot data to obtain the sorting result.

[0115] The main selection module 240 is used to determine the target identification information based on the sorting results and preset rules, and to determine the server node corresponding to the target identification information as the main server node selected by this server node.

[0116] Furthermore, the election request generation module 210 is specifically used to obtain the current election round of the local server node and generate a random number of preset lengths; generate ballot data based on the random number and the identification information of the local server node; and generate a first election request based on the current election round, the identification information of the local server node, and the ballot data of the local server node.

[0117] Furthermore, the election request generation module 210 is specifically used to add a random number after the identification information of the local server node to generate the vote data of the local server node.

[0118] Furthermore, the sorting module 230 is specifically used to reverse the order of each ballot data; convert each ballot data after reverse order processing into a preset format; and sort all the identification information according to all the ballot data converted into the preset format to obtain the sorting result.

[0119] Furthermore, before reversing the order of each ballot data, the sorting module 230 is specifically used to determine whether the length of each ballot data exceeds the preset character length; if the length of the ballot data exceeds the preset character length, then select data of the preset character length from the corresponding ballot data and determine the selected data as the new ballot data.

[0120] Furthermore, the server cluster master election device 200 also includes: a master election confirmation module, wherein:

[0121] After determining the server node corresponding to the target identifier information as the master server node selected by this server node, the master selection confirmation module is used to receive selection result confirmation requests sent by other server nodes; each selection result confirmation request includes the identifier information of the master server node selected by the corresponding server node; if the identifier information included in each selection result confirmation request is the same as the target identifier information, then the master selection is confirmed to be successful.

[0122] Furthermore, the server cluster master selection device 200 also includes: a checking module, wherein:

[0123] The inspection module is used to periodically send keep-alive information to other server nodes; receive response information sent by other servers based on the keep-alive information; check whether there are any downed server nodes based on the response information; if there are downed server nodes, determine whether the downed server node is the master server node selected by this server node; if the downed server node is the master server node selected by this server node, determine the server node corresponding to the next identifier information of the target identifier information in the sorting result as the newly selected master server node of this server node.

[0124] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the above-described device and module can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.

[0125] In several embodiments provided by the present invention, the coupling between modules can be electrical, mechanical or other forms of coupling.

[0126] Furthermore, the functional modules in the various embodiments of the present invention can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The integrated modules described above can be implemented in hardware or as software functional modules.

[0127] Please refer to Figure 8This is a block diagram of a server node 100 provided in an embodiment of the present invention. The server node 100 includes a memory 110, a processor 120, and a communication module 130. The memory 110, processor 120, and communication module 130 are electrically connected to each other directly or indirectly to realize data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses or signal lines.

[0128] The memory 110 is used to store programs or data. The memory 110 may be, but is not limited to, random access memory (RAM), read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), etc.

[0129] The processor 120 is used to read / write data or programs stored in the memory and perform corresponding functions. For example, when a computer program stored in the memory 110 is executed by the processor 120, the server cluster leader election method disclosed in the above embodiments can be implemented.

[0130] The communication module 130 is used to establish communication connections between multiple server nodes 100 via the network and to send and receive data via the network.

[0131] It should be understood that, Figure 8 The structure shown is only a schematic diagram of a server node; a server node may also include more... Figure 8 The number of components shown may be more or less. Figure 8 The components shown can be implemented using hardware, software, or a combination thereof.

[0132] This invention also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the server cluster leader election method described in the above method embodiments.

[0133] Computer-readable storage media can be electronic storage devices such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or ROM. Optionally, computer-readable storage media includes non-transitory computer-readable storage medium. The computer-readable storage medium has storage space for program code that performs any of the method steps described above. This program code can be read from or written to one or more computer program products. The program code can be compressed, for example, in a suitable form.

[0134] In the several embodiments provided by this invention, it should be understood that the disclosed apparatus and methods can also be implemented in other ways. The apparatus embodiments described above are merely illustrative; for example, the flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of the invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code, which contains one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive 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 and / or flowchart, and combinations of blocks in block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.

[0135] If the functionality is implemented as a software module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

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

Claims

1. A method for electing a master node in a server cluster, characterized in that, The method, applicable to any server node in a server cluster, includes: A first election request is generated based on the identification information of this server node, and the first election request is sent to the remaining server nodes in the server cluster. Obtain the second election requests generated by the remaining server nodes in the server cluster; each second election request includes the vote data and identification information of the corresponding server node; Each ballot data is reversed; each ballot data after reversal is converted into a preset format; all the identification information is sorted according to all the ballot data converted into the preset format to obtain a sorting result; target identification information is determined according to the sorting result and preset rules, and the server node corresponding to the target identification information is determined as the main server node selected by this server node.

2. The method according to claim 1, characterized in that, The step of generating a first election request based on the identifier information of this server node includes: Get the current leader election round of this server node and generate a random number of a preset number of bits; Generate ballot data based on the random number and the identification information of this server node; The first election request is generated based on the current election round, the identifier information of this server node, and the vote data of this server node.

3. The method according to claim 2, characterized in that, The step of generating vote data based on the random number and the identifier information of the server node includes: The random number is added to the identifier information of the local server node to generate vote data.

4. The method according to claim 1, characterized in that, Before reversing the order of each ballot data, the method further includes: Determine whether the length of each ballot data exceeds a preset character length; If the length of the ballot data exceeds the preset character length, then data of the preset character length is selected from the corresponding ballot data, and the selected data is determined as the new ballot data.

5. The method according to claim 1, characterized in that, After determining the server node corresponding to the target identifier information as the primary server node selected by this server node, the method further includes: Receive selection result confirmation requests sent by the other server nodes; each selection result confirmation request includes the identification information of the main server node selected by the corresponding server node; If the identification information included in each selection result confirmation request is the same as the target identification information, then the selection of the main body is confirmed to be successful.

6. The method according to claim 1, characterized in that, The method further includes; Periodically send keep-alive information to the remaining server nodes; Receive response information sent by the other servers based on the keep-alive information; Based on the response information, check if there are any downtime server nodes; If a server node is down, determine whether the downed server node is the primary server node selected by this server node. If the server node that crashed is the master server node selected by this server node, then the server node corresponding to the next identifier information of the target identifier information in the sorting result will be determined as the newly selected master server node of this server node.

7. A server cluster master node election device, characterized in that, The device is applicable to any server node in a server cluster and includes: The election request generation module is used to generate a first election request based on the identification information of this server node, and send the first election request to the remaining server nodes in the server cluster. The election request acquisition module is used to acquire the second election requests generated by the remaining server nodes in the server cluster; each second election request includes the vote data and identification information of the corresponding server node; The sorting module is used to reverse the order of each ballot data; convert each ballot data after reversal into a preset format; and sort all the identification information according to all the ballot data converted into the preset format to obtain a sorting result. The master selection module is used to determine the target identification information based on the sorting results and preset rules, and to determine the server node corresponding to the target identification information as the master server node selected by this server node.

8. A server node, characterized in that, It includes a processor and a memory, the memory storing a computer program executable by the processor, the processor executing the computer program to implement the server cluster master node election method according to any one of claims 1-6.

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

Citation Information

Patent Citations

  • Cluster election processing method and device, equipment and storage medium

    CN113014634A

  • Distributed election method, device and system and medium

    CN113760468A