A method and device for processing delayed tasks

Delayed tasks are managed through a multi-layer time wheel structure, which solves the problems of inaccurate execution of delayed tasks and waste of resources in the existing technology, and achieves efficient and accurate delayed task management and execution.

CN114296902BActive Publication Date: 2025-01-24SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111650581.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-30
Publication Date
2025-01-24
Estimated Expiration
2041-12-30

AI Technical Summary

Technical Problem

When processing delay tasks, the implementation scheme of timed task scanning database results in inaccurate execution time of delayed tasks, inefficient execution, and waste of resources.

Method used

The multi -layer time wheel structure is adopted to achieve efficient management and execution of delay tasks through the design of ring array and time pointer. The tank object of the time wheel maintains the expiration time attribute and two -way linked list. The multi -layer structure is used to increase the time span, which can undertake a large number of delay tasks.

Benefits of technology

The time accuracy of the delay task is improved, the resource occupation is reduced, and the management and execution of efficient delay tasks have been achieved. There is no need to introduce other components to avoid system complexity and resources waste.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114296902B_ABST
    Figure CN114296902B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of task processing technology, and specifically provides a method for processing delayed tasks, using a multi-layer time wheel, the underlying data structure of a layer of the time wheel is a ring array, each position in the array represents a slot in the time wheel, and the slot is used to store the delayed task; the time wheel also includes a time pointer, the time pointer points to the slot in the time wheel, as time goes by, the time pointer moves continuously along the time wheel according to a certain time period, when the time pointer points to a certain slot, it means that the delayed task in the slot has expired, and the delayed task needs to be taken out for execution; the multi-layer time wheel is distributed in a clock-like manner, the inner time wheel rotates one circle, the outer time wheel rotates one grid, and the time wheel expands outward in turn, thereby increasing the span of the time wheel. Compared with the prior art, the present invention can undertake a large number of delayed tasks without introducing other components, and will not increase the complexity of the system or cause waste of resources.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of task processing, and specifically provides a method and device for processing delayed tasks. Background Art

[0002] With the development of Internet technology, in many business fields based on the Internet, the application scenarios of delayed tasks are increasing because the fulfillment process is more complicated and the fulfillment cycle is longer. For example, after an order is submitted, if the payment is not successful within the specified payment time, the order needs to be cancelled. For another example, after an order is completed, if the evaluation is timely after the specified evaluation time, the default evaluation information is set for the order. For another example, after a virtual network resource such as a coupon is activated, if it is not used within the specified usage time, it will be automatically invalidated.

[0003] At present, in response to such business needs, in some implementation methods, after receiving the delayed task, the delayed task triggering logic information will be stored in the database. At the same time, the database is scanned by the scheduled task at a certain period to detect whether there is a delayed task that needs to be executed. If there is a delayed task that needs to be executed, the delayed task is taken out from the database at one time and then executed in batches. However, in the implementation scheme based on the scheduled task scanning database, the scheduled task is executed at a certain period, and the execution time of the delayed task will be relatively delayed, resulting in inaccurate execution time of the delayed task. In addition, the full table scan must be performed every time, which has low execution efficiency. In the cycle when the delayed task is not scanned, the CPU is in an idling state, resulting in a waste of resources. Summary of the invention

[0004] The present invention aims at solving the above-mentioned deficiencies of the prior art and provides a method for processing delayed tasks with strong practicability.

[0005] A further technical task of the present invention is to provide a delayed task implementation device that is reasonably designed, safe and applicable.

[0006] The technical solution adopted by the present invention to solve the technical problem is:

[0007] A method for processing delayed tasks, using a multi-layer time wheel, the underlying data structure of a layer of time wheel is a ring array, each position in the array represents a slot in the time wheel, and the slot is used to store the delayed task;

[0008] The time wheel also includes a time pointer, which points to a slot in the time wheel. As time goes by, the time pointer moves along the time wheel according to a certain time period. When the time pointer points to a slot, it indicates that the delayed task in the slot has expired and needs to be taken out for execution.

[0009] The multiple layers of time wheels are arranged in a clock-like manner. When the inner time wheel rotates one circle, the outer time wheel rotates one grid. The time wheels expand outwards in sequence, thereby increasing the span of the time wheels.

[0010] Furthermore, the time slot object is a bucket, which internally maintains an expiration time attribute and a bidirectional linked list taskList, and the bidirectional linked list taskList is used to store delayed tasks, and the initial value of the expiration time attribute is -1.

[0011] Furthermore, the time granularity of the time wheel is represented by tickMs, which represents the time required for the time wheel to rotate one slot, and the size of the time wheel is represented by wheelSize, which is the length of the circular array of a single-layer time wheel;

[0012] wheelSize is set to an exponential power of 2. If the expiration time of the delayed task is expiredTime, the subscript calculation formula of the slot in the time wheel where the delayed task is located is as follows:

[0013] index=(exp iredTime / tickMs)&(wheelSize-1) (1)

[0014] Among them, index is the index of the slot in the time wheel where the delayed task is located.

[0015] int erval: time round span, calculated as follows:

[0016] int erval=tickMs×wheelSize (2)

[0017] Furthermore, the time pointer of the current time wheel is represented by currentTime, and the time wheel span parameter interval is combined to determine whether the current time wheel can store a delayed task. If the expiration time of the delayed task is expiredTime, then expiredTime must meet two conditions at the same time before it can be stored in the time wheel.

[0018] expiredTime≥currentTime+tickMs (3)

[0019] expiredTime≤currentTime+interval (4)

[0020] If formula (3) is not satisfied, it means that the delayed task has expired and needs to be executed; if formula (4) is not satisfied, it means that the current time wheel cannot accommodate the delayed task, and then continue to try to store the delayed task in the outer time wheel;

[0021] The pointer to the outer time wheel uses timeWheel, where delayQueue refers to the native delay queue in JAVA, which is used to store the time wheel slot object bucket.

[0022] Furthermore, the time wheel initialization specifies tickMs, wheelSize and currentTime, where currentTime is the current time when the time wheel is initialized; a delay queue delayQueue is initialized inside the time wheel, and multiple layers of time wheels share the same delayQueue. At the same time, a background thread is started to continuously obtain buckets from the delay queues, and when a bucket is obtained, the stored tasks are traversed for processing;

[0023] When the first bucket in the delayQueue queue has not expired or the delayQueue is empty, the background thread enters a sleep state.

[0024] Furthermore, to add a task to the time wheel, the main steps are as follows:

[0025] S1. Determine whether the current time wheel can accommodate the delayed task according to formula (4);

[0026] S2. Calculate the bucket expiration time;

[0027] S3. Determine whether the task has expired according to formula (3).

[0028] Furthermore, in step S1, if the delayed task can be accommodated, the time slot index of the delayed task is calculated according to formula (1), the slot bucket is obtained according to the index, and then the task is added to the tail of the doubly linked list taskList in the bucket;

[0029] In step S2, determine whether the current expiration time of the bucket is consistent with the calculated expiration time. If they are consistent, execute step S3. Otherwise, update the expiration time of the bucket and then add the bucket to the delayQueue. If they are consistent, it means that the bucket is already in the delayQueue.

[0030] expiredTime bucket =expiredTime task -expiredTime task %tickMs (5)

[0031] In step S3, if it has expired, the delayed task is placed in the asynchronous thread pool for execution; if it has not expired and the current time wheel has an outer time wheel, tasks continue to be added to the outer time wheel; if it does not exist, an outer time wheel is constructed with the inner time wheel pointer as the outer time wheel pointer, and then tasks continue to be added to the outer time wheel.

[0032] Furthermore, when the time wheel obtains a task, the time wheel is initialized to start the background thread, and buckets are continuously obtained from the delayQueue. The first expired bucket at the head of the queue will be popped out of the queue first. Suppose the expiration time of the bucket is expiredTime, and according to formula (3), it is determined whether the minimum expiration time that the time wheel can currently represent is smaller than the bucket expiration time. If so, the time pointer currentTime of the time wheel is updated according to the following formula (6):

[0033] currentTime=exp iredTime-(exp iredTime%tickMs) (6)

[0034] If the current time wheel has an outer time wheel, the time pointer currentTime of the current time wheel is used as the expiration time expiredTime, and the time pointer of the outer time wheel is continuously updated. This process is a recursive update of the time wheel pointer.

[0035] Furthermore, the expiration time of delayed tasks in the same bucket is not consistent, and the difference range is tickMs. After the time wheel pointer is updated, it is necessary to re-execute the task adding process, and downgrade the task list on the outer time wheel to the inner time wheel with finer time granularity, until the delayed task is executed when it cannot be downgraded to the innermost time wheel; after taking out all tasks in the bucket, the bucket expiration time must be set to the initial value -1.

[0036] A delayed task implementation device, comprising: at least one memory and at least one processor;

[0037] The at least one memory is used to store a machine-readable program;

[0038] The at least one processor is used to call the machine-readable program to execute a method for processing a delayed task.

[0039] Compared with the prior art, the method and device for processing delayed tasks of the present invention have the following outstanding beneficial effects:

[0040] Compared with the timing polling method, the present invention has higher time accuracy and occupies less resources. The time wheel adopts a multi-layer structure to increase the overall time span and can undertake a large number of delayed tasks. It does not need to introduce other components, will not increase the complexity of the system, and will not cause waste of resources. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] In order to more clearly illustrate the embodiments of the present invention 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 some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0042] Attached Figure 1 It is a structural diagram of a method for processing delayed tasks. DETAILED DESCRIPTION

[0043] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0044] A best embodiment is given below:

[0045] like Figure 1 As shown, a method for processing delayed tasks in this embodiment uses a multi-layer time wheel, and the underlying data structure of a layer of time wheel is a ring array, and each position in the array represents a slot in the time wheel, and the slot is used to store the delayed task;

[0046] The time wheel also includes a time pointer, which points to a slot in the time wheel. As time goes by, the time pointer moves along the time wheel according to a certain time period. When the time pointer points to a slot, it means that the delayed task in the slot has expired and needs to be taken out for execution.

[0047] The multiple layers of time wheels are arranged in a clock-like manner. When the inner time wheel rotates one circle, the outer time wheel rotates one grid. The time wheels expand outwards in sequence, thereby increasing the span of the time wheels.

[0048] The time slot object is bucke, which internally maintains the expiration time attribute and the bidirectional linked list taskList. The bidirectional linked list taskList is used to store delayed tasks, and the initial value of the expiration time attribute is -1.

[0049] The time granularity of the time wheel is represented by tickMs, which represents the time it takes for the time wheel to rotate one slot. The size of the time wheel is represented by wheelSize, which is the length of the circular array of a single-layer time wheel. In order to improve the calculation efficiency of the position of the delayed task in the time wheel slot, wheelSize is set to an exponential power of 2. If the expiration time of the delayed task is expiredTime, the subscript calculation formula of the slot in the time wheel where the delayed task is located is as follows:

[0050] index=(exp iredTime / tickMs)&(wheelSize-1) (1)

[0051] Among them, index is the index of the slot in the time wheel where the delayed task is located.

[0052] int erval: time round span, calculated as follows:

[0053] int erval=tickMs×wheelSize (2)

[0054] The time pointer of the current time wheel is represented by currentTime. Combined with the time wheel span parameter interval, it is determined whether the current time wheel can store a delayed task. If the delayed task expires at expiredTime, then expiredTime must meet two conditions at the same time before it can be stored in the time wheel.

[0055] expiredTime≥currentTime+tickMs (3)

[0056] expiredTime≤currentTime+interval (4)

[0057] If formula (3) is not satisfied, it means that the delayed task has expired and needs to be executed; if formula (4) is not satisfied, it means that the current time wheel cannot accommodate the delayed task, and then continue to try to store the delayed task in the outer time wheel;

[0058] The pointer to the outer time wheel uses timeWheel, where delayQueue refers to the native delay queue in JAVA, which is used to store the time wheel slot object bucket.

[0059] The time wheel initialization needs to specify tickMs, wheelSize, and currentTime, where currentTi is the current time when the time wheel is initialized. A delay queue delayQueue is initialized inside the time wheel, and multiple layers of time wheels share the same delayQueue. At the same time, a background thread is started to continuously obtain buckets from the delay queue. When a bucket is obtained, it traverses its storage tasks for processing. When the first bucket in the delayQueue has not expired or the delayQueue is empty, the background thread enters a sleep state to prevent the CPU from idling and consuming resources.

[0060] To add a task in the time wheel, the main steps are as follows:

[0061] S1. Determine whether the current time wheel can accommodate the delayed task according to formula (4);

[0062] First, determine whether the current time wheel can accommodate this delayed task according to formula (4). If so, calculate the index of the time wheel slot where the delayed task is located according to formula (1), obtain the slot bucket according to the index, and then add the task to the tail of the doubly linked list taskList in the bucket.

[0063] S2. Calculate the bucket expiration time;

[0064] The bucket expiration time is calculated according to the following formula (5). Then determine whether the current expiration time of the bucket is consistent with the calculated expiration time. If they are consistent, execute step S3. Otherwise, update the bucket expiration time and then add the bucket to the delayQueue. If they are consistent, it means that the bucket is already in the delayQueue.

[0065] expiredTime bucket =expiredTime task -expiredTime task %tickMs (5)

[0066] S3, judging whether the task has expired according to formula (3);

[0067] If it has expired, the delayed task will be put into the asynchronous thread pool for execution. If it has not expired, if the current time wheel has an outer time wheel, continue to add tasks to the outer time wheel. If it does not exist, construct an outer time wheel with the inner time wheel pointer as the outer time wheel pointer, and then continue to add tasks to the outer time wheel.

[0068] When the time wheel obtains tasks, the time wheel is initialized to start the background thread, and buckets are continuously obtained from the delayQueue. The first expired bucket at the head of the queue will be popped out of the queue first. Suppose the expiration time of the bucket is expiredTime. According to formula (3), it is determined whether the minimum expiration time that the time wheel can currently represent is smaller than the bucket expiration time. If so, the time pointer currentTime of the time wheel is updated according to the following formula (6).

[0069] currentTime=exp iredTime-(exp iredTime%tickMs) (6)

[0070] If the current time wheel has an outer time wheel, the time pointer currentTime of the current time wheel is used as the expiration time expiredTime, and the time pointer of the outer time wheel continues to be updated. This process is a recursive update of the time wheel pointer.

[0071] The expiration time of delayed tasks in the same bucket is not consistent, and the difference range is tickMs. Therefore, the expiration of a bucket does not mean that the delayed tasks in the bucket have expired. Therefore, after the time wheel pointer is updated, it is necessary to re-execute the task adding process to downgrade the task list on the outer time wheel to the inner time wheel with finer time granularity until the delayed task is executed when it cannot be downgraded to the innermost time wheel. After taking out all the tasks in the bucket, the bucket expiration time must be set to the initial value -1.

[0072] A delayed task implementation device, comprising: at least one memory and at least one processor;

[0073] The at least one memory is used to store a machine-readable program;

[0074] The at least one processor is used to call the machine-readable program to execute a method for processing a delayed task.

[0075] The above-mentioned specific implementations are only specific cases of the present invention. The patent protection scope of the present invention includes but is not limited to the above-mentioned specific implementations. Any method and device for processing delayed tasks that conform to the claims of the present invention and any appropriate changes or substitutions made by any ordinary technician in the technical field shall fall within the patent protection scope of the present invention.

[0076] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.

Claims

1. A method for processing a delayed task, characterized in that: A multi-layer time wheel is used, and the underlying data structure of a layer of time wheel is a ring array, and each position in the array represents a slot in the time wheel, and the slot is used to store delayed tasks; The time wheel also includes a time pointer, which points to a slot in the time wheel. As time goes by, the time pointer moves along the time wheel according to a certain time period. When the time pointer points to a slot, it indicates that the delayed task in the slot has expired and needs to be taken out for execution. The multiple layers of time wheels are arranged in a clock-like manner. When the inner time wheel rotates one circle, the outer time wheel rotates one grid. The time wheels expand outwards in sequence, thereby increasing the span of the time wheels. The time slot object is a bucket, which internally maintains an expiration time attribute and a two-way linked list taskList, where the two-way linked list taskList is used to store delayed tasks, and the initial value of the expiration time attribute is -1; The time granularity of the time wheel is represented by tickMs, which represents the time required for the time wheel to rotate one slot. The size of the time wheel is represented by wheelSize, which is the length of the circular array of a single-layer time wheel. wheelSize is set to the exponential power of 2. If the expiration time of the delayed task is expiredTime, the subscript calculation formula of the slot in the time wheel where the delayed task is located is as follows: index=(exp iredTime / tickMs)&(wheelSize-1) (1) Among them, index is the index of the slot in the time wheel where the delayed task is located; int erval: time round span, calculated as follows: int erval=tickMs×wheelSize (2); The time pointer of the current time wheel is represented by currentTime. Combined with the time wheel span parameter int erval, it is determined whether the current time wheel can store a delayed task. If the expiration time of the delayed task is expiredTime, expiredTime must meet two conditions at the same time before it can be stored in the time wheel. expiredTime≥currentTime+tickMs (3) expiredTime≤currentTime+interval (4) If formula (3) is not satisfied, it means that the delayed task has expired and needs to be executed; if formula (4) is not satisfied, it means that the current time wheel cannot accommodate the delayed task, and then continue to try to store the delayed task in the outer time wheel; The pointer to the outer time wheel uses timeWheel, where delayQueue refers to the native delay queue in JAVA, which is used to store the time wheel slot object bucket; The time wheel initialization specifies tickMs, wheelSize and currentTime, where currentTime is the current time when the time wheel is initialized; a delay queue delayQueue is initialized inside the time wheel, and multiple layers of time wheels share the same delayQueue. At the same time, a background thread is started to continuously obtain buckets from the delay queue. When a bucket is obtained, it traverses its stored tasks for processing; When the first bucket of the delayQueue has not expired or the delayQueue is empty, the background thread enters the sleep state; To add a task in the time wheel, the main steps are as follows: S1. Determine whether the current time wheel can accommodate the delayed task according to formula (4); S2. Calculate the bucket expiration time; S3, judging whether the task has expired according to formula (3); In step S1, if the delayed task can be accommodated, the time slot index of the delayed task is calculated according to formula (1), the slot bucket is obtained according to the index, and then the task is added to the tail of the doubly linked list taskList in the bucket; In step S2, determine whether the current expiration time of the bucket is consistent with the calculated expiration time. If they are consistent, execute step S3. Otherwise, update the expiration time of the bucket and then add the bucket to the delayQueue. If they are consistent, it means that the bucket is already in the delayQueue. expiredTime bucket =expiredTime task -expiredTime task %tickMs (5) In step S3, if it has expired, the delayed task is put into the asynchronous thread pool for execution; if it has not expired, and the current time wheel has an outer time wheel, tasks are continued to be added to the outer time wheel; if it does not exist, an outer time wheel is constructed with the inner time wheel pointer as the outer time wheel pointer, and then tasks are continued to be added to the outer time wheel; When the time wheel obtains tasks, the time wheel is initialized to start the background thread, and buckets are continuously obtained from the delayQueue. The first expired bucket at the head of the queue will be popped out of the queue first. Suppose the expiration time of the bucket is expiredTime. According to formula (3), it is determined whether the minimum expiration time that the time wheel can currently represent is smaller than the bucket expiration time. If so, the time pointer currentTime of the time wheel is updated according to the following formula (6). currentTime=expiredTime-(expiredTime%tickMs) (6) If the current time wheel has an outer time wheel, the time pointer currentTime of the current time wheel is used as the expiration time expiredTime, and the time pointer of the outer time wheel continues to be updated. This process is a recursive update of the time wheel pointer.

2. A method for processing delayed tasks according to claim 1, characterized in that: The expiration time of delayed tasks in the same bucket is not consistent, and the difference range is tickMs. After the time wheel pointer is updated, it is necessary to re-execute the task adding process to downgrade the task list on the outer time wheel to the inner time wheel with finer time granularity until the delayed task is executed when it cannot be downgraded to the innermost time wheel; after taking out all tasks from the bucket, the bucket expiration time must be set to the initial value -1.

3. A delayed task implementation device, characterized in that: include: at least one memory and at least one processor; The at least one memory is used to store a machine-readable program; The at least one processor is configured to call the machine-readable program to execute the method according to any one of claims 1 to 2.

Citation Information

Patent Citations

  • Method, system and device for updating source information based on time wheel and a storage medium

    CN109360055A

  • Delay message processing method and system

    CN111782414A