Delay task execution method and device

By using a combination of time round and distributed storage in microservices, the uniqueness and lag problems of delayed task execution are solved, and the timely and efficient execution of delayed tasks is achieved.

CN119025231APending Publication Date: 2024-11-26BEIJING SHANGYIN MICRO CORE TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202410725542.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-05
Publication Date
2024-11-26

AI Technical Summary

Technical Problem

The delayed task execution method of existing microservices cannot guarantee the uniqueness of delayed tasks and avoid delayed task execution time lag.

Method used

By storing the unique identification of the delay task in the time round and determining the execution time of the delay task based on the time round, the distributed storage ensures the uniqueness and parallel processing of the delay task to avoid information backlog.

Benefits of technology

The uniqueness and timely execution of delayed tasks are realized, the delayed task execution time is avoided, and the system efficiency and reliability are improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119025231A_ABST
    Figure CN119025231A_ABST
Patent Text Reader

Abstract

The invention discloses a delay task execution method and device, and relates to the technical field of micro-services. The method is applied to a micro-service instance, and comprises the following steps: acquiring stored delay task basic information from distributed storage; based on the delay time of the delay task, storing the unique identifier of the delay task in a time wheel; based on the time wheel, when it is determined that the current time reaches the execution time of the delay task, the unique identifier of the corresponding delay task in the time wheel is read; and triggering the execution of the corresponding delay task based on the unique identifier of the corresponding delay task. According to the method, other threads are triggered to execute the delay task after it is determined that the delay time is up through the time wheel, multiple delay tasks can be processed in parallel, the situation that delay task information is overstocked and delay task execution time lags is avoided, and furthermore, the uniqueness of delay task execution can be guaranteed through distributed storage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of task processing, and in particular to a delayed task execution method and device. Background Art

[0002] With the rapid development of science and technology, the application of microservices (Microservices Architecture) is becoming more and more widespread. Microservices is a software architecture that splits an application into a set of small, independent services. Each service is built around a specific business function and can be independently developed, deployed and expanded. The application of microservices can improve development efficiency, enhance the scalability and maintainability of the system, etc.

[0003] In the process of running microservices, it is inevitable to execute delayed tasks. In current technology, there are three common ways to execute delayed tasks for microservices. The first is to implement delayed tasks for microservices based on the JDK timer mechanism; the second is to implement delayed tasks for microservices based on the ordered set of Redis / Redisson; the third is to implement delayed tasks for microservices based on the MQ (Message Queue) message queue.

[0004] In summary, the execution method of delayed tasks of microservices in the current technology cannot guarantee the uniqueness of delayed task execution and has the problem of delayed task execution time lag. Summary of the invention

[0005] Based on the above problems, the present application provides a delayed task execution method and device, which can avoid time lag in delayed task execution while ensuring the uniqueness of delayed task execution.

[0006] The embodiments of the present application disclose the following technical solutions:

[0007] In a first aspect, the present application provides a delayed task execution method, which is applied to a microservice instance, and the method includes:

[0008] Obtaining stored basic information of delayed tasks from distributed storage; the basic information of delayed tasks includes: the delay time of the delayed tasks and a unique identifier of the delayed tasks;

[0009] Based on the delay time of the delayed task, storing the unique identifier of the delayed task in the time wheel;

[0010] Based on the time wheel, when it is determined that the current time reaches the execution time of the delayed task, the unique identifier of the corresponding delayed task in the time wheel is read;

[0011] Based on the unique identifier of the corresponding delayed task, the execution of the corresponding delayed task is triggered.

[0012] Optionally, when determining, based on the time wheel, that the current time reaches the execution time of the delayed task, reading the unique identifier of the corresponding delayed task in the time wheel includes:

[0013] As the timing wheel rotates, when the timing wheel rotates to the slot storing the unique identifier of the delayed task, it is determined that the current time reaches the execution time of the delayed task, and the unique identifier of the corresponding delayed task in the timing wheel is read.

[0014] Optionally, the time wheel includes: a day time wheel, an hour time wheel, a minute time wheel and a second time wheel; based on the delay time of the delay task, storing the unique identifier of the delay task in the time wheel includes:

[0015] Based on the delay time of the delay task, determine the first delay time, the second delay time, the third delay time and the fourth delay time of the delay task; wherein the first delay time is the delay time in days, the second delay time is the delay time in hours, the third delay time is the delay time in minutes, and the fourth delay time is the delay time in seconds;

[0016] Based on the first delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the day time wheel; based on the second delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the hour time wheel; based on the third delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the minute time wheel; based on the fourth delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the second time wheel;

[0017] As the time wheel rotates, when the time wheel rotates to the slot storing the unique identifier of the delayed task, determining that the current time reaches the execution time of the delayed task includes:

[0018] As the day time wheel rotates, when the day time wheel rotates to the card slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the card slot of the day time wheel;

[0019] In response to removing the unique identifier of the delayed task from the card slot of the day time wheel, the hour time wheel starts to rotate, and as the hour time wheel rotates, when the hour time wheel rotates to the card slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the card slot of the hour time wheel;

[0020] In response to the unique identifier of the delayed task being removed from the card slot of the time wheel, the time-dividing wheel starts to rotate, and as the time-dividing wheel rotates, when the time-dividing wheel rotates to the card slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the card slot of the time-dividing wheel;

[0021] In response to removing the unique identifier of the delayed task from the slot of the split time wheel, the second time wheel starts to rotate. As the second time wheel rotates, when the second time wheel rotates to the slot that stores the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the second time wheel, and it is determined that the current time reaches the execution time of the delayed task.

[0022] Optionally, before triggering the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task, the method further includes:

[0023] Sending a creation request for a distributed lock corresponding to the delayed task to the distributed storage, so that the distributed storage responds to the creation request and determines whether there is a distributed lock corresponding to the delayed task. When the distributed storage determines that there is no distributed lock corresponding to the delayed task, it creates a distributed lock corresponding to the delayed task and returns a creation success message to the microservice instance;

[0024] The triggering the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task includes:

[0025] When the creation success information returned by the distributed storage is received, the execution of the corresponding delayed task is triggered based on the unique identifier of the corresponding delayed task.

[0026] Optionally, after triggering the execution of the corresponding delayed task, the method further includes:

[0027] When the delayed task is executed, the delayed task is deleted from the time wheel, and a deletion request for the delayed task is sent to the distributed storage, so that the distributed storage responds to the deletion request and deletes the corresponding delayed task basic information and the distributed lock corresponding to the delayed task.

[0028] Optionally, the method further includes:

[0029] When the creation failure information returned by the distributed storage is received, the execution of the corresponding delayed task is not triggered; the creation failure information is returned to the microservice instance when the distributed storage determines that the distributed lock corresponding to the delayed task already exists.

[0030] Optionally, when the execution of the corresponding delayed task is not triggered, the method further includes:

[0031] Recording the overtime time of the delayed task;

[0032] When the overdue time exceeds the preset time, the delayed task is deleted from the time wheel.

[0033] Optionally, the method further includes:

[0034] When an abnormal situation occurs during the execution of the delayed task, the abnormal information corresponding to the abnormal situation of the delayed task is recorded in the log, so as to re-trigger the execution of the delayed task based on the abnormal information recorded in the log.

[0035] Optionally, the acquiring the stored basic information of the delayed task from the distributed storage includes:

[0036] In response to starting or restarting the microservice instance, obtaining stored basic information of delayed tasks from distributed storage;

[0037] Or, when it is monitored that new delayed task basic information is added in the distributed storage, the stored new delayed task basic information is obtained from the distributed storage.

[0038] In a second aspect, the present application provides a delayed task execution device, which is applied to a microservice instance, and the device includes:

[0039] A basic information acquisition module is used to acquire the stored basic information of the delayed task from the distributed storage; the basic information of the delayed task includes: the delay time of the delayed task and the unique identifier of the delayed task;

[0040] A delayed task storage module, used for storing a unique identifier of the delayed task in a time wheel based on the delayed time of the delayed task;

[0041] A delayed task reading module, used for reading the unique identifier of the corresponding delayed task in the time wheel when determining that the current time reaches the execution time of the delayed task based on the time wheel;

[0042] The delayed task triggering module is used to trigger the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task.

[0043] Optionally, the delayed task reading module is specifically used to determine that the current time reaches the execution time of the delayed task and read the corresponding unique identifier of the delayed task in the time wheel as the time wheel rotates to the card slot storing the unique identifier of the delayed task.

[0044] Optionally, the time wheel includes: a day time wheel, an hour time wheel, a minute time wheel and a second time wheel.

[0045] The delayed task storage module is specifically used to determine the first delay time, the second delay time, the third delay time and the fourth delay time of the delayed task based on the delay time of the delayed task; wherein the first delay time is the delay time in days, the second delay time is the delay time in hours, the third delay time is the delay time in minutes, and the fourth delay time is the delay time in seconds; based on the first delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the day time wheel; based on the second delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the hour time wheel; based on the third delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the minute time wheel; based on the fourth delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the second time wheel.

[0046] The delayed task reading module is specifically used for, as the day time wheel rotates, when the day time wheel rotates to the card slot storing the unique identifier of the delayed task, removing the unique identifier of the delayed task from the card slot of the day time wheel; in response to the unique identifier of the delayed task being removed from the card slot of the day time wheel, the hour time wheel starts to rotate, and as the hour time wheel rotates, when the hour time wheel rotates to the card slot storing the unique identifier of the delayed task, removing the unique identifier of the delayed task from the card slot of the hour time wheel; in response to the unique identifier of the delayed task being removed from the card slot of the day time wheel The unique identifier of the delayed task is removed from the slot of the time-dividing wheel, and the time-dividing wheel starts to rotate. As the time-dividing wheel rotates, when the time-dividing wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the time-dividing wheel; in response to the unique identifier of the delayed task being removed from the slot of the time-dividing wheel, the second time wheel starts to rotate. As the second time wheel rotates, when the second time wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the second time wheel, and it is determined that the current time reaches the execution time of the delayed task.

[0047] Optionally, the delayed task execution device also includes: a distributed lock creation module, which is used to send a creation request for a distributed lock corresponding to the delayed task to the distributed storage, so that the distributed storage responds to the creation request and determines whether there is a distributed lock corresponding to the delayed task. When the distributed storage determines that there is no distributed lock corresponding to the delayed task, it creates a distributed lock corresponding to the delayed task and returns a creation success message to the microservice instance; a delayed task triggering module, which is specifically used to trigger the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task when receiving the creation success message returned by the distributed storage.

[0048] Optionally, the delayed task execution device also includes: a delayed task deletion module, which is used to delete the delayed task from the time wheel after the delayed task is executed, and send a deletion request for the delayed task to the distributed storage, so that the distributed storage responds to the deletion request and deletes the corresponding delayed task basic information and the distributed lock corresponding to the delayed task.

[0049] Optionally, the delayed task execution device also includes: a delayed task failure module, which is used to not trigger the execution of the corresponding delayed task when receiving the creation failure information returned by the distributed storage; the creation failure information is returned to the microservice instance when the distributed storage determines that it already has a distributed lock corresponding to the delayed task.

[0050] Optionally, the delayed task execution device also includes: an overdue time recording module, used to record the overdue time of the delayed task; and an overdue task deletion module, used to delete the delayed task from the time wheel when the overdue time exceeds a preset time.

[0051] Optionally, the delayed task execution device also includes: an exception information recording module, which is used to record the exception information corresponding to the abnormal situation of the delayed task into a log when an abnormal situation occurs during the execution of the delayed task, so as to re-trigger the execution of the delayed task based on the abnormal information recorded in the log.

[0052] Optionally, a basic information acquisition module is specifically used to obtain the stored basic information of delayed tasks from distributed storage in response to the startup or restart of a microservice instance; or, when monitoring the addition of new basic information of delayed tasks in distributed storage, obtain the stored new basic information of delayed tasks from distributed storage.

[0053] Compared with the prior art, the present application has the following beneficial effects: the unique identifier of the delayed task is stored in the corresponding time wheel, and whether the delayed task has reached the execution time is determined based on the time wheel. If the execution time of the delayed task is reached, the microservice instance triggers the execution of the delayed task based on reading the unique identifier of the delayed task from the time wheel. That is, after the embodiment of the present application records the delay time based on the time wheel, it triggers other threads to execute the delayed task, and can process multiple delayed tasks in parallel without causing a backlog of delayed task information and a delay in the execution time of the delayed task. Furthermore, the uniqueness of the delayed task execution can be guaranteed through distributed storage. BRIEF DESCRIPTION OF THE DRAWINGS

[0054] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. 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 paying creative labor.

[0055] Figure 1 A flowchart of a delayed task execution method provided in an embodiment of the present application;

[0056] Figure 2 A schematic diagram of a time wheel storing a delay task provided in an embodiment of the present application;

[0057] Figure 3 An interactive schematic diagram of a delayed task execution right contention process provided in an embodiment of the present application;

[0058] Figure 4 A schematic diagram of the structure of a delayed task execution system provided in an embodiment of the present application;

[0059] Figure 5 A schematic diagram of the structure of a delayed task execution device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0060] As described above, there are three common ways to execute delayed tasks of microservices in current technology. The following briefly introduces the execution methods of delayed tasks of three microservices in current technology.

[0061] The first is to implement delayed task execution of microservices based on the JDK timer mechanism. JDK timer is a simple timer scheduler provided by the Java standard library, which can execute tasks after a specified time interval (i.e., execute delayed tasks). However, the JDK timer mechanism only supports single services and cannot guarantee the uniqueness of delayed task execution under the microservice framework. Monolithic architecture refers to a service that integrates all functional modules of an application; while microservice refers to a service that differentiates an application into multiple small, autonomous services. The JDK timer mechanism only supports monolithic services. When applied to microservices, one microservice may correspond to multiple microservice instances, and the uniqueness of the delayed task executed by the microservice instance cannot be guaranteed. For example, for service A, in a monolithic service, only service A executes the delayed task, and there is no problem of uniqueness of delayed task execution; in microservices, service A corresponds to microservice instance A1, microservice instance A2, and microservice instance A3. When executing a delayed task based on the JDK timer mechanism, it is possible that when microservice instance A1 executes the delayed task, there is also microservice instance A2 executing the delayed task, and the uniqueness of delayed task execution cannot be guaranteed.

[0062] The second is to implement delayed task execution of microservices based on Redis / Redisson ordered sets. Specifically, the delayed time of the delayed task is used as the score (score) using the Redis ordered set structure, and the delayed task content is stored as the value in the ordered set, and the delayed task is executed serially based on the score arrangement order of the ordered set. Although the uniqueness of delayed task execution in the microservice framework can be guaranteed, it is easy to have message backlogs that cause the execution time of delayed tasks to lag. For example: in an ordered set, when there are multiple delayed tasks that need to be executed at or near a score, they can only be executed serially, which will cause message backlogs and cause subsequent delayed tasks to lag in execution time.

[0063] The third is to implement delayed task execution of microservices based on MQ (Message Queue) message queue. The delayed task execution method of message queue is similar to the ordered set of Redis / Redisson. Delayed tasks are arranged in chronological order in the message queue, which is prone to message backlogs leading to delayed execution of delayed tasks. Furthermore, to implement delayed task execution of microservices based on MQ, it is necessary to install relevant plug-ins and provide relevant installation environment. For example, RabbitMQ is installed to implement the installation of rabbitmq_delayed_message_exchang plug-in, which makes the implementation method very complicated, and RabbitMQ occupies more CPU resources, resulting in CPU performance bottleneck.

[0064] The present application provides a delayed task execution method, including: obtaining the stored basic information of the delayed task from the distributed storage; based on the delay time of the delayed task, storing the unique identifier of the delayed task in the time wheel; based on the time wheel, when determining that the current time reaches the execution time of the delayed task, reading the unique identifier of the corresponding delayed task in the time wheel; based on the unique identifier of the corresponding delayed task, triggering the execution of the corresponding delayed task. The unique identifier of the delayed task is stored in the corresponding time wheel, and it is determined whether the delayed task has reached the execution time based on the time wheel. If the execution time of the delayed task is reached, the microservice instance triggers the execution of the delayed task based on the unique identifier of the delayed task read from the time wheel, that is, after recording the delay time based on the time wheel, the present application triggers other threads to execute the delayed task, and can process multiple delayed tasks in parallel, without causing a backlog of delayed task information, resulting in a delay in the execution time of the delayed task. Furthermore, the uniqueness of the delayed task execution can be guaranteed through distributed storage.

[0065] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings 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.

[0066] Embodiment 1:

[0067] Combine the following Figure 1-Figure 3 , a delayed task execution method provided by an embodiment of the present application is described in detail. A delayed task execution method provided by an embodiment of the present application is applied to a microservice instance, comprising the following steps:

[0068] S101. Obtain stored basic information of delayed tasks from distributed storage.

[0069] Among them, the distributed storage includes: Redis, Zookeeper, ETCD, etc. It is used to store the basic information of all delayed tasks corresponding to the microservice. Furthermore, the microservice instance obtains the basic information of the delayed tasks corresponding to the microservice from the distributed storage. For example: the basic information of delayed tasks of microservices A, B, and C is stored in the distributed storage, and the microservice instance is instance A1 of microservice A, then the microservice instance A1 obtains the basic information of delayed tasks corresponding to microservice A from the distributed storage.

[0070] The basic information of the delayed task includes: the delayed time of the delayed task and the unique identifier of the delayed task. The unique identifier of the delayed task may be the name of the delayed task, the ID of the delayed task, etc., which is not specifically limited in this application.

[0071] In a possible implementation, in response to the startup or restart of the microservice instance, the stored basic information of the delayed task is obtained from the distributed storage. When the microservice instance is started or restarted, there is no relevant information of the delayed task in the microservice instance. At this time, it is necessary to obtain the stored corresponding basic information of the delayed task from the distributed storage. Distributed storage can ensure the consistency of delayed tasks in multiple microservice instances.

[0072] In another possible implementation, when the microservice instance monitors that new delayed task basic information is added to the distributed storage, the stored new delayed task basic information is obtained from the distributed storage. When adding new delayed task information, new delayed tasks are added to other microservice instances of the microservice corresponding to the microservice instance, and the new delayed task basic information is added to the distributed storage. The microservice instance monitors that new delayed task basic information is added to the distributed storage, and obtains the stored new delayed task basic information from the distributed storage to implement the delayed task addition operation. Exemplarily, the microservice instance adds new delayed tasks to microservice instances A1 and A2 corresponding to microservice A, then the new delayed task basic information is added to the distributed storage, and microservice instance A1 monitors that new delayed task basic information is added to the distributed storage, and obtains the stored new delayed task basic information from the distributed storage.

[0073] S102: Based on the delay time of the delayed task, store the unique identifier of the delayed task in the time wheel.

[0074] In a possible implementation, the time wheel includes a day time wheel, an hour time wheel, a minute time wheel and a second time wheel. Based on the delay time of the delayed task, the first delay time, the second delay time, the third delay time and the fourth delay time of the delayed task are determined. Among them, the first delay time is the delay time in days, the second delay time is the delay time in hours, the third delay time is the delay time in minutes, and the fourth delay time is the delay time in seconds. Based on the first delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the day time wheel; based on the second delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the hour time wheel; based on the third delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the minute time wheel; based on the fourth delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the second time wheel.

[0075] For ease of understanding, the following example introduces the storage process of the unique identifier of the delayed task. The delay time of the delayed task is 8 days, 3 hours, 12 minutes and 36 seconds, so the first delay time is 8 days, the second delay time is 3 hours, the third delay time is 12 minutes, and the fourth delay time is 36 seconds.

[0076] The day time wheel, hour time wheel, minute time wheel and second time wheel all start from slot 0. Figure 2 As shown in (a), the unique identifier of the delayed task is added to the slot 7 of the day time wheel, the unique identifier of the delayed task is added to the slot 2 of the hour time wheel, the unique identifier of the delayed task is added to the slot 11 of the minute time wheel, and the unique identifier of the delayed task is added to the slot 35 of the second time wheel.

[0077] Further, if Figure 2 As shown in (a), taking the time-dividing wheel as an example, each minute corresponds to a card slot, so the unique identifier of the delayed task is stored in the card slot No. 11 of the time-dividing wheel.

[0078] Furthermore, in a possible implementation method, the microservice instance monitors that new delayed task basic information is added to the distributed storage. After obtaining the stored new delayed task basic information from the distributed storage, the microservice instance determines whether the new delayed task basic information already exists in itself. If so, the unique identifier of the new delayed task is stored in the corresponding slot of the timing wheel; if not, the unique identifier of the new delayed task is not stored in the corresponding slot of the timing wheel.

[0079] S103: When it is determined based on the time wheel that the current time reaches the execution time of the delayed task, the unique identifier of the corresponding delayed task in the time wheel is read.

[0080] Specifically, as the time wheel rotates, when the time wheel rotates to the slot storing the unique identifier of the delayed task, it is determined that the current time reaches the execution time of the delayed task, and the unique identifier of the corresponding delayed task in the time wheel is read.

[0081] In a possible implementation, the time wheel includes a day time wheel, an hour time wheel, a minute time wheel, and a second time wheel. As the day time wheel rotates, when the day time wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the day time wheel; in response to the unique identifier of the delayed task being removed from the slot of the day time wheel, the hour time wheel starts to rotate, and as the hour time wheel rotates, when the hour time wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the hour time wheel; in response to the unique identifier of the delayed task being removed from the slot of the hour time wheel, the minute time wheel starts to rotate, As the dividing time wheel rotates, when the dividing time wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the dividing time wheel; in response to the removal of the unique identifier of the delayed task from the slot of the dividing time wheel, the second time wheel starts to rotate, and as the second time wheel rotates, when the second time wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the second time wheel, it is determined that the current time reaches the execution time of the delayed task, and the unique identifier of the delayed task is read.

[0082] S104: triggering the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task.

[0083] Exemplarily, based on the unique identifier of the corresponding delayed task, the corresponding execution thread is called to execute the delayed task.

[0084] In a possible implementation, before triggering the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task, the microservice instance needs to compete for the execution right of the delayed task. Specifically, the uniqueness of the delayed task execution can be achieved through a distributed lock.

[0085] Among them, a distributed lock is a synchronization mechanism for implementing resource access in a distributed system. Due to the characteristics of a distributed system, multiple nodes accessing shared resources at the same time may cause data inconsistency or conflict. Distributed locks coordinate operations between nodes to ensure that only one node can obtain the lock at any time, thereby achieving mutually exclusive access to resources.

[0086] For ease of understanding, the following Figure 3 How to compete for the execution right of the delayed task is introduced in detail. The microservice instance takes the instance B1 of microservice B as an example, and the microservice B includes microservice instance B1, microservice instance B2 and microservice instance B3, and the delayed task takes the delayed task b as an example.

[0087] S301. Microservice instance B1 sends a request to create a distributed lock corresponding to delayed task b to distributed storage.

[0088] Microservice instance B1 sends a request to create a distributed lock corresponding to delayed task b to the distributed storage, which can be understood as microservice instance B1 requesting to compete for the execution right of delayed task b.

[0089] S302: In response to the creation request, the distributed storage determines whether there is a distributed lock corresponding to the delayed task b.

[0090] When the distributed storage determines that there is no distributed lock corresponding to delayed task b, S303-S305 is performed. If the distributed storage does not have a distributed lock corresponding to delayed task b, it means that no other microservice instance (microservice instance B2 and microservice instance B3) has the execution right of delayed task b, that is, no other microservice instance executes delayed task b.

[0091] When the distributed storage determines that there is a distributed lock corresponding to the delayed task b, S306 is performed. If there is a distributed lock corresponding to the delayed task b in the distributed storage, it means that there are other microservice instances (microservice instance B2 and microservice instance B3) that have the execution right of the delayed task b, that is, there are other microservice instances that execute the delayed task b.

[0092] S303: Distributed storage creates a distributed lock corresponding to delayed task b.

[0093] When it is determined that there is no distributed lock corresponding to the delayed task b, a distributed lock corresponding to the delayed task b is created, indicating that the execution right of the delayed task b is allocated to the microservice instance B1.

[0094] S304. Distributed storage returns creation success information to microservice instance B1.

[0095] S305. When the microservice instance B1 receives the creation success information returned by the distributed storage, it triggers the execution of the delayed task b based on the unique identifier of the corresponding delayed task b.

[0096] When microservice instance B1 receives the creation success information, it indicates that microservice instance B1 has successfully obtained the execution right of delayed task b, and the execution of delayed task b is triggered.

[0097] Further, after S305 is executed, microservice instance B1 executes delayed task b. After delayed task b is executed, delayed task b is deleted from the time wheel, and a deletion request for delayed task b is sent to the distributed storage; the distributed storage responds to the deletion request of delayed task b, deletes the delayed task basic information of delayed task b and the distributed lock corresponding to delayed task b.

[0098] S306. The distributed storage returns creation failure information to the microservice instance B1.

[0099] When it is determined that there is a distributed lock corresponding to the delayed task b, it means that the execution right of the delayed task b has been allocated to other microservice instances, and the microservice instance B1 cannot successfully obtain the execution right of the delayed task b.

[0100] S307: When microservice instance B1 receives the creation failure information returned by the distributed storage, the execution of delayed task b is not triggered.

[0101] When microservice instance B1 receives the creation failure information, it indicates that microservice instance B1 has not successfully obtained the execution right of delayed task b, and the execution of delayed task b is not triggered.

[0102] Further, when S307 is completed and microservice instance B1 does not execute delayed task b, the overtime time of delayed task b is recorded; when the overtime time of delayed task b exceeds the preset time, the delayed task is deleted from the time wheel. That is, if microservice instance B1 does not execute delayed task b, then for microservice instance B1, delayed task b is overdue and not executed, so the overtime time of delayed task b is recorded. When the overtime time is greater than the preset time, delayed task b is an invalid task for microservice instance B1, and the delayed task b is deleted from the time wheel to prevent the relevant information of the invalid task from occupying the memory of microservice instance B1.

[0103] The above combination Figure 3 An example is given to introduce how to compete for the execution right of a delayed task and the subsequent execution process. The following will continue to introduce other situations that may be involved in a delayed task execution method provided in an embodiment of the present application.

[0104] In a possible implementation, due to the downtime or long-term outage of all microservice instances corresponding to the microservice, delayed tasks corresponding to the microservice stored in the distributed storage are stored with delayed tasks that have expired and have not been executed (hereinafter referred to as expired tasks). At this time, it is necessary to delete the expired tasks from the distributed storage to prevent the expired tasks from affecting the execution of other delayed tasks corresponding to the microservice that have not expired. Exemplarily, if the distributed storage is Redis, the TTL (Time To Live) mechanism of Redis can be used to automatically delete the expired tasks from Redis.

[0105] In a possible implementation, when an abnormal situation occurs during the execution of a delayed task by a microservice instance, the abnormal information corresponding to the abnormal situation of the delayed task is recorded in a log, so that the delayed task can be re-executed based on the abnormal information recorded in the log. For example: a microservice instance automatically sends a text message to user F after 1 hour. Because the platform or interface for automatically sending text messages expires, the microservice instance fails to send text messages to user F. Then, the abnormal information corresponding to the failure to send text messages to user F is recorded in the log. According to the abnormal information recorded in the log, the execution of sending text messages to user F can be re-triggered (for example, manually sending text messages to user F).

[0106] A delayed task execution method provided by an embodiment of the present application includes: obtaining stored basic information of delayed tasks from distributed storage; based on the delay time of the delayed task, storing the unique identifier of the delayed task in the time wheel; based on the time wheel, when determining that the current time reaches the execution time of the delayed task, reading the unique identifier of the corresponding delayed task in the time wheel; based on the unique identifier of the corresponding delayed task, triggering the execution of the corresponding delayed task. The unique identifier of the delayed task is stored in the corresponding time wheel, and it is determined whether the delayed task has reached the execution time based on the time wheel. If the execution time of the delayed task is reached, the microservice instance triggers the execution of the delayed task based on the unique identifier of the delayed task read from the time wheel, that is, after the embodiment of the present application records the delay time based on the time wheel, it triggers other threads to execute the delayed task, and can process multiple delayed tasks in parallel, without causing a backlog of delayed task information, resulting in a delay in the execution time of the delayed task. Further, the uniqueness of the delayed task execution can be guaranteed through distributed storage.

[0107] Furthermore, the delayed tasks that have not been executed overdue are deleted from the microservice instance and distributed storage in a timely manner to avoid the delayed tasks that have not been executed overdue occupying memory and to avoid the delayed tasks that have not been executed overdue affecting the execution of other delayed tasks that have not expired.

[0108] Furthermore, when an abnormal situation occurs during the execution of the delayed task, the abnormal information corresponding to the abnormal situation of the delayed task is recorded in the log, so as to re-trigger the execution of the abnormal delayed task based on the log later.

[0109] Embodiment 2:

[0110] Combine the following Figure 4 and Figure 5 , a delayed task execution device provided in an embodiment of the present application is introduced in detail.

[0111] First combine Figure 4, introduce a delayed task execution system provided by the embodiment of the present application, including: a microservice instance 400, a distributed storage 600 and a database 700. The microservice instance 400 includes a delayed task execution device 500. It should be noted that in the embodiment of the present application, only one microservice instance is used as an example to introduce the delayed task execution system, and the present application does not make specific limitations.

[0112] Among them, the distributed storage 600 is used to store the basic information of all delayed tasks corresponding to the microservices, and to achieve the uniqueness of delayed task execution.

[0113] The database 700 is used to store logs.

[0114] The microservice instance 400 is used to implement the corresponding business function. The delayed task execution device 500 is used to execute a delayed task execution method provided by the present application.

[0115] Specifically, the structure of the delayed task execution device 500 is as follows: Figure 5 As shown, the delayed task execution device 500 includes:

[0116] The basic information acquisition module 501 is used to acquire the stored basic information of the delayed task from the distributed storage 600;

[0117] The delayed task storage module 502 is used to store the unique identifier of the delayed task in the time wheel based on the delayed time of the delayed task;

[0118] The delayed task reading module 503 is used to read the unique identifier of the corresponding delayed task in the time wheel when determining that the current time reaches the execution time of the delayed task based on the time wheel;

[0119] The delayed task triggering module 504 is used to trigger the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task.

[0120] Furthermore, the delayed task reading module 503 is specifically used to determine that the current time reaches the execution time of the delayed task and read the unique identifier of the corresponding delayed task in the time wheel as the time wheel rotates to the slot storing the unique identifier of the delayed task.

[0121] Furthermore, the time wheel includes: a day time wheel, an hour time wheel, a minute time wheel and a second time wheel.

[0122] The delayed task storage module 502 is specifically used to determine the first delay time, the second delay time, the third delay time and the fourth delay time of the delayed task based on the delay time of the delayed task; wherein the first delay time is the delay time in days, the second delay time is the delay time in hours, the third delay time is the delay time in minutes, and the fourth delay time is the delay time in seconds; based on the first delay time, the unique identifier of the delayed task is stored in the corresponding slot of the day time wheel; based on the second delay time, the unique identifier of the delayed task is stored in the corresponding slot of the hour time wheel; based on the third delay time, the unique identifier of the delayed task is stored in the corresponding slot of the minute time wheel; based on the fourth delay time, the unique identifier of the delayed task is stored in the corresponding slot of the second time wheel.

[0123] The delayed task reading module 503 is specifically used for, as the day time wheel rotates, when the day time wheel rotates to the card slot storing the unique identifier of the delayed task, removing the unique identifier of the delayed task from the card slot of the day time wheel; in response to the unique identifier of the delayed task being removed from the card slot of the day time wheel, the hour time wheel starts to rotate, and as the hour time wheel rotates, when the hour time wheel rotates to the card slot storing the unique identifier of the delayed task, removing the unique identifier of the delayed task from the card slot of the hour time wheel; in response to the unique identifier of the delayed task being removed from the card slot of the day time wheel The unique identifier of the delayed task is removed from the slot of the time-dividing wheel, and the time-dividing wheel starts to rotate. As the time-dividing wheel rotates, when the time-dividing wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the time-dividing wheel; in response to the unique identifier of the delayed task being removed from the slot of the time-dividing wheel, the second time wheel starts to rotate. As the second time wheel rotates, when the second time wheel rotates to the slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the second time wheel, and it is determined that the current time reaches the execution time of the delayed task.

[0124] Furthermore, the delayed task execution device 500 also includes: a distributed lock creation module, which is used to send a creation request for a distributed lock corresponding to the delayed task to the distributed storage 600, so that the distributed storage 600 responds to the creation request and determines whether there is a distributed lock corresponding to the delayed task. When the distributed storage 600 determines that there is no distributed lock corresponding to the delayed task, it creates a distributed lock corresponding to the delayed task and returns a creation success message to the microservice instance 400; a delayed task triggering module 504, which is specifically used to trigger the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task when receiving the creation success message returned by the distributed storage 600.

[0125] Furthermore, the delayed task execution device 500 also includes: a delayed task deletion module, which is used to delete the delayed task from the time wheel after the delayed task is executed, and send a deletion request for the delayed task to the distributed storage 600, so that the distributed storage 600 responds to the deletion request and deletes the corresponding delayed task basic information and the distributed lock corresponding to the delayed task.

[0126] Furthermore, the delayed task execution device 500 also includes: a delayed task failure module, which is used to not trigger the execution of the corresponding delayed task when receiving the creation failure information returned by the distributed storage 600; the creation failure information is returned to the microservice instance 400 when the distributed storage 600 determines that the distributed lock corresponding to the delayed task already exists.

[0127] Furthermore, the delayed task execution device 500 also includes: an overdue time recording module for recording the overdue time of the delayed task; and an overdue task deletion module for deleting the delayed task from the time wheel when the overdue time exceeds a preset time.

[0128] Furthermore, the delayed task execution device 500 also includes: an exception information recording module, which is used to record the exception information corresponding to the abnormal situation of the delayed task into a log when an abnormal situation occurs during the execution of the delayed task, so as to re-trigger the execution of the delayed task based on the abnormal information recorded in the log.

[0129] Furthermore, the basic information acquisition module 501 is specifically used to obtain the stored basic information of the delayed task from the distributed storage 600 in response to the startup or restart of the microservice instance 400; or, when monitoring the addition of new basic information of the delayed task in the distributed storage 600, obtain the stored new basic information of the delayed task from the distributed storage 600.

[0130] A delayed task execution device 500 provided in an embodiment of the present application includes: a basic information acquisition module 501, which is used to obtain the stored basic information of the delayed task from the distributed storage 600; a delayed task storage module 502, which is used to store the unique identifier of the delayed task in the time wheel based on the delay time of the delayed task; a delayed task reading module 503, which is used to read the unique identifier of the corresponding delayed task in the time wheel when the current time reaches the execution time of the delayed task based on the time wheel; a delayed task triggering module 504, which is used to trigger the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task. After recording the delay time based on the time wheel, the embodiment of the present application triggers other threads to execute the delayed task, and can process multiple delayed tasks in parallel, without causing a backlog of delayed task information, resulting in a delay in the execution time of the delayed task. Further, the uniqueness of the delayed task execution can be guaranteed through the distributed storage 600.

[0131] Furthermore, the delayed tasks that have not been executed for a certain period of time are deleted from the microservice instance 400 and the distributed storage 600 in a timely manner, so as to avoid the delayed tasks that have not been executed for a certain period of time from occupying the memory and affecting the execution of other delayed tasks that have not expired.

[0132] Furthermore, when an abnormal situation occurs during the execution of the delayed task, the abnormal information corresponding to the abnormal situation of the delayed task is recorded in the log, so as to re-trigger the execution of the abnormal delayed task based on the log later.

[0133] It should be noted that each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device and system embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments. The device and system embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components indicated as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0134] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed in the present application should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

Claims

1. A delayed task execution method, characterized in that: Applied to a microservice instance, the method includes: Obtaining stored basic information of delayed tasks from distributed storage; the basic information of delayed tasks includes: the delay time of the delayed tasks and a unique identifier of the delayed tasks; Based on the delay time of the delayed task, storing the unique identifier of the delayed task in the time wheel; Based on the time wheel, when it is determined that the current time reaches the execution time of the delayed task, the unique identifier of the corresponding delayed task in the time wheel is read; Based on the unique identifier of the corresponding delayed task, the execution of the corresponding delayed task is triggered.

2. The method according to claim 1, characterized in that When determining based on the time wheel that the current time reaches the execution time of the delayed task, reading the unique identifier of the corresponding delayed task in the time wheel includes: As the timing wheel rotates, when the timing wheel rotates to the slot storing the unique identifier of the delayed task, it is determined that the current time reaches the execution time of the delayed task, and the unique identifier of the corresponding delayed task in the timing wheel is read.

3. The method according to claim 2, characterized in that The time wheel includes: a day time wheel, an hour time wheel, a minute time wheel and a second time wheel; based on the delay time of the delay task, storing the unique identifier of the delay task in the time wheel includes: Based on the delay time of the delay task, determine the first delay time, the second delay time, the third delay time and the fourth delay time of the delay task; wherein the first delay time is the delay time in days, the second delay time is the delay time in hours, the third delay time is the delay time in minutes, and the fourth delay time is the delay time in seconds; Based on the first delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the day time wheel; based on the second delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the hour time wheel; based on the third delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the minute time wheel; based on the fourth delay time, the unique identifier of the delayed task is stored in the corresponding card slot of the second time wheel; As the time wheel rotates, when the time wheel rotates to the slot storing the unique identifier of the delayed task, determining that the current time reaches the execution time of the delayed task includes: As the day time wheel rotates, when the day time wheel rotates to the card slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the card slot of the day time wheel; In response to removing the unique identifier of the delayed task from the card slot of the day time wheel, the hour time wheel starts to rotate, and as the hour time wheel rotates, when the hour time wheel rotates to the card slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the card slot of the hour time wheel; In response to the unique identifier of the delayed task being removed from the card slot of the time wheel, the time-dividing wheel starts to rotate, and as the time-dividing wheel rotates, when the time-dividing wheel rotates to the card slot storing the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the card slot of the time-dividing wheel; In response to removing the unique identifier of the delayed task from the slot of the split time wheel, the second time wheel starts to rotate. As the second time wheel rotates, when the second time wheel rotates to the slot that stores the unique identifier of the delayed task, the unique identifier of the delayed task is removed from the slot of the second time wheel, and it is determined that the current time reaches the execution time of the delayed task.

4. The method according to claim 1, characterized in that: Before triggering the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task, the method further includes: Sending a creation request for a distributed lock corresponding to the delayed task to the distributed storage, so that the distributed storage responds to the creation request and determines whether there is a distributed lock corresponding to the delayed task. When the distributed storage determines that there is no distributed lock corresponding to the delayed task, it creates a distributed lock corresponding to the delayed task and returns a creation success message to the microservice instance; The triggering the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task includes: When the creation success information returned by the distributed storage is received, the execution of the corresponding delayed task is triggered based on the unique identifier of the corresponding delayed task.

5. The method according to claim 4, characterized in that After triggering the execution of the corresponding delayed task, the method further includes: When the delayed task is executed, the delayed task is deleted from the time wheel, and a deletion request for the delayed task is sent to the distributed storage, so that the distributed storage responds to the deletion request and deletes the corresponding delayed task basic information and the distributed lock corresponding to the delayed task.

6. The method according to claim 4, characterized in that The method further comprises: When the creation failure information returned by the distributed storage is received, the execution of the corresponding delayed task is not triggered; the creation failure information is returned to the microservice instance when the distributed storage determines that the distributed lock corresponding to the delayed task already exists.

7. The method according to claim 6, characterized in that When the execution of the corresponding delayed task is not triggered, the method further includes: Recording the overtime time of the delayed task; When the overdue time exceeds the preset time, the delayed task is deleted from the time wheel.

8. The method according to claim 1, characterized in that The method further comprises: When an abnormal situation occurs during the execution of the delayed task, the abnormal information corresponding to the abnormal situation of the delayed task is recorded in the log, so as to re-trigger the execution of the delayed task based on the abnormal information recorded in the log.

9. The method according to claim 1, characterized in that: The step of obtaining the stored basic information of the delayed task from the distributed storage includes: In response to starting or restarting the microservice instance, obtaining stored basic information of delayed tasks from distributed storage; Or, when it is monitored that new delayed task basic information is added in the distributed storage, the stored new delayed task basic information is obtained from the distributed storage.

10. A delayed task execution device, characterized in that: Applied to a microservice instance, the device comprises: A basic information acquisition module is used to acquire the stored basic information of the delayed task from the distributed storage; the basic information of the delayed task includes: the delay time of the delayed task and the unique identifier of the delayed task; A delayed task storage module, used for storing a unique identifier of the delayed task in a time wheel based on the delayed time of the delayed task; A delayed task reading module, used for reading the unique identifier of the corresponding delayed task in the time wheel when determining that the current time reaches the execution time of the delayed task based on the time wheel; The delayed task triggering module is used to trigger the execution of the corresponding delayed task based on the unique identifier of the corresponding delayed task.

Citation Information

Cited By

  • MQ-based general delay task solving method

    CN120723505A

  • A general delay task solving method based on MQ

    CN120723505B