/
PrimaryStorageManagerImpl.java
executable file
·651 lines (571 loc) · 28.9 KB
/
PrimaryStorageManagerImpl.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package org.zstack.storage.primary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.zstack.core.Platform;
import org.zstack.core.cloudbus.*;
import org.zstack.core.componentloader.PluginRegistry;
import org.zstack.core.config.GlobalConfigException;
import org.zstack.core.config.GlobalConfigValidatorExtensionPoint;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.core.db.DbEntityLister;
import org.zstack.core.db.SQL;
import org.zstack.core.db.SimpleQuery;
import org.zstack.core.db.SimpleQuery.Op;
import org.zstack.core.defer.Deferred;
import org.zstack.core.errorcode.ErrorFacade;
import org.zstack.core.thread.AsyncThread;
import org.zstack.header.AbstractService;
import org.zstack.header.apimediator.ApiMessageInterceptionException;
import org.zstack.header.errorcode.ErrorCode;
import org.zstack.header.errorcode.OperationFailureException;
import org.zstack.header.errorcode.SysErrors;
import org.zstack.header.exception.CloudRuntimeException;
import org.zstack.header.managementnode.ManagementNodeChangeListener;
import org.zstack.header.managementnode.ManagementNodeReadyExtensionPoint;
import org.zstack.header.message.APIMessage;
import org.zstack.header.message.Message;
import org.zstack.header.message.MessageReply;
import org.zstack.header.storage.primary.*;
import org.zstack.header.tag.SystemTagValidator;
import org.zstack.header.vm.VmInstanceInventory;
import org.zstack.header.vm.VmInstanceStartExtensionPoint;
import org.zstack.search.GetQuery;
import org.zstack.search.SearchQuery;
import org.zstack.tag.TagManager;
import org.zstack.utils.*;
import org.zstack.utils.function.Function;
import org.zstack.utils.logging.CLogger;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import java.util.*;
import java.util.concurrent.Callable;
import static org.zstack.core.Platform.argerr;
import static org.zstack.core.Platform.operr;
public class PrimaryStorageManagerImpl extends AbstractService implements PrimaryStorageManager,
ManagementNodeChangeListener, ManagementNodeReadyExtensionPoint, VmInstanceStartExtensionPoint {
private static final CLogger logger = Utils.getLogger(PrimaryStorageManager.class);
@Autowired
private CloudBus bus;
@Autowired
private PluginRegistry pluginRgty;
@Autowired
private DatabaseFacade dbf;
@Autowired
private DbEntityLister dl;
@Autowired
private ErrorFacade errf;
@Autowired
private EventFacade evtf;
@Autowired
private TagManager tagMgr;
@Autowired
private ResourceDestinationMaker destMaker;
@Autowired
private PrimaryStorageOverProvisioningManager ratioMgr;
@Autowired
private PrimaryStoragePhysicalCapacityManager physicalCapacityMgr;
private Map<String, PrimaryStorageFactory> primaryStorageFactories = Collections.synchronizedMap(new HashMap<>());
private Map<String, PrimaryStorageAllocatorStrategyFactory> allocatorFactories = Collections.synchronizedMap(new HashMap<>());
private static final Set<Class> allowedMessageAfterSoftDeletion = new HashSet<>();
static {
allowedMessageAfterSoftDeletion.add(PrimaryStorageDeletionMsg.class);
}
void init() {
PrimaryStorageSystemTags.PRIMARY_STORAGE_ALLOCATOR_UUID_TAG.installValidator(new SystemTagValidator() {
@Override
public void validateSystemTag(String resourceUuid, Class resourceType, String systemTag) {
String uuid = PrimaryStorageSystemTags.PRIMARY_STORAGE_ALLOCATOR_UUID_TAG.getTokenByTag(
systemTag, PrimaryStorageSystemTags.PRIMARY_STORAGE_ALLOCATOR_UUID_TAG_TOKEN);
if (!StringDSL.isZStackUuid(uuid)) {
throw new ApiMessageInterceptionException(argerr("%s is invalid. %s is not a valid zstack uuid", systemTag, uuid));
}
if (!dbf.isExist(uuid, PrimaryStorageVO.class)) {
throw new ApiMessageInterceptionException(argerr("no primary storage[uuid:%s] found", resourceUuid));
}
}
});
}
private void handleApiMessage(APIMessage msg) {
if (msg instanceof APIAddPrimaryStorageMsg) {
handle((APIAddPrimaryStorageMsg) msg);
} else if (msg instanceof APIListPrimaryStorageMsg) {
handle((APIListPrimaryStorageMsg) msg);
} else if (msg instanceof APISearchPrimaryStorageMsg) {
handle((APISearchPrimaryStorageMsg) msg);
} else if (msg instanceof APIGetPrimaryStorageMsg) {
handle((APIGetPrimaryStorageMsg) msg);
} else if (msg instanceof PrimaryStorageMessage) {
passThrough((PrimaryStorageMessage) msg);
} else if (msg instanceof APIGetPrimaryStorageTypesMsg) {
handle((APIGetPrimaryStorageTypesMsg) msg);
} else if (msg instanceof APIGetPrimaryStorageAllocatorStrategiesMsg) {
handle((APIGetPrimaryStorageAllocatorStrategiesMsg) msg);
} else if (msg instanceof APIGetPrimaryStorageCapacityMsg) {
handle((APIGetPrimaryStorageCapacityMsg) msg);
} else {
bus.dealWithUnknownMessage(msg);
}
}
private void handle(final APIGetPrimaryStorageCapacityMsg msg) {
APIGetPrimaryStorageCapacityReply reply = new APIGetPrimaryStorageCapacityReply();
Tuple ret = new Callable<Tuple>() {
@Override
@Transactional(readOnly = true)
public Tuple call() {
if (msg.getPrimaryStorageUuids() != null && !msg.getPrimaryStorageUuids().isEmpty()) {
String sql = "select sum(psc.totalCapacity)," +
" sum(psc.availableCapacity)," +
" sum(psc.totalPhysicalCapacity)," +
" sum(psc.availablePhysicalCapacity)" +
" from PrimaryStorageCapacityVO psc" +
" where psc.uuid in (:psUuids)";
TypedQuery<Tuple> q = dbf.getEntityManager().createQuery(sql, Tuple.class);
q.setParameter("psUuids", msg.getPrimaryStorageUuids());
return q.getSingleResult();
} else if (msg.getClusterUuids() != null && !msg.getClusterUuids().isEmpty()) {
String sql = "select sum(psc.totalCapacity)," +
" sum(psc.availableCapacity)," +
" sum(psc.totalPhysicalCapacity)," +
" sum(psc.availablePhysicalCapacity)" +
" from PrimaryStorageCapacityVO psc, PrimaryStorageClusterRefVO ref" +
" where ref.primaryStorageUuid = psc.uuid" +
" and ref.clusterUuid in (:clusterUuids)";
TypedQuery<Tuple> q = dbf.getEntityManager().createQuery(sql, Tuple.class);
q.setParameter("clusterUuids", msg.getClusterUuids());
return q.getSingleResult();
} else if (msg.getZoneUuids() != null && !msg.getZoneUuids().isEmpty()) {
String sql = "select sum(psc.totalCapacity)," +
" sum(psc.availableCapacity)," +
" sum(psc.totalPhysicalCapacity)," +
" sum(psc.availablePhysicalCapacity)" +
" from PrimaryStorageCapacityVO psc, PrimaryStorageVO ps" +
" where ps.uuid = psc.uuid" +
" and ps.zoneUuid in (:zoneUuids)";
TypedQuery<Tuple> q = dbf.getEntityManager().createQuery(sql, Tuple.class);
q.setParameter("zoneUuids", msg.getZoneUuids());
return q.getSingleResult();
}
throw new CloudRuntimeException("should not be here");
}
}.call();
Long total = ret.get(0, Long.class);
Long avail = ret.get(1, Long.class);
Long ptotal = ret.get(2, Long.class);
Long pavail = ret.get(3, Long.class);
reply.setTotalCapacity(total == null ? 0 : total);
reply.setAvailableCapacity(avail == null ? 0 : avail);
reply.setTotalPhysicalCapacity(ptotal == null ? 0 : ptotal);
reply.setAvailablePhysicalCapacity(pavail == null ? 0 : pavail);
bus.reply(msg, reply);
}
private void handle(APIGetPrimaryStorageAllocatorStrategiesMsg msg) {
APIGetPrimaryStorageAllocatorStrategiesReply reply = new APIGetPrimaryStorageAllocatorStrategiesReply();
reply.setPrimaryStorageAllocatorStrategies(PrimaryStorageAllocatorStrategyType.getAllExposedTypeNames());
bus.reply(msg, reply);
}
private void handle(APIGetPrimaryStorageTypesMsg msg) {
APIGetPrimaryStorageTypesReply reply = new APIGetPrimaryStorageTypesReply();
List<String> ret = new ArrayList<>();
ret.addAll(PrimaryStorageType.getAllTypeNames());
reply.setPrimaryStorageTypes(ret);
bus.reply(msg, reply);
}
private void handle(APIGetPrimaryStorageMsg msg) {
GetQuery q = new GetQuery();
String res = q.getAsString(msg, PrimaryStorageInventory.class);
APIGetPrimaryStorageReply reply = new APIGetPrimaryStorageReply();
reply.setInventory(res);
bus.reply(msg, reply);
}
private void handle(APISearchPrimaryStorageMsg msg) {
SearchQuery<PrimaryStorageInventory> sq = SearchQuery.create(msg, PrimaryStorageInventory.class);
String content = sq.listAsString();
APISearchPrimaryStorageReply reply = new APISearchPrimaryStorageReply();
reply.setContent(content);
bus.reply(msg, reply);
}
private void handle(APIListPrimaryStorageMsg msg) {
List<PrimaryStorageVO> vos = dl.listByApiMessage(msg, PrimaryStorageVO.class);
List<PrimaryStorageInventory> invs = PrimaryStorageInventory.valueOf(vos);
APIListPrimaryStorageReply reply = new APIListPrimaryStorageReply();
reply.setInventories(invs);
bus.reply(msg, reply);
}
private void passThrough(PrimaryStorageMessage pmsg) {
PrimaryStorageVO vo = dbf.findByUuid(pmsg.getPrimaryStorageUuid(), PrimaryStorageVO.class);
if (vo == null && allowedMessageAfterSoftDeletion.contains(pmsg.getClass())) {
PrimaryStorageEO eo = dbf.findByUuid(pmsg.getPrimaryStorageUuid(), PrimaryStorageEO.class);
vo = ObjectUtils.newAndCopy(eo, PrimaryStorageVO.class);
}
Message msg = (Message) pmsg;
if (vo == null) {
String err = String.format("Cannot find primary storage[uuid:%s], it may have been deleted", pmsg.getPrimaryStorageUuid());
bus.replyErrorByMessageType(msg, errf.instantiateErrorCode(SysErrors.RESOURCE_NOT_FOUND, err));
return;
}
PrimaryStorageFactory factory = getPrimaryStorageFactory(PrimaryStorageType.valueOf(vo.getType()));
PrimaryStorage ps = factory.getPrimaryStorage(vo);
ps.handleMessage(msg);
}
@Deferred
private void handle(final APIAddPrimaryStorageMsg msg) {
PrimaryStorageType type = PrimaryStorageType.valueOf(msg.getType());
final PrimaryStorageFactory factory = getPrimaryStorageFactory(type);
PrimaryStorageVO primaryStorageVO = new PrimaryStorageVO();
if (msg.getResourceUuid() != null) {
primaryStorageVO.setUuid(msg.getResourceUuid());
} else {
primaryStorageVO.setUuid(Platform.getUuid());
}
primaryStorageVO.setUrl(msg.getUrl());
primaryStorageVO.setType(type.toString());
primaryStorageVO.setName(msg.getName());
primaryStorageVO.setDescription(msg.getDescription());
primaryStorageVO.setState(PrimaryStorageState.Enabled);
primaryStorageVO.setStatus(PrimaryStorageStatus.Connecting);
primaryStorageVO.setZoneUuid(msg.getZoneUuid());
final APIAddPrimaryStorageEvent evt = new APIAddPrimaryStorageEvent(msg.getId());
final PrimaryStorageInventory inv = factory.createPrimaryStorage(primaryStorageVO, msg);
primaryStorageVO = dbf.findByUuid(primaryStorageVO.getUuid(), PrimaryStorageVO.class);
tagMgr.createTagsFromAPICreateMessage(msg, inv.getUuid(), PrimaryStorageVO.class.getSimpleName());
PrimaryStorageCapacityVO primaryStorageCapacityVO = dbf.findByUuid(primaryStorageVO.getUuid(), PrimaryStorageCapacityVO.class);
if (primaryStorageCapacityVO == null) {
primaryStorageCapacityVO = new PrimaryStorageCapacityVO();
primaryStorageCapacityVO.setUuid(primaryStorageVO.getUuid());
dbf.persist(primaryStorageCapacityVO);
}
final ConnectPrimaryStorageMsg cmsg = new ConnectPrimaryStorageMsg();
cmsg.setPrimaryStorageUuid(inv.getUuid());
cmsg.setNewAdded(true);
bus.makeTargetServiceIdByResourceUuid(cmsg, PrimaryStorageConstant.SERVICE_ID, inv.getUuid());
final PrimaryStorageVO finalVo = primaryStorageVO;
bus.send(cmsg, new CloudBusCallBack(msg) {
@Override
public void run(MessageReply reply) {
if (!reply.isSuccess() && !reply.getError().isError(PrimaryStorageErrors.DISCONNECTED)) {
evt.setError(reply.getError());
logger.warn(String.format("failed to connect primary storage[uuid:%s, name:%s, url:%s]",
finalVo.getUuid(), finalVo.getName(), finalVo.getUrl()));
dbf.remove(finalVo);
} else {
PrimaryStorageInventory pinv = factory.getInventory(finalVo.getUuid());
logger.debug(String.format("successfully add primary storage[uuid:%s, name:%s, url: %s]",
finalVo.getUuid(), finalVo.getName(), finalVo.getUrl()));
evt.setInventory(pinv);
}
bus.publish(evt);
}
});
}
@Override
@MessageSafe
public void handleMessage(Message msg) {
if (msg instanceof APIMessage) {
handleApiMessage((APIMessage) msg);
} else {
handleLocalMessage(msg);
}
}
private void handleLocalMessage(Message msg) {
if (msg instanceof AllocatePrimaryStorageMsg) {
handle((AllocatePrimaryStorageMsg) msg);
} else if (msg instanceof IncreasePrimaryStorageCapacityMsg) {
handle((IncreasePrimaryStorageCapacityMsg) msg);
} else if (msg instanceof DecreasePrimaryStorageCapacityMsg) {
handle((DecreasePrimaryStorageCapacityMsg) msg);
} else if (msg instanceof RecalculatePrimaryStorageCapacityMsg) {
handle((RecalculatePrimaryStorageCapacityMsg) msg);
} else if (msg instanceof PrimaryStorageMessage) {
passThrough((PrimaryStorageMessage) msg);
} else {
bus.dealWithUnknownMessage(msg);
}
}
private void handle(final RecalculatePrimaryStorageCapacityMsg msg) {
RecalculatePrimaryStorageCapacityReply reply = new RecalculatePrimaryStorageCapacityReply();
final List<String> psUuids = new ArrayList<>();
if (msg.getPrimaryStorageUuid() != null) {
passThrough(msg);
} else if (msg.getZoneUuid() != null) {
SimpleQuery<PrimaryStorageVO> q = dbf.createQuery(PrimaryStorageVO.class);
q.select(PrimaryStorageVO_.uuid);
q.add(PrimaryStorageVO_.zoneUuid, Op.EQ, msg.getZoneUuid());
List<String> uuids = q.listValue();
psUuids.addAll(uuids);
PrimaryStorageCapacityRecalculator psRecal = new PrimaryStorageCapacityRecalculator();
psRecal.psUuids = psUuids;
psRecal.recalculate();
bus.reply(msg, reply);
}
}
private void handle(IncreasePrimaryStorageCapacityMsg msg) {
long diskSize = msg.isNoOverProvisioning() ? msg.getDiskSize() : ratioMgr.calculateByRatio(msg.getPrimaryStorageUuid(), msg.getDiskSize());
PrimaryStorageCapacityUpdater updater = new PrimaryStorageCapacityUpdater(msg.getPrimaryStorageUuid());
if (updater.increaseAvailableCapacity(diskSize)) {
if (logger.isTraceEnabled()) {
logger.trace(String.format("Successfully return %s bytes to primary storage[uuid:%s]",
diskSize, msg.getPrimaryStorageUuid()));
}
}
}
private void handle(DecreasePrimaryStorageCapacityMsg msg) {
long diskSize = msg.isNoOverProvisioning() ? msg.getDiskSize() : ratioMgr.calculateByRatio(msg.getPrimaryStorageUuid(), msg.getDiskSize());
PrimaryStorageCapacityUpdater updater = new PrimaryStorageCapacityUpdater(msg.getPrimaryStorageUuid());
if (updater.decreaseAvailableCapacity(diskSize)) {
if (logger.isTraceEnabled()) {
logger.trace(String.format("Successfully return %s bytes to primary storage[uuid:%s]",
diskSize, msg.getPrimaryStorageUuid()));
}
}
}
/**
* Supported allocation strategy:
* DefaultPrimaryStorageAllocationStrategy (only work for non-local primary storage)
* LocalPrimaryStorageStrategy (only work for local primary storage)
*
* Note:
* If the allocation strategy is not specified
* If the cluster is mounted with local storage, the default is LocalPrimaryStorageStrategy。
* Otherwise, it is DefaultPrimaryStorageAllocationStrategy
*/
private void handle(AllocatePrimaryStorageMsg msg) {
AllocatePrimaryStorageReply reply = new AllocatePrimaryStorageReply(null);
String allocatorStrategyType = null;
for (PrimaryStorageAllocatorStrategyExtensionPoint ext : pluginRgty.getExtensionList(PrimaryStorageAllocatorStrategyExtensionPoint.class)) {
allocatorStrategyType = ext.getPrimaryStorageAllocatorStrategyName(msg);
if (allocatorStrategyType != null) {
break;
}
}
if (allocatorStrategyType == null) {
allocatorStrategyType = msg.getAllocationStrategy() == null ?
PrimaryStorageConstant.DEFAULT_PRIMARY_STORAGE_ALLOCATION_STRATEGY_TYPE
: msg.getAllocationStrategy();
}
if (msg.getExcludeAllocatorStrategies() != null && msg.getExcludeAllocatorStrategies().contains(allocatorStrategyType)) {
throw new CloudRuntimeException(
String.format("%s is set as excluded, there is no available primary storage allocator strategy",
allocatorStrategyType));
}
PrimaryStorageAllocatorStrategyFactory factory = getPrimaryStorageAllocatorStrategyFactory(
PrimaryStorageAllocatorStrategyType.valueOf(allocatorStrategyType));
PrimaryStorageAllocatorStrategy strategy = factory.getPrimaryStorageAllocatorStrategy();
//
PrimaryStorageAllocationSpec spec = new PrimaryStorageAllocationSpec();
spec.setPossiblePrimaryStorageTypes(msg.getPossiblePrimaryStorageTypes());
spec.setExcludePrimaryStorageTypes(msg.getExcludePrimaryStorageTypes());
spec.setImageUuid(msg.getImageUuid());
spec.setDiskOfferingUuid(msg.getDiskOfferingUuid());
spec.setVmInstanceUuid(msg.getVmInstanceUuid());
spec.setPurpose(msg.getPurpose());
spec.setSize(msg.getSize());
spec.setNoOverProvisioning(msg.isNoOverProvisioning());
spec.setRequiredClusterUuids(msg.getRequiredClusterUuids());
spec.setRequiredHostUuid(msg.getRequiredHostUuid());
spec.setRequiredZoneUuid(msg.getRequiredZoneUuid());
spec.setRequiredPrimaryStorageUuid(msg.getRequiredPrimaryStorageUuid());
spec.setTags(msg.getTags());
spec.setAllocationMessage(msg);
spec.setAvoidPrimaryStorageUuids(msg.getExcludePrimaryStorageUuids());
List<PrimaryStorageInventory> ret = strategy.allocateAllCandidates(spec);
if (msg.isDryRun()){
// check capacity has been done before
AllocatePrimaryStorageDryRunReply r = new AllocatePrimaryStorageDryRunReply();
r.setPrimaryStorageInventories(ret);
bus.reply(msg, r);
return;
}
Iterator<PrimaryStorageInventory> it = ret.iterator();
List<String> errs = new ArrayList<>();
PrimaryStorageInventory target = null;
while (it.hasNext()) {
PrimaryStorageInventory psInv = it.next();
if (!physicalCapacityMgr.checkCapacityByRatio(psInv.getUuid(), psInv.getTotalPhysicalCapacity(), psInv.getAvailablePhysicalCapacity())) {
errs.add(String.format("primary storage[uuid:%s]'s physical capacity usage has exceeded the threshold[%s]",
psInv.getUuid(), physicalCapacityMgr.getRatio(psInv.getUuid())));
continue;
}
long requiredSize = spec.getSize();
if (!msg.isNoOverProvisioning()) {
requiredSize = ratioMgr.calculateByRatio(psInv.getUuid(), requiredSize);
}
if (reserve(psInv, requiredSize)) {
target = psInv;
break;
} else {
errs.add(String.format("unable to reserve capacity on the primary storage[uuid:%s], it has no space", psInv.getUuid()));
logger.debug(String.format("concurrent reservation on the primary storage[uuid:%s], try next one", psInv.getUuid()));
}
}
if (target == null) {
throw new OperationFailureException(operr("cannot find any qualified primary storage, errors are %s", errs));
}
reply.setPrimaryStorageInventory(target);
reply.setSize(msg.getSize());
bus.reply(msg, reply);
}
private boolean reserve(final PrimaryStorageInventory inv, final long size) {
PrimaryStorageCapacityUpdater updater = new PrimaryStorageCapacityUpdater(inv.getUuid());
return updater.run(new PrimaryStorageCapacityUpdaterRunnable() {
@Override
public PrimaryStorageCapacityVO call(PrimaryStorageCapacityVO cap) {
long avail = cap.getAvailableCapacity() - size;
if (avail < 0) {
logger.warn(String.format("[Primary Storage Allocation] reserved capacity on primary storage[uuid:%s] failed," +
" no available capacity on it", inv.getUuid()));
return null;
}
long origin = cap.getAvailableCapacity();
cap.setAvailableCapacity(avail);
if (logger.isTraceEnabled()) {
logger.trace(String.format("[Primary Storage Allocation] reserved %s bytes on primary storage[uuid:%s," +
" available before:%s, available now:%s]", size, inv.getUuid(), origin, avail));
}
return cap;
}
});
}
@Override
public String getId() {
return bus.makeLocalServiceId(PrimaryStorageConstant.SERVICE_ID);
}
@Override
public boolean start() {
populateExtensions();
installGlobalConfigValidator();
return true;
}
private void installGlobalConfigValidator() {
PrimaryStorageGlobalConfig.RESERVED_CAPACITY.installValidateExtension(new GlobalConfigValidatorExtensionPoint() {
@Override
public void validateGlobalConfig(String category, String name, String oldValue, String newValue) throws GlobalConfigException {
if (!SizeUtils.isSizeString(newValue)) {
throw new GlobalConfigException(String.format("%s is not a size string;" +
" a size string consists of a number ending with suffix B/K/M/G/T or without suffix;" +
" for example, 512M, 1G", newValue));
}
}
});
}
@Override
public boolean stop() {
return true;
}
private void populateExtensions() {
for (PrimaryStorageAllocatorStrategyFactory f : pluginRgty.getExtensionList(PrimaryStorageAllocatorStrategyFactory.class)) {
PrimaryStorageAllocatorStrategyFactory old = allocatorFactories.get(f.getPrimaryStorageAllocatorStrategyType().toString());
if (old != null) {
throw new CloudRuntimeException(String.format("duplicate PrimaryStorageAllocatorStrategyFactory[%s, %s] for type[%s]",
f.getClass().getName(), old.getClass().getName(), f.getPrimaryStorageAllocatorStrategyType()));
}
allocatorFactories.put(f.getPrimaryStorageAllocatorStrategyType().toString(), f);
}
for (PrimaryStorageFactory f : pluginRgty.getExtensionList(PrimaryStorageFactory.class)) {
PrimaryStorageFactory old = primaryStorageFactories.get(f.getPrimaryStorageType().toString());
if (old != null) {
throw new CloudRuntimeException(String.format("duplicate PrimaryStorageFactory[%s, %s] for type[%s]",
f.getClass().getName(), old.getClass().getName(), old.getPrimaryStorageType()));
}
primaryStorageFactories.put(f.getPrimaryStorageType().toString(), f);
}
}
private PrimaryStorageAllocatorStrategyFactory getPrimaryStorageAllocatorStrategyFactory(PrimaryStorageAllocatorStrategyType type) {
PrimaryStorageAllocatorStrategyFactory factory = allocatorFactories.get(type.toString());
if (factory == null) {
throw new CloudRuntimeException(String.format("No PrimaryStorageAllocatorStrategyFactory for type: %s found", type));
}
return factory;
}
@Override
public PrimaryStorageFactory getPrimaryStorageFactory(PrimaryStorageType type) {
PrimaryStorageFactory factory = primaryStorageFactories.get(type.toString());
if (factory == null) {
throw new CloudRuntimeException(String.format("No PrimaryStorageFactory for type: %s found", type));
}
return factory;
}
@Override
public void nodeJoin(String nodeId) {
logger.debug(String.format("management node[uuid:%s] join, node[uuid:%s] starts taking over primary storage...",
nodeId, Platform.getManagementServerId()));
loadPrimaryStorage();
}
@Override
public void nodeLeft(String nodeId) {
}
private List<String> getPrimaryStorageManagedByUs() {
List<String> ret = new ArrayList<>();
SimpleQuery<PrimaryStorageVO> q = dbf.createQuery(PrimaryStorageVO.class);
q.select(PrimaryStorageVO_.uuid);
List<String> uuids = q.listValue();
for (String uuid : uuids) {
if (destMaker.isManagedByUs(uuid)) {
ret.add(uuid);
}
}
return ret;
}
private void loadPrimaryStorage() {
List<String> uuids = getPrimaryStorageManagedByUs();
if (uuids.isEmpty()) {
return;
}
List<ConnectPrimaryStorageMsg> msgs = CollectionUtils.transformToList(uuids, new Function<ConnectPrimaryStorageMsg, String>() {
@Override
public ConnectPrimaryStorageMsg call(String arg) {
ConnectPrimaryStorageMsg msg = new ConnectPrimaryStorageMsg();
msg.setPrimaryStorageUuid(arg);
bus.makeTargetServiceIdByResourceUuid(msg, PrimaryStorageConstant.SERVICE_ID, arg);
return msg;
}
});
bus.send(msgs);
}
@Override
public void iAmDead(String nodeId) {
}
@Override
public void iJoin(String nodeId) {
}
@AsyncThread
@Override
public void managementNodeReady() {
logger.debug(String.format("management node[uuid:%s] joins, starts load primary storage ...",
Platform.getManagementServerId()));
loadPrimaryStorage();
}
private void checkVmAllVolumePrimaryStorageState(String vmUuid) {
String sql = "select uuid from PrimaryStorageVO where uuid in (" +
" select distinct(primaryStorageUuid) from VolumeVO" +
" where vmInstanceUuid = :vmUuid and primaryStorageUuid is not null)" +
" and state = :psState";
List<String> result = SQL.New(sql, String.class)
.param("vmUuid", vmUuid)
.param("psState", PrimaryStorageState.Maintenance)
.list();
if (result != null && !result.isEmpty()) {
throw new OperationFailureException(argerr("the VM[uuid:%s] volume stored location primary storage is in a state of maintenance", vmUuid));
}
}
@Override
public String preStartVm(VmInstanceInventory inv) {
try{
checkVmAllVolumePrimaryStorageState(inv.getUuid());
return null;
}catch (Exception e){
return e.getMessage();
}
}
@Override
public void beforeStartVm(VmInstanceInventory inv) {
// do nothing
}
@Override
public void afterStartVm(VmInstanceInventory inv) {
// do nothing
}
@Override
public void failedToStartVm(VmInstanceInventory inv, ErrorCode reason) {
// do nothing
}
}