1
- // Licensed to the Apacohe Software Foundation (ASF) under one
1
+ // Licensed to the Apache Software Foundation (ASF) under one
2
2
// or more contributor license agreements. See the NOTICE file
3
3
// distributed with this work for additional information
4
4
// regarding copyright ownership. The ASF licenses this file
49
49
import javax .naming .ConfigurationException ;
50
50
import javax .persistence .EntityExistsException ;
51
51
52
+ import com .cloud .configuration .Resource ;
52
53
import com .cloud .domain .Domain ;
53
54
import com .cloud .domain .dao .DomainDao ;
54
55
import com .cloud .network .vpc .VpcVO ;
87
88
import org .apache .cloudstack .framework .messagebus .MessageHandler ;
88
89
import org .apache .cloudstack .jobs .JobInfo ;
89
90
import org .apache .cloudstack .managed .context .ManagedContextRunnable ;
91
+ import org .apache .cloudstack .reservation .dao .ReservationDao ;
90
92
import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
91
93
import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
92
94
import org .apache .cloudstack .storage .to .VolumeObjectTO ;
@@ -296,6 +298,8 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
296
298
@ Inject
297
299
private VMInstanceDao _vmDao ;
298
300
@ Inject
301
+ private ReservationDao _reservationDao ;
302
+ @ Inject
299
303
private ServiceOfferingDao _offeringDao ;
300
304
@ Inject
301
305
private DiskOfferingDao _diskOfferingDao ;
@@ -914,7 +918,7 @@ protected boolean checkWorkItems(final VMInstanceVO vm, final State state) throw
914
918
915
919
@ DB
916
920
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 {
918
922
final long vmId = vm .getId ();
919
923
920
924
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
934
938
if (logger .isDebugEnabled ()) {
935
939
logger .debug ("Successfully transitioned to start state for " + vm + " reservation id = " + work .getId ());
936
940
}
941
+ if (VirtualMachine .Type .User .equals (vm .type ) && ResourceCountRunningVMsonly .value ()) {
942
+ _resourceLimitMgr .incrementVmResourceCount (owner .getAccountId (), vm .isDisplay (), offering , template );
943
+ }
937
944
return new Ternary <>(vm , context , work );
938
945
}
939
946
@@ -1126,7 +1133,10 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
1126
1133
1127
1134
final VirtualMachineGuru vmGuru = getVmGuru (vm );
1128
1135
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 );
1130
1140
if (start == null ) {
1131
1141
return ;
1132
1142
}
@@ -1136,8 +1146,6 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
1136
1146
ItWorkVO work = start .third ();
1137
1147
1138
1148
VMInstanceVO startedVm = null ;
1139
- final ServiceOfferingVO offering = _offeringDao .findById (vm .getId (), vm .getServiceOfferingId ());
1140
- final VirtualMachineTemplate template = _entityMgr .findByIdIncludingRemoved (VirtualMachineTemplate .class , vm .getTemplateId ());
1141
1149
1142
1150
DataCenterDeployment plan = new DataCenterDeployment (vm .getDataCenterId (), vm .getPodIdToDeployIn (), null , null , null , null , ctx );
1143
1151
if (planToDeploy != null && planToDeploy .getDataCenterId () != 0 ) {
@@ -1150,12 +1158,6 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
1150
1158
1151
1159
final HypervisorGuru hvGuru = _hvGuruMgr .getGuru (vm .getHypervisorType ());
1152
1160
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
-
1159
1161
boolean canRetry = true ;
1160
1162
ExcludeList avoids = null ;
1161
1163
try {
@@ -2277,16 +2279,21 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl
2277
2279
_workDao .update (work .getId (), work );
2278
2280
}
2279
2281
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 ;
2288
2293
}
2289
- } else {
2294
+ });
2295
+
2296
+ if (!result ) {
2290
2297
throw new CloudRuntimeException ("unable to stop " + vm );
2291
2298
}
2292
2299
} catch (final NoTransitionException e ) {
@@ -2319,6 +2326,12 @@ public boolean stateTransitTo(final VirtualMachine vm1, final VirtualMachine.Eve
2319
2326
vm .setLastHostId (vm .getHostId ());
2320
2327
}
2321
2328
}
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
+ }
2322
2335
return _stateMachine .transitTo (vm , e , new Pair <>(vm .getHostId (), hostId ), _vmDao );
2323
2336
}
2324
2337
0 commit comments