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
44 changes: 0 additions & 44 deletions server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2338,50 +2338,6 @@ public Volume changeDiskOfferingForVolumeInternal(Long volumeId, Long newDiskOff
}
}

if (!volumeMigrateRequired && !volumeResizeRequired) {
_volsDao.updateDiskOffering(volume.getId(), newDiskOffering.getId());
volume = _volsDao.findById(volume.getId());
updateStorageWithTheNewDiskOffering(volume, newDiskOffering);

return volume;
}

if (volumeMigrateRequired) {
if (CollectionUtils.isEmpty(poolsPair.first()) && CollectionUtils.isEmpty(poolsPair.second())) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Volume change offering operation failed for volume: %s as no suitable pool(s) found for migrating to support new disk offering", volume));
}
final Long newSizeFinal = newSize;
List<? extends StoragePool> suitableStoragePoolsWithEnoughSpace = suitableStoragePools.stream().filter(pool -> storageMgr.storagePoolHasEnoughSpaceForResize(pool, 0L, newSizeFinal)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(suitableStoragePoolsWithEnoughSpace)) {
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()));
}
}

if (volumeResizeRequired) {
// refresh volume data
volume = _volsDao.findById(volume.getId());
try {
volume = resizeVolumeInternal(volume, newDiskOffering, currentSize, newSize, newMinIops, newMaxIops, newHypervisorSnapshotReserve, shrinkOk);
} catch (Exception e) {
if (volumeMigrateRequired) {
logger.warn(String.format("Volume change offering operation succeeded for volume ID: %s but volume resize operation failed, so please try resize volume operation separately", volume.getUuid()));
} else {
throw new CloudRuntimeException(String.format("Volume change offering operation failed for volume ID: %s due to resize volume operation failed", volume.getUuid()));
}
}
}

return volume;

} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down Expand Up @@ -3080,4 +3081,37 @@ public void getReservedDeviceIdsWithConfigDriveReservesTheCdromOfConfigDrive() {
Assert.assertEquals(new HashSet<>(Arrays.asList(3L, 4L)), volumeApiServiceImpl.getReservedDeviceIds(vm, false));
Assert.assertEquals(new HashSet<>(Arrays.asList(3L, 4L, 5L)), volumeApiServiceImpl.getReservedDeviceIds(vm, true));
}

@Test
public void changeDiskOfferingForVolumeInternalMigratesAndResizesOnce() throws ResourceAllocationException {
Long newMaxIops = 1000L;
Mockito.when(volumeDaoMock.findById(volumeMockId)).thenReturn(volumeVoMock);
Mockito.when(volumeVoMock.getDiskOfferingId()).thenReturn(diskOfferingMockId);
Mockito.when(volumeVoMock.getState()).thenReturn(Volume.State.Ready);
Mockito.when(_diskOfferingDao.findByIdIncludingRemoved(diskOfferingMockId)).thenReturn(diskOfferingMock);
Mockito.when(_diskOfferingDao.findById(newDiskOfferingMockId)).thenReturn(newDiskOfferingMock);
Mockito.when(newDiskOfferingMock.getId()).thenReturn(newDiskOfferingMockId);
Mockito.when(volumeDataFactoryMock.getVolume(volumeMockId)).thenReturn(volumeInfoMock);
doNothing().when(volumeApiServiceImpl).validateVolumeResizeWithNewDiskOfferingAndLoad(any(), any(), any(), any(), any(), any(), any());
doNothing().when(volumeApiServiceImpl).validateVolumeReadyStateAndHypervisorChecks(any(), anyLong(), any());

StoragePool otherPool = Mockito.mock(StoragePool.class);
Mockito.when(otherPool.getId()).thenReturn(storagePoolMockId + 1);
Pair<List<? extends StoragePool>, List<? extends StoragePool>> pools = new Pair<>(List.of(otherPool), List.of(otherPool));
Mockito.doReturn(pools).when(managementService).listStoragePoolsForSystemMigrationOfVolume(volumeMockId, newDiskOfferingMockId, volumeSizeMock, null, newMaxIops, true, false);
Mockito.when(storageMgr.storagePoolHasEnoughSpaceForResize(otherPool, 0L, volumeSizeMock)).thenReturn(true);
doReturn(volumeVoMock).when(volumeApiServiceImpl).migrateVolume(any(MigrateVolumeCmd.class));
doReturn(volumeVoMock).when(volumeApiServiceImpl).resizeVolumeInternal(any(), any(), any(), any(), any(), any(), any(), anyBoolean());

volumeApiServiceImpl.changeDiskOfferingForVolumeInternal(volumeMockId, newDiskOfferingMockId, volumeSizeMock, null, newMaxIops, true, false);

ArgumentCaptor<MigrateVolumeCmd> migrateCmd = ArgumentCaptor.forClass(MigrateVolumeCmd.class);
InOrder inOrder = Mockito.inOrder(volumeApiServiceImpl);
inOrder.verify(volumeApiServiceImpl, times(1)).migrateVolume(migrateCmd.capture());
inOrder.verify(volumeApiServiceImpl, times(1)).resizeVolumeInternal(any(), any(), any(), any(), any(), any(), any(), anyBoolean());
Mockito.verify(volumeApiServiceImpl, times(1)).migrateVolume(any(MigrateVolumeCmd.class));
Mockito.verify(volumeApiServiceImpl, times(1)).resizeVolumeInternal(any(), any(), any(), any(), any(), any(), any(), anyBoolean());
assertEquals(Long.valueOf(storagePoolMockId + 1), migrateCmd.getValue().getStoragePoolId());
assertEquals(Long.valueOf(newDiskOfferingMockId), migrateCmd.getValue().getNewDiskOfferingId());
}
}
Loading