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 |
Configuration settings related to a batch of jobs that are about |
6672 |
11 Apr 22 |
nicklas |
to be submitted to the cluster at more or less the same time. |
6672 |
11 Apr 22 |
nicklas |
The information becomes readonly when linked to a {@link JobDefinition} |
6672 |
11 Apr 22 |
nicklas |
instance. A single configuration instance can (should!) be used with multiple jobs. |
6672 |
11 Apr 22 |
nicklas |
11 |
|
6672 |
11 Apr 22 |
nicklas |
@author nicklas |
6672 |
11 Apr 22 |
nicklas |
@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 |
Creates a default batch configuration with a delay interval of 30 seconds |
6672 |
11 Apr 22 |
nicklas |
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 |
If multiple jobs are submitted in a batch, instruct |
6672 |
11 Apr 22 |
nicklas |
the scheduler to defer each job for a short amount of |
6672 |
11 Apr 22 |
nicklas |
time. This can, for example, be used to prevent a situation |
6672 |
11 Apr 22 |
nicklas |
where several similar jobs are started at the same time that all try |
6672 |
11 Apr 22 |
nicklas |
to load the same resource over the network. This option is enabled |
6672 |
11 Apr 22 |
nicklas |
and set to 30 seconds by default. The value must be |
6672 |
11 Apr 22 |
nicklas |
a positive integer. Disable by setting the value to null. |
6672 |
11 Apr 22 |
nicklas |
@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 |
Set the delay of the first job that is submitted in a batch. |
6672 |
11 Apr 22 |
nicklas |
The value must be an non-negative integer. The default value |
6672 |
11 Apr 22 |
nicklas |
is 0. |
6672 |
11 Apr 22 |
nicklas |
@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 |
Get the start time fo the next job that is scheduled. |
6672 |
11 Apr 22 |
nicklas |
If auto delay is enabled the start time is increased by the |
6672 |
11 Apr 22 |
nicklas |
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 |
Helper method that get the start time for the next job and calculates |
6672 |
11 Apr 22 |
nicklas |
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 |
Check that the initial and delay interval are >=0. |
6672 |
11 Apr 22 |
nicklas |
If locking, the start time for the next job is set |
6672 |
11 Apr 22 |
nicklas |
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 |
} |