Disclosure of Invention
In order to solve the problems in the prior art, embodiments of the present invention provide a method and an apparatus for selecting a master node in a distributed system. The technical scheme is as follows:
in a first aspect, a method for selecting a master node in a distributed system is provided, where the method is applied to any node in the distributed system, and the method includes:
acquiring authorized information sent by at least one target node, wherein the target node is a node of which the performance parameter value is not less than a preset value in a distributed system;
and when the condition of reselecting the main node is met, determining the main node from the target nodes which send the authorized information, wherein the priority level of the main node in the target nodes is the highest.
Optionally, before the obtaining of the authorized information sent by the at least one target node, the method includes:
and acquiring the priority level sent by each node in the distributed system, wherein the priority level of each node is a preset numerical value or a numerical value calculated according to preset configuration parameters and a preset algorithm of the node.
Optionally, after obtaining the priority level sent by each node in the distributed system and before obtaining the authorized information sent by the at least one target node, the method further includes:
and determining the node with the highest priority level in the distributed system as a main node.
Optionally, after obtaining the priority level sent by each node in the distributed system and before obtaining the authorized information sent by the at least one target node, the method further includes:
and sequencing the nodes according to the priority levels of the nodes to obtain a node list.
Optionally, after obtaining the authorized information sent by the at least one target node, the method includes:
marking each target node in the node list with an authorized identifier according to the authorized information;
and when the condition of reselecting the main node is met, searching the nodes with the highest priority level in the node list in sequence, and determining the first searched node marked with the right identifier as the main node.
In a second aspect, an apparatus for selecting a master node in a distributed system is provided, where the apparatus includes:
the system comprises an acquisition unit, a processing unit and a processing unit, wherein the acquisition unit is used for acquiring authorized information sent by at least one target node, and the target node is a node of which the performance parameter value in a distributed system is not less than a preset value;
and the determining unit is used for determining the master node from the target nodes which send the authorized information when the condition of reselecting the master node is met, wherein the priority level of the master node in the target nodes is the highest.
Optionally, the obtaining unit is further configured to:
and acquiring the priority level sent by each node in the distributed system, wherein the priority level of each node is a preset numerical value or a numerical value calculated according to preset configuration parameters and a preset algorithm of the node.
Optionally, the determining unit is further configured to determine a node with a highest priority level in the distributed system as a master node.
Optionally, the apparatus further comprises: and the sequencing unit is used for sequencing the nodes according to the priority levels of the nodes to obtain a node list.
Optionally, the apparatus further comprises a marking unit;
the marking unit is used for marking each target node in the node list with a right identifier according to the right information;
and the determining unit is used for searching in sequence from the node with the highest priority level in the node list when the condition of reselecting the main node is met, and determining the searched node marked with the right identifier as the main node.
In the implementation of the invention, only the node with the authority as the master node can send the authorized information to other nodes, and the node without the authority as the master node can not send the authorized information to other nodes, so the communication times of the whole system are reduced, and simultaneously, the master node is selected from the nodes with the authority as the master node, and the selection speed of the master node is accelerated only by comparing the priority levels of all the nodes.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, embodiments of the present invention will be described in detail with reference to the accompanying drawings.
Fig. 1 is a schematic diagram of a network framework of a distributed system according to an embodiment of the present invention. The distributed system may include a plurality of nodes, each node being connected to another node via a network, and the nodes may be computers. A client may be networked to multiple nodes in a distributed system that may act as a server side to provide data to the client. When a client accesses data in the distributed system, the client can send access requests to the master node or the slave nodes, and some access requests sent by the client can directly respond through the slave nodes, and some access requests can only respond through the master node. When the access request sent by the client needs to be responded only through the master node, as described in the background art, the master node issues the access request sent by the client to the slave nodes, the slave nodes execute specific tasks, and the slave nodes send response information to the client through the master node. And when the slave node finds that the slave node cannot normally communicate with the master node, determining that the master node fails, and reselecting the master node. In a specific implementation, each slave node in the distributed system may send a test signal to the master node according to a fixed period to determine that the master node can normally communicate. When the slave node detects that it does not respond after sending the test signal, it can be determined that the master node has failed.
Referring to fig. 2, it is a flowchart of a method for selecting a master node in a distributed system according to an embodiment of the present invention, where the method may be applied to any node in the distributed system, and the method may specifically include the following steps.
Step 201, obtaining authorized information sent by at least one target node, wherein the target node is a node whose performance parameter value in the distributed system is not less than a preset value.
In a specific implementation, each node in the distributed system may calculate its own performance parameter value, and the performance parameter value may be calculated according to the resource usage. The resource usage may include usage of CPU resources, memory resources, disk space, and the like. It should be noted that, the embodiment of the present invention does not specifically limit the algorithm of the performance parameter value. When the performance parameter value of the node is not less than the preset value, the node can be determined to have the authority of serving as the main node. Each node having the authority to be the master node, i.e., the target node, may transmit the authority information to all nodes except the node itself in the distributed system at a fixed period. The authorized information may be any information capable of indicating that its sending node has the authority to serve as the master node, such as an Internet Protocol (IP) address of the node sending the authorized information, or a preset identifier, etc.
It should be noted that, when the node a acquires the authorized information sent by the target node, if the performance parameter value of the node a is not less than the preset value, the target node also includes the node a.
Step 202, when the condition of reselecting the master node is satisfied, determining the master node from the target nodes sending the authorized information, wherein the priority level of the master node in the target nodes is the highest.
And when any node finds that the current master node cannot normally communicate, the master node is reselected.
In the distributed system starting stage, each node can calculate the priority level according to the preset configuration parameters and the preset algorithm of the node. The preset configuration parameter may adopt an IP address or a Media Access Control (MAC) address, etc. For example, the value of the IP address may be calculated according to an Algorithm of Message-Digest Algorithm 5 (MD 5) to obtain an MD5 value corresponding to the IP address, and the MD5 value may be used as the priority level of the node. For example, the format of the MD5 value may be 38b8c2c1093dd0fec383a9d9ac 940515. The priority level of each node may also be a preset value, such as a preset code for each node. Of course, the priority level of each node may also be a numerical value calculated according to a preset code and a preset algorithm. It should be noted that, the algorithm of the priority level is not specifically limited in the embodiment of the present invention. After each node calculates its own priority level, it may send its own priority level to all other nodes in the system, so that each node can obtain the priority levels of all nodes, and each node may determine the node with the highest priority level in the distributed system as the master node. That is, during the distributed system startup phase, any node may determine the node with the highest priority level in the distributed system as the master node.
Therefore, before step 201, any node may obtain the priority level sent by each node in the distributed system, where the priority level of each node is a preset value or a value calculated according to a preset configuration parameter and a preset algorithm of the node. That is, each node in the distributed system may acquire and store the priority levels of all other nodes, so that when the master node is reselected, the node can determine the priority level of each target node that sends the entitled information according to the pre-stored priority levels.
It should be noted that, a target node having a right as a master node may send the right information at a fixed period, so when reselecting the master node, the master node is determined according to the right information obtained last time. The right information sent by each target node can comprise the priority level of the target node, so that the master node can be directly determined according to the level of each priority level. The authorized information sent by each target node can also comprise the IP address of the target node, so that when the main node is reselected, the corresponding priority level is directly calculated according to each IP address, and the main node is determined according to the level of each priority level.
In the implementation of the invention, only the node with the authority as the master node can send the authorized information to other nodes, and the node without the authority as the master node can not send the authorized information to other nodes, so the communication times of the whole system are reduced, and simultaneously, the master node is selected from the nodes with the authority as the master node, and the selection speed of the master node is accelerated only by comparing the priority levels of all the nodes.
Referring to fig. 3, a flowchart of a method for selecting a master node in another distributed system is provided in an embodiment of the present invention, where the method may be applied to any node in the distributed system, and the method may include the following steps.
Step 301, acquiring the priority level sent by each node in the distributed system.
And 302, sequencing the nodes according to the priority levels of the nodes to obtain a node list.
Step 303, obtaining the authorized information sent by at least one target node.
Step 304, marking the authorized identification for each target node in the node list according to the authorized information.
This list of nodes is now illustrated. For example, the distributed system includes a node a, a node B, and a node C, the priority level of the node a is higher than that of the node B, the priority level of the node B is higher than that of the node C, and the right information acquired last time is information sent by the node B and the node C. In this case, the node list that can be obtained is as follows:
node list:
priority level
|
Node name
|
Whether or not it has the right
|
1
|
Node A
|
Is free of
|
2
|
Node B
|
Is provided with
|
3
|
Node C
|
Is provided with |
And 305, when the condition of reselecting the master node is met, searching the nodes with the highest priority level in the node list in sequence, and determining the first searched node marked with the authorized identifier as the master node.
If the nodes in the node list are sorted in order of priority level from high to low, the search may be started from the first node in the node list when the master node is reselected. For example, the node list in the above example, may start with node a, find that node a does not have a token, and continue to search downward. The node B may be determined to be the primary node when the node B is found to be marked with a valid identity.
It should be noted that, the embodiment of the present invention has many similarities or similarities with the embodiment shown in fig. 2, and the same or similar parts may refer to the embodiment shown in fig. 2, and the embodiment of the present invention is not repeated.
In the embodiment of the invention, each node in the node list is sorted according to the priority level, after receiving the authorized information each time, the node which needs to be marked with the authorized identifier in the node list is re-determined according to the authorized information, when the master node is reselected, the node can be searched according to the existing sorting, and the searched node which is marked with the authorized identifier is determined as the master node, so that the priority level of the target node is not required to be determined according to the authorized information when the master node is determined, and then the highest priority level is searched from all the priority levels.
Referring to fig. 4, a block diagram of a structure of a selecting apparatus for a master node in a distributed system according to an embodiment of the present invention is shown, where the apparatus may be configured in a node or be the node itself, and the apparatus may specifically include: an acquisition unit 401 and a determination unit 402.
The obtaining unit 401 is configured to obtain authorized information sent by at least one target node, where the target node is a node whose performance parameter value is not less than a preset value in a distributed system.
A determining unit 402, configured to determine a master node from target nodes that transmit the authorized information when a condition for reselecting the master node is satisfied, where a priority level of the master node is highest in the target nodes.
Preferably, in the distributed system startup phase, the obtaining unit is further configured to: and acquiring the priority level sent by each node in the distributed system, wherein the priority level of each node is a preset numerical value or a numerical value calculated according to preset configuration parameters and a preset algorithm of the node.
The determining unit is further configured to determine a node with a highest priority level in the distributed system as a master node.
Preferably, the apparatus may further comprise a sorting unit and a marking unit.
The sorting unit is used for sorting the nodes according to the priority levels of the nodes to obtain a node list.
And the marking unit is used for marking the authorized identification of each target node in the node list according to the authorized information.
A determining unit 402, configured to, when a condition for reselecting a master node is satisfied, sequentially search for nodes from the node with the highest priority level in the node list, and determine a node marked with a right identifier as a master node.
In the implementation of the invention, only the node with the authority as the master node can send the authorized information to other nodes, and the node without the authority as the master node can not send the authorized information to other nodes, so the communication times of the whole system are reduced, and simultaneously, the master node is selected from the nodes with the authority as the master node, and the selection speed of the master node is accelerated only by comparing the priority levels of all the nodes.
It will be understood by those skilled in the art that all or part of the steps for implementing the above embodiments may be implemented by hardware, or may be implemented by a program instructing relevant hardware, where the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a magnetic disk or an optical disk, etc.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like that fall within the spirit and principle of the present invention are intended to be included therein.