extensions/net.sf.basedb.opengrid/trunk/src/net/sf/basedb/opengrid/config/BatchConfig.java

Code
Comments
Other
Rev Date Author Line
6672 11 Apr 22 nicklas 1 package net.sf.basedb.opengrid.config;
6672 11 Apr 22 nicklas 2
6672 11 Apr 22 nicklas 3
6672 11 Apr 22 nicklas 4 import net.sf.basedb.opengrid.JobDefinition;
6672 11 Apr 22 nicklas 5
6672 11 Apr 22 nicklas 6 /**
6672 11 Apr 22 nicklas 7   Configuration settings related to a batch of jobs that are about
6672 11 Apr 22 nicklas 8   to be submitted to the cluster at more or less the same time.
6672 11 Apr 22 nicklas 9   The information becomes readonly when linked to a {@link JobDefinition}
6672 11 Apr 22 nicklas 10   instance. A single configuration instance can (should!) be used with multiple jobs.
6672 11 Apr 22 nicklas 11   
6672 11 Apr 22 nicklas 12   @author nicklas
6672 11 Apr 22 nicklas 13   @since 1.5
6672 11 Apr 22 nicklas 14 */
6672 11 Apr 22 nicklas 15 public class BatchConfig 
6672 11 Apr 22 nicklas 16   extends AbstractLockable<BatchConfig>
6672 11 Apr 22 nicklas 17 {
6672 11 Apr 22 nicklas 18   
6672 11 Apr 22 nicklas 19   private int delayInterval;
6672 11 Apr 22 nicklas 20   private int initialDelay;
6672 11 Apr 22 nicklas 21   private long nextTime;
6672 11 Apr 22 nicklas 22
6672 11 Apr 22 nicklas 23   /**
6672 11 Apr 22 nicklas 24     Creates a default batch configuration with a delay interval of 30 seconds
6672 11 Apr 22 nicklas 25     and no initial delay.
6672 11 Apr 22 nicklas 26   */
6672 11 Apr 22 nicklas 27   public BatchConfig()
6672 11 Apr 22 nicklas 28   {
6672 11 Apr 22 nicklas 29     this.delayInterval = 30;
6672 11 Apr 22 nicklas 30     this.initialDelay = 0;
6672 11 Apr 22 nicklas 31   }
6672 11 Apr 22 nicklas 32   
6672 11 Apr 22 nicklas 33   /**
6672 11 Apr 22 nicklas 34     If multiple jobs are submitted in a batch, instruct
6672 11 Apr 22 nicklas 35     the scheduler to defer each job for a short amount of
6672 11 Apr 22 nicklas 36     time. This can, for example, be used to prevent a situation
6672 11 Apr 22 nicklas 37     where several similar jobs are started at the same time that all try
6672 11 Apr 22 nicklas 38     to load the same resource over the network. This option is enabled
6672 11 Apr 22 nicklas 39     and set to 30 seconds by default. The value must be
6672 11 Apr 22 nicklas 40     a positive integer. Disable by setting the value to null.
6672 11 Apr 22 nicklas 41     @since 1.5
6672 11 Apr 22 nicklas 42   */
6672 11 Apr 22 nicklas 43   public void setDelayInterval(int seconds)
6672 11 Apr 22 nicklas 44   {
6672 11 Apr 22 nicklas 45     checkLocked("setAutoDelayJobs()");
6672 11 Apr 22 nicklas 46     this.delayInterval = seconds;
6672 11 Apr 22 nicklas 47   }
6672 11 Apr 22 nicklas 48   public int getDelayInterval()
6672 11 Apr 22 nicklas 49   {
6672 11 Apr 22 nicklas 50     return delayInterval;
6672 11 Apr 22 nicklas 51   }
6672 11 Apr 22 nicklas 52   
6672 11 Apr 22 nicklas 53   /**
6672 11 Apr 22 nicklas 54     Set the delay of the first job that is submitted in a batch.
6672 11 Apr 22 nicklas 55     The value must be an non-negative integer. The default value
6672 11 Apr 22 nicklas 56     is 0.
6672 11 Apr 22 nicklas 57     @since 1.5
6672 11 Apr 22 nicklas 58   */
6672 11 Apr 22 nicklas 59   public void setInitialDelay(int seconds)
6672 11 Apr 22 nicklas 60   {
6672 11 Apr 22 nicklas 61     checkLocked("setInitialDelay()");
6672 11 Apr 22 nicklas 62     this.initialDelay = seconds;
6672 11 Apr 22 nicklas 63   }
6672 11 Apr 22 nicklas 64   public int getInitialDelay()
6672 11 Apr 22 nicklas 65   {
6672 11 Apr 22 nicklas 66     return initialDelay;
6672 11 Apr 22 nicklas 67   }
6672 11 Apr 22 nicklas 68   
6672 11 Apr 22 nicklas 69   /**
6672 11 Apr 22 nicklas 70     Get the start time fo the next job that is scheduled. 
6672 11 Apr 22 nicklas 71     If auto delay is enabled the start time is increased by the 
6672 11 Apr 22 nicklas 72     configured amount for each call to this method.
6672 11 Apr 22 nicklas 73   */
6672 11 Apr 22 nicklas 74   public long getNextStartTime()
6672 11 Apr 22 nicklas 75   {
6672 11 Apr 22 nicklas 76     long startTime = Math.max(nextTime, System.currentTimeMillis());
6672 11 Apr 22 nicklas 77     nextTime = startTime + delayInterval*1000;
6672 11 Apr 22 nicklas 78     return startTime;
6672 11 Apr 22 nicklas 79   }
6672 11 Apr 22 nicklas 80   
6672 11 Apr 22 nicklas 81   /**
6672 11 Apr 22 nicklas 82     Helper method that get the start time for the next job and calculates
6672 11 Apr 22 nicklas 83     the delay in seconds from the current time.
6672 11 Apr 22 nicklas 84   */
6672 11 Apr 22 nicklas 85   public int getDelayForNextJob()
6672 11 Apr 22 nicklas 86   {
6672 11 Apr 22 nicklas 87     long delay = getNextStartTime() - System.currentTimeMillis();
6672 11 Apr 22 nicklas 88     return delay > 0 ? (int)(delay / 1000) : 0;
6672 11 Apr 22 nicklas 89   }
6672 11 Apr 22 nicklas 90   
6672 11 Apr 22 nicklas 91   /**
6672 11 Apr 22 nicklas 92     Check that the initial and delay interval are &gt;=0.
6672 11 Apr 22 nicklas 93     If locking, the start time for the next job is set 
6672 11 Apr 22 nicklas 94     by adding the current time and initial delay.
6672 11 Apr 22 nicklas 95   */
6672 11 Apr 22 nicklas 96   @Override
6672 11 Apr 22 nicklas 97   protected void checkValid(boolean forLock) 
6672 11 Apr 22 nicklas 98   {
6672 11 Apr 22 nicklas 99     super.checkValid(forLock);
6672 11 Apr 22 nicklas 100     if (delayInterval < 0)
6672 11 Apr 22 nicklas 101     {
6672 11 Apr 22 nicklas 102       throw new IllegalArgumentException("DelayInterval must be >= 0: " + delayInterval);
6672 11 Apr 22 nicklas 103     }
6672 11 Apr 22 nicklas 104     if (initialDelay < 0)
6672 11 Apr 22 nicklas 105     {
6672 11 Apr 22 nicklas 106       throw new IllegalArgumentException("InitialDelay must be >= 0: " + initialDelay);
6672 11 Apr 22 nicklas 107     }
6672 11 Apr 22 nicklas 108     if (forLock) 
6672 11 Apr 22 nicklas 109     {
6672 11 Apr 22 nicklas 110       nextTime = System.currentTimeMillis()+1000*initialDelay;
6672 11 Apr 22 nicklas 111     }
6672 11 Apr 22 nicklas 112   }
6672 11 Apr 22 nicklas 113
6672 11 Apr 22 nicklas 114 }