Distributed cluster master node election methods, devices and computer equipment

By setting up a master node table in a distributed cluster and using a heartbeat thread to elect the master node, the problems of hardware configuration burden and cumbersome communication in existing technologies are solved, achieving efficient and simplified master node election and management.

CN116319789BActive Publication Date: 2025-10-31CHINA CONSTRUCTION BANK +1
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202310183943.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-03-01
Publication Date
2025-10-31
Estimated Expiration
2043-03-01

AI Technical Summary

Technical Problem

In existing distributed cluster management, the election algorithm requires each node to store a large amount of information about other nodes and communication rules, which increases the burden on hardware configuration, makes communication cumbersome, and is prone to communication chaos and system paralysis.

Method used

In a distributed cluster, a master node table is set up, and the master node status is checked periodically by a heartbeat thread. Nodes periodically run the heartbeat thread to elect a master node, reducing inter-node communication, lowering hardware requirements, and simplifying configuration.

Benefits of technology

It enables efficient election and update of master nodes, reduces the requirements for server hardware, reduces the consumption of communication resources between nodes, improves management efficiency, and avoids communication chaos and system paralysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116319789B_ABST
    Figure CN116319789B_ABST
Patent Text Reader

Abstract

This disclosure relates to the field of big data processing, specifically disclosing a method, apparatus, and computer device for electing a master node in a distributed cluster. In one embodiment, a master node table is set up in the cluster database. Nodes can perform heartbeats by executing a heartbeat thread and confirm whether a master node exists in the cluster based on the master node table. Under the control of the heartbeat thread, if no master node exists, the current node can write itself as the master node into the master node table in the database. In this way, nodes in the cluster can elect a master node by communicating with the database. This is easy to configure and implement, and there is no need to store information of other nodes, which greatly reduces the hardware requirements of the distributed cluster on the server or computer. It also eliminates the need for inter-node communication to confirm the master node, reducing the consumption of inter-node communication resources and improving the efficiency of cluster nodes in the distributed system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of big data processing, and in particular to a method, apparatus, computer device, computer-readable storage medium, and computer program product for electing master nodes in a distributed cluster. Background Technology

[0002] With the development of big data technology, single computers can no longer meet the processing needs of massive amounts of data. To cope with increasingly complex data processing tasks, distributed systems are being used more and more widely. A distributed system is a system in which multiple systems cooperate to complete a specific task, with each system responsible for one or more sub-tasks broken down from that task. To improve high availability when each system processes a sub-task, the system can be deployed on multiple servers, that is, cluster technology is used to realize system business processing. This kind of cluster formed under a distributed system can also be called a distributed cluster.

[0003] In the construction of distributed systems, one unavoidable topic is distributed election. Distributed election refers to the process by which a system selects a node as the master node from among multiple nodes managed in a cluster. Existing election algorithms used in distributed cluster management inevitably suffer from hardware and configuration issues. For example, the Raft election algorithm requires pre-configuration of information for each node, and the ZAB algorithm requires broadcasting confirmation from more than half of the nodes to elect a master node. This necessitates communication between all nodes, which can easily lead to communication chaos in large-scale clusters with many nodes. Therefore, existing election methods require each node to store a large amount of information about other nodes and election communication rules, placing a significant burden on the hardware configuration of the cluster nodes. This situation worsens as the number of nodes in the cluster increases. Summary of the Invention

[0004] Based on this, and to address the aforementioned technical problems, a method, apparatus, computer device, computer-readable storage medium, and computer program product for electing a master node in a distributed cluster are provided. The technical solution disclosed herein is as follows:

[0005] According to one aspect of the embodiments of this disclosure, a method for electing a master node in a distributed cluster is provided, comprising:

[0006] Obtain the master node table; the master node table is used to store information about the master node, and the master node table is stored in the database of a specified node in the distributed cluster;

[0007] During the operation of the distributed cluster, a preset heartbeat thread is run periodically. The heartbeat thread controls the nodes in the distributed cluster to perform the following processing steps: check the existence status of the master node in the master node table, and if the existence status is not found, participate in the master node election, update the master node table with the current node as the master node, and the thread ends after the master node is obtained.

[0008] In one embodiment, the master node information includes master node identification information and master node heartbeat time, and updating the master node table by setting the current node as the master node includes:

[0009] Based on the current node information and the current time, update the master node identifier information and the master node heartbeat time with status, and end the current heartbeat thread.

[0010] In one embodiment, when the existence state is yes, the processing step further includes:

[0011] Check if the heartbeat time of the master node has expired. If it has expired, change the existence status to no and end the current heartbeat thread.

[0012] And / or,

[0013] Check if the master node heartbeat time has expired. If it has not expired, confirm whether the master node identification information is consistent with the current node information. If they are consistent, update the master node heartbeat time to the current time and end the current heartbeat thread.

[0014] In one embodiment, the processing steps further include, before checking the existence status of master nodes in the master node table:

[0015] Obtain the cluster node registry stored in the database, check whether the current node has completed registration in the cluster node registry, if not, register the current node and end the current heartbeat thread.

[0016] In one embodiment, the cluster node registry stores the heartbeat times of each node in the distributed cluster, and after obtaining the master node table, it further includes:

[0017] The master node monitors the registry of the cluster nodes, identifies nodes whose heartbeat timeouts as abnormal nodes, and sends the information of the abnormal nodes to a designated receiving end.

[0018] In one embodiment, after obtaining the master node table, the method further includes:

[0019] Modify the existence status of the master node in the master node table according to the preset first instruction;

[0020] And / or,

[0021] The information of the master node is forcibly updated according to the preset second instruction.

[0022] According to another aspect of the embodiments of this disclosure, a master node election device for a distributed cluster is provided, comprising:

[0023] The information acquisition module is used to acquire the master node table; the master node table is used to store the information of the master node, and the master node table is stored in the database of a specified node in the distributed cluster;

[0024] The thread management module is used to periodically run a preset heartbeat thread during the operation of the distributed cluster, controlling the nodes in the distributed cluster to execute specific processing steps; the thread management module includes the following units:

[0025] The master node confirmation unit is used to control the node to check the existence status of the master node in the master node table.

[0026] The master node election unit is used to control the node to participate in the master node election when the existence status is not specified, and to update the master node table by making the current node the master node.

[0027] In one embodiment, the master node information includes master node identification information and master node heartbeat time, and the master node election unit includes:

[0028] The master node update unit is used to control the node to update the master node identification information and the master node heartbeat time with status based on the current node information and the current time.

[0029] In one embodiment, the thread management module further includes:

[0030] The master node reset unit is used to control the node to detect whether the master node heartbeat time has expired. If it has expired, the node is controlled to change the existence status to no.

[0031] And / or,

[0032] The heartbeat update unit is used to control the node to confirm whether the master node identification information is consistent with the current node information. If they are consistent, the master node heartbeat time is updated to the current time.

[0033] In one embodiment, the thread management module further includes:

[0034] The node registration unit is used to obtain the cluster node registry stored in the database, detect whether the current node has completed registration in the cluster node registry, and if not, register the current node.

[0035] In one embodiment, the cluster node registry stores the heartbeat times of each node in the distributed cluster, and the device further includes:

[0036] The node monitoring module is used to control the master node to monitor the registry of the cluster nodes, identify nodes whose heartbeat timeout has expired as abnormal nodes, and send the information of the abnormal nodes to a designated receiving end.

[0037] In one embodiment, the device further includes:

[0038] The master node modification module is used to modify the existence status of the master nodes in the master node table according to a preset first instruction.

[0039] And / or,

[0040] The master node designation module is used to force an update of the information of the master node according to a preset second instruction.

[0041] According to another aspect of the present disclosure, a computer device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the above-described method.

[0042] According to another aspect of the present disclosure, a computer-readable storage medium is also provided, on which a computer program is stored, which, when executed by a processor, implements the steps of the above-described method.

[0043] According to another aspect of the present disclosure, a computer program product is also provided, including a computer program that, when executed by a processor, implements the steps of the above-described method.

[0044] In the technical solution provided in this disclosure, a master node table is set in the database that all nodes in the cluster are connected to. Nodes in the cluster can perform heartbeats by executing a heartbeat thread and confirm the existence of a master node based on the master node table. If no master node exists, the current node can write itself as the master node into the master node table of the database. In this way, nodes in the cluster can achieve master node election and updates through communication with the database. This is easy to configure and implement, and eliminates the need to store information about other nodes, greatly reducing the hardware requirements of the distributed cluster on servers or computers. It also eliminates the need for inter-node communication to confirm the master node, reducing the consumption of inter-node communication resources and improving the management efficiency of the distributed system for cluster nodes. Furthermore, as long as at least one node in the cluster is online, the election of a master node can be achieved by periodically running a heartbeat thread, eliminating the need for additional system configuration for application detection of the master node and simplifying the cluster's working mode.

[0045] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description

[0046] To more clearly illustrate the technical solutions in the embodiments of this specification 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 recorded in this specification. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0047] Figure 1 This is a flowchart illustrating a master node election method for a distributed cluster in one embodiment;

[0048] Figure 2 This is a flowchart illustrating a master node election method for a distributed cluster in another embodiment;

[0049] Figure 3 This is a flowchart illustrating a master node election method for a distributed cluster in another embodiment;

[0050] Figure 4 This is a flowchart illustrating a master node election method for a distributed cluster in another embodiment;

[0051] Figure 5 This is a flowchart illustrating a master node election method for a distributed cluster in another embodiment;

[0052] Figure 6 This is a flowchart illustrating a master node election method for a distributed cluster in another embodiment;

[0053] Figure 7 This is a schematic diagram of a cluster node running a heartbeat thread in one embodiment;

[0054] Figure 8 This is a schematic diagram of the structure of a master node election device for a distributed cluster in one embodiment;

[0055] Figure 9 This is a schematic diagram of the internal structure of the master node election unit in one embodiment;

[0056] Figure 10 This is a schematic diagram of the internal structure of the thread management module in one embodiment;

[0057] Figure 11 This is a schematic diagram of the thread management module in another embodiment;

[0058] Figure 12This is a schematic diagram of the structure of a master node election device for a distributed cluster in another embodiment;

[0059] Figure 13 This is a schematic diagram of the structure of a master node election device for a distributed cluster in another embodiment;

[0060] Figure 14 This is a schematic diagram of the internal structure of a computer device in one embodiment. Detailed Implementation

[0061] To enable those skilled in the art to better understand the technical solutions of this disclosure, the technical solutions in the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings.

[0062] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this disclosure are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this disclosure described herein can be implemented in orders other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this disclosure as detailed in the appended claims. The terms "comprising," "including," or any other variations thereof are intended to cover a non-exclusive inclusion, such that a process, method, product, 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, product, or apparatus. Without further limitation, the presence of other identical or equivalent elements in a process, method, product, or apparatus that includes said elements is not excluded. For example, the use of terms such as "first," "second," etc., is to denote names and does not indicate any specific order.

[0063] Unless otherwise defined, the technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used herein in the specification of this invention is for the purpose of describing particular embodiments only and is not intended to limit the invention. The terms “and / or,” “and / or,” and “at least one of” as used herein include any and all combinations of one or more of the associated listed items. It should be noted that the connections, links, etc., described in this disclosure can be direct connections via interfaces or pins between devices, connections via leads, or wireless connections (communication connections).

[0064] It should also be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for display, data used for analysis, etc.) involved in this disclosure are all information and data authorized by the user or fully authorized by all parties. The acquisition, storage, use, and processing of data in the technical solution of this application all comply with the relevant provisions of national laws and regulations.

[0065] In existing methods, the number of nodes managed in a distributed system cluster is extremely large. To effectively manage these nodes, various fast and highly available distributed election algorithms are often used. However, these algorithms inevitably introduce hardware and configuration problems as the number of nodes increases. For example, the currently popular ZAB (Zookeeper Atomic Broadcast, ZooKeeper is a distributed, open-source distributed application coordination service) algorithm, due to its broadcast method, requires all nodes to communicate with each other during the election of the master node. A candidate node needs to be approved by more than half of the nodes to be successfully elected as the master node. This requires each node to store information about other nodes, putting significant pressure on the server or computer where the node resides. In addition, as the number of nodes increases, in addition to the further increase in the storage pressure on the nodes themselves, the communication process during node election becomes increasingly cumbersome, making it difficult for their own business processes to operate normally. In severe cases, it can even cause broadcast storms, leading to system paralysis.

[0066] To address the aforementioned problems, according to one aspect of an embodiment of this disclosure, such as Figure 1 As shown, a method for electing a master node in a distributed cluster is provided, the method comprising:

[0067] Step S1: Obtain the master node table; the master node table is used to store information about the master node, and the master node table is stored in the database of a specified node in the distributed cluster.

[0068] The database can be a database shared by all nodes within the cluster. The database can be located on a server where one or more designated nodes in the cluster reside. A master node table can be pre-created and stored in the database to record the current master node of the cluster. The master node table can include master node identification information, master node heartbeat time, etc., where the master node identification information can be the master node ID. In some other embodiments, the master node table may also include information indicating the existence or election status of the master node.

[0069] It should be noted that in a distributed system, each system needs to connect to a database, and therefore a data source is configured. In the embodiments of this disclosure, nodes connected to the same database schema can be considered as the same cluster, with the database schema (data schema or data model, which is a description of the logical structure and characteristics of all data in the database; it only involves the description of types and not specific values) as the boundary. In other words, the database in this disclosure is not limited to requiring all nodes in the cluster to use the exact same instance; if different nodes use databases with the same data model, they are also within the scope of protection covered by this disclosure.

[0070] Specifically, the master node table pre-stored in the cluster database can be obtained by pre-configuring data sources, and information such as the master node identifier and heartbeat time can be obtained from the master node table. In some other implementations, a separate field can be set in the master node table to represent the election status of the master node. Through this field, the status can be directly determined as follows: master node election has not started, master node election is in progress, master node election has been completed, etc.

[0071] Step S2: During the operation of the distributed cluster, a preset heartbeat thread is run periodically. The heartbeat thread controls the nodes in the distributed cluster to perform the following processing steps:

[0072] In a distributed cluster, the application functions of nodes within the cluster are identical, and they connect to the same database. Specifically, a heartbeat thread can be pre-created within the application. Each node in the cluster runs this heartbeat thread to perform heartbeats and execute the following processing steps. In some specific implementations, the heartbeat thread can confirm the node's heartbeat time by sending heartbeat packets with a designated server. If no response is received within a preset time interval, the heartbeat is considered to have timed out. It should be noted that when at least one node is online in the distributed cluster, all online nodes can periodically run the heartbeat thread to perform the following steps:

[0073] Step S21: Check the existence status of the master node in the master node table, and if the existence status is not found, participate in the master node election, update the master node table with the current node as the master node, and obtain the master node.

[0074] Specifically, after the heartbeat thread starts, the current node of the executing thread first checks the master node table. It can confirm the existence of a master node by checking if the master node identifier information is already stored in the master node table. If the master node identifier information in the master node table is empty, then the current cluster does not currently have a master node; if the master node identifier information is already in the master node table, then the cluster already has a master node. In some specific implementations, the existence of a master node can also be confirmed by the content of a specific field in the master node table. For example, an "existence status" field can be set in the master node table to indicate whether a master node exists. In some other implementations, an "election status" field can be set in the master node table to indicate the master node status. This field can include states such as "master node election not started," "master node election in progress," and "master node election completed." Here, "not started" is considered as "master node not existing," while "election in progress" and "election completed" are both considered as "master node existing."

[0075] If the master node's existence status in the master node table is negative, then the cluster does not have a master node. The current node can participate in the election of a master node, and by writing the current node as the master node into the master node table, the current node becomes the cluster's master node. It should be noted that writing the current node as the master node into the master node table involves a status update; that is, the update operation will only succeed if the status determined at the time of the update operation matches the actual status of the corresponding field in the database. If a master node already exists in the master node table when the write operation is performed, the write operation cannot be completed.

[0076] In the technical solution provided in this disclosure, a master node table is set in the database that all nodes in the cluster are connected to. Nodes in the cluster can perform heartbeats by executing a heartbeat thread and confirm the existence of a master node based on the master node table. If no master node exists, the current node can write itself as the master node into the master node table of the database. In this way, nodes in the cluster can achieve master node election and updates through communication with the database. This is easy to configure and implement, and eliminates the need to store information about other nodes, greatly reducing the hardware requirements of the distributed cluster on servers or computers. It also eliminates the need for inter-node communication to confirm the master node, reducing the consumption of inter-node communication resources and improving the management efficiency of the distributed system for cluster nodes. Furthermore, as long as at least one node in the cluster is online, the election of a master node can be achieved by periodically running a heartbeat thread, eliminating the need for additional system configuration for application detection of the master node and simplifying the cluster's working mode.

[0077] It should be noted that, in the embodiments of this disclosure, the term "current node" refers to the node executing the current heartbeat thread. The current time can be the time the current node executes the heartbeat thread. In some specific implementations, the current time can be determined based on the database time.

[0078] In one embodiment, the master node information includes master node identification information and master node heartbeat time, such as... Figure 2 As shown, updating the master node table by setting the current node as the master node includes:

[0079] Step S212: Based on the current node information and the current time, update the master node identifier information and the master node heartbeat time with status, and end the current heartbeat thread.

[0080] Specifically, the current node information and current time can be written into the corresponding fields in the master node table to update the master node identification information and master node heartbeat time, and end the current heartbeat thread. It should be noted that this update is a state-based update; that is, the update operation will only succeed if the state determined at the time of the update operation matches the actual state of the corresponding field in the database. For example, the current node checks whether the master node identification information in the master node table is empty to confirm the existence of a master node in the cluster. If the master node identification information is empty, the current node performs the operation of writing the current node information and current time into the master node table. If the master node identification information in the database master node table is empty, the write operation completes successfully; if the master node identification information in the master node table is not empty when the write operation is performed, the write operation cannot be completed.

[0081] In the above embodiments, data modification of the master node table in the database is achieved through state updates. If multiple nodes write data to the master node table in the database at the same time, the data writing operations of other nodes will fail after one node succeeds, thus avoiding the data competition problem caused by multiple nodes modifying the same data in the database.

[0082] In one embodiment, such as Figure 3 As shown, when the existence state is yes, the processing steps further include:

[0083] Step S22: Detect whether the heartbeat time of the master node has expired. If it has expired, change the existence status to no and end the current heartbeat thread.

[0084] And / or,

[0085] Step S23: Detect whether the master node heartbeat time has expired. If it has not expired, confirm whether the master node identification information is consistent with the current node information. If they are consistent, update the master node heartbeat time to the current time and end the current heartbeat thread.

[0086] Specifically, if a master node exists in the current cluster, the heartbeat time recorded in the master node table can be compared with a preset threshold. If the master node's heartbeat time exceeds the threshold, it is considered that the master node's heartbeat has timed out, the existence status of the master node in the master node table is changed to "no," and the current heartbeat thread ends. In some specific implementations, if the master node's heartbeat times out, the master node identifier information in the master node table can be cleared directly. In some other implementations, if the master node table has a field indicating the master node's status, the content of this field can be modified accordingly, for example, changing "master node election completed" to "master node election not started."

[0087] If the master node's heartbeat has not timed out, it can be confirmed whether the master node identifier information recorded in the master node table is consistent with the current node's information. If they are consistent, then the current node is the master node registered in the master node table. At this time, the master node's heartbeat time in the master node table is modified to the current time, and the current heartbeat thread ends. If the master node identifier information is inconsistent with the current node's information, then the current node is not the master node, and the master node table does not need to be modified; the current heartbeat thread can be ended directly.

[0088] In the above embodiments, nodes within the cluster can continuously monitor the status of the cluster master node by running a heartbeat thread. If the master node is abnormal or the heartbeat times out, the existence status of the master node in the master node table can be updated promptly, and the master node can be reset to facilitate a new master node election, reducing the probability of accidents. Furthermore, if the master node's heartbeat has not timed out, it can confirm whether the current node is the master node, and with the current node cleared as the master node, the heartbeat time of the master node can be refreshed using the current time, thus updating the working status of the cluster master node in a timely manner.

[0089] In one embodiment, before checking the existence status of the master node in the master node table, such as Figure 4 As shown, the processing steps further include:

[0090] Step S20: Obtain the cluster node registry stored in the database, and check whether the current node has completed registration in the cluster node registry. If the registration has not been completed, register the current node and end the current heartbeat thread.

[0091] The cluster node registry can be maintained by a database to record information about each node managed by the cluster. The information recorded in the cluster node registry may include each node's heartbeat time, ID, and other information. In some other implementations, the cluster node registry may also include fields such as whether it is a master node to indicate the node's identity.

[0092] Specifically, after the heartbeat thread starts, the current node can first retrieve the cluster node registry from the database and check whether its own node information has been recorded in the cluster node registry. If its own node information does not exist in the table, it first writes its own node identifier information and / or node heartbeat time, etc., into the table to complete the registration of the current node. After registration is completed, the current heartbeat thread ends. If the current node's information already exists in the table, the current node has completed registration and can continue to execute the subsequent steps of detecting the master node. The node identifier information can be the node's ID.

[0093] In the above embodiments, the node executing the heartbeat thread can first check whether it has registered in the cluster according to the cluster node registry. Unregistered nodes register first and do not participate in the election of the master node in the current thread round. In this way, unfamiliar nodes can be intercepted through the cluster node registry, preventing untrusted nodes from directly becoming the master node. Only after a node completes registration can it become the master node, reducing the risk of chaos caused by master node failure. In addition, unregistered nodes end their current thread round immediately after registration, which can avoid too many operations within the thread cycle and reduce the application's runtime burden. Furthermore, the current node's identification information and heartbeat time can be written to the cluster node registry to complete the node registration. In this way, the cluster node registry contains the node information and heartbeat time required by the master node table, and the cluster node registry in the database can be associated with the master node table. For example, when writing data to the master node table, data recorded in the cluster node registry is required, further improving the security of distributed cluster management.

[0094] In one embodiment, the cluster node registry stores the heartbeat times of each node in the distributed cluster, such as... Figure 5 As shown, after obtaining the master node table, the following steps are also included:

[0095] Step S12: Control the master node to monitor the registry of the cluster nodes, identify the nodes whose heartbeat timeout has expired as abnormal nodes, and send the information of the abnormal nodes to the designated receiving end.

[0096] The designated receiving end can be a pre-specified server or computer, or other similar device.

[0097] Specifically, the master node can be controlled via data commands, scripts, programs, etc., to monitor the cluster node registry in the database. If a node with a timeout is found in the table, it can be identified as an abnormal node, and its ID, heartbeat time, and other information can be sent to a designated receiver. In some other implementations, the abnormal node can be prevented from participating in the master node election process by blocking or deleting its registration.

[0098] In the above embodiments, the master node can monitor all nodes in the cluster node registry. If a node's heartbeat times out, the master node can identify the node as an abnormal node and send the abnormal node information to a designated party. In this way, by using the master node to monitor other nodes within the cluster, abnormal nodes can be discovered and reported, allowing for timely transfer or reassignment of tasks for those abnormal nodes, thus avoiding impact on the overall task progress of the cluster.

[0099] In one embodiment, such as Figure 6 As shown, after obtaining the master node table, the following steps are also included:

[0100] Step S14: Modify the existence status of the master node in the master node table according to the preset first instruction.

[0101] Specifically, after obtaining the master node table, the master node table in the database can be modified using preset commands, such as clearing the master node identifier information in the master node table or changing the content of the field in the master node table that indicates the existence status of the master node to no.

[0102] And / or,

[0103] Step S16: Force update the information of the master node according to the preset second instruction.

[0104] Specifically, the master node identification information in the master node table can be forcibly updated through preset instructions. For example, the node identification information of a specific node can be written into the master node table through an instruction, making that specific node the master node of the cluster, and the current time can be written into the table as the heartbeat time of the master node.

[0105] In the above embodiments, modifying the information in the master node table via instructions can clear the master node identifier information, causing the cluster to enter a masterless state. A new round of heartbeat threads can then elect a master node, or a normal node can be directly written into the master node table as the master node. This way, when it is known that the master node may be abnormal, the cluster's business processes can be prevented from being affected.

[0106] In one embodiment, the heartbeat time of the current node can be updated to the current time before each heartbeat thread ends.

[0107] Specifically, the heartbeat time of a node is used to indicate the online status of the node. If the current node can execute the heartbeat thread, it means that the node has not encountered any abnormalities. During each round of the heartbeat thread, the heartbeat time of the node in the cluster node registry can be updated before the end of the current round of the thread, and modified to the current time based on the database time.

[0108] In the above embodiments, before each heartbeat thread ends, the node executing the heartbeat thread can refresh its own heartbeat time, modifying it to the current time. This allows the unified time of the database to re-represent the online status of nodes, avoiding time discrepancies caused by differences in time standards between different nodes and improving the management efficiency of the distributed cluster.

[0109] In one embodiment, the master node identifier information in the master node table can be cleared using a preset instruction. In some other embodiments, if the master node table contains a field indicating the master node status, the master node status can also be forcibly modified using an instruction, for example, changing the master node status to "master node election has not started".

[0110] To further demonstrate the beneficial effects of this plan, the following will be combined with... Figure 7 A specific heartbeat thread in one embodiment will be described:

[0111] In one embodiment, nodes in the cluster connect to a common database that maintains a cluster node registry and a master node table. The cluster node registry includes fields such as node ID, heartbeat time, and whether it is a master node. The master node table includes fields such as master node ID, election status, and master node heartbeat time. The election status includes three states: not started, in election, and completed election, representing the current status of the master node in the cluster.

[0112] Figure 7 This is a schematic diagram illustrating the heartbeat thread running on the cluster nodes in the above embodiment. For example... Figure 7 As shown, after the heartbeat thread starts, the running node first checks whether it has registered. If not, it can insert a record of the current node into the cluster node registry in the database using its own node IP as the key, and use the current time as the heartbeat time to complete the registration. If registration is complete, the status of the manager (master node) can be determined based on the election status in the master node table, specifically in the following three cases:

[0113] 1. When the election status is "Election Not Started", there is no master node in the cluster. The current node attempts to conduct an election, writes the current node ID as the master node ID into the master node table, writes the current time as the master node heartbeat time into the master node table, changes the election status to "Election in Progress", and terminates the thread.

[0114] 2. When the election status is "Election in Progress," the cluster is currently conducting a master node election. By comparing the master node ID with the current node ID, it first confirms whether the current node is the master node in the election. If it is not the current node, it checks whether the heartbeat of the master node in the election has timed out. If it has, the election process is reset (the election status in the master node table is changed to "Election Not Started") and the thread ends. If it has not timed out, the thread ends directly. If the current node is the master node in the election, it first confirms whether the current node's heartbeat has timed out. If the current node's heartbeat has timed out, the election process is reset (the election status in the master node table is changed to "Election Not Started") and the thread ends. If it has not timed out, the current node is successfully elected as the master node, and the election status in the master node table is changed to "Election Completed." After the change is completed, the thread ends.

[0115] 3. When the election status is "Election Completed", the cluster already has a successfully elected master node. First, check if the master node's heartbeat timeout has occurred. If it has, reset the election process (change the election status in the master node table to "Election Not Started") and then end the thread. If it has not timed out, confirm whether the master node is the current node. If the master node is the current node, update the master node's heartbeat time in the master node table to the current time and then end the thread. If the master node is not the current node, then end the thread directly.

[0116] It should be noted that the operations involving modifications to the cluster node registry and master node table in the above embodiments all involve state updates. If the state has changed when modifying the relevant data in the database, the operation cannot be completed. Furthermore, regardless of the election state among the three states mentioned above, the heartbeat time of the current machine can be refreshed to the current time before the thread terminates.

[0117] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.

[0118] According to another aspect of the embodiments of this disclosure, such as Figure 8 As shown, a master node election device for a distributed cluster is also provided, comprising:

[0119] The information acquisition module 30 is used to acquire the master node table; the master node table is used to store the information of the master node, and the master node table is stored in the database of a specified node in the distributed cluster;

[0120] Thread management module 40 is used to periodically run a preset heartbeat thread during the operation of the distributed cluster, controlling the nodes in the distributed cluster to execute specific processing steps; the thread management module includes the following units:

[0121] The master node confirmation unit 42 is used to control the node to check the existence status of the master node in the master node table;

[0122] The master node election unit 43 is used to control the node to participate in the master node election when the existence status is not specified, and update the master node table by making the current node the master node.

[0123] In one embodiment, such as Figure 9 As shown, the information of the master node includes master node identification information and master node heartbeat time, and the master node election unit 43 includes:

[0124] The master node update unit 432 is used to control the node to update the master node identification information and the master node heartbeat time with status based on the current node information and the current time.

[0125] In one embodiment, such as Figure 10 As shown, the thread management module 40 also includes:

[0126] The master node reset unit 44 is used to control the node to detect whether the master node heartbeat time has expired. If it has expired, the node is controlled to change the existence status to no.

[0127] And / or,

[0128] The heartbeat update unit 45 is used to control the node to confirm whether the master node identification information is consistent with the current node information. If they are consistent, the master node heartbeat time is updated to the current time.

[0129] In one embodiment, such as Figure 11 As shown, the thread management module 40 further includes:

[0130] The node registration unit 41 is used to obtain the cluster node registry stored in the database, detect whether the current node has completed registration in the cluster node registry, and if the registration has not been completed, register the current node.

[0131] In one embodiment, such as Figure 12 As shown, the device further includes:

[0132] The node monitoring module 32 is used to control the master node to monitor the cluster node registry, identify nodes whose heartbeat timeout has expired as abnormal nodes, and send the information of the abnormal nodes to a designated receiving end.

[0133] In one embodiment, such as Figure 13 As shown, the device further includes:

[0134] Master node modification module 34 is used to modify the existence status of master nodes in the master node table according to a preset first instruction;

[0135] And / or,

[0136] The master node designation module 36 is used to forcibly update the information of the master node according to a preset second instruction.

[0137] Specific limitations regarding the aforementioned master node election device can be found in the limitations of the aforementioned master node election method described above, and will not be repeated here. According to the aforementioned master node election method, the master node election device can be supplemented with a first module, a second module, etc., to implement the steps in the corresponding method embodiments. Each module in the aforementioned master node election device can be implemented entirely or partially through software, hardware, or a combination thereof. Each module can be embedded in or independent of the processor in a computer device in hardware form, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.

[0138] According to another aspect of the embodiments of this disclosure, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 14 As shown, the computer device includes a processor, memory, communication interface, display screen, and input devices connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, carrier networks, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it implements the aforementioned master node election method. The display screen can be an LCD screen or an e-ink screen. The input devices can be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the computer device casing, or an external keyboard, touchpad, or mouse.

[0139] Those skilled in the art will understand that Figure 14The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0140] In one embodiment, a computer device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above method embodiments.

[0141] According to another aspect of the present disclosure, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps in the above-described method embodiments.

[0142] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments described above. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, or optical storage, etc. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in this application can include at least one relational database and non-relational database. Non-relational databases can include blockchain-based distributed databases, etc., and are not limited thereto. The processors involved in the various embodiments provided in this application may be general-purpose processors, central processing units, graphics processors, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, etc., and are not limited thereto.

[0143] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0144] The embodiments described above are merely illustrative of several implementations of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this application. It should be noted that those skilled in the art, upon considering the specification and practicing the invention disclosed herein, will readily conceive of other embodiments of this disclosure. This disclosure is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and embodiments are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the claims.

[0145] It should be understood that this disclosure is not limited to the precise structures already described and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope.

Claims

1. A method for electing a master node in a distributed cluster, characterized in that, include: Retrieve the master node table; The master node table is used to store information about the master node, and the master node table is stored in the database of a specified node in the distributed cluster; The information of the master node includes the master node identification information and the master node heartbeat time; During the operation of the distributed cluster, a preset heartbeat thread is run periodically. The heartbeat thread controls the nodes in the distributed cluster to perform the following processing steps: check the existence status of the master node in the master node table, and if the existence status is not positive, participate in the master node election, update the master node identification information and the master node heartbeat time with status according to the current node information and the current time, end the current round of the heartbeat thread, and the thread ends after the master node is obtained. If the existence status is "yes", check if the master node heartbeat timeout has occurred. If it has, change the existence status to "no", end the current heartbeat thread, and reset the election process. The status update includes the current node checking if the master node identifier information in the master node table is empty to confirm the existence of a master node in the cluster. If the master node identifier information is empty, perform the operation of writing the current node information and the current time to the master node table. If the master node identifier information in the database master node table is empty, the update operation is successfully completed.

2. The method according to claim 1, characterized in that, If the existence status is yes, the processing steps further include: Check if the master node heartbeat time has expired. If it has not expired, confirm whether the master node identification information is consistent with the current node information. If they are consistent, update the master node heartbeat time to the current time and end the current heartbeat thread.

3. The method according to claim 1 or 2, characterized in that, Before checking the existence status of the master nodes in the master node table, the processing steps further include: Obtain the cluster node registry stored in the database, check whether the current node has completed registration in the cluster node registry, if not, register the current node and end the current heartbeat thread.

4. The method according to claim 3, characterized in that, The cluster node registry stores the heartbeat times of each node in the distributed cluster. After obtaining the master node table, it also includes: The master node monitors the registry of the cluster nodes, identifies nodes whose heartbeat timeouts as abnormal nodes, and sends the information of the abnormal nodes to a designated receiving end.

5. The method according to claim 1, characterized in that, After obtaining the master node table, the following is also included: Modify the existence status of the master node in the master node table according to the preset first instruction; And / or, The information of the master node is forcibly updated according to the preset second instruction.

6. The method according to claim 1, characterized in that, Before the end of each heartbeat thread, the heartbeat time of the current node is updated to the current time.

7. A master node election device for a distributed cluster, characterized in that, include: The information acquisition module is used to retrieve the master node table; The master node table is used to store information about the master node, and the master node table is stored in the database of a specified node in the distributed cluster; The information of the master node includes the master node identification information and the master node heartbeat time; The thread management module is used to periodically run a preset heartbeat thread during the operation of the distributed cluster, controlling the nodes in the distributed cluster to execute specific processing steps; the thread management module includes the following units: The master node confirmation unit is used to control the node to check the existence status of the master node in the master node table. The master node election unit is used to control the node to participate in the master node election when the existence status is "no". Based on the current node information and the current time, it performs a state-based update on the master node identifier information and the master node heartbeat time, ends the current heartbeat thread, and terminates the thread after a master node is obtained. When the existence status is "yes", it checks whether the master node heartbeat time has timed out. If it has, it changes the existence status to "no", ends the current heartbeat thread, and resets the election process. The state-based update includes the current node checking whether the master node identifier information in the master node table is empty to confirm the existence of a master node in the cluster. If the master node identifier information is empty, it performs an operation to write the current node information and the current time to the master node table. If the master node identifier information in the database master node table is empty, the update operation is successful.

8. The apparatus according to claim 7, characterized in that, The thread management module also includes: The master node reset unit is used to control the node to detect whether the master node heartbeat time has expired. If it has expired, the node is controlled to change the existence status to no. And / or, The heartbeat update unit is used to control the node to confirm whether the master node identification information is consistent with the current node information. If they are consistent, the master node heartbeat time is updated to the current time.

9. The apparatus according to claim 7 or 8, characterized in that, The thread management module also includes: The node registration unit is used to obtain the cluster node registry stored in the database, detect whether the current node has completed registration in the cluster node registry, and if not, register the current node.

10. The apparatus according to claim 9, characterized in that, The cluster node registry stores the heartbeat times of each node in the distributed cluster, and the device further includes: The node monitoring module is used to control the master node to monitor the registry of the cluster nodes, identify nodes whose heartbeat timeout has expired as abnormal nodes, and send the information of the abnormal nodes to a designated receiving end.

11. The apparatus according to claim 7, characterized in that, The device further includes: The master node modification module is used to modify the existence status of the master nodes in the master node table according to a preset first instruction. And / or, The master node designation module is used to force an update of the information of the master node according to a preset second instruction.

12. The apparatus according to claim 8, characterized in that, The heartbeat update unit is also used to update the heartbeat time of the current node to the current time before the end of each round of heartbeat thread.

13. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.

14. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.

15. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Method for determining main node in cluster, electronic equipment and storage medium

    CN111538763A