Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1410,16 +1410,9 @@ public VolumeVO resizeVolume(ResizeVolumeCmd cmd) throws ResourceAllocationExcep
}

if (volumeMigrateRequired) {
MigrateVolumeCmd migrateVolumeCmd = new MigrateVolumeCmd(volume.getId(), suitableStoragePoolsWithEnoughSpace.get(0).getId(), newDiskOfferingId, true);
try {
Volume result = migrateVolume(migrateVolumeCmd);
volume = (result != null) ? _volsDao.findById(result.getId()) : null;
if (volume == null) {
throw new CloudRuntimeException(String.format("Volume resize operation failed for volume ID: %s as migration failed to storage pool %s accommodating new size", volume.getUuid(), suitableStoragePoolsWithEnoughSpace.get(0).getId()));
}
} catch (Exception e) {
throw new CloudRuntimeException(String.format("Volume resize operation failed for volume ID: %s as migration failed to storage pool %s accommodating new size", volume.getUuid(), suitableStoragePoolsWithEnoughSpace.get(0).getId()));
}
StoragePool destPool = suitableStoragePoolsWithEnoughSpace.get(0);
volume = migrateVolumeToPool(volume, destPool, newDiskOfferingId,
String.format("Volume resize operation failed for volume ID: %s as migration failed to storage pool %s accommodating new size", volume.getUuid(), destPool));
}

UserVmVO userVm = _userVmDao.findById(volume.getInstanceId());
Expand Down Expand Up @@ -2251,16 +2244,9 @@ public Volume changeDiskOfferingForVolumeInternal(Long volumeId, Long newDiskOff
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Volume change offering operation failed for volume: %s as no suitable pool(s) with enough space found for volume migration.", volume));
}
Collections.shuffle(suitableStoragePoolsWithEnoughSpace);
MigrateVolumeCmd migrateVolumeCmd = new MigrateVolumeCmd(volume.getId(), suitableStoragePoolsWithEnoughSpace.get(0).getId(), newDiskOffering.getId(), true);
try {
Volume result = migrateVolume(migrateVolumeCmd);
volume = (result != null) ? _volsDao.findById(result.getId()) : null;
if (volume == null) {
throw new CloudRuntimeException(String.format("Volume change offering operation failed for volume: %s migration failed to storage pool %s", volume, suitableStoragePools.get(0)));
}
} catch (Exception e) {
throw new CloudRuntimeException(String.format("Volume change offering operation failed for volume: %s migration failed to storage pool %s due to %s", volume, suitableStoragePools.get(0), e.getMessage()));
}
StoragePool destPool = suitableStoragePoolsWithEnoughSpace.get(0);
volume = migrateVolumeToPool(volume, destPool, newDiskOffering.getId(),
String.format("Volume change offering operation failed for volume: %s migration failed to storage pool %s", volume, destPool));
}

if (volumeResizeRequired) {
Expand All @@ -2284,6 +2270,20 @@ public Volume changeDiskOfferingForVolumeInternal(Long volumeId, Long newDiskOff
}
}

private VolumeVO migrateVolumeToPool(VolumeVO volume, StoragePool destPool, Long newDiskOfferingId, String failureMessage) {
Volume result;
try {
result = migrateVolume(new MigrateVolumeCmd(volume.getId(), destPool.getId(), newDiskOfferingId, true));
} catch (Exception e) {
throw new CloudRuntimeException(String.format("%s due to %s", failureMessage, ObjectUtils.defaultIfNull(e.getMessage(), e.getClass().getSimpleName())), e);
}
VolumeVO migratedVolume = result != null ? _volsDao.findById(result.getId()) : null;
if (migratedVolume == null) {
throw new CloudRuntimeException(String.format("%s: the migration did not return the migrated volume", failureMessage));
}
return migratedVolume;
}

private void updateStorageWithTheNewDiskOffering(VolumeVO volume, DiskOfferingVO newDiskOffering) {
DataStore dataStore = dataStoreMgr.getDataStore(volume.getPoolId(), DataStoreRole.Primary);
DataStoreDriver dataStoreDriver = dataStore != null ? dataStore.getDriver() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -1908,8 +1909,7 @@ public void testResizeVolumeWithEnoughCapacity() throws ResourceAllocationExcept
}
}

@Test
public void testResizeVolumeWithoutEnoughCapacity() throws ResourceAllocationException, ExecutionException, InterruptedException {
private ResizeVolumeCmd setupResizeVolumeNeedingMigration() throws ExecutionException, InterruptedException {
ResizeVolumeCmd cmd = new ResizeVolumeCmd();
ReflectionTestUtils.setField(cmd, "id", volumeMockId);
ReflectionTestUtils.setField(cmd, "newDiskOfferingId", newDiskOfferingMockId);
Expand All @@ -1921,6 +1921,12 @@ public void testResizeVolumeWithoutEnoughCapacity() throws ResourceAllocationExc
StoragePoolVO suitableStoragePool = Mockito.mock(StoragePoolVO.class);
Pair<List<? extends StoragePool>, List<? extends StoragePool>> poolsPair = new Pair<>(Arrays.asList(suitableStoragePool), Arrays.asList(suitableStoragePool));
when(managementService.listStoragePoolsForSystemMigrationOfVolume(anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyBoolean(), anyBoolean())).thenReturn(poolsPair);
return cmd;
}

@Test
public void testResizeVolumeWithoutEnoughCapacity() throws ResourceAllocationException, ExecutionException, InterruptedException {
ResizeVolumeCmd cmd = setupResizeVolumeNeedingMigration();
doReturn(volumeInfoMock).when(volumeApiServiceImpl).migrateVolume(any());
when(volumeInfoMock.getId()).thenReturn(volumeMockId);

Expand All @@ -1932,6 +1938,74 @@ public void testResizeVolumeWithoutEnoughCapacity() throws ResourceAllocationExc
}
}

@Test
public void testResizeVolumeFailsCleanlyWhenMigrationReturnsNoVolume() throws ResourceAllocationException, ExecutionException, InterruptedException {
ResizeVolumeCmd cmd = setupResizeVolumeNeedingMigration();
doReturn(null).when(volumeApiServiceImpl).migrateVolume(any());

try {
volumeApiServiceImpl.resizeVolume(cmd);
fail("Expected CloudRuntimeException when the migration returns no volume");
} catch (CloudRuntimeException e) {
Assert.assertTrue(e.getMessage(), e.getMessage().contains("the migration did not return the migrated volume"));
}
verify(volumeApiServiceImpl).migrateVolume(any());
verify(volumeServiceMock, times(0)).resize(any(VolumeInfo.class));
}

@Test
public void testResizeVolumeReportsMigrationFailureReason() throws ResourceAllocationException, ExecutionException, InterruptedException {
ResizeVolumeCmd cmd = setupResizeVolumeNeedingMigration();
CloudRuntimeException migrationFailure = new CloudRuntimeException("destination pool unreachable");
doThrow(migrationFailure).when(volumeApiServiceImpl).migrateVolume(any());

try {
volumeApiServiceImpl.resizeVolume(cmd);
fail("Expected CloudRuntimeException when the migration fails");
} catch (CloudRuntimeException e) {
Assert.assertTrue(e.getMessage(), e.getMessage().contains("due to destination pool unreachable"));
Assert.assertSame(migrationFailure, e.getCause());
}
verify(volumeApiServiceImpl).migrateVolume(any());
verify(volumeServiceMock, times(0)).resize(any(VolumeInfo.class));
}

@Test
public void testChangeDiskOfferingFailsCleanlyWhenMigrationReturnsNoVolume() throws ResourceAllocationException {
long poolId = 11L;
long newPoolId = 12L;
when(volumeDaoMock.findById(volumeMockId)).thenReturn(volumeVoMock);
when(volumeVoMock.getId()).thenReturn(volumeMockId);
when(volumeVoMock.getDiskOfferingId()).thenReturn(diskOfferingMockId);
when(volumeVoMock.getState()).thenReturn(Volume.State.Ready);
when(volumeVoMock.getSize()).thenReturn(volumeSizeMock);
when(volumeVoMock.getPoolId()).thenReturn(poolId);
when(_diskOfferingDao.findByIdIncludingRemoved(diskOfferingMockId)).thenReturn(diskOfferingMock);
when(_diskOfferingDao.findById(newDiskOfferingMockId)).thenReturn(newDiskOfferingMock);
when(newDiskOfferingMock.getId()).thenReturn(newDiskOfferingMockId);
doNothing().when(volumeApiServiceImpl).validateVolumeResizeWithNewDiskOfferingAndLoad(any(), any(), any(), any(), any(), any(), any());
when(volumeDataFactoryMock.getVolume(volumeMockId)).thenReturn(volumeInfoMock);

StoragePoolVO currentPool = Mockito.mock(StoragePoolVO.class);
when(currentPool.getId()).thenReturn(poolId);
when(primaryDataStoreDaoMock.findById(poolId)).thenReturn(currentPool);
StoragePoolVO suitablePool = Mockito.mock(StoragePoolVO.class);
when(suitablePool.getId()).thenReturn(newPoolId);
Pair<List<? extends StoragePool>, List<? extends StoragePool>> poolsPair = new Pair<>(Arrays.asList(suitablePool), Arrays.asList(suitablePool));
when(managementService.listStoragePoolsForSystemMigrationOfVolume(anyLong(), anyLong(), anyLong(), nullable(Long.class), nullable(Long.class), anyBoolean(), anyBoolean())).thenReturn(poolsPair);
when(storageMgr.storagePoolHasEnoughSpaceForResize(any(), anyLong(), anyLong())).thenReturn(true);
doReturn(null).when(volumeApiServiceImpl).migrateVolume(any());

try {
volumeApiServiceImpl.changeDiskOfferingForVolumeInternal(volumeMockId, newDiskOfferingMockId, volumeSizeMock, null, null, true, false);
fail("Expected CloudRuntimeException when the migration returns no volume");
} catch (CloudRuntimeException e) {
Assert.assertTrue(e.getMessage(), e.getMessage().contains("the migration did not return the migrated volume"));
Assert.assertFalse(e.getMessage(), e.getMessage().contains("volume: null"));
}
verify(volumeApiServiceImpl).migrateVolume(Mockito.argThat(cmd -> cmd.getStoragePoolId() == newPoolId));
}

@Test
public void testResizeVolumeInAllocateState() throws ResourceAllocationException, ExecutionException, InterruptedException {
ResizeVolumeCmd cmd = new ResizeVolumeCmd();
Expand Down
Loading