Skip to content

Commit 28548b2

Browse files
committed
Enhance snapshot cleanup process by updating state, managing resource limits, and removing annotations
1 parent 66f1e64 commit 28548b2

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,8 +2321,13 @@ protected void cleanupSnapshotRecordsInPrimaryStorageOnly(VolumeVO volume) {
23212321
}
23222322
List<SnapshotDataStoreVO> remaining = _snapshotStoreDao.findBySnapshotIdAndNotInDestroyedHiddenState(snapshot.getId());
23232323
if (CollectionUtils.isEmpty(remaining)) {
2324+
Snapshot.State previousState = snapshot.getState();
23242325
snapshot.setState(Snapshot.State.Destroyed);
23252326
_snapshotDao.update(snapshot.getId(), snapshot);
2327+
annotationDao.removeByEntityType(AnnotationService.EntityType.SNAPSHOT.name(), snapshot.getUuid());
2328+
if (previousState != Snapshot.State.Error && previousState != Snapshot.State.Destroyed) {
2329+
_resourceLimitMgr.decrementResourceCount(snapshot.getAccountId(), ResourceType.snapshot);
2330+
}
23262331
} else {
23272332
logger.warn("Storage driver did not remove all primary store refs for snapshot {}; leaving parent record for the next scavenger pass to retry", snapshot);
23282333
}

server/src/test/java/com/cloud/storage/StorageManagerImplTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ public class StorageManagerImplTest {
141141
org.apache.cloudstack.engine.subsystem.api.storage.SnapshotDataFactory snapshotFactory;
142142
@Mock
143143
org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService _snapshotService;
144+
@Mock
145+
com.cloud.user.ResourceLimitService _resourceLimitMgr;
146+
@Mock
147+
org.apache.cloudstack.annotation.dao.AnnotationDao annotationDao;
144148

145149
@Mock
146150
ClusterDao clusterDao;
@@ -1736,6 +1740,8 @@ public void testCleanupSnapshotRecordsInPrimaryStorageOnly() {
17361740

17371741
SnapshotVO snapshot = Mockito.mock(SnapshotVO.class);
17381742
Mockito.when(snapshot.getId()).thenReturn(10L);
1743+
Mockito.when(snapshot.getAccountId()).thenReturn(7L);
1744+
Mockito.when(snapshot.getUuid()).thenReturn("snap-uuid");
17391745
Mockito.when(snapshot.getState()).thenReturn(Snapshot.State.BackedUp);
17401746
Mockito.when(snapshotDao.listByVolumeId(1L)).thenReturn(List.of(snapshot));
17411747

@@ -1760,6 +1766,9 @@ public void testCleanupSnapshotRecordsInPrimaryStorageOnly() {
17601766
Mockito.verify(_snapshotService).deleteSnapshot(snapshotInfo);
17611767
Mockito.verify(snapshot).setState(Snapshot.State.Destroyed);
17621768
Mockito.verify(snapshotDao).update(10L, snapshot);
1769+
Mockito.verify(_resourceLimitMgr).decrementResourceCount(7L, com.cloud.configuration.Resource.ResourceType.snapshot);
1770+
Mockito.verify(annotationDao).removeByEntityType(
1771+
org.apache.cloudstack.annotation.AnnotationService.EntityType.SNAPSHOT.name(), "snap-uuid");
17631772
}
17641773

17651774
@Test
@@ -1793,6 +1802,8 @@ public void testCleanupSnapshotRecordsInPrimaryStorageOnlyLeavesParentWhenStorag
17931802
Mockito.verify(_snapshotService).deleteSnapshot(snapshotInfo);
17941803
Mockito.verify(snapshot, Mockito.never()).setState(Snapshot.State.Destroyed);
17951804
Mockito.verify(snapshotDao, Mockito.never()).update(Mockito.anyLong(), Mockito.any(SnapshotVO.class));
1805+
Mockito.verifyNoInteractions(_resourceLimitMgr);
1806+
Mockito.verifyNoInteractions(annotationDao);
17961807
}
17971808

17981809
@Test

0 commit comments

Comments
 (0)