-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathJCloudsPreCreationThread.java
More file actions
235 lines (206 loc) · 8.58 KB
/
JCloudsPreCreationThread.java
File metadata and controls
235 lines (206 loc) · 8.58 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package jenkins.plugins.openstack.compute;
import java.lang.Math;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import hudson.model.Executor;
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 org.openstack4j.model.compute.Server;
/**
* 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());
public JCloudsPreCreationThread() {
super("OpenStack slave pre-creation");
}
@Override
public long getRecurrencePeriod() {
return Functions.getIsUnitTest() ? Long.MAX_VALUE : MIN * 2;
}
@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);
}
}
}
}
}
/**
* Methods which return the list of VM
*/
public static List<? extends Server> getVmList(String templateName, String cloudName){
List<? extends Server> vmList = null;
for (JCloudsCloud cloud : JCloudsCloud.getClouds()) {
if (cloud.getDisplayName().equals(cloudName)){
vmList = cloud.getTemplate(templateName).getRunningNodes();
}
}
return vmList;
}
/**
* Methods which return the name of the oldestVM for a specific template.
*/
public static String getOldestVm(String templateName, String cloudName){
return getOldestVM(templateName, cloudName).getName();
}
/**
* Methods which return the oldestVM
* return a Server
* return a VM which is not offline/Pending delete in Jenkins
*/
private static Server getOldestVM(String templateName, String cloudName){
Server oldestVm = null;
long oldestVmTime;
long currentVMTime;
String computerName;
String vmName;
List<? extends Server> vmList = getVmList(templateName, cloudName);
List<JCloudsComputer> listComputer = JCloudsComputer.getAll();
if (oldestVm == null){
oldestVm = vmList.get(0);
}
for (int i = 0; i<vmList.size(); i++){
oldestVmTime = oldestVm.getCreated().getTime();
currentVMTime = vmList.get(i).getCreated().getTime();
if (oldestVmTime > currentVMTime){
for (int y = 0; y<listComputer.size(); y++){
computerName = listComputer.get(y).getName();
vmName = vmList.get(i).getName();
if (computerName.equals(vmName)){
if (!listComputer.get(y).isOffline()){
oldestVm = vmList.get(i);
}
}
}
}
}
return oldestVm;
}
/**
* Methods which return the number of free executors for a specific template
* take the template name in parameter.
*/
public static int getNumOfFreeExec(String templateName) {
int nbFreeExecutors = 0;
//List of Jenkins computer (VM into Jenkins)
List<JCloudsComputer> listComputer = JCloudsComputer.getAll();
List<Executor> listExecutors;
//For each computer..
for (int y=0; y<listComputer.size(); y++) {
//If this Virtual machine was created with the Template in parameters
//and
//If this VM is connected
if ((listComputer.get(y).getId().getTemplateName().equals(templateName)) && (listComputer.get(y).isOnline())){
//We've get the VM executors into a List
listExecutors = listComputer.get(y).getExecutors();
//For each executors in the list
for (int i = 0; i<listExecutors.size(); i++) {
//If the executor is free
if (listExecutors.get(i).isIdle()){
nbFreeExecutors = nbFreeExecutors + 1;
}
}
}
}
return nbFreeExecutors;
}
/**
* 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;
Integer instancesMin = computer.getNode().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; }
public static class OldestVM {
//attributes
public String templateName;
public String oldestVmName;
//Constructor
public OldestVM(String templateName, String oldestVmName){
this.templateName = templateName;
this.oldestVmName = oldestVmName;
}
public String getOldestVmName() {
return oldestVmName;
}
public String getTemplateName() {
return templateName;
}
public void setOldestVmName(String oldestVmName) {
this.oldestVmName = oldestVmName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
}
}