Skip to content
Permalink
Browse files

[FIX JENKINS-41864] Add warning for rare dates

Previously, impossible dates caused 100% CPU while Jenkins was
trying to find the previous/next occurrence of the date.

(cherry picked from commit 6e5225c)
  • Loading branch information...
daniel-beck authored and olivergondza committed Feb 20, 2017
1 parent 5f655a5 commit 933a4785afa03354055c6d12a8bb1ce3b8e43b0a
@@ -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);
@@ -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);
@@ -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 {
}
@@ -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;
@@ -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()));
}
}
}
@@ -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 933a478

Please sign in to comment.
You can’t perform that action at this time.