Skip to content

Commit

Permalink
[FIXED JENKINS-43424] Introduce entropy into the periodic folder trig…
Browse files Browse the repository at this point in the history
…ger for short intervals
  • Loading branch information
stephenc committed Apr 7, 2017
1 parent 806efc0 commit c0fcee5
Showing 1 changed file with 19 additions and 4 deletions.
Expand Up @@ -33,8 +33,11 @@
import hudson.triggers.TriggerDescriptor;
import hudson.util.ListBoxModel;
import hudson.util.TimeUnit2;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

Expand All @@ -48,6 +51,11 @@ public class PeriodicFolderTrigger extends Trigger<ComputedFolder<?>> {

private static final Logger LOGGER = Logger.getLogger(PeriodicFolderTrigger.class.getName());

/**
* Captures the time that this class was loaded.
*/
private static final long startup = System.currentTimeMillis();

/**
* The interval between successive indexings.
*/
Expand Down Expand Up @@ -85,13 +93,13 @@ private static String toCrontab(String interval) {
return "* * * * *";
}
if (millis < TimeUnit2.MINUTES.toMillis(10)) {
return "*/12 * * * *";
return "H/12 * * * *";
}
if (millis < TimeUnit2.MINUTES.toMillis(30)) {
return "*/6 * * * *";
return "H/6 * * * *";
}
if (millis < TimeUnit2.HOURS.toMillis(1)) {
return "*/2 * * * *";
return "H/2 * * * *";
}
if (millis < TimeUnit2.HOURS.toMillis(8)) {
return "H * * * *";
Expand Down Expand Up @@ -183,7 +191,14 @@ public void run() {
return;
}
}
if (now - lastTriggered < interval) {
if (lastTriggered == 0) {
// on start-up set the last triggered to sometime within the interval of start-up
// for short intervals this will have no effect
// for longer intervals this will stagger all the computations on start-up
// when creating new instances this will be ignored as the computation result will be null
lastTriggered = startup + new Random().nextInt((int)Math.min(TimeUnit.DAYS.toMillis(1), interval));
}
if (now - lastTriggered < interval && (computation != null && computation.getResult() != null)) {
LOGGER.log(Level.FINE, "Too early to reschedule {0} based on last triggering", job);
return;
}
Expand Down

0 comments on commit c0fcee5

Please sign in to comment.