Launch RESIZE event on volume snapshot revert (#10482)

Co-authored-by: Julien Hervot de Mattos Vaz <julien.vaz@scclouds.com.br>
Co-authored-by: dahn <daan.hoogland@gmail.com>
This commit is contained in:
julien-vaz 2025-04-15 04:49:53 -03:00 committed by GitHub
parent 4d572fa6d3
commit b2b2218a8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 9 deletions

View File

@ -390,15 +390,27 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
boolean result = snapshotStrategy.revertSnapshot(snapshotInfo); boolean result = snapshotStrategy.revertSnapshot(snapshotInfo);
if (result) { if (result) {
// update volume size and primary storage count updateVolumeSizeAndPrimaryStorageCount(volume, snapshot);
_resourceLimitMgr.decrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, new Long(volume.getSize() - snapshot.getSize()));
volume.setSize(snapshot.getSize());
_volsDao.update(volume.getId(), volume);
return snapshotInfo; return snapshotInfo;
} }
return null; return null;
} }
public void updateVolumeSizeAndPrimaryStorageCount(VolumeVO volume, SnapshotVO snapshot) {
Long differenceBetweenVolumeAndSnapshotSize = new Long(volume.getSize() - snapshot.getSize());
if (differenceBetweenVolumeAndSnapshotSize != 0) {
if (differenceBetweenVolumeAndSnapshotSize > 0) {
_resourceLimitMgr.decrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, differenceBetweenVolumeAndSnapshotSize);
} else if (differenceBetweenVolumeAndSnapshotSize < 0) {
_resourceLimitMgr.incrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, differenceBetweenVolumeAndSnapshotSize * -1L);
}
volume.setSize(snapshot.getSize());
_volsDao.update(volume.getId(), volume);
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_VOLUME_RESIZE, volume.getAccountId(), volume.getDataCenterId(), volume.getId(), volume.getName(),
volume.getDiskOfferingId(), volume.getTemplateId(), volume.getSize(), Volume.class.getName(), volume.getUuid());
}
}
@Override @Override
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_POLICY_UPDATE, eventDescription = "updating snapshot policy", async = true) @ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_POLICY_UPDATE, eventDescription = "updating snapshot policy", async = true)
public SnapshotPolicy updateSnapshotPolicy(UpdateSnapshotPolicyCmd cmd) { public SnapshotPolicy updateSnapshotPolicy(UpdateSnapshotPolicyCmd cmd) {
@ -805,7 +817,7 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
return result; return result;
} catch (Exception e) { } catch (Exception e) {
logger.debug("Failed to delete snapshot {}:{}", snapshotCheck, e.toString()); logger.debug("Failed to delete snapshot {}:{}", snapshotCheck.getId(), e.toString());
throw new CloudRuntimeException("Failed to delete snapshot:" + e.toString()); throw new CloudRuntimeException("Failed to delete snapshot:" + e.toString());
} }

View File

@ -235,7 +235,6 @@ public class SnapshotManagerTest {
doNothing().when(_resourceLimitMgr).checkResourceLimit(any(Account.class), any(ResourceType.class)); doNothing().when(_resourceLimitMgr).checkResourceLimit(any(Account.class), any(ResourceType.class));
doNothing().when(_resourceLimitMgr).checkResourceLimit(any(Account.class), any(ResourceType.class), anyLong()); doNothing().when(_resourceLimitMgr).checkResourceLimit(any(Account.class), any(ResourceType.class), anyLong());
doNothing().when(_resourceLimitMgr).decrementResourceCount(anyLong(), any(ResourceType.class), anyLong());
doNothing().when(_resourceLimitMgr).incrementResourceCount(anyLong(), any(ResourceType.class)); doNothing().when(_resourceLimitMgr).incrementResourceCount(anyLong(), any(ResourceType.class));
doNothing().when(_resourceLimitMgr).incrementResourceCount(anyLong(), any(ResourceType.class), anyLong()); doNothing().when(_resourceLimitMgr).incrementResourceCount(anyLong(), any(ResourceType.class), anyLong());
@ -352,7 +351,6 @@ public class SnapshotManagerTest {
when(_vmDao.findById(anyLong())).thenReturn(vmMock); when(_vmDao.findById(anyLong())).thenReturn(vmMock);
when(vmMock.getState()).thenReturn(State.Stopped); when(vmMock.getState()).thenReturn(State.Stopped);
when (snapshotStrategy.revertSnapshot(any(SnapshotInfo.class))).thenReturn(true); when (snapshotStrategy.revertSnapshot(any(SnapshotInfo.class))).thenReturn(true);
when(_volumeDao.update(anyLong(), any(VolumeVO.class))).thenReturn(true);
doReturn(DataStoreRole.Image).when(snapshotHelperMock).getDataStoreRole(any()); doReturn(DataStoreRole.Image).when(snapshotHelperMock).getDataStoreRole(any());
Snapshot snapshot = _snapshotMgr.revertSnapshot(TEST_SNAPSHOT_ID); Snapshot snapshot = _snapshotMgr.revertSnapshot(TEST_SNAPSHOT_ID);
Assert.assertNotNull(snapshot); Assert.assertNotNull(snapshot);

View File

@ -276,8 +276,8 @@ public class UriUtilsTest {
@Test @Test
public void validateUrl() { public void validateUrl() {
Pair<String, Integer> url1 = UriUtils.validateUrl("https://www.cloudstack.org"); Pair<String, Integer> url1 = UriUtils.validateUrl("https://cloudstack.apache.org/");
Assert.assertEquals(url1.first(), "www.cloudstack.org"); Assert.assertEquals(url1.first(), "cloudstack.apache.org");
Pair<String, Integer> url2 = UriUtils.validateUrl("https://www.apache.org"); Pair<String, Integer> url2 = UriUtils.validateUrl("https://www.apache.org");
Assert.assertEquals(url2.first(), "www.apache.org"); Assert.assertEquals(url2.first(), "www.apache.org");