From 77ee0def8848b4287d955cd855f8e31cc419e88b Mon Sep 17 00:00:00 2001 From: Brad House Date: Sun, 22 Nov 2026 18:32:10 +0000 Subject: [PATCH] Report a failed automigrate on volume resize instead of throwing NPE When resizeVolume needs to move the volume to another pool and migrateVolume returns null, the code set `volume` to null and then called `volume.getUuid()` to build the error, both in the try and in the catch. The caller got a NullPointerException. | path | before | after | |---|---|---| | resizeVolume, migration returns null | NullPointerException | "... the migration did not return the migrated volume" | | resizeVolume, migration throws | generic error, cause dropped | error includes the cause message and keeps it as cause | | changeOfferingForVolume, migration returns null | message said "volume: null" | names the volume | | changeOfferingForVolume, either case | named the first suitable pool, not the one tried | names the pool actually tried | - Move the migrate-and-check into one helper used by both paths. - Add unit tests for the null and exception cases of resizeVolume and the null case of changeOfferingForVolume. Signed-off-by: Brad House --- .../cloud/storage/VolumeApiServiceImpl.java | 40 ++++---- .../storage/VolumeApiServiceImplTest.java | 91 ++++++++++++++++++- 2 files changed, 109 insertions(+), 22 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index 9b2f4f831d3e..2024353c8bb8 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -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()); @@ -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) { @@ -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, StringUtils.defaultIfBlank(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; diff --git a/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java b/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java index b806eb4afe5c..ca00a031d58e 100644 --- a/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java +++ b/server/src/test/java/com/cloud/storage/VolumeApiServiceImplTest.java @@ -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; @@ -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); @@ -1921,6 +1921,12 @@ public void testResizeVolumeWithoutEnoughCapacity() throws ResourceAllocationExc StoragePoolVO suitableStoragePool = Mockito.mock(StoragePoolVO.class); Pair, List> 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); @@ -1932,6 +1938,87 @@ 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 testResizeVolumeNamesTheExceptionWhenMigrationFailureHasNoMessage() throws ResourceAllocationException, ExecutionException, InterruptedException { + ResizeVolumeCmd cmd = setupResizeVolumeNeedingMigration(); + doThrow(new CloudRuntimeException("")).when(volumeApiServiceImpl).migrateVolume(any()); + + try { + volumeApiServiceImpl.resizeVolume(cmd); + fail("Expected CloudRuntimeException when the migration fails"); + } catch (CloudRuntimeException e) { + Assert.assertTrue(e.getMessage(), e.getMessage().endsWith("due to CloudRuntimeException")); + } + } + + @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> 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();