Update snapshot physical size

This commit is contained in:
Suresh Kumar Anaparti 2026-01-21 12:37:06 +05:30
parent 71884a1501
commit 0659e8d973
No known key found for this signature in database
GPG Key ID: D7CEAE3A9E71D0AA
5 changed files with 22 additions and 29 deletions

View File

@ -111,5 +111,5 @@ StateDao<ObjectInDataStoreStateMachine.State, ObjectInDataStoreStateMachine.Even
int expungeBySnapshotList(List<Long> snapshotIds, Long batchSize);
long getSnapshotsSizeOnPrimaryByAccountId(long accountId);
long getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(long accountId);
}

View File

@ -78,11 +78,14 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
" order by created %s " +
" limit 1";
private static final String GET_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT = "SELECT SUM(size) " +
"FROM cloud.snapshots " +
"WHERE account_id = ? " +
"AND removed IS NULL " +
"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'))";
private static final String GET_PHYSICAL_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT = "SELECT SUM(s.physical_size) " +
"FROM cloud.snapshot_store_ref s " +
"INNER JOIN cloud.snapshots ON s.snapshot_id = snapshots.id " +
"WHERE snapshots.account_id = ? " +
"AND snapshots.removed IS NULL " +
"AND s.state = 'Ready' " +
"AND s.store_role = 'Primary' " +
"AND NOT EXISTS (SELECT 1 FROM cloud.snapshot_store_ref i WHERE i.snapshot_id = s.snapshot_id AND i.store_role = 'Image')";
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@ -585,27 +588,21 @@ public class SnapshotDataStoreDaoImpl extends GenericDaoBase<SnapshotDataStoreVO
}
@Override
public long getSnapshotsSizeOnPrimaryByAccountId(long accountId) {
String sql = String.format("SELECT SUM(size) " +
"FROM cloud.snapshots " +
"WHERE account_id = %d " +
"AND removed IS NULL " +
"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);
long snapshotsSize = 0;
public long getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(long accountId) {
long snapshotsPhysicalSize = 0;
try (TransactionLegacy transactionLegacy = TransactionLegacy.currentTxn()) {
try (PreparedStatement preparedStatement = transactionLegacy.prepareStatement(GET_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT)) {
try (PreparedStatement preparedStatement = transactionLegacy.prepareStatement(GET_PHYSICAL_SIZE_OF_SNAPSHOTS_ON_PRIMARY_BY_ACCOUNT)) {
preparedStatement.setLong(1, accountId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
snapshotsSize = resultSet.getLong(1);
snapshotsPhysicalSize = resultSet.getLong(1);
}
}
}
} catch (SQLException e) {
logger.warn("Failed to get the snapshots size for the account [{}] due to [{}].", accountId, e.getMessage(), e);
logger.warn("Failed to get the snapshots physical size for the account [{}] due to [{}].", accountId, e.getMessage(), e);
}
return snapshotsSize;
return snapshotsPhysicalSize;
}
}

View File

@ -1436,17 +1436,17 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
}
protected long calculatePrimaryStorageForAccount(long accountId, String tag) {
long snapshotsSizeOnPrimary = _snapshotDataStoreDao.getSnapshotsSizeOnPrimaryByAccountId(accountId);
long snapshotsPhysicalSizeOnPrimaryStorage = _snapshotDataStoreDao.getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(accountId);
if (StringUtils.isEmpty(tag)) {
List<Long> virtualRouters = _vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId);
return snapshotsSizeOnPrimary + _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters);
return snapshotsPhysicalSizeOnPrimaryStorage + _volumeDao.primaryStorageUsedForAccount(accountId, virtualRouters);
}
long storage = 0;
List<VolumeVO> volumes = getVolumesWithAccountAndTag(accountId, tag);
for (VolumeVO volume : volumes) {
storage += volume.getSize() == null ? 0L : volume.getSize();
}
return snapshotsSizeOnPrimary + storage;
return snapshotsPhysicalSizeOnPrimaryStorage + storage;
}
@Override

View File

@ -623,7 +623,7 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
_snapshotDao.update(snapshot.getId(), snapshot);
snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
Long snapshotOwnerId = vm.getAccountId();
long snapshotOwnerId = vm.getAccountId();
try {
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
@ -631,7 +631,6 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
throw new CloudRuntimeException(String.format("Unable to find Snapshot strategy to handle Snapshot [%s]", snapshot));
}
snapshotInfo = snapshotStrategy.backupSnapshot(snapshotInfo);
} catch (Exception e) {
logger.debug("Failed to backup Snapshot from Instance Snapshot", e);
_resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.snapshot);
@ -780,12 +779,11 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
_accountMgr.checkAccess(caller, null, true, snapshotCheck);
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshotCheck, zoneId, SnapshotOperation.DELETE);
if (snapshotStrategy == null) {
logger.error("Unable to find snapshot strategy to handle snapshot [{}]", snapshotCheck);
return false;
}
Pair<List<SnapshotDataStoreVO>, List<Long>> storeRefAndZones = getStoreRefsAndZonesForSnapshotDelete(snapshotId, zoneId);
List<SnapshotDataStoreVO> snapshotStoreRefs = storeRefAndZones.first();
List<Long> zoneIds = storeRefAndZones.second();
@ -998,9 +996,7 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
if (Type.MANUAL == snapshot.getRecurringType()) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.snapshot);
for (SnapshotDataStoreVO snapshotStoreRef : snapshotStoreRefs) {
if (!DataStoreRole.Primary.equals(snapshotStoreRef.getRole())) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
}
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(snapshotStoreRef.getPhysicalSize()));
}
}

View File

@ -843,7 +843,7 @@ public class ResourceLimitManagerImplTest extends TestCase {
String tag = null;
Mockito.when(vmDao.findIdsOfAllocatedVirtualRoutersForAccount(accountId))
.thenReturn(List.of(1L));
Mockito.when(snapshotDataStoreDao.getSnapshotsSizeOnPrimaryByAccountId(accountId)).thenReturn(100L);
Mockito.when(snapshotDataStoreDao.getSnapshotsPhysicalSizeOnPrimaryStorageByAccountId(accountId)).thenReturn(100L);
Mockito.when(volumeDao.primaryStorageUsedForAccount(Mockito.eq(accountId), Mockito.anyList())).thenReturn(100L);
Assert.assertEquals(200L, resourceLimitManager.calculatePrimaryStorageForAccount(accountId, tag));