Cluster master election method and apparatus, computer device, medium, and product

By synchronizing all leader election requests during the cluster leader election process, the problems of cluster leader election errors and excessive time are solved, ensuring that the elected master node is actually needed by the cluster and improving the leader election effect.

CN119484229BActive Publication Date: 2026-01-20CHINA TELECOM CLOUD TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411691878.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-25
Publication Date
2026-01-20
Estimated Expiration
2044-11-25

AI Technical Summary

Technical Problem

During the cluster leader election process, due to network latency, node failures, and data inconsistencies, existing technologies are prone to errors in leader election or excessively long election times, resulting in poor leader election performance.

Method used

When the cluster leader election conditions are detected, a first target leader election request is sent to the leader election request sharing area. All leader election requests are read, and the cluster leader election is performed based on these requests. This ensures that all leader election requests are synchronized in the same time slot, representing the leader selection needs of all nodes in the cluster.

Benefits of technology

It improves the success rate of cluster leader election, ensuring that the elected leader node is the one actually needed by the cluster, and avoiding problems such as leader election errors or excessively long election times caused by network latency, node failures, etc.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119484229B_ABST
    Figure CN119484229B_ABST
Patent Text Reader

Abstract

The application relates to a cluster master selection method and device, computer equipment, medium and product. The cluster master selection method applied to a first node of a cluster comprises the following steps: in the case that a cluster master selection condition is detected, a first target master selection request is sent to a master selection request sharing area in a cluster master selection state; all master selection requests in a first synchronization time slot to which the first target master selection request belongs are read from the master selection request sharing area, wherein the all master selection requests are composed of master selection requests sent by all master selection nodes of the cluster; and cluster master selection is performed based on the all master selection requests. The master selection effect of the cluster master selection is improved by using the method.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of cluster management, and in particular to a cluster master election method and device, a computer device, a computer readable storage medium and a computer program product. BACKGROUND

[0002] With the development of science and technology, clustering has been widely implemented in many industries. The Active-Standby (master-standby) mode has become the mainstream mode of clustering due to its high availability, high fault transfer efficiency and low resource utilization. However, the cluster master election problem inevitably arises in the process of cluster management.

[0003] Currently, in the process of cluster master election, a selection period is usually planned for the sending of master election requests by cluster nodes, i.e., at a certain point in time, the cluster node requested by a single node in the cluster is selected as the master node. However, since different nodes in the cluster have selection capabilities, if network delays, node failures and data inconsistencies occur, the master node selected based on a single node request may not be the actual required node, which may result in errors in cluster master election or long cluster master election times. Therefore, the current cluster master election effect is poor. SUMMARY

[0004] Therefore, it is necessary to provide a cluster master election method, device, computer device, computer readable storage medium and computer program product for improving the cluster master election effect.

[0005] In a first aspect, the present application provides a cluster master election method applied to a first node of a cluster, comprising:

[0006] In the case where it is detected that the cluster master election condition is met, a first target master election request is sent to a master election request sharing area in a cluster master election state;

[0007] All master election requests in the first synchronization time slot to which the first target master election request belongs are read from the master election request sharing area, wherein the all master election requests are composed of master election requests sent by all master nodes of the cluster;

[0008] The cluster master election is performed based on the all master election requests.

[0009] In one embodiment, the cluster master election condition comprises one of the following:

[0010] The master node state of the first node is not updated in the first synchronization time slot;

[0011] The heartbeat signal of the cluster master node is detected to be expired in the first synchronization time slot;

[0012] detecting a second master election request sent by a second node in the first synchronization time slot, wherein the second node is any master election node in the cluster except the first node.

[0013] In one of the embodiments, the all master election requests include a first target master election request and other master election requests sent by the second node to the master election request sharing area; and the cluster master election based on the all master election requests includes:

[0014] if the first cluster node requested by the all master election requests is detected to be online, first request information of the first target master election request and second request information of the other master election requests are acquired respectively, and the cluster master election is performed based on a comparison result of the first request information and the second request information;

[0015] if the first cluster node is not detected to be online, a second synchronization time slot for the cluster master election is determined, a second target master election request generated in the second synchronization time slot is sent to the master election request sharing area, and the cluster master election is performed based on the second target master election request.

[0016] In one of the embodiments, the cluster master election based on the second target master election request includes:

[0017] if the master node change condition is detected to be met, a request number of the second target master election request is detected according to third request information of the second target master election request;

[0018] if the request number is greater than a first preset request number threshold, a second cluster node requested by the second target master election request is taken as the cluster master node.

[0019] In one of the embodiments, the master node change condition includes one of the following:

[0020] a third master election request sent by the second node is not detected in the second synchronization time slot;

[0021] a third master election request is detected in the second synchronization time slot, and a request number of the third master election request is greater than a second preset request number threshold, wherein the second preset request number threshold is less than the first preset request number threshold.

[0022] In one of the embodiments, the method further includes one of the following:

[0023] changing a current running state from the cluster master election state to a master node running state;

[0024] change the current running state from the cluster master election state to a slave node running state;

[0025] change the current running state to an offline state.

[0026] In a second aspect, the present application further provides a cluster master election method and device, applied to a first node of a cluster, comprising:

[0027] a sending module, configured to send a first target master election request to a master election request sharing area in a cluster master election state when it is detected that a cluster master election condition is met;

[0028] a reading module, configured to read all master election requests of the master election request sharing area within a first synchronization time slot to which the first target master election request belongs, wherein the all master election requests are composed of master election requests sent by all master election nodes of the cluster;

[0029] a cluster master election module, configured to perform cluster master election based on the all master election requests.

[0030] In a third aspect, the present application further provides a computer device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the following steps when executing the computer program:

[0031] send a first target master election request to a master election request sharing area in a cluster master election state when it is detected that a cluster master election condition is met; read all master election requests of the master election request sharing area within a first synchronization time slot to which the first target master election request belongs, wherein the all master election requests are composed of master election requests sent by all master election nodes of the cluster; and perform cluster master election based on the all master election requests.

[0032] In a fourth aspect, the present application further provides a computer readable storage medium, which stores a computer program, and the computer program implements the following steps when executed by a processor:

[0033] send a first target master election request to a master election request sharing area in a cluster master election state when it is detected that a cluster master election condition is met; read all master election requests of the master election request sharing area within a first synchronization time slot to which the first target master election request belongs, wherein the all master election requests are composed of master election requests sent by all master election nodes of the cluster; and perform cluster master election based on the all master election requests.

[0034] In a fifth aspect, the present application further provides a computer program product, comprising a computer program, and the computer program implements the following steps when executed by a processor:

[0035] In a case where it is detected that the cluster master election condition is met, a first target master election request is sent to the master election request sharing area in the cluster master election state; all master election requests in a first synchronization time slot to which the first target master election request belongs are read from the master election request sharing area, wherein the all master election requests are composed of master election requests sent by all master nodes of the cluster; and cluster master election is performed based on the all master election requests.

[0036] The cluster master election method, device, computer device, computer readable storage medium, and computer program product are applied to a first node of a cluster. In a case where it is detected that the cluster master election condition is met, a first target master election request is sent to the master election request sharing area when the first node is in the cluster master election state. Then, all master election requests in a first synchronization time slot to which the first target master election request belongs are read from the master election request sharing area, wherein the all master election requests are composed of master election requests sent by all master nodes of the cluster, that is, master election requests sent by all master nodes of the cluster in the first synchronization time slot are obtained through the master election request sharing area. Finally, cluster master election is performed through the all master election requests. Since the all master election requests are in the same time slot, that is, master election requests sent by different master nodes of the cluster are synchronized in time, the all master election requests can objectively represent the master node selection requirements of all nodes in the cluster for the first synchronization time slot, and the purpose of ensuring that the master node obtained through cluster master election is the actual required master node of the cluster can be achieved. Therefore, the technical defect that the master node selected by relying on a single node request is not the actual required node due to the selection capability of different nodes in the cluster, network delay, node failure, and data inconsistency, and the like, and the cluster master election is prone to error or takes a long time can be overcome. Therefore, the master election effect of cluster master election is improved. BRIEF DESCRIPTION OF DRAWINGS

[0037] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related art, the drawings needed to be used in the embodiments or the related art description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without any creative effort.

[0038] Figure 1 A flowchart of the cluster master election method in one embodiment;

[0039] Figure 2 A node state diagram of the cluster master election method in one embodiment;

[0040] Figure 3A flowchart of a cluster master election method in another embodiment;

[0041] Figure 4 A state diagram of a cluster state of a cluster master election method in an embodiment;

[0042] Figure 5 A flowchart of a cluster master election flow of a cluster master election method in an embodiment;

[0043] Figure 6 A structure block diagram of a cluster master election apparatus in an embodiment;

[0044] Figure 7 An internal structure diagram of a computer device in an embodiment. DETAILED DESCRIPTION

[0045] In order to make the purposes, technical solutions and advantages of the present application clearer, the present application is further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and not used to limit the present application.

[0046] Firstly, it should be understood that the clustering of multi-service nodes can effectively deal with single point failure, that is, the availability of cluster services can still be guaranteed in the case of any abnormal node in the cluster, and in specific service scenarios, the Active-Standby mode is usually used, that is, a node in the cluster acts as a Master node, which is responsible for resource allocation and task scheduling and other transactions that require global centralized processing, while other nodes in the cluster act as Slave nodes, which are ready to compete to become a new Master when the Master is unavailable. At present, Raft algorithm and ZAB algorithm are usually used to solve the master selection problem of the master-slave cluster, in which process, on the one hand, the cost problem needs to be considered, for example: 1) reserved resources: CPU, memory, network IO and disk overheads; 2) maintenance management: reasonable parameter configuration can make the service run efficiently and stably, but for open source services, there is a certain maintenance management threshold, for example, when using ETCD (EtcdDistributed Database, distributed database) as a cluster coordination service, the parameter needs to be configured to specify the number of operations or the running time to compress the WAL snapshot to reduce the memory occupancy of ETCD, otherwise the configuration may continuously grow in memory; 3) fault impact: if ETCD service and other services are deployed on the same server, ETCD exception (such as memory growth) may affect other services, and if ETCD is deployed on several servers, the cost is too high, and on the other hand, the effect of the existing technology on cluster master selection, that is, if there is network delay, node failure or data inconsistency, the requests of different nodes in the cluster may not be able to be synchronized feedback, which may lead to cluster master selection error or long cluster master selection time, so there is an urgent need for a cluster master selection method to improve the effect of cluster master selection.

[0047] In one embodiment, as Figure 1As shown, a cluster master election method is provided, and in the embodiment, the method is applied to a terminal for example, and the terminal includes but is not limited to a personal computer, a notebook computer, a smart phone, a tablet computer and the like, and the terminal includes a sending module, a reading module and a cluster master election module, the sending module is configured to send a first target master election request to a master election request sharing area in a cluster master election state when it is detected that a cluster master election condition is met, the reading module is configured to read all master election requests in a first synchronization time slot to which the first target master election request belongs, wherein all the master election requests are composed of master election requests sent by all master nodes of the cluster, and the cluster master election module is configured to perform cluster master election based on all the master election requests, and through information interaction among the sending module, the reading module and the cluster master election module, in the process of performing cluster master election, the master election request sent by different nodes of the cluster to the master election request sharing area in the same synchronization time slot is relied on, that is, it is ensured that the master election requests sent by different master nodes of the cluster are synchronized in time, and then the purpose of ensuring that the master node obtained by cluster master election is the actual required master node of the cluster can be achieved, therefore, the cluster master election problem caused by the time synchronization of different nodes of the cluster in the cluster master election process can be avoided, and the master election effect of performing cluster master election can be improved. It can be understood that the method can also be applied to a server, and can also be applied to a system including a terminal and a server, and is implemented through interaction of the terminal and the server. In the embodiment, the method includes the following steps 202 to 206. Wherein:

[0048] Step 202, when it is detected that a cluster master election condition is met, a first target master election request is sent to a master election request sharing area in a cluster master election state.

[0049] It should be noted that in the face of complex coordination content in the cluster master selection process, the cluster master selection method deployed in this embodiment can avoid the heavy of the cluster master selection process, thereby reducing the use cost, and can also bring the improvement of the master selection effect. It can be understood that the cluster master selection method can rely on NTP (Network Time Protocol). NTP refers to a protocol used to synchronize computer time. It can make the computer and the NTP server do high-precision time correction. The cluster master selection method deployed in this embodiment relies on NTP to ensure the time synchronization accuracy between cluster nodes. The cluster master selection condition is used to trigger the first node of the cluster to participate in the cluster master selection. Specifically, it can be triggered by the instruction input or clicked by the user on the first node. The cluster can include multiple nodes. The first node can be a master node or a slave node. The master selection request sharing area is used to provide services for reading and writing data of multiple servers (nodes). Specifically, it can be a network file system, FTP, shared memory, database, etc. The nodes in the cluster can also use the master selection request sharing area to publish the master selection request and view the master selection requests of other nodes. It can be understood that the data in the master selection request sharing area can be periodically cleaned or permanently saved. The master selection request is used to represent the request object of the master selection. It can be understood as the recommendation content published by the node in the cluster at a certain time, which can also be called "master selection proposal". The master selection requests sent to the master selection request sharing area by the same node at different times or by different nodes at the same time can form a sequence through numbering. For example, in an implementable manner, a single master selection request can be composed of four parts: time slot serial number, initiator, master selection request content, and request number. The time slot serial number is guaranteed by NTP to ensure time synchronization accuracy. The nodes in the cluster are numbered uniformly according to the configured time slot granularity and the configured start time. The data type can be 64-bit unsigned integer. The initiator is used to identify the identity of the requester who initiates the master selection request, that is, the initiator is the node identifier of the node itself. The master selection request content refers to the master node selected by the node in the cluster according to the predetermined master selection rule. The predetermined master selection rule can be triggered based on the node serial number or the start time, etc. The request number is used to distinguish and identify different master selection requests. All nodes in the cluster use the same rule to number. The larger the number is, the newer the master selection request is. All nodes in the cluster can obtain the latest number of the current master selection request through the master selection request sharing area. In the case that the master selection request numbers of all nodes in the cluster are the same and the master selection request contents are consistent, it can be considered that the master selection request is consistent, that is, the cluster master selection is successful.

[0050] It should be noted that the first target master election request refers to the master election request sent by the first node. It can be understood that after the cluster starts the master election process, any node in the cluster needs to complete the cluster master election process within a single time slot. In the case of a single node meeting the cluster master election condition to trigger the cluster master election process, the following four parts will be involved: 1) Time synchronization: Time synchronization between cluster nodes is based on NTP to ensure that the time base of each node is consistent. The size of the time slot is related to the accuracy of NTP. The higher the accuracy, the smaller the time slot granularity can be. For example, if the agreed time slot granularity is 4 seconds, the alignment of different nodes in the cluster can be simplified to an initial time alignment every 4 seconds. In a single time slot granularity, any node is divided into a master election request submission stage and a master election stage, that is, all nodes in the cluster need to submit master election requests in the master election request submission stage, and the master node needs to obtain all master election requests from the master election request sharing area. 2) Master election request sharing area: The master election request sharing area is responsible for receiving master election requests from all nodes in the cluster and for any node in the cluster to read master election requests from other nodes in the cluster. It can be understood that the master election request sharing area needs to ensure that the time slot granularity is greater than the time for all nodes to complete row data read and write. 3) Heartbeat: The master node of the cluster needs to continuously update the heartbeat record in the master election request sharing area based on the time interval. The heartbeat record carries the time slot identifier. Other slave nodes in the cluster can determine whether the master node is abnormal by comparing the time slot of the heartbeat update record with the current time slot, and further determine whether to send the first target master election request to the master election request sharing area. 4) State machine: All nodes in the cluster run independently according to their own state machines, discover and participate in the master election process through time slot driven timing detection tasks, and complete automatically. It can be understood that in the case of starting the master election process of the cluster, the current business role is stopped until the master election is successful. Each node of the cluster is driven by the time slot to ensure that the cluster as a whole is natural and has state management. The symbol that the cluster starts the master election process can be that a node in the cluster sends a master election request to the master election request sharing area. The state machine of a single node has three states, namely master node state (M state), slave node state (S state) and cluster master election state (V state). Referring to Figure 2 , Figure 2 The figure shows the node state of the cluster node. The node in the master node state needs to perform a timing task and identify new master election requests. The node needs to go through the master process and reach an agreement among nodes to be selected as the master node. If it cannot continue to operate as the master node during business operation, it needs to actively switch to the S state and no longer update the heartbeat record in the master election request sharing area. The node in the slave node state only performs a timing detection task based on the time slot to identify whether the current master node is expired. The node defaults to the S state. The node needs to be switched to the S state first when exiting. The node in the cluster master election state is automatically triggered by the timing detection task to initiate or participate in the master election process until the master election is successful.

[0051] As an example, step 202 comprises: in the case of detecting that the cluster master election condition is met, switching the current running state of the first node to the cluster master election state, and generating a selection request content according to a preset rule, encapsulating the master election request content, the node identifier of the first node, the master election request identifier and the time slot sequence number into a first target master election request, and sending the first target master election request to the master election request sharing area.

[0052] Step 204, reading all master election requests in the first target master election request belonging to the first synchronization time slot in the master election request sharing area, wherein all master election requests are composed of master election requests sent by all master nodes in the cluster.

[0053] It should be noted that in the first target master election request belonging to the first synchronization time slot, in addition to the first node, other nodes in the cluster may also send master election requests to the master election request sharing area, and then all master election requests can be read by analyzing the time slot sequence number in the master election request sharing area without master election request, thereby laying a foundation for the first node to perform cluster master election.

[0054] As an example, step 204 comprises: obtaining the time slot sequence number of all candidate master election requests in the master election request sharing area, and filtering all master election requests in the first target master election request belonging to the first synchronization time slot from all candidate master election requests according to the comparison result between all time slot sequence numbers and the time slot sequence number of the first synchronization time slot to which the first target master election request belongs.

[0055] Step 206, performing cluster master election based on all master election requests.

[0056] It should be noted that if all master election requests only contain the first target master election request sent by the first node, the cluster node requested by the first target master election request can be directly selected as the cluster master node, and if all master election requests contain the first target master election request and other master election requests, the cluster master node can be selected in the case that the master election request contents of the master election requests are consistent.

[0057] As an example, step 206 comprises: in the case of detecting that all master election requests are one master election request, the cluster node requested by the first target master election request is selected as the master node, and in the case of detecting that all master election requests are multiple master election requests, the cluster node with the same master election request content is selected as the master node when the master election request contents of the multiple master election requests are consistent.

[0058] In an implementable manner, in a case where it is detected that all the master election requests are the plurality of master election requests, then in a case where the master election request contents of the plurality of master election requests are consistent, the cluster master election failure information is reported, and in a case where it is detected again that the cluster master election condition is met, the cluster master election process is started.

[0059] In the cluster master election method, in a case where it is detected that the cluster master election condition is met at the first node, a first target master election request is generated based on the master election request content, the node identifier of the first node, the master election request identifier and the time slot sequence number, and the first target master election request is sent to the master election request sharing area, and then after the submission phase of the master election request is completed in the first synchronization time slot, the master election phase is entered, that is, all the master election requests in the first synchronization time slot to which the first target master election request belongs are read in the master election request sharing area, and finally the cluster master election is completed based on all the master election requests. Since all the master election requests are in the same time slot, that is, the master election requests sent by different master election nodes of the cluster are synchronized in time, and then all the master election requests can objectively represent the master node selection requirements of all the nodes in the cluster for the first synchronization time slot, and then the purpose of ensuring that the master node obtained by the cluster master election is the actual required master node of the cluster can be achieved. Instead of relying on the master election request of a single node for cluster master election, the technical defect that the master node selected by relying on a single node request is not the actual required node due to network delay, node failure and data inconsistency of different nodes in the cluster, and the cluster master election is prone to error or takes a long time can be overcome. Therefore, the master election effect of the cluster master election is improved.

[0060] In one embodiment, the cluster master election condition comprises one of the following:

[0061] Failure to update the master node state of the first node in the first synchronization time slot; detecting that the heartbeat signal of the cluster master node expires in the first synchronization time slot; detecting the second master election request sent by the second node in the first synchronization time slot, wherein the second node refers to any master election node in the cluster except the first node.

[0062] It should be noted that in a case where the identity of the first node in the cluster is different, the first node can trigger and start the cluster master election process based on different cluster master election conditions. It can be understood that whether the first node is a master node or a slave node in the cluster, the first node can trigger the detection task based on the time slot, wherein the detection task is to detect whether the cluster master election condition is met.

[0063] As an example, the cluster leader election condition is that, in the case that the first node is a cluster leader node, the first node fails to update the leader node status of the first node to the leader node status shared area at the first synchronization time slot, and the first node switches the leader node status to a follower node status after the leader node status update fails.

[0064] As another example, the cluster leader election condition is that, in the case that the first node is a cluster follower node, the first node reads the heartbeat signal of the cluster leader node from the leader election request shared area at the first synchronization time slot, and detects that the heartbeat signal is expired.

[0065] As another example, the cluster leader election condition is that, in the case that the first node is a cluster leader node or a cluster follower node, the first node detects a second leader election request sent by a second node at the first synchronization time slot, and the second node refers to any leader election node in the cluster except the first node.

[0066] In the embodiment, different cluster leader election conditions are set based on the different identities of the first node in the cluster, and the triggering of the cluster leader election process is ensured, that is, the first node can automatically trigger the participation in the cluster leader election process in various application scenarios, so that the cluster leader election effect is improved, and the leader election limitation of the cluster leader election is reduced.

[0067] In one embodiment, as shown in Figure 3 All leader election requests include the first target leader election request and other leader election requests sent by the second node to the leader election request shared area, and the cluster leader election is performed based on all leader election requests, including:

[0068] In step 302, if it is detected that all leader election requests collectively request the first cluster node to be online, the first request information of the first target leader election request and the second request information of the other leader election requests are acquired respectively, and the cluster leader election is performed based on the information comparison result of the first request information and the second request information.

[0069] It should be noted that in the scenario in which multiple nodes in the cluster participate in the cluster leader election, different processing logics need to be adopted to cope with the cluster leader election process due to the different node requests of different leader election nodes and the different node states of the requested cluster nodes, so as to ensure the smooth progress of the cluster leader election, for example, in an implementable manner, assuming that all nodes in the cluster are online, it indicates that all leader election requests collectively request the first cluster node to be online, and then the cluster leader election can be completed by comparing the leader election request content and the request number in different leader election requests.

[0070] As an example, step 302 comprises: if all nodes in the cluster are detected to be online, determining that the first cluster node requested by all the leader election requests is online, extracting the first request information in the first target leader election request, and extracting the second request information in the other leader election requests, and taking the first cluster node as the cluster master node if the first request information and the second request information are consistent.

[0071] In an implementable manner, if the first request information and the second request information are inconsistent, the timing detection task of the first node is re-timed, and the cluster leader election process is triggered again if the cluster leader election condition is met.

[0072] Step 304: If the first cluster node is not detected to be online, a second synchronization time slot for cluster leader election is determined, a second target leader election request generated in the second synchronization time slot is sent to the leader election request sharing area, and cluster leader election is performed based on the second target leader election request.

[0073] It should be noted that if the first cluster node requested by all the leader election requests is not online, it means that the cluster leader election cannot be completed in the first synchronization time slot, and thus a second synchronization time slot for cluster leader election can be planned again. It can be understood that the first synchronization time slot and the second synchronization time slot are different, and the second synchronization time slot can be one time slot or multiple time slots. For example, assuming that the first synchronization time slot is time slot 1, the second synchronization time slot can be time slot 2, time slot 3, time slot 4, and time slot 5. The second synchronization time slot can be continuous with the first synchronization time slot or discontinuous with the first synchronization time slot. The second target leader election request can be understood as a leader election request sent to the leader election request sharing area in the second synchronization time slot, so as to repeat the cluster leader election process, that is, all the leader election requests in the leader election request sharing area in the second synchronization time slot to which the second target leader election request belongs can be read, and cluster leader election is performed based on all the leader election requests.

[0074] As an example, step 304 comprises: if the first cluster node is not detected to be online, a second synchronization time slot for cluster leader election is determined, a second target leader election request generated in the second synchronization time slot is sent to the leader election request sharing area, and all the leader election requests in the leader election request sharing area in the second synchronization time slot to which the second target leader election request belongs are read, and cluster leader election is performed based on all the leader election requests.

[0075] In this embodiment, different leader election modes are adopted to elect the master node of the cluster based on the different node states of the first cluster node requested by all the leader election requests, so as to ensure the smooth completion of the cluster leader election process in different application scenarios, thereby improving the leader election effect of cluster leader election while further reducing the leader election effect of cluster leader election.

[0076] In an implementable manner, the cluster master selection process defaults the first cluster node requested by all master selection nodes as the default candidate master node, and then in the case where all nodes are online, the default candidate master node requested according to the predetermined rule is online, the selection result is consistent, and takes effect immediately, and in the case where the default candidate master node is not online, the master selection request is repeatedly submitted and the cluster master selection is performed by one or more nodes in the cluster in different single time slots to ensure that the default candidate master node is successfully online and can still be selected as the master node of the cluster.

[0077] In one embodiment, the cluster master selection based on the second target master selection request comprises:

[0078] In the case where the master node change condition is detected, the request number of the second target master selection request is detected according to the third request information of the second target master selection request, and in the case where the request number is greater than the first preset request number threshold, the second cluster node requested by the second target master selection request is taken as the cluster master node.

[0079] It should be noted that in the process of cluster master selection, if the first cluster node requested by all master selection requests is not online in the master selection stage of the first synchronization time slot, the nodes in the cluster can also change the cluster node requesting to be the master node, and then the second cluster node requested by the second target request can be taken as the cluster master node depending on the size relationship between the request number and the preset request number threshold, that is, after the first node sends a request a sufficient number of times, the second cluster node requested by the second target request is taken as the cluster master node, wherein the master node change condition is a prerequisite for changing the default candidate master node of the cluster node.

[0080] As an example, in the case where the master node change condition is detected, the request number is extracted from the third request information of the second target master selection request, the request number of the second target master selection request is queried according to the request number, and in the case where the request number is greater than the first preset request number threshold, the master selection request content is extracted from the second target master selection request, and the second cluster node identified by the master selection request content is taken as the cluster master node.

[0081] In this embodiment, in the case where the cluster master node requested by all master selection nodes in the cluster changes, the request number of the second target master selection request is detected through the third request information of the second target master selection request, and in the case where the request number is greater than the first preset request number threshold, the second cluster node requested by the second target master selection request is taken as the cluster master node, thereby achieving the purpose of smoothly completing the cluster master selection process in the case where the default candidate master node changes, so that the limitation of cluster master selection is further reduced while improving the master selection effect of cluster master selection.

[0082] In one embodiment, the master node change condition comprises one of the following:

[0083] detecting a third master election request in the second synchronization time slot, and the request number of the third master election request is greater than a second preset request number threshold, wherein the second preset request number threshold is less than the first preset request number threshold.

[0084] It should be noted that the corresponding master node change condition is different in different application scenarios. For example, in the case where the default candidate master node does not go online continuously in the second synchronization time slot and no other node sends a master election request, the cluster master election can be implemented based on the request number of the second target master election request submitted by the first node. In the case where the default candidate master node goes online in the second synchronization time slot and there is another node sending a master election request, the cluster master election can also be implemented after the first node determines the final requested cluster node by detecting the request number of the third master election request, wherein the third master election request can be sent to the master election request sharing area by the first node or by the second node, and the second preset request number threshold is less than the first preset request number threshold.

[0085] As an example, the third master election request sent by the second node is not detected in the second synchronization time slot.

[0086] In an implementable manner, the process of cluster master election in the default candidate master node change scenario can be as follows: in time slot 1, node B as the first node proposes a first target master election request according to a predetermined rule and requests to select node A as the cluster master node, but finds that node A is absent when obtaining the master election request of the second node, and plans to regenerate a second target master election request in the next time slot (the second synchronization time slot). In time slot 2, node B proposes a second target master election request according to the predetermined rule and proposes to elect node B. In time slot 3, node B finds that no other node submits a master election request and repeats the second target master election request. In time slot 4, node B finds that no other node submits a master election request and repeats the second target master election request. In time slot 5, node B finds that no other node submits a master election request and repeats the second target master election request. At this time, the count (request number) of the second target master election request has exceeded the threshold (the first preset request number threshold), and the cluster master election is completed.

[0087] As another example, the third master election request is detected in the second synchronization time slot, and the request number of the third master election request is greater than the second preset request number threshold.

[0088] In an implementable manner, the cluster master election process in the scenario of default candidate change and candidate instability can be as follows: time slot 1: node B proposes a first target master election request according to a predetermined rule, and proposes node A, but finds that node A is absent when obtaining master election requests of other nodes; time slot 2: node B proposes a second target master election request according to a predetermined rule, and proposes node B instead; time slot 3: node A is online and participates in the cluster master election process, node A finds that node A should be proposed according to a predetermined rule, which is inconsistent with the request of the second target master election request, and then node A proposes a master election request and proposes node A itself, and node B repeats the second target master election request according to the result of time slot 2; when checking master election nodes of other nodes, node B finds other master election requests, and plans to propose the master election request in the next time slot; time slot 4: node A does not repeat the master election request; node B submits the master election request according to the plan, but does not see the master election request of node A; time slot 5: node B proposes a third master election node according to a predetermined rule, and proposes node B instead; time slot 6: node B finds that no other node submits a master election request, and repeats the third master election request; time slot 7: node B finds that no other node submits a proposal, and repeats the third master election request; time slot 8: node B finds that no other node submits a master election request, and repeats the third master election request. At this time, the count (request number) of the third master election request has exceeded the second preset request number threshold, and the cluster master election is completed.

[0089] In the embodiment, different master election node change conditions are set for different cluster master election conditions, and then the cluster master election is performed under the corresponding master election node change condition, so that the master election limitation of the cluster master election is further reduced.

[0090] In one embodiment, the method further comprises one of the following:

[0091] changing the current running state from the cluster master election state to the master node running state; changing the current running state from the cluster master election state to the slave node running state; and changing the current running state to the offline state.

[0092] It should be noted that after the cluster master election process is ended, the first node changes the node running state according to the indicated node identity, that is, in the case that the first node is not selected as the master node, the current running state of the first node is changed from the cluster master election state to the master node running state, in the case that the first node is selected as the master node, the current running state of the first node is changed from the cluster master election state to the slave node running state, and in the case that the node needs to be offline, the current running state is changed to the offline state. It can be understood that the above-mentioned node identity is determined according to the cluster master election process, and the node identity is determined according to the cluster master election process. Figure 4 , Figure 4The state diagram for representing the cluster state, wherein when the cluster is started, all nodes are in the slave node state (S state), and the cluster is in the SS state; after the cluster master election is successful, the master node is in the M state, and the other nodes are in the S state, and the cluster is in the MS state or the SM state; when a node is offline, the state of the node is -, and the remaining nodes continue to work; when a node is online, the MS state or the SM state is restored.

[0093] As an example, the current running state is changed from the cluster master election state to the master node running state; the current running state is changed from the cluster master election state to the slave node running state; and the current running state is changed to the offline state.

[0094] In this embodiment, after the cluster master election is completed, the first node actively changes the state based on the cluster master election result, and the current running state of the first node is changed from the cluster master election state to the master node running state, the slave node running state or the offline state, so as to ensure the state consistency of all nodes in the cluster, and therefore, the stability of the master election in the cluster is improved.

[0095] In an implementable manner, with reference to Figure 5 , Figure 5 The flowchart for representing the cluster master election process, that is, 1) preparing the master election request according to the predetermined rule: the master election request number should be the latest master election request number of the current cluster plus 1, and the master election request with a larger number is the new master election request; 2) submitting the master election request to the master election request sharing area (readable by other nodes), and counting the submission times of the master election request number; 3) reading all master election requests of other nodes from the master election request sharing area, if no master election request is submitted by other nodes in the current cluster master election process, it is indicated that the default candidate master node is invalid (for example, not online or not participating), and other candidate master nodes need to be selected according to the predetermined rule, the master election request is resubmitted, and step 1 is performed again in the next time slot; 4) checking whether other nodes submit master election requests, if only the node itself submits the master election request, checking whether the count of the master election request number exceeds the threshold, if yes, there is no other participant, and the current election is valid (the node in the master election request is itself in this scenario), and step 6 is performed; if no, the master election request is repeated, and step 2 is performed in the next time slot; 5) checking whether the master election request number and the master election request content of other master election requests are consistent with the master election request of the node itself, if not, other nodes need to be selected according to the predetermined rule, the master election request is resubmitted, and step 1 is performed in the next time slot; 6) all nodes reach an agreement, the master node election is successful, and the election process is ended, the node selected as the Master switches the state machine to the M state, and the state machine of the other nodes switches to the S state, and it is agreed that the node actively ends the master election process when the connection with the sharing area or the NTP is abnormal, until the environment returns to normal.

[0096] Since all the master election requests are in the same time slot, that is, the master election requests sent by different master election nodes of the cluster are synchronized in time, and then all the master election requests can objectively represent the master node selection requirements of all nodes in the cluster for the first synchronization time slot, and then the purpose of ensuring that the master node selected by the cluster is the actual required master node of the cluster at the moment can be achieved, instead of being able to only rely on the master election request of a single node to perform cluster master election, so the technical defect that the master node selected by relying on the request of a single node is not the actual required node due to the fact that different nodes in the cluster all have selection capability, and if network delay, node failure and data inconsistency occur, the cluster master election is prone to error or the cluster master election time is long is overcome, and therefore the master election effect of cluster master election is improved.

[0097] It should be understood that, although each step in the flowchart involved in the above embodiments is displayed in sequence according to the arrow, these steps are not necessarily executed in the order indicated by the arrow. Unless otherwise specified herein, the execution of these steps is not strictly limited in sequence, and these steps can be executed in other orders. Moreover, at least part of the steps in the flowchart involved in the above embodiments can include multiple steps or multiple stages, which are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily sequential, but can be alternately executed with at least part of other steps or steps or stages in other steps.

[0098] Based on the same inventive concept, the embodiments of the present application also provide a cluster master election device for implementing the above-mentioned cluster master election method. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme described in the above method, so the specific limitations in one or more cluster master election device embodiments provided below can refer to the limitations of the cluster master election method in the above text, which will not be repeated here.

[0099] In one exemplary embodiment, as shown in Figure 6 A cluster master election device is provided, applied to a first node of a cluster, comprising: a sending module 401, a reading module 402, and a cluster master election module 403, wherein:

[0100] The sending module 401 is configured to, in the case of detecting that the cluster master election condition is met, send a first target master election request to the master election request sharing area in the cluster master election state.

[0101] The reading module 402 is configured to read all the leader election requests in the first synchronization time slot to which the first target leader election request belongs, wherein the all the leader election requests are composed of leader election requests sent by all the leader nodes in the cluster.

[0102] The cluster leader election module 403 is configured to perform cluster leader election based on the all the leader election requests.

[0103] In one of the embodiments, the cluster leader election condition comprises one of the following:

[0104] Failure in updating the master node status of the first node in the first synchronization time slot, detection of expiration of the heartbeat signal of the cluster master node in the first synchronization time slot, and detection of a second leader election request sent by a second node in the first synchronization time slot, wherein the second node refers to any leader node in the cluster except the first node.

[0105] In one of the embodiments, the all the leader election requests comprise the first target leader election request and other leader election requests sent by the second node to the leader election request sharing area; and the cluster leader election module 403 is further configured to:

[0106] If it is detected that a first cluster node requested by the all the leader election requests is online, first request information of the first target leader election request and second request information of the other leader election requests are acquired respectively, and cluster leader election is performed based on a comparison result of the first request information and the second request information; if it is not detected that the first cluster node is online, a second synchronization time slot for cluster leader election is determined, a second target leader election request generated in the second synchronization time slot is sent to the leader election request sharing area, and cluster leader election is performed based on the second target leader election request.

[0107] In one of the embodiments, the cluster leader election module 403 is further configured to:

[0108] If it is detected that the leader node change condition is met, the request number of the second target leader election request is detected according to third request information of the second target leader election request; and if the request number is greater than a first preset request number threshold, a second cluster node requested by the second target leader election request is determined as the cluster master node.

[0109] In one of the embodiments, the leader node change condition comprises one of the following:

[0110] detecting no third master election request sent by the second node in the second synchronization time slot; detecting a third master election request in the second synchronization time slot, and the request number of the third master election request being greater than a second preset request number threshold, wherein the second preset request number threshold is less than the first preset request number threshold.

[0111] In one of the embodiments, the cluster master election apparatus is further configured to:

[0112] change the current running state from the cluster master election state to a master node running state; change the current running state from the cluster master election state to a slave node running state; and change the current running state to an offline state.

[0113] The above-mentioned modules in the cluster master election apparatus can be realized by software, hardware and combinations thereof in whole or in part. The above-mentioned modules can be embedded in or independent of the processor in the computer device in hardware form, or stored in the memory in the computer device in software form, so as to be called and executed by the processor to perform the operations corresponding to the above-mentioned modules.

[0114] In one exemplary embodiment, a computer device is provided, which can be a terminal, and the internal structure diagram thereof can be as shown in Figure 7 The computer device includes a processor, a memory, an input / output interface, a communication interface, a display unit and an input device. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface, the display unit and the input device are connected to the system bus through the input / output interface. The processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operating system and the computer program in the non-volatile storage medium to run. The input / output interface of the computer device is configured to exchange information between the processor and external devices. The communication interface of the computer device is configured to perform wired or wireless communication with external terminals, and the wireless communication can be achieved through WIFI, mobile cellular network, NFC (Near Field Communication) or other technologies. The computer program is executed by the processor to implement a cluster master election method. Those skilled in the art can understand that Figure 7 The structure shown in the above-mentioned figure is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the computer device to which the scheme of the present application is applied. The specific computer device can include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0115] In one embodiment, a computer device is also provided, including a memory and a processor, the memory storing a computer program, and the processor implementing the steps in the above method embodiments when executing the computer program.

[0116] In one embodiment, a computer readable storage medium is provided, storing a computer program, and the computer program implements the steps in the above method embodiments when executed by a processor.

[0117] In one embodiment, a computer program product is provided, including a computer program, and the computer program implements the steps in the above method embodiments when executed by a processor.

[0118] A person of ordinary skill in the art can understand that all or part of the processes in the above method embodiments can be completed by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer readable storage medium, and when executed, can include the processes of the above method embodiments. Any reference to a memory, database or other medium used in the embodiments provided in the present application can include at least one of a non-volatile and volatile memory. The non-volatile memory can include a read-only memory (ROM), a magnetic tape, a floppy disk, a flash memory, an optical storage, a high-density embedded non-volatile memory, a resistive memory (ReRAM), a magnetoresistive random access memory (MRAM), a ferroelectric memory (FRAM), a phase change memory (PCM), a graphene memory, etc. The volatile memory can include a random access memory (RAM) or an external cache memory, etc. As an illustration but not limitation, the RAM can be in various forms, such as a static random access memory (SRAM) or a dynamic random access memory (DRAM), etc. The database involved in the embodiments provided in the present application can include at least one of a relational database and a non-relational database. The non-relational database can include a distributed database based on a block chain, etc., without being limited thereto. The processor involved in the embodiments provided in the present application can be a general processor, a central processing unit, a graphics processing unit, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., without being limited thereto.

[0119] Any technical features in the above embodiments can be combined, and for the sake of brevity, not all possible combinations are described above, however, as long as the combinations of technical features do not have contradictions, they shall be considered within the scope of the present disclosure.

[0120] The above embodiments only express several implementation manners of the present application, and the description is relatively specific and detailed, but it shall not be understood as a limitation on the patent scope of the present application. It shall be pointed out that, for ordinary skilled persons in the art, several modifications and improvements can be made without departing from the concept of the present application, and these shall be within the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the appended claims.

Claims

1. A cluster leader election method, characterized in that, The method, applied to the first node of the cluster, includes: If the cluster leader election conditions are met, a first target leader election request is sent to the leader election request shared area in the cluster leader election state. Read all master election requests in the shared area of ​​the master election request within the first synchronization time slot to which the first target master election request belongs, wherein all master election requests consist of master election requests sent by all master election nodes of the cluster; Cluster leader election is performed based on all the leader election requests, wherein the cluster leader election criteria include one of the following: The update of the master node status of the first node fails within the first synchronization time slot; the heartbeat signal of the cluster master node expires within the first synchronization time slot; a second master election request sent by the second node is detected within the first synchronization time slot, wherein the second node refers to any master election node in the cluster other than the first node; wherein all master election requests include a first target master election request and other master election requests sent by the second node to the master election request shared area; the cluster master election based on all master election requests includes: If the first cluster node that is the common request of all the leader election requests is detected to be online, then the first request information of the first target leader election request and the second request information of the other leader election requests are obtained respectively, and the cluster leader is elected based on the information comparison results of the first request information and the second request information; if the first cluster node is not detected to be online, then the second synchronization time slot for the cluster leader election is determined, and the second target leader election request generated in the second synchronization time slot is sent to the leader election request sharing area, and the cluster leader is elected based on the second target leader election request.

2. The method according to claim 1, characterized in that, The process of electing a cluster leader based on the second target leader election request includes: If the conditions for changing the primary node are met, the number of requests for the second primary node election request is detected based on the third request information of the second primary node election request. If the number of requests exceeds a first preset request number threshold, the second cluster node requested by the second target master selection request will be designated as the cluster master node.

3. The method according to claim 2, characterized in that, The conditions for changing the selected master node include one of the following: No third master election request sent by the second node was detected during the second synchronization time slot; A third master selection request is detected within the second synchronization time slot, and the number of requests for the third master selection request is greater than a second preset request count threshold, wherein the second preset request count threshold is less than the first preset request count threshold.

4. The method according to claim 1, characterized in that, The method also includes one of the following: Change the current running status from the cluster master election status to the master node running status; Change the current running state from the cluster master election state to the slave node running state; Change the current running status to offline status.

5. A cluster master selection device, characterized in that, The device, applied to the first node of the cluster, includes: The sending module is used to send the first target leader election request to the leader election request sharing area in the cluster leader election state when the cluster leader election conditions are detected to be met. The reading module is used to read all master election requests in the shared area of ​​the master election request within the first synchronization time slot to which the first target master election request belongs, wherein all master election requests consist of master election requests sent by all master election nodes of the cluster; The cluster leader election module is used to perform cluster leader election based on all the leader election requests, wherein the cluster leader election conditions include one of the following: The following events occur: The update of the master node status of the first node fails within the first synchronization time slot; the heartbeat signal of the cluster master node expires within the first synchronization time slot; a second master election request sent by the second node is detected within the first synchronization time slot, wherein the second node refers to any master election node in the cluster other than the first node; wherein all master election requests include a first target master election request and other master election requests sent by the second node to the master election request shared area; the cluster master election module is further configured to: If the first cluster node that is the common request of all the leader election requests is detected to be online, then the first request information of the first target leader election request and the second request information of the other leader election requests are obtained respectively, and the cluster leader is elected based on the information comparison results of the first request information and the second request information; if the first cluster node is not detected to be online, then the second synchronization time slot for the cluster leader election is determined, and the second target leader election request generated in the second synchronization time slot is sent to the leader election request sharing area, and the cluster leader is elected based on the second target leader election request.

6. The apparatus according to claim 5, characterized in that, The cluster leader election module is also used for: If the conditions for changing the primary node are met, the number of requests for the second primary node election request is detected based on the third request information of the second primary node election request. If the number of requests exceeds a first preset request number threshold, the second cluster node requested by the second target master selection request will be designated as the cluster master node.

7. The apparatus according to claim 5, characterized in that, The cluster master election device is also used for: Change the current running state from the cluster master election state to the master node running state; change the current running state from the cluster master election state to the slave node running state; Change the current running status to offline status.

8. 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 4.

9. 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 4.

10. 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 4.

Citation Information

Patent Citations

  • Cluster main node selection method, system and device and computer readable storage medium

    CN108763501A

  • Main node switching method and device, equipment, storage medium and program product

    CN118656171A