3232import javax .inject .Inject ;
3333import javax .naming .ConfigurationException ;
3434
35+ import com .cloud .resourcelimit .CheckedReservation ;
3536import org .apache .cloudstack .acl .SecurityChecker ;
3637import com .cloud .api .ApiDBUtils ;
3738import org .apache .cloudstack .annotation .AnnotationService ;
6768import org .apache .cloudstack .framework .config .Configurable ;
6869import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
6970import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
71+ import org .apache .cloudstack .reservation .dao .ReservationDao ;
7072import org .apache .cloudstack .resourcedetail .SnapshotPolicyDetailVO ;
7173import org .apache .cloudstack .resourcedetail .dao .SnapshotPolicyDetailsDao ;
7274import org .apache .cloudstack .snapshot .SnapshotHelper ;
@@ -240,6 +242,8 @@ public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implement
240242 @ Inject
241243 private AnnotationDao annotationDao ;
242244
245+ @ Inject
246+ private ReservationDao reservationDao ;
243247 @ Inject
244248 protected SnapshotHelper snapshotHelper ;
245249 @ Inject
@@ -1705,20 +1709,6 @@ public Snapshot allocSnapshot(Long volumeId, Long policyId, String snapshotName,
17051709 Type snapshotType = getSnapshotType (policyId );
17061710 Account owner = _accountMgr .getAccount (volume .getAccountId ());
17071711
1708- ResourceType storeResourceType = getStoreResourceType (volume .getDataCenterId (), locationType );
1709- try {
1710- _resourceLimitMgr .checkResourceLimit (owner , ResourceType .snapshot );
1711- _resourceLimitMgr .checkResourceLimit (owner , storeResourceType , volume .getSize ());
1712- } catch (ResourceAllocationException e ) {
1713- if (snapshotType != Type .MANUAL ) {
1714- String msg = String .format ("Snapshot resource limit exceeded for account %s. Failed to create recurring snapshots" , owner );
1715- logger .warn (msg );
1716- _alertMgr .sendAlert (AlertManager .AlertType .ALERT_TYPE_UPDATE_RESOURCE_COUNT , 0L , 0L , msg , "Snapshot resource limit exceeded for account id : " + owner .getId ()
1717- + ". Failed to create recurring snapshots; please use updateResourceLimit to increase the limit" );
1718- }
1719- throw e ;
1720- }
1721-
17221712 // Determine the name for this snapshot
17231713 // Snapshot Name: VMInstancename + volumeName + timeString
17241714 String timeString = DateUtil .getDateDisplayString (DateUtil .GMT_TIMEZONE , new Date (), DateUtil .YYYYMMDD_FORMAT );
@@ -1750,6 +1740,14 @@ public Snapshot allocSnapshot(Long volumeId, Long policyId, String snapshotName,
17501740 hypervisorType = volume .getHypervisorType ();
17511741 }
17521742
1743+ ResourceType storeResourceType = ResourceType .secondary_storage ;
1744+ if (!isBackupSnapshotToSecondaryForZone (volume .getDataCenterId ()) ||
1745+ Snapshot .LocationType .PRIMARY .equals (locationType )) {
1746+ storeResourceType = ResourceType .primary_storage ;
1747+ }
1748+
1749+ try (CheckedReservation volumeSnapshotReservation = new CheckedReservation (owner , ResourceType .snapshot , null , null , 1L , reservationDao , _resourceLimitMgr );
1750+ CheckedReservation storageReservation = new CheckedReservation (owner , storeResourceType , null , null , volume .getSize (), reservationDao , _resourceLimitMgr )) {
17531751 SnapshotVO snapshotVO = new SnapshotVO (volume .getDataCenterId (), volume .getAccountId (), volume .getDomainId (), volume .getId (), volume .getDiskOfferingId (), snapshotName ,
17541752 (short )snapshotType .ordinal (), snapshotType .name (), volume .getSize (), volume .getMinIops (), volume .getMaxIops (), hypervisorType , locationType );
17551753
@@ -1761,6 +1759,17 @@ public Snapshot allocSnapshot(Long volumeId, Long policyId, String snapshotName,
17611759 _resourceLimitMgr .incrementResourceCount (volume .getAccountId (), ResourceType .snapshot );
17621760 _resourceLimitMgr .incrementResourceCount (volume .getAccountId (), storeResourceType , volume .getSize ());
17631761 return snapshot ;
1762+ } catch (Exception e ) {
1763+ if (e instanceof ResourceAllocationException ) {
1764+ if (snapshotType != Type .MANUAL ) {
1765+ String msg = String .format ("Snapshot resource limit exceeded for account id : %s. Failed to create recurring snapshots" , owner .getId ());
1766+ logger .warn (msg );
1767+ _alertMgr .sendAlert (AlertManager .AlertType .ALERT_TYPE_UPDATE_RESOURCE_COUNT , 0L , 0L , msg , msg + ". Please, use updateResourceLimit to increase the limit" );
1768+ }
1769+ throw (ResourceAllocationException ) e ;
1770+ }
1771+ throw new CloudRuntimeException (e );
1772+ }
17641773 }
17651774
17661775 @ Override
0 commit comments