This commit is contained in:
Andrija Panic 2026-07-04 15:36:55 +01:00 committed by GitHub
commit 7536ff3a59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 3 deletions

View File

@ -44,6 +44,8 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
List<VolumeVO> findByInstance(long id);
List<VolumeVO> findByInstanceAndNotDestroyed(long id);
List<VolumeVO> findByInstanceAndType(long id, Volume.Type vType);
List<VolumeVO> findByInstanceAndNotStates(long id, Volume.State...states);

View File

@ -132,6 +132,14 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
return listBy(sc);
}
@Override
public List<VolumeVO> findByInstanceAndNotDestroyed(long id) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
sc.setParameters("instanceId", id);
sc.setParameters("notDestroyed", Volume.State.Destroy, Volume.State.Expunged);
return listBy(sc);
}
@Override
public List<VolumeVO> findByInstanceAndDeviceId(long instanceId, long deviceId) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();

View File

@ -7347,7 +7347,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
}
public boolean isVMUsingLocalStorage(VMInstanceVO vm) {
List<VolumeVO> volumes = _volsDao.findByInstance(vm.getId());
List<VolumeVO> volumes = _volsDao.findByInstanceAndNotDestroyed(vm.getId());
return isAnyVmVolumeUsingLocalStorage(volumes);
}
@ -7783,10 +7783,20 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
protected boolean isAnyVmVolumeUsingLocalStorage(final List<VolumeVO> volumes) {
for (VolumeVO vol : volumes) {
DiskOfferingVO diskOffering = _diskOfferingDao.findById(vol.getDiskOfferingId());
if (diskOffering.isUseLocalStorage()) {
if (diskOffering != null && diskOffering.isUseLocalStorage()) {
return true;
}
StoragePoolVO storagePool = _storagePoolDao.findById(vol.getPoolId());
Long poolId = vol.getPoolId();
if (poolId == null) {
logger.debug("Skipping volume without storage pool while checking local storage usage: {}", vol);
continue;
}
StoragePoolVO storagePool = _storagePoolDao.findById(poolId);
if (storagePool == null || storagePool.getRemoved() != null) {
throw new CloudRuntimeException(String.format(
"Cannot determine local storage usage for active volume %s because storage pool ID %s is missing or removed",
vol, poolId));
}
if (storagePool.isLocal()) {
return true;
}

View File

@ -1252,6 +1252,38 @@ public class UserVmManagerImplTest {
}
}
@Test
public void testIsAnyVmVolumeUsingLocalStorageFailsForActiveVolumeWithMissingPool() {
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getDiskOfferingId()).thenReturn(1L);
Mockito.when(volume.getPoolId()).thenReturn(2L);
DiskOfferingVO diskOffering = Mockito.mock(DiskOfferingVO.class);
Mockito.when(diskOfferingDao.findById(1L)).thenReturn(diskOffering);
Mockito.when(diskOffering.isUseLocalStorage()).thenReturn(false);
Mockito.when(primaryDataStoreDao.findById(2L)).thenReturn(null);
CloudRuntimeException exception = assertThrows(CloudRuntimeException.class, () ->
userVmManagerImpl.isAnyVmVolumeUsingLocalStorage(Collections.singletonList(volume)));
Assert.assertTrue(exception.getMessage().contains("storage pool ID 2 is missing or removed"));
}
@Test
public void testIsAnyVmVolumeUsingLocalStorageFailsForActiveVolumeWithRemovedPool() {
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getDiskOfferingId()).thenReturn(1L);
Mockito.when(volume.getPoolId()).thenReturn(2L);
DiskOfferingVO diskOffering = Mockito.mock(DiskOfferingVO.class);
Mockito.when(diskOfferingDao.findById(1L)).thenReturn(diskOffering);
Mockito.when(diskOffering.isUseLocalStorage()).thenReturn(false);
StoragePoolVO storagePool = Mockito.mock(StoragePoolVO.class);
Mockito.when(storagePool.getRemoved()).thenReturn(new Date());
Mockito.when(primaryDataStoreDao.findById(2L)).thenReturn(storagePool);
CloudRuntimeException exception = assertThrows(CloudRuntimeException.class, () ->
userVmManagerImpl.isAnyVmVolumeUsingLocalStorage(Collections.singletonList(volume)));
Assert.assertTrue(exception.getMessage().contains("storage pool ID 2 is missing or removed"));
}
private List<VolumeVO> mockVolumesForIsAllVmVolumesOnZoneWideStore(int nullPoolIdVolumes, int nullPoolVolumes, int zoneVolumes, int nonZoneVolumes) {
List<VolumeVO> volumes = new ArrayList<>();
for (int i=0; i< nullPoolIdVolumes + nullPoolVolumes + zoneVolumes + nonZoneVolumes; ++i) {