Skip to content

Commit c3d8e20

Browse files
CLOUDSTACK-5314: Negative (-ve) values for primary storage and volumes are shown in the resource count table.
1 parent 6d75c31 commit c3d8e20

5 files changed

Lines changed: 29 additions & 33 deletions

File tree

engine/schema/src/com/cloud/storage/dao/SnapshotDao.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,4 @@ public interface SnapshotDao extends GenericDao<SnapshotVO, Long>, StateDao<Snap
5757

5858
List<SnapshotVO> listAllByStatus(Snapshot.State... status);
5959

60-
/**
61-
* Gets the Total Secondary Storage space (in bytes) used by snapshots
62-
* allocated for an account
63-
*
64-
* @param account
65-
* @return total Secondary Storage space allocated
66-
*/
67-
long secondaryStorageUsedForAccount(long accountId);
68-
6960
}

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.cloud.storage.SnapshotVO;
3737
import com.cloud.storage.Volume;
3838
import com.cloud.storage.VolumeVO;
39-
import com.cloud.storage.dao.VolumeDaoImpl.SumCount;
4039
import com.cloud.tags.dao.ResourceTagDao;
4140
import com.cloud.utils.db.DB;
4241
import com.cloud.utils.db.Filter;
@@ -46,7 +45,6 @@
4645
import com.cloud.utils.db.SearchBuilder;
4746
import com.cloud.utils.db.SearchCriteria;
4847
import com.cloud.utils.db.SearchCriteria.Func;
49-
import com.cloud.utils.db.SearchCriteria.Op;
5048
import com.cloud.utils.db.TransactionLegacy;
5149
import com.cloud.vm.VMInstanceVO;
5250
import com.cloud.vm.dao.VMInstanceDao;
@@ -72,7 +70,6 @@ public class SnapshotDaoImpl extends GenericDaoBase<SnapshotVO, Long> implements
7270
private SearchBuilder<SnapshotVO> InstanceIdSearch;
7371
private SearchBuilder<SnapshotVO> StatusSearch;
7472
private GenericSearchBuilder<SnapshotVO, Long> CountSnapshotsByAccount;
75-
private GenericSearchBuilder<SnapshotVO, SumCount> secondaryStorageSearch;
7673
@Inject
7774
ResourceTagDao _tagsDao;
7875
@Inject
@@ -193,12 +190,6 @@ protected void init() {
193190

194191
InstanceIdSearch.join("instanceSnapshots", volumeSearch, volumeSearch.entity().getId(), InstanceIdSearch.entity().getVolumeId(), JoinType.INNER);
195192
InstanceIdSearch.done();
196-
197-
secondaryStorageSearch = createSearchBuilder(SumCount.class);
198-
secondaryStorageSearch.select("sum", Func.SUM, secondaryStorageSearch.entity().getSize());
199-
secondaryStorageSearch.and("accountId", secondaryStorageSearch.entity().getAccountId(), Op.EQ);
200-
secondaryStorageSearch.and("isRemoved", secondaryStorageSearch.entity().getRemoved(), Op.NULL);
201-
secondaryStorageSearch.done();
202193
}
203194

204195
@Override
@@ -333,15 +324,4 @@ public boolean updateState(State currentState, Event event, State nextState, Sna
333324
return true;
334325
}
335326

336-
@Override
337-
public long secondaryStorageUsedForAccount(long accountId) {
338-
SearchCriteria<SumCount> sc = secondaryStorageSearch.create();
339-
sc.setParameters("accountId", accountId);
340-
List<SumCount> storageSpace = customSearch(sc, null);
341-
if (storageSpace != null) {
342-
return storageSpace.get(0).sum;
343-
} else {
344-
return 0;
345-
}
346-
}
347327
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535

3636
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
3737
import org.apache.cloudstack.context.CallContext;
38+
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
3839
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
3940
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
41+
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
42+
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO;
4043
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
4144
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
4245

@@ -68,6 +71,8 @@
6871
import com.cloud.projects.dao.ProjectDao;
6972
import com.cloud.service.ServiceOfferingVO;
7073
import com.cloud.service.dao.ServiceOfferingDao;
74+
import com.cloud.storage.DataStoreRole;
75+
import com.cloud.storage.SnapshotVO;
7176
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
7277
import com.cloud.storage.VMTemplateVO;
7378
import com.cloud.storage.dao.SnapshotDao;
@@ -149,8 +154,11 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
149154
private TemplateDataStoreDao _vmTemplateStoreDao;
150155
@Inject
151156
private VlanDao _vlanDao;
157+
@Inject
158+
private SnapshotDataStoreDao _snapshotDataStoreDao;
152159

153160
protected GenericSearchBuilder<TemplateDataStoreVO, SumCount> templateSizeSearch;
161+
protected GenericSearchBuilder<SnapshotDataStoreVO, SumCount> snapshotSizeSearch;
154162

155163
protected SearchBuilder<ResourceCountVO> ResourceCountSearch;
156164
ScheduledExecutorService _rcExecutor;
@@ -189,6 +197,15 @@ public boolean configure(final String name, final Map<String, Object> params) th
189197
templateSizeSearch.join("templates", join1, templateSizeSearch.entity().getTemplateId(), join1.entity().getId(), JoinBuilder.JoinType.INNER);
190198
templateSizeSearch.done();
191199

200+
snapshotSizeSearch = _snapshotDataStoreDao.createSearchBuilder(SumCount.class);
201+
snapshotSizeSearch.select("sum", Func.SUM, snapshotSizeSearch.entity().getSize());
202+
snapshotSizeSearch.and("state", snapshotSizeSearch.entity().getState(), Op.EQ);
203+
snapshotSizeSearch.and("storeRole", snapshotSizeSearch.entity().getRole(), Op.EQ);
204+
SearchBuilder<SnapshotVO> join2 = _snapshotDao.createSearchBuilder();
205+
join2.and("accountId", join2.entity().getAccountId(), Op.EQ);
206+
snapshotSizeSearch.join("snapshots", join2, snapshotSizeSearch.entity().getSnapshotId(), join2.entity().getId(), JoinBuilder.JoinType.INNER);
207+
snapshotSizeSearch.done();
208+
192209
_resourceCountCheckInterval = NumbersUtil.parseInt(_configDao.getValue(Config.ResourceCountCheckInterval.key()), 0);
193210
if (_resourceCountCheckInterval > 0) {
194211
_rcExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("ResourceCountChecker"));
@@ -922,7 +939,7 @@ public long calculateMemoryForAccount(long accountId) {
922939

923940
public long calculateSecondaryStorageForAccount(long accountId) {
924941
long totalVolumesSize = _volumeDao.secondaryStorageUsedForAccount(accountId);
925-
long totalSnapshotsSize = _snapshotDao.secondaryStorageUsedForAccount(accountId);
942+
long totalSnapshotsSize = 0;
926943
long totalTemplatesSize = 0;
927944

928945
SearchCriteria<SumCount> sc = templateSizeSearch.create();
@@ -934,6 +951,14 @@ public long calculateSecondaryStorageForAccount(long accountId) {
934951
totalTemplatesSize = templates.get(0).sum;
935952
}
936953

954+
SearchCriteria<SumCount> sc2 = snapshotSizeSearch.create();
955+
sc2.setParameters("state", ObjectInDataStoreStateMachine.State.Ready);
956+
sc2.setParameters("storeRole", DataStoreRole.Image);
957+
sc2.setJoinParameters("snapshots", "accountId", accountId);
958+
List<SumCount> snapshots = _snapshotDataStoreDao.customSearch(sc2, null);
959+
if (snapshots != null) {
960+
totalSnapshotsSize = snapshots.get(0).sum;
961+
}
937962
return totalVolumesSize + totalSnapshotsSize + totalTemplatesSize;
938963
}
939964

server/src/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ public VolumeVO createVolume(CreateVolumeCmd cmd) {
759759
if (!created) {
760760
s_logger.trace("Decrementing volume resource count for account id=" + volume.getAccountId() + " as volume failed to create on the backend");
761761
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.volume, cmd.getDisplayVolume());
762-
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, cmd.getDisplayVolume(), new Long(volume.getSize()));
762+
_resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.primary_storage.getOrdinal());
763763
}
764764
}
765765
}
@@ -1005,7 +1005,7 @@ public boolean deleteVolume(long volumeId, Account caller) throws ConcurrentOper
10051005
/* If volume is in primary storage, decrement primary storage count else decrement secondary
10061006
storage count (in case of upload volume). */
10071007
if (volume.getFolder() != null || volume.getPath() != null || volume.getState() == Volume.State.Allocated) {
1008-
_resourceLimitMgr.decrementResourceCount(volume.getAccountId(), ResourceType.primary_storage, volume.isDisplayVolume(), new Long(volume.getSize()));
1008+
_resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.primary_storage.getOrdinal());
10091009
} else {
10101010
_resourceLimitMgr.recalculateResourceCount(volume.getAccountId(), volume.getDomainId(), ResourceType.secondary_storage.getOrdinal());
10111011
}

server/src/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,7 @@ public boolean expunge(UserVmVO vm, long callerUserId, Account caller) {
16631663
// Update Resource count
16641664
if (vm.getAccountId() != Account.ACCOUNT_ID_SYSTEM && !rootVol.isEmpty()) {
16651665
_resourceLimitMgr.decrementResourceCount(vm.getAccountId(), ResourceType.volume);
1666-
_resourceLimitMgr.decrementResourceCount(vm.getAccountId(), ResourceType.primary_storage, new Long(rootVol.get(0).getSize()));
1666+
_resourceLimitMgr.recalculateResourceCount(vm.getAccountId(), vm.getDomainId(), ResourceType.primary_storage.getOrdinal());
16671667
}
16681668

16691669
// Only if vm is not expunged already, cleanup it's resources

0 commit comments

Comments
 (0)