3636import com .cloud .deploy .DeployDestination ;
3737import com .cloud .deploy .DeploymentPlan ;
3838import com .cloud .deploy .DeploymentPlanner ;
39+ import com .cloud .deploy .DeploymentPlanningManager ;
3940import com .cloud .deploy .DeploymentPlanner .ExcludeList ;
41+ import com .cloud .exception .AffinityConflictException ;
4042import com .cloud .exception .AgentUnavailableException ;
4143import com .cloud .exception .ConcurrentOperationException ;
4244import com .cloud .exception .InsufficientCapacityException ;
5557import com .cloud .storage .dao .VolumeDao ;
5658import com .cloud .user .dao .AccountDao ;
5759import com .cloud .user .dao .UserDao ;
58- import com .cloud .utils .component . ComponentContext ;
60+ import com .cloud .utils .exception . CloudRuntimeException ;
5961import com .cloud .vm .VMInstanceVO ;
6062import com .cloud .vm .VirtualMachineManager ;
6163import com .cloud .vm .VirtualMachineProfile ;
@@ -69,42 +71,45 @@ public class VMEntityManagerImpl implements VMEntityManager {
6971 protected VMInstanceDao _vmDao ;
7072 @ Inject
7173 protected VMTemplateDao _templateDao = null ;
72-
74+
7375 @ Inject
7476 protected ServiceOfferingDao _serviceOfferingDao ;
75-
77+
7678 @ Inject
7779 protected DiskOfferingDao _diskOfferingDao = null ;
78-
80+
7981 @ Inject
8082 protected NetworkDao _networkDao ;
81-
83+
8284 @ Inject
8385 protected AccountDao _accountDao = null ;
8486
8587 @ Inject
8688 protected UserDao _userDao = null ;
8789
88- @ Inject
90+ @ Inject
8991 protected VMEntityDao _vmEntityDao ;
90-
91- @ Inject
92+
93+ @ Inject
9294 protected VMReservationDao _reservationDao ;
93-
95+
9496 @ Inject
9597 protected VirtualMachineManager _itMgr ;
96-
98+
9799 @ Inject
98100 protected List <DeploymentPlanner > _planners ;
99-
101+
100102 @ Inject
101103 protected VolumeDao _volsDao ;
102-
104+
103105 @ Inject
104106 protected PrimaryDataStoreDao _storagePoolDao ;
105107 @ Inject
106108 DataStoreManager dataStoreMgr ;
107-
109+
110+ @ Inject
111+ DeploymentPlanningManager _dpMgr ;
112+
108113 @ Override
109114 public VMEntityVO loadVirtualMachine (String vmId ) {
110115 // TODO Auto-generated method stub
@@ -114,11 +119,11 @@ public VMEntityVO loadVirtualMachine(String vmId) {
114119 @ Override
115120 public void saveVirtualMachine (VMEntityVO entity ) {
116121 _vmEntityDao .persist (entity );
117-
122+
118123 }
119124
120125 @ Override
121- public String reserveVirtualMachine (VMEntityVO vmEntityVO , String plannerToUse , DeploymentPlan planToDeploy , ExcludeList exclude )
126+ public String reserveVirtualMachine (VMEntityVO vmEntityVO , String plannerToUse , DeploymentPlan planToDeploy , ExcludeList exclude )
122127 throws InsufficientCapacityException , ResourceUnavailableException {
123128
124129 //call planner and get the deployDestination.
@@ -130,12 +135,12 @@ public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse,
130135 if (planToDeploy != null && planToDeploy .getDataCenterId () != 0 ){
131136 plan = new DataCenterDeployment (planToDeploy .getDataCenterId (), planToDeploy .getPodId (), planToDeploy .getClusterId (), planToDeploy .getHostId (), planToDeploy .getPoolId (), planToDeploy .getPhysicalNetworkId ());
132137 }
133-
138+
134139 List <VolumeVO > vols = _volsDao .findReadyRootVolumesByInstance (vm .getId ());
135140 if (!vols .isEmpty ()){
136141 VolumeVO vol = vols .get (0 );
137142 StoragePool pool = (StoragePool )this .dataStoreMgr .getPrimaryDataStore (vol .getPoolId ());
138-
143+
139144 if (!pool .isInMaintenance ()) {
140145 long rootVolDcId = pool .getDataCenterId ();
141146 Long rootVolPodId = pool .getPodId ();
@@ -156,21 +161,21 @@ public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse,
156161
157162 }
158163 }
159-
164+
165+ }
166+
167+ DeployDestination dest ;
168+ try {
169+ dest = _dpMgr .planDeployment (vmProfile , plan , exclude );
170+ } catch (AffinityConflictException e ) {
171+ throw new CloudRuntimeException ("Unable to create deployment, affinity rules associted to the VM conflict" );
160172 }
161-
162- DeploymentPlanner planner = ComponentContext .getComponent (plannerToUse );
163- DeployDestination dest = null ;
164-
165- if (planner .canHandle (vmProfile , plan , exclude )) {
166- dest = planner .plan (vmProfile , plan , exclude );
167- }
168173
169174 if (dest != null ) {
170175 //save destination with VMEntityVO
171176 VMReservationVO vmReservation = new VMReservationVO (vm .getId (), dest .getDataCenter ().getId (), dest .getPod ().getId (), dest .getCluster ().getId (), dest .getHost ().getId ());
172177 Map <Long ,Long > volumeReservationMap = new HashMap <Long ,Long >();
173-
178+
174179 if (vm .getHypervisorType () != HypervisorType .BareMetal ) {
175180 for (Volume vo : dest .getStorageForDisks ().keySet ()){
176181 volumeReservationMap .put (vo .getId (), dest .getStorageForDisks ().get (vo ).getId ());
@@ -180,21 +185,21 @@ public String reserveVirtualMachine(VMEntityVO vmEntityVO, String plannerToUse,
180185
181186 vmEntityVO .setVmReservation (vmReservation );
182187 _vmEntityDao .persist (vmEntityVO );
183-
188+
184189 return vmReservation .getUuid ();
185190 }else {
186191 throw new InsufficientServerCapacityException ("Unable to create a deployment for " + vmProfile , DataCenter .class , plan .getDataCenterId ());
187192 }
188-
193+
189194 }
190195
191196 @ Override
192197 public void deployVirtualMachine (String reservationId , String caller , Map <VirtualMachineProfile .Param , Object > params ) throws InsufficientCapacityException , ResourceUnavailableException {
193198 //grab the VM Id and destination using the reservationId.
194-
199+
195200 VMReservationVO vmReservation = _reservationDao .findByReservationId (reservationId );
196201 long vmId = vmReservation .getVmId ();
197-
202+
198203 VMInstanceVO vm = _vmDao .findById (vmId );
199204 //Pass it down
200205 Long poolId = null ;
@@ -205,12 +210,17 @@ public void deployVirtualMachine(String reservationId, String caller, Map<Virtua
205210 poolId = storage .get (volIdList .get (0 ));
206211 }
207212 }
208-
209- DataCenterDeployment plan = new DataCenterDeployment (vm .getDataCenterId (), vmReservation .getPodId (), vmReservation .getClusterId (),
213+
214+ DataCenterDeployment reservedPlan = new DataCenterDeployment (vm .getDataCenterId (), vmReservation .getPodId (), vmReservation .getClusterId (),
210215 vmReservation .getHostId (), null , null );
211-
212- VMInstanceVO vmDeployed = _itMgr .start (vm , params , _userDao .findById (new Long (caller )), _accountDao .findById (vm .getAccountId ()), plan );
213-
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 );
222+ }
223+
214224 }
215225
216226 @ Override
@@ -227,7 +237,7 @@ public boolean destroyVirtualMachine(VMEntityVO vmEntityVO, String caller) throw
227237 VMInstanceVO vm = _vmDao .findByUuid (vmEntityVO .getUuid ());
228238 return _itMgr .destroy (vm , _userDao .findById (new Long (caller )), _accountDao .findById (vm .getAccountId ()));
229239
230-
240+
231241 }
232242
233243}
0 commit comments