-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathJCloudsPreCreationThread.java
More file actions
125 lines (108 loc) · 4.99 KB
/
JCloudsPreCreationThread.java
File metadata and controls
125 lines (108 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package jenkins.plugins.openstack.compute;
import java.lang.Math;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import hudson.Extension;
import hudson.Functions;
import hudson.model.TaskListener;
import hudson.model.AsyncPeriodicWork;
import java.util.concurrent.TimeUnit;
/**
* Periodically ensure enough slaves are created.
*
* The goal of this class is to pre-provision slaves ahead of time to avoid jobs
* having to wait until a slave gets provisioned to run.
*
* It works in conjunction with the logic in JCloudsRetentionStrategy to not
* only pre-provision slaves but also keep the slaves around to meet
* requirements.
*
* The behaviour is configured via the `instanceMin` setting which controls
* how many instances per-template will be pre-provisioned.
*
* A template's retention time of 0 (zero) will be interpreted as a sign that
* used instances shouldn't be re-used and thus new instances will be
* pre-provisioned, even if used instances are running.
*
* The pre-provisioning always respects the instance capacity (either global or
* per template).
*/
@Extension @Restricted(NoExternalUse.class)
public final class JCloudsPreCreationThread extends AsyncPeriodicWork {
private static final Logger LOGGER = Logger.getLogger(JCloudsPreCreationThread.class.getName());
private final Long recurrencePeriod;
public JCloudsPreCreationThread() {
super("OpenStack slave pre-creation");
recurrencePeriod = Functions.getIsUnitTest() ? Long.MAX_VALUE : Long.getLong("jenkins.openstack.preCreationPeriod", TimeUnit.MINUTES.toMillis(2));
LOGGER.log(Level.FINE, "OpenStack pre-creation recurrence period is {0}ms", recurrencePeriod);
}
@Override
public long getRecurrencePeriod() {
return recurrencePeriod;
}
@Override
public void execute(TaskListener listener) {
HashMap<JCloudsSlaveTemplate, JCloudsCloud> requiredCapacity = new HashMap<>();
for (JCloudsCloud cloud : JCloudsCloud.getClouds()) {
for (JCloudsSlaveTemplate template : cloud.getTemplates()) {
SlaveOptions to = template.getEffectiveSlaveOptions();
if (to.getInstancesMin() > 0) {
requiredCapacity.put(template, cloud);
}
}
}
if (requiredCapacity.isEmpty()) return; // No capacity required anywhere
for (Map.Entry<JCloudsSlaveTemplate, JCloudsCloud> entry : requiredCapacity.entrySet()) {
JCloudsCloud cloud = entry.getValue();
JCloudsSlaveTemplate template = entry.getKey();
SlaveOptions so = template.getEffectiveSlaveOptions();
Integer min = so.getInstancesMin();
Integer cap = so.getInstanceCap();
int available = template.getAvailableNodesTotal();
if (available >= min) continue; // Satisfied
if (available >= cap) continue; // Obey instanceCap even if instanceMin > instanceCap
int runningNodes = template.getRunningNodes().size();
if (runningNodes >= cap) continue; // Obey instanceCap
int permitted = cap - runningNodes;
int desired = min - available;
int toProvision = Math.min(desired, permitted);
if (toProvision > 0) {
LOGGER.log(Level.INFO, "Pre-creating " + toProvision + " instance(s) for template " + template.getName() + " in cloud " + cloud.name);
for (int i = 0; i < toProvision; i++) {
try {
cloud.provisionSlaveExplicitly(template);
} catch (Throwable ex) {
LOGGER.log(Level.SEVERE, "Failed to pre-create instance from template " + template.getName(), ex);
}
}
}
}
}
/**
* Should a slave be retained to meet the minimum instances constraint?
*
* @param computer Idle, not pending delete, not user offline but overdue w.r.t. retention time.
*/
/*package*/ static boolean isNeededReadyComputer(JCloudsComputer computer) {
if (computer == null) return false;
JCloudsSlave node = computer.getNode();
if (node == null) return false;
Integer instancesMin = node.getSlaveOptions().getInstancesMin();
if (instancesMin > 0) {
JCloudsCloud cloud = JCloudsCloud.getByName(computer.getId().getCloudName());
String templateName = computer.getId().getTemplateName();
JCloudsSlaveTemplate template = cloud.getTemplate(templateName);
if (template != null) {
int readyNodes = template.getAvailableNodesTotal();
return readyNodes <= instancesMin;
}
}
return false;
}
@Override protected Level getNormalLoggingLevel() { return Level.FINE; }
@Override protected Level getSlowLoggingLevel() { return Level.INFO; }
}