A distributed task scheduling method, system and storage medium based on ZooKeeper
By creating temporary nodes in the ZooKeeper cluster and utilizing the Watcher mechanism, the problems of repeated tasks triggering and load balancing in the XXL-JOB scheduling center cluster are solved, and high availability and resource optimization of distributed task scheduling are achieved.
Patent Information
- Application Number
- CN202011225767.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-05
- Publication Date
- 2025-01-03
- Estimated Expiration
- 2040-11-05
AI Technical Summary
In XXL-JOB's distributed task scheduling system, the deployment of the scheduling center cluster may cause repeated tasks to be triggered, and the load balancing strategy is lacking, affecting the high availability and efficiency of the system.
By creating temporary nodes in the ZooKeeper cluster, each node corresponds to a node number. The dispatching center server uses the node number to select work nodes to trigger tasks, and uses Watcher to monitor node changes to achieve high availability and load balancing.
Ensure that tasks will be triggered only once, avoid repeated triggering, improve the high availability of distributed task scheduling, and optimize resource utilization of the scheduling center through load balancing strategies.
Smart Images

Figure CN112433830B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a distributed task scheduling method, system and storage medium based on ZooKeeper. Background Art
[0002] Both Internet applications and enterprise applications are filled with a large number of batch processing tasks. The operation of these tasks has a certain periodicity, and often requires some task scheduling systems to solve the problem; with the gradual evolution of microservice architecture, the monolithic architecture has gradually evolved into a distributed, microservice architecture. In this context, many of the original task scheduling platforms can no longer meet the needs of business systems, so some distributed-based task scheduling frameworks have emerged, such as LTS (LightTask Schedule, lightweight distributed task scheduling framework), XXL-JOB and Elastic-Job; the distributed framework needs to ensure high availability, and when a scheduling machine fails, the task scheduling trigger can still be performed normally.
[0003] XXL-JOB uses a database (DB) lock to trigger tasks once when multiple scheduling centers are deployed. XXL-JOB separates the scheduling center from the executor. The scheduling center is responsible for scheduling the triggered tasks, and the triggered tasks will be sent to the executor. The executor is responsible for running the tasks, and the executor supports cluster deployment. However, the current solution based on XXL-JOB has the following problems: when the scheduling center is a cluster, repeated task triggering may occur, and there is no load balancing strategy for the cluster deployment of the scheduling center. Summary of the invention
[0004] The present application provides a distributed task scheduling method, system and storage medium based on ZooKeeper, which can improve the high availability of distributed task scheduling.
[0005] To solve the above technical problems, the technical solution adopted in the present application is: to provide a distributed task scheduling method based on ZooKeeper, the method comprising: using a scheduling center server to create a temporary node to a ZooKeeper cluster, wherein each temporary node corresponds to a node serial number; using the scheduling center server to conduct an election based on the node serial number of the temporary node to select a temporary node from all temporary nodes to trigger the task.
[0006] To solve the above technical problems, another technical solution adopted in the present application is: to provide a distributed task scheduling system, which includes a memory and a processor connected to each other, wherein the memory is used to store a computer program, and when the computer program is executed by the processor, it is used to implement the above-mentioned ZooKeeper-based distributed task scheduling method.
[0007] To solve the above technical problems, another technical solution adopted in the present application is: to provide a computer-readable storage medium, which is used to store a computer program. When the computer program is executed by a processor, it is used to implement the above-mentioned ZooKeeper-based distributed task scheduling method.
[0008] Through the above scheme, the beneficial effect of the present application is: the scheduling center server can send a request to the ZooKeeper cluster to create ordered temporary nodes in the ZooKeeper cluster, and each temporary node corresponds to a node serial number; then the scheduling center server can use the node serial number to select a temporary node from all temporary nodes to execute the task that needs to be triggered currently; since the node serial number corresponding to each temporary node is different, only one temporary node is selected from all temporary nodes to trigger the task that needs to be triggered currently, which can ensure that a task scheduling will only trigger one execution, and the task will not be triggered repeatedly, which can effectively improve the high availability of distributed task scheduling. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. Among them:
[0010] Figure 1 It is a flow chart of an embodiment of a distributed task scheduling method based on ZooKeeper provided by the present application;
[0011] Figure 2 yes Figure 1 A schematic diagram of the connection between the ZooKeeper cluster and the scheduling center server in the embodiment shown;
[0012] Figure 3 It is a flow chart of another embodiment of the distributed task scheduling method based on ZooKeeper provided by the present application;
[0013] Figure 4 yes Figure 3 A schematic diagram of the flow chart of step 32 in the embodiment shown;
[0014] Figure 5 yes Figure 1 A schematic diagram of the connection between the ZooKeeper cluster and the executor in the embodiment shown;
[0015] Figure 6It is a structural diagram of an embodiment of a distributed task scheduling system provided by the present application;
[0016] Figure 7 It is a structural schematic diagram of an embodiment of a computer-readable storage medium provided by the present application. DETAILED DESCRIPTION
[0017] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0018] See also Figure 1 , Figure 1 : is a flow chart of an embodiment of a distributed task scheduling method based on ZooKeeper provided by the present application, the method comprising:
[0019] Step 11: Use the scheduling center server to create a temporary node in the ZooKeeper cluster.
[0020] ZooKeeper is a software that provides consistency services for distributed applications. Its functions include configuration maintenance, domain name services, distributed synchronization or group services, etc. ZooKeeper cluster configuration information can be added to the configuration files of all scheduling center servers. The ZooKeeper cluster configuration information includes the address of the ZooKeeper cluster and the port of the ZooKeeper cluster. For example, the ZooKeeper cluster configuration information ZooKeeperAddress=192.168.0.1:2181, 192.168.0.2:2888, where "192.168.0.1" is the address of the ZooKeeper cluster, "2181" is the port corresponding to the address, "192.168.0.2" is the address of the ZooKeeper cluster, and "2888" is the port corresponding to the address.
[0021] After the scheduling center server is started normally, the scheduling center server can register services with the ZooKeeper cluster. Specifically, Figure 2As shown, each scheduling center server 22 can create ordered temporary nodes under the target directory in the ZooKeeper cluster 21, and each temporary node corresponds to a node serial number, such as: "00000", "00001" or "00002", etc. The scheduling center server 22 can write the address and port of its own service into the temporary node information, that is, the temporary node information includes the node serial number of the temporary node, and the address and port of the scheduling center server served by the temporary node.
[0022] Step 12: The scheduling center server performs an election based on the node sequence number of the temporary node to select a temporary node from all temporary nodes to trigger the task.
[0023] After each scheduling center server successfully registers with the ZooKeeper cluster, an election operation can be triggered, that is, a temporary node is selected from all temporary nodes to execute the task that needs to be triggered currently; since the node sequence number corresponding to each temporary node is different, only one temporary node is selected from all temporary nodes to trigger the task, which can ensure that the task is only triggered once when multiple scheduling centers are deployed, and the task will not be triggered repeatedly, which can effectively improve the high availability of distributed task scheduling.
[0024] See also Figure 3 , Figure 3 : is a flow chart of another embodiment of a distributed task scheduling method based on ZooKeeper provided by the present application, the method comprising:
[0025] Step 31: Use the scheduling center server to create a temporary node in the ZooKeeper cluster.
[0026] This step is the same as step 11 in the above embodiment and will not be described again.
[0027] Step 32: Classify all temporary nodes using their node numbers to obtain working nodes and non-working nodes, and select working nodes to trigger the task.
[0028] In order to select a temporary node from all temporary nodes to execute the trigger task, such as Figure 4 As shown, the following steps can be performed:
[0029] Step 321: Obtain the node serial numbers of all temporary nodes at the current moment.
[0030] After the scheduling center server successfully registers the temporary ordered node on the ZooKeeper cluster, it can obtain the node serial number of its own registration, such as "00000"; then obtain the node serial number set of all temporary nodes in the target directory in the ZooKeeper cluster at the current moment, such as "00000", "00001" or "00002".
[0031] Step 322: Determine whether the node serial number of the temporary node corresponding to the current scheduling center server meets the preset conditions.
[0032] The preset condition is a pre-set selection rule. For example, it can determine whether the node serial number of the temporary node corresponding to the current scheduling center server is the minimum / maximum value among the node serial numbers of all temporary nodes. It can be understood that other methods can also be set to select the temporary node that executes the trigger task, for example, the first registered or last registered temporary node can be selected, or a temporary node with an intermediate node serial number can be selected.
[0033] Step 323: If the node serial number of the temporary node corresponding to the current scheduling center server meets the preset condition, the temporary node corresponding to the current scheduling center server is the working node.
[0034] After the current scheduling center server determines that its own node serial number is the minimum or maximum value among all node serial numbers, it can determine that the temporary node corresponding to itself is a worker node.
[0035] Step 324: If the node serial number of the temporary node corresponding to the current scheduling center server does not meet the preset condition, the temporary node corresponding to the current scheduling center server is a non-working node.
[0036] If the current scheduling center server determines that its own node serial number is neither the minimum value nor the maximum value among all node serial numbers, it can be determined that the temporary node corresponding to itself is a non-Worker node.
[0037] Step 33: Use the working node to start the thread, query the set of tasks to be triggered, and use the working node to trigger the tasks to be triggered.
[0038] After determining the Worker node, the Worker node can start the scheduling thread and query the task set to be triggered. If there is a task to be triggered in the task set, that is, the task set includes at least one task to be triggered; then the task to be triggered is called asynchronously, and the cycle is repeated until all the tasks to be triggered in the task set are triggered; if the task to be triggered is not queried, no processing is performed; further, the triggering time of the task can be configured manually. For example, assuming that the scheduling thread is trained once every 5 seconds, it is found that there is no task to be triggered at 14:00:00, and it is found that there are 3 tasks to be triggered at 14:00:05. At this time, each task can be triggered in turn.
[0039] Step 34: Use the current non-working node to select a temporary node from other temporary nodes as a monitoring registration node, and use the current non-working node to register a monitor at the monitoring registration node to monitor the node deletion event of the current non-working node.
[0040] The current non-Worker node is used to select the temporary node with the largest node number from the temporary nodes whose node numbers are smaller than the node number of the current non-Worker node as the monitoring registration node, that is, the non-Worker node can select the temporary node with the largest node number from the temporary nodes with smaller node numbers than its own to register a one-time listener (Watcher) and subscribe to the node deletion event of the non-Worker node; for example, assuming that the node number of the current non-Worker node is "00005", there are four temporary nodes with node numbers "00001", "00002", "00003" and "00004" in the ZooKeeper cluster, then the Watcher is registered in the temporary node with node number "00004".
[0041] Step 35: When an exception occurs in a working node, ZooKeeper deletes the working node, and the scheduling center server that monitors the working node receives the node deletion event.
[0042] When an exception occurs in a Worker node, ZooKeeper can delete the Worker node. At the same time, the scheduling center server that registers the Watcher on the Worker node can receive the node deletion notification and become a new Worker node, thereby achieving high availability of the scheduling center. For example, when an exception occurs in the scheduling center server corresponding to the temporary node with the node serial number "00000", the temporary node with the node serial number "00000" in the ZooKeeper cluster is deleted, and the scheduling center server that monitors the temporary node can receive the node deletion notification and restart the election process, that is, return to step 32.
[0043] Step 36: Use the executor to obtain the dispatch center cluster address list, and use the dispatch center cluster address list to perform load balancing, thereby selecting a dispatch center server from all dispatch center servers to execute the task callback.
[0044] ZooKeeper cluster configuration information can be added to the configuration file of each executor, and the ZooKeeper cluster configuration information includes the address and port of the ZooKeeper cluster; the executor is used to obtain temporary node information corresponding to all temporary nodes from the ZooKeeper cluster, and the temporary node information includes the node serial number of the temporary node and the address and port of the scheduling center server corresponding to the temporary node; the executor is then used to monitor each temporary node in the target directory, so that the ZooKeeper cluster sends a notification to the executor after deleting / adding a temporary node; after the executor receives the notification sent by the ZooKeeper cluster, the executor can be used to update the scheduling center cluster address list; specifically, the scheduling center cluster address list includes all temporary node information, if the executor receives a notification that a temporary node is deleted, the information related to the temporary node in the scheduling center cluster address list is deleted; if the executor receives a notification to add a temporary node, the information related to the temporary node is added to the scheduling center cluster address list.
[0045] For example, the dispatch center cluster address list is shown in the following table:
[0046] Temporary Node address port A1 IP1 C1 A2 IP2 C2 A3 IP3 C1
[0047] Temporary node A1 corresponds to address IP1 and port C1, temporary node A2 corresponds to address IP2 and port C2, and temporary node A3 corresponds to address IP3 and port C3. After temporary node A2 is deleted, the executor can update the address list of the dispatch center cluster to obtain the following list:
[0048] Temporary Node address port A1 IP1 C1 A3 IP3 C1
[0049] In a specific embodiment, Figure 5As shown, the executor 51 has a built-in address management module 511, and the address management module 511 can maintain the scheduling center cluster address list in real time and dynamically by monitoring the target directory in the ZooKeeper cluster 52; specifically, when the address management module 511 is initialized, it can obtain all temporary node information under the target directory from the ZooKeeper cluster 52 and cache it locally, and can monitor the node change events under the target directory, that is, monitor whether the temporary node is deleted or whether a new temporary node is added. When the temporary node changes, the ZooKeeper cluster 52 can push notifications to the address management module 511; after receiving the notification sent by the ZooKeeper cluster 52, the address management module 511 dynamically adds or deletes the information in the scheduling center cluster address list.
[0050] The executor 51 can obtain the dispatch center cluster address list from the address management module 511, select a unique address and port according to the task identification (ID) through a load balancing algorithm (such as round-robin or hash), and connect to the dispatch center server corresponding to the address and port to execute the callback content.
[0051] In a specific implementation, the dispatch center cluster address list is an ordered list, and the dispatch center server corresponding to the first temporary node information can be selected from the dispatch center cluster address list to execute the current callback task, and the first temporary node information is moved to the end of the dispatch center cluster address list.
[0052] For example, suppose there are three dispatch center addresses, forming an ordered list {IP1, IP2, IP3}. When callback task A, the dispatch center server corresponding to the address at the head of the list (i.e. IP1) is selected to execute the callback task, and then the address IP1 is moved to the end of the list. At this time, the list becomes: {IP2, IP3, IP1}; when callback task B, the dispatch center server corresponding to the address at the head of the list (i.e. IP2) is selected to execute the callback task, and the address IP2 is moved to the end of the list. At this time, the list is updated to {IP3, IP1, IP2}; and so on. After selecting the dispatch center server currently used to execute the callback, its corresponding address is moved to the end of the list to ensure that each dispatch center server can be reasonably used to achieve load balancing.
[0053] In another specific implementation, the identifier of the current callback task is divided by the number of scheduling center servers to obtain a remainder; the remainder is matched with the scheduling center server corresponding to the temporary node information; and the scheduling center server matching the remainder is selected to execute the current callback task.
[0054] For example, the task ID is a positive integer. The task ID is divided by the number of addresses in the dispatch center cluster address list to obtain a remainder, which is between 0 and the number of addresses. Assume that the dispatch center cluster address list contains three IPs: IP1, IP2, and IP3. If the remainder is 0, the dispatch center server corresponding to the address IP1 is selected to execute the callback task; if the remainder is 1, the dispatch center server corresponding to the address IP2 is selected to execute the callback task; if the remainder is 3, the dispatch center server corresponding to the address IP3 is selected to execute the callback task.
[0055] In this embodiment, temporary nodes can be created under the ZooKeeper cluster. By dividing the temporary nodes into Worker nodes and non-Worker nodes, it is ensured that the tasks will not be triggered repeatedly. In addition, Watcher can be used to monitor the temporary nodes to delete or add temporary nodes in real time to ensure that there is only one Worker node and it can work normally, thereby achieving high availability of the XXL-JOB scheduling center. In addition, the cluster address list of the XXL-JOB scheduling center can be dynamically maintained in real time, and load balancing algorithms such as polling or Hash can be combined to achieve load balancing of processing callback tasks.
[0056] See also Figure 6 , Figure 6 It is a structural diagram of an embodiment of a distributed task scheduling system provided in the present application. The distributed task scheduling system 60 includes a memory 61 and a processor 62 connected to each other. The memory 61 is used to store computer programs. When the computer program is executed by the processor 62, it is used to implement the ZooKeeper-based distributed task scheduling method in the above embodiment.
[0057] This embodiment can ensure that there is only one Worker node in the scheduling center cluster to trigger the task, so that the task will not be triggered repeatedly; and the scheduling center cluster address list can be maintained in real time. Based on the list combined with the load balancing algorithm, load balancing of processing callback tasks can be achieved, which helps to improve the work efficiency of the system.
[0058] See also Figure 7 , Figure 7 It is a structural diagram of an embodiment of a computer-readable storage medium provided in the present application. The computer-readable storage medium 70 is used to store a computer program 71. When the computer program 71 is executed by the processor, it is used to implement the ZooKeeper-based distributed task scheduling method in the above embodiment.
[0059] The computer-readable storage medium 70 may be a server, a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, or other media that can store program codes.
[0060] In the several embodiments provided in this application, it should be understood that the disclosed methods and devices can be implemented in other ways. For example, the device implementation described above is only illustrative, for example, the division of modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed.
[0061] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0062] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0063] The above descriptions are merely embodiments of the present application and are not intended to limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A distributed task scheduling method based on ZooKeeper, characterized in that: include: Using the scheduling center server to create a temporary node to the ZooKeeper cluster, wherein each of the temporary nodes corresponds to a node serial number; Using the scheduling center server to conduct an election based on the node sequence number of the temporary node, so as to select a temporary node from all the temporary nodes to trigger the task; The step of using the scheduling center server to perform election based on the node sequence number of the temporary node to select a temporary node from all the temporary nodes to trigger the task includes: All the temporary nodes are classified using the node serial numbers of the temporary nodes to obtain working nodes and non-working nodes, and the working nodes are selected to trigger the task.
2. The distributed task scheduling method based on ZooKeeper according to claim 1, characterized in that: The step of classifying all the temporary nodes by using the node serial numbers of the temporary nodes to obtain working nodes and non-working nodes includes: Obtain the node serial numbers of all the temporary nodes at the current moment; Determine whether the node serial number of the temporary node corresponding to the current scheduling center server meets the preset conditions; If yes, the temporary node corresponding to the current scheduling center server is the working node; If not, the temporary node corresponding to the current scheduling center server is the non-working node.
3. The distributed task scheduling method based on ZooKeeper according to claim 2, characterized in that: The step of determining whether the node sequence number of the temporary node corresponding to the current scheduling center server meets the preset condition includes: It is determined whether the node serial number of the temporary node corresponding to the current scheduling center server is the minimum value / maximum value of the node serial numbers of all the temporary nodes.
4. The distributed task scheduling method based on ZooKeeper according to claim 1, characterized in that: The step of using the scheduling center server to perform election based on the node sequence number of the temporary node to select a temporary node from all the temporary nodes to trigger the task also includes: Using the working node to start a thread, querying a task set to be triggered, wherein the task set includes at least one task to be triggered; The working node is used to trigger the task to be triggered.
5. The distributed task scheduling method based on ZooKeeper according to claim 1, characterized in that: The method further comprises: Using the current non-working node, select a temporary node from other temporary nodes as a monitoring registration node; The current non-working node is used to register and listen at the monitoring registration node to monitor the node deletion event of the current non-working node.
6. The distributed task scheduling method based on ZooKeeper according to claim 5, characterized in that: The step of using the current non-working node to select a temporary node from other temporary nodes as a monitoring registration node includes: The temporary node with the largest node serial number is selected from the temporary nodes whose node serial numbers are smaller than the node serial number of the current non-working node by using the current non-working node as the monitoring registration node.
7. The distributed task scheduling method based on ZooKeeper according to claim 1, characterized in that: The method further comprises: When an abnormality occurs in the working node, ZooKeeper deletes the working node, and the scheduling center server that monitors the working node receives the node deletion event and returns to the step of using the scheduling center server to conduct an election based on the node sequence number of the temporary node to select a temporary node from all the temporary nodes to trigger the task.
8. The distributed task scheduling method based on ZooKeeper according to claim 1, characterized in that: The method further comprises: The executor is used to obtain the dispatch center cluster address list, so as to use the dispatch center cluster address list for load balancing, and then select a dispatch center server from all the dispatch center servers to execute the task callback.
9. The distributed task scheduling method based on ZooKeeper according to claim 8, characterized in that: The method further comprises: Adding ZooKeeper cluster configuration information to the configuration file of the executor, wherein the ZooKeeper cluster configuration information includes the address and port of the ZooKeeper cluster; Using the executor to obtain temporary node information corresponding to all the temporary nodes from the ZooKeeper cluster, wherein the temporary node information includes the node serial number of the temporary node and the address and port of the scheduling center server corresponding to the temporary node; Using the executor to monitor each of the temporary nodes, so that the ZooKeeper cluster sends a notification to the executor after deleting / adding the temporary node; After the executor receives the notification sent by the ZooKeeper cluster, the executor is used to update the scheduling center cluster address list, wherein the scheduling center cluster address list includes all the temporary node information.
10. The distributed task scheduling method based on ZooKeeper according to claim 8, characterized in that: The step of using the executor to obtain the dispatch center cluster address list, using the dispatch center cluster address list to perform load balancing, and then realizing the step of selecting a dispatch center server from all the dispatch center servers to execute the task callback includes: The scheduling center server corresponding to the first temporary node information is selected from the scheduling center cluster address list to execute the current callback task, and the first temporary node information is moved to the end of the scheduling center cluster address list.
11. The distributed task scheduling method based on ZooKeeper according to claim 8, characterized in that: The step of using the executor to obtain the dispatch center cluster address list, using the dispatch center cluster address list to perform load balancing, and then realizing the step of selecting a dispatch center server from all the dispatch center servers to execute the task callback includes: Dividing the identifier of the current callback task by the number of the scheduling center servers to obtain a remainder; Matching the remainder with the scheduling center server corresponding to the temporary node information; The scheduling center server matching the remainder is selected to execute the current callback task.
12. A distributed task scheduling system, characterized in that: It comprises a memory and a processor connected to each other, wherein the memory is used to store a computer program, and when the computer program is executed by the processor, it is used to implement the ZooKeeper-based distributed task scheduling method described in any one of claims 1 to 11.
13. A computer-readable storage medium for storing a computer program, characterized in that: When the computer program is executed by a processor, it is used to implement the ZooKeeper-based distributed task scheduling method described in any one of claims 1 to 11.
Citation Information
Patent Citations
Data transmission method and device, electronic equipment and readable storage medium
CN109922053A
Distributed task scheduling system and method based on web service
CN110888719A