Skip to content

Commit

Permalink
[FIX JENKINS-41864] Add warning for rare dates
Browse files Browse the repository at this point in the history
Previously, impossible dates caused 100% CPU while Jenkins was
trying to find the previous/next occurrence of the date.
  • Loading branch information
daniel-beck committed Feb 20, 2017
1 parent 0e67ad4 commit 6e5225c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
13 changes: 13 additions & 0 deletions core/src/main/java/hudson/scheduler/CronTab.java
Expand Up @@ -328,8 +328,14 @@ public Calendar ceil(long t) {
* This method modifies the given calendar and returns the same object.
*/
public Calendar ceil(Calendar cal) {
Calendar twoYearsFuture = (Calendar) cal.clone();
twoYearsFuture.add(Calendar.YEAR, 2);
OUTER:
while (true) {
if (cal.compareTo(twoYearsFuture) > 0) {
// we went at least two years into the future
throw new RareOrImpossibleDateException();
}
for (CalendarField f : CalendarField.ADJUST_ORDER) {
int cur = f.valueOf(cal);
int next = f.ceil(this,cur);
Expand Down Expand Up @@ -380,8 +386,15 @@ public Calendar floor(long t) {
* This method modifies the given calendar and returns the same object.
*/
public Calendar floor(Calendar cal) {
Calendar twoYearsAgo = (Calendar) cal.clone();
twoYearsAgo.add(Calendar.YEAR, -2);

OUTER:
while (true) {
if (cal.compareTo(twoYearsAgo) < 0) {
// we went at least two years into the past
throw new RareOrImpossibleDateException();
}
for (CalendarField f : CalendarField.ADJUST_ORDER) {
int cur = f.valueOf(cal);
int next = f.floor(this,cur);
Expand Down
@@ -0,0 +1,31 @@
/*
* The MIT License
*
* Copyright (c) 2017 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.scheduler;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

@Restricted(NoExternalUse.class)
public class RareOrImpossibleDateException extends RuntimeException {
}
19 changes: 12 additions & 7 deletions core/src/main/java/hudson/triggers/TimerTrigger.java
Expand Up @@ -32,6 +32,7 @@
import hudson.model.Item;
import hudson.scheduler.CronTabList;
import hudson.scheduler.Hash;
import hudson.scheduler.RareOrImpossibleDateException;
import hudson.util.FormValidation;
import java.text.DateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -104,13 +105,17 @@ private void updateValidationsForSanity(Collection<FormValidation> validations,
}

private void updateValidationsForNextRun(Collection<FormValidation> validations, CronTabList ctl) {
Calendar prev = ctl.previous();
Calendar next = ctl.next();
if (prev != null && next != null) {
DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
validations.add(FormValidation.ok(Messages.TimerTrigger_would_last_have_run_at_would_next_run_at(fmt.format(prev.getTime()), fmt.format(next.getTime()))));
} else {
validations.add(FormValidation.warning(Messages.TimerTrigger_no_schedules_so_will_never_run()));
try {
Calendar prev = ctl.previous();
Calendar next = ctl.next();
if (prev != null && next != null) {
DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
validations.add(FormValidation.ok(Messages.TimerTrigger_would_last_have_run_at_would_next_run_at(fmt.format(prev.getTime()), fmt.format(next.getTime()))));
} else {
validations.add(FormValidation.warning(Messages.TimerTrigger_no_schedules_so_will_never_run()));
}
} catch (RareOrImpossibleDateException ex) {
validations.add(FormValidation.warning(Messages.TimerTrigger_the_specified_cron_tab_is_rare_or_impossible()));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/resources/hudson/triggers/Messages.properties
Expand Up @@ -29,5 +29,7 @@ TimerTrigger.MissingWhitespace=You appear to be missing whitespace between * and
TimerTrigger.no_schedules_so_will_never_run=No schedules so will never run
TimerTrigger.TimerTriggerCause.ShortDescription=Started by timer
TimerTrigger.would_last_have_run_at_would_next_run_at=Would last have run at {0}; would next run at {1}.
TimerTrigger.the_specified_cron_tab_is_rare_or_impossible=This cron tab will match dates only rarely (e.g. February 29) or \
never (e.g. June 31), so this job may be triggered very rarely, if at all.
Trigger.init=Initializing timer for triggers
SCMTrigger.AdministrativeMonitorImpl.DisplayName=Too Many SCM Polling Threads

0 comments on commit 6e5225c

Please sign in to comment.