{"action":"created","issue":{"url":"https://api.github.com/repos/kubernetes/kubernetes/issues/39608","repository_url":"https://api.github.com/repos/kubernetes/kubernetes","labels_url":"https://api.github.com/repos/kubernetes/kubernetes/issues/39608/labels{/name}","comments_url":"https://api.github.com/repos/kubernetes/kubernetes/issues/39608/comments","events_url":"https://api.github.com/repos/kubernetes/kubernetes/issues/39608/events","html_url":"https://github.com/kubernetes/kubernetes/pull/39608","id":199548822,"number":39608,"title":"WIP: Do not list CronJob unmet starting times beyond deadline","user":{"login":"peay","id":7261177,"avatar_url":"https://avatars.githubusercontent.com/u/7261177?v=3","gravatar_id":"","url":"https://api.github.com/users/peay","html_url":"https://github.com/peay","followers_url":"https://api.github.com/users/peay/followers","following_url":"https://api.github.com/users/peay/following{/other_user}","gists_url":"https://api.github.com/users/peay/gists{/gist_id}","starred_url":"https://api.github.com/users/peay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/peay/subscriptions","organizations_url":"https://api.github.com/users/peay/orgs","repos_url":"https://api.github.com/users/peay/repos","events_url":"https://api.github.com/users/peay/events{/privacy}","received_events_url":"https://api.github.com/users/peay/received_events","type":"User","site_admin":false},"labels":[{"id":477397086,"url":"https://api.github.com/repos/kubernetes/kubernetes/labels/cncf-cla:%20yes","name":"cncf-cla: yes","color":"bfe5bf","default":false},{"id":349530249,"url":"https://api.github.com/repos/kubernetes/kubernetes/labels/release-note-none","name":"release-note-none","color":"c2e0c6","default":false},{"id":253450895,"url":"https://api.github.com/repos/kubernetes/kubernetes/labels/size/S","name":"size/S","color":"77bb00","default":false}],"state":"open","locked":false,"assignee":{"login":"soltysh","id":576341,"avatar_url":"https://avatars.githubusercontent.com/u/576341?v=3","gravatar_id":"","url":"https://api.github.com/users/soltysh","html_url":"https://github.com/soltysh","followers_url":"https://api.github.com/users/soltysh/followers","following_url":"https://api.github.com/users/soltysh/following{/other_user}","gists_url":"https://api.github.com/users/soltysh/gists{/gist_id}","starred_url":"https://api.github.com/users/soltysh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soltysh/subscriptions","organizations_url":"https://api.github.com/users/soltysh/orgs","repos_url":"https://api.github.com/users/soltysh/repos","events_url":"https://api.github.com/users/soltysh/events{/privacy}","received_events_url":"https://api.github.com/users/soltysh/received_events","type":"User","site_admin":false},"assignees":[{"login":"soltysh","id":576341,"avatar_url":"https://avatars.githubusercontent.com/u/576341?v=3","gravatar_id":"","url":"https://api.github.com/users/soltysh","html_url":"https://github.com/soltysh","followers_url":"https://api.github.com/users/soltysh/followers","following_url":"https://api.github.com/users/soltysh/following{/other_user}","gists_url":"https://api.github.com/users/soltysh/gists{/gist_id}","starred_url":"https://api.github.com/users/soltysh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soltysh/subscriptions","organizations_url":"https://api.github.com/users/soltysh/orgs","repos_url":"https://api.github.com/users/soltysh/repos","events_url":"https://api.github.com/users/soltysh/events{/privacy}","received_events_url":"https://api.github.com/users/soltysh/received_events","type":"User","site_admin":false}],"milestone":null,"comments":2,"created_at":"2017-01-09T13:11:39Z","updated_at":"2017-01-12T11:39:43Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/kubernetes/kubernetes/pulls/39608","html_url":"https://github.com/kubernetes/kubernetes/pull/39608","diff_url":"https://github.com/kubernetes/kubernetes/pull/39608.diff","patch_url":"https://github.com/kubernetes/kubernetes/pull/39608.patch"},"body":"**What this PR does / why we need it**:\r\n\r\nSee #36311. `getRecentUnmetScheduleTimes` gives up after 100 unmet times to avoid wasting too much CPU or memory generating all the times, as it generates them sequentially.\r\n\r\nWhen concurrency is forbidden, this is conceptually un-necessary: we only need the last unmet start time. This suggests that when concurrency is forbidden, we could generate times by going backward in time from now. This is not very practical as CronJob currently relies on a package that only provides `Next` and no `Prev`. Hand-cooking a `Prev` does not seem like a good idea. I could submit a PR to the cron library to add a `Prev` method, and use that when concurrency is forbidden through something like `getLastUnmetScheduleTime`. This would be `O(1)` and there would be no limit involved.\r\n\r\n(edit: actually, even for the other concurrency settings, we only start the last unmet start times -- there is a `TODO` in the controller to actually start all of them, but that is not implemented at the moment. This means the solution would apply, at least temporarily, to all concurrency settings).\r\n\r\ncc @soltysh what do you think?\r\n\r\nIn the meantime, I would suggest to do something simple. Currently, the user has no way to configure anything to ensure that his CronJob will not get stuck if one job takes more that 100 unmet times.\r\n\r\n `getRecentUnmetScheduleTimes` starts with an initial time corresponding to the last start (or to the creation of the CronJob, if nothing has started yet). However, when `StartingDeadlineSeconds` is set, the controller will not start anything that is older than the deadline, so if the last start is way beyond the deadline, we are generating potentially lots of unmet start times that will not be considered by the scheduler for scheduling anyway.\r\n\r\nConsider a job running every minute, where the last instance has taken 120 minutes. This means there are more than 100 unmet times when we start counting from the last start time.\r\n\r\n**The PR makes `getRecentUnmetScheduleTimes` only consider times that do not fall beyond the deadline.** Here, the CronJob can be configured with a `StartingDeadlineSeconds` of, say, 10 minutes. After the 120min job has run, `getRecentUnmetScheduleTimes` will only consider the times in the last 10 minutes from now, and will not get stuck.\r\n\r\nAs a side note on the max. number of unmet times to use as limits in terms of CPU used by the controller: I have run a quick benchmark on my i7 mac. Schedules corresponding to \"once a week\" tend to be more expensive to generate unmet times for. Just FYI.\r\n\r\n```\r\n+--------------+---------------+--------------+\r\n|   SCHEDULE   | MISSED STARTS |    TIMING    |\r\n+--------------+---------------+--------------+\r\n| */1 * * * ?  |           100 | 383.645µs    |\r\n| */30 * * * ? |           100 | 354.765µs    |\r\n| 30 1 * * ?   |           100 | 1.065124ms   |\r\n| 30 1 * * 0   |           100 | 1.80034ms    |\r\n| */1 * * * ?  |           500 | 1.341365ms   |\r\n| */30 * * * ? |           500 | 1.814441ms   |\r\n| 30 1 * * ?   |           500 | 8.475012ms   |\r\n| 30 1 * * 0   |           500 | 10.020613ms  |\r\n| */1 * * * ?  |          1000 | 2.551697ms   |\r\n| */30 * * * ? |          1000 | 4.075813ms   |\r\n| 30 1 * * ?   |          1000 | 17.674945ms  |\r\n| 30 1 * * 0   |          1000 | 19.149324ms  |\r\n| */1 * * * ?  |         10000 | 25.725531ms  |\r\n| */30 * * * ? |         10000 | 87.520022ms  |\r\n| 30 1 * * ?   |         10000 | 174.29216ms  |\r\n| 30 1 * * 0   |         10000 | 196.565748ms |\r\n+--------------+---------------+--------------+\r\n```\r\n\r\nusing\r\n\r\n```.go\r\npackage main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n    \"os\"\r\n    \"strconv\"\r\n\r\n    \"github.com/robfig/cron\"\r\n    \"github.com/olekukonko/tablewriter\"\r\n)\r\n\r\nfunc timeSchedule(schedule string, iterations int) (time.Duration) {\r\n    sched, err := cron.ParseStandard(schedule)\r\n\r\n    if err != nil {\r\n        panic(fmt.Sprintf(\"Unparseable schedule: %s\", err))\r\n    }\r\n\r\n    start := time.Now()\r\n    t := time.Now()\r\n\r\n    for i := 1; i <= iterations; i++ {\r\n        t = sched.Next(t)\r\n    }\r\n\r\n    return time.Since(start)\r\n}\r\n\r\nfunc main() {\r\n    table := tablewriter.NewWriter(os.Stdout)\r\n    table.SetHeader([]string{\"Schedule\", \"Missed starts\", \"Timing\"})\r\n\r\n    schedules := []string{\"*/1 * * * ?\", \"*/30 * * * ?\", \"30 1 * * ?\", \"30 1 * * 0\"}\r\n    iteration_nums := []int{100, 500, 1000, 10000}\r\n\r\n    for _, iterations := range iteration_nums {\r\n        for _, schedule := range schedules {\r\n            table.Append([]string{schedule,\r\n                                  strconv.Itoa(iterations),\r\n                                  timeSchedule(schedule, iterations).String()})\r\n        }\r\n    }\r\n    table.Render()\r\n}\r\n```\r\n\r\n**Which issue this PR fixes**: fixes #36311\r\n\r\n**Special notes for your reviewer**:\r\n\r\n**Release note**:\r\n<!--  Steps to write your release note:\r\n1. Use the release-note-* labels to set the release note state (if you have access) \r\n2. Enter your extended release note in the below block; leaving it blank means using the PR title as the release note. If no release note is required, just write `NONE`. \r\n-->\r\n```release-note\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/kubernetes/kubernetes/issues/comments/272142412","html_url":"https://github.com/kubernetes/kubernetes/pull/39608#issuecomment-272142412","issue_url":"https://api.github.com/repos/kubernetes/kubernetes/issues/39608","id":272142412,"user":{"login":"soltysh","id":576341,"avatar_url":"https://avatars.githubusercontent.com/u/576341?v=3","gravatar_id":"","url":"https://api.github.com/users/soltysh","html_url":"https://github.com/soltysh","followers_url":"https://api.github.com/users/soltysh/followers","following_url":"https://api.github.com/users/soltysh/following{/other_user}","gists_url":"https://api.github.com/users/soltysh/gists{/gist_id}","starred_url":"https://api.github.com/users/soltysh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soltysh/subscriptions","organizations_url":"https://api.github.com/users/soltysh/orgs","repos_url":"https://api.github.com/users/soltysh/repos","events_url":"https://api.github.com/users/soltysh/events{/privacy}","received_events_url":"https://api.github.com/users/soltysh/received_events","type":"User","site_admin":false},"created_at":"2017-01-12T11:39:43Z","updated_at":"2017-01-12T11:39:43Z","body":"@peay sorry for the long waiting time. I like your idea, esp. that we explicitly say that anything above `startingDeadlineSeconds` is considered as [failed](https://github.com/kubernetes/kubernetes/blob/1b6d17da24943225058d311898f28e4bd0e97b1d/pkg/apis/batch/v2alpha1/types.go#L239). "}}
{"actor":{"display_login":"soltysh"}}