3535
3636import org .apache .cloudstack .acl .SecurityChecker .AccessType ;
3737import org .apache .cloudstack .context .CallContext ;
38+ import org .apache .cloudstack .engine .subsystem .api .storage .ObjectInDataStoreStateMachine ;
3839import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
3940import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
41+ import org .apache .cloudstack .storage .datastore .db .SnapshotDataStoreDao ;
42+ import org .apache .cloudstack .storage .datastore .db .SnapshotDataStoreVO ;
4043import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreDao ;
4144import org .apache .cloudstack .storage .datastore .db .TemplateDataStoreVO ;
4245
6871import com .cloud .projects .dao .ProjectDao ;
6972import com .cloud .service .ServiceOfferingVO ;
7073import com .cloud .service .dao .ServiceOfferingDao ;
74+ import com .cloud .storage .DataStoreRole ;
75+ import com .cloud .storage .SnapshotVO ;
7176import com .cloud .storage .VMTemplateStorageResourceAssoc .Status ;
7277import com .cloud .storage .VMTemplateVO ;
7378import 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
0 commit comments