Skip to content

Commit 0659e8d

Browse files
Update snapshot physical size
1 parent 71884a1 commit 0659e8d

File tree

5 files changed

+22
-29
lines changed

5 files changed

+22
-29
lines changed

engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/SnapshotDataStoreDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,5 @@ public interface SnapshotDataStoreDao extends GenericDao<SnapshotDataStoreVO, Lo
111111

112112
int expungeBySnapshotList(List<Long> snapshotIds, Long batchSize);
113113

114-
long getSnapshotsSizeOnPrimaryByAccountId(long accountId);
114+
long getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(long accountId);
115115
}

engine/schema/src/main/java/org/apache/cloudstack/storage/datastore/db/SnapshotDataStoreDaoImpl.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
7878
" order by created %s " +
7979
" limit 1";
8080

81-
private static final String GET_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT = "SELECT SUM(size) " +
82-
"FROM cloud.snapshots " +
83-
"WHERE account_id = ? " +
84-
"AND removed IS NULL " +
85-
"AND id IN (SELECT s.snapshot_id FROM cloud.snapshot_store_ref s WHERE s.store_role = 'Primary' AND s.state = 'Ready' AND NOT EXISTS (SELECT 1 FROM cloud.snapshot_store_ref i WHERE i.snapshot_id = s.snapshot_id AND i.store_role = 'Image'))";
81+
private static final String GET_PHYSICAL_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT = "SELECT SUM(s.physical_size) " +
82+
"FROM cloud.snapshot_store_ref s " +
83+
"INNER JOIN cloud.snapshots ON s.snapshot_id = snapshots.id " +
84+
"WHERE snapshots.account_id = ? " +
85+
"AND snapshots.removed IS NULL " +
86+
"AND s.state = 'Ready' " +
87+
"AND s.store_role = 'Primary' " +
88+
"AND NOT EXISTS (SELECT 1 FROM cloud.snapshot_store_ref i WHERE i.snapshot_id = s.snapshot_id AND i.store_role = 'Image')";
8689

8790
@Override
8891
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@@ -585,27 +588,21 @@ public int expungeBySnapshotList(final List<Long> snapshotIds, final Long batchS
585588
}
586589

587590
@Override
588-
public long getSnapshotsSizeOnPrimaryByAccountId(long accountId) {
589-
String sql = String.format("SELECT SUM(size) " +
590-
"FROM cloud.snapshots " +
591-
"WHERE account_id = %d " +
592-
"AND removed IS NULL " +
593-
"AND id IN (SELECT s.snapshot_id FROM cloud.snapshot_store_ref s WHERE s.store_role = 'Primary' AND s.state = 'Ready' AND NOT EXISTS (SELECT 1 FROM cloud.snapshot_store_ref i WHERE i.snapshot_id = s.snapshot_id AND i.store_role = 'Image'))", accountId);
594-
595-
long snapshotsSize = 0;
591+
public long getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(long accountId) {
592+
long snapshotsPhysicalSize = 0;
596593
try (TransactionLegacy transactionLegacy = TransactionLegacy.currentTxn()) {
597-
try (PreparedStatement preparedStatement = transactionLegacy.prepareStatement(GET_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT)) {
594+
try (PreparedStatement preparedStatement = transactionLegacy.prepareStatement(GET_PHYSICAL_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT)) {
598595
preparedStatement.setLong(1, accountId);
599596

600597
try (ResultSet resultSet = preparedStatement.executeQuery()) {
601598
if (resultSet.next()) {
602-
snapshotsSize = resultSet.getLong(1);
599+
snapshotsPhysicalSize = resultSet.getLong(1);
603600
}
604601
}
605602
}
606603
} catch (SQLException e) {
607-
logger.warn("Failed to get the snapshots size for the account [{}] due to [{}].", accountId, e.getMessage(), e);
604+
logger.warn("Failed to get the snapshots physical size for the account [{}] due to [{}].", accountId, e.getMessage(), e);
608605
}
609-
return snapshotsSize;
606+
return snapshotsPhysicalSize;
610607
}
611608
}

server/src/main/java/com/cloud/resourcelimit/ResourceLimitManagerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,17 +1436,17 @@ private long calculatePublicIpForAccount(long accountId) {
14361436
}
14371437

14381438
protected long calculatePrimaryStorageForAccount(long accountId, String tag) {
1439-
long snapshotsSizeOnPrimary = _snapshotDataStoreDao.getSnapshotsSizeOnPrimaryByAccountId(accountId);
1439+
long snapshotsPhysicalSizeOnPrimaryStorage = _snapshotDataStoreDao.getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(accountId);
14401440
if (StringUtils.isEmpty(tag)) {
14411441
List<Long> virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId);
1442-
return snapshotsSizeOnPrimary + _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters);
1442+
return snapshotsPhysicalSizeOnPrimaryStorage + _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters);
14431443
}
14441444
long storage = 0;
14451445
List<VolumeVO> volumes = getVolumesWithAccountAndTag(accountId, tag);
14461446
for (VolumeVO volume : volumes) {
14471447
storage += volume.getSize() == null ? 0L : volume.getSize();
14481448
}
1449-
return snapshotsSizeOnPrimary + storage;
1449+
return snapshotsPhysicalSizeOnPrimaryStorage + storage;
14501450
}
14511451

14521452
@Override

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -623,15 +623,14 @@ public Snapshot backupSnapshotFromVmSnapshot(Long snapshotId, Long vmId, Long vo
623623
_snapshotDao.update(snapshot.getId(), snapshot);
624624
snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
625625

626-
Long snapshotOwnerId = vm.getAccountId();
626+
long snapshotOwnerId = vm.getAccountId();
627627

628628
try {
629629
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
630630
if (snapshotStrategy == null) {
631631
throw new CloudRuntimeException(String.format("Unable to find Snapshot strategy to handle Snapshot [%s]", snapshot));
632632
}
633633
snapshotInfo = snapshotStrategy.backupSnapshot(snapshotInfo);
634-
635634
} catch (Exception e) {
636635
logger.debug("Failed to backup Snapshot from Instance Snapshot", e);
637636
_resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.snapshot);
@@ -780,12 +779,11 @@ public boolean deleteSnapshot(long snapshotId, Long zoneId) {
780779
_accountMgr.checkAccess(caller, null, true, snapshotCheck);
781780

782781
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshotCheck, zoneId, SnapshotOperation.DELETE);
783-
784782
if (snapshotStrategy == null) {
785783
logger.error("Unable to find snapshot strategy to handle snapshot [{}]", snapshotCheck);
786-
787784
return false;
788785
}
786+
789787
Pair<List<SnapshotDataStoreVO>, List<Long>> storeRefAndZones = getStoreRefsAndZonesForSnapshotDelete(snapshotId, zoneId);
790788
List<SnapshotDataStoreVO> snapshotStoreRefs = storeRefAndZones.first();
791789
List<Long> zoneIds = storeRefAndZones.second();
@@ -998,9 +996,7 @@ public boolean deleteSnapshotDirsForAccount(Account account) {
998996
if (Type.MANUAL == snapshot.getRecurringType()) {
999997
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.snapshot);
1000998
for (SnapshotDataStoreVO snapshotStoreRef : snapshotStoreRefs) {
1001-
if (!DataStoreRole.Primary.equals(snapshotStoreRef.getRole())) {
1002-
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
1003-
}
999+
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
10041000
}
10051001
}
10061002

server/src/test/java/com/cloud/resourcelimit/ResourceLimitManagerImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ public void testCalculatePrimaryStorageForAccount() {
843843
String tag = null;
844844
Mockito.when(vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId))
845845
.thenReturn(List.of(1L));
846-
Mockito.when(snapshotDataStoreDao.getSnapshotsSizeOnPrimaryByAccountId(accountId)).thenReturn(100L);
846+
Mockito.when(snapshotDataStoreDao.getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(accountId)).thenReturn(100L);
847847
Mockito.when(volumeDao.primaryStorageUsedForAccount(Mockito.eq(accountId), Mockito.anyList())).thenReturn(100L);
848848
Assert.assertEquals(200L, resourceLimitManager.calculatePrimaryStorageForAccount(accountId, tag));
849849

0 commit comments

Comments
 (0)