Node master election method and apparatus

By confirming the network connection status of ordinary nodes in a distributed system and adjusting the master node based on the candidate duration and the number of supporting votes, the problem of the master node being unable to adjust according to the network status is solved, thus improving the network connection and working efficiency of the master node.

CN119728706BActive Publication Date: 2025-11-07THE 711TH RES INST OF CHINA STATE SHIPBUILDING CORP
View PDF 1 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

In existing technologies, the master node cannot adjust according to the network status of nodes in the network, which leads to problems during network scheduling.

Method used

By confirming the network connection status of ordinary nodes, the current candidate duration is determined. If no heartbeat information from the master node is received before the candidate duration ends, the ordinary node status is changed to a candidate node to obtain the number of support votes. After confirming that the number of support votes is greater than the preset number of winning votes, the candidate node is changed to the master node.

Benefits of technology

Ensuring the master node has more network connections improves its efficiency and avoids potential problems during network scheduling.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119728706B_ABST
    Figure CN119728706B_ABST
Patent Text Reader

Abstract

The application discloses a node master selection method and device, belongs to the technical field of distributed networks, and is applied to a common node and comprises the following steps: confirming the network connection state of the common node; determining the current candidate duration of the common node based on the network connection state; before the current candidate duration ends, determining that the common node does not receive heartbeat information sent by a master node, and then changing the state of the common node to a candidate node; acquiring the support vote number of the candidate node; and confirming that the support vote number is greater than a preset election winning vote number, and then changing the candidate node to the master node. The node master selection method provided by the application can select a master node 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 problems of the master node in network scheduling are avoided.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present application relate to the technical field of distributed network, and in particular, to a node master election method and device. BACKGROUND

[0002] In a distributed system, a master election method is a method of selecting a master node from multiple nodes for performing tasks such as task scheduling, load balancing, unified external service, and ensuring consistency and high availability of data and services of the distributed system. In the current master election algorithm, the master node cannot be adjusted according to the network state of the nodes in the network during the master election process or after the master election is completed, which causes problems in network scheduling of the master node. SUMMARY

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

[0004] To solve the above technical problem, embodiments of the present application disclose the following technical solutions:

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

[0006] confirming a network connection state of the common node;

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

[0008] before the current candidate duration ends, determining that the common node has not received heartbeat information sent by a master node, and changing a state of the common node to a candidate node;

[0009] obtaining a support vote number of the candidate node;

[0010] confirming that the support vote number is greater than a preset election winning vote number, and changing the candidate node to a master node.

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

[0012] determining that the common node has received the heartbeat information sent by the master node;

[0013] determining that new master node information is contained in the heartbeat information, and the new master node information is the common node, and changing the common node to a master node.

[0014] In combination with the first aspect, the method of confirming the network connection state of the common node comprises:

[0015] sending test data to all networks;

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

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

[0018] obtaining an initial limit parameter of the ordinary node;

[0019] determining a minimum candidate duration and a maximum candidate duration based on the initial limit parameter;

[0020] randomly determining the current candidate duration between the minimum candidate duration and the maximum candidate duration;

[0021] The initial limit parameter comprises an initial minimum candidate duration, an initial maximum candidate duration, and a network influence factor constant.

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

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

[0024] taking a sum of the initial minimum candidate duration and the first product as the minimum candidate duration.

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

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

[0027] taking a sum of the initial maximum candidate duration and the second product as the maximum candidate duration.

[0028] In combination with the first aspect, before confirming the network connection state of the ordinary node, the method further comprises:

[0029] initializing the ordinary node and setting an initial limit parameter;

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

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

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

[0033] The candidate node acquires support votes from other nodes through the network;

[0034] The candidate node counts all support votes in the network to obtain a support vote number.

[0035] In a second aspect, a node master election apparatus is provided, which is applied to a common node and includes:

[0036] A first operation module configured to confirm a network connection state of the common node;

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

[0038] A first processing module configured to change a state of the common node to a candidate node if the common node does not receive heartbeat information sent by a master node before the current candidate duration ends;

[0039] A third operation module configured to acquire a support vote number of the candidate node;

[0040] A second processing module configured to change the candidate node to a master node if the support vote number is greater than a preset election winning vote number.

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

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

[0043] Acquiring a second network number of other nodes;

[0044] Determining that the first network number and the second network number of any one or more of the nodes satisfy a first preset condition, and selecting one of the any one or more of the nodes as a new master node by the master node.

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

[0046] Comparing the first network number with the second network number;

[0047] If the first network number is less than the second network number, it is determined that the first preset condition is satisfied.

[0048] In combination with the third aspect, the method for the master node to select one of any one or more of the nodes as a new master node comprises:

[0049] The master node sends first heartbeat information to all of the nodes, the first heartbeat information comprising address information of the selected node;

[0050] The selected node becomes the new master node upon receipt of the first heartbeat information.

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

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

[0053] The former master node becomes a normal node upon receipt of the second heartbeat information.

[0054] A fourth aspect provides a node master selection apparatus applied to a master node, the apparatus comprising:

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

[0056] A second acquisition module configured to acquire a second network number of other nodes;

[0057] A processor module configured to determine that the first network number and the second network number of any one or more of the nodes satisfy a first preset condition, and select one of any one or more of the nodes as a new master node.

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

[0059] Embodiments of the present application provide a node master selection method applied to a normal node, comprising: confirming a network connection state of the normal node; determining a current candidate duration of the normal node based on the network connection state; before the current candidate duration ends, determining that the normal node has not received heartbeat information sent by a master node, and then changing a state of the normal node to a candidate node; acquiring a support vote number of the candidate node; confirming that the support vote number is greater than a preset election winning vote number, and then changing the candidate node to a master node. The node master selection method provided by the present application can select a master node according to a network connection state of a node, thereby ensuring that the master node has more network connections, improving working efficiency of the master node, and avoiding problems in network scheduling of the master node.

[0060] The embodiment of the present application further provides a node master selection method, applied to a master node, comprising: obtaining a first network number of the master node; obtaining a second network number of other nodes; determining that the first network number and the second network number of any one or more nodes satisfy a first preset condition, and selecting 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 state of the node, thereby ensuring that the master node has more network connections, improving the working efficiency of the master node, and avoiding problems in network scheduling of the master node. BRIEF DESCRIPTION OF DRAWINGS

[0061] The technical solutions and other beneficial effects of the present application will become apparent through the following detailed description of the specific embodiments of the present application, combined with the accompanying drawings.

[0062] Figure 1 A step schematic diagram of the master selection method applied to the common node provided by the embodiment of the present application;

[0063] Figure 2 A step schematic diagram of the current candidate duration determination method of the common node provided by the embodiment of the present application;

[0064] Figure 3 A module connection schematic diagram of the master selection device applied to the common node provided by the embodiment of the present application;

[0065] Figure 4 A master selection method applied to the master node provided by the embodiment of the present application;

[0066] Figure 5 A module connection schematic diagram of the master selection device applied to the master node provided by the embodiment of the present application;

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

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

[0069] The technical solutions in the embodiments of the present application will be described clearly and completely in combination with the accompanying 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 only used for description purpose, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features limited by “first” and “second” can 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 specifically limited.

[0070] Noun explanation:

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

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

[0073] Regular node: Regular Node refers to a node in a distributed system that does not have special permissions and responsibilities to become a master node or candidate node. Regular nodes are general participants in the system, responsible for executing tasks assigned to them and processing requests from other nodes.

[0074] The specific embodiments of the present application are described below by way of example:

[0075] As shown in Figure 1 and Figure 7 , the embodiment of the present application provides a node master election method, applied to a regular node, comprising:

[0076] S1: Confirm the network connection state of the regular node.

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

[0078] It can be understood that, by acquiring the number of networks in which the node is in a connected state or is in a disconnected state, the master node is ensured to have more network connections, the working efficiency of the master node is improved, and problems in network scheduling of the master node are avoided.

[0079] S2: determining a current candidate duration of the ordinary node based on the network connection state.

[0080] As shown in the method, the method comprises the following steps. Figure 2

[0081] S201: acquiring an initial limit parameter of the ordinary node, wherein the initial limit parameter comprises an initial minimum candidate duration, an initial maximum candidate duration, and a network influence factor constant.

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

[0083] S202: determining a minimum candidate duration and a maximum candidate duration based on the initial limit parameter.

[0084] ​Specifically, in the embodiments of the present application, after the initial restriction parameter is determined, the minimum candidate duration is confirmed, and the method comprises: obtaining a first product of an initial minimum candidate duration, a number of networks in the disconnected state and a network influence factor constant; and taking a 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, according to the value range of the initial minimum candidate duration, the initial minimum candidate duration is selected as 200 ms, and according to the value range of the network influence factor constant, the network influence 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 x 2 x 0.5;

[0086] The final minimum candidate duration can be obtained as 400 ms.

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

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

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

[0090] The final minimum candidate duration can be obtained as 1000 ms.

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

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

[0093] It can be understood that, since the number of networks in the connected state and the disconnected state with each node is different, the candidate duration required by each node in the candidate duration is also different, and the current candidate duration of each node is determined through the minimum candidate duration and the maximum candidate duration, so as to ensure that each node can obtain sufficient waiting duration, and avoid that the node cannot receive relevant signals in time.

[0094] S3: Before the end of the current candidate duration, it is determined that the ordinary node does not receive the heartbeat information sent by the master node, and the state of the ordinary node is changed to a candidate node. Specifically, when there is already a master node in the network, the master node will periodically send heartbeat information to all nodes to determine the connection state of other nodes, and also synchronizes the corresponding information to all other nodes. When the ordinary node first accesses the distributed system, it will first wait for a period of time, which is the current candidate duration. If no heartbeat information is received from the master node before the end of the current candidate duration, it means that there is no master node in the distributed system currently connected. At this time, all nodes need to enter the master selection program, and the state of the ordinary node is 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 that the current candidate duration of the ordinary node is located between two heartbeat information, resulting in an error in the state management of the node.

[0095] In the embodiment of the application, before the end of the current candidate duration, the method further comprises: 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 the ordinary node, and then changing the ordinary node to the master node. When the ordinary node first accesses the distributed system, and receives the heartbeat information from the master node before the end of the current candidate duration, it means that there is a master node in the distributed system currently connected. If the received heartbeat information contains new master node information, and the new master node information is for the ordinary node, the ordinary node becomes the new master node immediately. 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 state of the other ordinary nodes remains unchanged.

[0096] It is worth noting that before the end of the current candidate duration, if the heartbeat information of the master node is received, the current term value of the ordinary node can be changed to the same as the term value of the master node; if the heartbeat information of the master node is not received, the state of the ordinary node is changed to the candidate node, and the term value is also increased by 1.

[0097] It can be understood that by setting the current candidate duration, the ordinary node can determine whether there is a master node in the system when accessing the distributed system, and participate in the candidate when there is no master node, so that the distributed system can quickly select a master node, and the efficiency of the system master selection is improved.

[0098] S4: Obtain the support vote number of the candidate node.

[0099] The specific method comprises: a candidate node casting a support vote for itself through a network; the candidate node obtaining support votes from other nodes through the network; and counting all support votes of the candidate node in the network to obtain a support vote number. It is worth noting that after the candidate node updates the term value, the candidate node casts a support vote for itself through the network and simultaneously requests other nodes to cast support votes for it through the network; when other nodes receive the voting request, the term value of the other nodes is compared with that of the candidate node (i.e. the candidate node requesting the vote); if the term value of the other nodes is greater than that of the candidate node, the other nodes cast an opposing vote for the candidate node; if the term values of the other nodes and the candidate node are equal and the other nodes have not cast support votes for other candidate nodes, the other nodes cast a support vote for the candidate node; if the term values of the other nodes and the candidate node are equal but the other nodes have cast support votes for other candidate nodes, the other nodes cast an opposing vote for the candidate node; and if the term value of the other nodes is less than that of the candidate node, the term value of the other nodes is updated to be the same as that of the candidate node, and the other nodes cast a support vote for the candidate node.

[0100] It can be understood that the size of the term value of the node itself and that of the other node are compared to determine whether to cast a vote for the other node, so that the voting result is more unique, and the situation that multiple nodes have the same number of votes is avoided, and the suitable master node can be quickly selected through voting.

[0101] S5: confirming that the support vote number is greater than a preset election winning vote number, and changing the candidate node to a master node.

[0102] Specifically, the election winning vote number can be determined when the node is initialized, and the value range of the election winning vote number is 0-2n, n being the number of nodes in a connected state; if it is intended to quickly select a master node from a plurality of nodes, the value of the election winning vote number can be close to 0, for example, 1, 2 or 3; if it is intended to select a suitable node from a plurality of nodes at one time, the value of the election winning vote number can be close to 2n, for example, when n is 3, the value of the election winning vote number can be 5 or 6.

[0103] It can be understood that the value of the election winning vote number is associated with the number of nodes in a connected state, so that the master node can be selected even if the nodes are disconnected, and the master node cannot be selected if the voting result of each node cannot exceed the election winning vote number when a plurality of nodes are disconnected.

[0104] It is worth noting that the above-mentioned content of the embodiments of the application is disclosed for one node only, and when there are a plurality of nodes in the distributed system, each node accesses the distributed system and selects a master node by using the above-mentioned node master selection method, and finally a master node is determined from the plurality of nodes.

[0105] In summary, the node master selection method provided in the embodiments of the present application converts the state of the ordinary node to a candidate node in the case that the ordinary node does not receive the heartbeat information of the master node after waiting for a corresponding time length, 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 state of the node, thereby ensuring that the master node has more network connections, improving the working efficiency of the master node, and avoiding problems in network scheduling of the master node.

[0106] As shown in Figure 3 The embodiments of the present application also provide a node master selection device applied to an ordinary node, which comprises: a first operation module configured to confirm the network connection state of the ordinary node; a second operation module configured to determine the current candidate time length of the ordinary node based on the network connection state; a first processing module configured to change the state of the ordinary node to a candidate node if the ordinary node does not receive the heartbeat information sent by the master node before the current candidate time length ends; a third operation module configured to obtain the support vote number of the candidate node; and a second processing module configured to change the candidate node to the master node if the support vote number is greater than a preset election winning vote number. Specifically, the node master selection device provided in the embodiments of the present application determines the network connection state of the ordinary node through the first operation module, determines the current candidate time length of the ordinary node through the second operation module, determines whether the heartbeat information of the master node is received based on the current candidate time length through the first processing module, confirms the ordinary node as the new master node if the heartbeat information of the master node is received and the ordinary node is the new master node in the heartbeat information, otherwise, the ordinary node is still the ordinary node, changes the ordinary node to the candidate node to participate in the selection of the master node if the heartbeat information of the master node is not received, obtains the support vote number of the candidate node through the third operation module, and changes the candidate node to the master node if the support vote number is greater than the election winning vote number through the second processing module.

[0107] It can be understood that the node master selection device provided in the embodiments of the present application converts the state of the ordinary node to a candidate node in the case that the ordinary node does not receive the heartbeat information of the master node after waiting for a corresponding time length, 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 state of the node, thereby ensuring that the master node has more network connections, improving the working efficiency of the master node, and avoiding problems in network scheduling of the master node.

[0108] As shown in Figure 4 and Figure 7 The embodiments of the present application also provide a node master selection method applied to a master node, which comprises:

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

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

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

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

[0113] F3: Determine that the first network number and the second network number of any one or more nodes satisfy a first preset condition, and select one of the any one or more nodes as a new master node. The specific method comprises: 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 satisfied. Specifically, compare the network connection number of all the networks of the master node with each of the other nodes; if there is one or more nodes in the other nodes whose network connection number is greater than that of the master node, determine the node with the largest network connection number among the network connection numbers of the other nodes as the new master node; if there are multiple nodes with the largest network connection number, randomly select one of the multiple nodes that satisfy the condition as the master node. If the network connection number of any one of the other nodes is less than or equal to that of the master node, the master node remains unchanged.

[0114] It can be understood that this method can determine the node with the largest network connection number among the other nodes, and replace the master node when the condition is satisfied, thereby ensuring that the master node always has the largest network connection number in the distributed system.

[0115] In this embodiment, the method for a master node to select one or more nodes as a new master node includes: the master node sending a first heartbeat message to all nodes, the first heartbeat message including the address information of the selected node; the selected node immediately becomes the new master node upon 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 known as a relinquishment heartbeat message) to all other nodes. The first heartbeat message contains the address information of the new master node. After receiving the first heartbeat message, the selected ordinary node or candidate node finds that the address information matches its own address and thus becomes the new master node.

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

[0117] In summary, the node master selection method provided in this application, when a master node exists in a distributed system, obtains the number of network connections of other nodes in real time by sending heartbeat information. When it is determined that there are other nodes with more network connections than itself, the master node yields its position to the node with more network connections, thereby ensuring that the master node in the distributed system has the maximum number of network connections.

[0118] like Figure 5 As shown in the illustration, this application embodiment also provides a node master selection device applied to a master node. The device includes: a first acquisition module configured to acquire a first number of networks of the master node; a second acquisition module configured to acquire a second number of networks of other nodes; and a processor module configured to determine that the first number of networks and the second number of networks of any one or more nodes satisfy a first preset condition, and the master node selects one of the one or more nodes as a new master node. Specifically, the node master selection device provided in this application embodiment acquires the number of networks in a connected state (hereinafter referred to as the number of network connections) of all networks on the master node through the first acquisition module; acquires the number of network connections of all other nodes through the second acquisition module; and compares the number of network connections of the master node with the number of network connections of other nodes through the processor module. 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 of the other nodes that meets the condition to become the new master node.

[0119] The content of the present application is further illustrated by the following examples:

[0120] As shown in Figure 6 and Figure 7 , 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 full-connection network topology structure, each node has 3 independent network interfaces, each network interface is connected with 3 switches to form 3 independent networks. The four nodes of the system all adopt the master selection method provided in the above examples to select the master, and the master selection process of one node is specifically described as follows:

[0121] Step one: initialize the master selection program, set the initial minimum candidate duration as 200 ms, the initial maximum candidate duration as 500 ms, the network influence factor constant as 0.5, set the election winning vote number as 4, and set the current term as 0;

[0122] Step two: set the state of the node as a normal node, calculate the minimum candidate duration (200 ms) and the maximum candidate duration (500 ms) of this time according to the initial minimum candidate duration (200 ms), the initial maximum candidate duration (500 ms), the network influence 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, 300 ms);

[0123] Step three: wait for the current candidate duration to timeout, that is, the program waits for 300 ms;

[0124] Step four: during the waiting period, if the heartbeat message of the master node is received, set the current term as the same term as the master node, and then return to step two; if the heartbeat message of the master node is received, the heartbeat message contains the information of the new master node and is for itself, then become the master node immediately, and send the heartbeat message to all other nodes in real time;

[0125] Step five: wait for the timeout, the state of the node is immediately converted into a candidate node, the current term is increased by 1, and the node casts the approval votes for itself on each network, that is, 3 support votes are cast for itself, and then requests the other nodes to vote for it (for example, node 1: through switch 1, get the votes from node 2, node 3 and node 4, then through switch 2, get the votes from node 2, node 3 and node 4, and then through switch 3, get the votes from node 2, node 3 and node 4), the total number of votes should be 3+3*3, that is, 12;

[0126] Step six: when other nodes receive the voting request, if the current term of the node is greater than the current term of the candidate node, the node votes against; if the current term of the node is equal to the current term of the candidate node and the node has not voted for other candidate nodes, the node votes for; if the current term of the node is equal to the current term of the candidate node and the node has voted for other candidate nodes, the node votes against; if the current term of the node is less than the current term of the candidate node, the current term of the node is set to be the same as the current term of the candidate node, and the node votes for;

[0127] Step seven: the candidate node counts the voting results, if the number of votes for is greater than or equal to the set number of winning votes, the candidate node wins the election, becomes the master node, and sends a heartbeat message to all other nodes in real time; otherwise, the election fails, and the process returns to step two.

[0128] The 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 selection method provided in any one of the above embodiments.

[0129] The above describes in detail the node master selection method and device provided by the embodiments of the application, and the principle and implementation manner of the application are described by using specific examples; the above embodiment is only used to help understand the technical solution of the application and its core idea; those skilled in the art should understand that the technical solution recorded in the above embodiments can be modified, or some technical features can be replaced equivalently; the modification or replacement does not make the essence of the corresponding technical solution deviate from the scope of the technical solution of the embodiments of the application.

Claims

1. A method for node master election, the method comprising: The method is applied to a common node, and comprises the following steps of: confirming a network connection state of the common node; determining a current candidate time length of the common node based on the network connection state, which comprises the following steps of: obtaining initial restriction parameters of the common node; determining a minimum candidate time length and a maximum candidate time length based on the initial restriction parameters, a method for determining the minimum candidate time length based on the initial restriction parameters comprises the following steps of: obtaining a first product of an initial minimum candidate time length, a number of networks in a disconnected state and a network influence factor constant; taking a sum of the initial minimum candidate time length and the first product as the minimum candidate time length; a method for determining the maximum candidate time length based on the initial restriction parameters comprises the following steps of: obtaining a second product of an initial maximum candidate time length, the number of networks in the disconnected state and the network influence factor constant; taking a sum of the initial maximum candidate time length and the second product as the maximum candidate time length; randomly determining the current candidate time length between the minimum candidate time length and the maximum candidate time length; wherein the initial restriction parameters comprise the initial minimum candidate time length, the initial maximum candidate time length and the network influence factor constant; before the current candidate time length ends, determining that the common node does not receive heartbeat information sent by a master node, and then changing a state of the common node to a candidate node; obtaining a support vote number of the candidate node; confirming that the support vote number is greater than a preset election winning vote number, and then changing the candidate node to the master node.

2. The method of claim 1, wherein, before the current candidate time length ends, the method further comprises the following steps of: determining that the common 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 the common node, and then changing the common node to the master node.

3. The method of claim 1, wherein, the method for confirming the network connection state of the common node comprises the following steps of: sending test data to all networks; obtaining feedback of the networks based on the test data to obtain a number of networks in a disconnected state.

4. The method of claim 3, wherein, before confirming the network connection state of the common node, the method further comprises the following steps of: initializing the common node and setting initial restriction parameters; the initial restriction parameters comprise an initial minimum candidate time length, an initial maximum candidate time length, a network influence factor constant, an election winning vote number and a current term.

5. The method of claim 1, wherein, the method for obtaining the support vote number of the candidate node comprises the following steps of: the candidate node casts a support vote for itself through the networks; the candidate node obtains support votes from other nodes through the networks; counting all support votes of the candidate node in the networks to obtain the support vote number.

6. A node master election apparatus, characterized by comprising: The device is applied to a common node, and comprises the following modules: a first operation module configured to confirm a network connection state of the common node; a second operation module configured to determine a current candidate time length of the common node based on the network connection state, which comprises the following steps of: obtaining initial restriction parameters of the common node; determining a minimum candidate time length and a maximum candidate time length based on the initial restriction parameters, a method for determining the minimum candidate time length based on the initial restriction parameters comprises the following steps of: obtaining an initial minimum candidate duration, a first product of a number of networks in the disconnected state and a network influence factor constant; obtaining a sum of the initial minimum candidate duration and the first product as the minimum candidate duration; a method for determining a maximum candidate duration based on the initial limit parameter comprises: obtaining an initial maximum candidate duration, a second product of the number of networks in the disconnected state and the network influence factor constant; obtaining a sum of the initial maximum candidate duration and the second product as the maximum candidate duration; randomly determining the current candidate duration between the minimum candidate duration and the maximum candidate duration; wherein the initial limit parameter comprises an initial minimum candidate duration, an initial maximum candidate duration and a network influence factor constant; a first processing module configured to determine that the ordinary node does not receive the heartbeat information sent by the master node before the current candidate duration ends, and change the state of the ordinary node to a candidate node; a third operation module configured to obtain a support vote number of the candidate node; a second processing module configured to confirm that the support vote number is greater than a preset election winning vote number, and change the candidate node to a master node.

Citation Information

Patent Citations

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

    CN118316777A