1- // Licensed to the Apacohe Software Foundation (ASF) under one
1+ // Licensed to the Apache Software Foundation (ASF) under one
22// or more contributor license agreements. See the NOTICE file
33// distributed with this work for additional information
44// regarding copyright ownership. The ASF licenses this file
4949import javax .naming .ConfigurationException ;
5050import javax .persistence .EntityExistsException ;
5151
52+ import com .cloud .configuration .Resource ;
5253import com .cloud .domain .Domain ;
5354import com .cloud .domain .dao .DomainDao ;
5455import com .cloud .network .vpc .VpcVO ;
8788import org .apache .cloudstack .framework .messagebus .MessageHandler ;
8889import org .apache .cloudstack .jobs .JobInfo ;
8990import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
91+ import org .apache .cloudstack .reservation .dao .ReservationDao ;
9092import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
9193import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
9294import org .apache .cloudstack .storage .to .VolumeObjectTO ;
@@ -296,6 +298,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
296298 @ Inject
297299 private VMInstanceDao _vmDao ;
298300 @ Inject
301+ private ReservationDao _reservationDao ;
302+ @ Inject
299303 private ServiceOfferingDao _offeringDao ;
300304 @ Inject
301305 private DiskOfferingDao _diskOfferingDao ;
@@ -914,7 +918,7 @@ protected boolean checkWorkItems(final VMInstanceVO vm, final State state) throw
914918
915919 @ DB
916920 protected Ternary <VMInstanceVO , ReservationContext , ItWorkVO > changeToStartState (final VirtualMachineGuru vmGuru , final VMInstanceVO vm , final User caller ,
917- final Account account ) throws ConcurrentOperationException {
921+ final Account account , Account owner , ServiceOfferingVO offering , VirtualMachineTemplate template ) throws ConcurrentOperationException {
918922 final long vmId = vm .getId ();
919923
920924 ItWorkVO work = new ItWorkVO (UUID .randomUUID ().toString (), _nodeId , State .Starting , vm .getType (), vm .getId ());
@@ -934,6 +938,9 @@ public Ternary<VMInstanceVO, ReservationContext, ItWorkVO> doInTransaction(final
934938 if (logger .isDebugEnabled ()) {
935939 logger .debug ("Successfully transitioned to start state for " + vm + " reservation id = " + work .getId ());
936940 }
941+ if (VirtualMachine .Type .User .equals (vm .type ) && ResourceCountRunningVMsonly .value ()) {
942+ _resourceLimitMgr .incrementVmResourceCount (owner .getAccountId (), vm .isDisplay (), offering , template );
943+ }
937944 return new Ternary <>(vm , context , work );
938945 }
939946
@@ -1126,7 +1133,10 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
11261133
11271134 final VirtualMachineGuru vmGuru = getVmGuru (vm );
11281135
1129- final Ternary <VMInstanceVO , ReservationContext , ItWorkVO > start = changeToStartState (vmGuru , vm , caller , account );
1136+ final Account owner = _entityMgr .findById (Account .class , vm .getAccountId ());
1137+ final ServiceOfferingVO offering = _offeringDao .findById (vm .getId (), vm .getServiceOfferingId ());
1138+ final VirtualMachineTemplate template = _entityMgr .findByIdIncludingRemoved (VirtualMachineTemplate .class , vm .getTemplateId ());
1139+ final Ternary <VMInstanceVO , ReservationContext , ItWorkVO > start = changeToStartState (vmGuru , vm , caller , account , owner , offering , template );
11301140 if (start == null ) {
11311141 return ;
11321142 }
@@ -1136,8 +1146,6 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
11361146 ItWorkVO work = start .third ();
11371147
11381148 VMInstanceVO startedVm = null ;
1139- final ServiceOfferingVO offering = _offeringDao .findById (vm .getId (), vm .getServiceOfferingId ());
1140- final VirtualMachineTemplate template = _entityMgr .findByIdIncludingRemoved (VirtualMachineTemplate .class , vm .getTemplateId ());
11411149
11421150 DataCenterDeployment plan = new DataCenterDeployment (vm .getDataCenterId (), vm .getPodIdToDeployIn (), null , null , null , null , ctx );
11431151 if (planToDeploy != null && planToDeploy .getDataCenterId () != 0 ) {
@@ -1150,12 +1158,6 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
11501158
11511159 final HypervisorGuru hvGuru = _hvGuruMgr .getGuru (vm .getHypervisorType ());
11521160
1153- // check resource count if ResourceCountRunningVMsonly.value() = true
1154- final Account owner = _entityMgr .findById (Account .class , vm .getAccountId ());
1155- if (VirtualMachine .Type .User .equals (vm .type ) && ResourceCountRunningVMsonly .value ()) {
1156- _resourceLimitMgr .incrementVmResourceCount (owner .getAccountId (), vm .isDisplay (), offering , template );
1157- }
1158-
11591161 boolean canRetry = true ;
11601162 ExcludeList avoids = null ;
11611163 try {
@@ -2277,16 +2279,21 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl
22772279 _workDao .update (work .getId (), work );
22782280 }
22792281
2280- boolean result = stateTransitTo (vm , Event .OperationSucceeded , null );
2281- if (result ) {
2282- vm .setPowerState (PowerState .PowerOff );
2283- _vmDao .update (vm .getId (), vm );
2284- if (VirtualMachine .Type .User .equals (vm .type ) && ResourceCountRunningVMsonly .value ()) {
2285- ServiceOfferingVO offering = _offeringDao .findById (vm .getId (), vm .getServiceOfferingId ());
2286- VMTemplateVO template = _templateDao .findByIdIncludingRemoved (vm .getTemplateId ());
2287- _resourceLimitMgr .decrementVmResourceCount (vm .getAccountId (), vm .isDisplay (), offering , template );
2282+ boolean result = Transaction .execute (new TransactionCallbackWithException <Boolean , NoTransitionException >() {
2283+ @ Override
2284+ public Boolean doInTransaction (TransactionStatus status ) throws NoTransitionException {
2285+ boolean result = stateTransitTo (vm , Event .OperationSucceeded , null );
2286+
2287+ if (result && VirtualMachine .Type .User .equals (vm .type ) && ResourceCountRunningVMsonly .value ()) {
2288+ ServiceOfferingVO offering = _offeringDao .findById (vm .getId (), vm .getServiceOfferingId ());
2289+ VMTemplateVO template = _templateDao .findByIdIncludingRemoved (vm .getTemplateId ());
2290+ _resourceLimitMgr .decrementVmResourceCount (vm .getAccountId (), vm .isDisplay (), offering , template );
2291+ }
2292+ return result ;
22882293 }
2289- } else {
2294+ });
2295+
2296+ if (!result ) {
22902297 throw new CloudRuntimeException ("unable to stop " + vm );
22912298 }
22922299 } catch (final NoTransitionException e ) {
@@ -2319,6 +2326,12 @@ public boolean stateTransitTo(final VirtualMachine vm1, final VirtualMachine.Eve
23192326 vm .setLastHostId (vm .getHostId ());
23202327 }
23212328 }
2329+
2330+ if (e .equals (VirtualMachine .Event .DestroyRequested ) || e .equals (VirtualMachine .Event .ExpungeOperation )) {
2331+ _reservationDao .setResourceId (Resource .ResourceType .user_vm , null );
2332+ _reservationDao .setResourceId (Resource .ResourceType .cpu , null );
2333+ _reservationDao .setResourceId (Resource .ResourceType .memory , null );
2334+ }
23222335 return _stateMachine .transitTo (vm , e , new Pair <>(vm .getHostId (), hostId ), _vmDao );
23232336 }
23242337
0 commit comments