Database-based election methods, apparatus, and computer-readable media
By querying the database in a distributed cluster to obtain node information and updating the target node status, the configuration difficulties caused by the complexity of Zookeeper are solved, the election process is simplified, and the development and maintenance costs are reduced.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-25
- Publication Date
- 2026-03-13
AI Technical Summary
In existing technologies, the complexity of Zookeeper leads to high requirements for understanding configuration items, which makes development and maintenance difficult. In addition, it requires obtaining information about other nodes through configuration files, which increases the complexity of development and maintenance.
By querying the database to obtain node information in the distributed cluster, including node identifier, current term, current status, update time, and timeout, the state of the target node is updated based on this information, simplifying the election process and reducing the complexity of inter-node communication.
It simplifies the distributed election process, reduces the complexity of inter-cluster communication, reduces the complexity of development and maintenance, simplifies configuration, and facilitates monitoring of cluster status.
Smart Images

Figure CN116708116B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of cloud computing technology, and in particular to a database-based election method, apparatus, and computer-readable medium. Background Technology
[0002] Existing distributed frameworks typically use ZooKeeper to provide cluster services. ZooKeeper primarily relies on the Zab protocol to achieve data consistency. Based on this protocol, ZooKeeper implements a master-slave model (i.e., Leader model and Follower model) system architecture to ensure data consistency among nodes in the cluster. In this master-slave system architecture, only one node (Leader) is responsible for handling external write transaction requests, and the Leader node then synchronizes data to the other Follower nodes.
[0003] The Zookeeper client will randomly connect to a node in the Zookeeper cluster. If it is a read request, it will read data directly from the current node. If it is a write request, the node will commit the transaction to the Leader. When the Leader receives the transaction commit, it will broadcast the transaction. The transaction will be committed as long as more than half of the nodes successfully write.
[0004] In the process of realizing this invention, the inventors discovered at least the following problems in the prior art:
[0005] Due to the complexity of Zookeeper, a high level of understanding of its configuration items is required. Furthermore, Zookeeper needs to obtain information about other nodes through its configuration files, so each node needs to be configured, which brings certain difficulties to development and maintenance. Summary of the Invention
[0006] In view of this, embodiments of the present invention provide a database-based election method, apparatus, electronic device, and computer-readable medium to solve the technical problem of high difficulty in development and maintenance.
[0007] To achieve the above objectives, according to one aspect of the present invention, a database-based election method is provided, applied to any target node in a distributed cluster, comprising:
[0008] Query the database to obtain node information for each node in the distributed cluster; wherein, the node information includes node identifier, current term, current status, update time, timeout time, and candidate node identifier;
[0009] Based on the node information of each node, the current state of the target node is updated, thereby updating the node information of the target node in the database;
[0010] If the target node is currently a slave node, then based on the node information of each node, online candidate nodes are queried, and it is determined whether the current term of the candidate node is the maximum term. If yes, then the candidate node with the maximum term is voted on, thereby updating the node information of the target node in the database. If no, then the current state of the target node is updated to a candidate node, the node information of the target node in the database is updated, and a master node is elected based on the current term of each node and the candidate node identifier.
[0011] If the current state of the target node is a candidate node, then a master node is elected based on the current term of each node and the candidate node identifier.
[0012] Optionally, the current state of the target node is updated based on the node information of each node, including:
[0013] Determine whether the current time is greater than the timeout of the target node; if so, update the current state of the target node to a slave node, thereby updating the node information of the target node in the database.
[0014] The node information of each node is traversed to determine whether the current term of the target node is the maximum term. If not, the current term of the target node is updated to the maximum term and the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database.
[0015] Optionally, based on the node information of each node, online candidate nodes are queried, and it is determined whether the current term of the candidate node is the maximum term, including:
[0016] Based on the node information of each node, query whether there is an online master node;
[0017] If not, then based on the node information of each node, query the online candidate nodes and determine whether the current term of the candidate node is the maximum term.
[0018] If so, then the process ends.
[0019] Optionally, voting is conducted on the candidate nodes with the longest terms, including:
[0020] Determine whether a vote has been cast for the longest term; if not, count the number of candidate nodes for the longest term; if there is only one candidate node for the longest term, vote for that candidate node; if there are multiple candidate nodes for the longest term, vote for the candidate node with the largest node identifier among the candidate nodes for the longest term.
[0021] Optionally, a master node is elected based on the current term of office of each node and the candidate node identifier, including:
[0022] Waiting for a random amount of time;
[0023] Based on the node information of each node, query whether there is an online master node;
[0024] If so, the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database;
[0025] If not, the votes of each candidate node under the maximum term are counted, and the master node is elected based on the vote results.
[0026] Optionally, the votes of each candidate node under the maximum term are counted, and the master node is elected based on the vote results, including:
[0027] Based on the node information of each node, query whether there are other candidate nodes that are online and whose current term is the longest term.
[0028] If not, count the number of votes received by the target node under the maximum term, and determine whether to update the current state of the target node to the master node based on the number of votes received.
[0029] If so, the current state of the target node is updated to that of a slave node, thereby updating the node information of the target node in the database.
[0030] Optionally, the number of votes received by the target node under the maximum term is counted, and a decision is made based on the number of votes to update the current state of the target node to the master node, including:
[0031] Count the number of votes received by the target node under the maximum term;
[0032] Determine whether the number of votes received by the target node is greater than half of the total number of nodes;
[0033] If so, the current state of the target node is updated to the master node, thereby updating the node information of the target node in the database;
[0034] If not, then increment the current term of the target node by one, thereby updating the node information of the target node in the database.
[0035] Furthermore, according to another aspect of the present invention, a database-based election device is provided, which is set at any target node in a distributed cluster, comprising:
[0036] The query module is used to query the database to obtain node information of each node in the distributed cluster; wherein, the node information includes node identifier, current term, current status, update time, timeout time and candidate node identifier;
[0037] The status module updates the current status of the target node based on the node information of each node, thereby updating the node information of the target node in the database;
[0038] The election module is used to: if the current state of the target node is a slave node, query online candidate nodes based on the node information of each node, determine whether the current term of the candidate node is the maximum term; if yes, vote for the candidate node with the maximum term, thereby updating the node information of the target node in the database; if no, update the current state of the target node to a candidate node, update the node information of the target node in the database, and elect a master node based on the current term of each node and the candidate node identifier; if the current state of the target node is a candidate node, elect a master node based on the current term of each node and the candidate node identifier.
[0039] Optionally, the status module is further configured to:
[0040] Determine whether the current time is greater than the timeout of the target node; if so, update the current state of the target node to a slave node, thereby updating the node information of the target node in the database.
[0041] The node information of each node is traversed to determine whether the current term of the target node is the maximum term. If not, the current term of the target node is updated to the maximum term and the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database.
[0042] Optionally, the election module is further configured to:
[0043] Based on the node information of each node, query whether there is an online master node;
[0044] If not, then based on the node information of each node, query the online candidate nodes and determine whether the current term of the candidate node is the maximum term.
[0045] If so, then the process ends.
[0046] Optionally, the election module is further configured to:
[0047] Determine whether a vote has been cast for the longest term; if not, count the number of candidate nodes for the longest term; if there is only one candidate node for the longest term, vote for that candidate node; if there are multiple candidate nodes for the longest term, vote for the candidate node with the largest node identifier among the candidate nodes for the longest term.
[0048] Optionally, the election module is further configured to:
[0049] Waiting for a random amount of time;
[0050] Based on the node information of each node, query whether there is an online master node;
[0051] If so, the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database;
[0052] If not, the votes of each candidate node under the maximum term are counted, and the master node is elected based on the vote results.
[0053] Optionally, the election module is further configured to:
[0054] Based on the node information of each node, query whether there are other candidate nodes that are online and whose current term is the longest term.
[0055] If not, count the number of votes received by the target node under the maximum term, and determine whether to update the current state of the target node to the master node based on the number of votes received.
[0056] If so, the current state of the target node is updated to that of a slave node, thereby updating the node information of the target node in the database.
[0057] Optionally, the election module is further configured to:
[0058] Count the number of votes received by the target node under the maximum term;
[0059] Determine whether the number of votes received by the target node is greater than half of the total number of nodes;
[0060] If so, the current state of the target node is updated to the master node, thereby updating the node information of the target node in the database;
[0061] If not, then increment the current term of the target node by one, thereby updating the node information of the target node in the database.
[0062] According to another aspect of the present invention, an electronic device is also provided, comprising:
[0063] One or more processors;
[0064] Storage device for storing one or more programs.
[0065] When the one or more programs are executed by the one or more processors, the one or more processors implement the method described in any of the above embodiments.
[0066] According to another aspect of the present invention, a computer-readable medium is also provided, on which a computer program is stored, which, when executed by a processor, implements the methods described in any of the above embodiments.
[0067] According to another aspect of the present invention, a computer program product is also provided, including a computer program that, when executed by a processor, implements the methods described in any of the above embodiments.
[0068] One embodiment of the above invention has the following advantages or beneficial effects: Because it employs a technique of querying a database to obtain node information for each node and then electing a master node based on that information, it overcomes the significant development and maintenance difficulties inherent in existing technologies. In this embodiment, each node maintains its own state by periodically updating node information in the database. The database maintains the state of each node, facilitating cluster status monitoring. This embodiment simplifies the distributed election process, changing communication from inter-node communication to communication between each node and the database, thus reducing the complexity of inter-cluster communication. Compared to Zookeeper's election algorithm, this embodiment maintains the state of each node in the database, reducing the difficulty of monitoring cluster status, simplifying configuration, and preparing for subsequent cluster scheduling by electing a master node, thereby reducing development and maintenance complexity.
[0069] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description
[0070] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. Wherein:
[0071] Figure 1 This is a flowchart of a database-based election method according to an embodiment of the present invention;
[0072] Figure 2 This is a flowchart of a database-based election method according to a possible embodiment of the present invention;
[0073] Figure 3 This is a flowchart of a database-based election method according to another applicable embodiment of the present invention;
[0074] Figure 4a , Figure 4b and Figure 4c This is a schematic diagram of an election process according to an embodiment of the present invention;
[0075] Figure 5 This is a schematic diagram of a database-based election device according to an embodiment of the present invention;
[0076] Figure 6 This is an exemplary system architecture diagram in which embodiments of the present invention can be applied;
[0077] Figure 7 This is a schematic diagram of the structure of a computer system suitable for implementing terminal devices or servers of the present invention. Detailed Implementation
[0078] 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.
[0079] It should be noted that the collection, analysis, use, transmission, and storage of user personal information involved in the technical solution of this invention all comply with relevant laws and regulations, are used for legitimate and reasonable purposes, and are not shared, disclosed, or sold outside of these legitimate uses, and are subject to supervision and management by regulatory authorities. Necessary measures should be taken to prevent unauthorized access to such personal information data, ensure that personnel authorized to access personal information data comply with relevant laws and regulations, and ensure the security of user personal information. Once this user personal information data is no longer needed, the risk should be minimized by restricting or even prohibiting data collection and / or deleting the data.
[0080] When applicable, including in certain relevant applications, user privacy is protected by de-identifying data, such as by removing specific identifiers (e.g., date of birth, name, gender, etc.), controlling the amount or specificity of stored data, controlling how data is stored, and / or other methods of de-identification.
[0081] Figure 1This is a flowchart of a database-based election method according to an embodiment of the present invention. As one embodiment of the present invention, such as... Figure 1 As shown, the database-based election method, applied to any target node in a distributed cluster, may include:
[0082] Step 101: Query the database to obtain the node information of each node in the distributed cluster; wherein, the node information includes node identifier, current term, current status, update time, timeout time and candidate node identifier.
[0083] The database-based election method provided in this embodiment of the invention can be executed on any node (i.e., the target node) in the distributed cluster. Each node maintains a timed thread, which executes the database-based election method provided in this embodiment of the invention through a timed task. Therefore, only one thread performs state changes for each node, avoiding multiple threads changing the node state and reducing the complexity of node state changes.
[0084] In step 101, the node queries the database to obtain node information for each node stored in the database. This node information includes node identifier, current term, current status, update time, timeout time, and candidate node identifier. Optionally, the database structure is as follows:
[0085]
[0086] It's important to note that whenever a node's information (such as current term, current status, etc.) changes, the node updates the latest node information to the database, including updating the update time and timeout fields. For example, a timeout threshold of T can be set, in which case the timeout is the update time plus the timeout threshold T. Furthermore, to maintain online status, nodes update the update time and timeout fields in the database periodically.
[0087] Step 102: Update the current state of the target node based on the node information of each node, thereby updating the node information of the target node in the database.
[0088] For any node in the distributed cluster (i.e., the target node), the node determines whether it needs to update its current state based on the node information of each node in the distributed cluster obtained from the database. If it needs to update its current state, it updates its current state and then updates the current state to the database. If it does not need to update its current state, it keeps the current state.
[0089] Optionally, based on the node information of each node, the current state of the target node is updated, including: determining whether the current time is greater than the timeout of the target node; if so, updating the current state of the target node to a slave node, thereby updating the node information of the target node in the database; traversing the node information of each node to determine whether the current term of the target node is the maximum term; if not, updating the current term of the target node to the maximum term and updating the current state of the target node to a slave node, thereby updating the node information of the target node in the database. The target node determines whether the current time is greater than its timeout. If so, it indicates that the target node has been offline and is now back online; therefore, the current state of the target node is updated to a slave node, and the current state, update time, timeout, and other fields of the target node in the database are updated. If not, it indicates that the target node has been continuously online; therefore, it is not necessary to update the current state, only the update time and timeout are updated. Iterate through the current term of office in the node information of each node, and determine whether the current term of office of the target node is the maximum term. If it is, only update the update time and timeout time. If not, update the current term of office of the target node to the maximum term and update the current state of the target node to a slave node. Then update the current term of office, current state, update time, timeout time and other fields of the target node in the database.
[0090] If the target node is currently a slave node, proceed to step 103. If the target node is currently a candidate node, proceed to step 104. If the target node is currently a master node, schedule other nodes (i.e., slave nodes).
[0091] Step 103: Based on the node information of each node, query online candidate nodes and determine whether the current term of the candidate node is the maximum term. If yes, vote for the candidate node with the maximum term, thereby updating the node information of the target node in the database. If no, update the current state of the target node to a candidate node, update the node information of the target node in the database, and elect a master node based on the current term of each node and the candidate node identifier.
[0092] Optionally, based on the node information of each node, online candidate nodes are queried, and it is determined whether the current term of the candidate node is the maximum term. This includes: based on the node information of each node, querying whether there is an online master node; if not, based on the node information of each node, online candidate nodes are queried, and it is determined whether the current term of the candidate node is the maximum term; if so, the process ends. If the target node's current state is a slave node, it first queries whether there is an online master node based on the node information of each node. If the master node does not exist, it queries whether there is an online candidate node with the current term being the maximum term. If a master node exists, the process exits, i.e., the election ends.
[0093] Optionally, voting on the candidate nodes for the longest term includes: determining whether a vote has been cast for the longest term; if not, counting the number of candidate nodes for the longest term; if there is only one candidate node for the longest term, voting on that candidate node; if there are multiple candidate nodes for the longest term, voting on the candidate node with the largest node identifier among the candidate nodes for the longest term. If there is an online candidate node whose current term is the longest term, determining whether a vote has already been cast for the longest term; if no vote has been cast for the longest term, counting the number of candidate nodes for the longest term; if there is only one candidate node for the longest term, voting on that candidate node; if there are two or more candidate nodes for the longest term, randomly selecting one candidate node for voting or selecting the candidate node with the largest node identifier for voting; if a vote has already been cast for the longest term, no further voting is required for that term, and the process ends. Finally, updating the candidate node identifier, update time, timeout, and other fields of the target node in the database.
[0094] It should be noted that if there are no online candidate nodes with the current term being the maximum term, the current status of the target node will be updated to that of a candidate node, and then the current status, update time, timeout, and other fields of the target node in the database will be updated.
[0095] Step 104: Elect a master node based on the current term of each node and the candidate node identifier.
[0096] Optionally, a master node is elected based on the current term of each node and the candidate node identifier, including: waiting for a random time; querying whether there is an online master node based on the node information of each node; if so, updating the current state of the target node to a slave node, thereby updating the node information of the target node in the database; if not, counting the votes of each candidate node under the maximum term, and electing a master node based on the voting results. If the current state of the target node is a candidate node, a random time is waited, such as any time within 0-100 milliseconds, which can reduce conflicts with other nodes and help to elect a master node as quickly as possible. Then, it is queried whether there is an online master node. If so, the current state of the target node is updated to a slave node, and the current state, update time, timeout time, and other fields of the target node in the database are updated. If not, the votes of each candidate node under the maximum term are counted, and a master node is elected based on the voting results.
[0097] Optionally, the votes of each candidate node under the maximum term are counted, and a master node is elected based on the voting results. This includes: querying whether there are other online candidate nodes whose current term is the maximum term based on the node information of each node; if not, counting the number of votes of the target node under the maximum term, and determining whether to update the current state of the target node to master node based on the number of votes; if so, updating the current state of the target node to slave node, thereby updating the node information of the target node in the database. If there is no online master node, querying whether there are other online candidate nodes whose current term is the maximum term based on the node information of each node, if not, counting the number of votes of the target node under the maximum term, and determining whether to update the current state of the target node to master node based on the voting results; if so, updating the current state of the target node to slave node, and then updating the current state, update time, timeout time, and other fields of the target node in the database, which helps to complete the election quickly.
[0098] Optionally, the number of votes received by the target node under the maximum term is counted, and a decision is made based on the number of votes to update the current state of the target node to the master node. This includes: counting the number of votes received by the target node under the maximum term; determining whether the number of votes received by the target node is greater than half of the total number of nodes; if yes, the current state of the target node is updated to the master node, thereby updating the node information of the target node in the database; if no, the current term of the target node is incremented by one, thereby updating the node information of the target node in the database. If the number of votes received by the target node is greater than half of the total number of nodes, it means that the target node has obtained a majority of votes, so the current state of the target node is updated to the master node, and then the current state, update time, timeout time, and other fields of the target node in the database are updated. Otherwise, the current term is incremented by one, entering the election for the next term, and then the current term (becoming the maximum term), update time, timeout time, and other fields of the target node in the database are updated.
[0099] Based on the various embodiments described above, it can be seen that the present invention solves the technical problem of high development and maintenance difficulty in the prior art by querying the database to obtain the node information of each node in the database and electing a master node based on the node information of each node. In the embodiments of the present invention, each node maintains its own state by periodically updating the node information in the database, and the database maintains the state of each node, which facilitates monitoring the cluster state. The embodiments of the present invention simplify the distributed election process, changing the original inter-node communication to communication between each node and the database, reducing the communication complexity between the clusters. Compared with Zookeeper's election algorithm, the embodiments of the present invention can maintain the state of each node in the database, reducing the difficulty of monitoring the cluster state, simplifying configuration, and preparing for the election of the master node between the clusters, thus reducing the development and maintenance complexity.
[0100] Figure 2 This is a flowchart of a database-based election method according to a possible embodiment of the present invention. As another embodiment of the present invention, such as... Figure 2 As shown, if the target node's current state is a slave node, the database-based election method may include:
[0101] Step 201: Based on the node information of each node, query whether there is an online master node; if not, proceed to step 202; if yes, end.
[0102] Step 202: Based on the node information of each node, query the online candidate nodes.
[0103] Step 203: Determine whether the current term of the candidate node is the maximum term; if yes, proceed to step 204; if no, proceed to step 206.
[0104] Step 204: Determine if the candidate has voted during their maximum term; if not, proceed to step 205; if yes, the process ends.
[0105] Step 205: Count the number of candidate nodes with the longest term; if there is only one candidate node with the longest term, vote on the candidate node; if there are multiple candidate nodes with the longest term, vote on the candidate node with the largest node identifier among the candidate nodes with the longest term; and update the node information of the target node in the database.
[0106] Step 206: Update the current state of the target node to a candidate node and update the node information of the target node in the database.
[0107] Step 207: Elect a master node based on the current term of each node and the candidate node identifier.
[0108] Furthermore, the specific implementation details of the database-based election method in one of the reference embodiments of the present invention have been described in detail in the above-described database-based election method, so the details will not be repeated here.
[0109] Figure 3 This is a flowchart of a database-based election method according to another possible embodiment of the present invention. As another embodiment of the present invention, such as... Figure 3 As shown, if the current state of the target node is a candidate node, the database-based election method may include:
[0110] Step 301: Wait for a random amount of time.
[0111] Step 302: Based on the node information of each node, query whether there is an online master node; if not, proceed to step 303; if yes, proceed to step 307.
[0112] Step 303: Based on the node information of each node, query whether there are other candidate nodes that are online and whose current term is the longest term; if not, proceed to step 304; if yes, proceed to step 307.
[0113] Step 304: Count the number of votes received by the target node under the maximum term, and determine whether the number of votes received by the target node is greater than half of the total number of nodes; if yes, proceed to step 305; if no, proceed to step 306.
[0114] Step 305: Update the current state of the target node to the master node, thereby updating the node information of the target node in the database.
[0115] Step 306: Increment the current term of the target node by one, thereby updating the node information of the target node in the database.
[0116] Step 307: Update the current state of the target node to a slave node, thereby updating the node information of the target node in the database.
[0117] like Figure 4a As shown, the cluster consists of three nodes: node 1, node 2, and node 3. Initially, all three nodes have a tenure of 0 and are currently in the follower state. Figure 4b As shown, the cluster begins electing a leader. Node 1's current state becomes candidate, its term is incremented by one, and it awaits votes from other nodes. Figure 4c As shown, the voting has ended, node 1 has received two votes, and its current state has become leader.
[0118] In addition, the specific implementation details of the database-based election method in another reference embodiment of the present invention have been described in detail in the above-described database-based election method, so the details will not be repeated here.
[0119] Figure 5 This is a schematic diagram of a database-based election device according to an embodiment of the present invention. Figure 5 As shown, the database-based election device 500 includes a query module 501, a status module 502, and an election module 503. The query module 501 queries the database to obtain node information for each node in the distributed cluster. This node information includes node identifier, current term, current status, update time, timeout time, and candidate node identifier. The status module 502 updates the current status of the target node based on the node information of each node, thereby updating the node information of the target node in the database. The election module 503 is used if the current status of the target node is a slave node... Based on the node information of each node, online candidate nodes are queried, and it is determined whether the current term of the candidate node is the maximum term. If so, a vote is held for the candidate node with the maximum term, thereby updating the node information of the target node in the database. If not, the current state of the target node is updated to a candidate node, the node information of the target node in the database is updated, and a master node is elected based on the current term of each node and the candidate node identifier. If the current state of the target node is a candidate node, a master node is elected based on the current term of each node and the candidate node identifier.
[0120] Optionally, the status module 502 is further configured to:
[0121] Determine whether the current time is greater than the timeout of the target node; if so, update the current state of the target node to a slave node, thereby updating the node information of the target node in the database.
[0122] The node information of each node is traversed to determine whether the current term of the target node is the maximum term. If not, the current term of the target node is updated to the maximum term and the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database.
[0123] Optionally, the election module 502 is further configured to:
[0124] Based on the node information of each node, query whether there is an online master node;
[0125] If not, then based on the node information of each node, query the online candidate nodes and determine whether the current term of the candidate node is the maximum term.
[0126] If so, then the process ends.
[0127] Optionally, the election module 502 is further configured to:
[0128] Determine whether a vote has been cast for the longest term; if not, count the number of candidate nodes for the longest term; if there is only one candidate node for the longest term, vote for that candidate node; if there are multiple candidate nodes for the longest term, vote for the candidate node with the largest node identifier among the candidate nodes for the longest term.
[0129] Optionally, the election module 502 is further configured to:
[0130] Waiting for a random amount of time;
[0131] Based on the node information of each node, query whether there is an online master node;
[0132] If so, the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database;
[0133] If not, the votes of each candidate node under the maximum term are counted, and the master node is elected based on the vote results.
[0134] Optionally, the election module 502 is further configured to:
[0135] Based on the node information of each node, query whether there are other candidate nodes that are online and whose current term is the longest term.
[0136] If not, count the number of votes received by the target node under the maximum term, and determine whether to update the current state of the target node to the master node based on the number of votes received.
[0137] If so, the current state of the target node is updated to that of a slave node, thereby updating the node information of the target node in the database.
[0138] Optionally, the election module 502 is further configured to:
[0139] Count the number of votes received by the target node under the maximum term;
[0140] Determine whether the number of votes received by the target node is greater than half of the total number of nodes;
[0141] If so, the current state of the target node is updated to the master node, thereby updating the node information of the target node in the database;
[0142] If not, then increment the current term of the target node by one, thereby updating the node information of the target node in the database.
[0143] It should be noted that the specific implementation details of the database-based election device described in this invention have been described in detail in the database-based election method described above, so the details will not be repeated here.
[0144] Figure 6 An exemplary system architecture 600 is shown that can be applied to the database-based election method or database-based election apparatus of the present invention.
[0145] like Figure 6 As shown, system architecture 600 may include terminal devices 601, 602, and 603, a network 604, and a server 605. Network 604 serves as the medium for providing communication links between terminal devices 601, 602, and 603 and server 605. Network 604 may include various connection types, such as wired or wireless communication links or fiber optic cables, etc.
[0146] Users can use terminal devices 601, 602, and 603 to interact with server 605 via network 604 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 601, 602, and 603, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc. (for example only).
[0147] Terminal devices 601, 602, and 603 can be various electronic devices with displays and web browsing capabilities, including but not limited to smartphones, tablets, laptops, and desktop computers.
[0148] Server 605 can be a server that provides various services, such as a backend management server that supports shopping websites browsed by users using terminal devices 601, 602, and 603 (this is just an example). The backend management server can analyze and process data such as received item information query requests, and then feed the processing results back to the terminal devices.
[0149] It should be noted that the database-based election method provided in this embodiment of the invention is generally executed by server 605, and correspondingly, the database-based election device is generally set in server 605.
[0150] It should be understood that Figure 6 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0151] The following is for reference. Figure 7 It shows a schematic diagram of the structure of a computer system 700 suitable for implementing a terminal device of the present invention. Figure 7 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.
[0152] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 702 or programs loaded from storage section 708 into random access memory (RAM) 703. The RAM 703 also stores various programs and data required for the operation of the system 700. The CPU 701, ROM 702, and RAM 703 are interconnected via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0153] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, mouse, etc.; an output section 707 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 710 as needed so that computer programs read from it can be installed into the storage section 708 as needed.
[0154] 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 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 709, and / or installed from removable medium 711. When the computer program is executed by central processing unit (CPU) 701, it performs the functions defined above in the system of this invention.
[0155] 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.
[0156] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer programs 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 the 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.
[0157] The modules described in the embodiments of the present invention can be implemented in software or hardware. The described modules can also be housed in a processor; for example, a processor can be described as including a query module, a status module, and an election module, wherein the names of these modules do not necessarily limit the module itself.
[0158] 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 alone and not assembled into the device. The aforementioned computer-readable medium carries one or more programs. When one or more of these programs are executed by a device, the device implements the following method: querying a database to obtain node information of each node in the distributed cluster; wherein the node information includes node identifier, current term, current state, update time, timeout time, and candidate node identifier; updating the current state of the target node based on the node information of each node, thereby updating the node information of the target node in the database; if the current state of the target node is a slave node, querying online candidate nodes based on the node information of each node, and determining whether the current term of the candidate node is the maximum term; if yes, voting is performed on the candidate node with the maximum term, thereby updating the node information of the target node in the database; if no, updating the current state of the target node to a candidate node, updating the node information of the target node in the database, and electing a master node based on the current term of each node and the candidate node identifier; if the current state of the target node is a candidate node, electing a master node based on the current term of each node and the candidate node identifier.
[0159] In another aspect, embodiments of the present invention also provide a computer program product, including a computer program that, when executed by a processor, implements the methods described in any of the above embodiments.
[0160] According to the technical solution of this invention, by employing a database query to obtain node information of each node in the database and then performing election based on that information, the technical problem of high development and maintenance difficulty in the prior art is overcome. In this embodiment, each node maintains its own state by periodically updating node information in the database. The database maintains the state of each node, facilitating cluster status monitoring. This embodiment simplifies the distributed election process, changing communication from inter-node communication to communication between each node and the database, reducing the complexity of inter-cluster communication. Compared to Zookeeper's election algorithm, this embodiment maintains the state of each node in the database, reducing the difficulty of monitoring cluster status, simplifying configuration, and preparing for subsequent cluster scheduling by electing a master node, thus reducing development and maintenance complexity.
[0161] 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 database-based election method, characterized by, The application is applied to any target node in a distributed cluster, comprising: querying a database to obtain node information of each node in the distributed cluster; wherein the node information comprises node identification, current term, current state, update time, timeout time and candidate node identification; updating the current state of the target node according to the node information of each node, thereby updating the node information of the target node in the database; if the current state of the target node is a follower node, then according to the node information of each node, querying an online candidate node, judging whether the current term of the candidate node is the maximum term; if yes, then voting for the candidate node with the maximum term, thereby updating the node information of the target node in the database; if no, then updating the current state of the target node to a candidate node, updating the node information of the target node in the database, and electing a master node according to the current term of each node and the candidate node identification; if the current state of the target node is a candidate node, then electing a master node according to the current term of each node and the candidate node identification.
2. The method of claim 1, wherein, updating the current state of the target node according to the node information of each node, comprising: judging whether the current time is greater than the timeout time of the target node; if yes, then updating the current state of the target node to a follower node, thereby updating the node information of the target node in the database; traversing the node information of each node, judging whether the current term of the target node is the maximum term; if no, then updating the current term of the target node to the maximum term and updating the current state of the target node to a follower node, thereby updating the node information of the target node in the database.
3. The method of claim 1, wherein, according to the node information of each node, querying an online candidate node, judging whether the current term of the candidate node is the maximum term, comprising: according to the node information of each node, querying whether there is an online master node; if no, then according to the node information of each node, querying an online candidate node, judging whether the current term of the candidate node is the maximum term; if yes, then ending.
4. The method of claim 3, wherein, voting for the candidate node with the maximum term, comprising: judging whether a vote has been cast for the maximum term; if no, then counting the number of candidate nodes with the maximum term; if the number of candidate nodes with the maximum term is one, then voting for the candidate node; if the number of candidate nodes with the maximum term is more than one, then voting for the candidate node with the maximum node identification.
5. The method of claim 1, wherein, electing a master node according to the current term of each node and the candidate node identification, comprising: waiting for a random time; according to the node information of each node, querying whether there is an online master node; if yes, then updating the current state of the target node to a follower node, thereby updating the node information of the target node in the database; if no, then counting the votes of each candidate node under the maximum term, and electing a master node according to the vote results.
6. The method of claim 5, wherein, The votes of each candidate node under the maximum term are counted, and a master node is elected according to the vote results, including: According to the node information of the nodes, it is inquired whether there is another candidate node which is online and whose current term is the maximum term; If not, the number of votes of the target node under the maximum term is counted, and it is determined whether to update the current state of the target node to the master node according to the number of votes; If yes, the current state of the target node is updated to the slave node, thereby updating the node information of the target node in the database.
7. The method of claim 6, wherein, The number of votes of the target node under the maximum term is counted, and it is determined whether to update the current state of the target node to the master node according to the number of votes, including: The number of votes of the target node under the maximum term is counted; It is determined whether the number of votes of the target node is greater than half of the total number of nodes; If yes, the current state of the target node is updated to the master node, thereby updating the node information of the target node in the database; If not, the current term of the target node is added by one, thereby updating the node information of the target node in the database.
8. A database-based election apparatus, characterized by comprising: Any target node arranged in a distributed cluster, including: A query module is configured to query a database to obtain node information of each node in the distributed cluster; wherein the node information includes a node identifier, a current term, a current state, an update time, a timeout time, and a candidate node identifier; A state module is configured to update the current state of the target node according to the node information of the nodes, thereby updating the node information of the target node in the database; An election module is configured to, if the current state of the target node is a slave node, query online candidate nodes according to the node information of the nodes, and determine whether the current term of the candidate nodes is the maximum term; if yes, vote for the candidate nodes under the maximum term, thereby updating the node information of the target node in the database; if not, update the current state of the target node to a candidate node, update the node information of the target node in the database, and elect a master node according to the current term of the nodes and the candidate node identifier; if the current state of the target node is a candidate node, elect a master node according to the current term of the nodes and the candidate node identifier.
9. The apparatus of claim 8, wherein, The state module is further configured to: Determine whether the current time is greater than the timeout time of the target node; if yes, update the current state of the target node to a slave node, thereby updating the node information of the target node in the database; Traverse the node information of the nodes to determine whether the current term of the target node is the maximum term; If not, update the current term of the target node to the maximum term and update the current state of the target node to a slave node, thereby updating the node information of the target node in the database.
10. The apparatus of claim 8, wherein, The election module is further configured to: According to the node information of the nodes, it is inquired whether there is an online master node; If not, according to the node information of the respective nodes, a candidate node online is queried out, and it is judged whether the current term of the candidate node is the maximum term; If yes, the process ends.
11. The apparatus of claim 10, wherein, The election module is further configured to: Judge whether a vote is cast for the maximum term; if not, the number of the candidate nodes in the maximum term is counted; if the number of the candidate nodes in the maximum term is one, a vote is cast for the candidate node; If the number of the candidate nodes in the maximum term is more than one, a vote is cast for the candidate node with the largest node identifier among the candidate nodes in the maximum term.
12. The apparatus of claim 8, wherein, The election module is further configured to: Wait for a random time; According to the node information of the respective nodes, it is queried whether there is a master node online; If yes, the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database; If not, the votes of the respective candidate nodes in the maximum term are counted, and a master node is elected according to the vote results.
13. The apparatus of claim 12, wherein, The election module is further configured to: According to the node information of the respective nodes, it is queried whether there is another candidate node online and the current term of which is the maximum term; If not, the number of votes of the target node in the maximum term is counted, and it is judged whether the current state of the target node is updated to a master node according to the number of votes; If yes, the current state of the target node is updated to a slave node, thereby updating the node information of the target node in the database.
14. The apparatus of claim 13, wherein, The election module is further configured to: Count the number of votes of the target node in the maximum term; Judge whether the number of votes of the target node is greater than half of the total number of nodes; If yes, the current state of the target node is updated to a master node, thereby updating the node information of the target node in the database; If not, the current term of the target node is added by one, thereby updating the node information of the target node in the database.
15. An electronic device, comprising: Comprise: One or more processors; Storage devices 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 of any one of claims 1-7.
16. A computer readable medium having stored thereon a computer program, characterized in that, The program is executed by the processor to implement the method of any one of claims 1-7.
17. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the method of any one of claims 1-7.
Citation Information
Patent Citations
Node election method and device, electronic equipment and storage medium
CN114363154A
System and method for determining consensus within a distributed database
US20170262519A1