Node master selection method and device

By providing a master selection method for ordinary nodes in a distributed system, adjusting the candidate time and node status according to the network connection status, the problem that the master node cannot adjust according to the network state is solved, and the work efficiency and number of network connections of the master node are improved.

CN119728706AActive Publication Date: 2025-03-28THE 711TH RES INST OF CHINA STATE SHIPBUILDING CORP
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202411663947.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-20
Publication Date
2025-03-28
Estimated Expiration
2044-11-20

AI Technical Summary

Technical Problem

In the prior art, the master node cannot adjust according to the network status of the nodes in the network, resulting in problems with the master node during network scheduling.

Method used

Provide a method of selecting a master at the node. By confirming the network connection status of an ordinary node, determining the current candidate time, and determining whether the heartbeat information of the master node is received before the end of the candidate time. If it is not received, it will be changed to a candidate node to obtain the number of support votes. If the number of support votes is greater than the preset number of votes, it will be changed to a master node.

Benefits of technology

The master node is adjusted according to the network connection status, ensuring that the master node has more network connections, improving the working efficiency of the master node, and avoiding problems that the master node has during network scheduling.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119728706A_ABST
    Figure CN119728706A_ABST
Patent Text Reader

Abstract

The invention discloses a node master selection method and device, belongs to the technical field of distributed networks, is applied to a common node, and comprises the following steps: confirming a network connection state of the common node; determining the current candidate duration of the common node based on the network connection state; before the end of the current candidate duration, if it is determined that the normal node does not receive heartbeat information sent by the main node, changing the state of the normal node into a candidate node; obtaining the number of votes supported by the candidate nodes; and determining that the number of votes supported is greater than the preset number of votes winned by election, and changing the candidate node as a main node. According to the node master selection method provided by the invention, master selection can be carried out according to the network connection state of the node, so that the master node has more network connections, the working efficiency of the master node is improved, and the problem of the master node during network scheduling is avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present application relate to the field of distributed network technology, and more particularly to a node master election method and device. Background Art

[0002] In a distributed system, the master election method is to select a master node from multiple nodes to be responsible for task scheduling, load balancing, unified external services, etc., to ensure the consistency and high availability of distributed system data and services. In the current master election algorithm, during or after the master election, the master node cannot adjust according to the network status of the nodes in the network, resulting in problems when the master node performs network scheduling. Summary of the invention

[0003] The embodiment of the present application aims to provide a node master election method and device to solve the technical problem in the prior art that the master node cannot be adjusted according to the network status of the nodes in the network.

[0004] In order to solve the above technical problems, the embodiments of the present application disclose the following technical solutions:

[0005] In a first aspect, a node master election method is provided, which is applied to a common node and includes:

[0006] Confirming the network connection status of the common node;

[0007] Determine the current candidate duration of the common node based on the network connection state;

[0008] Before the current candidate duration ends, if it is determined that the common node has not received the heartbeat information sent by the master node, the state of the common node is changed to a candidate node;

[0009] Obtain the number of support votes for the candidate node;

[0010] Confirm that the number of support votes is greater than the preset number of election winning votes, and change the candidate node to the master node.

[0011] In combination with the first aspect, before the current candidate duration ends, the method further includes:

[0012] Determine that the common node receives the heartbeat information sent by the master node;

[0013] If it is determined that the heartbeat information contains new master node information and the new master node information is the common node, the common node is changed to a master node.

[0014] In combination with the first aspect, the method for confirming the network connection status of the common node includes:

[0015] Send test data to all networks;

[0016] Acquire feedback of the network based on the test data to obtain the number of networks in a disconnected state.

[0017] In combination with the first aspect, the method for determining the current candidate duration of the common node based on the network connection state includes:

[0018] Obtaining initial restriction parameters of the common node;

[0019] Determine a minimum candidate duration and a maximum candidate duration based on the initial restriction parameters;

[0020] Randomly determine the current candidate duration between the minimum candidate duration and the maximum candidate duration;

[0021] The initial restriction parameters include an initial minimum candidate duration, an initial maximum candidate duration and a network impact factor constant.

[0022] In combination with the first aspect, the method for determining the minimum candidate duration based on the initial restriction parameter includes:

[0023] Obtaining a first product of the initial minimum candidate duration, the number of networks in the disconnected state, and the network impact factor constant;

[0024] The sum of the initial minimum candidate duration and the first product is used as the minimum candidate duration.

[0025] In combination with the first aspect, the method for determining the maximum candidate duration based on the initial restriction parameter includes:

[0026] Obtaining a second product of the initial maximum candidate duration, the number of networks in the disconnected state, and the network impact factor constant;

[0027] The sum of the initial maximum candidate duration and the second product is used as the maximum candidate duration.

[0028] In combination with the first aspect, before confirming the network connection status of the common node, the method further includes:

[0029] Initializing the common node and setting initial restriction parameters;

[0030] The initial restriction parameters include at least one of an initial minimum candidate duration, an initial maximum candidate duration, a network influence factor constant, an election winning number of votes, and a current term.

[0031] In combination with the first aspect, the method for obtaining the number of support votes for the candidate node includes:

[0032] The candidate node casts a support vote for itself through the network;

[0033] The candidate node obtains support tickets from other nodes through the network;

[0034] All support votes of the candidate node in the network are counted to obtain the number of support votes.

[0035] In a second aspect, a node master selection device is provided, which is applied to a common node, and the device includes:

[0036] A first operation module, wherein the first operation module is configured to confirm the network connection status of the common node;

[0037] A second operation module, the second operation module is configured to determine a current candidate duration of the common node based on the network connection state;

[0038] A first processing module, wherein the first processing module is configured to change the state of the ordinary node to a candidate node if it is determined that the ordinary node has not received the heartbeat information sent by the master node before the current candidate duration ends;

[0039] A third operation module, wherein the third operation module is configured to obtain the number of support votes for the candidate node;

[0040] The second processing module is configured to confirm that the number of support votes is greater than a preset number of election winning votes and change the candidate node to a master node.

[0041] In a third aspect, a node master election method is provided, which is applied to a master node and includes:

[0042] Obtaining a first network number of the master node;

[0043] Get the second network number of other nodes;

[0044] It is determined that the first number of networks and the second number of networks of any one or more of the nodes meet a first preset condition, and the master node selects one of the one or more nodes as a new master node.

[0045] In combination with the third aspect, the method for determining whether the first number of networks and the second number of networks of any one or more of the nodes satisfy a first preset condition includes:

[0046] comparing the first number of networks with the second number of networks;

[0047] If the number of the first networks is less than the number of the second networks, it is determined that the first preset condition is met.

[0048] In conjunction with the third aspect, the method by which the master node selects one or more of the nodes as a new master node includes:

[0049] The master node sends a first heartbeat message to all the nodes, where the first heartbeat message includes address information of the selected node;

[0050] The selected node becomes the new master node immediately after receiving the first heartbeat information.

[0051] In combination with the third aspect, the method further includes:

[0052] The new master node sends a second heartbeat message to all other nodes;

[0053] The original master node becomes a common node after receiving the second heartbeat information.

[0054] In a fourth aspect, a node master selection device is provided, which is applied to a master node, and the device includes:

[0055] A first acquisition module, wherein the first acquisition module is configured to acquire a first network number of the master node;

[0056] A second acquisition module, wherein the second acquisition module is configured to acquire the second network number of other nodes;

[0057] A processor module, wherein the processor module is configured to determine that the first number of networks and the second number of networks of any one or more of the nodes meet a first preset condition, and the master node selects one of the one or more nodes as a new master node.

[0058] One of the above technical solutions has the following advantages or beneficial effects:

[0059] The embodiment of the present application provides a node master selection method, which is applied to ordinary nodes, including: confirming the network connection status of the ordinary node; determining the current candidate duration of the ordinary node based on the network connection status; before the end of the current candidate duration, determining that the ordinary node has not received the heartbeat information sent by the master node, then changing the status of the ordinary node to a candidate node; obtaining the number of support votes for the candidate node; confirming that the number of support votes is greater than the preset number of winning votes for the election, and changing the candidate node to the master node. The node master selection method provided by the present application can select a master according to the network connection status of the node, thereby ensuring that the master node has more network connections, improving the work efficiency of the master node, and avoiding problems when the master node performs network scheduling.

[0060] The embodiment of the present application also provides a node master selection method, which is applied to the master node, including: obtaining the first network number of the master node; obtaining the second network number of other nodes; determining that the first network number and the second network number of any one or more nodes meet the first preset condition, and the master node selects one of the any one or more nodes as a new master node. The node master selection method provided by the present application can adjust the master node according to the network connection status of the node, thereby ensuring that the master node has more network connections, improving the working efficiency of the master node, and avoiding problems when the master node performs network scheduling. BRIEF DESCRIPTION OF THE DRAWINGS

[0061] The technical solution and other beneficial effects of the present application will be made apparent by describing in detail the specific implementation methods of the present application in conjunction with the accompanying drawings.

[0062] Figure 1 A schematic diagram of the steps of the master election method applied to a common node provided in an embodiment of the present application;

[0063] Figure 2 A schematic diagram of the steps of a method for determining a current candidate duration of a common node provided in an embodiment of the present application;

[0064] Figure 3 A schematic diagram of module connections of a master selection device applied to a common node provided in an embodiment of the present application;

[0065] Figure 4 A schematic diagram of a master selection method applied to a master node is provided for an embodiment of the present application;

[0066] Figure 5 A schematic diagram of module connections of a master selection device applied to a master node provided in an embodiment of the present application;

[0067] Figure 6 A schematic diagram of the topological structure of a distributed system provided in an embodiment of the present application;

[0068] Figure 7 A schematic diagram of the node state transition relationship provided in an embodiment of the present application. DETAILED DESCRIPTION

[0069] The technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. In the description of the present application, it should be understood that the terms "first" and "second" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. In the description of the present application, the meaning of "multiple" is two or more, unless otherwise clearly and specifically defined.

[0070] Glossary:

[0071] Master Node: A master node is a node with special permissions and responsibilities, responsible for coordinating and managing the work of other nodes. A master node is usually a centralized entity in a distributed system, with higher permissions and control rights, used to manage and monitor the operation of the entire system.

[0072] Candidate Node: A candidate node is a node that has the qualifications and conditions to become a master node in a distributed system. Candidate nodes are usually nodes that participate in the election to become the new master node when the master node fails or needs to be re-elected.

[0073] Regular Node: A regular node is a node in a distributed system that does not have the special authority and responsibility to become a master node or candidate node. Regular nodes are general participants in the system, responsible for performing tasks assigned to them and processing requests from other nodes.

[0074] The specific implementation methods of the present application are described below by way of examples:

[0075] like Figure 1 and Figure 7 As shown, the embodiment of the present application provides a node master election method, which is applied to ordinary nodes and includes:

[0076] S1: Confirm the network connection status of common nodes.

[0077] The specific method includes: confirming the network connection status of the common node, and the method includes: sending test data to all networks; obtaining feedback from the network based on the test data, and obtaining the number of networks in a disconnected state. The node sends test data to each network, and each network returns corresponding feedback data to the node according to the content of the test data after receiving the test data, and the node confirms the number of networks in a connected state according to the received feedback data; if the node does not receive feedback data for more than a certain time, the number of networks in a disconnected state is confirmed, and the number of networks in a connected state and the number of networks in a disconnected state are counted. It should be noted that the test data may include heartbeat information, which is a small data packet or signal sent periodically, which can be used to detect the connection status and availability between network devices or hosts. Heartbeat information is similar to the beating of a human heart and is used to confirm whether a network device or host is still active. Heartbeat information is usually sent periodically by a network device, server or application to other devices or systems. The receiver monitors the arrival of the heartbeat information to determine whether the sender is still active. If the receiver does not receive the heartbeat information within a certain period of time, it can be inferred that the sender may have a fault or disconnect.

[0078] It can be understood that by obtaining the number of networks that are connected or disconnected from the node, it is easier for the node to select a master according to the network connection status, ensuring that the master node has more network connections, improving the work efficiency of the master node, and avoiding problems when the master node performs network scheduling.

[0079] S2: Determine the current candidate duration of the common node based on the network connection status.

[0080] like Figure 2 As shown, the specific methods include:

[0081] S201: Acquire initial restriction parameters of common nodes, wherein the initial restriction parameters include an initial minimum candidate duration, an initial maximum candidate duration, and a network impact factor constant.

[0082] Specifically, in an embodiment of the present application, before confirming the network connection status of an ordinary node, it is necessary to initialize the ordinary node and set the initial restriction parameters; the initial restriction parameters include the initial minimum candidate duration, the initial maximum candidate duration, the network influence factor constant, the number of votes for election winning, and the current term. Among them, the value range of the initial minimum candidate duration is 100ms to 300ms, for example, the initial minimum candidate duration is 100ms, 110ms, 120ms, 130ms, 140ms, 150ms, 160ms, 170ms, 180ms, 190ms, 200ms, 210ms, 220ms, 230ms, 240ms, 250ms, 260ms, 270ms, 280ms, 290ms, 300ms, any one or any two of the range values. The value range of the initial maximum candidate duration is 400ms to 600ms, for example, the initial maximum candidate duration is any one or any two of 400ms, 410ms, 420ms, 430ms, 440ms, 450ms, 460ms, 470ms, 480ms, 490ms, 500ms, 510ms, 520ms, 530ms, 540ms, 550ms, 560ms, 570ms, 580ms, 590ms, 600ms. The value range of the network impact factor constant is 0 to 1, for example, the network impact factor constant is 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9. The number of votes for winning the election is 0 to 2n, where n is a positive integer representing the total number of nodes, and the current term is 0.

[0083] S202: Determine a minimum candidate duration and a maximum candidate duration based on the initial restriction parameters.

[0084] Specifically, in the embodiment of the present application, after the initial restriction parameters are determined, the minimum candidate duration is confirmed, and the method includes: obtaining the first product of the initial minimum candidate duration, the number of networks in the disconnected state, and the network impact factor constant; and taking the sum of the initial minimum candidate duration and the first product as the minimum candidate duration. Specifically, if it is determined that the number of networks in the disconnected state is 2, then according to the value range of the initial minimum candidate duration, the initial minimum candidate duration is selected as 200ms, and according to the value range of the network impact factor constant, the network impact factor parameter is selected as 0.5. Then, according to the above calculation formula, the minimum candidate duration of the ordinary node can be obtained as:

[0085] Minimum candidate duration = 200 + 200 × 2 × 0.5;

[0086] The final minimum candidate duration is 400ms.

[0087] In an embodiment of the present application, after the minimum candidate duration is calculated, the maximum candidate duration is determined based on the initial restriction parameters. The method includes: obtaining the second product of the initial maximum candidate duration, the number of networks in the disconnected state, and the network impact factor constant; and taking the sum of the initial maximum candidate duration and the second product as the maximum candidate duration.

[0088] Specifically, similarly, if the number of networks in the disconnected state is determined to be 2, the initial maximum candidate duration is selected as 500ms according to the value range of the initial maximum candidate duration, and the network impact factor parameter is selected as 0.5 according to the value range of the network impact factor constant, then the maximum candidate duration of the ordinary node can be obtained according to the above calculation formula:

[0089] Maximum candidate duration = 500 + 500 × 2 × 0.5;

[0090] The final minimum candidate duration is 1000ms.

[0091] S203: Randomly determine a current candidate duration between the minimum candidate duration and the maximum candidate duration.

[0092] Specifically, in an embodiment of the present application, after the minimum candidate duration and the maximum candidate duration are determined, a duration is selected between the minimum candidate duration and the maximum candidate duration as the current candidate duration of the common node through a random algorithm or other algorithms.

[0093] It is understandable that since the number of networks in connected and disconnected states with each node is different, the candidate time required for each node is also different. The current candidate time of each node is determined by the minimum candidate time and the maximum candidate time, thereby ensuring that each node can get sufficient waiting time to avoid the node being unable to receive relevant signals in time.

[0094] S3: Before the end of the current candidate duration, if it is determined that the ordinary node has not received the heartbeat information sent by the master node, the state of the ordinary node is changed to a candidate node. Specifically, when a master node already exists in the network, the master node will periodically send heartbeat information to all nodes to determine the connection status of other nodes, and also synchronize the corresponding information to all other nodes. When an ordinary node is first connected to a distributed system, it will wait for a period of time, which is the current candidate duration. If the heartbeat information sent by the master node is not received before the end of the current candidate duration, it means that there is no master node in the currently connected distributed system. At this time, all nodes need to enter the master selection program, and the state of the ordinary node is also changed to a candidate node. It is worth noting that the current candidate duration needs to be greater than the interval duration of the heartbeat information, so as to avoid the current candidate duration of the ordinary node being between two heartbeat messages, resulting in errors in the state management of the node.

[0095] In an embodiment of the present application, before the end of the current candidate duration, the method also includes: determining that the ordinary node receives the heartbeat information sent by the master node; determining that the heartbeat information contains new master node information, and the new master node information is for an ordinary node, then changing the ordinary node to a master node. When an ordinary node is first connected to a distributed system, and receives a heartbeat message from the master node before the end of the current candidate duration, it means that there is a master node in the currently connected distributed system. If there is new master node information in the received heartbeat information, and the new master node information is for the ordinary node, the ordinary node immediately becomes the new master node. It is worth noting that if the new master node information is for other ordinary nodes, the selected ordinary node becomes the new master node, and the status of other ordinary nodes remains unchanged.

[0096] It is worth noting that before the end of the current candidate period, if the ordinary node receives the heartbeat information from the master node, the ordinary node can change its current term value to the same as the term value of the master node; if the heartbeat information from the master node is not received, the term value must be increased by 1 while changing the status of the ordinary node to a candidate node.

[0097] It can be understood that by setting the current candidate duration, ordinary nodes can determine whether there is a master node in the system when they are connected to the distributed system, and participate in the candidate when there is no master node, so that the distributed system can quickly select the master node, thereby improving the efficiency of the system's master selection.

[0098] S4: Get the number of support votes of the candidate node.

[0099] The specific method includes: the candidate node casts a support vote for itself through the network; the candidate node obtains support votes from other nodes through the network; and counts all the support votes of the candidate node in the network to obtain the number of support votes. It is worth noting that after the candidate node updates its term value, it casts a support vote for itself through the network, and at the same time requests other nodes to vote for itself through the network; when other nodes receive the voting request, they will compare their own term value with the term value of the other party (i.e., the candidate node requesting the vote). If their own term value is greater than the term value of the other party, they will cast a negative vote for the other party; if their own term value is equal to the term value of the other party and they have not voted for other candidate nodes, they will vote for the other party; if their own term value is equal to the term value of the other party but they have voted for other candidate nodes, they will vote against the other party; if their own term value is less than the term value of the other party, they will update their own term value to the same term value as the other party and vote for the other party.

[0100] It can be understood that by comparing the node's own term value with the other node's term value to determine whether to vote for other nodes, the voting results can be made more unique, avoiding the situation where multiple nodes have duplicate votes. At the same time, the voting can also quickly select a suitable master node.

[0101] S5: Confirm that the number of support votes is greater than the preset number of votes to win the election, and change the candidate node to the master node.

[0102] Specifically, the number of winning votes in the election can be determined when the node is initialized. The value range of the winning votes in the election is 0 to 2n, where n is the number of nodes in a connected state. If the primary node is selected more quickly from among many nodes, the value of the winning votes in the election can be close to 0, such as 1, 2 or 3. If the appropriate node is selected from among many nodes at one time, the value of the winning votes in the election can be close to 2n. For example, when n is 3, the value of the winning votes in the election can be 5, 6, etc.

[0103] It can be understood that by associating the value of the winning votes in the election with the number of nodes in a connected state, it is possible to avoid the situation where a node is disconnected and still be able to elect a master node, and to avoid the situation where the voting results of each node cannot exceed the winning votes in the election and the master node cannot be elected when multiple nodes are disconnected.

[0104] It is worth noting that the content stated above in the embodiment of the present application only discloses one node. When there are multiple nodes in a distributed system, each node will adopt the above-mentioned node master election method to elect a master before connecting to the distributed system, and finally determine the master node from multiple nodes.

[0105] In summary, the node master selection method provided in the embodiment of the present application converts the state to a candidate node when the ordinary node does not receive the heartbeat information of the master node after waiting for a corresponding period of time, and the candidate node can become the master node after winning the vote. The master node can also re-determine a new master node according to the network connection status of the node, thereby ensuring that the master node has more network connections, improving the working efficiency of the master node, and avoiding problems when the master node performs network scheduling.

[0106] like Figure 3 As shown, an embodiment of the present application also provides a node master election device, which is applied to ordinary nodes, and the device includes: a first operation module, the first operation module is configured to confirm the network connection status of the ordinary node; a second operation module, the second operation module is configured to determine the current candidate duration of the ordinary node based on the network connection status; a first processing module, the first processing module is configured to determine that the ordinary node has not received the heartbeat information sent by the master node before the end of the current candidate duration, and then change the status of the ordinary node to a candidate node; a third operation module, the third operation module is configured to obtain the number of support votes of the candidate node; a second processing module, the second processing module is configured to confirm that the number of support votes is greater than the preset number of election winning votes, and change the candidate node to the master node. Specifically, the node master election device provided in the embodiment of the present application determines the network connection status of the ordinary node through the first operation module, determines the current candidate duration of the ordinary node through the second operation module, and the first processing module determines whether the heartbeat information of the master node is received based on the current candidate duration. If the heartbeat information of the master node is received and it itself is the new master node in the heartbeat information, the ordinary node is confirmed as the new master node, otherwise it remains an ordinary node. If the heartbeat information of the master node is not received, the ordinary node is converted into a candidate node to participate in the candidate of the master node, the number of support votes for the candidate node is obtained through the third operation module, and the second processing module is used to confirm that the number of support votes is greater than the number of winning votes in the election, then the candidate node is changed to the master node.

[0107] It is understandable that the node master selection device provided in the embodiment of the present application converts the state to a candidate node when the ordinary node does not receive the heartbeat information of the master node after waiting for a corresponding period of time, and the candidate node can become the master node after winning the vote. The master node can also re-determine a new master node according to the network connection status of the node, thereby ensuring that the master node has more network connections, improving the working efficiency of the master node, and avoiding problems when the master node performs network scheduling.

[0108] like Figure 4 and Figure 7 As shown, the embodiment of the present application also provides a node master selection method, which is applied to the master node, including:

[0109] F1: Get the first network number of the master node.

[0110] Specifically, the master node determines the first number of networks among all networks connected to the master node by sending heartbeat information or other detection data; wherein, the first number of networks is used to characterize the number of networks in a connected state among all networks on the master node (hereinafter referred to as the number of network connections), and the number of networks in a disconnected state among all networks on the master node can also be obtained.

[0111] F2: Get the second network number of other nodes.

[0112] Specifically, the master node obtains the number of second networks of other nodes by sending detection data to other nodes, or obtains the number of second networks by obtaining heartbeat information of other nodes; wherein the number of second networks is used to characterize the number of network connections in all networks of each node, and the number of networks in disconnected state in all networks on other nodes can also be obtained.

[0113] F3: Determine whether the first network number and the second network number of any one or more nodes meet the first preset condition, and the master node selects one of any one or more nodes as a new master node. The specific method includes: comparing the first network number with the second network number; if the first network number is less than the second network number, it is determined that the first preset condition is met. Specifically, compare the number of all network connections on the master node with each other node. If there are one or more nodes in the other nodes whose number of network connections is greater than the number of network connections of the master node, then the node with the largest number of network connections among the other nodes is determined as the new master node; if there are multiple nodes with the largest number of network connections, then randomly select one of the multiple nodes that meet the conditions as the master node. If the number of network connections of any of the other nodes is less than or equal to the number of network connections of the master node, the master node remains unchanged.

[0114] It can be understood that this method can determine the node with the largest number of network connections among other nodes, and replace the main node when the conditions are met, thereby ensuring that the number of network connections in the distributed system is always the main node.

[0115] In an embodiment of the present application, a method for a master node to select one or more nodes as a new master node includes: the master node sends a first heartbeat message to all nodes, the first heartbeat message includes the address information of the selected node; the selected node immediately becomes the new master node after receiving the first heartbeat message. Specifically, when it is determined that the number of networks in a connected state among ordinary nodes or candidate nodes is greater than the number of networks in a connected state on the master node, the master node sends a first heartbeat message (also called abdication heartbeat message) to all other nodes, the first heartbeat message includes the address information of the new master node, and after the selected ordinary node or candidate node receives the first heartbeat message and finds that the address information contains its own address, it becomes the new master node.

[0116] In an embodiment of the present application, the method further includes: the new master node sends a second heartbeat message to all other nodes; the original master node becomes a common node after receiving the second heartbeat message. Specifically, after the common node or candidate node becomes the new master node, the second heartbeat message is first sent to all nodes (including the original master node), and other common nodes or candidate nodes can become common nodes after receiving the second heartbeat message.

[0117] To sum up the above content, the node master election method provided in the embodiment of the present application is that when there is a master node in a distributed system, the master node obtains the number of network connections of other nodes in real time by sending heartbeat information. When it is determined that the number of network connections of other nodes is more than its own, the position of the master node is relinquished to the node with a larger number of network connections, thereby ensuring that the master node in the distributed system has the largest number of network connections.

[0118] like Figure 5 As shown, the embodiment of the present application also provides a node master selection device, which is applied to the master node, and the device includes: a first acquisition module, the first acquisition module is configured to obtain the first network number of the master node; a second acquisition module, the second acquisition module is configured to obtain the second network number of other nodes; a processor module, the processor module is configured to determine that the first network number and the second network number of any one or more nodes meet the first preset condition, and the master node selects one of any one or more nodes as a new master node. Specifically, the node master selection device provided in the embodiment of the present application obtains the number of networks in a connected state (hereinafter referred to as the number of network connections) in all networks on the master node through the first acquisition module; obtains the number of network connections of all other nodes through the second acquisition module; compares the number of network connections of the master node with the number of network connections of other nodes through the processor module, and if the number of network connections of other nodes is greater than the number of network connections of the master node, the processor module randomly selects one from other nodes that meet the conditions to become the new master node.

[0119] The content of this application is further described below by examples:

[0120] like Figure 6 and Figure 7 As shown, the distributed system includes four nodes and three switches, namely, node 1, node 2, node 3, node 4, switch 1, switch 2 and switch 3. The system adopts a fully connected network topology structure, each node has three independent network ports, each network port is connected to three switches, forming three independent networks. The four nodes of the system all use the master election method provided in the above embodiment to elect a master. The following specifically describes the master election process of a node:

[0121] Step 1: Initialize the master election program, set the initial minimum candidate time to 200ms, the initial maximum candidate time to 500ms, the network impact factor constant to 0.5, the number of winning votes to 4, and the current term to 0;

[0122] Step 2: Set the node status to a normal node, and calculate the minimum candidate duration (200ms) and maximum candidate duration (500ms) based on the initial minimum candidate duration (200ms), the initial maximum candidate duration (500ms), the network impact factor constant (0.5), and the number of disconnected networks (for example, if there is no disconnected network at this moment, it is 0), and then select a random number in the interval as the current candidate duration (for example, 300ms);

[0123] Step 3: Wait for the current candidate duration to time out, that is, the program waits for 300ms;

[0124] Step 4: During the waiting period, if a heartbeat message from the master node is received, the current term is set to the same term as the master node, and then return to step 2; if a heartbeat message from the master node is received, and the heartbeat message contains the new master node information and is for itself, it immediately becomes the master node and sends heartbeat messages to all other nodes in real time;

[0125] Step 5: When the waiting time has expired, the node's status is immediately converted to a candidate node, and the current term is increased by 1. The node casts a vote of approval for itself on each network, that is, it casts 3 votes for itself, and then requests votes from other nodes in the entire network (taking node 1 as an example: it obtains votes from nodes 2, 3, and 4 through switch 1, and then obtains votes from nodes 2, 3, and 4 through switch 2, and then obtains votes from nodes 2, 3, and 4 through switch 3). The total number of votes should be 3+3*3, that is, 12;

[0126] Step 6: When other nodes receive a voting request, if their current term is greater than the current term of the candidate node, they will vote against it; if their current term is equal to the current term of the candidate node and they have not voted in favor of other candidate nodes, they will vote in favor of it; if their current term is equal to the current term of the candidate node and they have voted in favor of other candidate nodes, they will vote against it; if their current term is less than the current term of the candidate node, they will set their current term to the same term as the candidate node and vote in favor of it;

[0127] Step 7: The candidate node counts the voting results. If the number of votes obtained is greater than or equal to the set number of votes to win the election, it immediately wins the election and becomes the master node, and sends heartbeat messages to all other nodes in real time. Otherwise, the election fails and returns to step 2.

[0128] An embodiment of the present application also provides an electronic device, including a memory and a processor; the memory is used to store a computer program; the processor is used to execute the computer program by using the node master election method provided in any of the above embodiments.

[0129] The above is a detailed introduction to a node master election method and device provided in an embodiment of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the technical solution and its core idea of ​​the present application. Ordinary technical personnel in this field should understand that they can still modify the technical solutions recorded in the aforementioned embodiments, or replace some of the technical features therein with equivalents; and these modifications or replacements do not make the essence of the corresponding technical solution deviate from the scope of the technical solution of the embodiments of the present application.

Claims

1. A node master election method, characterized in that: Applicable to common nodes, including: Confirming the network connection status of the common node; Determine the current candidate duration of the common node based on the network connection state; Before the current candidate duration ends, if it is determined that the common node has not received the heartbeat information sent by the master node, the state of the common node is changed to a candidate node; Obtain the number of support votes for the candidate node; Confirm that the number of support votes is greater than the preset number of election winning votes, and change the candidate node to the master node.

2. The node master election method according to claim 1, characterized in that: Before the current candidate duration ends, the method further includes: Determine that the common node receives the heartbeat information sent by the master node; If it is determined that the heartbeat information contains new master node information and the new master node information is the common node, the common node is changed to a master node.

3. The node master election method according to claim 1, characterized in that: The method for confirming the network connection status of the common node comprises: Send test data to all networks; Acquire feedback of the network based on the test data to obtain the number of networks in a disconnected state.

4. The node master election method according to claim 3, characterized in that: The method for determining the current candidate duration of the common node based on the network connection state includes: Obtaining initial restriction parameters of the common node; Determine a minimum candidate duration and a maximum candidate duration based on the initial restriction parameters; Randomly determine the current candidate duration between the minimum candidate duration and the maximum candidate duration; The initial restriction parameters include an initial minimum candidate duration, an initial maximum candidate duration and a network impact factor constant.

5. The node master election method according to claim 4, characterized in that: The method for determining the minimum candidate duration based on the initial restriction parameter comprises: Obtaining a first product of the initial minimum candidate duration, the number of networks in the disconnected state, and the network impact factor constant; The sum of the initial minimum candidate duration and the first product is used as the minimum candidate duration.

6. The node master election method according to claim 5, characterized in that: The method for determining the maximum candidate duration based on the initial restriction parameter comprises: Obtaining a second product of the initial maximum candidate duration, the number of networks in the disconnected state, and the network impact factor constant; The sum of the initial maximum candidate duration and the second product is used as the maximum candidate duration.

7. The node master election method according to claim 4, characterized in that: Before confirming the network connection status of the common node, the method further includes: Initializing the common node and setting initial restriction parameters; The initial restriction parameters include at least one of an initial minimum candidate duration, an initial maximum candidate duration, a network influence factor constant, an election winning number of votes, and a current term.

8. The node master election method according to claim 1, characterized in that: The method for obtaining the number of support votes for the candidate node includes: The candidate node casts a support vote for itself through the network; The candidate node obtains support tickets from other nodes through the network; All support votes of the candidate node in the network are counted to obtain the number of support votes.

9. A node master selection device, characterized in that: Applied to a common node, the device comprises: A first operation module, wherein the first operation module is configured to confirm the network connection status of the common node; A second operation module, the second operation module is configured to determine a current candidate duration of the common node based on the network connection state; A first processing module, wherein the first processing module is configured to change the state of the ordinary node to a candidate node if it is determined that the ordinary node has not received the heartbeat information sent by the master node before the current candidate duration ends; A third operation module, wherein the third operation module is configured to obtain the number of support votes of the candidate node; The second processing module is configured to confirm that the number of support votes is greater than a preset number of election winning votes and change the candidate node to a master node.

10. A node master election method, characterized in that: Applied to the master node, including: Obtaining a first network number of the master node; Get the second network number of other nodes; It is determined that the first number of networks and the second number of networks of any one or more of the nodes meet a first preset condition, and the master node selects one of the one or more nodes as a new master node.

11. The node master election method according to claim 10, characterized in that: The method for determining whether the first number of networks and the second number of networks of any one or more nodes satisfy a first preset condition includes: comparing the first number of networks with the second number of networks; If the number of the first networks is less than the number of the second networks, it is determined that the first preset condition is met.

12. The node master election method according to claim 11, characterized in that: The method for the master node to select one of any one or more nodes as a new master node includes: The master node sends a first heartbeat message to all the nodes, where the first heartbeat message includes address information of the selected node; The selected node becomes the new master node immediately after receiving the first heartbeat information.

13. The node master election method according to claim 10, characterized in that: The method further comprises: The new master node sends a second heartbeat message to all other nodes; The original master node becomes a common node after receiving the second heartbeat information.

14. A node master selection device, characterized in that: Applied to a master node, the device comprises: A first acquisition module, wherein the first acquisition module is configured to acquire a first network number of the master node; A second acquisition module, wherein the second acquisition module is configured to acquire the second network number of other nodes; A processor module, wherein the processor module is configured to determine that the first number of networks and the second number of networks of any one or more of the nodes meet a first preset condition, and the master node selects one of the one or more nodes as a new master node.

Citation Information

Patent Citations

  • Cluster election method

    CN115378936A

  • Randomization of heartbeat communications between multiple partition groups

    CN116075819A

  • Node election method and device, electronic equipment and storage medium

    CN118316777A

  • Timeout time setting method and device, equipment and storage medium

    CN118474049A

  • Data processing method and related device

    CN118827687A