From d121908e6d4de4ec01cb8753c99d00966685bf22 Mon Sep 17 00:00:00 2001 From: Brad House Date: Sun, 22 Nov 2026 20:12:28 +0000 Subject: [PATCH] Run the change-offering migrate and resize only once changeDiskOfferingForVolumeInternal on main carried its tail block twice: the no-op check, the migrate step and the resize step. The 4.22 merge 82bfa9fb3f kept both main's copy and 4.22's copy when it wrapped the method in the reservation try/finally from 9c0c8da706. - An offering change that needed a migration migrated the volume twice, the second time to a pool picked from the pool list computed before the first move. - An offering change that needed a resize resized twice. - Kept the first copy: it carries main's newer error messages from bc76f2042d (#12367). The removed copy is 4.22's older text. - Added a test that a change needing migration and resize calls migrateVolume and resizeVolumeInternal once each. Signed-off-by: Brad House --- .../cloud/storage/VolumeApiServiceImpl.java | 44 ------------------- .../storage/VolumeApiServiceImplTest.java | 34 ++++++++++++++ 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index 11b24a9beef6..1abacfc951fe 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -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 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 { diff --git a/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java b/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java index 401b12ebbf46..e1da5fc3a214 100644 --- a/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java +++ b/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java @@ -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; @@ -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> 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 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()); + } }