Resource detection method and device, equipment, storage medium and product
By introducing a leadership node election mechanism into the node cluster of distributed systems, and using the resource information of candidate nodes to determine the leadership node, the problem of insufficient resource detection automation and reliability in the existing technology is solved, and efficient node cluster management is achieved.
Patent Information
- Application Number
- CN202311318910.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-11
- Publication Date
- 2025-04-11
AI Technical Summary
In a distributed system, resource information of each node needs to be detected to understand the overall situation of the system for service management, but it is difficult for the prior art to achieve automated and highly reliable resource detection.
By introducing a leadership node election mechanism into the node cluster, the resource information detected by the candidate node is used to vote, and the leadership node and follower node are automatically determined, thereby obtaining the resource information of the node cluster.
It realizes that the election instructions are triggered and new leadership nodes are elected without user participation, improving the reliability and automated management capabilities of the node cluster.
Smart Images

Figure CN119814798A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to a resource detection method, device, equipment, storage medium and product. Background Art
[0002] For a distributed system corresponding to a certain service, the distributed system includes multiple nodes, and the service runs on each node; it is necessary to detect the resource information of each node to understand the overall situation of the distributed system, so as to manage the service. Summary of the invention
[0003] The embodiments of the present application provide a resource detection method, device, equipment, storage medium and product, which can improve the reliability of node clusters. The technical solution is as follows:
[0004] In one aspect, a resource detection method is provided, the method comprising:
[0005] The method is applied in a node cluster, wherein the node cluster includes a plurality of nodes running a service; the method includes:
[0006] When the node cluster triggers an election instruction of a leader node, obtaining resource information detected by multiple candidate nodes in the node cluster;
[0007] Determine, based on the resource information detected by the multiple candidate nodes, first voting results of other nodes in the node cluster on the multiple candidate nodes, where the first voting results are used to represent the voting ranking of the multiple candidate nodes;
[0008] Based on the first voting result, determining a leader node and a follower node in the node cluster;
[0009] The leader node obtains resource information detected by the follower node based on a first detection period to determine first resource information of the node cluster.
[0010] On the other hand, a resource detection device is provided, the device is applied in a node cluster, the node cluster includes a plurality of nodes running a service; the device includes:
[0011] A first acquisition module, configured to acquire resource information detected by multiple candidate nodes in the node cluster when the node cluster triggers an election instruction of a leader node;
[0012] A first determination module, configured to determine first voting results of other nodes in the node cluster on the multiple candidate nodes based on resource information detected by the multiple candidate nodes, wherein the first voting results are used to represent a voting ranking of the multiple candidate nodes;
[0013] A second determination module, configured to determine a leader node and a follower node in the node cluster based on the first voting result;
[0014] The third determination module is used to obtain the resource information detected by the follower node based on the first detection period through the leader node to determine the first resource information of the node cluster.
[0015] On the other hand, a node is provided, which includes one or more processors and one or more memories, wherein at least one program code is stored in the one or more memories, and the at least one program code is loaded and executed by the one or more processors to implement the resource detection method as described above.
[0016] On the other hand, a computer-readable storage medium is provided, wherein at least one program code is stored in the storage medium, and the at least one program code is loaded and executed by a processor to implement the resource detection method described above.
[0017] On the other hand, a computer program product is provided. The computer program product stores at least one program code. The at least one program code is used to be executed by a processor to implement the resource detection method described above.
[0018] In an embodiment of the present application, when an election instruction is triggered, the election of a leader node is performed by voting on the resource information detected by the candidate nodes, thereby eliminating the need for user participation, adaptively triggering the election instruction, and adaptively electing a leader node. In this way, even if the previous leader node leaves or is not suitable to continue to serve as the leader node due to problems such as insufficient power, a new leader node can be quickly elected, thereby enabling the node cluster to work normally and improving the reliability of the node cluster. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 A schematic diagram of an implementation environment of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0020] Figure 2 A flow chart of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0021] Figure 3 A schematic diagram showing state changes of multiple nodes shown in an exemplary embodiment of the present application is shown;
[0022] Figure 4 A flow chart of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0023] Figure 5A flow chart of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0024] Figure 6 A flowchart of a heartbeat processing function shown in an exemplary embodiment of the present application is shown;
[0025] Figure 7 A flow chart of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0026] Figure 8 A flow chart of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0027] Fig. 9 A flow chart of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0028] Fig.10 A schematic diagram of a resource detection method shown in an exemplary embodiment of the present application is shown;
[0029] Fig.11 A block diagram of a resource detection device shown in an exemplary embodiment of the present application is shown;
[0030] Fig.12 A block diagram of a terminal according to an exemplary embodiment of the present application is shown. DETAILED DESCRIPTION
[0031] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0032] The term "multiple" as used herein refers to two or more than two. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the related objects are in an "or" relationship.
[0033] It should be noted that the information (including but not limited to user device information, user personal information, etc.), data (including but not limited to data used for analysis, stored data, displayed data, etc.) and signals involved in this application are all authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions. For example, the resource information involved in this application is obtained with full authorization.
[0034] Please refer to Figure 1 , which shows a schematic diagram of a node cluster shown in an exemplary embodiment of the present application. Figure 1, the node cluster includes multiple nodes running services, and the multiple nodes are connected through the D2D network. Accordingly, the multiple nodes can communicate directly based on the D2D network without going through the core network of the network operator. In addition, the multiple nodes in the D2D network communicate directly through Wi-Fi, so that the multiple nodes form a local area network. For example, the multiple nodes in the D2D network include a wireless access point and multiple terminal devices, and the multiple terminal devices can be smart phones, tablet computers or smart watches. In addition, in the D2D network, multiple terminal devices can be connected to the same subnet, and the IP address used is 192.168.1.x. For example, multiple terminal devices can be connected to the same Wi-Fi network to form a local area network. In some embodiments, devices such as routers and switches can also be used in the D2D network to manage and maintain the network to ensure the stability and security of the D2D network.
[0035] Multiple nodes in the D2D network will form a mobile device cloud collaborative computing architecture. Multiple nodes will send the resource information detected by themselves, and multiple nodes will elect a leader node, which is responsible for the detection and overall scheduling of resource information at the current stage; and other nodes in the multiple nodes temporarily serve as follower nodes. In addition, each node includes a basic support module and a leader node election module; the basic support module is responsible for communicating with other nodes to obtain the resource information detected by other nodes, and transmit the resource information detected by other nodes to the leader node election module. The leader node election module is used to elect a leader node based on the resource information detected by other nodes, and notify the basic support module whether it is a leader node; if it is a leader node, the basic support module acts as a heartbeat sender to send heartbeats to other nodes to request to obtain the resource information detected by other nodes; if it is not a leader node, the basic support module acts as a heartbeat receiver to receive the heartbeat sent by the leader node to send the detected resource information to the leader node based on the heartbeat. The resource information includes at least one of CPU utilization, memory utilization, and network bandwidth.
[0036] In some embodiments, the service running in the node can be any ubiquitous service, and a ubiquitous service refers to a service with dynamic characteristics; for example, a ubiquitous service can be a video service, an audio service, a social service, a short video service, etc.; for example, the service running in the node is a video service, and the multiple nodes can be smart phones, tablet computers, and smart TVs; then, through the method provided in the embodiment of the present application, the resource information detected by the multiple nodes is obtained, and based on the resource information detected by the multiple nodes, it is determined through which device's video service to play the video.
[0037] Please refer to Figure 2, which shows a flow chart of a resource detection method shown in an exemplary embodiment of the present application. The method is applied in a node cluster, and the node cluster includes multiple nodes running a service. The method includes:
[0038] Step 201: When the node cluster triggers the election instruction of the leader node, the resource information detected by multiple candidate nodes in the node cluster is obtained.
[0039] Resource information includes at least one of CPU utilization, memory utilization, network bandwidth and battery utilization, etc. Resource information can also include service information of its running services. In addition, resource information can be expanded by itself. Since the resource information needs to participate in the result judgment of the election process, a custom sorting method is also defined in the resource information. The custom sorting method includes the interface that needs to be implemented. For example, the resource information also includes Len() function, Swap function and Less function. For any node, after receiving the resource information detected by multiple candidate nodes, the node stores the resource information in the infoList list; the Len function is used to return the length of the list, and the Swap function defines the element exchange method in the list, that is, the exchange method of multiple information in the resource information; the Less function is responsible for comparing the size of the elements in the list. The Less function is the core method used to determine whether a node can be elected as a leader node in an election; and the Les function can be set and changed as needed; for example, other Less can be changed to elect different nodes as leader nodes through other methods.
[0040] The node roles in the node cluster are divided into leader nodes, candidate nodes, and follower nodes; and at any time, there is at most one leader node in the node cluster, and the resource information in the node cluster is completed by the leader node grabbing the resource information of other nodes. In addition, the leader node sends heartbeat information at regular intervals to notify other nodes in the node cluster of its existence and obtain resource information detected by other nodes. Candidate nodes are nodes that run for the leader node, and follower nodes are nodes in the node cluster other than the leader node and candidate nodes, which are used to detect their own resource information and report it to the leader node based on the heartbeat information of the leader node.
[0041] In this step, for any candidate node, the candidate node detects its own resource information and sends the resource information to other nodes in the node cluster; for any node in the node cluster, the resource information sent by multiple candidate nodes is received, so that through the above steps, each node can obtain the resource information detected by multiple candidate nodes. For any node in the node cluster, Waitgroup wg is set and controlled, and multi-threaded concurrent control is performed through Waitgroup wg, and resource information detected by multiple candidate nodes is obtained through multiple threads.
[0042] In some embodiments, any node in the node cluster can serve as a candidate node; for example, for any node in the node cluster, before the node cluster triggers an election instruction, the role of the node in the node cluster is a follower node; when the node cluster triggers an election instruction, the role of the node in the node cluster is changed from a follower node to a candidate node.
[0043] In some embodiments, the triggering timing of the election instruction includes any of the following methods;
[0044] For the first method: when the node cluster is initialized, determine that the node cluster triggers the election instruction of the leader node.
[0045] When the node cluster is initialized, the roles of multiple nodes in the node cluster are all follower nodes. Since there is no leader node in the node cluster, the follower node cannot receive the heartbeat information of the leader node. When the follower node does not receive the heartbeat information of the leader node within the first preset time, it is determined that the node cluster triggers the election instruction of the leader node. In the embodiment of the present application, when the node cluster is initialized, the leader node is elected, so that the user does not need to manually specify the leader node, and thus the node cluster can be run without the user's participation.
[0046] For the second method: when the previous leader node in the node cluster goes offline, determine that the node cluster triggers the election instruction of the leader node.
[0047] When the previous leader node goes offline (for example, the previous leader node leaves the node cluster), the follower nodes in the node cluster will not receive the heartbeat information of the leader node, that is, when the follower node does not receive the heartbeat information of the leader node within the first preset time, it is determined that the node cluster triggers the election instruction of the leader node. In the implementation of this application, when the previous leader node goes offline, the election instruction of the leader node is triggered, and a new leader node is elected based on the election instruction, thereby avoiding the situation where the node cluster cannot work due to the lack of a leader node, and improving the reliability of the node cluster.
[0048] For the third method: when the previous leader node in the node cluster does not meet the conditions for taking on the leadership node, the node cluster is determined to trigger an election instruction for the leader node.
[0049] The conditions for taking the position include at least one of the remaining power being greater than a preset power, the network bandwidth being greater than a preset bandwidth, and the CPU utilization being less than a preset utilization; for example, when the remaining power of the previous leader node is not greater than the preset power, it is determined that the node cluster triggers the election instruction of the leader node; for another example, when the network bandwidth of the previous leader node is not greater than the preset bandwidth, it is determined that the node cluster triggers the election instruction of the leader node; for another example, when the CPU utilization of the previous leader node is not less than the preset utilization, it is determined that the node cluster triggers the election instruction of the leader node. In the implementation of the present application, due to the limited resources such as power, network bandwidth, and CPU utilization of the nodes in the node cluster, the leader node cannot always serve as the leader node. Through the method of the embodiment of the present application, when the previous leader node does not meet the conditions for taking the position of the leader node, the election instruction is triggered, so that a new leader node is elected based on the election instruction, thereby ensuring the normal operation of the node cluster.
[0050] In some embodiments, when the leader node is offline or is not suitable to continue to serve as the leader node due to reasons such as power exhaustion, it automatically becomes a follower node. The data structure State contains three node states: the value corresponding to the follower node is 0; the value corresponding to the leader node is 1; the value corresponding to the candidate node is 2; each node is in at least one of the states and will switch between these three states; the state changes of the leader node, follower node and candidate node are as follows Figure 3 shown.
[0051] For the fourth method: when the previous leader node in the node cluster does not obtain the resource information detected by the follower node but obtains an error indication, it is determined that the node cluster triggers an election instruction of the leader node.
[0052] The previous leader node will periodically send heartbeat information to the follower node to request the resource information detected by the follower node; when the follower node receives the heartbeat information sent by the previous leader node and agrees that the previous leader node continues to serve as the leader node, it will return the resource information detected by itself to the previous leader node; when the follower node receives the heartbeat information sent by the previous leader node but does not agree that the previous leader node continues to serve as the leader node, it will return an error indication to the previous leader node; when the previous leader node receives the error indication, the election instruction is triggered. In some embodiments, the election instruction is triggered when the previous leader node receives an error indication returned by a follower node; in other embodiments, the election instruction is triggered when the previous leader node receives an error indication returned by a first preset number of follower nodes, that is, when some follower nodes disagree that the previous leader node continues to serve as the leader node, the leader node is re-elected, avoiding the situation of excessive resource consumption caused by multiple elections of leader nodes.
[0053] For the fifth method: when the follower node in the node cluster does not receive the heartbeat information of the previous leader node, it is determined that the node cluster triggers the election instruction of the leader node.
[0054] When a second preset number of follower nodes do not receive the heartbeat information of the previous leader node within a second preset time period before the current time, it is determined that the previous leader node cannot continue to serve as the leader node, and an election instruction is triggered to re-elect the leader node.
[0055] In the embodiments of the present application, the above five implementation methods are for example only and do not limit the timing of triggering the election instruction, and may be expanded to other triggering timings later.
[0056] Step 202: Based on the resource information detected by the multiple candidate nodes, determine the first voting results of the other nodes in the node cluster on the multiple candidate nodes, where the first voting results are used to represent the voting ranking of the multiple candidate nodes.
[0057] For any candidate node, after the candidate node obtains the resource information detected by itself, it sends the resource information to other nodes in the node cluster; after receiving the resource information, other nodes vote on the resource information to obtain a second voting result, and the second voting result is used to indicate whether to agree with the candidate node to serve as the leader node; since there are multiple candidate nodes in the node cluster, for any other node, multiple candidate nodes will be voted on to obtain multiple second voting results, and based on the multiple voting results, the votes for the multiple candidate nodes will be sorted in order from most to least votes to obtain a first voting result, and the first voting result will be sent to other nodes, so that all nodes in the node cluster will receive the first voting result.
[0058] Step 203: Based on the first voting result, determine the leader node and follower nodes in the node cluster.
[0059] The first voting result is obtained by sorting the votes for multiple candidate nodes in order from most to least number of votes; therefore, the candidate node ranked first in the first voting result is determined as the leader node, and the other nodes in the node cluster except the leader node are determined as follower nodes.
[0060] In some embodiments, the candidate nodes that are not selected as the leader nodes may continue to be candidate nodes or may be converted to follower nodes.
[0061] Step 204: Obtain resource information detected by the follower nodes through the leader node based on the first detection cycle to determine first resource information of the node cluster.
[0062] The leader node will periodically send heartbeat information to the follower nodes in the node cluster to request the resource information detected by the follower nodes. Then the leader node will also obtain the resource information detected by itself, and combine the resource information detected by itself and the resource information detected by the follower nodes to form the first resource information of the node cluster.
[0063] In an embodiment of the present application, when an election instruction is triggered, the election of a leader node is performed by voting on the resource information detected by the candidate nodes, thereby eliminating the need for user participation, adaptively triggering the election instruction, and adaptively electing a leader node. In this way, even if the previous leader node leaves or is not suitable to continue to serve as the leader node due to problems such as insufficient power, a new leader node can be quickly elected, thereby enabling the node cluster to work normally and improving the reliability of the node cluster.
[0064] Please refer to Figure 4 , which shows a flow chart of a resource detection method shown in an exemplary embodiment of the present application. The method is applied in a node cluster, and the node cluster includes multiple nodes running services; the method includes:
[0065] Step 401: When the node cluster triggers the election instruction of the leader node, the resource information detected by multiple candidate nodes in the node cluster is obtained.
[0066] In some embodiments, this step is the same as step 201 and will not be repeated here.
[0067] Step 402: For any candidate node, add the resource information detected by the candidate node to the voting request structure.
[0068] The candidate node uses gopsutil / mem and gopsutil / cpu to obtain the detected resource information; and sets a voting request structure (RequestVoteArgs), adds the resource information detected by the candidate node to the voting request structure, and then executes step 402.
[0069] In some embodiments, after the candidate node adds the detected resource information to the voting request structure, it also performs a counting conversion on the resource information in the voting request structure through the Swap function; the counting conversion process refers to setting weights for multiple options included in the resource information. For example, the resource information includes CPU utilization, memory utilization, network bandwidth, and battery usage; and the weights of CPU utilization, memory utilization, network bandwidth, and battery usage are 0.5, 0.2, 0.2, and 0.1, respectively; then the resource information in the voting request structure is counted and converted through the Swap function, and the converted voting request structure is 0.5 CPU utilization, 0.2 memory utilization, 0.2 network bandwidth, and 0.1 battery usage.
[0070] In some embodiments, after the candidate node detects the resource information, it stores the detected resource information in the infoList list. In addition, the candidate node sets a loop to traverse all nodes in the node cluster and applies the following logic to each node; if the currently traversed node is itself, the following steps are skipped; if the currently traversed node is not itself, step 403 is executed.
[0071] Step 403: Send a voting request structure to other nodes in the node cluster, and receive a first voting response structure returned by other nodes.
[0072] The first voting response structure includes the second voting result of the other nodes on the candidate node based on the voting request structure, and the second voting result is used to indicate whether the candidate node is approved to be elected as the leader node. The candidate node creates a thread and obtains the node identification of other nodes, the voting request structure, and the reference of the multi-thread control wg. This step can be implemented by the following steps (1) to (4), that is, the following steps (1) to (4) are executed in the newly created thread.
[0073] (1) Create a second voting response structure.
[0074] The second voting response structure (RequestVoteReply) is a structure used to receive the voting return value.
[0075] (2) Establishing communication connections with other nodes based on the node identifiers of other nodes.
[0076] The node identifiers of other nodes may be unique identifiers owned by other nodes, and the identifiers of nodes in the node cluster are not repeated; and the node identifiers are allocated when the node cluster is initialized and cannot be changed; the node identifiers in the node cluster are incremented from 0 by default; for example, if the node cluster includes 10 nodes, then when the node cluster is initialized, node identifiers are allocated to these 10 nodes, and the node identifiers of these 10 nodes are 0-9 respectively.
[0077] For any node in the node cluster, the node stores an ip list (ipList), which stores the correspondence between the ip addresses and node identifiers of multiple nodes in the node cluster. The ip address of the node is the ip address in the network environment where the node cluster is located. The ip address can be an ip address of a wide area network or an ip address of a local area network. In addition, the node also stores a port list (portList), which stores the correspondence between the ip addresses and port numbers of multiple nodes. The port number can be 7007 by default, and the port number can be set and changed as needed. In this step, the candidate node obtains the ip address of the other node from the ipList based on the node identifier of the other node, and obtains the port number of the other node from the portList based on the ip address of the other node; based on the ip address and port number of the other node, dial (Dial) is called to establish a communication connection (TCP connection) with other nodes.
[0078] (3) Sending a first call request to other nodes through a communication connection, the first call request carrying a voting request structure and a second voting response structure, and the first call request is used to call the voting request processing function of other nodes to obtain the second voting results of other nodes on the candidate node.
[0079] The candidate node sends a first call request to other nodes through a communication connection; the other nodes receive the first call request of the candidate node, obtain the resource information detected by the candidate node based on the voting request structure carried in the call request, and vote on the candidate node through a voting processing function based on the resource information detected by the candidate node to obtain a second voting result, which is used to indicate whether the candidate node is agreed to serve as a leading node, and the second voting result is added to a second voting response structure to obtain a first voting response structure, and the first voting response structure is sent to the candidate node through the communication connection.
[0080] In some embodiments, the voting processing function may be Node.HandleVote. Other nodes add the resource information of the candidate node to their own infoList, then set the success flag in the voting request structure to true, set the node flag in the voting request structure to their own flag, and return an error of nil. In some embodiments, the candidate node may make an error when calling the voting processing function, in which case the first voting response structure includes an error flag.
[0081] (4) Receive a first voting response structure returned by other nodes, where the first voting response structure is obtained by adding the second voting result to the first voting response structure.
[0082] The candidate node receives the first voting response structure returned by other nodes. If the first voting response structure includes the second voting result, the candidate node obtains the second voting result from the first voting response structure. If the first voting response structure does not include the second voting result but includes an error flag, the execution of this thread ends; and the multithread control Done method is called to notify the execution of this thread. In some embodiments, the error flag can be error.
[0083] In some embodiments, the data communicated by each node is not protected in the relevant technology; that is, when each node communicates, the information is transmitted in plain text, and it is impossible to prevent malicious nodes from grabbing and tampering with the data. For example, a node may maliciously guide the election process by using a man-in-the-middle attack, so that a node with insufficient resources becomes a leader node, thereby affecting the availability of the node cluster. In an embodiment of the present application, when communicating between each node, the communication data can also be encrypted to protect the security of the data and enhance the security and availability of the node cluster. Accordingly, step 403 can be: the candidate node encrypts the voting request structure and sends the encrypted voting request structure to other nodes in the node cluster; receives the encrypted first voting response structure returned by other nodes; decrypts the encrypted first voting response structure to obtain the first voting response structure.
[0084] Step 404: Based on the second voting results of other nodes on the multiple candidate nodes, the multiple candidate nodes are sorted to obtain the first voting results.
[0085] For any candidate node, the number of votes for the candidate node is determined based on the second voting results of other nodes on the candidate node, thereby obtaining the votes of multiple candidate nodes. Based on the votes of the multiple candidate nodes, the multiple candidate nodes are sorted in order from most to least votes to obtain the first voting result.
[0086] Step 405: Based on the first voting result, determine the leader node and follower nodes in the node cluster.
[0087] In some embodiments, this step is the same as step 203 and will not be repeated here.
[0088] In some embodiments, a multithreaded control Wait method is used to wait for all threads in the above loop to complete execution. All collected infoLists are sorted, and the sorting logic is performed according to the Swap function settings, which can be modified according to requirements. The node ranked first is determined, and if it is itself, it is converted to a leader node, otherwise it is converted to a follower node.
[0089] Step 406: Obtain resource information detected by the follower nodes through the leader node based on the first detection cycle to determine first resource information of the node cluster.
[0090] In some embodiments, this step is the same as step 204 and will not be repeated here.
[0091] In some embodiments, the related technology does not have an alarm strategy for node cluster anomalies and a log system for recording system operation status; in the embodiments of the present application, an alarm module for alarming possible abnormal conditions such as network anomalies and system operation anomalies can be added to the solution, and these anomalies and information required for operation and maintenance can be recorded in the log to facilitate operation and maintenance personnel to maintain the system and upgrade the system in the future; accordingly, the method also includes: when any node in the node cluster has an operation error, obtaining the error information of any node, and recording the error information in the log corresponding to the node.
[0092] In an embodiment of the present application, when an election instruction is triggered, interaction is performed through a voting request structure and a voting response structure so that the nodes in the node cluster vote on the resource information detected by the candidate node; and the voting request structure and the voting response structure have defined the format of the communication data, so the voting request structure and the voting response structure can improve the performance of data transmission, thereby improving the reliability of the node cluster.
[0093] Please refer to Figure 5 , which shows a flow chart of a resource detection method shown in an exemplary embodiment of the present application. The method is applied in a node cluster, and the node cluster includes multiple nodes running services; the method includes:
[0094] Step 501: When a node cluster triggers an election instruction for a leader node, resource information detected by multiple candidate nodes in the node cluster is obtained.
[0095] In some embodiments, this step is the same as step 201 and will not be repeated here.
[0096] Step 502: Based on the resource information detected by the multiple candidate nodes, determine the first voting results of the other nodes in the node cluster on the multiple candidate nodes, where the first voting results are used to represent the voting ranking of the multiple candidate nodes.
[0097] In some embodiments, this step can be implemented through the above steps 402-404, which will not be repeated here.
[0098] Step 503: Based on the first voting result, determine the leader node and follower nodes in the node cluster.
[0099] In some embodiments, this step is the same as step 203 and will not be repeated here.
[0100] Step 504: Through the leader node, based on the first detection cycle, the following steps 405 and 406 are executed.
[0101] When a node is elected as a leader node, it needs to periodically send heartbeat information to other nodes in the node cluster to inform the remaining nodes of its existence and obtain resource information of the remaining nodes. The first detection period can be a fixed detection period, or a detection period obtained by adaptive adjustment based on the previous detection period; in the embodiment of the present application, the first detection period is an example of a detection period obtained by adaptive adjustment based on the previous detection period, so as to realize dynamic adjustment of the detection period, reduce the power consumption of the detection itself, network bandwidth and other resources, so that this scheme is more suitable for ubiquitous service scenarios with limited resources than the scheme with fixed detection period.
[0102] Step 505: Add resource parameters to the heartbeat request structure.
[0103] The heartbeat request structure may be a HeartBeatsArgs structure; and the leader node sets the resource parameters therein to tell the follower nodes what information it wants to obtain. The resource parameters may be memory utilization or CPU utilization, etc. For example, if the resource parameters include memory utilization, the leader node tells the follower node that it wants to obtain memory utilization; for another example, if the resource information includes CPU utilization, the leader node tells the follower node that it wants to obtain CPU utilization.
[0104] Step 506: Send a heartbeat request structure to the follower node, and receive a first heartbeat response structure returned by the follower node.
[0105] The first heartbeat response structure includes resource information detected by the follower node based on the resource parameters. In some embodiments, this step can be implemented by the following steps (1) to (4), including:
[0106] (1) Create a second heartbeat response structure.
[0107] The second heartbeat response structure can be:
[0108] (2) Based on the node identifier of the follower node, a communication connection is established with the follower node.
[0109] In some embodiments, the process of establishing a communication connection between the leading node and the follower node based on the node identifier of the follower node is similar to the process of establishing a communication connection between the candidate node and other nodes based on the node identifier of other nodes, and will not be repeated here.
[0110] (3) Sending a second call request to the follower node through the communication connection, the second call request carrying a heartbeat request structure and a second heartbeat response structure, and the second call request is used to call the heartbeat request processing function of the follower node to generate a first heartbeat response structure.
[0111] The process of the follower node generating the first heartbeat response structure based on the heartbeat request processing function includes:
[0112] The heartbeat sending state of the leader node is set to not timed out; when the heartbeat sending of the leader node is not timed out, the resource information corresponding to the resource parameter is obtained, the obtained resource information is added to the second heartbeat response structure, and the resource acquisition success flag bit in the second heartbeat response structure is set to the first value to obtain the first heartbeat response structure, and the first value is used to indicate that the resource information is successfully obtained. The first value may be 1; in some embodiments, when the follower node fails to successfully detect the resource information, the follower node sets the resource acquisition success flag bit in the second heartbeat response structure to the second value, and the second value is used to indicate that the resource information acquisition fails, and the second value may be 0.
[0113] For example, see Figure 6 For any node in the node cluster, the node determines whether it is a candidate node; when it is not a candidate node (that is, it is a follower node), the heartbeat sending state of the leader node is set to not timed out (set Node.eleTimer to false); when it is a candidate node, it sets itself as a follower node; then sets Node.eleTimer to false; obtains the resource information requested by the leader node for detection, and adds the detected resource information to the second heartbeat response structure (HeartBeatsReply), sets the resource acquisition success flag to the second value (sets repil.Success to true), and returns an error of zero (nil).
[0114] (4) Receive a first heartbeat response structure returned by the follower node, where the first heartbeat response structure is obtained by adding the resource information detected by the follower node to the first heartbeat response structure.
[0115] The leading node receives the first heartbeat response structure returned by the follower node, and when the resource acquisition success mark bit in the first heartbeat response body is a first value, obtains the resource information detected by the follower node from the first heartbeat response structure; when the resource acquisition success mark bit in the first heartbeat response structure is a second value, the execution of this thread ends; and, the multi-thread control Done method is called to notify this thread of the end of execution.
[0116] In some embodiments, when the nodes communicate with each other, the communication data can also be encrypted to protect the security of the data and enhance the security and availability of the node cluster. Accordingly, step 506 can be: the leader node encrypts the heartbeat request structure, sends the encrypted heartbeat request structure to the follower node in the node cluster; receives the encrypted first heartbeat response structure returned by the follower node; decrypts the encrypted first heartbeat response structure to obtain the first heartbeat response structure.
[0117] Step 507: The resource information detected by the leader node and the resource information detected by the follower nodes are combined into the first resource information of the node cluster.
[0118] In some embodiments, after the leading node obtains the first resource information of the node cluster, it can also report the first resource information to the upper-layer application, and the upper-layer application performs downstream tasks based on the first resource information of the node cluster; for example, the upper-layer application determines which node plays the video based on the first resource information, or, after the leading node obtains the first resource information of the node cluster, it directly performs downstream tasks based on the first resource information, thereby improving the efficiency of executing downstream tasks.
[0119] In an embodiment of the present application, when an election instruction is triggered, interaction is performed through a heartbeat request structure and a heartbeat response structure so that the leading node obtains resource information detected by the follower nodes in the node cluster; and the heartbeat request structure and the heartbeat response structure have defined the format of communication data, so the heartbeat request structure and the heartbeat response structure can improve the performance of data transmission, thereby improving the reliability of the node cluster.
[0120] Please refer to Figure 7 , which shows a flow chart of a resource detection method shown in an exemplary embodiment of the present application. The method is applied in a node cluster, and the node cluster includes multiple nodes running services; the method includes:
[0121] Step 701: Acquire second resource information, where the second resource information is a weighted average value of resource information detected in a historical detection period.
[0122] The leader node stores a weighted average of resource information detected in the historical detection cycle; in this step, the leader node obtains the stored second resource information. In some embodiments, when the node cluster is initialized, the second resource information is the initial resource information.
[0123] Step 702: Perform a weighted average on the second resource information and the first resource information detected in the current detection period to obtain third resource information.
[0124] For example, the leading node performs weighted averaging of the second resource information and the first resource information detected in the current detection cycle through the following formula 1 to obtain the third resource information.
[0125] Formula 1: y t =αx t +(1-α)y t-1
[0126] Among them, y t Indicates the third resource information, x t Indicates the first resource information, y t-1 represents the second resource information, α represents the smoothing coefficient, and the value range of α is 0-1.
[0127] In some embodiments, the leader node uses the EWMA algorithm to update the detection period, and the weighted average is calculated by weighting the historical data and the current value. According to the principle of the EWMA algorithm, the weight of the historical data decreases exponentially with the increase of time. Therefore, the influence of the historical data on the weighted average will gradually weaken, and the influence of the current value will gradually increase.
[0128] Step 703: Update the first detection period based on the third resource information and the first resource information.
[0129] The updated first detection cycle is used for the next heartbeat information transmission by the leading node.
[0130] In an embodiment of the present application, the first detection cycle is dynamically adjusted based on the second resource information detected in the historical detection cycle and the first resource information detected in the current detection cycle, so that the first detection cycle can be dynamically adjusted according to the severity of the resource change, thereby reducing the resource consumption of the detection itself compared to the fixed-period resource detection scheme.
[0131] Please refer to Figure 8 , which shows a flow chart of a resource detection method shown in an exemplary embodiment of the present application. The method is applied in a node cluster, and the node cluster includes multiple nodes running services; the method includes:
[0132] Step 801: Acquire second resource information, where the second resource information is a weighted average value of resource information detected in a historical detection period.
[0133] In some embodiments, this step is the same as step 701 and will not be repeated here.
[0134] Step 802: Perform a weighted average on the second resource information and the first resource information detected in the current detection period to obtain third resource information.
[0135] In some embodiments, this step is the same as step 702 and will not be repeated here.
[0136] Step 803: Obtain a first resource distribution parameter, where the first resource distribution parameter is a weighted average value of distribution parameters of resource information detected in a historical detection period.
[0137] The leader node stores a weighted average of distribution parameters of resource information detected in historical detection cycles; in this step, the leader node obtains the stored first resource distribution parameter. In some embodiments, when the node cluster is initialized, the first resource distribution parameter is an initial resource distribution parameter.
[0138] Step 804: Determine a second resource distribution parameter of the current detection cycle based on the third resource information, the first resource information and the first resource distribution parameter.
[0139] The leading node determines the third resource distribution parameter based on the first resource information and the third resource information, and performs weighted summation on the third resource distribution parameter and the first resource distribution parameter to obtain the second resource distribution parameter. For example, the leading node can determine the second resource distribution parameter using the following formula 2.
[0140] Formula 2:
[0141] Among them, s t represents the second resource distribution parameter, s t-1 represents the first resource distribution parameter, y t Indicates the third resource information, x t represents the first resource information, α represents the smoothing coefficient, and the value range of α is 0-1.
[0142] Step 805: Based on the second resource distribution parameter and the third resource information, determine a resource information range, where the resource information range is used to indicate a resource information range that does not require adjustment of the detection period.
[0143] The leader node determines the product of the confidence coefficient and the second resource distribution parameter to obtain a second offset, and shifts the third resource information distribution to both ends by the second offset to obtain a resource information range. For example, the leader node determines the resource information range by the following formula three.
[0144] Formula 3: l t =[y t -ks t ,y t +ks t ]
[0145] Among them, l t Indicates the resource information range, k indicates the confidence coefficient, and the value of k is 2 or 3, s trepresents the second resource distribution parameter, y t Indicates third resource information.
[0146] Step 806: Based on the first resource information and the resource information range, update the first detection period.
[0147] When the first resource information is not located in the resource information range, the first offset of the first resource information is determined based on the first resource information and the resource information range; when the first offset is greater than the first preset offset, the first detection period is reduced; when the first offset is not greater than the first preset offset, the first detection period is increased. When the first resource information is located in the resource information range, the first detection period is kept unchanged.
[0148] In some embodiments, the leading node may also determine a second offset based on the first resource information and the predicted resource information; reduce the first detection period if the second offset is greater than a second preset offset; and increase the first detection period if the second offset is not greater than the second preset offset. The predicted resource period may be a previous detection period; or the predicted detection period may be a detection period predicted by a neural network.
[0149] In some embodiments, if x in a certain detection cycle t Outside the confidence interval l t , it is considered that the resource usage has changed abnormally during the detection cycle. If the deviation between the actual value and the predicted value is large, the detection cycle is shortened to respond to the abnormal change in time. On the contrary, if the deviation between the actual value and the predicted value is small, the detection cycle is extended to reduce the impact of the detection on system performance.
[0150] Please refer to Fig. 9 , which shows a flow chart of a resource detection method shown in an exemplary embodiment of the present application. The method is applied in a node cluster, and the node cluster includes multiple nodes running services; the method includes:
[0151] Step 901: when the node cluster is initialized, based on the roles of the multiple nodes in the node cluster, the preset sleep durations of the multiple nodes in the node cluster are controlled, and the preset sleep durations of the nodes with different roles are different.
[0152] The execution function of this application is the loop function, which executes the main logic of the program. This function is called when the main function completes the initialization of the program parameters. The loop function first calls time.Sleep() to pre-sleep, waiting for each node program to start and the rpc server to start working. Then when node.looping is true, it continuously loops to check its own status and performs heartbeat sending, information capture, voting, etc. according to its own status.
[0153] When electing a leader node, the nodes in the node cluster need to vote, and there may be a tie, that is, the leader node cannot be elected; in the embodiment of the present application, by making the nodes of different roles sleep for different preset time lengths, it is ensured that these nodes do not end their sleep at the same time, and different nodes are used each time the leader node is elected, thus avoiding the situation where the leader node cannot be elected. For example, the follower node sleeps for 700ms, the candidate node sleeps for 100ms, and the leader node sleeps for 300ms.
[0154] Step 902: When any node ends its hibernation and the detection program of the leader node in the node cluster is in operation, a corresponding operation is performed according to the role of the node in the node cluster.
[0155] For example, see Fig.10 , for any node in the node cluster, determine whether the detection program corresponding to the node cluster (whether node.looping is true) is running normally; if the detection program is running normally, determine whether it is a follower node (Follower); if it is a follower node, set the heartbeat sending state of the leader node to timeout (set node.eleTimer to true), and then sleep for the third preset time (700ms); when the sleep ends, determine whether it is a follower node, and if it is a follower node, determine whether the heartbeat sending state of the leader node is timed out; if the heartbeat sending state of the leader node is timed out, according to its own state, convert itself to a candidate node. When the sleep ends, if it is determined that it is not a follower node, determine whether it is a candidate node; if it is a candidate node, start to elect a leader node; if it is not a candidate node, send a heartbeat request response body to other nodes.
[0156] In some embodiments, when the detection program runs normally, it determines whether it is a follower node (Follower); when it is not a follower node, it determines whether it is a candidate node. When it is a candidate node, it sleeps for a fourth preset time length (100ms). When it is not a candidate node, it sleeps for a fifth preset time length (300ms). Then, when the sleep ends, it determines whether it is a follower node. This process is the same as the above process and will not be repeated again.
[0157] When electing a leader node, the nodes in the node cluster are required to vote, and a tie may occur, that is, the leader node cannot be elected. In the embodiment of the present application, by making nodes with different roles sleep for different preset time lengths, it is ensured that these nodes do not end their sleep at the same time, and different nodes are used each time a leader node is elected, thus avoiding the situation where the leader node cannot be elected.
[0158] Please refer to Fig.11 , which shows a block diagram of a resource detection device shown in an exemplary embodiment of the present application, the device is applied in a node cluster, and the node cluster includes multiple nodes running services; the device includes:
[0159] The first acquisition module 1101 is used to acquire resource information detected by multiple candidate nodes in the node cluster when the node cluster triggers the election instruction of the leader node;
[0160] A first determination module 1102 is used to determine first voting results of other nodes in the node cluster on the multiple candidate nodes based on resource information detected by the multiple candidate nodes, where the first voting results are used to represent the voting ranking of the multiple candidate nodes;
[0161] A second determination module 1103, configured to determine a leader node and a follower node in the node cluster based on the first voting result;
[0162] The third determination module 1104 is configured to obtain, through the leader node, resource information detected by the follower nodes based on the first detection period, and determine first resource information of the node cluster.
[0163] In some embodiments, the first determination module 1102 is used to add the resource information detected by any candidate node to a voting request structure, send a voting request structure to other nodes in the node cluster, and receive a first voting response structure returned by the other nodes, the first voting response structure including a second voting result of other nodes on the candidate node based on the voting request structure, the second voting result being used to indicate whether the candidate node is agreed to be elected as the leading node; based on the second voting results of other nodes on multiple candidate nodes, the multiple candidate nodes are sorted to obtain a first voting result.
[0164] In some embodiments, the first determination module 1102 is used to create a second voting response structure; establish a communication connection with other nodes based on the node identifiers of other nodes; send a first call request to other nodes through the communication connection, the first call request carries a voting request structure and a second voting response structure, and the first call request is used to call the voting request processing function of other nodes to obtain the second voting results of other nodes on the candidate nodes; receive the first voting response structure returned by other nodes, the first voting response structure is obtained by adding the second voting result to the second voting response structure.
[0165] In some embodiments, the third determination module 1104 is configured to perform the following steps based on the first detection cycle through the leader node:
[0166] Add resource parameters to the heartbeat request structure, send the heartbeat request structure to the follower node, receive the first heartbeat response structure returned by the follower node, the first heartbeat response structure includes resource information detected by the follower node based on the resource parameters; the resource information detected by the leading node and the resource information detected by the follower node are combined into the first resource information of the node cluster.
[0167] In some embodiments, the third determination module 1104 is used to create a second heartbeat response structure; establish a communication connection with the follower node based on the node identifier of the follower node; send a second call request to the follower node through the communication connection, the second call request carries a heartbeat request structure and a second heartbeat response structure, and the second call request is used to call the heartbeat request processing function of the follower node to generate a first heartbeat response structure; receive the first heartbeat response structure returned by the follower node, the first heartbeat response structure is obtained by adding the resource information detected by the follower node to the first heartbeat response structure.
[0168] In some embodiments, the third determination module 1104 is used to set the heartbeat sending status of the leading node to not timed out; when the heartbeat sending of the leading node has not timed out, obtain resource information corresponding to the resource parameters, add the obtained resource information to the second heartbeat response structure, and set the resource acquisition success flag bit in the second heartbeat response structure to a first value to obtain a first heartbeat response structure, and the first value is used to indicate that the resource information is successfully obtained.
[0169] In some embodiments, the apparatus further comprises:
[0170] A second acquisition module is used to acquire second resource information, where the second resource information is a weighted average value of resource information detected in a historical detection period;
[0171] A weighting module, used to perform weighted averaging on the second resource information and the first resource information detected in the current detection period to obtain third resource information;
[0172] The updating module is used to update the first detection period based on the third resource information and the first resource information.
[0173] In some embodiments, the update module is used to obtain a first resource distribution parameter, which is a weighted average of distribution parameters of resource information detected in historical detection cycles; determine a second resource distribution parameter of a current detection cycle based on third resource information, the first resource information, and the first resource distribution parameter; determine a resource information range based on the second resource distribution parameter and the third resource information, the resource information range being used to indicate a resource information range that does not require adjustment of the detection cycle; and update the first detection cycle based on the first resource information and the resource information range.
[0174] In some embodiments, the update module is used to determine the first offset of the first resource information based on the first resource information and the resource information range when the first resource information is not located within the resource information range; to reduce the first detection period when the first offset is greater than a preset offset; and to increase the first detection period when the first offset is not greater than a preset offset.
[0175] In some embodiments, the updating module is used to determine a third resource distribution parameter based on the first resource information and the third resource information; and perform weighted summation on the third resource distribution parameter and the first resource distribution parameter to obtain a second resource distribution parameter.
[0176] In some embodiments, the updating module is used to determine the product of the confidence coefficient and the second resource distribution parameter to obtain a second offset; and to shift the third resource information distribution to both ends by the second offset to obtain a resource information range.
[0177] In some embodiments, the apparatus further comprises:
[0178] The change module is used to change the role of the node in the node cluster to a follower node before the node cluster triggers the election instruction; when the node cluster triggers the election instruction, the role of the node in the node cluster is changed from a follower node to a candidate node.
[0179] In some embodiments, the apparatus further comprises:
[0180] A sleep module is used to control the preset sleep duration of multiple nodes in the node cluster based on the roles of the multiple nodes in the node cluster when the node cluster is initialized, and the preset sleep duration of nodes with different roles is different;
[0181] The execution module is used to execute corresponding operations according to the role of the node in the node cluster when any node ends its sleep state and the detection program of the leader node in the node cluster is in operation.
[0182] In some embodiments, the apparatus further comprises a fourth determining module;
[0183] The fourth determination module is used to perform the following steps:
[0184] When the node cluster is initialized, determine the node cluster triggers the election instruction of the leader node;
[0185] When the previous leader node in the node cluster goes offline, the node cluster is determined to trigger the election instruction of the leader node;
[0186] When the previous leader node in the node cluster does not meet the conditions for taking on the role of the leader node, determine that the node cluster triggers the election instruction of the leader node;
[0187] When the previous leader node in the node cluster does not obtain the resource information detected by the follower node but obtains an error indication, it is determined that the node cluster triggers the election instruction of the leader node;
[0188] When the follower node in the node cluster does not receive the heartbeat information of the previous leader node, it is determined that the node cluster triggers the election instruction of the leader node.
[0189] In some embodiments, the apparatus further comprises:
[0190] The recording module is used to obtain the error information of any node when an error occurs in the operation of any node in the node cluster, and record the error information in the log corresponding to the node.
[0191] In some embodiments, the first determination module 1102 is used to encrypt the voting request structure, send the encrypted voting request structure to other nodes in the node cluster; receive the encrypted first voting response structure returned by other nodes; and decrypt the encrypted first voting response structure to obtain the first voting response structure.
[0192] In an embodiment of the present application, when an election instruction is triggered, the election of a leader node is performed by voting on the resource information detected by the candidate nodes, thereby eliminating the need for user participation, adaptively triggering the election instruction, and adaptively electing a leader node. In this way, even if the previous leader node leaves or is not suitable to continue to serve as the leader node due to problems such as insufficient power, a new leader node can be quickly elected, thereby enabling the node cluster to work normally and improving the reliability of the node cluster.
[0193] It should be noted that the resource detection device provided in the above embodiment only uses the division of the above functional modules as an example when performing resource detection. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the terminal is divided into different functional modules to complete all or part of the functions described above. In addition, the resource detection device provided in the above embodiment and the resource detection method embodiment belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0194] Please refer to Fig.12 , which shows a block diagram of a node 1200 according to an exemplary embodiment of the present application. The node 1200 in the present application includes one or more of the following components: a processor 1210 , a memory 1220 , and a display screen 1230 .
[0195] The processor 1210 may include one or more processing cores. The processor 1210 uses various interfaces and lines to connect the various parts of the entire node 1200, and executes various functions and processes data of the node 1200 by running or executing instructions, programs, code sets or instruction sets stored in the memory 1220, and calling data stored in the memory 1220. Optionally, the processor 1210 can be implemented in at least one hardware form of digital signal processing (DSP), field programmable gate array (FPGA), and programmable logic array (PLA). The processor 1210 can integrate one or more combinations of a central processing unit (CPU), a graphics processing unit (GPU), a neural network processor (NPU), and a modem. Among them, the CPU mainly processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing the content that needs to be displayed on the display screen 1230; the NPU is used to implement artificial intelligence (AI) functions; and the modem is used to process wireless communications. It is understandable that the above-mentioned modem may not be integrated into the processor 1210, but may be implemented by a separate chip.
[0196] The memory 1220 may include a random access memory (RAM) or a read-only memory (ROM). Optionally, the memory 1220 includes a non-transitory computer-readable storage medium. The memory 1220 may be used to store instructions, programs, codes, code sets, or instruction sets. The memory 1220 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the following various method embodiments, etc.; the data storage area may store data (such as audio data, a phone book), etc., created according to the use of the node 1200.
[0197] The display screen 1230 is a display component for displaying a user interface. Optionally, the display screen 1230 is a display screen with a touch function, through which the user can use any suitable object such as a finger, a touch pen, etc. to perform touch operations on the display screen 1230.
[0198] The display screen 1230 is usually arranged on the front panel of the node 1200. The display screen 1230 can be designed as a full screen, a curved screen, a special-shaped screen, a double-sided screen or a folding screen. The display screen 1230 can also be designed as a combination of a full screen and a curved screen, a combination of a special-shaped screen and a curved screen, etc., which is not limited in this embodiment.
[0199] In addition, those skilled in the art can understand that the structure of the node 1200 shown in the above figures does not constitute a limitation on the node 1200, and the node 1200 may include more or fewer components than shown, or combine certain components, or arrange the components differently. For example, the node 1200 also includes an audio acquisition device, a speaker, a radio frequency circuit, an input unit, a sensor, an audio circuit, a wireless fidelity (Wireless Fidelity, Wi-Fi) module, a power supply, a Bluetooth module and other components, which will not be repeated here.
[0200] An embodiment of the present application further provides a computer-readable medium, which stores at least one program code, and the at least one program code is loaded and executed by the processor to implement the resource detection method shown in the above embodiments.
[0201] An embodiment of the present application further provides a computer program product, which stores at least one program code, and the at least one program code is loaded and executed by the processor to implement the resource detection method shown in the above embodiments.
[0202] In some embodiments, the computer program product involved in the embodiments of the present application may be deployed and executed on one node, or on multiple nodes located at one location, or on multiple nodes distributed at multiple locations and interconnected by a communication network. Multiple nodes distributed at multiple locations and interconnected by a communication network may constitute a blockchain system.
[0203] Those skilled in the art should be aware that in one or more of the above examples, the functions described in the embodiments of the present application can be implemented with hardware, software, firmware, or any combination thereof. When implemented using software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or codes on a computer-readable medium. Computer-readable media include computer storage media and communication media, wherein the communication media include any media that facilitates the transmission of a computer program from one place to another. The storage medium can be any available medium that a general or special-purpose computer can access.
[0204] The above description is only an optional embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
Claims
1. A resource detection method, characterized in that: The method is applied in a node cluster, wherein the node cluster includes a plurality of nodes running a service; the method includes: When the node cluster triggers an election instruction of a leader node, obtaining resource information detected by multiple candidate nodes in the node cluster; Determine, based on the resource information detected by the multiple candidate nodes, first voting results of other nodes in the node cluster on the multiple candidate nodes, where the first voting results are used to represent the voting ranking of the multiple candidate nodes; Based on the first voting result, determining a leader node and a follower node in the node cluster; The leader node obtains resource information detected by the follower node based on a first detection period to determine first resource information of the node cluster.
2. The method according to claim 1, characterized in that The determining, based on the resource information detected by the multiple candidate nodes, first voting results of other nodes in the node cluster for the multiple candidate nodes includes: For any candidate node, adding the resource information detected by the candidate node to a voting request structure, sending the voting request structure to other nodes in the node cluster, and receiving a first voting response structure returned by the other nodes, wherein the first voting response structure includes a second voting result of the other nodes on the candidate node based on the voting request structure, and the second voting result is used to indicate whether the candidate node is approved to be elected as a leader node; Based on the second voting results of the other nodes on the multiple candidate nodes, the multiple candidate nodes are sorted to obtain the first voting result.
3. The method according to claim 2, characterized in that The sending the voting request structure to other nodes in the node cluster and receiving the first voting response structure returned by the other nodes includes: Create the second voting response structure; Establishing a communication connection with the other node based on the node identifier of the other node; Sending a first call request to the other node through the communication connection, where the first call request carries the voting request structure and the second voting response structure, and the first call request is used to call the voting request processing function of the other node to obtain the second voting result of the other node on the candidate node; The first voting response structure returned by the other node is received, where the first voting response structure is obtained by adding the second voting result to the second voting response structure.
4. The method according to claim 1, characterized in that: The obtaining, through the leader node, resource information detected by the follower node based on a first detection period, and determining first resource information of the node cluster includes: The leader node performs the following steps based on the first detection cycle: Add resource parameters to a heartbeat request structure, send the heartbeat request structure to the follower node, and receive a first heartbeat response structure returned by the follower node, wherein the first heartbeat response structure includes resource information detected by the follower node based on the resource parameters; The resource information detected by the leader node and the resource information detected by the follower node are combined into the first resource information of the node cluster.
5. The method according to claim 4, characterized in that The step of sending the heartbeat request structure to the follower node and receiving the first heartbeat response structure returned by the follower node includes: Create the second heartbeat response structure; Establishing a communication connection with the follower node based on the node identifier of the follower node; Sending a second call request to the follower node through the communication connection, wherein the second call request carries the heartbeat request structure and the second heartbeat response structure, and the second call request is used to call the heartbeat request processing function of the follower node to generate the first heartbeat response structure; The first heartbeat response structure returned by the follower node is received, where the first heartbeat response structure is obtained by adding resource information detected by the follower node to the first heartbeat response structure.
6. The method according to claim 5, characterized in that The process of the follower node generating the first heartbeat response structure based on the heartbeat request processing function includes: Setting the heartbeat sending state of the leader node to not time out; When the heartbeat sending of the leading node has not timed out, the resource information corresponding to the resource parameter is obtained, the obtained resource information is added to the second heartbeat response structure, and the resource acquisition success flag bit in the second heartbeat response structure is set to a first value to obtain the first heartbeat response structure, where the first value is used to indicate that the resource information has been successfully acquired.
7. The method according to claim 1, characterized in that The method further comprises: Acquire second resource information, where the second resource information is a weighted average value of resource information detected in a historical detection period; Taking a weighted average of the second resource information and the first resource information detected in the current detection period to obtain third resource information; The first detection period is updated based on the third resource information and the first resource information.
8. The method according to claim 7, characterized in that The updating of the first detection period based on the third resource information and the first resource information includes: Acquire a first resource distribution parameter, where the first resource distribution parameter is a weighted average value of distribution parameters of resource information detected in a historical detection period; Determine a second resource distribution parameter of the current detection period based on the third resource information, the first resource information and the first resource distribution parameter; Determine a resource information range based on the second resource distribution parameter and the third resource information, where the resource information range is used to indicate a resource information range for which a detection period does not need to be adjusted; The first detection period is updated based on the first resource information and the resource information range.
9. The method according to claim 8, characterized in that The updating of the first detection period based on the first resource information and the resource information range includes: In a case where the first resource information is not located within the resource information range, determining a first offset of the first resource information based on the first resource information and the resource information range; When the first deviation is greater than a preset deviation, reducing the first detection period; When the first deviation is not greater than the preset deviation, the first detection period is increased.
10. The method according to claim 8, characterized in that The determining, based on the third resource information, the first resource information and the first resource distribution parameter, the second resource distribution parameter of the current detection period includes: Determining a third resource distribution parameter based on the first resource information and the third resource information; A weighted sum is performed on the third resource distribution parameter and the first resource distribution parameter to obtain the second resource distribution parameter.
11. The method according to claim 8, characterized in that The determining the resource information range based on the second resource distribution parameter and the third resource information includes: Determine a product of a confidence coefficient and the second resource distribution parameter to obtain a second offset; The third resource information distribution is shifted toward both ends by the second shift degree to obtain the resource information range.
12. The method according to claim 1, characterized in that The method further comprises: Before the node cluster triggers the election instruction, the role of the node in the node cluster is a follower node; When the node cluster triggers the election instruction, the role of the node in the node cluster is changed from a follower node to a candidate node.
13. The method according to claim 1, characterized in that The method further comprises: When the node cluster is initialized, based on the roles of the multiple nodes in the node cluster, the multiple nodes in the node cluster are controlled to sleep for a preset time, and the preset sleep time of nodes with different roles is different; When any node ends its hibernation and the detection program of the leader node in the node cluster is in operation, a corresponding operation is performed according to the role of the node in the node cluster.
14. The method according to claim 1, characterized in that The method further comprises at least one of the following: When the node cluster is initialized, determining that the node cluster triggers an election instruction of a leader node; When a previous leader node in the node cluster is offline, determining that the node cluster triggers an election instruction for a leader node; When the last leader node in the node cluster does not meet the conditions for taking on the role of a leader node, determining that the node cluster triggers an election instruction for a leader node; When a previous leader node in the node cluster fails to obtain resource information detected by the follower node but obtains an error indication, determining that the node cluster triggers an election instruction for a leader node; When the follower node in the node cluster does not receive the heartbeat information of the previous leader node, it is determined that the node cluster triggers an election instruction of the leader node.
15. The method according to claim 1, characterized in that The method further comprises: When any node in the node cluster fails to run, error information of the node is obtained and the error information is recorded in a log corresponding to the node.
16. The method according to claim 2, characterized in that The sending the voting request structure to other nodes in the node cluster and receiving the first voting response structure returned by the other nodes includes: Encrypting the voting request structure, and sending the encrypted voting request structure to other nodes in the node cluster; Receiving the encrypted first voting response structure returned by the other node; The encrypted first voting response structure is decrypted to obtain the first voting response structure.
17. A resource detection device, characterized in that: The device is applied in a node cluster, and the node cluster includes a plurality of nodes running a service; the device includes: A first acquisition module, configured to acquire resource information detected by multiple candidate nodes in the node cluster when the node cluster triggers an election instruction of a leader node; A first determination module, configured to determine first voting results of other nodes in the node cluster on the multiple candidate nodes based on resource information detected by the multiple candidate nodes, wherein the first voting results are used to represent a voting ranking of the multiple candidate nodes; A second determination module, configured to determine a leader node and a follower node in the node cluster based on the first voting result; The third determination module is used to obtain the resource information detected by the follower node based on the first detection period through the leader node to determine the first resource information of the node cluster.
18. A node, characterized in that: The node includes one or more processors and one or more memories, wherein at least one program code is stored in the one or more memories, and the at least one program code is loaded and executed by the one or more processors to implement the resource detection method as described in any one of claims 1 to 16.
19. A computer-readable storage medium, characterized in that: The storage medium stores at least one program code, and the at least one program code is loaded and executed by the processor to implement the resource detection method according to any one of claims 1 to 16.
20. A computer program product, characterized in that The computer program product stores at least one program code, and the at least one program code is used to be executed by a processor to implement the resource detection method according to any one of claims 1 to 16.