Summary of the invention
To be overcome the problems, such as present in the relevant technologies at least to a certain extent, the application provides a kind of touching of timer-triggered scheduler
Forwarding method and device.
According to the embodiment of the present application in a first aspect, providing a kind of triggering method of timer-triggered scheduler, comprising:
It determines timing cycles and controls the duration in period, wherein the timing cycles and the control period are from beginning to end
Connected cycle period, the timing cycles are equally divided into multiple sub-periods;
Within a control period, the execution team of several sub-periods after the start time in control period is successively determined
Column;
In a sub-period, the execution queue of the sub-period is taken out, and triggers all timing tune in the execution queue
Degree.
Further, the duration of the timing cycles and the duration in the control period are preset fixed value;
Duration of the duration of the timing cycles not less than the control period;
The control period when an a length of sub-period duration integral multiple.
Further, the method also includes:
Determine start time point, the common starting point as timing cycles and control period.
Further, the execution queue for successively determining several sub-periods after the start time in control period, packet
It includes:
When controlling the period and starting, first sub-period domestic demand timer-triggered scheduler to be generated that current time starts is determined,
It puts it into the execution queue of the sub-period;
Successively determine second sub-period after first sub-period, third sub-period until n-th sub-period
Execute queue;
The stopping when n is equal to the period of the day from 11 p.m. to 1 a.m segment number that the timing cycles are included.
Further, the timing cycles are realized by time wheel, and the duration of the timing cycles is time wheel length,
The sub-period of the timing cycles is the grid of time wheel;
The then duration of the determining timing cycles and control period, comprising:
The initial time that time wheel is arranged is T0, and the time takes turns a length of T Δ, and indicates the finger of time wheel for seasonable with Tp
Between.
Further, the execution queue for successively determining several sub-periods after the start time in control period, packet
It includes:
At the T0 moment, start the control thread that a duration is Td, and every the Td time, all starting is primary continues
Time is the control thread of Td;Wherein Td is the duration for controlling the period;
The control thread calculates the timer-triggered scheduler that each trigger needs to generate in T delta time;
The control thread successively calculates each time triggered time of each trigger in T delta time, and each timing is adjusted
Degree is put into corresponding time wheel grid.
Further, the method also includes:
Every the Td time, starts the control thread that a duration is Td, form new trigger timer-triggered scheduler.
Further, the execution queue for taking out the sub-period, and all timer-triggered schedulers in the execution queue are triggered,
Include:
When grid one lattice of advance that the pointer Tp of time wheel takes turns along the time, all execution queues in current grid are checked
Wheel number;
It takes out wheel number and is equal to time wheel when the execution queue of front-wheel number, and take out these all timings executed in queue and adjust
Degree is triggered.
Further, the method also includes:
If it find that wheel number is less than the execution queue that time wheel works as front-wheel number, then these are executed into queue according to MisFire
Strategy is handled.
Further, grid one lattice of advance that the pointer Tp of the time wheel takes turns along the time, comprising:
When initialization, Tp=Tc is enabled, wherein Tc is current time;
By a timing thread, every pointer one lattice of advance of a regular time Ts traveling time wheel, and Tp is enabled
=Tp+Ts, wherein Tc is the duration of a grid.
Further, the method also includes:
The time wheel is int ((Tc-T0)/T Δ) when front-wheel number;
It is int ((Tp-T0) %T Δ) that the pointer of the time wheel, which corresponds to grid,.
Further, the method also includes:
Whether consistent compare Tc and Tp;
If Tp lags behind Tc, Tp is adjusted to current time Tc, and wheel number will be corresponded in all grid of leap
The timer-triggered scheduler executed in queue takes out and triggers;
If Tp is faster than Tc, suspends and update Tp, restores to update when waiting Tc consistent with Tp.
According to the second aspect of the embodiment of the present application, a kind of trigger device of timer-triggered scheduler is provided, comprising:
Period determination module, for determining timing cycles and controlling the duration in period, wherein timing cycles and described
Controlling the period is end to end cycle period, and the timing cycles are equally divided into multiple sub-periods;
Queue determining module was used within a control period, if after successively determining the start time in control period
The execution queue of dry sub-period;
Trigger module, for taking out the execution queue of the sub-period, and trigger in the execution queue in a sub-period
All timer-triggered schedulers.
According to the third aspect of the embodiment of the present application, provide a kind of calculating equipment, the calculating equipment include: processor and
Memory;
The memory is for storing computer program instructions;
When the calculating equipment is run, the processor executes the computer program instructions in the memory, to execute
The operating procedure of any method as described above.
Embodiments herein provide technical solution have it is following the utility model has the advantages that
The scheme of the application by way of it will need the time point run to generate related operation data in advance recently, in machine
In the case that device resource requirement is extremely low, a large amount of timing operation functions can be supported, while guaranteeing to trigger low delay;It solves more
Handover overhead existing for thread, a large amount of timing be unbalanced to cause thread utilization rate not high and timing form supports single etc. ask
Topic.
It should be understood that above general description and following detailed description be only it is exemplary and explanatory, not
The application can be limited.
Specific embodiment
Example embodiments are described in detail here, and the example is illustrated in the accompanying drawings.Following description is related to
When attached drawing, unless otherwise indicated, the same numbers in different drawings indicate the same or similar elements.Following exemplary embodiment
Described in embodiment do not represent all embodiments consistent with the application.On the contrary, they be only with it is such as appended
The example of the consistent method and apparatus of some aspects be described in detail in claims, the application.
Fig. 1 is a kind of flow chart of the triggering method of timer-triggered scheduler shown according to an exemplary embodiment.This method packet
Include following steps:
Step S1: determining timing cycles and controls the duration in period, wherein the timing cycles and the control period are equal
For end to end cycle period, the timing cycles are equally divided into multiple sub-periods;
Step S2: within a control period, several sub-periods after the start time in control period are successively determined
Execute queue;
Step S3: in a sub-period, taking out the execution queue of the sub-period, and triggers all in the execution queue
Timer-triggered scheduler.
This method is a kind of under the requirement of timer-triggered scheduler enormous amount, guarantees the method for the scheduling triggering of efficient stable.
The scheme of the application by way of it will need the time point run to generate related operation data in advance recently, in machine
In the case that device resource requirement is extremely low, a large amount of timing operation functions can be supported, while guaranteeing to trigger low delay;It solves more
Handover overhead existing for thread, a large amount of timing be unbalanced to cause thread utilization rate not high and timing form supports single etc. ask
Topic.
In some embodiments, the duration in the duration of the timing cycles and the control period is preset fixed value;
Duration of the duration of the timing cycles not less than the control period;
The control period when an a length of sub-period duration integral multiple.
In some embodiments, the method also includes:
Determine start time point, the common starting point as timing cycles and control period.
In some embodiments, the execution team for successively determining several sub-periods after the start time in control period
Column, comprising:
When controlling the period and starting, first sub-period domestic demand timer-triggered scheduler to be generated that current time starts is determined,
It puts it into the execution queue of the sub-period;
Successively determine second sub-period after first sub-period, third sub-period until n-th sub-period
Execute queue;
The stopping when n is equal to the period of the day from 11 p.m. to 1 a.m segment number that the timing cycles are included.
Below with reference to concrete implementation mode, expansion explanation is carried out to the scheme of the application.
As a kind of embodiment of the application, it can be taken turns and be calculated by the time based on Redis and support Cron timing form
Method realizes the program.From basal latency wheel algorithm, the dependency structure that algorithm is related to is deposited by cache Redis
Storage and calculating meet high availability again, while meet algorithm relevant operation by Redis characteristic while realizing supercomputing
Idempotent characteristic uses to extend.
The time point for needing to run recently by calculating Cron expression formula generates the mode of related operation data, In in advance
In the case that machine resources requirement is extremely low, a large amount of timing operation functions can be supported, while guaranteeing to trigger low delay.
By being then based on Redis design, avoids and only may be because that the timer-triggered scheduler that delay machine causes is lost in memory operation
Problem, high availability have obtained very big guarantee.
In some embodiments, the timing cycles are realized by time wheel, and the duration of the timing cycles is the time
Wheel length, the sub-period of the timing cycles are the grid of time wheel;
The then duration of the determining timing cycles and control period, comprising:
The initial time that time wheel is arranged is T0, and the time takes turns a length of T Δ, and indicates the finger of time wheel for seasonable with Tp
Between.
Referring to Fig. 2, the condition that the time takes turns algorithm is as follows:
1. giving an initial time T0, the designated time takes turns a length of T Δ, and current time indicates that the next time uses Tn with Tc
It indicates, time wheel pointer corresponds to the time to be indicated with Tp;
2. taking turns number calculates formula: int ((Tc-T0)/T Δ);
3. time wheel pointer corresponds to grid and calculates formula: int ((Tp-T0) %T Δ);
4. program initialization is seasonal: Tp=Tc.
Program execution flow is as follows:
1. be less than the timed thread of T Δ using a cycle, after blocking current time Tc out in slave flipflop SortedSet
The trigger of T Δ second generates timer-triggered scheduler insertion time wheel lattice according to the next triggered time TrigTn and frequency of the trigger
In the corresponding wheel number queue of son and the next triggered time is updated, is calculated repeatedly until TrigTn > Tc+T Δ;
2. constantly advancing a lattice using thread by sleep (1s) traveling time wheel pointer, and Tp=Tp+1 is enabled, so
The corresponding whole timer-triggered schedulers of the wheel number queue in corresponding grid are taken out afterwards to issue.
The control method of program is as follows:
1. thread-safe controls, by comparing initial value when calculating next triggered time update in a program into Redis
Atomic update is carried out, wheel number counter structure can prevent from being repeatedly inserted into Set;
2. whether Tc and Tp is compared in time deviation amendment consistent, if backwardness settles to current time, and from present to
Just now the example taking-up in the corresponding wheel number queue of all grid crossed over issues;If fast do not change the lower 1s such as Tp;
3. delay machine restore after need current pointer forward T Δ lattice it is corresponding wheel number queue < when front-wheel number whole processing
(can continue by corresponding MisFire strategy is discardable).
It is as follows that trigger starts over control rule of the time in time wheel:
1. when newly-increased, the time started≤current end time time and > current time, will sentence when being inserted into Redis
It is disconnected;
2. when modification, the time started≤current end time time and > current time, will sentence when being inserted into Redis
It is disconnected, if there is without operation when trigger in handling time wheel in Redis structure, if there is no needing to relay
Enter, the operation that data are related in this modification Redis needs to guarantee atomicity with Lua script;
3. restore, first judge at the beginning of trigger and the end time whether meet section it is regular (time started≤
Time and end time > current time at present), meet section rule and trigger is restored in time wheel trigger structure, no
Satisfaction is not restored;
4. when processing, if current triggered time≤current time next time, needs to handle MisFire strategy, if counted again
The next triggered time of calculation is greater than the end time, directly trigger is taken turns from the time place deletion for placing trigger.
In some embodiments, the execution team for successively determining several sub-periods after the start time in control period
Column, comprising:
At the T0 moment, start the control thread that a duration is Td, and every the Td time, all starting is primary continues
Time is the control thread of Td;Wherein Td is the duration for controlling the period;
The control thread calculates the timer-triggered scheduler that each trigger needs to generate in T delta time;
The control thread successively calculates each time triggered time of each trigger in T delta time, and each timing is adjusted
Degree is put into corresponding time wheel grid.
In some embodiments, the method also includes:
Every the Td time, starts the control thread that a duration is Td, form new trigger timer-triggered scheduler.
In some embodiments, the execution queue for taking out the sub-period, and trigger all timings in the execution queue
Scheduling, comprising:
When grid one lattice of advance that the pointer Tp of time wheel takes turns along the time, all execution queues in current grid are checked
Wheel number;
It takes out wheel number and is equal to time wheel when the execution queue of front-wheel number, and take out these all timings executed in queue and adjust
Degree is triggered.
In some embodiments, the method also includes:
If it find that wheel number is less than the execution queue that time wheel works as front-wheel number, then these are executed into queue according to MisFire
Strategy is handled.
In some embodiments, grid one lattice of advance that the pointer Tp of time wheel take turns along the time, comprising:
When initialization, Tp=Tc is enabled, wherein Tc is current time;
By a timing thread, every pointer one lattice of advance of a regular time Ts traveling time wheel, and Tp is enabled
=Tp+Ts, wherein Tc is the duration of a grid.
In some embodiments, the method also includes:
The time wheel is int ((Tc-T0)/T Δ) when front-wheel number;
It is int ((Tp-T0) %T Δ) that the pointer of the time wheel, which corresponds to grid,.
In some embodiments, the method also includes:
Whether consistent compare Tc and Tp;
If Tp lags behind Tc, Tp is adjusted to current time Tc, and wheel number will be corresponded in all grid of leap
The timer-triggered scheduler executed in queue takes out and triggers;
If Tp is faster than Tc, suspends and update Tp, restores to update when waiting Tc consistent with Tp.
For the technical solution that the application is described in further detail, it is illustrated in conjunction with specific application scenarios.
Referring to Fig. 3, the schematic diagram of a specific time wheel is given in figure.The time, wheel length was 8 seconds in figure, Mei Gege
Son 1 second.Operational process is as follows:
1. Tp is directed toward 0 lattice using T0 as starting point, trigger SortedSet starts the control line of a timing 5s with the T0 moment
Journey (Td=5s), start calculate trig1, the trigger timer-triggered scheduler that two triggers of trig2 should generate in T delta time,
The position of the next triggered time promotion trigger of calculating is more than Tc+T Δ until the next triggered time of trigger repeatedly, while right
Just now each time point trigger timer-triggered scheduler generated is put into the specified grid of time wheel.
2. every 1s, Tp gos ahead a lattice along time wheel grid, if it find that there is wheel number to be equal to the queue when front-wheel number,
The whole timer-triggered schedulers then taken out in queue are triggered, and are carried out when front-wheel number by MisFire strategy if wheel number is less than
Reason.
Referring to Fig. 4, after 5s, last control thread terminates, and program continues to run:
3. continue the operation of step 1 after the control thread of 5s reaches runing time point, start new control thread,
And form new trigger timer-triggered scheduler.
4.Tp pointer continues to go ahead a lattice every 1s, and the operation carried out is identical as step 2.
Referring to Fig. 5, in one cycle, the trigger timing on whole triggers of above-mentioned generation and the time passed by wheel
Scheduling opens the reciprocation cycle operation in new period again after from the 15s of starting.
In actual use, the scheme of the application and the performance comparison of other schemes are as follows:
Fig. 6 is a kind of circuit block diagram of the trigger device of timer-triggered scheduler shown according to an exemplary embodiment.Referring to figure
6, which includes:
Period determination module 601, for determine timing cycles and control the period duration, wherein the timing cycles and
The control period is end to end cycle period, and the timing cycles are equally divided into multiple sub-periods;
Queue determining module 602, within a control period, after successively determining the start time in control period
The execution queue of several sub-periods;
Trigger module 603, for taking out the execution queue of the sub-period, and trigger the execution team in a sub-period
All timer-triggered schedulers in column.
About the device in above-described embodiment, wherein modules execute the specific steps of operation in related this method
Embodiment in be described in detail, no longer elaborate explanation herein.
The application also provides following embodiment:
A kind of calculating equipment, the calculating equipment includes: processor and memory;
The memory is for storing computer program instructions;
When the calculating equipment is run, the processor executes the computer program instructions in the memory, to execute
The operating procedure of any method as described above.
It is understood that same or similar part can mutually refer in the various embodiments described above, in some embodiments
Unspecified content may refer to the same or similar content in other embodiments.
It should be noted that term " first ", " second " etc. are used for description purposes only in the description of the present application, without
It can be interpreted as indication or suggestion relative importance.In addition, in the description of the present application, unless otherwise indicated, the meaning of " multiple "
Refer at least two.
Any process described otherwise above or method description are construed as in flow chart or herein, and expression includes
It is one or more for realizing specific logical function or process the step of executable instruction code module, segment or portion
Point, and the range of the preferred embodiment of the application includes other realization, wherein can not press shown or discussed suitable
Sequence, including according to related function by it is basic simultaneously in the way of or in the opposite order, Lai Zhihang function, this should be by the application
Embodiment person of ordinary skill in the field understood.
It should be appreciated that each section of the application can be realized with hardware, software, firmware or their combination.Above-mentioned
In embodiment, software that multiple steps or method can be executed in memory and by suitable instruction execution system with storage
Or firmware is realized.It, and in another embodiment, can be under well known in the art for example, if realized with hardware
Any one of column technology or their combination are realized: having a logic gates for realizing logic function to data-signal
Discrete logic, with suitable combinational logic gate circuit specific integrated circuit, programmable gate array (PGA), scene
Programmable gate array (FPGA) etc..
Those skilled in the art are understood that realize all or part of step that above-described embodiment method carries
It suddenly is that relevant hardware can be instructed to complete by program, the program can store in a kind of computer-readable storage medium
In matter, which when being executed, includes the steps that one or a combination set of embodiment of the method.
It, can also be in addition, can integrate in a processing module in each functional unit in each embodiment of the application
It is that each unit physically exists alone, can also be integrated in two or more units in a module.Above-mentioned integrated mould
Block both can take the form of hardware realization, can also be realized in the form of software function module.The integrated module is such as
Fruit is realized and when sold or used as an independent product in the form of software function module, also can store in a computer
In read/write memory medium.
Storage medium mentioned above can be read-only memory, disk or CD etc..
In the description of this specification, reference term " one embodiment ", " some embodiments ", " example ", " specifically show
The description of example " or " some examples " etc. means specific features, structure, material or spy described in conjunction with this embodiment or example
Point is contained at least one embodiment or example of the application.In the present specification, schematic expression of the above terms are not
Centainly refer to identical embodiment or example.Moreover, particular features, structures, materials, or characteristics described can be any
One or more embodiment or examples in can be combined in any suitable manner.
Although embodiments herein has been shown and described above, it is to be understood that above-described embodiment is example
Property, it should not be understood as the limitation to the application, those skilled in the art within the scope of application can be to above-mentioned
Embodiment is changed, modifies, replacement and variant.