@@ -425,8 +425,10 @@ public boolean deleteSnapshot(long snapshotId) {
425425 snapshotCheck .getDataCenterId (), snapshotId , snapshotCheck .getName (), null , null , 0L ,
426426 snapshotCheck .getClass ().getName (), snapshotCheck .getUuid ());
427427 }
428- _resourceLimitMgr .decrementResourceCount (snapshotCheck .getAccountId (), ResourceType .snapshot );
429- _resourceLimitMgr .decrementResourceCount (snapshotCheck .getAccountId (), ResourceType .secondary_storage , new Long (snapshotStoreRef .getSize ()));
428+ if (snapshotCheck .getState () != Snapshot .State .Error && snapshotCheck .getState () != Snapshot .State .Destroyed )
429+ _resourceLimitMgr .decrementResourceCount (snapshotCheck .getAccountId (), ResourceType .snapshot );
430+ if (snapshotCheck .getState () == Snapshot .State .BackedUp )
431+ _resourceLimitMgr .decrementResourceCount (snapshotCheck .getAccountId (), ResourceType .secondary_storage , new Long (snapshotStoreRef .getSize ()));
430432 }
431433 return result ;
432434 } catch (Exception e ) {
@@ -948,7 +950,6 @@ public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationExc
948950 UsageEventUtils .publishUsageEvent (EventTypes .EVENT_SNAPSHOT_CREATE , snapshot .getAccountId (),
949951 snapshot .getDataCenterId (), snapshotId , snapshot .getName (), null , null ,
950952 volume .getSize (), snapshot .getClass ().getName (), snapshot .getUuid ());
951- _resourceLimitMgr .incrementResourceCount (snapshotOwner .getId (), ResourceType .snapshot );
952953 SnapshotDataStoreVO snapshotStoreRef = _snapshotStoreDao .findBySnapshot (snapshotId , DataStoreRole .Image );
953954 // Correct the resource count of snapshot in case of delta snapshots.
954955 _resourceLimitMgr .decrementResourceCount (snapshotOwner .getId (), ResourceType .secondary_storage , new Long (volume .getSize () - snapshotStoreRef .getSize ()));
@@ -957,6 +958,7 @@ public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationExc
957958 }
958959 } catch (Exception e ) {
959960 s_logger .debug ("Failed to create snapshot" , e );
961+ _resourceLimitMgr .decrementResourceCount (snapshotOwner .getId (), ResourceType .snapshot );
960962 _resourceLimitMgr .decrementResourceCount (snapshotOwner .getId (), ResourceType .secondary_storage , new Long (volume .getSize ()));
961963 throw new CloudRuntimeException ("Failed to create snapshot" , e );
962964 }
@@ -1100,6 +1102,7 @@ public Snapshot allocSnapshot(Long volumeId, Long policyId) throws ResourceAlloc
11001102 if (snapshot == null ) {
11011103 throw new CloudRuntimeException ("Failed to create snapshot for volume: " + volume .getId ());
11021104 }
1105+ _resourceLimitMgr .incrementResourceCount (volume .getAccountId (), ResourceType .snapshot );
11031106 _resourceLimitMgr .incrementResourceCount (volume .getAccountId (), ResourceType .secondary_storage , new Long (volume .getSize ()));
11041107 return snapshot ;
11051108 }
0 commit comments