Cluster master selection method and device, computer equipment, medium and product
By sending a target master selection request to the master selection shared area in the cluster master selection state and reading all master selection requests in the time slot, and selecting a master cluster based on the requests of multiple nodes, the cluster master selection in the existing technology is solved It is easy to cause errors in the selection of the owner or long time due to network delay, node failure and data inconsistency, which improves the selection effect.
Patent Information
- Application Number
- CN202411691878.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-25
- Publication Date
- 2025-02-18
- Estimated Expiration
- 2044-11-25
AI Technical Summary
In the process of selecting a cluster, the prior art is prone to errors in the selection of a master or a long time due to network delay, node failure and data inconsistency, and the selection effect is poor.
By sending a target master selection request to the master selection shared area in the cluster selection state and reading all master selection requests in the time slot, the cluster master selection is performed based on the requests of multiple nodes, and ensuring that the selected master node is The nodes that the cluster currently actually needs.
The master selection effect of cluster master selection is improved, and the problem of master selection errors or long time caused by single node requests is avoided, ensuring that the selected master node is the node currently required by the cluster.
Smart Images

Figure CN119484229A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of cluster management, and in particular to a cluster leader election method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art
[0002] With the development of science and technology, clustering has been widely implemented in many industries. Among them, the Active-Standby mode has become the mainstream mode of clustering due to its high availability, high failover efficiency and low resource utilization. In the process of cluster management, it is inevitable to face the problem of cluster leader election.
[0003] At present, in the process of cluster leader election, a selection cycle is usually planned for cluster nodes to send leader election requests, that is, at a certain point in time, the cluster node requested by a single node in the cluster is used as the master node. However, since different nodes in the cluster have the ability to be selected, if network delays, node failures, and data inconsistencies occur, the master node selected by relying on a single node request may not be the actual required node, which makes it easy for cluster leader election errors or long cluster leader election time to occur. Therefore, the current cluster leader election has a poor leader election effect. Summary of the invention
[0004] Based on this, it is necessary to provide a cluster leader election method, apparatus, computer equipment, computer-readable storage medium and computer program product to improve the cluster leader election effect in order to solve the above technical problems.
[0005] In a first aspect, the present application provides a cluster leader election method, which is applied to a first node of a cluster, comprising:
[0006] When it is detected that the cluster leader election condition is met, sending a first target leader election request to the leader election request sharing area in the cluster leader election state;
[0007] Reading all the master election requests in the master election request sharing area within the first synchronization time slot to which the first target master election request belongs, wherein all the master election requests are composed of the master election requests sent by all the master election nodes in the cluster;
[0008] A cluster leader is elected based on all the leader election requests.
[0009] In one embodiment, the cluster master election condition includes one of the following:
[0010] Updating the master node status of the first node within the first synchronization time slot fails;
[0011] Detecting that the heartbeat signal of the cluster master node has expired within the first synchronization time slot;
[0012] A second master election request sent by a second node is detected in the first synchronization time slot, wherein the second node refers to any master election node in the cluster except the first node.
[0013] In one embodiment, the all the leader election requests include a 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 based on the all the leader election requests includes:
[0014] If it is detected that the first cluster node commonly requested by all the master election requests is online, first request information of the first target master election request and second request information of the other master election requests are respectively obtained, and cluster leader election is performed based on the information 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 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.
[0016] In one embodiment, the performing cluster leader election based on the second target leader election request includes:
[0017] When it is detected that the master election node change condition is met, detecting the request count of the second target master election request according to the third request information of the second target master election request;
[0018] When the number of requests is greater than a first preset request number threshold, the second cluster node requested by the second target master election request is used as the cluster master node.
[0019] In one embodiment, the master node selection change condition includes one of the following:
[0020] not detecting a third master election request sent by the second node in the second synchronization time slot;
[0021] A third master election request is detected in the second synchronization time slot, and the request times of the third master election request are greater than a second preset request times threshold, wherein the second preset request times threshold is less than the first preset request times threshold.
[0022] In one embodiment, the method further comprises one of the following:
[0023] Change the current running state from the cluster master election state to the master node running state;
[0024] Changing the current running state from the cluster master election state to the slave node running state;
[0025] The current running state is changed to an offline state.
[0026] In a second aspect, the present application further provides a cluster leader election method device, which is 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 the master election requests in the master election request sharing area within the first synchronization time slot to which the first target master election request belongs, wherein all the master election requests are composed of the master election requests sent by all the master election nodes of the cluster;
[0029] The cluster leader election module is used to elect a cluster leader based on all the leader election requests.
[0030] In a third aspect, the present application further provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:
[0031] When it is detected that the cluster leader election conditions are met, a first target leader election request is sent to the leader election request sharing area in the cluster leader election state; all leader election requests in the leader election request sharing area within the first synchronization time slot to which the first target leader election request belongs are read, wherein all leader election requests are composed of leader election requests sent by all leader election nodes in the cluster; and cluster leader election is performed based on all leader election requests.
[0032] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:
[0033] When it is detected that the cluster leader election conditions are met, a first target leader election request is sent to the leader election request sharing area in the cluster leader election state; all leader election requests in the leader election request sharing area within the first synchronization time slot to which the first target leader election request belongs are read, wherein all leader election requests are composed of leader election requests sent by all leader election nodes in the cluster; and cluster leader election is performed based on all leader election requests.
[0034] In a fifth aspect, the present application further provides a computer program product, including a computer program, which implements the following steps when executed by a processor:
[0035] When it is detected that the cluster leader election conditions are met, a first target leader election request is sent to the leader election request sharing area in the cluster leader election state; all leader election requests in the leader election request sharing area within the first synchronization time slot to which the first target leader election request belongs are read, wherein all leader election requests are composed of leader election requests sent by all leader election nodes in the cluster; and cluster leader election is performed based on all leader election requests.
[0036] The above-mentioned cluster leader election method, device, computer equipment, computer-readable storage medium and computer program product are applied to the first node of the cluster. First, when the first node detects that the cluster leader election conditions are met and the first node is in the cluster leader election state, it sends a first target leader election request to the leader election request sharing area, and then reads all leader election requests in the first synchronization time slot to which the first target leader election request belongs in the leader election request sharing area, wherein all leader election requests are composed of leader election requests sent by all leader election nodes of the cluster, that is, the leader election requests sent by all leader election nodes of the cluster in the first synchronization time slot are obtained through the leader election request sharing area, and finally the cluster leader is selected through all the leader election requests. Since all the leader election requests are in the same time slot, that is, the cluster The master election requests sent by different master election nodes are synchronized in time, so all master election requests can objectively represent the master node selection requirements of all nodes in the cluster for the first synchronization time slot, so as to achieve the purpose of ensuring that the master node obtained by cluster master election is the master node actually required by the cluster at the moment, rather than relying on the master election request of a single node for cluster master election. Therefore, the technical defect that different nodes in the cluster have the ability to select, if network delays, node failures and data inconsistencies occur, the master node selected by relying on a single node request may not be the actual required node, which makes it easy for cluster master election to make mistakes or take a long time to select the cluster master is overcome. Therefore, the master election effect of cluster master election is improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related technologies, the drawings required for use in the embodiments or the related technical descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0038] Figure 1 A schematic diagram of a process flow of a cluster leader selection method in one embodiment;
[0039] Figure 2 A schematic diagram of node states of cluster nodes in a cluster master election method in one embodiment;
[0040] Figure 3A schematic diagram of a process flow of a cluster leader selection method in another embodiment;
[0041] Figure 4 A state diagram of a cluster state in a cluster master election method in one embodiment;
[0042] Figure 5 A schematic diagram of a cluster leader selection process of a cluster leader selection method in an embodiment;
[0043] Figure 6 A structural block diagram of a cluster master selection device in one embodiment;
[0044] Figure 7 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0045] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0046] First of all, it should be understood that the clustering of multiple business nodes can effectively deal with single point failures, that is, the availability of cluster services can be guaranteed when any node in the cluster is abnormal. In specific business scenarios, the Active-Standby mode is usually used, that is, a node in the cluster serves as the Master node, which is responsible for resource allocation and task scheduling and other tasks that require global centralized processing, while other nodes in the cluster serve as Slave nodes, which respond to tasks assigned by the Master node and are ready to compete to become the new Master when the Master is unavailable. At present, the Raft algorithm and the ZAB algorithm are usually used to solve the master selection problem of the master-slave cluster. In this process, on the one hand, the cost of use needs to be considered, such as: 1) Reserved resources: CPU, memory, network IO and disk overhead; 2) Maintenance and management: Reasonable parameter configuration can make the service run efficiently and stably, but for open source services, there is a certain maintenance and management threshold. For example, when using ETCD (EtcdDistributed ETCD, a distributed database, is used as a cluster coordination service. It is necessary to configure parameters to specify the number of operations or the running time before snapshot compression of WAL is performed to reduce the memory usage of ETCD. Otherwise, the configuration may cause the memory to continue to grow. 3) Failure impact: If the ETCD service is deployed on the same server with other services, ETCD abnormalities (such as memory growth) may affect other services. If ETCD is deployed separately on several servers, the cost of use is too high. On the other hand, the existing technology affects the cluster leader election effect. That is, if there is network delay, node failure or data inconsistency, it may cause the requests of different nodes in the cluster to fail to synchronously feedback, which makes it easy for the cluster leader election to fail or take a long time. Therefore, there is an urgent need for a cluster leader election method that improves the leader election effect of the cluster leader election.
[0047] In one embodiment, Figure 1As shown, a cluster leader selection method is provided. This embodiment takes the method applied to a terminal as an example. The terminal includes but is not limited to a personal computer, a laptop computer, a smart phone, and a tablet computer. The terminal includes a sending module, a reading module, and a cluster leader selection module. The sending module is used to send a first target leader selection request to a leader selection request sharing area in a cluster leader selection state when it is detected that a cluster leader selection condition is met. The reading module is used to read all leader selection requests in the leader selection request sharing area within a first synchronization time slot to which the first target leader selection request belongs, wherein all leader selection requests are composed of leader selection requests sent by all leader selection nodes in the cluster. The cluster leader selection module is used to perform cluster leader selection based on all leader selection requests. The information interaction between the module and the cluster master election module, during the cluster master election process, relies on the master election requests sent by different nodes of the cluster to the master election request sharing area in the same synchronization time slot, that is, it ensures that the master election requests sent by different master election nodes of the cluster are synchronized in time, thereby achieving the purpose of ensuring that the master node obtained by the cluster master election is the master node actually required by the cluster at the moment. Therefore, the cluster master election problem caused by the time synchronization of different nodes in the cluster during the cluster master election process can be avoided. Therefore, the master election effect of the cluster master election can be improved. It can be understood that this method can also be applied to servers, and can also be applied to systems including terminals and servers, and is implemented through the interaction between terminals and servers. In this embodiment, the method includes the following steps 202 to 206. Among them:
[0048] Step 202: When it is detected that the cluster leader election condition is met, a first target leader election request is sent to the leader election request sharing area in the cluster leader election state.
[0049] It should be noted that, in the face of complex coordination content in the cluster leader election process, the cluster leader election method deployed in this embodiment can avoid the heavyweighting of the cluster leader election process, thereby reducing the use cost, and can also improve the leader election effect. It is understandable that the cluster leader election method can rely on NTP (Network Protocol). The cluster master election condition is used to trigger the first node of the cluster to participate in the cluster master election, which can be specifically triggered by the user clicking or inputting an instruction in the first section. The cluster may include multiple nodes, the first node can be either a master node or a slave node. The master election request sharing area is used to provide services for multiple servers (nodes) to read and write data, which can be specifically a network file system, FTP, shared memory, and database, etc. The nodes in the cluster can also use the master election request sharing area to publish master election requests and view master election requests of other nodes. It can be understood that the data in the master election request sharing area can be periodically cleaned or permanently stored. The master election request is used to represent the request object of the master election, which can be understood as the node in the cluster publishing recommended content at a certain time, which can also be called a "master election proposal". The same node at different times or not The master election requests sent by the same node to the master election request sharing area at the same time can be sequenced by numbering. For example, in an implementable manner, a single master election request can specifically consist of four parts: a time slot number, an initiator, a master election request content, and a request number. The time slot number is used by NTP to ensure time synchronization accuracy. The cluster is uniformly numbered according to the configured time slot granularity and the configured start time. The data type can be a 64-bit unsigned integer. The initiator is used to identify the identity of the requester who initiates the master election request, that is, the initiator is the node identifier of the node itself. The master election request content refers to the master node selected by the nodes in the cluster according to the predetermined master election rule. The predetermined master election rule can be triggered based on the node number or the start time. The request number is used to distinguish and identify different master election requests. All nodes in the cluster are numbered using the same rule. The larger the number, the newer the master election request. All nodes in the cluster can obtain the latest number of the current master election request through the master election request sharing area. Only when the master election request numbers of all nodes in the cluster are the same and the master election request contents are consistent can it be considered that the master election requests are consistent, that is, the cluster leader election is successful.
[0050] It should be noted that the first target leader election request refers to the leader election request sent by the first node. It can be understood that after the cluster starts the leader election process, for any node in the cluster, the cluster leader election process must be completed within a single time slot. When a single node meets the cluster leader election conditions and triggers the cluster leader election process, the following four parts will be involved: 1) Time synchronization. The cluster nodes implement time synchronization based on NTP to ensure that the time reference of each node is consistent. The time slot size is related to the NTP accuracy. 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 method of different nodes in the cluster can be simplified to the initial time alignment method every 4 seconds, wherein any node is divided into the master election request submission phase and the master election phase within a single time slot granularity, that is, all nodes in the cluster must submit the master election request in the master election request submission phase, and obtain all master election requests from the master election request sharing area at the master election node; 2) The master election request sharing area, which is responsible for receiving the master election requests of all nodes in the cluster and for allowing any node in the cluster to read the master election requests of other nodes in the cluster. 1) The master election request of the node. It can be understood that the master election request shared area needs to ensure that the time slot granularity is greater than the time it takes for all nodes to complete row data reading and writing. 2) Heartbeat. The master node of the cluster needs to continuously update the heartbeat record based on the time slot in the master election request shared area. The heartbeat record carries the time slot identifier. Other slave nodes in the cluster can determine whether the master node is abnormal through the time slot of the heartbeat update record and the current time slot, so as to further determine whether to send the first target master election request to the master election request shared area. 3) State machine. All nodes in the cluster run independently according to their own state machines, discover and participate in the master election process through the time slot-driven timing detection task, and complete it spontaneously. It can be understood that when the cluster starts the master election process, the current business role is stopped until the re-election is successful. Each node in the cluster is driven by the time slot to ensure that the cluster as a whole is natural and has state management. The sign of the cluster starting the master election process can be that the node in the cluster sends a master election request to the master election request shared area. The state machine of a single node has three states, namely the master node state (M state), the slave node state (S state) and the cluster master election state (V state). Figure 2 , Figure 2 The figure is a schematic diagram of the node status of the cluster nodes, where the node in the master node state needs to execute scheduled tasks and identify new master election requests. The node needs to go through the main process and reach a consensus among all nodes before it can be selected as the master node. If it cannot continue to run as the master node during business operation, it needs to actively switch to the S state and no longer refresh the heartbeat record of the master election request shared area in the M state. The node in the slave node state only executes the time slot-based scheduled detection task to identify whether the current master node is expired. The node enters the S state by default. When the node exits, it needs to switch to the S state first. The node in the cluster master election state is automatically triggered by the scheduled detection task to initiate or participate in the master election process until the master election is successful.
[0051] As an example, step 202 includes: when it is detected that the cluster leader election conditions are met, switching the current operating state of the first node to the cluster leader election state, generating a selection request content according to preset rules, encapsulating the leader election request content, the node identifier of the first node, the leader election request identifier and the time slot number together as a first target leader election request, and sending the first target leader election request to the leader election request sharing area.
[0052] Step 204 , read all the master election requests in the master election request sharing area within the first synchronization time slot to which the first target master election request belongs, wherein all the master election requests are composed of the master election requests sent by all the master election nodes in the cluster.
[0053] It should be noted that in the first synchronization time slot to which the first target master election request belongs, in addition to the first node, other nodes in the cluster may also send a master election request to the master election request sharing area. Then, by analyzing the time slot numbers that do not pass the master election request in the master election request sharing area, all master election requests can be read, thereby laying the foundation for the first node to perform cluster leader election.
[0054] As an example, step 204 includes: obtaining the time slot numbers of all candidate master election requests in the master election request sharing area, and based on the comparison results between all the time slot numbers and the time slot numbers of the first synchronization time slot to which the first target master election request belongs, screening out all the master election requests within the first synchronization time slot to which the first target master election request belongs from all the candidate master election requests.
[0055] Step 206: Perform cluster leader election based on all leader election requests.
[0056] It should be noted that if all master election requests only include 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 used as the cluster master node. If all master election requests include the first target master election request and other master election requests, the selection of the cluster master node can be completed if the contents of the master election requests of the above master election requests are consistent.
[0057] As an example, step 206 includes: when it is detected that all the master election requests are one master election request, the cluster node requested by the first target master election request is used as the master node; and when it is detected that all the master election requests are multiple master election requests, when the master election request contents of the multiple master election requests are consistent, the cluster node identified by the master election request content is used as the master node.
[0058] In one feasible manner, when it is detected that all the leader election requests are multiple leader election requests, if the leader election request contents of the multiple leader election requests are consistent, cluster leader election failure information is reported, and when it is detected again that the cluster leader election conditions are met, the cluster leader election process is started.
[0059] In the above cluster leader election method, first, when the first node detects that the cluster leader election conditions are met, a first target leader election request is generated based on the leader election request content, the node identifier of the first node, the leader election request identifier and the time slot sequence number, and the first target leader election request is sent to the leader election request shared area. After completing the submission phase of the leader election request in the first synchronization time slot, the leader election phase is entered, that is, all leader election requests in the leader election request shared area within the first synchronization time slot to which the first target leader election request belongs are read, and finally the cluster leader election is completed based on all the leader election requests. Since all the leader election requests are in the same time slot, that is, the leader election requests sent by different leader election nodes in the cluster are synchronized in time. , and thus all master election requests can objectively represent the master node selection requirements of all nodes in the cluster for the first synchronization time slot, thereby achieving the purpose of ensuring that the master node obtained by the cluster master election is the master node actually required by the cluster at the moment, rather than relying on the master election request of a single node for cluster master election. Therefore, it overcomes the technical defect that since different nodes in the cluster have the ability to select, if network delays, node failures, and data inconsistencies occur, the master node selected by relying on a single node request may not be the actual required node, which makes it easy for cluster master election errors or long cluster master election time to occur. Therefore, the master election effect of cluster master election is improved.
[0060] In one embodiment, the cluster master election condition includes one of the following:
[0061] Updating the master node status of the first node fails within the first synchronization time slot; the heartbeat signal of the cluster master node is detected to be expired within the first synchronization time slot; the 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 except the first node.
[0062] It should be noted that when the first node has different identities in the cluster, the first node can trigger the cluster master election process based on different cluster master election conditions. It can be understood that, whether it is a master node or a slave node in the cluster, the first node can trigger the detection task based on the time slot, where the detection task is to detect whether the cluster master election conditions are met.
[0063] As an example, the cluster master election condition is that when the first node serves as the cluster master node, the update of the master node status of the first node in the shared area requested by the master election in the first synchronization time slot fails, wherein after the master node status update fails, the first node immediately switches the master node status to the slave node status.
[0064] As another example, the cluster master election condition is that when the first node acts as a cluster slave node, the heartbeat signal of the cluster master node in the master election request sharing area is read in the first synchronization time slot, and the heartbeat signal is detected to be expired.
[0065] As another example, the cluster master election condition is that when the first node acts as the cluster master node or the cluster slave node, a second master election request sent by the second node is detected in the first synchronization time slot, where the second node refers to any master election node in the cluster except the first node.
[0066] In this embodiment, different cluster leader election conditions are set based on different identities of the first node in the cluster, and the cluster leader election process is triggered, so as to ensure that the first node can be automatically triggered to participate in the cluster leader election process in various application scenarios. Therefore, while improving the cluster leader election effect, the leader selection limitations of the cluster leader election are reduced.
[0067] In one embodiment, Figure 3 As shown, all the 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 sharing area; cluster leader election is performed based on all the leader election requests, including:
[0068] Step 302: If it is detected that the first cluster node commonly requested by all the master election requests is online, first request information of the first target master election request and second request information of other master election requests are obtained respectively, and 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 where multiple nodes in a cluster participate in cluster leader election, due to the differences in node requests of different master election nodes and the differences in node states of the requested cluster nodes, different processing logics need to be adopted to deal with the cluster leader election process to ensure the smooth progress of the cluster leader election. For example, in one feasible method, assuming that all nodes in the cluster are online, it means that the first cluster node requested by all master election requests is online, and then the cluster leader election can be completed by comparing the master election request contents and request numbers in different master election requests.
[0070] As an example, step 302 includes: if it is detected that all nodes in the cluster are online, determining that the first cluster node requested by all master election requests is online, extracting first request information in the first target master election request, and extracting second request information in other master election requests, and when the first request information and the second request information are compared and found to be consistent, the first cluster node is used as the cluster master node.
[0071] In an implementable manner, when the first request information and the second request information are found to be inconsistent, the timing detection task of the first node is rescheduled, and when it is detected that the cluster leader election condition is met, the cluster leader election process is triggered again.
[0072] Step 304: if the first cluster node is not detected to be online, determine a second synchronization time slot for cluster leader election, send a second target leader election request generated in the second synchronization time slot to the leader election request sharing area, and perform cluster leader election based on the second target leader election request.
[0073] It should be noted that if the first cluster node requested by all master election requests is not online, it means that the cluster master election cannot be completed within the first synchronization time slot, and the second synchronization time slot for cluster master election can be planned again. It can be understood that the first synchronization time slot is different from the second synchronization time slot, 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 master election request can be understood as a master election request sent to the master election request sharing area within the second synchronization time slot, thereby repeating the cluster master election process, that is, all master election requests in the master election request sharing area within the second synchronization time slot to which the second target master election request belongs can be further read, and cluster master election can be performed based on all master election requests.
[0074] As an example, step 304 includes: if the first cluster node is not detected to be online, determining the second synchronization time slot for cluster leader election, and sending the second target leader election request generated in the second synchronization time slot to the leader election request sharing area, and reading all leader election requests in the leader election request sharing area within the second synchronization time slot to which the second target leader election request belongs, and performing cluster leader election based on all leader election requests.
[0075] In this embodiment, based on the different node states of the first cluster nodes commonly requested by all leader election requests, different leader election methods are adopted to select the master node of the cluster, thereby ensuring the smooth completion of the cluster leader election process in different application scenarios. Therefore, while improving the leader election effect of the cluster leader election, the leader election effect of the cluster leader election is further reduced.
[0076] In one implementable manner, the above-mentioned cluster leader election process defaults to the first cluster node jointly requested by all leader election nodes as the default candidate leader node, and then when all nodes are online, the default candidate leader node requested according to the predetermined rule is online, and the selection result is consistent and takes effect immediately, and when the default candidate leader node is not online, one or more nodes in the cluster repeat the submission of the leader election request and the cluster leader election in different single time slots to ensure that the default candidate leader node can still be selected as the cluster's master node after it successfully comes online.
[0077] In one embodiment, performing cluster leader election based on the second target leader election request includes:
[0078] When it is detected that the conditions for changing the master node election are met, the number of requests for the second target master election request is detected according to the third request information of the second target master election request; when the number of requests is greater than the first preset request number threshold, the second cluster node requested by the second target master election request is used as the cluster master node.
[0079] It should be noted that, in the process of cluster leader election, if the first cluster node jointly requested by all leader election requests has no upper limit in the leader election stage of the first synchronization time slot, the nodes in the cluster can also replace the cluster node requested as the master node, and then, depending on the relationship between the number of requests and the preset request number threshold, the second cluster node requested by the second target leader election request can be used as the cluster master node, that is, after the first node sends a sufficient number of requests, the second cluster node requested by the second target request is used as the cluster master node, wherein the leader node change condition serves as a prerequisite for changing the default candidate master node of the cluster node.
[0080] As an example, when it is detected that the conditions for changing the master node election are met, the request number is extracted from the third request information of the second target master election request, and the number of requests for the second target master election request is queried based on the request number; when the number of requests is greater than the first preset request number threshold, the master election request content is extracted from the second target master election request, and the second cluster node identified by the master election request content is used as the cluster master node.
[0081] In this embodiment, when there is a change in the cluster master node requested by all the master election nodes in the cluster, the number of requests for the second target master election request is detected through the third request information of the second target master election request, and when the number of requests is greater than the first preset request number threshold, the second cluster node requested by the second target master election request is used as the cluster master node, thereby achieving the purpose of successfully completing the cluster master election process when there is a change in the default candidate master node. Therefore, while improving the master election effect of the cluster master election, the limitations of the cluster master election are further reduced.
[0082] In one embodiment, the master node selection change condition includes one of the following:
[0083] The third master election request sent by the second node is not detected in the second synchronization time slot; the third master election request is detected in the second synchronization time slot, and the number of requests for the third master election request is greater than the 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 election node change conditions are different in different application scenarios. For example, when the default candidate master node is continuously offline in the second synchronization time slot and no other node sends a master election request, the cluster leader election can be achieved based on the number of requests for the second target master election request submitted by the first node. When the default candidate master node is online in the second synchronization time slot and there are other nodes sending master election requests, the cluster leader election can also be achieved by detecting the number of requests for the third master election request after the first node determines the cluster node for the final request. The third master election request can be sent by the first node to the master election request sharing area, or by the second node to the master election request sharing area, 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 one implementable manner, the process of cluster leader election in the default candidate change scenario can be as follows: in time slot 1, node B, as the first node, submits a first target leader election request according to a predetermined rule, and requests to select node A as the cluster master node, but when obtaining the second node's leader election request, it is found that node A is absent, and it is planned to regenerate the second target leader election request in the next time slot (the second synchronization time slot). In time slot 2, node B submits a second target leader election request according to a predetermined rule, and changes it to nominating node B. In time slot 3, node B finds that no other node submits a leader election request, and repeats the second target leader election request. In time slot 4, node B finds that no other node submits a leader election request, and repeats the second target leader election request. In time slot 5, node B finds that no other node submits a leader election request, and repeats the second target leader election request. At this time, the count (number of requests) of the second target leader election request has exceeded the threshold (the first preset request number threshold), and the cluster leader election is completed.
[0087] As another example, a third master election request is detected in the second synchronization time slot, and the number of requests of the third master election request is greater than the second preset request number threshold.
[0088] In an implementable manner, the process of cluster leader election in a scenario where the default candidate is changed and the candidate is unstable can be as follows: Time slot 1: Node B submits a first target leader election request according to a predetermined rule and recommends node A, but finds that node A is absent when obtaining leader election requests from other nodes. Time slot 2: Node B submits a second target leader election request according to a predetermined rule and recommends node B instead. Time slot 3: Node A goes online to participate in the cluster leader election process. Node A finds that according to the predetermined rule, node A should be recommended, which is inconsistent with the second target leader election request. Then, node A submits a leader election request and recommends itself node A. Node B repeats the process according to the result of time slot 2. Second target leader election request; when checking the leader election nodes of other nodes, other leader election requests are found. Node B plans to submit the leader election request in the next time slot. In time slot 4, node A does not repeat the leader election request. Node B submits the leader election request as planned, but does not see the leader election request from node A. In time slot 5, node B proposes the third leader election node according to the predetermined rules and recommends node B instead. In time slot 6, node B finds that no other node has submitted a leader election request and repeats the third leader election request. In time slot 7, node B finds that no other node has submitted a proposal and repeats the third leader election request. In time slot 8, node B finds that no other node has submitted a leader election request and repeats the third leader election request. At this time, the count (number of requests) of the third leader election request has exceeded the second preset request number threshold, and the cluster leader election is completed.
[0089] In this embodiment, different leader election node change conditions are set for different cluster leader election conditions, and cluster leader election is performed under the corresponding leader election node change conditions, thereby further reducing the leader selection limitations of cluster leader election.
[0090] In one embodiment, the method further comprises one of the following:
[0091] 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 state to the offline state.
[0092] It should be noted that after the cluster master election process is completed, the first node changes the node operation state according to the indicated node identity, that is, when the first node is not selected as the master node, the current operation state of the first node is changed from the cluster master election state to the master node operation state; when the first node is selected as the master node, the current operation state of the first node is changed from the cluster master election state to the slave node operation state; when the node needs to go offline, the current operation state is changed to the offline state. It can be understood that, referring to Figure 4 , Figure 4The figure is a state diagram representing the cluster status. When the cluster is started, all nodes are in slave node state (S state), and the cluster is in SS state. After the cluster successfully elects a master, the master node is in M state, other nodes are in S state, and the cluster is in MS state or SM state. If a node goes offline, the node state is -, and the remaining nodes continue to work. If a node comes online, the MS state or 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; the current running state is changed to the offline state.
[0094] In this embodiment, after completing the cluster leader election, the first node actively changes its state based on the cluster leader election result, and changes the current operating state of the first node from the cluster leader election state to the master node operating state, the slave node operating state or the offline state, thereby ensuring the state consistency of all nodes in the cluster, thereby improving the stability of the cluster leader election.
[0095] In one practicable manner, referring to Figure 5 , Figure 5 The flowchart of the cluster leader election process is as follows: 1) prepare a leader election request according to the predetermined rules: the leader election request number should be 1 plus the latest leader election request number of the current cluster, and the leader election request with a larger number is the new leader election request; 2) the node submits the leader election request to the leader election request sharing area (readable by other nodes) and counts the number of submissions of the leader election request number; 3) read the leader election requests of all other nodes from the leader election request sharing area. If other nodes have not submitted leader election requests during this cluster leader election process, it means that the elected default candidate leader node is invalid (for example, it is not online / not participating), and it is necessary to re-elect other candidate leader nodes according to the predetermined rules, resubmit the leader election request, and re-execute step 1 in the next time slot; 4) check whether other nodes have submitted leader election requests this time. If only your own node has submitted a leader election request, check whether the leader election request has been submitted. Check whether the number count exceeds the threshold. If it has exceeded the threshold, there are no other participants, and this election takes effect (the node in the master election request in this scenario is itself), and execute step 6; if it does not exceed the threshold, repeat the master election request and execute step 2 in the next time slot; 5) Check whether the master election request number and the master election request content of other master election requests are consistent with its own master election request. If the numbers are inconsistent or the nominated nodes are inconsistent, other nodes need to be nominated according to the predetermined rules, and the master election request is resubmitted. Execute step 1 in the next time slot; 6) All nodes reach a consensus, the master node election is successful, the election process ends, the node selected as the Master, its own state machine switches to the M state, and the state machines of other nodes switch to the S state. It is agreed that if the connection between the node and the shared area or NTP is abnormal, the master election process will be actively terminated until the environment returns to normal.
[0096] Since all master election requests are in the same time slot, that is, the master election requests sent by different master election nodes in the cluster are synchronized in time, and thus all master election requests can objectively represent the master node selection requirements of all nodes in the cluster for the first synchronization time slot, thereby achieving the purpose of ensuring that the master node obtained by the cluster master election is the master node actually required by the cluster at the moment, rather than relying on the master election request of a single node for cluster master election. Therefore, the technical defect that different nodes in the cluster have the ability to select, and if network delays, node failures, and data inconsistencies occur, the master node selected by relying on a single node request may not be the actual required node, which makes it easy for cluster master election errors or long cluster master election time to occur is overcome. Therefore, the master election effect of cluster master election is improved.
[0097] It should be understood that, although the steps in the flowcharts involved in the above embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above embodiments may include multiple steps or multiple stages, and these steps or stages 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 carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0098] Based on the same inventive concept, the embodiment of the present application also provides a cluster leader selection device for implementing the cluster leader selection method involved above. The implementation solution provided by the device to solve the problem is similar to the implementation solution recorded in the above method, so the specific limitations in one or more cluster leader selection device embodiments provided below can refer to the limitations of the cluster leader selection method above, and will not be repeated here.
[0099] In an exemplary embodiment, Figure 6 As shown, a cluster leader selection device is provided, which is applied to the first node of the cluster, including: a sending module 401, a reading module 402 and a cluster leader selection module 403, wherein:
[0100] A sending module 401 is configured to send a first target leader election request to a leader election request sharing area in a cluster leader election state when it is detected that a cluster leader election condition is met;
[0101] A reading module 402 is configured to read all the master election requests in the master election request sharing area within the first synchronization time slot to which the first target master election request belongs, wherein all the master election requests are composed of the master election requests sent by all the master election nodes in the cluster;
[0102] The cluster leader election module 403 is configured to perform cluster leader election based on all the leader election requests.
[0103] In one embodiment, the cluster master election condition includes one of the following:
[0104] Updating the master node status of the first node within the first synchronization time slot fails; the heartbeat signal of the cluster master node is detected to be expired 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 except the first node.
[0105] In one embodiment, the 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 sharing area; the cluster leader election module 403 is further configured to:
[0106] If it is detected that the first cluster node commonly requested by all the master election requests is online, the first request information of the first target master election request and the second request information of the other master election requests are respectively obtained, and cluster leader election is performed based on the information comparison result of the first request information and the second request information; if the first cluster node is not detected to be online, the second synchronization time slot for cluster leader election is determined, and the second target master election request generated in the second synchronization time slot is sent to the master election request shared area, and cluster leader election is performed based on the second target master election request.
[0107] In one embodiment, the cluster leader election module 403 is further used for:
[0108] When it is detected that the conditions for changing the master node election are met, the number of requests for the second target master election request is detected based on the third request information of the second target master election request; when the number of requests is greater than the first preset request number threshold, the second cluster node requested by the second target master election request is used as the cluster master node.
[0109] In one embodiment, the master node selection change condition includes one of the following:
[0110] The third master election request sent by the second node is not detected in the second synchronization time slot; the third master election request is detected in the second synchronization time slot, and the number of requests for the third master election request is greater than the second preset request number threshold, wherein the second preset request number threshold is less than the first preset request number threshold.
[0111] In one embodiment, the cluster leader selection device is further used for:
[0112] 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; the current running state is changed to the offline state.
[0113] Each module in the above cluster master election device can be implemented in whole or in part by software, hardware, or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.
[0114] In an exemplary embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as shown in FIG. Figure 7 As shown. 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 via a system bus, and the communication interface, the display unit and the input device are connected to the system bus via the input / output interface. The processor of the computer device is used 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 operation of the operating system and the computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, NFC (near field communication) or other technologies. When the computer program is executed by the processor, a cluster master election method is implemented. Those skilled in the art can understand that Figure 7 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may 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 further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above method embodiments when executing the computer program.
[0116] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.
[0117] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above method embodiments when executed by a processor.
[0118] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium 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, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0119] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, 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.
[0120] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A cluster leader election method, characterized in that: Applied to a first node of a cluster, the method comprises: When it is detected that the cluster leader election condition is met, sending a first target leader election request to the leader election request sharing area in the cluster leader election state; Reading all the master election requests in the master election request sharing area within the first synchronization time slot to which the first target master election request belongs, wherein all the master election requests are composed of the master election requests sent by all the master election nodes in the cluster; A cluster leader is elected based on all the leader election requests.
2. The method according to claim 1, characterized in that The cluster master election condition includes one of the following: Updating the master node status of the first node within the first synchronization time slot fails; Detecting that the heartbeat signal of the cluster master node has expired within the first synchronization time slot; A second master election request sent by a second node is detected in the first synchronization time slot, wherein the second node refers to any master election node in the cluster except the first node.
3. The method according to claim 2, characterized in that The all the 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 sharing area; and the cluster leader election based on the all the leader election requests includes: If it is detected that the first cluster node commonly requested by all the master election requests is online, first request information of the first target master election request and second request information of the other master election requests are respectively obtained, and cluster leader election is performed based on the information comparison result of the first request information and the second request information; 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.
4. The method according to claim 3, characterized in that The performing cluster leader election based on the second target leader election request includes: When it is detected that the master election node change condition is met, detecting the request count of the second target master election request according to the third request information of the second target master election request; When the number of requests is greater than a first preset request number threshold, the second cluster node requested by the second target master election request is used as the cluster master node.
5. The method according to claim 4, characterized in that The master node election change condition includes one of the following: not detecting a third master election request sent by the second node in the second synchronization time slot; A third master election request is detected in the second synchronization time slot, and the request times of the third master election request are greater than a second preset request times threshold, wherein the second preset request times threshold is less than the first preset request times threshold.
6. The method according to claim 1, characterized in that The method further comprises one of the following: Change 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; The current running state is changed to an offline state.
7. A cluster leader selection device, characterized in that: Applied to a first node of a cluster, the device comprises: 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; a reading module, configured to read all the master election requests in the master election request sharing area within the first synchronization time slot to which the first target master election request belongs, wherein all the master election requests are composed of the master election requests sent by all the master election nodes of the cluster; The cluster leader election module is used to elect a cluster leader based on all the leader election requests.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
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
Method and apparatus for failover processing
US20170364423A1
Master / backup database cluster and master selection method therefor, computing device, and storage medium
WO2024083259A1