-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consistent scheduling when tasks run within the poll interval of thei…
…r original time (#190093) Resolves #189114 In this PR, I'm changing the logic to calculate the task's next run at. Whenever the gap between the task's runAt and when it was picked up is less than the poll interval, we'll use the `runAt` to schedule the next. This way we don't continuously add time to the task's next run (ex: running every 1m turns into every 1m 3s). I've had to modify a few tests to have a more increased interval because this made tasks run more frequently (on time), which introduced flakiness. ## To verify 1. Create an alerting rule that runs every 10s 2. Apply the following diff to your code ``` diff --git a/x-pack/plugins/task_manager/server/lib/get_next_run_at.ts b/x-pack/plugins/task_manager/server/lib/get_next_run_at.ts index 55d5f85e5d3..4342dcdd845 100644 --- a/x-pack/plugins/task_manager/server/lib/get_next_run_at.ts +++ b/x-pack/plugins/task_manager/server/lib/get_next_run_at.ts @@ -31,5 +31,7 @@ export function getNextRunAt( Date.now() ); + console.log(`*** Next run at: ${new Date(nextCalculatedRunAt).toISOString()}, interval=${newSchedule?.interval ?? schedule.interval}, originalRunAt=${originalRunAt.toISOString()}, startedAt=${startedAt.toISOString()}`); + return new Date(nextCalculatedRunAt); } ``` 3. Observe the logs, the gap between runAt and startedAt should be less than the poll interval, so the next run at is based on `runAt` instead of `startedAt`. 4. Stop Kibana for 15 seconds then start it again 5. Observe the first logs when the rule runs again and notice now that the gap between runAt and startedAt is larger than the poll interval, the next run at is based on `startedAt` instead of `runAt` to spread the tasks out evenly. --------- Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
e404a39
commit 1f673dc
Showing
10 changed files
with
173 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { type TaskManagerConfig, configSchema } from './config'; | ||
|
||
const createConfigMock = (overwrites: Partial<TaskManagerConfig> = {}) => { | ||
const mocked: TaskManagerConfig = configSchema.validate(overwrites); | ||
return mocked; | ||
}; | ||
|
||
export const configMock = { | ||
create: createConfigMock, | ||
}; |
58 changes: 58 additions & 0 deletions
58
x-pack/plugins/task_manager/server/lib/get_next_run_at.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { taskManagerMock } from '../mocks'; | ||
|
||
import { getNextRunAt } from './get_next_run_at'; | ||
|
||
describe('getNextRunAt', () => { | ||
test('should use startedAt when the task delay is greater than the threshold', () => { | ||
const now = new Date(); | ||
// Use time in the past to ensure the task delay calculation isn't relative to "now" | ||
const fiveSecondsAgo = new Date(now.getTime() - 5000); | ||
const fourSecondsAgo = new Date(now.getTime() - 4000); | ||
const nextRunAt = getNextRunAt( | ||
taskManagerMock.createTask({ | ||
schedule: { interval: '1m' }, | ||
runAt: fiveSecondsAgo, | ||
startedAt: fourSecondsAgo, | ||
}), | ||
500 | ||
); | ||
expect(nextRunAt).toEqual(new Date(fourSecondsAgo.getTime() + 60000)); | ||
}); | ||
|
||
test('should use runAt when the task delay is greater than the threshold', () => { | ||
const now = new Date(); | ||
// Use time in the past to ensure the task delay calculation isn't relative to "now" | ||
const fiveSecondsAgo = new Date(now.getTime() - 5000); | ||
const aBitLessThanFiveSecondsAgo = new Date(now.getTime() - 4995); | ||
const nextRunAt = getNextRunAt( | ||
taskManagerMock.createTask({ | ||
schedule: { interval: '1m' }, | ||
runAt: fiveSecondsAgo, | ||
startedAt: aBitLessThanFiveSecondsAgo, | ||
}), | ||
500 | ||
); | ||
expect(nextRunAt).toEqual(new Date(fiveSecondsAgo.getTime() + 60000)); | ||
}); | ||
|
||
test('should not schedule in the past', () => { | ||
const testStart = new Date(); | ||
const fiveMinsAgo = new Date(Date.now() - 300000); | ||
const nextRunAt = getNextRunAt( | ||
taskManagerMock.createTask({ | ||
schedule: { interval: '1m' }, | ||
runAt: fiveMinsAgo, | ||
startedAt: fiveMinsAgo, | ||
}), | ||
0 | ||
); | ||
expect(nextRunAt.getTime()).toBeGreaterThanOrEqual(testStart.getTime()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { intervalFromDate } from './intervals'; | ||
import type { ConcreteTaskInstance } from '../task'; | ||
|
||
export function getNextRunAt( | ||
{ runAt, startedAt, schedule }: Pick<ConcreteTaskInstance, 'runAt' | 'startedAt' | 'schedule'>, | ||
taskDelayThresholdForPreciseScheduling: number = 0 | ||
): Date { | ||
const taskDelay = startedAt!.getTime() - runAt.getTime(); | ||
const scheduleFromDate = taskDelay < taskDelayThresholdForPreciseScheduling ? runAt : startedAt!; | ||
|
||
// Ensure we also don't schedule in the past by performing the Math.max with Date.now() | ||
const nextCalculatedRunAt = Math.max( | ||
intervalFromDate(scheduleFromDate, schedule!.interval)!.getTime(), | ||
Date.now() | ||
); | ||
|
||
return new Date(nextCalculatedRunAt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.