Skip to content

Commit fa43987

Browse files
committed
CLOUDSTACK-5303: fix incorrect resource count (snapshot, secondary_storage
1 parent dfded88 commit fa43987

2 files changed

Lines changed: 13 additions & 4 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
@@ -176,6 +176,7 @@ protected void init() {
176176
CountSnapshotsByAccount = createSearchBuilder(Long.class);
177177
CountSnapshotsByAccount.select(null, Func.COUNT, null);
178178
CountSnapshotsByAccount.and("account", CountSnapshotsByAccount.entity().getAccountId(), SearchCriteria.Op.EQ);
179+
CountSnapshotsByAccount.and("status", CountSnapshotsByAccount.entity().getState(), SearchCriteria.Op.NIN);
179180
CountSnapshotsByAccount.and("removed", CountSnapshotsByAccount.entity().getRemoved(), SearchCriteria.Op.NULL);
180181
CountSnapshotsByAccount.done();
181182

@@ -277,6 +278,7 @@ public long updateSnapshotSecHost(long dcId, long secHostId) {
277278
public Long countSnapshotsForAccount(long accountId) {
278279
SearchCriteria<Long> sc = CountSnapshotsByAccount.create();
279280
sc.setParameters("account", accountId);
281+
sc.setParameters("status", State.Error, State.Destroyed);
280282
return customSearch(sc, null).get(0);
281283
}
282284

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,16 @@ public boolean deleteSnapshot(long snapshotId) {
483483
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_DELETE, snapshotCheck.getAccountId(),
484484
snapshotCheck.getDataCenterId(), snapshotId, snapshotCheck.getName(), null, null, 0L,
485485
snapshotCheck.getClass().getName(), snapshotCheck.getUuid());
486+
}
487+
if (snapshotCheck.getState() != Snapshot.State.Error && snapshotCheck.getState() != Snapshot.State.Destroyed) {
488+
_resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.snapshot);
489+
if (snapshotCheck.getState() == Snapshot.State.BackedUp)
490+
_resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.secondary_storage,
491+
new Long(snapshotCheck.getSize()));
492+
else if (!backup && snapshotCheck.getState() == Snapshot.State.CreatedOnPrimary)
493+
_resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.primary_storage,
494+
new Long(snapshotCheck.getSize()));
486495
}
487-
_resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.snapshot);
488-
_resourceLimitMgr.decrementResourceCount(snapshotCheck.getAccountId(), ResourceType.secondary_storage,
489-
new Long(snapshotCheck.getSize()));
490496
}
491497
return result;
492498
} catch (Exception e) {
@@ -1012,12 +1018,12 @@ public SnapshotInfo takeSnapshot(VolumeInfo volume) throws ResourceAllocationExc
10121018
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_CREATE, snapshot.getAccountId(),
10131019
snapshot.getDataCenterId(), snapshotId, snapshot.getName(), null, null,
10141020
volume.getSize(), snapshot.getClass().getName(), snapshot.getUuid());
1015-
_resourceLimitMgr.incrementResourceCount(snapshotOwner.getId(), ResourceType.snapshot);
10161021
} catch (Exception e) {
10171022
s_logger.debug("post process snapshot failed", e);
10181023
}
10191024
} catch(Exception e) {
10201025
s_logger.debug("Failed to create snapshot", e);
1026+
_resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.snapshot);
10211027
if (backup) {
10221028
_resourceLimitMgr.decrementResourceCount(snapshotOwner.getId(), ResourceType.secondary_storage,
10231029
new Long(volume.getSize()));
@@ -1171,6 +1177,7 @@ public Snapshot allocSnapshot(Long volumeId, Long policyId) throws ResourceAlloc
11711177
if (snapshot == null) {
11721178
throw new CloudRuntimeException("Failed to create snapshot for volume: " + volume.getId());
11731179
}
1180+
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.snapshot);
11741181
if (backup) {
11751182
_resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage,
11761183
new Long(volume.getSize()));

0 commit comments

Comments
 (0)