This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Backfill.java
446 lines (374 loc) · 16.7 KB
/
Backfill.java
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Copyright 1999-2011 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.globus.workspace.async.backfill;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.globus.workspace.Lager;
import org.globus.workspace.persistence.PersistenceAdapter;
import org.globus.workspace.persistence.WorkspaceDatabaseException;
import org.nimbustools.api._repr._AsyncCreateRequest;
import org.nimbustools.api._repr._Caller;
import org.nimbustools.api._repr.vm._NIC;
import org.nimbustools.api._repr.vm._RequiredVMM;
import org.nimbustools.api._repr.vm._ResourceAllocation;
import org.nimbustools.api._repr.vm._Schedule;
import org.nimbustools.api._repr.vm._VMFile;
import org.nimbustools.api.repr.AsyncCreateRequest;
import org.nimbustools.api.repr.CreateRequest;
import org.nimbustools.api.repr.ReprFactory;
import org.nimbustools.api.repr.RequestInfo;
import org.nimbustools.api.repr.si.SIConstants;
import org.nimbustools.api.repr.vm.NIC;
import org.nimbustools.api.repr.vm.ResourceAllocation;
import org.nimbustools.api.repr.vm.VMFile;
import org.nimbustools.api.services.rm.AuthorizationException;
import org.nimbustools.api.services.rm.CoSchedulingException;
import org.nimbustools.api.services.rm.CreationException;
import org.nimbustools.api.services.rm.DoesNotExistException;
import org.nimbustools.api.services.rm.ManageException;
import org.nimbustools.api.services.rm.Manager;
import org.nimbustools.api.services.rm.MetadataException;
import org.nimbustools.api.services.rm.ResourceRequestDeniedException;
import org.nimbustools.api.services.rm.SchedulingException;
import java.net.URI;
import java.net.URISyntaxException;
public class Backfill {
private static final Log logger =
LogFactory.getLog(Backfill.class.getName());
private final PersistenceAdapter persistenceAdapter;
private final ReprFactory reprFactory;
private final Lager lager;
private Manager manager;
private boolean backfillEnabled;
private int maxInstances;
private String diskImage;
private String repoUser;
private int memoryMB;
private int vcpus;
private int durationSeconds;
private String network;
private int siteCapacity;
public Backfill(PersistenceAdapter persistenceAdapter,
ReprFactory reprFactory,
Lager lager) {
this.persistenceAdapter = persistenceAdapter;
this.reprFactory = reprFactory;
this.lager = lager;
}
public void setManager(Manager manager) {
this.manager = manager;
}
public boolean isBackfillDisabled() {
return !this.backfillEnabled;
}
public boolean isBackfillEnabled() {
return this.backfillEnabled;
}
public int getMaxInstances() {
return this.maxInstances;
}
public String getDiskImage() {
return this.diskImage;
}
public String getRepoUser() {
return repoUser;
}
public int getMemoryMB() {
return this.memoryMB;
}
public int getVcpus() {
return this.vcpus;
}
public int getDurationSeconds() {
return this.durationSeconds;
}
public String getNetwork() {
return this.network;
}
public int getSiteCapacity() {
return siteCapacity;
}
public void setBackfillEnabled(boolean backfillEnabled) {
this.backfillEnabled = backfillEnabled;
}
public void setMaxInstances(int maxInstances) {
this.maxInstances = maxInstances;
}
public void setDiskImage(String diskImage) {
this.diskImage = diskImage;
}
public void setRepoUser(String repoUser) {
this.repoUser = repoUser;
}
public void setMemoryMB(int memoryMB) {
this.memoryMB = memoryMB;
}
public void setVcpus(int vcpus) {
this.vcpus = vcpus;
}
public void setDurationSeconds(int durationSeconds) {
this.durationSeconds = durationSeconds;
}
public void setNetwork(String network) {
this.network = network;
}
public void setSiteCapacity(int siteCapacity) {
this.siteCapacity = siteCapacity;
}
/**
* This method is called after service startup (not bean initialization). There are
* five situations to deal with:
*
* 1. Backfill was disabled and it is still disabled
* 2. Backfill was disabled and now it is enabled
* 3. Backfill was enabled and now it is disabled
* 4. Backfill was enabled, is still enabled, and now there is a new configuration
* 5. Backfill was enabled, is still enabled, and it's the same configuration
* 6. Backfill was enabled, is still enabled, and it's the same configuration -- and the
* configuration is for max-instances and the site's resource pool has been changed.
*/
public void initiateBackfill() throws Exception {
if (this.persistenceAdapter == null || this.reprFactory == null) {
throw new IllegalStateException("You may not invoke initiateBackfill() on a " +
"backfill instance not instantiated via IoC");
}
if (this.manager == null) {
throw new IllegalStateException("You may not invoke initiateBackfill() without " +
"priming a manager instance");
}
if (this.repoUser == null || this.repoUser.trim().length() == 0) {
throw new IllegalStateException("Backfill is not configured with a repo user.");
}
final int currentSiteCapacity = this.pollLiveSiteCapacity();
logger.debug("current site capacity MB: " + currentSiteCapacity);
this.setSiteCapacity(currentSiteCapacity);
final Backfill oldBackfill = this.persistenceAdapter.getStoredBackfill();
if (this.isBackfillDisabled()) {
logger.info("Backfill is disabled.");
if (oldBackfill != null && oldBackfill.isBackfillEnabled()) {
// 3. Backfill was enabled and now it is disabled
logger.info("Backfill was previously enabled.");
this.withdrawAllRequests();
} else {
// 1. Backfill was disabled and it is still disabled
logger.info("Backfill was previously disabled, nothing to do");
}
this.saveCurrentConfiguration();
return; // EARLY RETURN
}
logger.info("Backfill is enabled.");
// 2. Backfill was disabled and now it is enabled
if (oldBackfill != null && oldBackfill.isBackfillDisabled()) {
logger.info("Backfill was previously disabled.");
this.registerBackfillRequest();
this.saveCurrentConfiguration();
return; // EARLY RETURN
}
boolean newConfig = false;
if (oldBackfill == null || !oldBackfill.equals(this)) {
newConfig = true;
// 4. Backfill was enabled, is still enabled, and now there is a new configuration
logger.info("Backfill was previously enabled, but has a new configuration.");
}
// 6. Backfill was enabled, is still enabled, and it's the same configuration -- and the
// configuration is for max-instances and the site's resource pool has been changed.
// If newConfig is true, no need to check because this will be recalculated anyhow.
// What we need to catch is where the (max) config hasn't changed but the site has!
if (!newConfig && this.getMaxInstances() == 0) {
if (oldBackfill != null
&& oldBackfill.getSiteCapacity() != currentSiteCapacity) {
newConfig = true;
logger.info("Site reports different VM capacity, going to recalculate " +
"backfill instance number and reconfigure.");
}
}
if (newConfig) {
this.withdrawAllRequests();
this.registerBackfillRequest();
this.saveCurrentConfiguration();
} else {
// 5. Backfill was enabled, is still enabled, and it's the same configuration
logger.info("Backfill was previously enabled and has the same configuration, " +
"nothing to do.");
}
}
private void withdrawAllRequests()
throws ManageException, AuthorizationException, DoesNotExistException {
// From http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-soap-CancelSpotInstanceRequests.html
// Canceling a Spot Instance request does not terminate running Spot Instances
// associated with the request.
// But, with backfill we want to withdraw, so the async manager will do that for these
// requests (that are marked backfill by "isSpotRequest()" being false.
logger.info("Withdrawing previous backfill configuration from the scheduler");
if (this.persistenceAdapter == null
|| this.manager == null || this.reprFactory == null) {
throw new IllegalStateException("Programmer error.");
}
_Caller superuser = this.reprFactory._newCaller();
superuser.setIdentity(this.repoUser);
superuser.setSuperUser(true);
RequestInfo[] bfRequests = this.manager.getBackfillRequestsByCaller(superuser);
if (bfRequests == null || bfRequests.length == 0) {
logger.info("No previous backfill requests were registered");
return;
}
final String[] ids = new String[bfRequests.length];
for (int i = 0; i < bfRequests.length; i++) {
final String reqID = bfRequests[i].getRequestID();
ids[i] = reqID;
}
this.manager.cancelBackfillRequests(ids, superuser);
}
private void registerBackfillRequest()
throws WorkspaceDatabaseException, SchedulingException,
ResourceRequestDeniedException, CreationException, AuthorizationException,
MetadataException, CoSchedulingException {
logger.info("Registering backfill configuration with the scheduler");
if (this.persistenceAdapter == null
|| this.manager == null || this.reprFactory == null) {
throw new IllegalStateException("Programmer error.");
}
int numInstances;
if (this.maxInstances == 0) {
// If zero, request is for as many as possible.
// This functionality should be consolidated: you put in a type of instance and
// it tells you how many can fit in a given capacity. This calculation does not
// take into account a possible reduction because of limited network mappings.
// But the scheduler can't launch more instances than there are networks available
// so it will top out.
numInstances = this.getSiteCapacity() / this.memoryMB;
if (numInstances < 1) {
logger.warn("Not enough total VM capacity for even one backfill node.");
numInstances = 1;
}
} else {
numInstances = this.maxInstances;
}
_Caller superuser = this.reprFactory._newCaller();
superuser.setIdentity(this.repoUser);
superuser.setSuperUser(true);
for (int i = 0; i < numInstances; i++) {
AsyncCreateRequest req = this.getBackfillRequest(1, "BACKFILL-" + (i+1));
this.manager.addBackfillRequest(req, superuser);
}
}
// Allow easy IoC driven extension for customizing this better
public AsyncCreateRequest getBackfillRequest(int numInstances, String name)
throws WorkspaceDatabaseException {
if (this.lager.traceLog) {
final StringBuilder sb = new StringBuilder("Backfill request:");
sb.append("\nNum instances: ").append(numInstances);
sb.append("\nImage: ").append(this.getDiskImage());
sb.append("\nNetwork: ").append(this.getNetwork());
sb.append("\nMemory: ").append(this.getMemoryMB());
sb.append("\nvcpus: ").append(this.getVcpus());
logger.debug(sb.toString());
}
final _AsyncCreateRequest req = this.reprFactory._newBackfillRequest();
req.setInstanceType(SIConstants.SI_TYPE_BASIC);
req.setName(name);
final _NIC nic = this.reprFactory._newNIC();
nic.setNetworkName(this.getNetwork());
nic.setAcquisitionMethod(NIC.ACQUISITION_AllocateAndConfigure);
req.setRequestedNics(new _NIC[]{nic});
final _ResourceAllocation ra = this.reprFactory._newResourceAllocation();
req.setRequestedRA(ra);
final _Schedule schedule = this.reprFactory._newSchedule();
schedule.setDurationSeconds(this.getDurationSeconds());
req.setRequestedSchedule(schedule);
ra.setNodeNumber(numInstances);
ra.setIndCpuCount(this.getVcpus());
ra.setMemory(this.getMemoryMB());
req.setShutdownType(CreateRequest.SHUTDOWN_TYPE_TRASH);
req.setInitialStateRequest(CreateRequest.INITIAL_STATE_RUNNING);
ra.setArchitecture(ResourceAllocation.ARCH_x86);
ra.setSpotInstance(true);
final _RequiredVMM reqVMM = this.reprFactory._newRequiredVMM();
reqVMM.setType("Xen");
reqVMM.setVersions(new String[]{"3"});
req.setRequiredVMM(reqVMM);
final _VMFile file = this.reprFactory._newVMFile();
file.setRootFile(true);
file.setBlankSpaceName(null);
file.setBlankSpaceSize(-1);
try {
file.setURI(new URI("file://" + this.getDiskImage()));
} catch (URISyntaxException e) {
throw new WorkspaceDatabaseException(e.getMessage());
}
file.setMountAs("sda1");
file.setDiskPerms(VMFile.DISKPERMS_ReadWrite);
req.setVMFiles(new _VMFile[]{file});
return req;
}
private int pollLiveSiteCapacity() throws WorkspaceDatabaseException {
return this.persistenceAdapter.getTotalMaxMemory();
}
private void saveCurrentConfiguration() throws WorkspaceDatabaseException {
if (this.persistenceAdapter == null) {
throw new IllegalStateException("Programmer error.");
}
this.persistenceAdapter.setBackfill(this);
}
public void validate() throws Exception {
if (this.persistenceAdapter == null) {
throw new IllegalStateException("There is no persistenceAdapter configured");
}
if (this.maxInstances < 0) {
throw new Exception("maxInstances may not be less than 0");
}
if (this.diskImage == null) {
throw new Exception("diskImage may not be null");
}
if (this.memoryMB < 8) {
throw new Exception("memoryMB must be a reasonable value. " +
"Have you considered at least 64 MB?");
}
if (this.durationSeconds < 60) {
throw new Exception("durationSeconds must be 60 seconds or longer.");
}
if (this.network == null) {
throw new Exception("network may not be null");
}
logger.debug("Validated backfill settings");
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Backfill backfill = (Backfill) o;
if (backfillEnabled != backfill.backfillEnabled) return false;
if (durationSeconds != backfill.durationSeconds) return false;
if (maxInstances != backfill.maxInstances) return false;
if (memoryMB != backfill.memoryMB) return false;
if (vcpus != backfill.vcpus) return false;
if (diskImage != null ? !diskImage.equals(backfill.diskImage) :
backfill.diskImage != null)
return false;
if (network != null ? !network.equals(backfill.network) : backfill.network != null)
return false;
return true;
}
public int hashCode() {
int result = (backfillEnabled ? 1 : 0);
result = 31 * result + maxInstances;
result = 31 * result + (diskImage != null ? diskImage.hashCode() : 0);
result = 31 * result + memoryMB;
result = 31 * result + vcpus;
result = 31 * result + durationSeconds;
result = 31 * result + (network != null ? network.hashCode() : 0);
return result;
}
}