Method, device and equipment for distributed timing task execution and readable storage medium
By obtaining the application's runtime and task information in a distributed system, and using distributed locks and thread pools to manage scheduled tasks, the problems of missed execution and low efficiency of scheduled tasks are solved, and efficient parallel execution of tasks is achieved.
Patent Information
- Application Number
- CN202010348313.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-04-27
- Publication Date
- 2025-11-21
- Estimated Expiration
- 2040-04-27
AI Technical Summary
In distributed systems, contention among multiple instances of scheduled tasks can lead to issues such as missed executions and low execution efficiency, especially when using distributed locks, which allow tasks to be executed serially.
By receiving the application's startup command, obtaining the runtime, using a pre-set distributed lock to obtain the key value and task duration range, using a resident thread to obtain compensation tasks and scheduled tasks, inserting them into a queue linear list, and using a thread pool to execute tasks, ensuring that tasks are executed according to conditions.
It avoids the loss of scheduled tasks, improves the execution efficiency of scheduled tasks, and enables the parallel execution of multiple tasks.
Smart Images

Figure CN111679895B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, and in particular to a method, apparatus, computer device, and computer-readable storage medium for executing distributed timed tasks. Background Technology
[0002] In web applications, most applications have task scheduling or scheduled task execution capabilities. With the development of software architecture, distributed solutions have emerged. These solutions vertically decompose software functionality into multiple modules for deployment. Each module has different functions, thus refining the application's functional modules and reducing the server load. Using distributed decomposition is equivalent to distributing multiple functions of an application across multiple servers for processing. In this way, each server is responsible for its own module's business logic and is not affected by other business processes.
[0003] Currently, deploying scheduled tasks as a separate module on a dedicated machine, with all resources on that machine dedicated to serving these tasks, not only improves reliability but also allows for finer-grained control over them (the service can be started and stopped at any time, or the module can be deployed in a separate cluster when the amount of data to be executed for scheduled tasks is too large). However, executing scheduled tasks in a distributed environment can lead to data contention among multiple instances, potentially resulting in missed scheduled tasks. Furthermore, while distributed locks are used to execute scheduled tasks on each instance, only one instance's scheduled task can be executed at a time when multiple tasks are available, resulting in sequential execution and low efficiency. Summary of the Invention
[0004] The main purpose of this application is to provide a method, apparatus, computer device, and computer-readable storage medium for executing distributed scheduled tasks. The aim is to address the technical problem of low execution efficiency caused by multiple instances competing for data when executing scheduled tasks in a distributed manner. This is because while distributed locks are used to execute the scheduled tasks of each instance, only one instance's scheduled task can be executed at any given time when there are multiple tasks. All tasks are executed serially.
[0005] Firstly, this application provides a method for executing a distributed scheduled task, the method comprising the following steps:
[0006] Receive the application's launch command, run the application based on the launch command, and obtain the application's runtime.
[0007] Based on a pre-set distributed lock, the application's key value and / or task duration range are obtained according to the application's runtime.
[0008] Based on the first preset resident thread of the application, obtain the compensation task corresponding to the key value of the application and / or the timed task corresponding to the task duration range of the application;
[0009] Insert the compensation task and / or the timed task into a preset queue linear list;
[0010] If it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions, then the compensation task and / or the timed task are executed by the task thread in the preset thread pool.
[0011] Secondly, this application also provides an execution device for a distributed scheduled task, the execution device for the distributed scheduled task comprising:
[0012] The receiving and acquiring module is used to receive the application's startup command, run the application based on the startup command, and acquire the application's running time;
[0013] The first acquisition module is used to acquire the key value of the application and / or the task duration range based on the running time of the application and a pre-set distributed lock.
[0014] The second acquisition module is used to acquire, based on the first preset resident thread of the application, a compensation task corresponding to the key value of the application and / or a timed task corresponding to the task duration range of the application.
[0015] The insertion module is used to insert the compensation task and / or the timed task into a preset queue linear list.
[0016] An execution module is configured to execute the compensation task and / or the timed task through a task thread in a preset thread pool if it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions.
[0017] Thirdly, this application also provides a computer device, the computer device including a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, it implements the steps of the distributed timed task execution method as described above.
[0018] Fourthly, this application also provides a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, it implements the steps of the distributed timed task execution method described above.
[0019] This application provides a method, apparatus, computer device, and computer-readable storage medium for executing distributed scheduled tasks. The method involves receiving a startup command from an application, running the application based on the startup command, and obtaining the application's runtime. Based on a preset distributed lock, the method obtains the application's key value and / or task duration range according to the application's runtime. Based on a first preset resident thread of the application, the method obtains a compensation task corresponding to the application's key value and / or a scheduled task corresponding to the application's task duration range. The method inserts the compensation task and / or the scheduled task into a preset queue linear list. If it is determined that the compensation task and / or the scheduled task in the preset queue linear list meets the execution conditions, the compensation task and / or the scheduled task are executed through a task thread in a preset thread pool. This avoids the loss of scheduled tasks and allows for the simultaneous execution of multiple scheduled tasks, improving the efficiency of scheduled task execution. Attached Figure Description
[0020] To more clearly illustrate the technical solutions of the embodiments of this application, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0021] Figure 1 A flowchart illustrating a method for executing a distributed timed task, as provided in an embodiment of this application;
[0022] Figure 2 for Figure 1 A flowchart illustrating the sub-steps of the distributed scheduled task execution method in the diagram.
[0023] Figure 3 for Figure 1 A flowchart illustrating the sub-steps of the distributed scheduled task execution method in the diagram.
[0024] Figure 4 A flowchart illustrating another method for executing a distributed timed task provided in an embodiment of this application;
[0025] Figure 5 A schematic block diagram of an execution device for a distributed timed task provided in an embodiment of this application;
[0026] Figure 6 This is a schematic block diagram of the structure of a computer device according to an embodiment of this application.
[0027] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation
[0028] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0029] The flowchart shown in the attached diagram is for illustrative purposes only and does not necessarily include all content and operations / steps, nor does it necessarily have to be performed in the order described. For example, some operations / steps can be broken down, combined, or partially merged, so the actual execution order may change depending on the actual situation.
[0030] This application provides a method, apparatus, computer device, and computer-readable storage medium for executing distributed scheduled tasks. The method for executing distributed scheduled tasks can be applied to terminal devices, such as mobile phones, tablets, laptops, and desktop computers.
[0031] The following detailed description of some embodiments of this application is provided in conjunction with the accompanying drawings. Unless otherwise specified, the following embodiments and features can be combined with each other.
[0032] Please refer to Figure 1 , Figure 1 This is a flowchart illustrating a method for executing a distributed timed task, as provided in an embodiment of this application.
[0033] like Figure 1 As shown, the execution method of the distributed timed task includes steps S101 to S105.
[0034] Step S101: Receive the application's startup command, run the application based on the startup command, and obtain the application's running time.
[0035] It receives the application's launch command, runs the corresponding application using the launch command, and retrieves the application's launch time from the launch command, using that launch time as the application's runtime. Alternatively, it can receive the launch command input by the user, record the time the application launch command is received, and use that time as the application's runtime.
[0036] Specifically, when multiple application launch commands are received, the application is run based on the launch command corresponding to each application, and the running time of multiple applications is obtained.
[0037] In one embodiment, a user inputs a launch command for a first application and a second application. The first application and the second application are run respectively via the launch command, and the running time of the first application and the running time of the second application are obtained. The method of obtaining the running time is to record the time when the launch commands for the first application and the second application are received at the same time, and use the time when the launch commands are received as the running time of the first application and the running time of the second application.
[0038] For example, when the terminal receives launch commands for the first application and the second application at 2019-08-01-12:10:00, 2019-08-01-12:10:00 is taken as the launch time of the first and second applications. Alternatively, the terminal receives launch commands for the first application and the second application at different times, and records the time of receiving the launch command for the first application and the time of receiving the launch command for the second application, respectively. The time of receiving the launch command for the first application is taken as the running time of the first application, and the time of receiving the launch command for the second application is taken as the running time of the second application. The number of applications is multiple and not limited.
[0039] Step S102: Based on the preset distributed lock, obtain the key value of the application and / or the task duration range according to the running time of the application.
[0040] The pre-defined distributed locks include pre-defined compensated query distributed locks and pre-defined round-robin distributed locks. The pre-defined compensated query distributed lock determines the application's key value based on the application's runtime. For example, when the application's runtime is obtained, the duration granularity of the pre-defined compensated query distributed lock is obtained, and the application's variable 'a' is calculated using a pre-defined formula.
[0041] For example, if the application's runtime is 2019-08-01-12:25 and the duration granularity t_interval is 10, the default formula is a = currenetMinute / t_interval, where currenetMinute is the runtime and t_interval is the duration granularity.
[0042] For example, if a = 25 / 10, the variable a is calculated to be 2, where the decimal point is removed and only the integer part is retained. After obtaining the variable a, it is substituted into the predefined formula key: yyyy-MM-dd-HH_{a} to determine the application's key value. Here, yyyy represents the year, MM represents the month, dd represents the day, and HH represents the clock, such as 2019-8-1-12:20. When the predefined compensated query distributed lock is used to lock the application, the application's key value is obtained.
[0043] Once it is determined that a pre-set polling distributed lock is used to lock the application, the current time information is obtained. Based on the duration of the pre-set polling distributed lock, the start time key value and end time key value of the pre-set polling distributed lock are calculated. Based on the start time key value and end time key value of the pre-set polling distributed lock, the task duration range of the application is obtained.
[0044] For example, the start time key value of the pre-set polling distributed lock is yyyy-MM-dd-HH-{b1} and the end time key value is yyyy-MM-dd-HH-{b2}, and the pre-set formula t_interval=b2-b1 is satisfied. If the current time is 2019-08-01-12:25 and the duration t_interval is 10, then we get b1 = 25 / t_interval = 2. Using the formula t_interval = b2 - b1, we get b2 = 8. Substituting b1 into the starting time key value: yyyy-MM-dd-HH-{b1}, we get the starting time key value as 2019-08-01-12-{20}. Substituting b2 into the ending time key value: yyyy-MM-dd-HH-{b2}, we get 2019-08-01-12-{80}, which is equivalent to 2019-08-01-13-{20}. Using 2019-08-01-12-{20} and 2019-08-01-13-{20}, the task duration range is obtained from 2019-08-01-12-{20} to 2019-08-01-13-{20}.
[0045] In one embodiment, such as Figure 2 As shown, step S102 includes sub-steps S1021 to S1023.
[0046] Sub-step S1021: Determine the key value of each application by using the preset compensation query distributed lock according to the running time of each application.
[0047] When the runtime times of multiple applications are obtained, the key value of each application is determined by using the pre-defined duration granularity of the compensated query distributed lock and the runtime times of each application. For example, when competition for the pre-defined compensated query distributed lock between the first and second applications is detected, the runtime times of the first and second applications, along with the duration granularity of the pre-defined compensated query distributed lock, are obtained. The key value of the first application is determined by using its runtime time and duration granularity, and the key value of the second application is determined by using its runtime time and duration granularity.
[0048] Sub-step S1022: When the preset compensation query distributed lock locks each of the applications, determine the application locked by the preset compensation query distributed lock and the key value corresponding to the locked application.
[0049] When the pre-set compensation query distributed lock locks the first application and the second application, the application locked by the pre-set compensation query distributed lock is determined. The application locked by the pre-set compensation query distributed lock is randomly locked, and the locked application can be either the first application or the second application. If the application locked by the pre-set compensation query distributed lock is the first application, then the key value of the first application is determined.
[0050] Sub-step S1023, and / or, when the preset polling distributed lock locks each of the applications, determine the application locked by the preset polling distributed lock and the key value of the application, and determine the task duration range of the application based on the key value of the application and the preset duration.
[0051] When the first application and the second application compete for a pre-set polling distributed lock, and the pre-set polling distributed lock acquires a lock on both applications, the application being locked by the pre-set polling distributed lock is determined. If the application being locked by the pre-set polling distributed lock is the first application, the key value of the first application is determined. Based on the key value of the first application and the pre-set duration, the task duration range of the first application is determined. For example, if the key value of the first application is determined to be 2019-08-01-12-{20} and the pre-set duration is 60 minutes, then the task duration range of the first application is determined to be from 2019-08-01-12-{20} to 2019-08-01-13-{20}.
[0052] Alternatively, when the first application and the second application compete for a preset polling distributed lock, and the preset polling distributed lock locks both the first and second applications, the application locked by the preset polling distributed lock is determined. If the application locked by the preset polling distributed lock is the second application, the key value of the second application is determined. Based on the key value of the second application and the preset duration, the task duration range of the second application is determined.
[0053] For example, if the key value of the second application is determined to be 2019-08-01-12-{20} and the preset duration is 60 minutes, then the task duration range of the second application is determined to be from 2019-08-01-12-{20} to 2019-08-01-13-{20}.
[0054] Step S103: Based on the first preset resident thread of the application, obtain the compensation task corresponding to the key value of the application and / or the timed task corresponding to the task duration range of the application.
[0055] The application retrieves, via its first pre-defined persistent thread, a compensation task corresponding to the application's key value and a scheduled task corresponding to the application's task duration range; or, it retrieves either a compensation task corresponding to the application's key value or a scheduled task corresponding to the application's task duration range. Each application has two persistent threads: a first pre-defined persistent thread and a second pre-defined persistent thread. These two threads run concurrently with the application from startup until it stops. The first pre-defined persistent thread performs a query based on the application's runtime, retrieving the corresponding compensation task and / or scheduled task from the pre-defined scheduled task table for that runtime. The compensation task is a type of scheduled task.
[0056] For example, when the application's key value is 2019-08-01-12-{20}, the first pre-defined persistent thread queries the pre-defined scheduled task table for the application's scheduled tasks and obtains the execution time of each scheduled task. If an execution time less than 2019-08-01-12-{20} is found, that scheduled task is treated as a compensation task. And / or, when the application's execution duration range is obtained, the first pre-defined persistent thread retrieves scheduled tasks from the pre-defined scheduled task table whose execution times fall within that range.
[0057] Specifically, based on the first preset resident thread and the preset timed task table of the application, a compensation task corresponding to the key value of the application is obtained from the preset timed task table; and / or, based on the first preset resident thread and the preset timed task table of the application, a timed task corresponding to the task duration range of the application is obtained from the preset timed task table.
[0058] In one embodiment, if the key value of the first application and the task duration range of the first application are obtained, based on the first preset persistent thread and the preset scheduled task table of the first application, the first preset persistent thread queries the preset scheduled task table according to the key value of the first application, determines that the execution time of the scheduled task in the preset scheduled task table is less than the key value of the first application, obtains the scheduled task whose execution time is less than the key value of the first application, and uses the scheduled task whose execution time is less than the key value of the first application as a compensation task. The first preset persistent thread of the first application queries the preset scheduled task table according to the task duration range of the first application, determines that the execution time of the scheduled task in the preset scheduled task table is within the task duration range, and obtains the scheduled task whose execution time is within the task duration range.
[0059] Alternatively, if the key value of the first application and the task duration range of the second application are obtained, based on the first preset persistent thread and the preset scheduled task table of the first application, the first preset persistent thread queries the preset scheduled task table according to the key value of the first application, determines that the execution time of the scheduled task in the preset scheduled task table is less than the key value of the first application, obtains the scheduled task with the execution time less than the key value of the first application, and uses the scheduled task with the execution time less than the key value of the first application as a compensation task. Based on the first preset persistent thread of the second application, queries the preset scheduled task table according to the task duration range of the second application, determines that the execution time of the scheduled task in the preset scheduled task table is within the task duration range, and obtains the scheduled task with the execution time within the task duration range.
[0060] Step S104: Insert the compensation task and / or the timed task into the preset queue linear list.
[0061] The application's first pre-defined persistent thread inserts the acquired compensation tasks and / or scheduled tasks into a pre-defined linear list. A linear list is a special type of linear list, a First-In-First-Out (FIFO) data structure. Deletion is only allowed at the front of the list, while insertion is allowed at the rear. The end where insertion occurs is called the rear, and the end where deletion occurs is called the front. A queue without elements is called an empty queue. The acquired compensation tasks and / or scheduled tasks are inserted into a pre-defined linear list in the database. Before insertion, the first pre-defined persistent thread retrieves the execution times of the compensation tasks and / or scheduled tasks and arranges them according to their execution times.
[0062] If the compensation task and the timed task of the first application are obtained, the compensation task and the timed task are inserted into the preset queue linear list through the first preset resident thread of the first application.
[0063] Alternatively, if the compensation task of the first application and the timed task of the second application are obtained, the compensation task is inserted into the preset queue linear list through the first preset resident thread of the first application, and the timed task is inserted into the preset queue linear list through the first preset resident thread of the second application.
[0064] Step S105: If it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions, then the compensation task and / or the timed task are executed through the task thread in the preset thread pool.
[0065] The application's second pre-defined resident thread queries the pre-defined queue linear list to determine if the compensation tasks and / or timed tasks in the queue meet the execution conditions. This second pre-defined resident thread is responsible for retrieving tasks from the queue linear list, comparing them with the current time, and checking if the task's execution time has arrived. The application's second thread monitors the compensation tasks and / or timed tasks in the queue linear list and extracts them into the pre-defined thread pool.
[0066] When the provisioned thread pool detects a compensation task and / or a scheduled task submitted by a second provisioned resident thread of the application, the compensation task and / or scheduled task are executed by a task thread in the provisioned thread pool. A thread pool is a form of multithreaded processing where tasks are added to a queue during processing, and these tasks are automatically started after the thread is created. Thread pool threads are all background threads. Each thread uses a default stack size, runs at a default priority, and exists within a multithreaded unit. If a thread is idle in managed code (e.g., waiting for an event), the thread pool will insert another helper thread to keep all processors busy. If all thread pool threads are always busy, but there is pending work in the queue, the thread pool will create another helper thread after a period of time, but the number of threads will never exceed the maximum value. Threads exceeding the maximum value can be queued, but they will wait until other threads complete before starting.
[0067] In one embodiment, such as Figure 3 As shown, step S105 includes sub-steps S1051 to S1053.
[0068] Sub-step S1051: Based on the second resident thread of the application, determine whether the compensation task and / or the timed task in the preset queue linear table meet the execution conditions.
[0069] The first application queries the pre-set queue linear table through the second pre-set resident thread, compares the execution time of the compensation task and the timed task of the first application in the pre-set queue linear table with the current time, and determines that the execution time of the compensation task and the timed task of the first application is met when the execution time is less than or equal to the current time.
[0070] Alternatively, the second preset resident thread of the first application queries the preset queue linear table, compares the execution time of the compensation task of the first application in the preset queue linear table with the current time, and determines that the compensation task of the first application meets the execution time requirement when the execution time of the compensation task is less than or equal to the current time; similarly, the second preset resident thread of the second application queries the preset queue linear table, compares the execution time of the timed task of the second application in the preset queue linear table with the current time, and determines that the timed task of the second application meets the execution time requirement when the execution time of the timed task is less than or equal to the current time.
[0071] Sub-step S1052: If the compensation task and / or the timed task meet the execution conditions, then submit the compensation task and / or the timed task to the preset thread pool.
[0072] The second pre-defined persistent thread of the first application submits compensation tasks and timed tasks that meet the execution conditions to the pre-defined thread pool. Alternatively, the second pre-defined persistent thread of the first application submits compensation tasks that meet the execution conditions to the pre-defined thread pool, or the second pre-defined persistent thread of the second application submits timed tasks that meet the execution conditions to the pre-defined thread pool.
[0073] Sub-step S1053: Generate the same number of task threads as the compensation task and / or the timed task through the preset thread pool, and execute the compensation task and / or the timed task based on the task threads.
[0074] When a compensation task and / or timed task that meets the execution conditions is obtained through a pre-configured thread pool, the number of compensation tasks and / or timed tasks that meet the execution conditions is counted, and the same number of task threads as the number of compensation tasks and / or timed tasks that meet the execution conditions are generated. The compensation task and / or timed task is then executed through the generated task threads.
[0075] Before executing the compensation task and / or the timed task based on the task thread, the method further includes: when the number of the compensation task and / or the timed task is greater than the target number of the preset thread pool, obtaining the unexecuted compensation task and / or the timed task, and pre-storing the unexecuted compensation task and / or the timed task to the cache queue of the preset thread pool; after the preset thread pool destroys the task thread, generating a task thread with the same number of unexecuted compensation tasks and / or the timed task in the cache queue.
[0076] In one embodiment, when the number of compensation tasks and / or timed tasks that meet the execution conditions is obtained from the preset thread pool statistics, the size of the core pool of the preset thread pool is read. Based on the size of the core pool, the target number of task threads to be generated by the preset thread pool is determined. The size of the core pool is related to the resources allocated to the terminal; the more resources allocated to the terminal, the larger the core pool of the thread pool. Core pool: After the thread pool is created, there are no threads in the thread pool. Instead, threads are created to execute tasks only when tasks arrive. After the thread pool is created, the number of threads in the thread pool is 0. When a task arrives, a thread is created to execute the task. When the number of threads in the thread pool reaches the size of the core pool, the arriving tasks are placed in the cache queue.
[0077] When the number of compensation tasks and / or timed tasks that meet the execution conditions exceeds the target number of the thread pool, the target number of task threads are generated to execute the compensation tasks and / or timed tasks, and the unexecuted target compensation tasks and / or timed tasks are marked and stored in the thread pool's cache queue. When it is detected that the generated task thread executes successfully or encounters an exception, the generated task thread or the task thread that encountered an exception is destroyed, and a new number of task threads equal to the number of unexecuted compensation tasks and / or timed tasks are generated and executed.
[0078] In this application, a compensation query distributed lock is used to obtain the application's compensation tasks during application runtime. A distributed lock is polled to obtain the application's scheduled tasks. A pre-configured thread pool generates task threads corresponding to the compensation tasks and / or scheduled tasks, allowing multiple scheduled tasks to be executed simultaneously. This includes any scheduled tasks that were not executed at the specified execution time, such as the compensation tasks. This avoids the loss of scheduled tasks and improves the efficiency of executing multiple scheduled tasks concurrently.
[0079] Please refer to Figure 4 , Figure 4 This is a flowchart illustrating another method for executing a distributed timed task provided in an embodiment of this application.
[0080] like Figure 4 As shown, the execution method of this distributed timed task includes steps S201 to S207.
[0081] Step S201: Receive the application's startup command, run the application based on the startup command, and obtain the application's running time.
[0082] It receives the application's launch command, runs the corresponding application using the launch command, and retrieves the application's launch time from the launch command, using that launch time as the application's runtime. Alternatively, it can receive the launch command input by the user, record the time the application launch command is received, and use that time as the application's runtime.
[0083] Step S202: Based on the preset distributed lock, obtain the key value of the application and / or the task duration range according to the running time of the application.
[0084] The pre-defined distributed locks include pre-defined compensated query distributed locks and pre-defined round-robin distributed locks. The pre-defined compensated query distributed lock determines the application's key value based on the application's runtime. For example, when the application's runtime is obtained, the duration granularity of the pre-defined compensated query distributed lock is obtained, and the application's variable 'a' is calculated using a pre-defined formula.
[0085] For example, if the application's runtime is obtained as 2019-08-01-12:25, and the duration granularity t_interval is 10, the application's key value is obtained using the predefined formula a = currenetMinute / t_interval, where currenetMinute is the runtime and t_interval is the duration granularity. For instance, a = 25 / 10, calculating variable a to be 2 (decimal points removed, integers retained). After obtaining variable a, it is substituted into the predefined formula key: yyyy-MM-dd-HH_{a} to determine the application's key value. Here, yyyy represents the year, MM the month, dd the day, and HH the clock, such as 2019-08-01-12:20. When a predefined compensated query distributed lock is used to lock the application, the application's key value is obtained.
[0086] And / or, when it is determined that the pre-set polling distributed lock is used to lock the application, the current time information is obtained, the start time key value and end time key value of the pre-set polling distributed lock are calculated through the duration of the pre-set polling distributed lock, and the task duration range of the application is obtained based on the start time key value and end time key value of the pre-set polling distributed lock.
[0087] For example, the start time key value of the pre-set polling distributed lock is yyyy-MM-dd-HH-{b1} and the end time key value is yyyy-MM-dd-HH-{b2}, and the pre-set formula t_interval=b2-b1 is satisfied. If the current time is 2019-08-01-12:25 and the duration t_interval is 10, then we get b1 = 25 / t_interval = 2. Using the formula t_interval = b2 - b1, we get b2 = 8. Substituting b1 into the starting time key value: yyyy-MM-dd-HH-{b1}, we get the starting time key value as 2019-08-01-12-{20}. Substituting b2 into the ending time key value: yyyy-MM-dd-HH-{b2}, we get 2019-08-01-12-{80}, which is equivalent to 2019-08-01-13-{20}. Using 2019-08-01-12-{20} and 2019-08-01-13-{20}, the task duration range is obtained from 2019-08-01-12-{20} to 2019-08-01-13-{20}.
[0088] Step S203: Based on the first preset resident thread of the application, obtain the compensation task corresponding to the key value of the application and / or the timed task corresponding to the task duration range of the application.
[0089] The application retrieves, via its first pre-defined persistent thread, a compensation task corresponding to the application's key value and a scheduled task corresponding to the application's task duration range; or, it retrieves either a compensation task corresponding to the application's key value or a scheduled task corresponding to the application's task duration range. Each application has two persistent threads: a first pre-defined persistent thread and a second pre-defined persistent thread. These two threads run concurrently with the application from startup until it stops. The first pre-defined persistent thread performs a query based on the application's runtime, retrieving the corresponding compensation task and / or scheduled task from the pre-defined scheduled task table for that runtime. The compensation task is a type of scheduled task.
[0090] For example, when the application's key value is 2019-08-01-12-{20}, the first pre-defined persistent thread queries the pre-defined scheduled task table for the application's scheduled tasks and obtains the execution time of each scheduled task. If an execution time less than 2019-08-01-12-{20} is found, that scheduled task is treated as a compensation task. And / or, when the application's execution duration range is obtained, the first pre-defined persistent thread retrieves scheduled tasks from the pre-defined scheduled task table whose execution times fall within that range.
[0091] Step S204: Insert the compensation task and / or the timed task into the preset queue linear list.
[0092] The application's first pre-defined persistent thread inserts the acquired compensation tasks and / or scheduled tasks into a pre-defined linear list. A linear list is a special type of linear list, a First-In-First-Out (FIFO) data structure. It only allows deletion operations at the front (front) and insertion operations at the rear (rear). The end where insertion occurs is called the rear, and the end where deletion occurs is called the front. A queue without elements is called an empty queue. The acquired compensation tasks and / or scheduled tasks are inserted into a pre-defined linear list in the database. Before insertion, the first pre-defined persistent thread obtains the execution times of the compensation tasks and / or scheduled tasks and arranges them according to their execution times.
[0093] Step S205: If it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions, then the compensation task and / or the timed task are executed through the task thread in the preset thread pool.
[0094] The application's second pre-defined resident thread queries the pre-defined queue linear list to determine if the compensation tasks and / or timed tasks in the queue meet the execution conditions. This second pre-defined resident thread is responsible for retrieving tasks from the queue linear list, comparing them with the current time, and checking if the task's execution time has arrived. The application's second thread monitors the compensation tasks and / or timed tasks in the queue linear list and extracts them into the pre-defined thread pool.
[0095] When the provisioned thread pool detects a compensation task and / or a scheduled task submitted by a second provisioned resident thread of the application, the compensation task and / or scheduled task are executed by a task thread in the provisioned thread pool. A thread pool is a form of multithreaded processing where tasks are added to a queue during processing, and these tasks are automatically started after the thread is created. Thread pool threads are all background threads. Each thread uses a default stack size, runs at a default priority, and exists within a multithreaded unit. If a thread is idle in managed code (e.g., waiting for an event), the thread pool will insert another helper thread to keep all processors busy. If all thread pool threads are always busy, but there is pending work in the queue, the thread pool will create another helper thread after a period of time, but the number of threads will never exceed the maximum value. Threads exceeding the maximum value can be queued, but they will wait until other threads complete before starting.
[0096] Step S206: When abnormal information of the task thread is detected, obtain the compensation task and / or the timed task corresponding to the abnormal information.
[0097] When an exception is detected in a task thread, the detection method is as follows: each task thread sends an identifier when it successfully executes a compensation task and / or a scheduled task. When the detected identifier is an exception, the corresponding compensation task and / or scheduled task is retrieved. For example, the identifier sent by the task thread can be pre-defined: when the identifier sent by the task thread is 1, it indicates that the task thread has executed successfully; when the identifier sent by the task thread is 0, it indicates that the task thread has failed, i.e., an exception has occurred.
[0098] Step S207: Add the compensation task and / or the timed task to the preset queue linear list, and record the number of times the compensation task and / or the timed task are executed.
[0099] When obtaining the compensation task and / or scheduled task corresponding to the exception information, the compensation task and / or scheduled task corresponding to the exception information are added to the queue linear pairing list, and the number of times the compensation task and / or scheduled task corresponding to the exception information are executed is recorded.
[0100] In this application, compensation tasks and / or scheduled tasks of the application are obtained through compensation distributed locks and polling distributed locks. Task threads corresponding to the compensation tasks and / or scheduled tasks are generated in a preset thread pool. If an exception is detected during the execution of the compensation tasks and / or scheduled tasks by the task threads, the compensation tasks and / or scheduled tasks corresponding to the exception are obtained and re-executed, thereby avoiding the failure of the compensation tasks and / or scheduled tasks and improving the success rate of task execution.
[0101] Please refer to Figure 5 , Figure 5 This is a schematic block diagram of a distributed timed task execution device provided in an embodiment of this application.
[0102] like Figure 5 As shown, the distributed timed task execution device 400 includes: a receiving module 401, a first acquisition module 402, a second acquisition module 403, an insertion module 404, an execution module 405, a third acquisition module 406, and a recording module 407.
[0103] The receiving and acquiring module 401 is used to receive the application's startup instruction, run the application based on the startup instruction, and acquire the application's running time;
[0104] The first acquisition module 402 is used to acquire the key value of the application and / or the task duration range based on the running time of the application and a preset distributed lock.
[0105] The second acquisition module 403 is used to acquire, based on the first preset resident thread of the application, a compensation task corresponding to the key value of the application and / or a timed task corresponding to the task duration range of the application.
[0106] Insertion module 404 is used to insert the compensation task and / or the timed task into a preset queue linear list;
[0107] The execution module 405 is used to execute the compensation task and / or the timed task through a task thread in a preset thread pool if it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions.
[0108] The third acquisition module 406 is used to acquire the compensation task and / or the timed task corresponding to the abnormal information when the abnormal information of the task thread is detected.
[0109] The recording module 407 is used to add the compensation task and / or the timed task to the preset queue linear list, and record the number of times the compensation task and / or the timed task is executed.
[0110] Specifically, the receiving and acquisition module 401 is used to acquire the running times of multiple applications.
[0111] Specifically, the first acquisition module 402 is used for:
[0112] Based on the runtime of each application, the key value of each application is determined by querying the distributed lock through the preset compensation query.
[0113] When the pre-set compensation query distributed lock locks each of the applications, the application being locked by the pre-set compensation query distributed lock and the key value corresponding to the locked application are determined; and / or
[0114] When a pre-set polling distributed lock locks each of the applications, the application being locked by the pre-set polling distributed lock and the key value of the application are determined, and the task duration range of the application is determined based on the key value of the application and a pre-set duration.
[0115] Specifically, the second acquisition module 403 is used for:
[0116] Based on the application's first preset resident thread and preset scheduled task table, obtain the compensation task corresponding to the application's key value in the preset scheduled task table; and / or
[0117] Based on the first preset resident thread and the preset timed task table of the application, obtain the timed task in the preset timed task table that corresponds to the task duration range of the application.
[0118] Specifically, the execution module 405 is used for:
[0119] Based on the second persistent thread of the application, determine whether the compensation task and / or the timed task in the preset queue linear list meet the execution conditions;
[0120] If the compensation task and / or the timed task meet the execution conditions, then the compensation task and / or the timed task will be submitted to the preset thread pool.
[0121] The preset thread pool generates the same number of task threads as the compensation task and / or the timed task, and the compensation task and / or the timed task are executed based on the task threads.
[0122] Specifically, the execution module 405 is used for:
[0123] When the number of the compensation tasks and / or the timed tasks exceeds the target number of the preset thread pool, the unexecuted compensation tasks and / or the timed tasks are obtained and pre-stored in the cache queue of the preset thread pool.
[0124] After the task thread is destroyed in the preset thread pool, a number of task threads equal to the number of unexecuted compensation tasks and / or timed tasks in the cache queue are generated.
[0125] It should be noted that those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the above-described device and its modules and units can be referred to the corresponding processes in the aforementioned embodiments of the distributed timed task execution method, and will not be repeated here.
[0126] The apparatus provided in the above embodiments can be implemented as a computer program, which can be used in, for example... Figure 6 It runs on the computer device shown.
[0127] Please see Figure 6 , Figure 6 This is a schematic block diagram illustrating the structure of a computer device provided in an embodiment of this application. The computer device is a server.
[0128] like Figure 6 As shown, the computer device includes a processor, memory, and network interface connected via a system bus, wherein the memory may include non-volatile storage media and internal memory.
[0129] Non-volatile storage media can store operating systems and computer programs. These computer programs include program instructions that, when executed, cause the processor to perform any distributed timed task execution method.
[0130] The processor provides computing and control capabilities, supporting the operation of the entire computer device.
[0131] Internal memory provides an environment for the execution of computer programs stored in non-volatile storage media. When the computer program is executed by the processor, it enables the processor to execute any distributed timed task.
[0132] This network interface is used for network communication, such as sending assigned tasks. Those skilled in the art will understand that... Figure 6 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0133] It should be understood that the processor can be a Central Processing Unit (CPU), but it can also be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among these, a general-purpose processor can be a microprocessor or any conventional processor.
[0134] In one embodiment, the processor is configured to run a computer program stored in memory to perform the following steps:
[0135] Receive the application's launch command, run the application based on the launch command, and obtain the application's runtime.
[0136] Based on a pre-set distributed lock, the application's key value and / or task duration range are obtained according to the application's runtime.
[0137] Based on the first preset resident thread of the application, obtain the compensation task corresponding to the key value of the application and / or the timed task corresponding to the task duration range of the application;
[0138] Insert the compensation task and / or the timed task into a preset queue linear list;
[0139] If it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions, then the compensation task and / or the timed task are executed by the task thread in the preset thread pool.
[0140] In one embodiment, when the processor acquires the runtime of the application, it is configured to acquire the runtime of multiple applications.
[0141] In one embodiment, the processor implements a preset distributed lock including a preset compensated query distributed lock and a preset polling distributed lock; when obtaining the application's key value and / or task duration range based on the preset distributed lock according to the application's runtime, it is used to:
[0142] Based on the runtime of each application, the key value of each application is determined by querying the distributed lock through the preset compensation query.
[0143] When the pre-set compensation query distributed lock locks each of the applications, the application being locked by the pre-set compensation query distributed lock and the key value corresponding to the locked application are determined; and / or
[0144] When a pre-set polling distributed lock locks each of the applications, the application being locked by the pre-set polling distributed lock and the key value of the application are determined, and the task duration range of the application is determined based on the key value of the application and a pre-set duration.
[0145] In one embodiment, when the processor, based on a first preset resident thread of the application, obtains a compensation task corresponding to the key value of the application and / or a timed task corresponding to the task duration range of the application, it is configured to:
[0146] Based on the application's first preset resident thread and preset scheduled task table, obtain the compensation task corresponding to the application's key value in the preset scheduled task table; and / or
[0147] Based on the first preset resident thread and the preset timed task table of the application, obtain the timed task in the preset timed task table that corresponds to the task duration range of the application.
[0148] In one embodiment, when the processor executes the compensation task and / or the timed task through a task thread in a preset thread pool if it determines that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions, the processor is configured to:
[0149] Based on the second persistent thread of the application, determine whether the compensation task and / or the timed task in the preset queue linear list meet the execution conditions;
[0150] If the compensation task and / or the timed task meet the execution conditions, then the compensation task and / or the timed task will be submitted to the preset thread pool.
[0151] The preset thread pool generates the same number of task threads as the compensation task and / or the timed task, and the compensation task and / or the timed task are executed based on the task threads.
[0152] In one embodiment, before executing the compensation task and / or the timed task based on the task thread, the processor is configured to:
[0153] When the number of the compensation tasks and / or the timed tasks exceeds the target number of the preset thread pool, the unexecuted compensation tasks and / or the timed tasks are obtained and pre-stored in the cache queue of the preset thread pool.
[0154] After the task thread is destroyed in the preset thread pool, a number of task threads equal to the number of unexecuted compensation tasks and / or timed tasks in the cache queue are generated.
[0155] In another embodiment, the processor is configured to run a computer program stored in memory to perform the following steps:
[0156] When abnormal information of the task thread is detected, the compensation task and / or the timed task corresponding to the abnormal information are obtained;
[0157] Add the compensation task and / or the timed task to the preset queue linear list, and record the number of times the compensation task and / or the timed task are executed.
[0158] This application also provides a computer-readable storage medium storing a computer program, the computer program including program instructions, and the method implemented when the program instructions are executed can refer to various embodiments of the distributed timed task execution method of this application.
[0159] The computer-readable storage medium may be an internal storage unit of the computer device described in the foregoing embodiments, such as the hard disk or memory of the computer device. The computer-readable storage medium may also be an external storage device of the computer device, such as a plug-in hard disk, SmartMedia Card (SMC), Secure Digital (SD) card, or Flash Card equipped on the computer device.
[0160] Furthermore, the computer-readable storage medium may primarily include a program storage area and a data storage area, wherein the program storage area may store the operating system, at least one application required for a function, etc.; and the data storage area may store data created based on the use of blockchain nodes, etc.
[0161] The blockchain referred to in this invention is a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. Essentially, a blockchain is a decentralized database, a chain of data blocks linked together using cryptographic methods. Each data block contains information about a batch of network transactions, used to verify the validity of the information (anti-counterfeiting) and generate the next block. A blockchain can include an underlying blockchain platform, a platform product service layer, and an application service layer.
[0162] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or system. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or system that includes that element.
[0163] The sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments. The above descriptions are merely specific implementations of this application, but the scope of protection of this application is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in this application, and these modifications or substitutions should all be covered within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method for executing a distributed scheduled task, characterized in that, include: Receive the application's launch command, run the application based on the launch command, and obtain the application's runtime. Based on a pre-set distributed lock, the application's key value and / or task duration range are obtained according to the application's runtime. Based on the first preset resident thread of the application, obtain the compensation task corresponding to the key value of the application and / or the timed task corresponding to the task duration range of the application; Insert the compensation task and / or the timed task into a preset queue linear list; If it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions, then the compensation task and / or the timed task are executed through the task thread in the preset thread pool. The step of obtaining the application's key value and / or task duration range based on a pre-set distributed lock and the application's runtime includes: Obtain the duration granularity of the preset distributed lock; the preset distributed lock includes at least one of a preset compensation query distributed lock and a preset polling distributed lock; The variables of the application are determined based on the running time of the application and the duration granularity of the preset distributed lock; the variables of the application include at least one of the following: a first variable corresponding to the preset compensated query distributed lock, a second variable corresponding to the preset polling distributed lock, and a third variable. The application's variables are substituted into a preset formula corresponding to the application's key value to obtain the application's key value; the application's key value includes at least one of the following: the application's key value corresponding to the first variable, the start time key value corresponding to the second variable, and the end time key value corresponding to the third variable; When the application's key value includes the start time key value corresponding to the second variable and the end time key value corresponding to the third variable, the task duration range of the application is determined based on the start time key value corresponding to the second variable and the end time key value corresponding to the third variable.
2. The method for executing a distributed timed task as described in claim 1, characterized in that, The step of obtaining the runtime of the application includes obtaining the runtime of multiple applications.
3. The method for executing a distributed timed task as described in claim 2, characterized in that, The pre-set distributed lock includes a pre-set compensation query distributed lock and a pre-set polling distributed lock; The step of obtaining the application's key value and / or task duration range based on a pre-set distributed lock and the application's runtime includes: Based on the runtime of each application, the key value of each application is determined by querying the distributed lock through the preset compensation query. When the pre-set compensation query distributed lock locks each of the applications, the application being locked by the pre-set compensation query distributed lock and the key value corresponding to the locked application are determined; and / or When a pre-set polling distributed lock locks each of the applications, the application being locked by the pre-set polling distributed lock and the key value of the application are determined, and the task duration range of the application is determined based on the key value of the application and a pre-set duration.
4. The method for executing a distributed scheduled task as described in claim 2, characterized in that, The step of obtaining a compensation task corresponding to the key value of the application and / or a timed task corresponding to the task duration range of the application based on the first preset resident thread of the application includes: Based on the application's first preset resident thread and preset scheduled task table, obtain the compensation task corresponding to the application's key value in the preset scheduled task table; and / or Based on the first preset resident thread and the preset timed task table of the application, obtain the timed task in the preset timed task table that corresponds to the task duration range of the application.
5. The method for executing a distributed timed task as described in claim 1, characterized in that, If it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions, then the compensation task and / or the timed task are executed through a task thread in the preset thread pool, including: Based on the second persistent thread of the application, determine whether the compensation task and / or the timed task in the preset queue linear list meet the execution conditions; If the compensation task and / or the timed task meet the execution conditions, then the compensation task and / or the timed task will be submitted to the preset thread pool. The preset thread pool generates the same number of task threads as the compensation task and / or the timed task, and the compensation task and / or the timed task are executed based on the task threads.
6. The method for executing a distributed scheduled task as described in claim 5, characterized in that, Before executing the compensation task and / or the scheduled task based on the task thread, the method further includes: When the number of the compensation tasks and / or the timed tasks exceeds the target number of the preset thread pool, the unexecuted compensation tasks and / or the timed tasks are obtained and pre-stored in the cache queue of the preset thread pool. After the task thread is destroyed in the preset thread pool, a number of task threads equal to the number of unexecuted compensation tasks and / or timed tasks in the cache queue are generated.
7. The method for executing a distributed timed task as described in any one of claims 1-6, characterized in that, After the compensation task and / or the timed task are executed by a task thread in a pre-set thread pool, the process further includes: When abnormal information of the task thread is detected, the compensation task and / or the timed task corresponding to the abnormal information are obtained; Add the compensation task and / or the timed task to the preset queue linear list, and record the number of times the compensation task and / or the timed task are executed.
8. A distributed timed task execution device, characterized in that, The execution device for the distributed timed task includes: The receiving and acquiring module is used to receive the application's startup command, run the application based on the startup command, and acquire the application's running time; The first acquisition module is used to acquire the key value of the application and / or the task duration range based on the running time of the application and a pre-set distributed lock. The second acquisition module is used to acquire, based on the first preset resident thread of the application, a compensation task corresponding to the key value of the application and / or a timed task corresponding to the task duration range of the application. The insertion module is used to insert the compensation task and / or the timed task into a preset queue linear list. An execution module is configured to execute the compensation task and / or the timed task through a task thread in a preset thread pool if it is determined that the compensation task and / or the timed task in the preset queue linear list meet the execution conditions. The step of obtaining the application's key value and / or task duration range based on a pre-set distributed lock and the application's runtime includes: Obtain the duration granularity of the preset distributed lock; the preset distributed lock includes at least one of a preset compensation query distributed lock and a preset polling distributed lock; The variables of the application are determined based on the running time of the application and the duration granularity of the preset distributed lock; the variables of the application include at least one of the following: a first variable corresponding to the preset compensated query distributed lock, a second variable corresponding to the preset polling distributed lock, and a third variable. The application's variables are substituted into a preset formula corresponding to the application's key value to obtain the application's key value; the application's key value includes at least one of the following: the application's key value corresponding to the first variable, the start time key value corresponding to the second variable, and the end time key value corresponding to the third variable; When the application's key value includes the start time key value corresponding to the second variable and the end time key value corresponding to the third variable, the task duration range of the application is determined based on the start time key value corresponding to the second variable and the end time key value corresponding to the third variable.
9. A computer device, characterized in that, The computer device includes a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, it implements the steps of the method for executing a distributed timed task as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, it implements the steps of the method for executing a distributed timed task as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Distributed lock execution method, device and system, application server and storage medium
CN108874552A
Timing task monitoring method, computer readable storage medium and terminal device
CN109298962A