A task scheduling method, device, storage medium and server
By assigning timestamps to requests in the microservice environment and executing tasks in the order of timestamps, the delay problem caused by the need to reallocate the thread pool after the request interrupt returns under the asynchronous programming style is solved, achieving priority processing and efficient response.
Patent Information
- Application Number
- CN201910603430.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-07-05
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2039-07-05
AI Technical Summary
In a microservice environment, the asynchronous programming style and thread pool method cause the thread pool to be reallocated after the request is interrupted and returned, resulting in delays.
By obtaining the time of the client request, using a monad based on functional programming to obtain the task, and assigning the same timestamp to each request, the tasks are executed in order from small to large timestamps to avoid reallocating the thread pool.
Ensure that requests that are executed first are processed first after the interrupt returns to avoid delays and improve CPU utilization and response speed.
Smart Images

Figure CN110442434B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a task scheduling method, device, storage medium and server. Background Art
[0002] At present, in microservice environments, asynchronous programming style plus thread pool is usually used to replace the traditional request-thread model to improve CPU utilization. However, this approach will cause the following problems: if a request that is executed first is interrupted, when the request is interrupted and returned, it needs to be reallocated to the thread pool queue, causing the request to be processed later and cause delays. Summary of the invention
[0003] In view of this, an embodiment of the present invention provides a task scheduling method, device, storage medium and server, which can enable the first executed request to be processed first after the interrupt returns, without having to be reallocated to the thread pool queue, thereby avoiding delays.
[0004] A first aspect of an embodiment of the present invention provides a task scheduling method, including:
[0005] Get each request sent by the client and record the time when each request is obtained;
[0006] Use monads based on functional programming to obtain each task contained in each request;
[0007] Combined with the time when each request was obtained, each task included in each request is assigned a same timestamp;
[0008] Set up the task scheduler to execute the tasks contained in each request in ascending order of timestamps.
[0009] A second aspect of an embodiment of the present invention provides a task scheduling device, including:
[0010] The request acquisition module is used to obtain each request sent by the client and record the time when each request is obtained;
[0011] The task acquisition module is used to obtain each task contained in each request using a monad based on functional programming;
[0012] The timestamp assignment module is used to assign a same timestamp to each task contained in each request based on the time of each request.
[0013] The task scheduling module is used to set up the task scheduler to execute the tasks contained in each request in order from small to large timestamps.
[0014] According to a third aspect of an embodiment of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by a processor, the steps of the task scheduling method proposed in the first aspect of the embodiment of the present invention are implemented.
[0015] According to a fourth aspect of an embodiment of the present invention, a server is provided, comprising a memory, a processor, and computer-readable instructions stored in the memory and executable on the processor, wherein when the processor executes the computer-readable instructions, the steps of the task scheduling method proposed in the first aspect of the embodiment of the present invention are implemented.
[0016] The task scheduling method proposed in the embodiment of the present invention includes: obtaining each request sent by the client and recording the time when each request is obtained; using a monad based on functional programming to obtain each task contained in each request; combining the time when each request is obtained, assigning a same timestamp to each task contained in each request; setting a task scheduler to execute each task contained in each request in order from small to large timestamps. Through such a setting, since the timestamp of the task contained in the request executed first is smaller than the timestamp of the task contained in the request executed later, the request executed first will be processed first after the interrupt returns, and does not need to be reallocated to the thread pool queue, thereby avoiding delays. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, 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 invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0018] Figure 1 is a flowchart of an embodiment of a task scheduling method provided by an embodiment of the present invention;
[0019] Figure 2 is a structural diagram of an embodiment of a task scheduling device provided by an embodiment of the present invention;
[0020] Figure 3 It is a schematic diagram of a server provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0021] The embodiment of the present invention provides a task scheduling method, device, storage medium and server, which can make the first executed request be processed first after the interruption returns, without having to be reallocated to the thread pool queue, thus avoiding delay.
[0022] In order to make the purpose, features and advantages of the present invention more obvious and easy to understand, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described below are only part of the embodiments of the present invention, not 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.
[0023] See also Figure 1 , an embodiment of a task scheduling method in an embodiment of the present invention includes:
[0024] 101. Obtain each request sent by the client and record the time when each request is obtained;
[0025] After receiving each request sent by the client, the server will record the time when each request is received, and then parse and execute the tasks contained in each request to complete the request response.
[0026] 102. Use monads based on functional programming to obtain each task contained in each request;
[0027] Each time a request is received, a monad based on functional programming is used to determine the various tasks contained in the request. Monad is an abstract data type in functional programming, used to represent calculations rather than data. Monad follows the traditional shell concept, executes interactive commands or batch executes scripts saved as files, and can easily communicate with data storage areas (regardless of the data). In programs written in a functional style, monads can be used to organize processes involving ordered operations, or to define arbitrary control flows (such as handling concurrency, exceptions, and continuations). Monads can be used as containers for loading functors to take things. Functors take out data for processing, then put it in a new monad and return it.
[0028] Specifically, for any target request, the steps of using a monad based on functional programming to obtain each task included in the target request may include:
[0029] (1) encapsulating the first task included in the target request as a parameter into a list and passing it to the second task that has a dependency relationship with the first task;
[0030] (2) encapsulating the second task as a parameter into the next list and passing it to a third task that has a dependency relationship with the second task;
[0031] (3) In the same manner, the previous task included in the target request is encapsulated as a parameter into a list, and the list is passed to the next task that has a dependency relationship with the previous task, until the last task included in the target request is encapsulated as a parameter into the last list;
[0032] (4) Parse the last list to obtain each task included in the target request.
[0033] Specifically, you can use the map and flatmap functions to store a task (function) contained in the request as a parameter in a list, return the task to the next function through the list, and then use the map and flatmap functions again to store the next task as a parameter in another list. Repeat this process continuously, and you can get all the tasks contained in the request from the last list, as well as the dependencies between these tasks, that is, the calls and associations between the tasks.
[0034] Furthermore, the above step (4) may include:
[0035] (4.1) parsing the last list to obtain the tasks and task dependencies contained in the target request;
[0036] (4.2) Constructing a task dependency graph of the target request according to the tasks and task dependencies included in the target request.
[0037] The task dependency graph is equivalent to a traditional graph data structure - each node is associated with other nodes. In the semantics of the task dependency graph, a node is equivalent to a task, and these tasks contain references to other tasks. Many tasks reference each other to represent the dependencies between tasks, and finally form a graph data structure. For example, based on the semantics of Monad, when a task (denoted as TaskA) executes map or flatMap, a new task will be generated, denoted as TaskB. TaskB will have a parent variable pointing to TaskA, indicating the dependency relationship.
[0038] 103. Based on the time when each request is obtained, assign a same timestamp to each task included in each request;
[0039] Next, based on the time when each request is obtained, each task included in each request is assigned a same timestamp. The principle is that the tasks included in the request that are obtained earlier are assigned a smaller timestamp.
[0040] Specifically, the time when the request is obtained can generally be directly used as the timestamp of each task included in the request, that is, the goal of allocating a smaller timestamp to the task included in the request that is obtained earlier is achieved.
[0041] Furthermore, if a task dependency graph of each request is constructed, after assigning a same timestamp to each task included in each request, the following may also be included:
[0042] (1) Count the number of nodes in the task dependency graph of each request and the number of levels of task dependency relationships;
[0043] (2) Based on the number of nodes in the task dependency graph of each request and the number of levels of the task dependency relationship, the timestamps of the tasks included in each request are adjusted respectively.
[0044] After obtaining the task dependency graphs corresponding to each request, we can count the number of nodes in each task dependency graph and the number of levels of task dependency, and then adjust the timestamps assigned to the tasks of each request based on this information. The number of nodes in the task dependency graph is the number of tasks, and the number of levels is the highest level of mutual references between tasks. For example, if the longest reference chain is TaskA-TaskB-TaskC–TaskD, the corresponding number of levels is 4.
[0045] Optionally, adjusting the timestamp of the tasks included in each request in combination with the number of nodes in the task dependency graph of each request and the number of levels of the task dependency relationship may include:
[0046] (1) if the number of nodes in the task dependency graph of any request exceeds a first threshold, and / or the number of levels of the task dependency relationship exceeds a second threshold, then the timestamp of the task included in any request is adjusted backwards;
[0047] (2) If the number of nodes in the task dependency graph of any request is less than a third threshold, and / or the number of levels of the task dependency relationship is less than a fourth threshold, then the timestamp of the task included in any one of the requests is adjusted forward, the third threshold is less than the first threshold, and the fourth threshold is less than the second threshold.
[0048] The essence of this timestamp adjustment method is to give priority to simpler requests (that is, requests that include fewer tasks and fewer dependencies between tasks). These requests require less processing time and system resources, so they can be executed first to reduce waiting time.
[0049] Optionally, adjusting the timestamp of the tasks included in each request in combination with the number of nodes in the task dependency graph of each request and the number of levels of the task dependency relationship may include:
[0050] (1) For any request, the corresponding request processing deadline is constructed according to the number of nodes in its task dependency graph and the number of levels of task dependency relationships;
[0051] (2) If the current time exceeds the request processing deadline, adjust the timestamp of the task included in any one of the requests backward.
[0052] For example, the more nodes a request has in its task dependency graph, and / or the higher the number of levels of task dependencies, the longer the request processing deadline can be set. In actual operation, the request processing deadline can be set based on the time required to process the request under normal circumstances. Under normal circumstances, the request is processed within the request processing deadline, and the timestamp of the task included in the request will not be changed. However, in some special cases, the request cannot be processed smoothly within the request processing deadline, such as when a task error occurs. In this case, in order to avoid other requests waiting for the request to be executed, the timestamp of the task included in the request can be adjusted backwards, thereby giving priority to other requests that can be executed normally, thereby improving the rationality of task scheduling.
[0053] 104. Set up a task scheduler to execute the tasks contained in each request in order from small to large timestamps.
[0054] A task scheduler is set up on the server side. When multiple requests are received successively, the tasks triggered by each request will be executed in order from small to large timestamps, that is, the smaller the timestamp, the higher the priority of task execution. Specifically, if there are multiple types of requests, the scheduler will still execute according to the principle of processing the first received task first. For example, within 1 minute, 3000 requests of type A are obtained successively, and each A request contains 5 task steps 1, 2, 3, 4, and 5. Among them, the A request obtained first will give priority to the corresponding 5 steps. Assuming that the first A request is A0001, the second A request is A0002, and the thousandth A request is A1000, then when step 4 of A0001 and step 1 of A1000 need to be executed, the steps related to A0001 will be queued before the execution of the steps of A1000 (because the timestamp of step 4 of A0001 is less than the timestamp of step 1 of A1000).
[0055] The task scheduling method proposed in the embodiment of the present invention includes: obtaining each request sent by the client and recording the time when each request is obtained; using a monad based on functional programming to obtain each task contained in each request; combining the time when each request is obtained, assigning a same timestamp to each task contained in each request; setting a task scheduler to execute each task contained in each request in order from small to large timestamps. Through such a setting, since the timestamp of the task contained in the request executed first is smaller than the timestamp of the task contained in the request executed later, the request executed first will be processed first after the interrupt returns, and does not need to be reallocated to the thread pool queue, thereby avoiding delays.
[0056] It should be understood that the order of execution of the steps in the above embodiment does not necessarily mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present invention.
[0057] A task scheduling method is mainly described above, and a task scheduling device will be described in detail below.
[0058] See also Figure 2 , an embodiment of a task scheduling device in an embodiment of the present invention includes:
[0059] The request acquisition module 201 is used to acquire each request sent by the client and record the time when each request is acquired;
[0060] A task acquisition module 202, used to obtain each task included in each request using a monad based on functional programming;
[0061] The timestamp assigning module 203 is used to assign a same timestamp to each task included in each request in combination with the time of each request obtained;
[0062] The task scheduling module 204 is used to set a task scheduler to execute the tasks included in each request in order from small to large timestamps.
[0063] Furthermore, the timestamp allocation module may include:
[0064] A first encapsulation unit is used for encapsulating a first task included in any target request as a parameter into a list, and passing it to a second task having a dependency relationship with the first task;
[0065] A second encapsulation unit is used to encapsulate the second task as a parameter into a next list, and pass it to a third task having a dependency relationship with the second task;
[0066] a third encapsulation unit, configured to encapsulate the last task included in the target request as a parameter into a list in the same manner, and pass it to the next task having a dependency relationship with the last task, until the last task included in the target request is encapsulated as a parameter into the last list;
[0067] The list parsing unit is used to parse the last list and obtain each task included in the target request.
[0068] Furthermore, the singleton parsing unit may include:
[0069] A list parsing subunit, used to parse the last list to obtain the tasks and task dependencies contained in the target request;
[0070] A task dependency graph construction subunit, used to construct a task dependency graph of the target request according to the tasks and task dependencies included in the target request;
[0071] The timestamp allocation module may further include:
[0072] A node number and task dependency level number counting unit, used to count the number of nodes and the number of levels of task dependency relationships in the task dependency graph of the target request;
[0073] The timestamp adjustment unit is used to adjust the timestamp of the task included in the target request in combination with the number of nodes in the task dependency graph of the target request and the number of levels of the task dependency relationship.
[0074] Optionally, the timestamp adjustment unit may include:
[0075] A first adjustment subunit, configured to adjust backward the timestamp of the task included in the target request if the number of nodes in the task dependency graph of the target request exceeds a first threshold, and / or the number of levels of the task dependency relationship exceeds a second threshold;
[0076] The second adjustment subunit is used to adjust the timestamp of the task included in the target request forward if the number of nodes in the task dependency graph of the target request is less than a third threshold and / or the number of levels of task dependencies is less than a fourth threshold, the third threshold is less than the first threshold, and the fourth threshold is less than the second threshold.
[0077] Further, the timestamp adjustment unit may include:
[0078] A request processing deadline construction subunit, used to construct a corresponding request processing deadline according to the number of nodes in the task dependency graph of the target request and the number of levels of the task dependency relationship;
[0079] The third adjustment subunit is configured to adjust backward the timestamp of the task included in the target request if the current time exceeds the request processing deadline.
[0080] The embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by a processor, the following is implemented: Figure 1 Represents the steps of any task scheduling method.
[0081] An embodiment of the present invention further provides a server, comprising a memory, a processor, and computer-readable instructions stored in the memory and executable on the processor, wherein when the processor executes the computer-readable instructions, Figure 1 Represents the steps of any task scheduling method.
[0082] Figure 3 FIG. 1 is a schematic diagram of a server provided by an embodiment of the present invention. Figure 3 As shown, the server 3 of this embodiment includes: a processor 30, a memory 31, and a computer-readable instruction 32 stored in the memory 31 and executable on the processor 30. When the processor 30 executes the computer-readable instruction 32, the steps in the above-mentioned various task scheduling method embodiments are implemented, for example Figure 1 Alternatively, when the processor 30 executes the computer-readable instructions 32, the functions of each module / unit in the above-mentioned device embodiments are realized, for example, Figure 2 The functions of modules 201 to 204 are shown.
[0083] Exemplarily, the computer readable instructions 32 may be divided into one or more modules / units, which are stored in the memory 31 and executed by the processor 30 to implement the present invention. The one or more modules / units may be a series of computer readable instruction segments capable of implementing specific functions, which are used to describe the execution process of the computer readable instructions 32 in the server 3.
[0084] The server 3 may be a computing device such as a smart phone, a notebook, a PDA, or a cloud server. The server 3 may include, but is not limited to, a processor 30 and a memory 31. Those skilled in the art will appreciate that Figure 3 It is only an example of server 3 and does not constitute a limitation on server 3. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the server 3 may also include input and output devices, network access devices, buses, etc.
[0085] The processor 30 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.
[0086] The memory 31 may be an internal storage unit of the server 3, such as a hard disk or memory of the server 3. The memory 31 may also be an external storage device of the server 3, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card (FLash Card), etc. equipped on the server 3. Further, the memory 31 may also include both an internal storage unit of the server 3 and an external storage device. The memory 31 is used to store the computer-readable instructions and other programs and data required by the server. The memory 31 may also be used to temporarily store data that has been output or is to be output.
[0087] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0088] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0089] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-OnLyMemory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0090] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A task scheduling method, characterized in that: include: Get each request sent by the client and record the time when each request is obtained; Use monads based on functional programming to obtain each task contained in each request; Combined with the time when each request was obtained, each task included in each request is assigned a same timestamp; Set up a task scheduler to execute each task contained in each request in ascending order of timestamps; The method of using a monad based on functional programming to obtain each task included in each request includes: For any target request among the requests, encapsulate a first task included in the target request as a parameter into a list, and pass it to a second task having a dependency relationship with the first task; Encapsulate the second task as a parameter into the next list, and pass it to the third task that has a dependency relationship with the second task; In the same way, the previous task included in the target request is encapsulated as a parameter into a list, and passed to the next task that has a dependency relationship with the previous task, until the last task included in the target request is encapsulated as a parameter into the last list; Parse the last list to obtain each task included in the target request.
2. The task scheduling method according to claim 1, characterized in that: The parsing of the last list to obtain the tasks included in the target request includes: Parse the last list to obtain the tasks and task dependencies contained in the target request; Constructing a task dependency graph of the target request according to the tasks and task dependencies included in the target request; After combining the time of obtaining each request and assigning the same timestamp to each task contained in each request, it also includes: Counting the number of nodes in the task dependency graph of the target request and the number of levels of task dependency relationships; In combination with the number of nodes in the task dependency graph of the target request and the number of levels of task dependency relationships, the timestamps of the tasks included in the target request are adjusted.
3. The task scheduling method according to claim 2, characterized in that: The adjusting the timestamp of the task included in the target request based on the number of nodes in the task dependency graph of the target request and the number of levels of the task dependency relationship includes: If the number of nodes in the task dependency graph of the target request exceeds a first threshold, and / or the number of levels of task dependency relations exceeds a second threshold, then adjusting the timestamps of the tasks included in the target request backwards; If the number of nodes in the task dependency graph of the target request is less than a third threshold, and / or the number of levels of task dependencies is less than a fourth threshold, then the timestamp of the task included in the target request is adjusted forward, the third threshold is less than the first threshold, and the fourth threshold is less than the second threshold.
4. The task scheduling method according to claim 2, characterized in that: The adjusting the timestamp of the task included in the target request based on the number of nodes in the task dependency graph of the target request and the number of levels of the task dependency relationship includes: Constructing a corresponding request processing deadline according to the number of nodes in the task dependency graph of the target request and the number of levels of task dependency relationships; If the current time exceeds the request processing deadline, the timestamp of the task included in the target request is adjusted backwards.
5. A task scheduling device, characterized in that: include: The request acquisition module is used to obtain each request sent by the client and record the time when each request is obtained; The task acquisition module is used to obtain each task contained in each request using a monad based on functional programming; The timestamp assignment module is used to assign a same timestamp to each task contained in each request based on the time of each request. The task scheduling module is used to set up the task scheduler to execute the tasks contained in each request in the order of timestamps from small to large; Wherein, the timestamp allocation module includes: A first encapsulation unit is used for encapsulating a first task included in any target request as a parameter into a list, and passing it to a second task having a dependency relationship with the first task; A second encapsulation unit is used to encapsulate the second task as a parameter into a next list, and pass it to a third task having a dependency relationship with the second task; a third encapsulation unit, for encapsulating the last task included in the target request as a parameter into a list in the same manner, and passing it to the next task having a dependency relationship with the last task, until the last task included in the target request is encapsulated as a parameter into the last list; The list parsing unit is used to parse the last list and obtain each task included in the target request.
6. A computer-readable storage medium storing computer-readable instructions, characterized in that: When the computer-readable instructions are executed by a processor, the steps of the task scheduling method according to any one of claims 1 to 4 are implemented.
7. A server comprising a memory, a processor, and computer-readable instructions stored in the memory and executable on the processor, characterized in that: When the processor executes the computer-readable instructions, the following steps are implemented: Get each request sent by the client and record the time when each request is obtained; Use monads based on functional programming to obtain each task contained in each request; Combined with the time when each request was obtained, each task included in each request is assigned a same timestamp; Set up a task scheduler to execute each task contained in each request in ascending order of timestamps; The method of using a monad based on functional programming to obtain each task included in each request includes: For any target request among the requests, encapsulate a first task included in the target request as a parameter into a list, and pass it to a second task having a dependency relationship with the first task; Encapsulate the second task as a parameter into the next list, and pass it to the third task that has a dependency relationship with the second task; In the same way, the previous task included in the target request is encapsulated as a parameter into a list, and passed to the next task that has a dependency relationship with the previous task, until the last task included in the target request is encapsulated as a parameter into the last list; Parse the last list to obtain each task included in the target request.
8. The server according to claim 7, characterized in that: The parsing of the last list to obtain the tasks included in the target request includes: Parse the last list to obtain the tasks and task dependencies contained in the target request; Constructing a task dependency graph of the target request according to the tasks and task dependencies included in the target request; After combining the time of obtaining each request and assigning the same timestamp to each task contained in each request, it also includes: Counting the number of nodes in the task dependency graph of the target request and the number of levels of task dependency relationships; In combination with the number of nodes in the task dependency graph of the target request and the number of levels of task dependency relationships, the timestamps of the tasks included in the target request are adjusted.
