Skip to content

Commit e13959a

Browse files
committed
CLOUDSTACK-3716: State of expunged volumes are not consistent in volumes
table and volume_store_ref.
1 parent 24af02e commit e13959a

5 files changed

Lines changed: 120 additions & 55 deletions

File tree

‎engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/VolumeDataFactory.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
*/
1919
package org.apache.cloudstack.engine.subsystem.api.storage;
2020

21+
import com.cloud.storage.DataStoreRole;
22+
2123
public interface VolumeDataFactory {
2224
VolumeInfo getVolume(long volumeId, DataStore store);
2325

2426
VolumeInfo getVolume(DataObject volume, DataStore store);
2527

28+
VolumeInfo getVolume(long volumeId, DataStoreRole storeRole);
29+
2630
VolumeInfo getVolume(long volumeId);
2731
}

‎engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeDataFactoryImpl.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ public VolumeInfo getVolume(long volumeId, DataStore store) {
5151
return vol;
5252
}
5353

54+
@Override
55+
public VolumeInfo getVolume(long volumeId, DataStoreRole storeRole) {
56+
VolumeVO volumeVO = volumeDao.findById(volumeId);
57+
VolumeObject vol = null;
58+
if (storeRole == DataStoreRole.Image) {
59+
VolumeDataStoreVO volumeStore = volumeStoreDao.findByVolume(volumeId);
60+
if (volumeStore != null) {
61+
DataStore store = this.storeMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image);
62+
vol = VolumeObject.getVolumeObject(store, volumeVO);
63+
}
64+
} else {
65+
// Primary data store
66+
if (volumeVO.getPoolId() != null) {
67+
DataStore store = this.storeMgr.getDataStore(volumeVO.getPoolId(), DataStoreRole.Primary);
68+
vol = VolumeObject.getVolumeObject(store, volumeVO);
69+
}
70+
}
71+
return vol;
72+
}
73+
5474
@Override
5575
public VolumeInfo getVolume(long volumeId) {
5676
VolumeVO volumeVO = volumeDao.findById(volumeId);

‎engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeObject.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void setUuid(String uuid) {
109109

110110
@Override
111111
public String get_iScsiName() {
112-
return volumeVO.get_iScsiName();
112+
return volumeVO.get_iScsiName();
113113
}
114114

115115
public void setSize(Long size) {
@@ -254,9 +254,8 @@ public void processEvent(ObjectInDataStoreStateMachine.Event event) {
254254
}
255255
if (this.dataStore.getRole() == DataStoreRole.Image) {
256256
objectInStoreMgr.update(this, event);
257-
if (this.volumeVO.getState() == Volume.State.Migrating
258-
|| this.volumeVO.getState() == Volume.State.Copying
259-
|| this.volumeVO.getState() == Volume.State.Uploaded) {
257+
if (this.volumeVO.getState() == Volume.State.Migrating || this.volumeVO.getState() == Volume.State.Copying || this.volumeVO.getState() == Volume.State.Uploaded
258+
|| this.volumeVO.getState() == Volume.State.Expunged) {
260259
return;
261260
}
262261
if (event == ObjectInDataStoreStateMachine.Event.CreateOnlyRequested) {
@@ -513,6 +512,7 @@ public void processEvent(ObjectInDataStoreStateMachine.Event event, Answer answe
513512

514513
}
515514

515+
@Override
516516
public void incRefCount() {
517517
if (this.dataStore == null) {
518518
return;

‎engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.cloud.storage.VMTemplateStoragePoolVO;
7676
import com.cloud.storage.VMTemplateStorageResourceAssoc;
7777
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
78+
import com.cloud.storage.Volume.State;
7879
import com.cloud.storage.Volume;
7980
import com.cloud.storage.VolumeVO;
8081
import com.cloud.storage.dao.VMTemplatePoolDao;
@@ -211,13 +212,34 @@ public AsyncCallFuture<VolumeApiResult> getFuture() {
211212
}
212213
}
213214

215+
// check if a volume is expunged on both primary and secondary
216+
private boolean canVolumeBeRemoved(long volumeId) {
217+
VolumeVO vol = volDao.findById(volumeId);
218+
if (vol == null) {
219+
// already removed from volumes table
220+
return false;
221+
}
222+
VolumeDataStoreVO volumeStore = _volumeStoreDao.findByVolume(volumeId);
223+
if (vol.getState() == State.Expunged && volumeStore == null) {
224+
// volume is expunged from primary, as well as on secondary
225+
return true;
226+
} else {
227+
return false;
228+
}
229+
230+
}
231+
214232
@DB
215233
@Override
216234
public AsyncCallFuture<VolumeApiResult> expungeVolumeAsync(VolumeInfo volume) {
217235
AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>();
218236
VolumeApiResult result = new VolumeApiResult(volume);
219237
if (volume.getDataStore() == null) {
220-
volDao.remove(volume.getId());
238+
s_logger.info("Expunge volume with no data store specified");
239+
if (canVolumeBeRemoved(volume.getId())) {
240+
s_logger.info("Volume " + volume.getId() + " is not referred anywhere, remove it from volumes table");
241+
volDao.remove(volume.getId());
242+
}
221243
future.complete(result);
222244
return future;
223245
}
@@ -246,7 +268,11 @@ public AsyncCallFuture<VolumeApiResult> expungeVolumeAsync(VolumeInfo volume) {
246268
}
247269
VolumeObject vo = (VolumeObject) volume;
248270

249-
volume.processEvent(Event.ExpungeRequested);
271+
if (volume.getDataStore().getRole() == DataStoreRole.Image) {
272+
volume.processEvent(Event.DestroyRequested);
273+
} else if (volume.getDataStore().getRole() == DataStoreRole.Primary) {
274+
volume.processEvent(Event.ExpungeRequested);
275+
}
250276

251277
DeleteVolumeContext<VolumeApiResult> context = new DeleteVolumeContext<VolumeApiResult>(null, vo, future);
252278
AsyncCallbackDispatcher<VolumeServiceImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this);
@@ -256,14 +282,18 @@ public AsyncCallFuture<VolumeApiResult> expungeVolumeAsync(VolumeInfo volume) {
256282
return future;
257283
}
258284

285+
259286
public Void deleteVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CommandResult> callback,
260287
DeleteVolumeContext<VolumeApiResult> context) {
261288
CommandResult result = callback.getResult();
262289
VolumeObject vo = context.getVolume();
263290
VolumeApiResult apiResult = new VolumeApiResult(vo);
264291
if (result.isSuccess()) {
265292
vo.processEvent(Event.OperationSuccessed);
266-
volDao.remove(vo.getId());
293+
if (canVolumeBeRemoved(vo.getId())) {
294+
s_logger.info("Volume " + vo.getId() + " is not referred anywhere, remove it from volumes table");
295+
volDao.remove(vo.getId());
296+
}
267297
} else {
268298
vo.processEvent(Event.OperationFailed);
269299
apiResult.setResult(result.getResult());

‎server/src/com/cloud/storage/VolumeManagerImpl.java‎

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public VolumeManagerImpl() {
349349
public VolumeInfo moveVolume(VolumeInfo volume, long destPoolDcId,
350350
Long destPoolPodId, Long destPoolClusterId,
351351
HypervisorType dataDiskHyperType)
352-
throws ConcurrentOperationException {
352+
throws ConcurrentOperationException {
353353

354354
// Find a destination storage pool with the specified criteria
355355
DiskOfferingVO diskOffering = _diskOfferingDao.findById(volume
@@ -411,7 +411,7 @@ public VolumeVO uploadVolume(UploadVolumeCmd cmd)
411411

412412
private boolean validateVolume(Account caller, long ownerId, Long zoneId,
413413
String volumeName, String url, String format)
414-
throws ResourceAllocationException {
414+
throws ResourceAllocationException {
415415

416416
// permission check
417417
_accountMgr.checkAccess(caller, null, true,
@@ -480,17 +480,17 @@ private boolean validateVolume(Account caller, long ownerId, Long zoneId,
480480
&& !url.toLowerCase().endsWith("qcow2.zip")
481481
&& !url.toLowerCase().endsWith("qcow2.bz2") && !url
482482
.toLowerCase().endsWith("qcow2.gz")))
483-
|| (format.equalsIgnoreCase("ova") && (!url.toLowerCase()
484-
.endsWith(".ova")
485-
&& !url.toLowerCase().endsWith("ova.zip")
486-
&& !url.toLowerCase().endsWith("ova.bz2") && !url
487-
.toLowerCase().endsWith("ova.gz")))
488-
|| (format.equalsIgnoreCase("raw") && (!url.toLowerCase()
489-
.endsWith(".img") && !url.toLowerCase().endsWith("raw")))) {
483+
|| (format.equalsIgnoreCase("ova") && (!url.toLowerCase()
484+
.endsWith(".ova")
485+
&& !url.toLowerCase().endsWith("ova.zip")
486+
&& !url.toLowerCase().endsWith("ova.bz2") && !url
487+
.toLowerCase().endsWith("ova.gz")))
488+
|| (format.equalsIgnoreCase("raw") && (!url.toLowerCase()
489+
.endsWith(".img") && !url.toLowerCase().endsWith("raw")))) {
490490
throw new InvalidParameterValueException(
491491
"Please specify a valid URL. URL:" + url
492-
+ " is an invalid for the format "
493-
+ format.toLowerCase());
492+
+ " is an invalid for the format "
493+
+ format.toLowerCase());
494494
}
495495
UriUtils.validateUrl(url);
496496

@@ -908,26 +908,26 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd)
908908

909909
if (isCustomizedIops != null) {
910910
if (isCustomizedIops) {
911-
minIops = cmd.getMinIops();
912-
maxIops = cmd.getMaxIops();
913-
914-
if (minIops == null && maxIops == null) {
915-
minIops = 0L;
916-
maxIops = 0L;
917-
}
918-
else {
911+
minIops = cmd.getMinIops();
912+
maxIops = cmd.getMaxIops();
913+
914+
if (minIops == null && maxIops == null) {
915+
minIops = 0L;
916+
maxIops = 0L;
917+
}
918+
else {
919919
if (minIops == null || minIops <= 0) {
920920
throw new InvalidParameterValueException("The min IOPS must be greater than 0.");
921921
}
922922

923-
if (maxIops == null) {
924-
maxIops = 0L;
925-
}
923+
if (maxIops == null) {
924+
maxIops = 0L;
925+
}
926926

927-
if (minIops > maxIops) {
928-
throw new InvalidParameterValueException("The min IOPS must be less than or equal to the max IOPS.");
929-
}
930-
}
927+
if (minIops > maxIops) {
928+
throw new InvalidParameterValueException("The min IOPS must be less than or equal to the max IOPS.");
929+
}
930+
}
931931
}
932932
else {
933933
minIops = diskOffering.getMinIops();
@@ -936,10 +936,10 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd)
936936
}
937937

938938
if (!validateVolumeSizeRange(size)) {// convert size from mb to gb
939-
// for validation
939+
// for validation
940940
throw new InvalidParameterValueException(
941941
"Invalid size for custom volume creation: " + size
942-
+ " ,max volume size is:" + _maxVolumeSizeInGb);
942+
+ " ,max volume size is:" + _maxVolumeSizeInGb);
943943
}
944944
} else { // create volume from snapshot
945945
Long snapshotId = cmd.getSnapshotId();
@@ -960,7 +960,7 @@ public VolumeVO allocVolume(CreateVolumeCmd cmd)
960960
diskOffering = _diskOfferingDao.findById(diskOfferingId);
961961
zoneId = snapshotCheck.getDataCenterId();
962962
size = snapshotCheck.getSize(); // ; disk offering is used for tags
963-
// purposes
963+
// purposes
964964

965965
// check snapshot permissions
966966
_accountMgr.checkAccess(caller, null, true, snapshotCheck);
@@ -1314,7 +1314,7 @@ public boolean deleteVolume(long volumeId, Account caller)
13141314

13151315
_accountMgr.checkAccess(caller, null, true, volume);
13161316

1317-
if (volume.getInstanceId() != null) {
1317+
if (volume.getInstanceId() != null) {
13181318
throw new InvalidParameterValueException(
13191319
"Please specify a volume that is not attached to any VM.");
13201320
}
@@ -1358,9 +1358,20 @@ storage count (in case of upload volume). */
13581358
_usageEventDao.persist(usageEvent);
13591359
}
13601360
}
1361-
AsyncCallFuture<VolumeApiResult> future = volService.expungeVolumeAsync(volFactory.getVolume(volume.getId()));
1362-
future.get();
1363-
1361+
// expunge volume from primary if volume is on primary
1362+
VolumeInfo volOnPrimary = volFactory.getVolume(volume.getId(), DataStoreRole.Primary);
1363+
if (volOnPrimary != null) {
1364+
s_logger.info("Expunging volume " + volume.getId() + " from primary data store");
1365+
AsyncCallFuture<VolumeApiResult> future = volService.expungeVolumeAsync(volOnPrimary);
1366+
future.get();
1367+
}
1368+
// expunge volume from secondary if volume is on image store
1369+
VolumeInfo volOnSecondary = volFactory.getVolume(volume.getId(), DataStoreRole.Image);
1370+
if (volOnSecondary != null) {
1371+
s_logger.info("Expunging volume " + volume.getId() + " from secondary data store");
1372+
AsyncCallFuture<VolumeApiResult> future2 = volService.expungeVolumeAsync(volOnSecondary);
1373+
future2.get();
1374+
}
13641375
} catch (Exception e) {
13651376
s_logger.warn("Failed to expunge volume:", e);
13661377
return false;
@@ -1647,7 +1658,7 @@ private VolumeVO sendAttachVolumeCommand(UserVmVO vm, VolumeVO volumeToAttach, L
16471658
volumeToAttach = _volsDao.findById(volumeToAttach.getId());
16481659

16491660
if (volumeToAttachStoragePool.isManaged() &&
1650-
volumeToAttach.getPath() == null) {
1661+
volumeToAttach.getPath() == null) {
16511662
volumeToAttach.setPath(answer.getDisk().getVdiUuid());
16521663

16531664
_volsDao.update(volumeToAttach.getId(), volumeToAttach);
@@ -1659,8 +1670,8 @@ private VolumeVO sendAttachVolumeCommand(UserVmVO vm, VolumeVO volumeToAttach, L
16591670
// insert record for disk I/O statistics
16601671
VmDiskStatisticsVO diskstats = _vmDiskStatsDao.findBy(vm.getAccountId(), vm.getDataCenterId(),vm.getId(), volumeToAttach.getId());
16611672
if (diskstats == null) {
1662-
diskstats = new VmDiskStatisticsVO(vm.getAccountId(), vm.getDataCenterId(),vm.getId(), volumeToAttach.getId());
1663-
_vmDiskStatsDao.persist(diskstats);
1673+
diskstats = new VmDiskStatisticsVO(vm.getAccountId(), vm.getDataCenterId(),vm.getId(), volumeToAttach.getId());
1674+
_vmDiskStatsDao.persist(diskstats);
16641675
}
16651676

16661677
return _volsDao.findById(volumeToAttach.getId());
@@ -2033,7 +2044,7 @@ public Volume detachVolumeFromVM(DetachVolumeCmd cmmd) {
20332044
@DB
20342045
protected VolumeVO switchVolume(VolumeVO existingVolume,
20352046
VirtualMachineProfile<? extends VirtualMachine> vm)
2036-
throws StorageUnavailableException {
2047+
throws StorageUnavailableException {
20372048
Transaction txn = Transaction.currentTxn();
20382049

20392050
Long templateIdToUse = null;
@@ -2340,14 +2351,14 @@ private static enum VolumeTaskType {
23402351
MIGRATE
23412352
}
23422353
private static class VolumeTask {
2343-
final VolumeTaskType type;
2344-
final StoragePoolVO pool;
2345-
final VolumeVO volume;
2346-
VolumeTask(VolumeTaskType type, VolumeVO volume, StoragePoolVO pool) {
2347-
this.type = type;
2348-
this.pool = pool;
2349-
this.volume = volume;
2350-
}
2354+
final VolumeTaskType type;
2355+
final StoragePoolVO pool;
2356+
final VolumeVO volume;
2357+
VolumeTask(VolumeTaskType type, VolumeVO volume, StoragePoolVO pool) {
2358+
this.type = type;
2359+
this.pool = pool;
2360+
this.volume = volume;
2361+
}
23512362
}
23522363

23532364
private List<VolumeTask> getTasks(List<VolumeVO> vols, Map<Volume, StoragePool> destVols) throws StorageUnavailableException {
@@ -2448,7 +2459,7 @@ private Pair<VolumeVO, DataStore> recreateVolume(VolumeVO vol, VirtualMachinePro
24482459
DataStore destPool = null;
24492460
if (recreate
24502461
&& (dest.getStorageForDisks() == null || dest
2451-
.getStorageForDisks().get(vol) == null)) {
2462+
.getStorageForDisks().get(vol) == null)) {
24522463
destPool = dataStoreMgr.getDataStore(vol.getPoolId(), DataStoreRole.Primary);
24532464
s_logger.debug("existing pool: " + destPool.getId());
24542465
} else {
@@ -2605,8 +2616,8 @@ public boolean configure(String name, Map<String, Object> params)
26052616
.getValue(Config.CustomDiskOfferingMinSize.toString());
26062617
_customDiskOfferingMinSize = NumbersUtil.parseInt(
26072618
_customDiskOfferingMinSizeStr, Integer
2608-
.parseInt(Config.CustomDiskOfferingMinSize
2609-
.getDefaultValue()));
2619+
.parseInt(Config.CustomDiskOfferingMinSize
2620+
.getDefaultValue()));
26102621

26112622
String maxVolumeSizeInGbString = _configDao
26122623
.getValue("storage.max.volume.size");

0 commit comments

Comments
 (0)