Skip to content
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

Merged
merged 1 commit into from Jun 27, 2023

Conversation

soltysh
Copy link
Contributor

@soltysh soltysh commented Jun 17, 2023

What type of PR is this?

/kind bug

What this PR does / why we need it:

This is alternative approach to fix the problem described in #117166

Before this change we've assumed a constant time between schedule runs, which is not true for cases like "30 6-16/4 * * 1-5". The fix is to calculate the potential next run using the fixed schedule as the baseline, and then go back one schedule back and allow the cron library to calculate the correct time.

This approach saves us from iterating multiple times between last schedule time and now, if the cronjob for any reason wasn't running for significant amount of time.

Which issue(s) this PR fixes:

None

Special notes for your reviewer:

/assign @kmala @atiratree

Does this PR introduce a user-facing change?

Fix cronjob controller handling of complex schedules, like "30 6-16/4 * * 1-5", for example

@k8s-ci-robot k8s-ci-robot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Jun 17, 2023
@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 17, 2023
@k8s-ci-robot
Copy link
Contributor

@soltysh: GitHub didn't allow me to assign the following users: kmala.

Note that only kubernetes members with read permissions, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

What type of PR is this?

/kind bug

What this PR does / why we need it:

This is alternative approach to fix the problem described in #117166

Before this change we've assumed a constant time between schedule runs, which is not true for cases like "30 6-16/4 * * 1-5". The fix is to calculate the potential next run using the fixed schedule as the baseline, and then go back one schedule back and allow the cron library to calculate the correct time.

This approach saves us from iterating multiple times between last schedule time and now, if the cronjob for any reason wasn't running for significant amount of time.

Which issue(s) this PR fixes:

None

Special notes for your reviewer:

/assign @kmala @atiratree

Does this PR introduce a user-facing change?

Fix cronjob controller handling of complex schedules, like "30 6-16/4 * * 1-5", for example

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 17, 2023
@soltysh
Copy link
Contributor Author

soltysh commented Jun 17, 2023

/sig apps
/priority backlog
/triage accepted

@k8s-ci-robot k8s-ci-robot added sig/apps Categorizes an issue or PR as relevant to SIG Apps. priority/backlog Higher priority than priority/awaiting-more-evidence. triage/accepted Indicates an issue or PR is ready to be actively worked on. approved Indicates a PR has been approved by an approver from all required OWNERS files. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jun 17, 2023
@kmala
Copy link
Contributor

kmala commented Jun 19, 2023

/retest

@kmala
Copy link
Contributor

kmala commented Jun 19, 2023

/lgtm

@k8s-ci-robot
Copy link
Contributor

@kmala: changing LGTM is restricted to collaborators

In response to this:

/lgtm

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

// in time one more time unit back and let the cron library calculate
// a proper schedule, for case where the schedule is not consistent,
// for example something like 30 6-16/4 * * 1-5
potentialEarliest := t1.Add(time.Duration((numberOfMissedSchedules-1-1)*timeBetweenTwoSchedules) * time.Second)
Copy link
Contributor

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

{
			name: "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:     deltaTimeAfterTopOfTheHour(26*time.Hour + 30*time.Minute),
			expectedEarliestTime:   *deltaTimeAfterTopOfTheHour(30 * time.Minute),
			expectedNumberOfMisses: 2,
		},

Copy link
Member

@helayoty helayoty Jun 23, 2023

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?

  • 2016-05-19 T 10:30 (expectedEarliestTime)
  • 2016-05-19 T 11:30
  • 2016-05-19 T 12:30
  • 2016-05-20 T 10:30
  • 2016-05-20 T 11:30
  • 2016-05-20 T 12:30 (expectedRecentTime)

Copy link
Contributor

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

Copy link
Contributor Author

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.

now: *deltaTimeAfterTopOfTheHour(24*time.Hour + 31*time.Minute),
expectedRecentTime: deltaTimeAfterTopOfTheHour(24*time.Hour + 30*time.Minute),
expectedEarliestTime: *topOfTheHour(),
expectedNumberOfMisses: 7,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expectedNumberOfMisses should be four(4) only; (10:30, 14:30, 6:30, and 10:30)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#118940 is the promised followup dropping that number

Before this change we've assumed a constant time between schedule runs,
which is not true for cases like "30 6-16/4 * * 1-5".
The fix is to calculate the potential next run using the fixed schedule
as the baseline, and then go back one schedule back and allow the cron
library to calculate the correct time.

This approach saves us from iterating multiple times between last
schedule time and now, if the cronjob for any reason wasn't running for
significant amount of time.
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 27, 2023
@soltysh
Copy link
Contributor Author

soltysh commented Jun 27, 2023

Unrelated failure
/test pull-kubernetes-unit

@dims
Copy link
Member

dims commented Jun 27, 2023

/approve
/lgtm

consensus on slack was to merge and iterate!
https://kubernetes.slack.com/archives/C18NZM5K9/p1687862539511419?thread_ts=1683306886.062509&cid=C18NZM5K9

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 27, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 459d25375f05bbfefd96979b94752e191154e9cc

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dims, soltysh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/backlog Higher priority than priority/awaiting-more-evidence. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/apps Categorizes an issue or PR as relevant to SIG Apps. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants