New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update schedule logic to properly calculate missed schedules #118724
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,7 +290,9 @@ func TestByJobStartTime(t *testing.T) { | |
func TestMostRecentScheduleTime(t *testing.T) { | ||
metav1TopOfTheHour := metav1.NewTime(*topOfTheHour()) | ||
metav1HalfPastTheHour := metav1.NewTime(*deltaTimeAfterTopOfTheHour(30 * time.Minute)) | ||
metav1MinuteAfterTopOfTheHour := metav1.NewTime(*deltaTimeAfterTopOfTheHour(1 * time.Minute)) | ||
oneMinute := int64(60) | ||
tenSeconds := int64(10) | ||
|
||
tests := []struct { | ||
name string | ||
|
@@ -346,6 +348,94 @@ func TestMostRecentScheduleTime(t *testing.T) { | |
expectedEarliestTime: *deltaTimeAfterTopOfTheHour(10 * time.Second), | ||
expectedNumberOfMisses: 5, | ||
}, | ||
{ | ||
name: "complex schedule", | ||
cj: &batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
CreationTimestamp: metav1TopOfTheHour, | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: "30 6-16/4 * * 1-5", | ||
}, | ||
Status: batchv1.CronJobStatus{ | ||
LastScheduleTime: &metav1HalfPastTheHour, | ||
}, | ||
}, | ||
now: *deltaTimeAfterTopOfTheHour(24*time.Hour + 31*time.Minute), | ||
expectedRecentTime: deltaTimeAfterTopOfTheHour(24*time.Hour + 30*time.Minute), | ||
expectedEarliestTime: *deltaTimeAfterTopOfTheHour(30 * time.Minute), | ||
expectedNumberOfMisses: 2, | ||
}, | ||
{ | ||
name: "another complex schedule", | ||
cj: &batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
CreationTimestamp: metav1TopOfTheHour, | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: "30 10,11,12 * * 1-5", | ||
}, | ||
Status: batchv1.CronJobStatus{ | ||
LastScheduleTime: &metav1HalfPastTheHour, | ||
}, | ||
}, | ||
now: *deltaTimeAfterTopOfTheHour(30*time.Hour + 30*time.Minute), | ||
expectedRecentTime: nil, | ||
expectedEarliestTime: *deltaTimeAfterTopOfTheHour(30 * time.Minute), | ||
expectedNumberOfMisses: 30, | ||
}, | ||
{ | ||
name: "complex schedule with longer diff between executions", | ||
cj: &batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
CreationTimestamp: metav1TopOfTheHour, | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: "30 6-16/4 * * 1-5", | ||
}, | ||
Status: batchv1.CronJobStatus{ | ||
LastScheduleTime: &metav1HalfPastTheHour, | ||
}, | ||
}, | ||
now: *deltaTimeAfterTopOfTheHour(96*time.Hour + 31*time.Minute), | ||
expectedRecentTime: deltaTimeAfterTopOfTheHour(96*time.Hour + 30*time.Minute), | ||
expectedEarliestTime: *deltaTimeAfterTopOfTheHour(30 * time.Minute), | ||
expectedNumberOfMisses: 6, | ||
}, | ||
{ | ||
name: "complex schedule with shorter diff between executions", | ||
cj: &batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
CreationTimestamp: metav1TopOfTheHour, | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: "30 6-16/4 * * 1-5", | ||
}, | ||
}, | ||
now: *deltaTimeAfterTopOfTheHour(24*time.Hour + 31*time.Minute), | ||
expectedRecentTime: deltaTimeAfterTopOfTheHour(24*time.Hour + 30*time.Minute), | ||
expectedEarliestTime: *topOfTheHour(), | ||
expectedNumberOfMisses: 7, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm aware, the error comes from calculating the diff between schedules, which will depend on when it's calculated, so I wouldn't take much care into that value, I'm even inclined to drop that number entirely, if it's confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was discussed on slack, in followup we'll get rid of exposing that information from this method entirely. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #118940 is the promised followup dropping that number |
||
}, | ||
{ | ||
name: "@every schedule", | ||
cj: &batchv1.CronJob{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
CreationTimestamp: metav1.NewTime(*deltaTimeAfterTopOfTheHour(-59 * time.Minute)), | ||
}, | ||
Spec: batchv1.CronJobSpec{ | ||
Schedule: "@every 1h", | ||
StartingDeadlineSeconds: &tenSeconds, | ||
}, | ||
Status: batchv1.CronJobStatus{ | ||
LastScheduleTime: &metav1MinuteAfterTopOfTheHour, | ||
}, | ||
}, | ||
now: *deltaTimeAfterTopOfTheHour(7 * 24 * time.Hour), | ||
expectedRecentTime: deltaTimeAfterTopOfTheHour((6 * 24 * time.Hour) + 23*time.Hour + 1*time.Minute), | ||
expectedEarliestTime: *deltaTimeAfterTopOfTheHour(1 * time.Minute), | ||
expectedNumberOfMisses: 167, | ||
}, | ||
{ | ||
name: "rogue cronjob", | ||
cj: &batchv1.CronJob{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon second thought, this will not work in few corner scenarios.If the schedule is such that if the difference between now and the actual most recent time is greater than twice the timeBetweenTwoSchedules, then mostRecentTime would be returned as empty. An example test case which would fail would be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we expect the number of misses to be five (5) here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@helayoty yes that's correct, the expectedNumberOfMisses should be 5 but since numberOfMissedSchedules is an approximate, i was concentrating more on the failure with respective expectedRecentTime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my other comment below about missed schedules.