2020import java .util .HashMap ;
2121import java .util .List ;
2222import java .util .Map ;
23+ import java .util .UUID ;
2324
2425import javax .inject .Inject ;
2526
@@ -136,6 +137,7 @@ public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse,
136137 plan = new DataCenterDeployment (planToDeploy .getDataCenterId (), planToDeploy .getPodId (), planToDeploy .getClusterId (), planToDeploy .getHostId (), planToDeploy .getPoolId (), planToDeploy .getPhysicalNetworkId ());
137138 }
138139
140+ boolean planChangedByReadyVolume = false ;
139141 List <VolumeVO > vols = _volsDao .findReadyRootVolumesByInstance (vm .getId ());
140142 if (!vols .isEmpty ()){
141143 VolumeVO vol = vols .get (0 );
@@ -158,7 +160,7 @@ public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse,
158160 plan = new DataCenterDeployment (planToDeploy .getDataCenterId (), planToDeploy .getPodId (), planToDeploy .getClusterId (), planToDeploy .getHostId (), vol .getPoolId (), null , null );
159161 }else {
160162 plan = new DataCenterDeployment (rootVolDcId , rootVolPodId , rootVolClusterId , null , vol .getPoolId (), null , null );
161-
163+ planChangedByReadyVolume = true ;
162164 }
163165 }
164166
@@ -187,38 +189,47 @@ public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse,
187189 _vmEntityDao .persist (vmEntityVO );
188190
189191 return vmReservation .getUuid ();
192+ } else if (planChangedByReadyVolume ) {
193+ // we could not reserve in the Volume's cluster - let the deploy
194+ // call retry it.
195+ return UUID .randomUUID ().toString ();
190196 }else {
191197 throw new InsufficientServerCapacityException ("Unable to create a deployment for " + vmProfile , DataCenter .class , plan .getDataCenterId ());
192198 }
193199
194200 }
195201
196202 @ Override
197- public void deployVirtualMachine (String reservationId , String caller , Map <VirtualMachineProfile .Param , Object > params ) throws InsufficientCapacityException , ResourceUnavailableException {
203+ public void deployVirtualMachine (String reservationId , VMEntityVO vmEntityVO , String caller , Map <VirtualMachineProfile .Param , Object > params ) throws InsufficientCapacityException , ResourceUnavailableException {
198204 //grab the VM Id and destination using the reservationId.
199205
206+ VMInstanceVO vm = _vmDao .findByUuid (vmEntityVO .getUuid ());
207+
200208 VMReservationVO vmReservation = _reservationDao .findByReservationId (reservationId );
201- long vmId = vmReservation .getVmId ();
202-
203- VMInstanceVO vm = _vmDao .findById (vmId );
204- //Pass it down
205- Long poolId = null ;
206- Map <Long ,Long > storage = vmReservation .getVolumeReservation ();
207- if (storage != null ){
208- List <Long > volIdList = new ArrayList <Long >(storage .keySet ());
209- if (volIdList !=null && !volIdList .isEmpty ()){
210- poolId = storage .get (volIdList .get (0 ));
209+ if (vmReservation != null ){
210+ // Pass it down
211+ Long poolId = null ;
212+ Map <Long , Long > storage = vmReservation .getVolumeReservation ();
213+ if (storage != null ) {
214+ List <Long > volIdList = new ArrayList <Long >(storage .keySet ());
215+ if (volIdList != null && !volIdList .isEmpty ()) {
216+ poolId = storage .get (volIdList .get (0 ));
217+ }
211218 }
212- }
213219
214- DataCenterDeployment reservedPlan = new DataCenterDeployment (vm .getDataCenterId (), vmReservation .getPodId (), vmReservation .getClusterId (),
215- vmReservation .getHostId (), null , null );
216- try {
217- VMInstanceVO vmDeployed = _itMgr .start (vm , params , _userDao .findById (new Long (caller )), _accountDao .findById (vm .getAccountId ()), reservedPlan );
218- }catch (Exception ex ){
219- //Retry the deployment without using the reservation plan
220- DataCenterDeployment plan = new DataCenterDeployment (vm .getDataCenterId (), null , null ,null , null , null );
221- _itMgr .start (vm , params , _userDao .findById (new Long (caller )), _accountDao .findById (vm .getAccountId ()), plan );
220+ DataCenterDeployment reservedPlan = new DataCenterDeployment (vm .getDataCenterId (),
221+ vmReservation .getPodId (), vmReservation .getClusterId (), vmReservation .getHostId (), null , null );
222+ try {
223+ VMInstanceVO vmDeployed = _itMgr .start (vm , params , _userDao .findById (new Long (caller )),
224+ _accountDao .findById (vm .getAccountId ()), reservedPlan );
225+ } catch (Exception ex ) {
226+ // Retry the deployment without using the reservation plan
227+ _itMgr .start (vm , params , _userDao .findById (new Long (caller )), _accountDao .findById (vm .getAccountId ()),
228+ null );
229+ }
230+ } else {
231+ // no reservation found. Let VirtualMachineManager retry
232+ _itMgr .start (vm , params , _userDao .findById (new Long (caller )), _accountDao .findById (vm .getAccountId ()), null );
222233 }
223234
224235 }
0 commit comments