Detailed Description
The present invention will be described in further detail with reference to the accompanying drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the invention and are not limiting of the invention. It should be further noted that, for the convenience of description, only some of the structures related to the present invention are shown in the drawings, not all of the structures.
Fig. 1 is a schematic flowchart of a method for determining a master node according to an embodiment of the present invention, where the method is applicable to a situation of determining a master node of a distributed cluster, and the method is applied to the distributed cluster and can be executed by a device for determining a master node, where the device can be implemented in a form of software and/or hardware, and the hardware can be an electronic device, such as a mobile terminal or a PC terminal.
Before the technical solution of the present embodiment is described, an application scenario may be exemplarily described. The distributed type has become a trend of internet architecture, in a distributed cluster, usually only one node is a master node at the same time, the master node is in a readable and writable state externally, and operations which can be executed include updating of a database, enqueuing or dequeuing of a message queue and the like. If multi-node read-write is adopted, distributed node concurrency may be caused, and the reliability and the safety of the system are reduced. When determining the master node based on zookeeper distributed coordination, the number of available nodes is limited, i.e., the number of available nodes must exceed half of the total number to determine the master node. Further, the complexity of the distributed clusters is also limited, for example, if only 3 nodes exist in a certain distributed cluster, the master node cannot be determined based on zookeeper distributed coordination, that is, this method has a problem of poor versatility. Thirdly, if the number of usable nodes is even, the number of determined master nodes is two, and at this time, election determination master nodes need to be triggered again, that is, multiple elections are easily caused to determine master nodes, resulting in the problem of system instability. Finally, the method is limited by the network environment, and when the network environment changes greatly, the behavior of the election master node is greatly influenced, namely, the problem of poor universality exists.
The technical scheme of the embodiment is mainly used for determining the condition of the master node in the distributed cluster, and can be applied to the distributed cluster. The distributed cluster is composed of a plurality of servers, each server can be used as a node in the distributed cluster, and the nodes are used for expression in the following.
The distributed nodes comprise two types of nodes, one type is a main standby node, and the other type is an arbitration node. The active and standby nodes can be used as reading nodes and writing nodes. And determining a main node to be finally used from the main node and the standby node. The arbitration node can only act as a read node.
Meanwhile, the distributed cluster also comprises two stable states and three unstable states. The stable state refers to a state that can stably maintain the current state under the condition that the network is stable and the cluster normally operates; the unstable state refers to a state that the state of the state can be actively modified under the condition that a certain time interval is exceeded and a task network message is not received. It should be noted that the two stable states include a main state and a standby state. If the current node is in the master state, the current node can provide services to the cluster, and the services generally provided comprise the write operation of the database and the message queue. That is, the current node is the master node of the distributed cluster. The standby state can be understood as that the current node can provide service to the cluster and generally undertakes reading operation of a database and a message queue, namely, the current node is used as a common node and has the same function as an arbitration node. The three unstable states may be an initial state, a candidate state, and a master silence state, respectively. The initial state is the state that the node is automatically in when the cluster is started; the candidate state refers to a state in which the node serves as a candidate master node; the main silent state refers to a state in which a node is converted into a main node if the node does not receive a main heartbeat within a certain time period.
In this embodiment, the main node and the standby node are used as main nodes to be selected, and the arbitration node is used as a node to be used, which is different from the main node in type.
As shown in fig. 1, the method of this embodiment includes:
and S110, determining the master nodes to be selected which are consistent with the preset node types according to the node types of the nodes to be used.
Wherein, each server in the distributed cluster can be used as a node to be used. Each node to be used has a corresponding node type, such as a master node type and a non-master node type. The main node type is a type which can be used as a main node, namely a node type which can provide read-write operation; the non-master node type refers to a type that cannot be used as a master node, i.e., a type that can only provide a read operation.
It should be noted that whether the server can be used as the active/standby node is related to the hardware performance of the server. The user can set the node type of each node in the distributed cluster according to actual conditions. The preset node type is a master node type that can be used as a master node. And taking the node to be used of the main node type as a main node to be selected. That is, the finally determined master node is screened from the master nodes to be selected.
Specifically, each server in the distributed cluster may be used as a node to be used, and the node type of each node to be used may be obtained. And taking the node to be used with the node type consistent with the preset node type as a main node to be selected.
In this embodiment, the determining, according to the node type of each node to be used, a to-be-selected master node that is consistent with the preset node type includes: acquiring the node type of each node to be used; the node types comprise a main node type and a non-main node type; and taking the node to be used consistent with the type of the main node as the main node to be selected.
The node type of the node to be used is matched with the device performance corresponding to the node. If the device performance corresponding to the node is high, the node type of the node may be set as the master node type, otherwise, the node type is set as the non-master node type.
Specifically, the node type of each node to be used in the distributed cluster may be obtained, for example, the node type is a master node type and a non-master node type, and the node to be used corresponding to the master node type is used as the master node to be selected.
In this embodiment, the distributed clusters are in different stages, and the processing modes of the nodes to be used, which are inconsistent with the preset node types, are also different. Optionally, if the distributed cluster is in a starting stage, determining a node inconsistent with a preset node type from the nodes to be used, and updating the state identifier of the node from the initial identifier to the standby state identifier; and if the distributed cluster is in the operation stage, keeping the state identifier of the node to be used, which is inconsistent with the preset node type, as the standby state identifier unchanged. The starting phase may be understood as starting each server in the distributed cluster, where each server is in an initial state, that is, each node in the distributed cluster is in an initial state. The operation stage can be understood as that each node is in a normal use stage. And presetting the node type as a main node type. And if the state identifier of the node is the standby state identifier, the node is mainly used for executing the read operation of the message queue.
Specifically, if the distributed cluster is in the startup phase, the node state identifier of each distributed node is the initial state identifier, and the state identifier of the node to be used, which is inconsistent with the master node type, may be updated from the initial state identifier to the standby state identifier. If the distributed cluster is in the operation stage, the node to be used, the node type of which is not the main node type, is marked as the standby state identifier, at this time, the standby state identifier can be kept unchanged, and the node to be used, which is consistent with the preset node type and is in the standby state identifier, is taken as the main node to be selected.
Illustratively, assume that the distributed cluster includes five to-be-used nodes A, B, C, D, E. The node types of A, B, C three to-be-used nodes are master node types, the node types of D, E two to-be-used nodes are non-master node types, and the to-be-used node D, E can be referred to as an arbitration node. During the cluster startup phase, the to-be-used node A, B, C, D, E is in an initialized state. Referring to fig. 2, since the node type of the to-be-used node D, E is not consistent with the preset master node type, the state identifier of the to-be-used node D, E may be marked as a standby state from an initial state, and then a heartbeat packet is broadcast to other to-be-used nodes, so that the other to-be-used nodes determine that the to-be-used node D, E can perform a read operation. In the cluster operation stage, to re-determine the master node, the standby state identifier of the to-be-used node D, E inconsistent with the preset node type (master node type) may be kept unchanged, that is, the to-be-used node D, E is kept in a standby state.
S120, according to the attribute information of each master node to be selected, determining the theoretical waiting time of the corresponding master node to be selected in each waiting state, and updating the state identifier of the corresponding master node to be selected based on the theoretical waiting time.
It should be noted that if the master node to be selected is determined based on the start phase, the master node to be selected may pass through the initial state, the candidate state, and the master silent state in sequence. If the master node to be selected is determined based on the run phase, the master node to be selected may pass through the candidate state and the master quiesce state in sequence.
The attribute information may be, for example, information such as node priorities of the master nodes to be selected. The waiting state refers to a state that the master node to be selected needs to pass through when the master node is to be selected as the master node, or each state that the master node to be selected identified by the standby state needs to pass through when the master node is to be selected as the master node. The wait state includes at least one of an initial state, a candidate state, and a master silence state. Each master node to be selected needs to last for a certain duration in different waiting states, and the duration can be used as the theoretical waiting duration. Namely, the waiting time determined according to the attribute information is used as the theoretical waiting time. The waiting state includes at least one of the three states, and correspondingly, the theoretical waiting time length includes waiting time lengths corresponding to the three states.
It should be noted that, when the attribute information of each master node to be selected is different, the duration of each master node in each waiting state is also different; of course, even if the attribute information of each master node to be selected is the same, the waiting time lengths of the master nodes in the waiting states may be different.
Specifically, for each master node to be selected, the waiting time of the current master node to be selected in the candidate state may be determined according to the node attribute information of the current master node to be selected. Meanwhile, when the main node to be selected enters the main silent state, the continuous waiting time of the main node in the main silent state is determined. When detecting that the actual waiting time of the current master node to be selected in the master silent state reaches the theoretical waiting time, the state identifier of the current master node to be selected can be updated, and then the target master node is determined based on the updated state identifier.
For example, the to-be-used node A, B, C is a master node to be selected, and when it is detected that the master node to be selected reaches each waiting state, the theoretical waiting duration in each waiting state may be determined according to the attribute information of each master node to be selected.
In this embodiment, based on the above expression, it can be known that the waiting status includes a candidate status and a main silent status, and correspondingly, the theoretical waiting duration information includes a candidate waiting duration in the candidate status and a silent waiting duration in the main silent status. Optionally, determining a theoretical waiting duration of the corresponding host node to be selected in each waiting state according to the attribute information of each host node to be selected, including: and determining the candidate waiting time of the corresponding main node to be selected in the candidate state and the silent waiting time of the main silent state according to the node priority in the attribute information.
Specifically, when it is detected that one of the master nodes to be selected is in the candidate state, the theoretical waiting time of the master node to be selected in the candidate state may be determined according to the node priority of the master node to be selected, and optionally, the higher the priority level is, the shorter the theoretical waiting time is. The method may further include establishing a correspondence between the node priority and the theoretical waiting duration, where the correspondence includes a plurality of node priorities, each node priority corresponds to one theoretical waiting duration segment, and one duration may be randomly determined from the theoretical waiting duration segments as the theoretical waiting duration of the node in a certain state. Of course, when it is detected that a certain master node to be selected is in the master silent state, the theoretical waiting duration of the master node to be selected continuously waiting in the master silent state, that is, the master silent waiting duration, can still be determined according to the priority of the node.
In order to further improve the rationality of the determined theoretical waiting duration, in the present embodiment, the following manner may be adopted to determine the waiting duration that each master node to be selected lasts in each path state.
Optionally, for each master node to be selected, when it is detected that the state identifier of the current master node to be selected is updated to the candidate state identifier, determining the candidate waiting time of the current master node to be selected in the candidate state according to the node priority of the current master node to be selected, the target time information of the updated candidate state identifier, and the heartbeat time information; and for each master node to be selected, when detecting that the state identifier of the current master node to be selected is updated to be the master silent state identifier from the candidate state identifier, determining the silent waiting time of the current master node to be selected in the master silent state according to the node priority and the preset numerical value of the current master node to be selected.
Wherein the node priorities may be represented by corresponding numbers, e.g. the node priorities are represented by numbers between 0 and 5. The priority identifier of the node to be used of the non-master node type is 0, and the priority identifier of the master node to be selected may be any number from 1 to 5. The user may set the hardware performance of the server, where a larger number characterizes a higher priority of the node.
It is understood that the candidate waiting duration lasting in the candidate state is determined according to the node priority, the target time information updated to the candidate state identification, and the heartbeat duration information. Correspondingly, the silent waiting time length is determined according to the node priority of the main node to be selected and a preset numerical value.
Specifically, the target time information, the heartbeat time information and the node priority identification are processed by adopting a random algorithm, so that the candidate duration of the master node to be selected is obtained. For example, the formula level check (now-level-heart _ time) is used to determine the candidate duration of each master node to be selected in the candidate state. The now represents the time when the state identifier of the master node to be selected is updated to the candidate state identifier, that is, the target time information, the level represents the node priority identifier of the master node to be selected, and the heart-beat time information is represented by the heart-beat time. Meanwhile, when the fact that the actual waiting time of a certain main node to be selected in the candidate state reaches the candidate waiting time is detected, the main silent state can be entered, namely the state identifier of the main node to be selected is updated to the main silent state identifier from the candidate state identifier, and the silent waiting time in the main silent state can be determined according to a corresponding mode. For example, a random algorithm is used to determine the waiting duration for which the master node to be selected is in the master silent state. The random algorithm may be radom (3,3+ (5-level)), where 3 and 5 may be preset values. Assuming that the node priority level is labeled as 1, the radom (3,3+ (5-1)) can be obtained by substituting the formula, the silent waiting duration can be any duration from 3ms to 7ms, and the specific time measurement unit can be set according to the actual requirement.
S130, determining a target main node according to the state identifier, broadcasting a main heartbeat message to each node to be used based on the target main node, so that each node to be used determines the target main node according to the received main heartbeat message, and updating the state identifiers of other main nodes to be selected into standby state identifiers.
Wherein the target master node is the node that ultimately determines to provide the write operation. After the target master node is determined, a master heartbeat message may be broadcast to each to-be-used node (including all nodes except the target master node) based on the target master node, so that after each other to-be-used node receives the master heartbeat message, it may be determined to determine the target master node according to a node identifier corresponding to the master heartbeat message, and meanwhile, a state identifier of each to-be-selected master node is updated to be a standby state identifier.
Specifically, when it is detected that the actual waiting time of a certain master node to be selected in the master silent state reaches the silent waiting time, and the state identifier of another node is the silent state identifier corresponding to the master silent state or the candidate state identifier corresponding to the candidate state, the node may be used as the target master node.
In this embodiment, determining the target master node according to the status information may be: and preferentially updating the state identifier into the master state identifier to be selected as the target master node.
The master status identifier refers to an identifier that the node can serve as a target master node.
Specifically, if the actual waiting time of the master node to be selected in the master silent state reaches the silent waiting time, the status identifier of the master node to be selected may be updated to the master status identifier, and the master node to be selected is used as the target master node. Based on the above, only one node of the distributed cluster may be the master node at the same time, and therefore, the master node to be selected, which has entered the master state from the master silent state preferentially, is taken as the target master node.
On the above exemplary basis, with continued reference to fig. 2, during the distributed cluster startup phase, the priority levels of the master node A, B, C to be selected are in turn: a > B > C. When it is detected that each master node to be selected enters the candidate state, the duration in the candidate state may be determined according to the priority of each master node to be selected, for example, the duration in the candidate state is shorter as the priority level is higher. At this time, the duration of the master node a to be selected in the candidate state may be the shortest, the waiting duration of the master node B to be selected is the next time, and the waiting duration of the master node C to be selected is the longest. That is, the master node a to be selected can preferentially enter the master silent state, the master node B to be selected is next, and the master node C to be selected finally enters the master silent state. With continued reference to fig. 2, when master node a to be selected is in the master silent state, B, C may enter the master silent state and may be in the candidate state. That is, when the status flag of the master node a to be selected is the master silence status flag, the status flag of the master node B, C to be selected may be the master silence status flag or the candidate status flag.
Referring to fig. 3, when it is detected that the actual waiting duration of the master node a to be selected in the master silent state reaches the silent waiting duration, the master node a may enter the master state, that is, the status identifier is updated from the silent status identifier to the master status identifier. The master nodes B and C to be selected may be in a master silent state or a candidate state, i.e. the state identifier is a silent state identifier or a candidate state identifier. At this time, the master node a to be selected may be taken as the target master node, that is, the master node to be selected whose status identifier is preferentially changed from the master silent status identifier to the master status identifier may be taken as the target master node. After determining the target master node a, the target master node a may send a master heartbeat message to the other four to-be-used nodes B, C, D, E, so that the other four to-be-used nodes determine that the master node is the to-be-used node a.
Referring to fig. 4, after the other four to-be-used nodes B, C, D, E receive the master heartbeat message, since the state identifier of the to-be-used node D, E is already the standby state identifier, the state identifier of the master node B, C to be selected only needs to be updated to the standby state identifier, that is, the state identifier of the master node to be selected is updated to the standby state identifier as long as the master heartbeat message sent by the target master node is received, regardless of whether the master node to be selected is in the master silent state or the candidate state, so that the master node to be selected executes a corresponding read operation.
According to the technical scheme of the embodiment of the invention, the master nodes to be selected, the types of which are consistent with the preset node types, are determined from the nodes to be used, the waiting time of the master nodes to be selected in each state is determined according to the attribute information of the master nodes to be selected, and the state identification of the corresponding master nodes to be selected is adjusted based on the waiting time, so that the target master node is determined from the master nodes to be selected according to the state identification, and in this way, a cache database (independent of the cache database) is not required to be additionally introduced, and the method is not limited by the number of the usable nodes, namely, the problems that the master nodes are determined by depending on the database, the instability of the database easily causes database faults, and the master nodes cannot be determined timely and effectively in the prior art are solved; meanwhile, the problem that the host node is limited by the number of usable nodes and the complexity of the distributed cluster and has poor universality is solved, the effectiveness, convenience and universality of the determination of the host node are improved, and the use safety and the universality of the distributed cluster are further improved.
Fig. 5 is a schematic flow chart of a method for determining a target master node according to an embodiment of the present invention, where on the basis of the foregoing embodiment, if at least one new node to be used is added under the condition that the target master node is determined, the node type of the node to be used may be a master node type or a non-master node type, and the node state of the newly added node to be used may be updated in the following manner. The technical terms that are the same as or corresponding to the above embodiments are not repeated herein.
As shown in fig. 5, the method includes:
s210, determining the master nodes to be selected which are consistent with the preset node types according to the node types of the nodes to be used.
S220, according to the attribute information of each master node to be selected, determining the theoretical waiting time of the corresponding master node to be selected in each waiting state, and updating the state identifier of the corresponding master node to be selected based on the theoretical waiting time.
In this embodiment, updating the state identifier of the corresponding master node to be selected based on the theoretical waiting duration may be: for each master node to be selected, if the current master node to be selected is detected to enter a candidate state, updating the state identifier of the master node to be selected into a candidate state identifier, and when the actual duration of the current master node to be selected in the candidate state reaches the candidate waiting duration, entering the master silent state, and updating the state identifier of the current master node to be selected from the candidate state identifier into a master silent state identifier; and if the actual silent duration of the current master node to be selected in the master silent state reaches the silent waiting duration, updating the state identifier of the current master node to be selected from the master silent state identifier to the master state identifier.
The candidate state identifier is an identifier corresponding to the candidate state. The master silence state identification is a silence state identification corresponding to the master silence state. The master state identifier is an identifier of a master node to be selected as a target master node.
And S230, determining a target main node according to the state identifier, broadcasting a main heartbeat message to each node to be used based on the target main node, so that each node to be used determines the target main node according to the received main heartbeat message, and updating the state identifiers of other main nodes to be selected into standby state identifiers.
S240, if a newly added node to be used is detected and the node to be used receives the main heartbeat message sent by the target main node, updating the state identifier of the node to be used into a standby state identifier.
On the basis of the above technical solution, if at least one node to be used is newly added, the newly added node to be used may be processed based on S210 to S230, so that the distributed cluster is balanced.
Illustratively, two nodes to be used are newly added to the cluster, the node type of one node to be used is the master node type and is marked as F, and the node type of the other node to be used is the non-master node type and is marked as G. Referring to fig. 6, nodes F and G to be used in the initialization state are in an initial state. Referring to fig. 7, after the node F to be used is initialized, the candidate state may be converted into a standby state identifier if the master heartbeat packet broadcast by the target master node a is received, that is, the standby state identifier is used as a read operation node. The node G to be used can be directly converted into a standby state after initialization because the node G is an arbitration node.
According to the technical scheme of the embodiment of the invention, when the nodes to be used are newly added under the condition that the target master node is determined, the master heartbeat message can be broadcasted to the nodes to be used based on the target master node, so that after the nodes to be used are newly added and receive the master heartbeat message, the state identification of the nodes to be used is updated to the alternative state identification, and the corresponding reading operation is executed, so that the distributed cluster can still normally provide corresponding service under the condition that the nodes to be used are newly added, and the technical effect of improving the data processing safety is achieved.
Fig. 8 is a flowchart illustrating a method for determining a target master node according to an embodiment of the present invention, where, based on the foregoing embodiment, the target master node provides read/write operations, and if a network outage or power failure occurs, the target master node may not provide corresponding services, and at this time, the target master node may be determined from a node to be used whose state identifier is a standby state representation and is consistent with a preset node type, and a specific implementation manner of the method may be described in detail in this embodiment. The technical terms that are the same as or corresponding to the above embodiments are not repeated herein.
As shown in fig. 8, the method includes:
s310, determining a master node to be selected which is consistent with the preset node type according to the node type of each node to be used.
S320, according to the attribute information of each main node to be selected, determining the theoretical waiting time of the corresponding main node to be selected in each waiting state, and updating the state identifier of the corresponding main node to be selected based on the theoretical waiting time.
S330, determining a target main node according to the state identifier, broadcasting a main heartbeat message to each node to be used based on the target main node, so that each node to be used determines the target main node according to the received main heartbeat message, and updating the state identifiers of other main nodes to be selected into standby state identifiers.
S340, when the target main node is detected to be offline, the main node to be selected, which is consistent with the preset node type, is determined from all the nodes to be used of the standby state identification, the target main node is re-determined according to the attribute information of all the main nodes to be selected, and the state identification of the nodes to be used, which is inconsistent with the preset node type, is kept unchanged as the standby state identification.
The off-line can be understood as that the target host node does not provide read-write operation. The nodes to be used of the standby state identifier include nodes of a master node type and nodes of a non-master node type, a target master node can be determined from the nodes to be used of the master node type, and a specific determination method can be repeatedly executed from S310 to S330.
Specifically, when it is detected that the target master node goes offline, a node that is consistent with a preset node type (master node type) may be determined from the nodes to be used whose state identifiers are in the standby state as the master node to be selected. When the main node to be selected enters the candidate state, the candidate waiting time length in the candidate state can be determined according to the priority, and the main silent state is entered when the actual waiting time length reaches the candidate waiting time length. Meanwhile, the silent waiting time of the master node to be selected in the master silent state can still be determined according to the priority, and when the actual silent waiting time of the master node to be selected is detected to reach the silent waiting time, the status identifier of the master node to be selected is updated to the master status identifier from the silent status identifier. If the master node to be selected is the first master state identifier, the master node to be selected may be used as the target master node.
For example, referring to fig. 9, at a certain time, a target master node a in the cluster goes offline, and the nodes to be used in the standby status identifier that match the master node type include node B, C, F. When the node to be used B, C, F monitors that the master heartbeat times out, that is, when the heartbeat message broadcast by the target master node a is not received for a long time, the node to be used B, C, F may enter a candidate state as the master node to be selected, see fig. 10, and S310 to S330 are repeatedly executed to determine the target master node from the master node to be selected B, C, F. If the determined target master node is C, the status flag of the master node to be selected B, F is updated to the standby status flag to perform the read operation, see fig. 11. Meanwhile, the nodes to be used in the standby state identification, which are inconsistent with the preset node type, keep the standby state identification unchanged and still execute corresponding reading operation.
It should be noted that the candidate waiting duration and the silent waiting duration are determined by using a random algorithm, so that a node to be used first becomes a target master node, and when other master nodes to be selected receive a heartbeat message broadcast by the target master node, the node can be converted into a standby state.
It should be further noted that, when a master node to be selected is a target master node, due to factors such as a network, the broadcasted master heartbeat packet is not received by the node in the master silent state, and at this time, the node in the master silent state becomes the master node. The method comprises the steps that when two main nodes receive heartbeat messages broadcasted mutually, the state identification of the main node with the lower priority level can be updated to be the standby state identification, and the node with the higher priority level is reserved as the main state identification, namely the node is the target main node. Of course, if the priority levels of the two host nodes are the same, the adopted measures may be to re-determine the target host node, or to take any host node as the target host node, update the status identifier of the other host node to the standby status identifier, and execute the read operation.
That is to say, the technical scheme realizes the persistent stability and reliability of the target host node in the cluster, does not depend on third-party middleware such as a database and the like, and reduces the complexity of the system. Furthermore, the method for determining the target main node has good compatibility with the system, is easier to expand in a multi-language environment, and weakens the coupling with a third-party system. Furthermore, when the target master node is determined again after the target master node is offline, the requirements on the reliability of system hardware are reduced without depending on the number, parity and the like of the surviving nodes. Finally, the existing small-sized distributed system is generally built in a mode of a public network card by adopting a three-network result built by a management network, a storage network and a service network. When the traffic volume sharply increases (for example, under the condition that a certain website does work), backlog is generated by network disk IO, and there may be delay in heartbeat messages between nodes, that is, it is considered that the master node is offline, and in this case, frequent master selection may be generated in the prior art, which results in that the service is always unavailable.
According to the technical scheme of the embodiment of the invention, when the target main node is detected to be offline, the main node to be selected can be determined from the nodes to be used, which are consistent with the main node types, in the standby state identification, and the target main node can be determined from the main nodes to be selected repeatedly according to the attribute information of each main node to be selected, so that the target main node can be determined quickly and conveniently on the basis of not depending on other databases and the number of the nodes, and the convenience and the universality for determining the target main node are improved.
Fig. 12 is a schematic structural diagram of an apparatus for determining a master node according to an embodiment of the present invention, where the apparatus may be integrated in a distributed cluster, and the apparatus includes: a master node to be selected determiningmodule 410, a statusinformation determining module 420, and a target masternode determining module 430.
The to-be-selected masternode determining module 410 is configured to determine, according to the node type of each to-be-used node, a to-be-selected master node that is consistent with a preset node type; the stateinformation determining module 420 is configured to determine, according to the attribute information of each master node to be selected, a theoretical waiting time of each master node to be selected in each waiting state, and update a state identifier of the corresponding master node to be selected based on the theoretical waiting time; and the target masternode determining module 430 is configured to determine a target master node according to the state identifier, broadcast a master heartbeat message to each node to be used based on the target master node, so that each node to be used determines the target master node according to the received master heartbeat message, and update the state identifiers of other master nodes to be selected as standby state identifiers.
On the basis of the above technical solution, the preset node type includes a master node type, and the to-be-selected master node determining module includes:
a node type obtaining unit, configured to obtain a node type of each node to be used; the node types comprise a main node type and a non-main node type;
and the to-be-selected main node determining unit is used for taking the to-be-used node with the same type as the main node as the to-be-selected main node.
On the basis of the above technical solution, the apparatus further includes: a processing module, comprising:
the first processing unit is used for determining a node which is inconsistent with a preset node type from the nodes to be used if the distributed cluster is in a starting stage, and updating the state identifier of the node from an initial identifier to a standby state identifier;
and the second processing unit is used for keeping the state identifier of the node to be used, which is inconsistent with the preset node type, unchanged as the standby state identifier if the distributed cluster is in the operation stage.
On the basis of the above technical solution, the waiting status includes a candidate status and a main silent status, the theoretical waiting duration includes a candidate waiting duration in the candidate status and a silent waiting duration in the main silent status, and the status information determining module is further configured to:
and determining the candidate waiting time of the corresponding main node to be selected in the candidate state and the silent waiting time of the main silent state according to the node priority in the attribute information.
On the basis of the above technical solution, the state information determining module includes:
a candidate waiting time length determining unit, configured to determine, for each master node to be selected, a candidate waiting time length of the current master node to be selected in a candidate state according to the node priority of the current master node to be selected, target time information updated to be the candidate state identifier, and heartbeat time length information when it is detected that the state identifier of the current master node to be selected is updated to be the candidate state identifier;
and the silence waiting duration determining unit is used for determining the silence waiting duration of the current main node to be selected in the main silence state according to the node priority and the preset value of the current main node to be selected when the condition identification of the current main node to be selected is updated to the main silence condition identification from the candidate condition identification.
On the basis of the technical scheme, the target main node determining module comprises:
a first identifier updating unit, configured to update, for each master node to be selected, a state identifier of the master node to be selected as a candidate state identifier if it is detected that the current master node to be selected enters a candidate state, and enter the master silent state if it is detected that an actual duration of the current master node to be selected in the candidate state reaches the candidate waiting duration, and update the state identifier of the current master node to be selected from the candidate state identifier to the master silent state identifier;
and the second identifier updating unit is used for updating the state identifier of the current master node to be selected from the master silent state identifier to the master state identifier if the fact that the actual silent duration of the current master node to be selected in the master silent state reaches the silent waiting duration is detected.
On the basis of the above technical solution, the target master node determining module is further configured to:
and preferentially updating the state identifier into the master state identifier to be selected as the target master node.
On the basis of the technical scheme, the target main node determining module comprises:
the target main node determining unit is used for determining a target main node from main nodes to be selected corresponding to at least two state identifiers by adopting a random algorithm if the fact that the at least two state identifiers are updated to be the main state identifiers at the same time is detected;
and the main heartbeat message broadcasting unit is used for broadcasting the main heartbeat message to other nodes to be used based on the target main node.
On the basis of the above technical solution, the apparatus further includes:
and the first target main node re-determination module is used for determining a main node to be selected, which is consistent with the preset node type, from all nodes to be used of the standby state identifier when the target main node is detected to be offline, re-determining the target main node according to the attribute information of all the main nodes to be selected, and keeping the state identifier of the node to be used, which is inconsistent with the preset node type, as the standby state identifier unchanged.
On the basis of the above technical solution, the apparatus further includes:
and if the first target main node re-determination module detects that a new node to be used is added and the node to be used receives a main heartbeat message sent by the target main node, updating the state identifier of the node to be used into a standby state identifier.
According to the technical scheme of the embodiment of the invention, the master nodes to be selected, the types of which are consistent with the preset node types, are determined from the nodes to be used, the waiting time of the master nodes to be selected in each state is determined according to the attribute information of the master nodes to be selected, and the state identification of the corresponding master nodes to be selected is adjusted based on the waiting time, so that the target master node is determined from the master nodes to be selected according to the state identification, and in this way, a cache database (independent of the cache database) is not required to be additionally introduced, and the method is not limited by the number of the usable nodes, namely, the problems that the master nodes are determined by depending on the database, the instability of the database easily causes database faults, and the master nodes cannot be determined timely and effectively in the prior art are solved; meanwhile, the problem that the host node is limited by the number of usable nodes and the complexity of the distributed cluster and has poor universality is solved, the effectiveness, convenience and universality of the determination of the host node are improved, and the use safety and the universality of the distributed cluster are further improved.
The device for determining the master node provided by the embodiment of the invention can execute the method for determining the master node provided by any embodiment of the invention, and has corresponding functional modules and beneficial effects of the execution method.
It should be noted that, the units and modules included in the apparatus are merely divided according to functional logic, but are not limited to the above division as long as the corresponding functions can be implemented; in addition, specific names of the functional units are only for convenience of distinguishing from each other, and are not used for limiting the protection scope of the embodiment of the invention.
Fig. 13 is a schematic structural diagram of an electronic device according to an embodiment of the present invention. FIG. 13 illustrates a block diagram of an exemplaryelectronic device 50 suitable for use in implementing embodiments of the present invention. Theelectronic device 50 shown in fig. 13 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiment of the present invention.
As shown in fig. 13, theelectronic device 50 is in the form of a general purpose computing device. The components of theelectronic device 50 may include, but are not limited to: one or more processors orprocessing units 501, asystem memory 502, and abus 503 that couples the various system components (including thesystem memory 502 and the processing unit 501).
Bus 503 represents one or more of any of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor, or a local bus using any of a variety of bus architectures. By way of example, such architectures include, but are not limited to, Industry Standard Architecture (ISA) bus, micro-channel architecture (MAC) bus, enhanced ISA bus, Video Electronics Standards Association (VESA) local bus, and Peripheral Component Interconnect (PCI) bus.
Electronic device 50 typically includes a variety of computer system readable media. Such media may be any available media that is accessible byelectronic device 50 and includes both volatile and nonvolatile media, removable and non-removable media.
Thesystem memory 502 may include computer system readable media in the form of volatile memory, such as Random Access Memory (RAM)504 and/orcache memory 505. Theelectronic device 50 may further include other removable/non-removable, volatile/nonvolatile computer system storage media. By way of example only,storage system 506 may be used to read from and write to non-removable, nonvolatile magnetic media (not shown in FIG. 13, commonly referred to as a "hard drive"). Although not shown in FIG. 13, a magnetic disk drive for reading from and writing to a removable, nonvolatile magnetic disk (e.g., a "floppy disk") and an optical disk drive for reading from or writing to a removable, nonvolatile optical disk (e.g., a CD-ROM, DVD-ROM, or other optical media) may be provided. In these cases, each drive may be connected to thebus 503 by one or more data media interfaces.Memory 502 may include at least one program product having a set (e.g., at least one) of program modules that are configured to carry out the functions of embodiments of the invention.
A program/utility 508 having a set (at least one) ofprogram modules 507 may be stored, for instance, inmemory 502,such program modules 507 including, but not limited to, an operating system, one or more application programs, other program modules, and program data, each of which examples or some combination thereof may comprise an implementation of a network environment.Program modules 507 generally perform the functions and/or methodologies of embodiments of the invention as described herein.
Theelectronic device 50 may also communicate with one or more external devices 509 (e.g., keyboard, pointing device,display 510, etc.), with one or more devices that enable a user to interact with theelectronic device 50, and/or with any devices (e.g., network card, modem, etc.) that enable theelectronic device 50 to communicate with one or more other computing devices. Such communication may occur via input/output (I/O) interfaces 511. Also, theelectronic device 50 may communicate with one or more networks (e.g., a Local Area Network (LAN), a Wide Area Network (WAN), and/or a public network, such as the Internet) via thenetwork adapter 512. As shown, thenetwork adapter 512 communicates with the other modules of theelectronic device 50 over thebus 503. It should be appreciated that although not shown in FIG. 13, other hardware and/or software modules may be used in conjunction withelectronic device 50, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, among others.
Theprocessing unit 501 executes various functional applications and data processing by executing programs stored in thesystem memory 502, for example, to implement the method for determining a target master node provided by the embodiment of the present invention.
Embodiments of the present invention also provide a storage medium containing computer-executable instructions which, when executed by a computer processor, perform a method of determining a target master node.
The method comprises the following steps:
determining a master node to be selected which is consistent with a preset node type according to the node type of each node to be used;
according to the attribute information of each master node to be selected, determining the theoretical waiting time of the corresponding master node to be selected in each waiting state, and updating the state identifier of the corresponding master node to be selected based on the theoretical waiting time;
and determining a target main node according to the state identifier, broadcasting a main heartbeat message to each node to be used based on the target main node so that each node to be used determines the target main node according to the received main heartbeat message, and updating the state identifiers of other main nodes to be selected into standby state identifiers.
Computer storage media for embodiments of the invention may employ any combination of one or more computer-readable media. The computer readable medium may be a computer readable signal medium or a computer readable storage medium. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples (a non-exhaustive list) of the computer readable storage medium would include the following: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the context of this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
A computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Computer program code for carrying out operations for embodiments of the present invention may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C + + or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
It is to be noted that the foregoing is only illustrative of the preferred embodiments of the present invention and the technical principles employed. It will be understood by those skilled in the art that the present invention is not limited to the particular embodiments described herein, but is capable of various obvious changes, rearrangements and substitutions as will now become apparent to those skilled in the art without departing from the scope of the invention. Therefore, although the present invention has been described in greater detail by the above embodiments, the present invention is not limited to the above embodiments, and may include other equivalent embodiments without departing from the spirit of the present invention, and the scope of the present invention is determined by the scope of the appended claims.