Skip to content

Commit 4bb85cd

Browse files
committed
CLOUDSTACK-5303: fix incorrect resource count (snapshot, secondary_storage)
(cherry picked from commit fa43987)
1 parent 3d9a6ec commit 4bb85cd

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎engine/schema/src/com/cloud/storage/dao/SnapshotDaoImpl.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ protected void init() {
173173
CountSnapshotsByAccount = createSearchBuilder(Long.class);
174174
CountSnapshotsByAccount.select(null, Func.COUNT, null);
175175
CountSnapshotsByAccount.and("account", CountSnapshotsByAccount.entity().getAccountId(), SearchCriteria.Op.EQ);
176+
CountSnapshotsByAccount.and("status", CountSnapshotsByAccount.entity().getState(), SearchCriteria.Op.NIN);
176177
CountSnapshotsByAccount.and("removed", CountSnapshotsByAccount.entity().getRemoved(), SearchCriteria.Op.NULL);
177178
CountSnapshotsByAccount.done();
178179

@@ -268,6 +269,7 @@ public long updateSnapshotSecHost(long dcId, long secHostId) {
268269
public Long countSnapshotsForAccount(long accountId) {
269270
SearchCriteria<Long> sc = CountSnapshotsByAccount.create();
270271
sc.setParameters("account", accountId);
272+
sc.setParameters("status", State.Error, State.Destroyed);
271273
return customSearch(sc, null).get(0);
272274
}
273275

‎server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)