Improve logs in primary storage removal process (#8649)

* Improve delete storage pool logs

* Address Daniel's reviews

Co-authored-by: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com>

* Address Daniel's review

---------

Co-authored-by: Henrique Sato <henrique.sato@scclouds.com.br>
Co-authored-by: Daniel Augusto Veronezi Salvador <38945620+GutoVeronezi@users.noreply.github.com>
This commit is contained in:
Henrique Sato 2024-08-28 09:06:50 -03:00 committed by GitHub
parent e6cb7f26ac
commit 2a1db67eeb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 6 deletions

View File

@ -1411,7 +1411,8 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
} }
}); });
} else { } else {
throw new CloudRuntimeException("Cannot delete pool " + sPool.getName() + " as there are associated " + "non-destroyed vols for this pool"); logger.debug("Cannot delete storage pool {} as the following non-destroyed volumes are on it: {}.", sPool::getName, () -> getStoragePoolNonDestroyedVolumesLog(sPool.getId()));
throw new CloudRuntimeException(String.format("Cannot delete pool %s as there are non-destroyed volumes associated to this pool.", sPool.getName()));
} }
} }
return deleteDataStoreInternal(sPool, forced); return deleteDataStoreInternal(sPool, forced);
@ -1472,7 +1473,8 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
if (vlms.first() > 0) { if (vlms.first() > 0) {
Pair<Long, Long> nonDstrdVlms = volumeDao.getNonDestroyedCountAndTotalByPool(sPool.getId()); Pair<Long, Long> nonDstrdVlms = volumeDao.getNonDestroyedCountAndTotalByPool(sPool.getId());
if (nonDstrdVlms.first() > 0) { if (nonDstrdVlms.first() > 0) {
throw new CloudRuntimeException("Cannot delete pool " + sPool.getName() + " as there are associated " + "non-destroyed vols for this pool"); logger.debug("Cannot delete storage pool {} as the following non-destroyed volumes are on it: {}.", sPool::getName, () -> getStoragePoolNonDestroyedVolumesLog(sPool.getId()));
throw new CloudRuntimeException(String.format("Cannot delete pool %s as there are non-destroyed volumes associated to this pool.", sPool.getName()));
} }
// force expunge non-destroyed volumes // force expunge non-destroyed volumes
List<VolumeVO> vols = volumeDao.listVolumesToBeDestroyed(); List<VolumeVO> vols = volumeDao.listVolumesToBeDestroyed();
@ -1480,9 +1482,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
AsyncCallFuture<VolumeApiResult> future = volService.expungeVolumeAsync(volFactory.getVolume(vol.getId())); AsyncCallFuture<VolumeApiResult> future = volService.expungeVolumeAsync(volFactory.getVolume(vol.getId()));
try { try {
future.get(); future.get();
} catch (InterruptedException e) { } catch (InterruptedException | ExecutionException e) {
logger.debug("expunge volume failed:" + vol.getId(), e);
} catch (ExecutionException e) {
logger.debug("expunge volume failed:" + vol.getId(), e); logger.debug("expunge volume failed:" + vol.getId(), e);
} }
} }
@ -1491,7 +1491,8 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
// Check if the pool has associated volumes in the volumes table // Check if the pool has associated volumes in the volumes table
// If it does , then you cannot delete the pool // If it does , then you cannot delete the pool
if (vlms.first() > 0) { if (vlms.first() > 0) {
throw new CloudRuntimeException("Cannot delete pool " + sPool.getName() + " as there are associated volumes for this pool"); logger.debug("Cannot delete storage pool {} as the following non-destroyed volumes are on it: {}.", sPool::getName, () -> getStoragePoolNonDestroyedVolumesLog(sPool.getId()));
throw new CloudRuntimeException(String.format("Cannot delete pool %s as there are non-destroyed volumes associated to this pool.", sPool.getName()));
} }
} }
@ -1514,6 +1515,23 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
return lifeCycle.deleteDataStore(store); return lifeCycle.deleteDataStore(store);
} }
protected String getStoragePoolNonDestroyedVolumesLog(long storagePoolId) {
StringBuilder sb = new StringBuilder();
List<VolumeVO> nonDestroyedVols = volumeDao.findByPoolId(storagePoolId, null).stream().filter(vol -> vol.getState() != Volume.State.Destroy).collect(Collectors.toList());
VMInstanceVO volInstance;
List<String> logMessageInfo = new ArrayList<>();
sb.append("[");
for (VolumeVO vol : nonDestroyedVols) {
volInstance = _vmInstanceDao.findById(vol.getInstanceId());
logMessageInfo.add(String.format("Volume [%s] (attached to VM [%s])", vol.getUuid(), volInstance.getUuid()));
}
sb.append(String.join(", ", logMessageInfo));
sb.append("]");
return sb.toString();
}
@Override @Override
public boolean connectHostToSharedPool(long hostId, long poolId) throws StorageUnavailableException, StorageConflictException { public boolean connectHostToSharedPool(long hostId, long poolId) throws StorageUnavailableException, StorageConflictException {
StoragePool pool = (StoragePool)_dataStoreMgr.getDataStore(poolId, DataStoreRole.Primary); StoragePool pool = (StoragePool)_dataStoreMgr.getDataStore(poolId, DataStoreRole.Primary);

View File

@ -110,6 +110,18 @@ public class StorageManagerImplTest {
@InjectMocks @InjectMocks
private StorageManagerImpl storageManagerImpl; private StorageManagerImpl storageManagerImpl;
@Mock
private StoragePoolVO storagePoolVOMock;
@Mock
private VolumeVO volume1VOMock;
@Mock
private VolumeVO volume2VOMock;
@Mock
private VMInstanceVO vmInstanceVOMock;
@Test @Test
public void createLocalStoragePoolName() { public void createLocalStoragePoolName() {
String hostMockName = "host1"; String hostMockName = "host1";
@ -515,6 +527,24 @@ public class StorageManagerImplTest {
.update(StorageManager.DataStoreDownloadFollowRedirects.key(),StorageManager.DataStoreDownloadFollowRedirects.defaultValue()); .update(StorageManager.DataStoreDownloadFollowRedirects.key(),StorageManager.DataStoreDownloadFollowRedirects.defaultValue());
} }
@Test
public void getStoragePoolNonDestroyedVolumesLogTestNonDestroyedVolumesReturnLog() {
Mockito.doReturn(1L).when(storagePoolVOMock).getId();
Mockito.doReturn(1L).when(volume1VOMock).getInstanceId();
Mockito.doReturn("786633d1-a942-4374-9d56-322dd4b0d202").when(volume1VOMock).getUuid();
Mockito.doReturn(1L).when(volume2VOMock).getInstanceId();
Mockito.doReturn("ffb46333-e983-4c21-b5f0-51c5877a3805").when(volume2VOMock).getUuid();
Mockito.doReturn("58760044-928f-4c4e-9fef-d0e48423595e").when(vmInstanceVOMock).getUuid();
Mockito.when(_volumeDao.findByPoolId(storagePoolVOMock.getId(), null)).thenReturn(List.of(volume1VOMock, volume2VOMock));
Mockito.doReturn(vmInstanceVOMock).when(vmInstanceDao).findById(Mockito.anyLong());
String log = storageManagerImpl.getStoragePoolNonDestroyedVolumesLog(storagePoolVOMock.getId());
String expected = String.format("[Volume [%s] (attached to VM [%s]), Volume [%s] (attached to VM [%s])]", volume1VOMock.getUuid(), vmInstanceVOMock.getUuid(), volume2VOMock.getUuid(), vmInstanceVOMock.getUuid());
Assert.assertEquals(expected, log);
}
private ChangeStoragePoolScopeCmd mockChangeStoragePooolScopeCmd(String newScope) { private ChangeStoragePoolScopeCmd mockChangeStoragePooolScopeCmd(String newScope) {
ChangeStoragePoolScopeCmd cmd = new ChangeStoragePoolScopeCmd(); ChangeStoragePoolScopeCmd cmd = new ChangeStoragePoolScopeCmd();
ReflectionTestUtils.setField(cmd, "id", 1L); ReflectionTestUtils.setField(cmd, "id", 1L);