CN112631745A - Timed task execution method, device and system based on time window - Google Patents

Timed task execution method, device and system based on time window Download PDF

Info

Publication number
CN112631745A
CN112631745A CN202011398542.0A CN202011398542A CN112631745A CN 112631745 A CN112631745 A CN 112631745A CN 202011398542 A CN202011398542 A CN 202011398542A CN 112631745 A CN112631745 A CN 112631745A
Authority
CN
China
Prior art keywords
task
worker
time
time window
execution
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202011398542.0A
Other languages
Chinese (zh)
Inventor
刘利
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Weimeng Chuangke Network Technology China Co Ltd
Original Assignee
Weimeng Chuangke Network Technology China Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Weimeng Chuangke Network Technology China Co Ltd filed Critical Weimeng Chuangke Network Technology China Co Ltd
Priority to CN202011398542.0A priority Critical patent/CN112631745A/en
Publication of CN112631745A publication Critical patent/CN112631745A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4812Task transfer initiation or dispatching by interrupt, e.g. masked
    • G06F9/4825Interrupt from clock, e.g. time of day
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4843Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system

Abstract

The embodiment of the invention provides a timing task execution method, a device and a system based on a time window, wherein at least one worker periodically executes in parallel at a specified scheduling interval, each worker schedules and executes each time, a corresponding time window is set, each worker inquires a task table according to the corresponding time window, and a timing task is obtained from the task table and is used as a corresponding preparation task; each worker executes the corresponding preparation task and modifies the task state of the corresponding preparation task in the task table to be completed; by the scheme, the single-point problem in the timing task system is solved, and the effects of thermal disaster recovery and automatic timing task recovery without manual intervention are achieved.

Description

Timed task execution method, device and system based on time window
Technical Field
The invention relates to the field of computers, in particular to a timing task execution method, device and system based on a time window.
Background
Timing tasks are often used in software development. For example, the ERP system sends an email regularly to remind people to punch cards every day at 6 pm; in the settlement system, the order payment condition is checked regularly, and if the order is not paid in time, the order is cancelled, and commodity resources are released. Currently, many companies adopt Linux timed task scripts for implementing timed task technologies. The advantages of this are simplicity and fast development. Particularly, the PHP language development system is adopted, the PHP language is a script language, and the timing task function can be completed easily by means of an operating system without additional development. The biggest problem in the prior art is that a single point exists in a system, the system is not robust enough, and disaster recovery is avoided. If the machine where the timing task is located is down, the timing function cannot be quickly recovered.
Disclosure of Invention
The embodiment of the invention provides a timing task execution method, a timing task execution device and a timing task execution system based on a time window.
In order to achieve the above object, in one aspect, an embodiment of the present invention provides a method for executing a timed task based on a time window, including:
when at least one worker receives the scheduling command, all workers execute in parallel; the scheduling command is periodically transmitted to the at least one worker at a specified scheduling interval;
each worker in the at least one worker sets a corresponding time window according to the current time, wherein the time windows are in the sequence of the starting time on the time axis, and no time overlap exists between two non-adjacent time windows;
each worker inquires the task table and acquires a timing task from the task table as a corresponding preparation task; the execution time of the corresponding preparation task of any worker in the task table is within the time window range corresponding to the worker, and the task state of each preparation task in the task table is to be executed;
and each worker executes the corresponding preparatory task and modifies the task state of the corresponding preparatory task in the task table to be complete.
Further, each worker of the at least one worker sets a respective corresponding time window according to the current time, including:
each worker acquires the current time;
each worker acquires a pre-configured corresponding time window starting deviant and a time window ending deviant respectively;
each worker backs a respective corresponding time window starting deviation value from the current time to the past time direction to obtain the starting time of the respective corresponding time window; returning the corresponding time window termination deviation values from the current time to the past time direction to obtain the termination time of the corresponding time windows;
and taking the time range from the starting time to the ending time of each worker as the corresponding time window of each worker.
Further, the value of the value range formed by the time window starting offset value and the time window ending offset value corresponding to each worker in the at least one worker is continuous and has no overlap.
Further, the method also comprises a respective preset time window length value of each worker in the at least one worker;
the respective time window for each worker can be expressed as: (T)sn,Ten) Wherein, TsnRepresents a start time, T, of a time window of an nth worker of the at least one workerenRepresenting a termination time of a time window for an nth worker of the at least one worker;
each worker of the at least one worker sets a corresponding time window according to the current time, and the specific steps are as follows:
each worker obtains the current time;
each worker obtains a preset time window length value of all workers in the at least one worker;
setting respective corresponding time windows of all the workers according to the current time and the preset time window length values of all the workers by the following formulas:
Tsn=Ts(n-1)-Dn
Ten=Ts(n-1)
wherein:
n is a positive integer greater than or equal to 1;
Ts(n-1)representing a start time of a time window for an (n-1) th worker of the at least one worker; when n is 1, Ts(n-1)Represents the current time;
Dna preset length of time representing a time window for an nth worker of the at least one worker.
Further, the specified scheduling interval is less than a width of a time window for each of the at least one worker.
Further, the scheduling command is periodically sent to the at least one worker at a specified scheduling interval, specifically: the scheduling command is periodically transmitted to the at least one worker at a scheduling interval of 5 seconds.
Further, the timing task is provided with a task lock;
each worker executes the corresponding preparation task and modifies the task state of the corresponding preparation task in the task table to be completed, and the method comprises the following steps:
each worker judges whether a task lock already exists in the corresponding preparation task;
if yes, the current worker gives up processing the corresponding preparation task and waits for receiving the next scheduling command;
if not, the current worker adds a task lock for the corresponding preparation task, executes the corresponding preparation task, modifies the task state of the corresponding preparation task in the task table into the execution completion, and finally deletes the task lock of the corresponding preparation task.
On the other hand, an embodiment of the present invention further provides a timed task execution device based on a time window, which is arranged on a worker, and includes:
the scheduling command receiving device is used for triggering the time window setting device to execute when receiving the scheduling command; the scheduling command is periodically sent to at least one worker at a specified scheduling interval to enable the workers to execute in parallel;
the time window setting device is used for setting corresponding time windows of the workers, wherein the time windows set for the workers in the at least one worker are not overlapped with each other in time according to the sequence of the starting time on a time axis;
the task acquisition device is used for inquiring the task table and acquiring the timing task from the task table as a corresponding preparation task of the worker; the execution time of the corresponding preparation tasks of the worker in the task table is within the time window range corresponding to the worker, and the task state of each preparation task in the task table is to be executed;
and the task execution device is used for executing the corresponding preparation task of the worker and modifying the task state of the corresponding preparation task in the task table into the execution completion.
Further, the time window setting device is specifically configured to:
acquiring current time;
acquiring a time window starting deviant and a time window ending deviant which are configured in advance and correspond to the worker;
returning a corresponding time window starting deviant of the worker from the current time to the past time direction to obtain the starting time of the corresponding time window of the worker; returning a time window termination deviant corresponding to the worker from the current time to the past time direction to obtain the termination time of the time window corresponding to the worker;
and taking the time range from the corresponding starting time to the corresponding ending time of the worker as the corresponding time window of the worker.
Furthermore, the value of the value range formed by the time window starting offset value and the time window ending offset value corresponding to each worker is continuous and has no overlap.
Further, the method also comprises a respective preset time window length value of each worker in the at least one worker;
the respective time window for each worker can be expressed as: (T)sn,Ten) Wherein, TsnRepresents a start time, T, of a time window of an nth worker of the at least one workerenRepresenting a termination time of a time window for an nth worker of the at least one worker;
the time window setting device is specifically configured to:
acquiring current time;
acquiring preset time window length values of all workers in the at least one worker;
setting respective corresponding time windows of the workers according to the current time and the preset time window length values of all the workers and the following formulas:
Tsn=Ts(n-1)-Dn
Ten=Ts(n-1)
wherein:
n is a positive integer greater than or equal to 1;
Ts(n-1)representing a start time of a time window for an (n-1) th worker of the at least one worker; when n is 1, Ts(n-1)Represents the current time;
Dna preset length of time representing a time window for an nth worker of the at least one worker.
Further, the specified scheduling interval is less than a width of a time window for each of the at least one worker.
Further, the scheduling command receiving apparatus is specifically configured to: when a scheduling command is received, triggering the time window setting device to execute; the scheduling command is periodically sent to the present worker at a scheduling interval of 5 seconds.
Further, the timing task is provided with a task lock;
the task execution device is specifically configured to:
judging whether a task lock exists in a corresponding preparation task of the worker;
if yes, the worker gives up processing the corresponding preparation task and waits for receiving the next scheduling command;
and if the task lock does not exist, the worker adds the task lock for the corresponding prepared task, executes the corresponding prepared task, modifies the task state of the corresponding prepared task in the task table into the execution completion state, and finally deletes the task lock of the corresponding prepared task.
On the other hand, an embodiment of the present invention further provides a timing task execution system based on a time window, including: at least one dispatcher, at least one worker, and a data device;
the at least one dispatcher, for periodically sending dispatch commands to the at least one worker at specified dispatch intervals;
the at least one worker, each worker setting any one of the timed task execution devices based on the time window;
and the data device is used for storing a task table, and the related information of the timing task is recorded in the task table.
Further, the data device is further configured to store a task lock table, where the task lock table records state information of whether a task lock of each timing task exists.
Further, the number of the at least one dispatcher is one, the dispatcher is used for periodically distributing the dispatching command to the at least one worker at a specified dispatching interval; or the number of the at least one dispatcher is consistent with the number of the at least one worker, a one-to-one correspondence relationship is established between the dispatchers, and each dispatcher periodically sends the dispatching command to a corresponding worker at a specified dispatching interval.
Different from the prior art, the technical scheme has the following beneficial effects:
according to the technical scheme of the invention, aiming at the single-point problem in the existing timed task scheduling system, the timed task execution method, the timed task execution device and the timed task execution system based on the time window are provided, one or more workers are scheduled periodically in parallel, the respective time window of each worker slides along the current time in a stepping mode with the scheduling interval as the scheduling interval, the time window range of each worker at the current time is determined, and the timed task with the execution time in the time window of each worker in the task table is inquired and executed, so that the single-point problem in the existing timed task scheduling system is solved, the thermal disaster recovery of the timed task scheduling system is realized, and the effect of quickly and automatically recovering the timed task without manual intervention after the fault occurs is achieved.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
FIG. 1 is a flowchart of a method for executing a timed task based on a time window according to one embodiment of the present invention;
FIG. 2 is a schematic diagram of a timing task execution device based on time windows according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of a system for executing a timed task based on a time window according to an embodiment of the present invention;
FIG. 4 is a diagram illustrating a process for scheduling and executing workers according to an embodiment of the present invention;
FIG. 5 is a schematic diagram illustrating the sliding of a time window with the current time according to an embodiment of the present invention;
FIG. 6 is a schematic diagram of another embodiment of a system for timing task execution based on time windows;
FIG. 7 is a flow diagram of a worker performing a preparation task according to one embodiment of the invention;
FIG. 8 is a diagram illustrating a discontinuity of adjacent time windows according to an embodiment of the present invention;
FIG. 9 is a diagram illustrating the succession of adjacent time windows according to one embodiment of the present invention;
FIG. 10 is a diagram illustrating a partial overlap of adjacent time windows according to an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
On the basis of fully analyzing the problems that a single point exists in the conventional timed task system, the system is not robust enough and has no disaster, and if the machine where the timed task is located is down and the timing function cannot be recovered quickly, the invention provides the highly available timed task method, the highly available timed task device and the highly available timed task system based on the time window through deep analysis of the requirements, application scenes and the conventional problems of the timed task system, so that the single point of the system is eliminated, the timed task can be recovered without manual intervention under the condition that the machine is down, the manpower is saved, the robustness of the system is improved, and a good method is provided for the highly available timed task system of the system for realizing automatic and quick recovery of faults.
Before explaining the embodiments of the present invention, it is specified that the time format used in the present specification uses the following two definitions: hh: mm: ss or hh: mm, e.g., 9:55 for 9 hours 55 minutes, 9:55:05 for 9 hours 55 minutes 05 seconds.
As shown in fig. 1, the method for executing a timed task based on a time window provided by the present invention includes the following steps:
101, when at least one worker receives a scheduling command, all workers execute in parallel; the scheduling command is periodically transmitted to the at least one worker at a specified scheduling interval;
102, each worker of the at least one worker sets a corresponding time window according to the current time, wherein the time windows are in the sequence of the starting time on a time axis, and no time is overlapped between two non-adjacent time windows;
103, each worker queries a task table, and acquires a timing task from the task table as a corresponding preparation task; the execution time of the corresponding preparation task of any worker in the task table is within the time window range corresponding to the worker, and the task state of each preparation task in the task table is to be executed;
and 104, each worker executes the corresponding preparation task, and modifies the task state of the corresponding preparation task in the task table to be execution completion.
As explained further below, in step 101, when at least one worker receives a scheduling command, the workers execute in parallel; issuing, by a dispatcher, a dispatch command to periodically send to the at least one worker at a specified dispatch interval; logically, as shown in fig. 3, one dispatcher may correspondingly dispatch one or more workers, at this time, the multiple workers run in parallel without interfering with each other, and when any worker fails, other workers can still continue to execute the timing task; the shared dispatcher is only responsible for dispatching the workers, so that the fault rate of the dispatcher is low, and the single-point problem of a timing task system can be avoided to a certain extent; preferably, in order to avoid the single-point problem to the greatest extent, logically, as shown in fig. 6, one dispatcher may also dispatch one worker to form a group of dispatching relationships, a plurality of dispatchers and a plurality of workers form a plurality of groups of dispatching relationships in one-to-one correspondence, and each group of dispatching relationships run in parallel without interfering with each other, for example, when the first dispatcher dispatches the first worker, the second dispatcher dispatches the second worker, and the third dispatcher dispatches the third worker; all workers access the task table in parallel, so that the single-point problem caused by the fault of the dispatcher is further avoided. In deployment, a dispatcher and a worker can be deployed in consideration of fault characteristics of a specific project, for example, the specific project mainly considers the problem of stable execution of a timing task in the same machine, and mainly needs to avoid a process fault in the same machine, and at this time, the dispatcher and the worker can be deployed on the same machine according to the aforementioned logical relationship; for another example, if it is necessary to avoid a failure of software and hardware including a machine downtime in a specific project, one calling device may be separately deployed on one machine according to the above logical relationship, and multiple workers are respectively deployed on respective machines to form a logical relationship in which one caller corresponds to multiple workers running in parallel, and a machine in which any worker is located is down without affecting the running of other workers, so that the timed task system can still run normally. The foregoing examples of deployment are provided for illustrating and understanding the present embodiment, and are not intended to limit the specific implementation of the present embodiment, and other deployment manners not listed are considered to be within the scope of the present invention as long as the foregoing logical relationship between the dispatcher and the worker in the present description is met. In step 101, the value of the scheduling interval may be defined according to the response delay allowed by the timing task in the specific project; the smaller the scheduling interval, the faster the response the timed task is executed when the timing time of the timed task arrives, otherwise the slower the response. After each worker is scheduled to execute, step 102 is executed, each worker sets a respective corresponding time window according to the current time, wherein each time window is in the sequence of the starting time on the time axis, and no time is overlapped between two non-adjacent time windows; in the order of the start times of the time windows on the time axis, the time windows between two adjacent time windows may be discontinuous as shown in fig. 8 or continuous as shown in fig. 9 or partially overlapped with one end of the end time of the time window on the past time side and one end of the start time of the time window on the future time side among the two adjacent time windows as shown in fig. 10; in specific implementation, a portion between respective corresponding time windows of each worker can be shown in fig. 8, another portion is shown in fig. 9, and yet another portion is shown in fig. 10; it is within the scope of the present invention that the respective time windows of the workers correspond to each other in whole or in part according to the sequence of the start times on the time axis of the time windows shown in fig. 8 and/or fig. 9 and/or fig. 10, and that no time overlap exists between two non-adjacent time windows. And each time the worker schedules execution, the time window corresponding to the worker is set according to the current time. Specifically, since the scheduling command is periodically transmitted to the at least one worker at a specified scheduling interval, each worker sets a respective corresponding time window according to the current time when the scheduling command is received at this time each time the worker receives the scheduling command, the respective corresponding time window of each worker slides along the time axis in steps of the scheduling interval following the lapse of the current time; the time windows corresponding to the workers may be continuous in time, for example, the time window of the worker1 is (9:55,10:00), the time window of the worker2 is (9:25,9: 55), the time window corresponding to the worker may be discontinuous in time, for example, the time window of the worker1 is (9:55,10:00), the time window of the worker2 is (9:25,9: 40), there may be a partial overlap between adjacent time windows due to the fact that the workers cannot completely synchronize scheduling, for example, the time window of the worker1 is (9:55:00,10:00: 00), the time window of the worker2 is (9:25:05,9:55: 05), and at this time, the time length of the overlap portion is mainly related to the scheduling interval and is also related to the error between the current times acquired by the workers, the time length of the overlapping part can be controlled to be smaller than the width of the time window of each worker by setting the scheduling interval and the width of each time window, and the influence of the overlapping part on the specific implementation of the embodiment of the invention can be eliminated by combining the property that the time window slides along with the current time. The overlapping of the time windows may also be avoided by setting the time windows corresponding to the workers to be discontinuous in time, for example, the interval between two adjacent time windows may be set to be greater than a preset interval value, which may be equal to the maximum system time error between the scheduling interval processing authors plus a custom margin time, for example, assuming that the scheduling interval is 5 seconds, the maximum system time error between the workers is 1 second, and the custom margin time may be 1 second, which is 7 seconds. In the above examples, for example, (9:55,10:00) the time window uses the time range representation of the left-side open interval and the right-side closed interval, and in the specific implementation, for example, [9:55,10:00) the time window uses the time range representation of the left-side closed interval and the right-side open interval; the time window may also use a time range representation of left-hand open interval, right-hand open interval, for example (9:55,10: 00); 103, each worker carries respective time window information to query a task table, wherein the basic content of the task table is shown in table 1; at this time, because the multiple workers run in parallel, the multiple workers can simultaneously query the task table or randomly and successively query the task table; each worker acquires the time to be executed, namely Exe _ time, in the time window of each worker and the state, namely status, as the time task to be executed from the task table as a respective prepared task; and step 104, each worker executes the corresponding preparatory task, and modifies the task state of the corresponding preparatory task in the task table to be execution completion, so that the timed task which is executed is prevented from being repeatedly executed by other workers. The timed task processing procedure is described below in the case of two workers, and the time window of each worker is updated according to the current time at each scheduling, so that the time window of each worker slides along with the lapse of the current time; as shown in fig. 4, in which Quartz, i.e., a scheduler, is used to periodically schedule a first worker, i.e., worker1 and a second worker, i.e., worker2, at specified time intervals, a time window 1 of the first worker and a time window 2 of the second worker are sequentially arranged with a current time as a starting point, and follow the lapse of the current time to slide on execution time data columns in a task table along a time axis; the execution time in the task table in fig. 4 is the time to be executed; in the task table, the state slipped by the first worker's time window is the timed task to be executed, will be read out by the first worker for execution, and will mark the state as execution complete. The time window of the second worker follows the time window of the first worker, and although the second worker and the first worker read the task table at the same time, the timed tasks corresponding to the time window of the second worker are all slipped through the window of the first worker, so that the state of the timed tasks inquired by the second worker and other workers is execution completion during the normal work of the first worker, and all the timed tasks are completed by the first worker. When the first worker fails, timing tasks within a time range occupied by the time window of the first worker at the current time cannot be executed by the first worker, and the timing tasks need to wait for the time window of the second worker to slide over the time window of the first worker before being read out for execution by the second worker, and the state is marked as execution completion. At the moment, the first worker fails to execute the timing task, the second worker takes over the timing task automatically and continues to execute the timing task, the thermal disaster recovery effect is achieved, and the single-point failure in the prior art is avoided. When the first worker fails, the timed task to be executed will be delayed from being executed for a preset length of time of the first worker's time window. If it is desired to reduce this delay, the width of the first worker's time window may be shortened. The way of working in the case of three or more workers can be understood from the above explanation of two workers as an example.
Field(s) Type (B) Use of
Task_id Long shaping type Primary key ofUniquely determining tasks
status Short shaping type Task state, divided into waiting execution, during execution, execution is completed
Biz_id Long shaping type Concrete service main key
Exe_time Time stamp Wait for execution time
Create_time Time stamp Task creation time
TABLE 1 task Table basic Structure
Further, each worker of the at least one worker sets a respective corresponding time window according to the current time, including:
each worker acquires the current time;
each worker acquires a pre-configured corresponding time window starting deviant and a time window ending deviant respectively;
each worker backs a respective corresponding time window starting deviation value from the current time to the past time direction to obtain the starting time of the respective corresponding time window; returning the corresponding time window termination deviation values from the current time to the past time direction to obtain the termination time of the corresponding time windows;
and taking the time range from the starting time to the ending time of each worker as the corresponding time window of each worker.
The above is a method for setting a respective time window for each worker, the method using a current time, a time window start offset value and a time window end offset value for each worker to determine a respective time window for each worker; for example, when the time is 10:00, the time window start offset value of the worker1 is 5 minutes, the time window end offset value is 0 minutes, the time window start offset value of the worker2 is 30 minutes, and the time window end offset value is 5 minutes, the time window of the worker1 is (9:55,10:00), the time window of the worker2 is (9:25,9:55], this is a case where time values between time windows are continuous, the foregoing has described that time values between time windows may be discontinuous, or overlap of partial times between adjacent time windows may exist, which is not described herein again by way of example, in specific implementation, for example, the current time may be obtained by each worker calling a system time function, further, for example, the time window start offset value and the time window end offset value corresponding to each worker may be written into a configuration file of each worker in advance, and each worker acquires the time window starting offset value and the time window ending offset value corresponding to each worker from the configuration file before the time window is set for the first time or before the time window is set each time.
Preferably, the first worker, worker1, has a time window starting offset value of 10 minutes and the first worker's time window ending offset value of 0 minutes; the start offset value of the time window for the second worker, worker2, was 1440 minutes and the end offset value of the time window for the second worker was 10 minutes.
Further, the value of the value range formed by the time window starting offset value and the time window ending offset value corresponding to each worker in the at least one worker is continuous and has no overlap.
The value ranges formed by the time window starting offset value and the time window ending offset value corresponding to the workers are configured in advance to be continuous in value and have no overlap, so that the time windows corresponding to the workers are set to be continuous; according to the scheme, time windows of respective responses of all the workers can be set to be continuous, and corresponding time windows of each worker can be set to be continuous.
Further, as another method for setting a respective corresponding time window for each worker, the method includes setting a respective preset time window length value for each worker in the at least one worker;
the respective time window for each worker can be expressed as: (T)sn,Ten) Wherein, TsnRepresents a start time, T, of a time window of an nth worker of the at least one workerenRepresenting a termination time of a time window for an nth worker of the at least one worker;
each worker of the at least one worker sets a corresponding time window according to the current time, and the specific steps are as follows:
each worker obtains the current time;
each worker obtains a preset time window length value of all workers in the at least one worker;
setting respective corresponding time windows of all the workers according to the current time and the preset time window length values of all the workers by the following formulas:
Tsn=Ts(n-1)-Dn
Ten=Ts(n-1)
wherein:
n is a positive integer greater than or equal to 1;
Ts(n-1)representing a start time of a time window for an (n-1) th worker of the at least one worker; when n is 1, Ts(n-1)Represents the current time;
Dna preset length of time representing a time window for an nth worker of the at least one worker.
The following is specific to the first worker and the second workerThe two worker cases illustrate the process of the workers updating their respective time windows in the present scenario. Assume a preset length of time D for a first worker15 minutes, preset length of time D for the second worker230 minutes;
when the first worker and the second worker are scheduled to execute in parallel, the current time T is acquireds010:00:00, then at this point, the first worker's time window (T) is updated according to the above-described step of updating the time windows1,Te1) To (9:55:00,10:00: 00), time window (T) of the second workers2,Te2) Is (9:25:00, 9:55: 00);
when the first worker and the second worker are executed in parallel scheduling again, the current time T is acquireds010:35:00, then at this point, the first worker's time window (T) is based on the step of updating the time window described aboves1,Te1) To (10:30:00, 10:35:00), time window (T) of the second workers2,Te2) Is (10:00:00, 10:30: 00);
preferably, the window width for the first worker is 10 minutes and the window width for the second worker is 1430 minutes;
at each scheduling, the time window of each worker is updated according to the current time, so the time window of each worker slides along with the lapse of the current time; as shown in fig. 4, in which Quartz, i.e., a scheduler, is used to periodically schedule a first worker, i.e., worker1 and a second worker, i.e., worker2, at specified time intervals, a time window 1 of the first worker and a time window 2 of the second worker are sequentially arranged, starting from a current time, following the lapse of the current time, in a task table at an execution time, i.e., a to-be-executed time, sliding on a data column; in the task table, the state slipped by the first worker's time window is the timed task to be executed, will be read out by the first worker for execution, and will mark the state as execution complete. The time window of the second worker follows the time window of the first worker, and although the second worker and the first worker read the task table at the same time, the timed tasks corresponding to the time window of the second worker are all slipped through the window of the first worker, so that the state of the timed tasks inquired by the second worker and other workers is execution completion during the normal work of the first worker, and all the timed tasks are completed by the first worker. When the first worker fails, timing tasks within a time range occupied by the time window of the first worker at the current time cannot be executed by the first worker, and the timing tasks need to wait for the time window of the second worker to slide over the time window of the first worker before being read out for execution by the second worker, and the state is marked as execution completion. At the moment, the first worker fails to execute the timing task, the second worker takes over the timing task automatically and continues to execute the timing task, the thermal disaster recovery effect is achieved, and the single-point failure in the prior art is avoided. When the first worker fails, the timed task to be executed will be delayed from being executed for a preset length of time of the first worker's time window. If it is desired to reduce this delay, the preset length of time of the first worker's time window may be shortened.
The above two workers are used as examples, and are not intended to limit the present invention, and one or two or more workers may be used in the specific implementation, and all fall within the scope of the present invention.
Since the time window is slid with the current time each time a worker is dispatched, the range of the time window may or may not include TsnAnd TenThe range of the time window can be expressed as (T)sn,Ten) Or [ Tsn,Ten) Or (T)sn,Ten]And the like, which fall within the scope of the present invention, as long as the time windows corresponding to the settings of the respective workers are sequentially arranged along the time axis.
Further, the specified scheduling interval is less than a width of a time window for each of the at least one worker.
Preferably, the scheduling interval is less than a preset length of time of the time window for all workers; when the time to be executed of the timing tasks in the task table is distributed densely, the scheduling interval is smaller than the preset time length of the time windows of all the workers, and the first worker can be prevented from missing the timing tasks to be executed. When a timed task to be performed is missed by a first worker, it will be read and executed within the time window of a second or other worker.
Further, the scheduling command is periodically sent to the at least one worker at a specified scheduling interval, specifically: the scheduling command is periodically transmitted to the at least one worker at a scheduling interval of 5 seconds.
Specifically, the time interval at which the worker is scheduled may be determined according to the response delay requirement of the specific project on the timing task, where if the scheduled time interval is large, the response delay is large, and otherwise, the response delay is small; preferably, the scheduling interval may be set to 5 seconds.
Further, the timing task is provided with a task lock;
each worker executes the corresponding preparation task and modifies the task state of the corresponding preparation task in the task table to be completed, and the method comprises the following steps:
each worker judges whether a task lock exists in the corresponding preparation task;
if yes, the current worker gives up processing the corresponding preparation task and waits for receiving the next scheduling command;
if not, the current worker adds a task lock for the corresponding preparation task, executes the corresponding preparation task, modifies the task state of the corresponding preparation task in the task table into the execution completion, and finally deletes the task lock of the corresponding preparation task.
In order to further improve the stability of the embodiment of the present invention, as shown in fig. 7, where worker1 is worker1, and worker2 is worker2, the present invention provides a task lock mechanism to solve the problem that worker1 acquires a task _ id of 1 and is processing a preparation task with the task _ id of 1, but has not completed processing yet, and during this period, worker2 also acquires the task _ id of 1 as its corresponding preparation task. Taking worker1 as an example, as can be seen from fig. 7, worker1 obtains a timing task with task identifier, namely task _ id ═ 1, from a task library, namely a task table, as a preparatory task, worker1 queries a task lock table, namely lock mysql/redis, where the task lock table may be stored in a database such as mysql, may also be a record file, and may also be a redis record in a memory; the worker1 judges the query result, and if the task _ id queried by the worker1 already exists in the task lock table, the worker1 abandons executing the preparation task with the task _ id of 1 and waits for the next scheduling command; if the worker1 inquires that the task _ id is not in the task lock table, the worker1 writes the task lock with the task _ id of 1 into the task lock table, executes the preparation task of the worker1, which is the timing task corresponding to the task _ id of 1, modifies the state of the timing task in the task table to be the execution completion, and finally deletes the task lock record of the timing task with the task _ id of 1 in the task lock table.
On the other hand, as shown in fig. 2, an embodiment of the present invention further provides a timed task execution device based on a time window, which is disposed on a worker, and includes:
scheduling command receiving means 201, configured to trigger the time window setting means to execute when receiving the scheduling command; the scheduling command is periodically sent to at least one worker at a specified scheduling interval to enable the workers to execute in parallel;
a time window setting device 202, configured to set a corresponding time window for the worker, where, for the time window set for each worker in the at least one worker, the time windows are in an order of a start time on a time axis, and there is no time overlap between two non-adjacent time windows;
the task acquisition device 203 is used for inquiring the task table and acquiring the timing task from the task table as a corresponding preparation task of the worker; the execution time of the corresponding preparation tasks of the worker in the task table is within the time window range corresponding to the worker, and the task state of each preparation task in the task table is to be executed;
and the task executing device 204 is used for executing the corresponding preparation task of the worker and modifying the task state of the corresponding preparation task in the task table into the execution completion.
As explained further below, in the scheduling command receiving means 201, when at least one worker receives a scheduling command, the workers execute in parallel; issuing, by a dispatcher, a dispatch command to periodically send to the at least one worker at a specified dispatch interval; logically, as shown in fig. 3, one dispatcher may correspondingly dispatch one or more workers, at this time, the multiple workers run in parallel without interfering with each other, and when any worker fails, other workers can still continue to execute the timing task; the shared dispatcher is only responsible for dispatching the workers, so that the fault rate of the dispatcher is low, and the single-point problem of a timing task system can be avoided to a certain extent; preferably, in order to avoid the single-point problem to the greatest extent, logically, as shown in fig. 6, one dispatcher may also dispatch one worker to form a group of dispatching relationships, a plurality of dispatchers and a plurality of workers form a plurality of groups of dispatching relationships in one-to-one correspondence, and each group of dispatching relationships run in parallel without interfering with each other, for example, when the first dispatcher dispatches the first worker, the second dispatcher dispatches the second worker, and the third dispatcher dispatches the third worker; all workers access the task table in parallel, so that the single-point problem caused by the fault of the dispatcher is further avoided. In deployment, a dispatcher and a worker can be deployed in consideration of fault characteristics of a specific project, for example, the specific project mainly considers the problem of stable execution of a timing task in the same machine, and mainly needs to avoid a process fault in the same machine, and at this time, the dispatcher and the worker can be deployed on the same machine according to the aforementioned logical relationship; for another example, if it is necessary to avoid a failure of software and hardware including a machine downtime in a specific project, one calling device may be separately deployed on one machine according to the above logical relationship, and multiple workers are respectively deployed on respective machines to form a logical relationship in which one caller corresponds to multiple workers running in parallel, and a machine in which any worker is located is down without affecting the running of other workers, so that the timed task system can still run normally. The foregoing examples of deployment are provided for illustrating and understanding the present embodiment, and are not intended to limit the specific implementation of the present embodiment, and other deployment manners not listed are considered to be within the scope of the present invention as long as the foregoing logical relationship between the dispatcher and the worker in the present description is met. In the scheduling command receiving apparatus 201, the value of the scheduling interval may be defined according to the response delay allowed by the timing task in the specific project; the smaller the scheduling interval, the faster the response the timed task is executed when the timing time of the timed task arrives, otherwise the slower the response. After each worker is scheduled to execute, the time window setting device 202 is executed, and the worker sets a corresponding time window according to the current time, wherein the time windows are in the sequence of the starting time on the time axis, and no time is overlapped between two non-adjacent time windows; in order of the start times of the time windows on the time axis, the time windows between two adjacent time windows may be discontinuous as shown in fig. 8 or continuous as shown in fig. 9 or one end of the end time of the time window on the past time side among the two adjacent time windows as shown in fig. 10 overlaps with one end of the start time of the time window on the future time side; in specific implementation, a portion between respective corresponding time windows of each worker can be shown in fig. 8, another portion is shown in fig. 9, and yet another portion is shown in fig. 10; it is within the scope of the present invention that the respective time windows of the workers correspond to each other in whole or in part according to the sequence of the start times on the time axis of the time windows shown in fig. 8 and/or fig. 9 and/or fig. 10, and that no time overlap exists between two non-adjacent time windows. And each time the worker schedules execution, the time window corresponding to the worker is set according to the current time. Specifically, since the scheduling command is periodically transmitted to the at least one worker at a specified scheduling interval, each worker sets a respective corresponding time window according to the current time when the scheduling command is received at this time each time the worker receives the scheduling command, the respective corresponding time window of each worker slides along the time axis in steps of the scheduling interval following the lapse of the current time; the time windows corresponding to the workers may be continuous in time, for example, the time window of the worker1 is (9:55,10:00), the time window of the worker2 is (9:25,9: 55), the time window corresponding to the worker may be discontinuous in time, for example, the time window of the worker1 is (9:55,10:00), the time window of the worker2 is (9:25,9: 40), there may be a partial overlap between adjacent time windows due to the fact that the workers cannot completely synchronize scheduling, for example, the time window of the worker1 is (9:55:00,10:00: 00), the time window of the worker2 is (9:25:05,9:55: 05), and at this time, the time length of the overlap portion is mainly related to the scheduling interval and is also related to the error between the current times acquired by the workers, the time length of the overlapping part can be controlled to be smaller than the width of the time window of each worker by setting the scheduling interval and the width of each time window, and the influence of the overlapping part on the specific implementation of the embodiment of the invention can be eliminated by combining the property that the time window slides along with the current time. The overlapping of the time windows may also be avoided by setting the time windows corresponding to the workers to be discontinuous in time, for example, the interval between two adjacent time windows may be set to be greater than a preset interval value, which may be equal to the maximum system time error between the scheduling interval processing authors plus a custom margin time, for example, assuming that the scheduling interval is 5 seconds, the maximum system time error between the workers is 1 second, and the custom margin time may be 1 second, which is 7 seconds. In the above examples, for example, (9:55,10:00) the time window uses the time range representation of the left-side open interval and the right-side closed interval, and in the specific implementation, for example, [9:55,10:00) the time window uses the time range representation of the left-side closed interval and the right-side open interval; the time window may also use a time range representation of left-hand open interval, right-hand open interval, for example (9:55,10: 00); the task acquisition device 203 is used for inquiring a task table with corresponding time window information carried by each worker, and the basic content of the task table is shown in table 1; at this time, because the multiple workers run in parallel, the multiple workers can simultaneously query the task table or randomly and successively query the task table; each worker acquires the time to be executed, namely Exe _ time, in the time window of each worker and the state, namely status, as the time task to be executed from the task table as a respective prepared task; and the task executing device 204 is used for executing the corresponding preparatory task by each worker and modifying the task state of the corresponding preparatory task in the task table into the execution completion, so that the timed task which is executed is prevented from being repeatedly executed by other workers. The timed task processing procedure is described below in the case of two workers, and the time window of each worker is updated according to the current time at each scheduling, so that the time window of each worker slides along with the lapse of the current time; as shown in fig. 4, in which Quartz, i.e., a scheduler, is used to periodically schedule a first worker, i.e., worker1 and a second worker, i.e., worker2, at specified time intervals, a time window 1 of the first worker and a time window 2 of the second worker are sequentially arranged with a current time as a starting point, and follow the lapse of the current time to slide on execution time data columns in a task table along a time axis; the execution time in the task table in fig. 4 is the time to be executed; in the task table, the state slipped by the first worker's time window is the timed task to be executed, will be read out by the first worker for execution, and will mark the state as execution complete. The time window of the second worker follows the time window of the first worker, and although the second worker and the first worker read the task table at the same time, the timed tasks corresponding to the time window of the second worker are all slipped through the window of the first worker, so that the state of the timed tasks inquired by the second worker and other workers is execution completion during the normal work of the first worker, and all the timed tasks are completed by the first worker. When the first worker fails, timing tasks within a time range occupied by the time window of the first worker at the current time cannot be executed by the first worker, and the timing tasks need to wait for the time window of the second worker to slide over the time window of the first worker before being read out for execution by the second worker, and the state is marked as execution completion. At the moment, the first worker fails to execute the timing task, the second worker takes over the timing task automatically and continues to execute the timing task, the thermal disaster recovery effect is achieved, and the single-point failure in the prior art is avoided. When the first worker fails, the timed task to be executed will be delayed from being executed for a preset length of time of the first worker's time window. If it is desired to reduce this delay, the width of the first worker's time window may be shortened. The way of working in the case of three or more workers can be understood from the above explanation of two workers as an example.
Further, the time window setting device 202 is specifically configured to:
acquiring current time;
acquiring a time window starting deviant and a time window ending deviant which are configured in advance and correspond to the worker;
returning a corresponding time window starting deviant of the worker from the current time to the past time direction to obtain the starting time of the corresponding time window of the worker; returning a time window termination deviant corresponding to the worker from the current time to the past time direction to obtain the termination time of the time window corresponding to the worker;
and taking the time range from the corresponding starting time to the corresponding ending time of the worker as the corresponding time window of the worker.
The above is a method for setting a time window corresponding to the worker, in which the method uses the current time, the time window starting offset value and the time window ending offset value of the worker to determine the time window corresponding to the worker; for example, when the time is 10:00, the time window start offset value of the worker1 is 5 minutes, the time window end offset value is 0 minutes, the time window start offset value of the worker2 is 30 minutes, and the time window end offset value is 5 minutes, the time window of the worker1 is (9:55,10:00), the time window of the worker2 is (9:25,9:55], this is a case where time values between time windows are continuous, the foregoing has described that time values between time windows may be discontinuous, or overlap of partial times between adjacent time windows may exist, which is not described herein again by way of example, in specific implementation, for example, the current time may be obtained by each worker calling a system time function, further, for example, the time window start offset value and the time window end offset value corresponding to each worker may be written into a configuration file of each worker in advance, and each worker acquires the time window starting offset value and the time window ending offset value corresponding to each worker from the configuration file before the time window is set for the first time or before the time window is set each time.
Preferably, the first worker, worker1, has a time window starting offset value of 10 minutes and the first worker's time window ending offset value of 0 minutes; the start offset value of the time window for the second worker, worker2, was 1440 minutes and the end offset value of the time window for the second worker was 10 minutes.
Furthermore, the value of the value range formed by the time window starting offset value and the time window ending offset value corresponding to each worker is continuous and has no overlap.
The value ranges formed by the time window starting offset value and the time window ending offset value corresponding to the workers are configured in advance to be continuous in value and have no overlap, so that the time windows corresponding to the workers are set to be continuous; according to the scheme, time windows of respective responses of all the workers can be set to be continuous, and corresponding time windows of each worker can be set to be continuous.
Further, the method also comprises a respective preset time window length value of each worker in the at least one worker;
the respective time window for each worker can be expressed as: (T)sn,Ten) Wherein, TsnIndicating a time of an nth worker of the at least one workerStarting time of window, TenRepresenting a termination time of a time window for an nth worker of the at least one worker;
the time window setting device 202 is specifically configured to:
acquiring current time;
acquiring preset time window length values of all workers in the at least one worker;
setting respective corresponding time windows of the workers according to the current time and the preset time window length values of all the workers and the following formulas:
Tsn=Ts(n-1)-Dn
Ten=Ts(n-1)
wherein:
n is a positive integer greater than or equal to 1;
Ts(n-1)representing a start time of a time window for an (n-1) th worker of the at least one worker; when n is 1, Ts(n-1)Represents the current time;
Dna preset length of time representing a time window for an nth worker of the at least one worker.
The following describes the process of updating the respective time windows by the workers in the present scenario, specifically in the case of two workers, namely, the first worker and the second worker. Assume a preset length of time D for a first worker15 minutes, preset length of time D for the second worker230 minutes;
when the first worker and the second worker are scheduled to execute in parallel, the current time T is acquireds010:00:00, then at this point, the first worker's time window (T) is updated according to the above-described step of updating the time windows1,Te1) To (9:55:00,10:00: 00), time window (T) of the second workers2,Te2) Is (9:25:00, 9:55: 00);
when the first worker and the second worker are executed in parallel scheduling again, the current time T is acquireds010:35:00, then according to the above-mentioned update time windowA time window (T) of the first workers1,Te1) To (10:30:00, 10:35:00), time window (T) of the second workers2,Te2) Is (10:00:00, 10:30: 00);
preferably, the window width for the first worker is 10 minutes and the window width for the second worker is 1430 minutes;
at each scheduling, the time window of each worker is updated according to the current time, so the time window of each worker slides along with the lapse of the current time; as shown in fig. 4, in which Quartz, i.e., a scheduler, is used to periodically schedule a first worker, i.e., worker1 and a second worker, i.e., worker2, at specified time intervals, a time window 1 of the first worker and a time window 2 of the second worker are sequentially arranged, starting from a current time, following the lapse of the current time, in a task table at an execution time, i.e., a to-be-executed time, sliding on a data column; in the task table, the state slipped by the first worker's time window is the timed task to be executed, will be read out by the first worker for execution, and will mark the state as execution complete. The time window of the second worker follows the time window of the first worker, and although the second worker and the first worker read the task table at the same time, the timed tasks corresponding to the time window of the second worker are all slipped through the window of the first worker, so that the state of the timed tasks inquired by the second worker and other workers is execution completion during the normal work of the first worker, and all the timed tasks are completed by the first worker. When the first worker fails, timing tasks within a time range occupied by the time window of the first worker at the current time cannot be executed by the first worker, and the timing tasks need to wait for the time window of the second worker to slide over the time window of the first worker before being read out for execution by the second worker, and the state is marked as execution completion. At the moment, the first worker fails to execute the timing task, the second worker takes over the timing task automatically and continues to execute the timing task, the thermal disaster recovery effect is achieved, and the single-point failure in the prior art is avoided. When the first worker fails, the timed task to be executed will be delayed from being executed for a preset length of time of the first worker's time window. If it is desired to reduce this delay, the preset length of time of the first worker's time window may be shortened.
The above two workers are used as examples, and are not intended to limit the present invention, and one or two or more workers may be used in the specific implementation, and all fall within the scope of the present invention.
Since the time window is slid with the current time each time a worker is dispatched, the range of the time window may or may not include TsnAnd TenThe range of the time window can be expressed as (T)sn,Ten) Or [ Tsn,Ten) Or (T)sn,Ten]And the like, which fall within the scope of the present invention, as long as the time windows corresponding to the settings of the respective workers are sequentially arranged along the time axis.
Further, the specified scheduling interval is less than a width of a time window for each of the at least one worker.
Preferably, the scheduling interval is less than a preset length of time of the time window for all workers; when the time to be executed of the timing tasks in the task table is distributed densely, the scheduling interval is smaller than the preset time length of the time windows of all the workers, and the first worker can be prevented from missing the timing tasks to be executed. When a timed task to be performed is missed by a first worker, it will be read and executed within the time window of a second or other worker.
Further, the scheduling command receiving apparatus 201 is specifically configured to: when a scheduling command is received, triggering the time window setting device 202 to execute; the scheduling command is periodically sent to the present worker at a scheduling interval of 5 seconds.
Specifically, the time interval at which the worker is scheduled may be determined according to the response delay requirement of the specific project on the timing task, where if the scheduled time interval is large, the response delay is large, and otherwise, the response delay is small; preferably, the scheduling interval may be set to 5 seconds.
Further, the timing task is provided with a task lock;
the task execution device 204 is specifically configured to:
judging whether a task lock exists in a corresponding preparation task of the worker;
if yes, the worker gives up processing the corresponding preparation task and waits for receiving the next scheduling command;
if not, the worker adds a task lock for the corresponding preparation task, executes the corresponding preparation task, modifies the task state of the corresponding preparation task in the task table into the execution completion, and finally deletes the task lock of the corresponding preparation task.
In order to further improve the stability of the embodiment of the present invention, as shown in fig. 7, where worker1 is worker1, and worker2 is worker2, the present invention provides a task lock mechanism to solve the problem that worker1 acquires a task _ id of 1 and is processing a preparation task with the task _ id of 1, but has not completed processing yet, and during this period, worker2 also acquires the task _ id of 1 as its corresponding preparation task. Taking worker1 as an example, as can be seen from fig. 7, worker1 obtains a timing task with task identifier, namely task _ id ═ 1, from a task library, namely a task table, as a preparatory task, worker1 queries a task lock table, namely lock mysql/redis, where the task lock table may be stored in a database such as mysql, may also be a record file, and may also be a redis record in a memory; the worker1 judges the query result, and if the task _ id queried by the worker1 already exists in the task lock table, the worker1 abandons executing the preparation task with the task _ id of 1 and waits for the next scheduling command; if the worker1 inquires that the task _ id is not in the task lock table, the worker1 writes the task lock with the task _ id of 1 into the task lock table, executes the preparation task of the worker1, which is the timing task corresponding to the task _ id of 1, modifies the state of the timing task in the task table to be the execution completion, and finally deletes the task lock record of the timing task with the task _ id of 1 in the task lock table.
On the other hand, as shown in fig. 3 and fig. 6, an embodiment of the present invention further provides a system for executing a timed task based on a time window, including: at least one dispatcher, at least one worker, and a data device;
the at least one dispatcher, for periodically sending dispatch commands to the at least one worker at specified dispatch intervals;
the at least one worker, each worker setting any one of the timed task execution devices based on the time window;
and the data device is used for storing a task table, and the related information of the timing task is recorded in the task table.
Furthermore, the value of the value range formed by the time window starting offset value and the time window ending offset value corresponding to each worker is continuous and has no overlap.
The time window starting deviant and the time window ending deviant which correspond to each worker are preset, the time window starting deviant and the time window ending deviant which correspond to each worker can be written into the configuration file of each worker, and the numerical value between the value ranges which are formed by the time window starting deviant and the time window ending deviant and correspond to each worker is set to be continuous, so that the time windows which correspond to each worker are set to be continuous, and the worker which is used as disaster recovery can automatically recover the execution of the timing task in the shortest possible time when the worker of the time window where the current time is located breaks down.
Further, the specified scheduling interval is less than a width of a time window for each of the at least one worker.
Preferably, the scheduling interval is less than a preset length of time of the time window for all workers; when the time to be executed of the timing tasks in the task table is distributed densely, the scheduling interval is smaller than the preset time length of the time windows of all the workers, and the first worker can be prevented from missing the timing tasks to be executed. When a timed task to be performed is missed by a first worker, it will be read and executed within the time window of a second or other worker.
Further, the data device is further configured to store a task lock table, where the task lock table records state information of whether a task lock of each timing task exists.
In order to further improve the stability of the embodiment of the present invention, as shown in fig. 7, where worker1 is worker1, and worker2 is worker2, the present invention provides a task lock mechanism to solve the problem that worker1 acquires a task _ id of 1 and is processing a preparation task with the task _ id of 1, but has not completed processing yet, and during this period, worker2 also acquires the task _ id of 1 as its corresponding preparation task. Taking worker1 as an example, as can be seen from fig. 7, worker1 obtains a timing task with task identifier, namely task _ id ═ 1, from a task library, namely a task table, as a preparatory task, worker1 queries a task lock table, namely lock mysql/redis, where the task lock table may be stored in a database such as mysql, may also be a record file, and may also be a redis record in a memory; the worker1 judges the query result, and if the task _ id queried by the worker1 already exists in the task lock table, the worker1 abandons executing the preparation task with the task _ id of 1 and waits for the next scheduling command; if the worker1 inquires that the task _ id is not in the task lock table, the worker1 writes the task lock with the task _ id of 1 into the task lock table, executes the preparation task of the worker1, which is the timing task corresponding to the task _ id of 1, modifies the state of the timing task in the task table to be the execution completion, and finally deletes the task lock record of the timing task with the task _ id of 1 in the task lock table.
Further, as shown in fig. 3, the number of the at least one dispatcher is one, and the dispatcher is used for periodically distributing the dispatching command to the at least one worker at a specified dispatching interval; or, as shown in fig. 6, the number of the at least one dispatcher is consistent with the number of the at least one worker, and a one-to-one correspondence relationship is established between the dispatchers, and each dispatcher periodically sends the dispatching command to a corresponding worker at a specified dispatching interval.
Further, the system can further comprise a task lock device, wherein the task lock device is used for saving a task lock table, and the task lock table is used for inquiring, creating and deleting task locks associated with preparation tasks corresponding to the workers by the workers. At this time, the data device is used for storing a task table read and written by the at least one worker, and the information of the timing task is stored in the task table;
those skilled in the art can understand the implementation of a high availability timed task system based on a time window according to the foregoing specific embodiments of a high availability timed task method based on a time window and a high availability timed task apparatus based on a time window, and will not be described herein again.
The present invention is described below in still another embodiment.
The implementation of a Quartz scheduling framework, i.e., a dispatcher, a management Worker, i.e., a Worker, is deployed on at least two machines for disaster recovery. Where Quartz is only responsible for starting and closing the Worker and scheduling rules. For example, Worker every 5 seconds. And the Worker is responsible for acquiring tasks from the task list and executing specific service tasks. The whole process is shown in fig. 4.
The worker is deployed on 2 machines, respectively, and is marked as worker1, namely a first worker, and worker2, namely a second worker, and the scheduling policy of each worker is set to be executed once every 5 seconds.
For example, a time window of a minute level is set, worker1 (0, -5), namely, the starting offset value of the time window of the worker1 is 5 minutes, and the ending offset value of the time window of the worker1 is 0 minute; worker2 (-5, -30), i.e., worker 2's time window starting offset value of 30 minutes and worker 2's time window ending offset value of 5 minutes; it can be represented by the time axis, and the current time is T1 — case 10:00: the query time window of the worker1 is 9: 55-10: 00, and the query time window of the worker2 is 9: 25-9: 55; when the time reaches T2-10: 35, the time window of worker1 is 10: 35-10: 30, the time window of worker2 is 10: 00-10: 30, and the execution situation enters the virtual frame part, which is shown in fig. 5.
And starting worker1 and worker2, inquiring the tasks to be executed in the respective time windows by each worker, and updating the task state to be completed after the execution is completed.
Through the deployment and the setting, the single-point problem can be solved, and the effect of high availability of the scheduling task is achieved. Such as when the machine on which worker1 is located is down, the timed tasks in the task table will not be queried and executed within 5 minutes from the current time. After 5 minutes, the timed task in the task table may be queried and executed by worker 2. Therefore, the execution of the timing task can be automatically recovered under the condition of no manual intervention. Based on the analysis, the time window of worker1 can be shortened, so that worker2 can inquire and execute as soon as possible, and the waiting time of the task to be executed can be shortened as much as possible.
On the basis of fully analyzing the problems that a single point exists in the conventional timed task system, the system is not robust enough and has no disaster, and if the machine where the timed task is located is down and the timing function cannot be recovered quickly, the invention provides the highly available timed task method, the highly available timed task device and the highly available timed task system based on the time window through deep analysis of the requirements, application scenes and the conventional problems of the timed task system, so that the single point of the system is eliminated, the timed task can be recovered without manual intervention under the condition that the machine is down, the manpower is saved, the robustness of the system is improved, and a good method is provided for the highly available timed task system of the system for realizing automatic and quick recovery of faults.
It should be understood that the specific order or hierarchy of steps in the processes disclosed is an example of exemplary approaches.
Based upon design preferences, it is understood that the specific order or hierarchy of steps in the processes may be rearranged without departing from the scope of the present disclosure. The accompanying method claims present elements of the various steps in a sample order, and are not intended to be limited to the specific order or hierarchy presented.
In the foregoing detailed description, various features are grouped together in a single embodiment for the purpose of streamlining the disclosure. This method of disclosure is not to be interpreted as reflecting an intention that the claimed embodiments of the subject matter require more features than are expressly recited in each claim. Rather, as the following claims reflect, invention lies in less than all features of a single disclosed embodiment. Thus, the following claims are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate preferred embodiment of the invention.
The previous description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the present invention. To those skilled in the art; various modifications to these embodiments will be readily apparent, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the disclosure. Thus, the present disclosure is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
What has been described above includes examples of one or more embodiments. It is, of course, not possible to describe every conceivable combination of components or methodologies for purposes of describing the aforementioned embodiments, but one of ordinary skill in the art may recognize that many further combinations and permutations of various embodiments are possible. Accordingly, the embodiments described herein are intended to embrace all such alterations, modifications and variations that fall within the scope of the appended claims. Furthermore, to the extent that the term "includes" is used in either the detailed description or the claims, such term is intended to be inclusive in a manner similar to the term "comprising" as "comprising" is interpreted when employed as a transitional word in a claim. Furthermore, any use of the term "or" in the specification of the claims is intended to mean a "non-exclusive or".
Those of skill in the art will further appreciate that the various illustrative logical blocks, units, and steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both. To clearly illustrate the interchangeability of hardware and software, various illustrative components, elements, and steps have been described above generally in terms of their functionality. Whether such functionality is implemented as hardware or software depends upon the particular application and design requirements of the overall system. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present embodiments.
The various illustrative logical blocks, or elements, described in connection with the embodiments disclosed herein may be implemented or performed with a general purpose processor, a digital signal processor, an Application Specific Integrated Circuit (ASIC), a field programmable gate array or other programmable logic device, discrete gate or transistor logic, discrete hardware components, or any combination thereof designed to perform the functions described herein. A general-purpose processor may be a microprocessor, but in the alternative, the processor may be any conventional processor, controller, microcontroller, or state machine. A processor may also be implemented as a combination of computing devices, e.g., a digital signal processor and a microprocessor, a plurality of microprocessors, one or more microprocessors in conjunction with a digital signal processor core, or any other similar configuration.
The steps of a method or algorithm described in connection with the embodiments disclosed herein may be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. A software module may be stored in RAM memory, flash memory, ROM memory, EPROM memory, EEPROM memory, registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art. For example, a storage medium may be coupled to the processor such the processor can read information from, and write information to, the storage medium. In the alternative, the storage medium may be integral to the processor. The processor and the storage medium may reside in an ASIC, which may be located in a user terminal. In the alternative, the processor and the storage medium may reside in different components in a user terminal.
In one or more exemplary designs, the functions described above in connection with the embodiments of the invention may be implemented in hardware, software, firmware, or any combination of the three. If implemented in software, the functions may be stored on or transmitted over as one or more instructions or code on a computer-readable medium. Computer-readable media includes both computer storage media and communication media that facilitate transfer of a computer program from one place to another. Storage media may be any available media that can be accessed by a general purpose or special purpose computer. For example, such computer-readable media can include, but is not limited to, RAM, ROM, EEPROM, CD-ROM or other optical disk storage, magnetic disk storage or other magnetic storage devices, or any other medium which can be used to carry or store program code in the form of instructions or data structures and which can be read by a general-purpose or special-purpose computer, or a general-purpose or special-purpose processor. Additionally, any connection is properly termed a computer-readable medium, and, thus, is included if the software is transmitted from a website, server, or other remote source via a coaxial cable, fiber optic cable, twisted pair, Digital Subscriber Line (DSL), or wirelessly, e.g., infrared, radio, and microwave. Such discs (disk) and disks (disc) include compact disks, laser disks, optical disks, DVDs, floppy disks and blu-ray disks where disks usually reproduce data magnetically, while disks usually reproduce data optically with lasers. Combinations of the above may also be included in the computer-readable medium.
The above-mentioned embodiments are intended to illustrate the objects, technical solutions and advantages of the present invention in further detail, and it should be understood that the above-mentioned embodiments are merely exemplary embodiments of the present invention, and are not intended to limit the scope of the present invention, and any modifications, equivalent substitutions, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.

Claims (11)

1. A timed task execution method based on a time window is characterized by comprising the following steps:
when at least one worker receives the scheduling command, all workers execute in parallel; the scheduling command is periodically transmitted to the at least one worker at a specified scheduling interval;
each worker in the at least one worker sets a corresponding time window according to the current time, wherein the time windows are in the sequence of the starting time on the time axis, and no time overlap exists between two non-adjacent time windows;
each worker inquires the task table and acquires a timing task from the task table as a corresponding preparation task; the execution time of the corresponding preparation task of any worker in the task table is within the time window range corresponding to the worker, and the task state of each preparation task in the task table is to be executed;
and each worker executes the corresponding preparatory task and modifies the task state of the corresponding preparatory task in the task table to be complete.
2. The method for timed task execution based on a time window of claim 1 wherein each worker of the at least one worker sets a respective time window according to the current time, comprising:
each worker acquires the current time;
each worker acquires a pre-configured corresponding time window starting deviant and a time window ending deviant respectively;
each worker backs a respective corresponding time window starting deviation value from the current time to the past time direction to obtain the starting time of the respective corresponding time window; returning the corresponding time window termination deviation values from the current time to the past time direction to obtain the termination time of the corresponding time windows;
and taking the time range from the starting time to the ending time of each worker as the corresponding time window of each worker.
3. The method of claim 2, wherein the range of values for each worker that is comprised of the time window start offset value and the time window end offset value is continuous and non-overlapping.
4. A method for timed task execution based on a time window according to claim 1 or 2 or 3, characterized in that the timed task is provided with a task lock;
each worker executes the corresponding preparation task and modifies the task state of the corresponding preparation task in the task table to be completed, and the method comprises the following steps:
each worker judges whether a task lock exists in the corresponding preparation task;
if yes, the current worker gives up processing the corresponding preparation task and waits for receiving the next scheduling command;
if not, the current worker adds a task lock for the corresponding preparation task, executes the corresponding preparation task, modifies the task state of the corresponding preparation task in the task table into the execution completion, and finally deletes the task lock of the corresponding preparation task.
5. A timed task execution device based on a time window, arranged on a worker, comprising:
the scheduling command receiving device is used for triggering the time window setting device to execute when receiving the scheduling command; the scheduling command is periodically sent to at least one worker at a specified scheduling interval to enable the workers to execute in parallel;
the time window setting device is used for setting corresponding time windows of the workers, wherein the time windows set for the workers in the at least one worker are not overlapped with each other in time according to the sequence of the starting time on a time axis;
the task acquisition device is used for inquiring the task table and acquiring the timing task from the task table as a corresponding preparation task of the worker; the execution time of the corresponding preparation tasks of the worker in the task table is within the time window range corresponding to the worker, and the task state of each preparation task in the task table is to be executed;
and the task execution device is used for executing the corresponding preparation task of the worker and modifying the task state of the corresponding preparation task in the task table into the execution completion.
6. The timed task execution device based on time window according to claim 5, characterized in that said time window setting means is specifically configured to:
acquiring current time;
acquiring a time window starting deviant and a time window ending deviant which are configured in advance and correspond to the worker;
returning a corresponding time window starting deviant of the worker from the current time to the past time direction to obtain the starting time of the corresponding time window of the worker; returning a time window termination deviant corresponding to the worker from the current time to the past time direction to obtain the termination time of the time window corresponding to the worker;
and taking the time range from the corresponding starting time to the corresponding ending time of the worker as the corresponding time window of the worker.
7. The timed task execution apparatus according to claim 6, wherein the ranges of values for each worker that consist of the time window start offset value and the time window end offset value are continuous and non-overlapping.
8. The timed task execution device based on time windows according to claim 5, 6 or 7, characterized in that the timed task is provided with a task lock;
the task execution device is specifically configured to:
judging whether a task lock already exists in a corresponding preparation task of the worker;
if yes, the worker gives up processing the corresponding preparation task and waits for receiving the next scheduling command;
and if the task lock does not exist, the worker adds the task lock for the corresponding prepared task, executes the corresponding prepared task, modifies the task state of the corresponding prepared task in the task table into the execution completion state, and finally deletes the task lock of the corresponding prepared task.
9. A timed task execution system based on time windows, comprising: at least one dispatcher, at least one worker, and a data device;
the at least one dispatcher, for periodically sending dispatch commands to the at least one worker at specified dispatch intervals;
the at least one worker, each worker setting a timed task execution device based on a time window according to any one of claims 5 to 8;
and the data device is used for storing a task table, and the related information of the timing task is recorded in the task table.
10. The timed task execution system based on time windows according to claim 9,
the data device is also used for storing a task lock table, and the task lock table records the state information of whether the task lock of each timing task exists or not.
11. The timed task execution system based on a time window of claim 9, wherein the number of said at least one dispatcher for periodically dispatching said dispatch commands to said at least one worker at specified dispatch intervals is one; alternatively, the first and second electrodes may be,
the number of the at least one dispatcher is consistent with the number of the at least one worker, a one-to-one correspondence relationship is established between the dispatchers and the workers, and each dispatcher periodically sends the dispatching command to a corresponding worker at a specified dispatching interval.
CN202011398542.0A 2020-12-02 2020-12-02 Timed task execution method, device and system based on time window Pending CN112631745A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202011398542.0A CN112631745A (en) 2020-12-02 2020-12-02 Timed task execution method, device and system based on time window

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011398542.0A CN112631745A (en) 2020-12-02 2020-12-02 Timed task execution method, device and system based on time window

Publications (1)

Publication Number Publication Date
CN112631745A true CN112631745A (en) 2021-04-09

Family

ID=75307721

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202011398542.0A Pending CN112631745A (en) 2020-12-02 2020-12-02 Timed task execution method, device and system based on time window

Country Status (1)

Country Link
CN (1) CN112631745A (en)

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090007123A1 (en) * 2007-06-28 2009-01-01 Samsung Electronics Co., Ltd. Dynamic Application Scheduler in a Polling System
US20150120959A1 (en) * 2013-10-29 2015-04-30 Solana Networks Inc. Method and system for monitoring and analysis of network traffic flows
CN109660981A (en) * 2019-02-14 2019-04-19 中国联合网络通信集团有限公司 User scheduling method and device in a kind of high-speed rail mobile communications network
CN110046039A (en) * 2019-03-28 2019-07-23 厦门网宿有限公司 Method for scheduling task and system, control centre's server and storage medium
US20200034073A1 (en) * 2016-02-22 2020-01-30 Nutanix, Inc. Accelerating background tasks in a computing cluster
CN110780985A (en) * 2019-09-25 2020-02-11 苏州浪潮智能科技有限公司 Parallel task scheduling method and device with limited time
CN110895488A (en) * 2018-09-12 2020-03-20 北京奇虎科技有限公司 Task scheduling method and device
US20200336435A1 (en) * 2017-12-31 2020-10-22 Huawei Technologies Co., Ltd. Packet Sending Method, Device, and System

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090007123A1 (en) * 2007-06-28 2009-01-01 Samsung Electronics Co., Ltd. Dynamic Application Scheduler in a Polling System
US20150120959A1 (en) * 2013-10-29 2015-04-30 Solana Networks Inc. Method and system for monitoring and analysis of network traffic flows
US20200034073A1 (en) * 2016-02-22 2020-01-30 Nutanix, Inc. Accelerating background tasks in a computing cluster
US20200336435A1 (en) * 2017-12-31 2020-10-22 Huawei Technologies Co., Ltd. Packet Sending Method, Device, and System
CN110895488A (en) * 2018-09-12 2020-03-20 北京奇虎科技有限公司 Task scheduling method and device
CN109660981A (en) * 2019-02-14 2019-04-19 中国联合网络通信集团有限公司 User scheduling method and device in a kind of high-speed rail mobile communications network
CN110046039A (en) * 2019-03-28 2019-07-23 厦门网宿有限公司 Method for scheduling task and system, control centre's server and storage medium
CN110780985A (en) * 2019-09-25 2020-02-11 苏州浪潮智能科技有限公司 Parallel task scheduling method and device with limited time

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
RISAT PATHAN: "scheduling parallel real-time recurrent tasks on multicore platforms", IEEE *
张小林: "微小卫星星务计算机任务调度算法", 计算机工程 *

Similar Documents

Publication Publication Date Title
US10101991B2 (en) Managing a software-patch submission queue
CN111125444A (en) Big data task scheduling management method, device, equipment and storage medium
CN103647669A (en) System and method for guaranteeing distributed data processing consistency
CN105279261B (en) Dynamic scalable database filing method and system
CN105069134A (en) Method for automatically collecting Oracle statistical information
CN105487924A (en) Batch processing controlling method and device
CN105677465B (en) The data processing method and device of batch processing are run applied to bank
CN107566460B (en) Method and system for distributed deployment of planning tasks
CN109634970A (en) Table method of data synchronization, equipment, storage medium and device
CN111580939B (en) Method and device for processing transactions in hierarchical and asynchronous mode
CN106777126A (en) A kind of online data moving method for supporting isomery time series database
CN101106734B (en) Task dispatching system and method for intelligent network system
CN107704550A (en) File migration method, apparatus and computer-readable recording medium
CN112286661A (en) Task scheduling method and device, storage medium and terminal
CN113157426B (en) Task scheduling method, system, equipment and storage medium
CN112631745A (en) Timed task execution method, device and system based on time window
CN109697112A (en) Distributed intensive one-stop operating system and implementation method
CN111538525B (en) Rapid iteration and rollback method for program release
CN112905613A (en) Data bidirectional synchronization method and device for heterogeneous database
US10261718B1 (en) Adaptively controlling data copy operations that copy data in an electronic environment
CN105450737B (en) A kind of data processing method, device and system
CN111782618A (en) Data synchronization system
US10409651B2 (en) Incremental workflow execution
CN111078418A (en) Operation synchronization method and device, electronic equipment and computer readable storage medium
CN107621975B (en) TIMER logic implementation method based on JAVA TIMER high availability

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination