2222
2323import javax .inject .Inject ;
2424
25+ import org .apache .commons .collections .CollectionUtils ;
2526import org .apache .log4j .Logger ;
2627import org .springframework .stereotype .Component ;
2728
2829import com .cloud .agent .manager .allocator .HostAllocator ;
30+ import com .cloud .capacity .CapacityManager ;
31+ import com .cloud .dc .ClusterDetailsDao ;
32+ import com .cloud .dc .dao .ClusterDao ;
2933import com .cloud .deploy .DeploymentPlan ;
3034import com .cloud .deploy .DeploymentPlanner .ExcludeList ;
3135import com .cloud .host .Host ;
3438import com .cloud .host .dao .HostDao ;
3539import com .cloud .offering .ServiceOffering ;
3640import com .cloud .resource .ResourceManager ;
41+ import com .cloud .utils .Pair ;
3742import com .cloud .utils .component .AdapterBase ;
3843import com .cloud .vm .VirtualMachine ;
3944import com .cloud .vm .VirtualMachineProfile ;
@@ -45,120 +50,104 @@ public class RandomAllocator extends AdapterBase implements HostAllocator {
4550 private HostDao _hostDao ;
4651 @ Inject
4752 private ResourceManager _resourceMgr ;
53+ @ Inject
54+ private ClusterDao clusterDao ;
55+ @ Inject
56+ private ClusterDetailsDao clusterDetailsDao ;
57+ @ Inject
58+ private CapacityManager capacityManager ;
4859
49- @ Override
50- public List <Host > allocateTo (VirtualMachineProfile vmProfile , DeploymentPlan plan , Type type , ExcludeList avoid , int returnUpTo ) {
51- return allocateTo (vmProfile , plan , type , avoid , returnUpTo , true );
52- }
53-
54- @ Override
55- public List <Host > allocateTo (VirtualMachineProfile vmProfile , DeploymentPlan plan , Type type , ExcludeList avoid , List <? extends Host > hosts , int returnUpTo ,
56- boolean considerReservedCapacity ) {
60+ private List <Host > findSuitableHosts (VirtualMachineProfile vmProfile , DeploymentPlan plan , Type type ,
61+ ExcludeList avoid , List <? extends Host > hosts , int returnUpTo ,
62+ boolean considerReservedCapacity ) {
5763 long dcId = plan .getDataCenterId ();
5864 Long podId = plan .getPodId ();
5965 Long clusterId = plan .getClusterId ();
6066 ServiceOffering offering = vmProfile .getServiceOffering ();
67+ List <? extends Host > hostsCopy = null ;
6168 List <Host > suitableHosts = new ArrayList <Host >();
62- List <Host > hostsCopy = new ArrayList <Host >(hosts );
6369
6470 if (type == Host .Type .Storage ) {
6571 return suitableHosts ;
6672 }
67-
6873 String hostTag = offering .getHostTag ();
6974 if (hostTag != null ) {
7075 s_logger .debug ("Looking for hosts in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId + " having host tag:" + hostTag );
7176 } else {
7277 s_logger .debug ("Looking for hosts in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId );
7378 }
74-
75- // list all computing hosts, regardless of whether they support routing...it's random after all
76- if (hostTag != null ) {
77- hostsCopy .retainAll (_hostDao .listByHostTag (type , clusterId , podId , dcId , hostTag ));
79+ if (hosts != null ) {
80+ // retain all computing hosts, regardless of whether they support routing...it's random after all
81+ hostsCopy = new ArrayList <Host >(hosts );
82+ if (hostTag != null ) {
83+ hostsCopy .retainAll (_hostDao .listByHostTag (type , clusterId , podId , dcId , hostTag ));
84+ } else {
85+ hostsCopy .retainAll (_resourceMgr .listAllUpAndEnabledHosts (type , clusterId , podId , dcId ));
86+ }
7887 } else {
79- hostsCopy .retainAll (_resourceMgr .listAllUpAndEnabledHosts (type , clusterId , podId , dcId ));
88+ // list all computing hosts, regardless of whether they support routing...it's random after all
89+ hostsCopy = new ArrayList <HostVO >();
90+ if (hostTag != null ) {
91+ hostsCopy = _hostDao .listByHostTag (type , clusterId , podId , dcId , hostTag );
92+ } else {
93+ hostsCopy = _resourceMgr .listAllUpAndEnabledHosts (type , clusterId , podId , dcId );
94+ }
8095 }
81-
8296 s_logger .debug ("Random Allocator found " + hostsCopy .size () + " hosts" );
8397 if (hostsCopy .size () == 0 ) {
8498 return suitableHosts ;
8599 }
86-
87100 Collections .shuffle (hostsCopy );
88101 for (Host host : hostsCopy ) {
89102 if (suitableHosts .size () == returnUpTo ) {
90103 break ;
91104 }
92-
93- if (!avoid .shouldAvoid (host )) {
94- suitableHosts .add (host );
95- } else {
105+ if (avoid .shouldAvoid (host )) {
96106 if (s_logger .isDebugEnabled ()) {
97- s_logger .debug ("Host name: " + host .getName () + ", hostId: " + host .getId () + " is in avoid set, " + " skipping this and trying other available hosts" );
107+ s_logger .debug ("Host name: " + host .getName () + ", hostId: " + host .getId () + " is in avoid set, skipping this and trying other available hosts" );
98108 }
109+ continue ;
99110 }
111+ Pair <Boolean , Boolean > cpuCapabilityAndCapacity = capacityManager .checkIfHostHasCpuCapabilityAndCapacity (host , offering , considerReservedCapacity );
112+ if (!cpuCapabilityAndCapacity .first () || !cpuCapabilityAndCapacity .second ()) {
113+ if (s_logger .isDebugEnabled ()) {
114+ s_logger .debug ("Not using host " + host .getId () + "; host has cpu capability? " + cpuCapabilityAndCapacity .first () + ", host has capacity?" + cpuCapabilityAndCapacity .second ());
115+ }
116+ continue ;
117+ }
118+ if (s_logger .isDebugEnabled ()) {
119+ s_logger .debug ("Found a suitable host, adding to list: " + host .getId ());
120+ }
121+ suitableHosts .add (host );
100122 }
101-
102123 if (s_logger .isDebugEnabled ()) {
103124 s_logger .debug ("Random Host Allocator returning " + suitableHosts .size () + " suitable hosts" );
104125 }
105-
106126 return suitableHosts ;
107127 }
108128
109129 @ Override
110- public List <Host > allocateTo (VirtualMachineProfile vmProfile , DeploymentPlan plan , Type type , ExcludeList avoid , int returnUpTo , boolean considerReservedCapacity ) {
111-
112- long dcId = plan .getDataCenterId ();
113- Long podId = plan .getPodId ();
114- Long clusterId = plan .getClusterId ();
115- ServiceOffering offering = vmProfile .getServiceOffering ();
116-
117- List <Host > suitableHosts = new ArrayList <Host >();
118-
119- if (type == Host .Type .Storage ) {
120- return suitableHosts ;
121- }
122-
123- String hostTag = offering .getHostTag ();
124- if (hostTag != null ) {
125- s_logger .debug ("Looking for hosts in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId + " having host tag:" + hostTag );
126- } else {
127- s_logger .debug ("Looking for hosts in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId );
128- }
129-
130- // list all computing hosts, regardless of whether they support routing...it's random after all
131- List <? extends Host > hosts = new ArrayList <HostVO >();
132- if (hostTag != null ) {
133- hosts = _hostDao .listByHostTag (type , clusterId , podId , dcId , hostTag );
134- } else {
135- hosts = _resourceMgr .listAllUpAndEnabledHosts (type , clusterId , podId , dcId );
136- }
137-
138- s_logger .debug ("Random Allocator found " + hosts .size () + " hosts" );
139-
140- if (hosts .size () == 0 ) {
141- return suitableHosts ;
142- }
143-
144- Collections .shuffle (hosts );
145- for (Host host : hosts ) {
146- if (suitableHosts .size () == returnUpTo ) {
147- break ;
148- }
130+ public List <Host > allocateTo (VirtualMachineProfile vmProfile , DeploymentPlan plan , Type type , ExcludeList avoid , int returnUpTo ) {
131+ return allocateTo (vmProfile , plan , type , avoid , returnUpTo , true );
132+ }
149133
150- if (!avoid .shouldAvoid (host )) {
151- suitableHosts .add (host );
152- } else {
153- if (s_logger .isDebugEnabled ()) {
154- s_logger .debug ("Host name: " + host .getName () + ", hostId: " + host .getId () + " is in avoid set, skipping this and trying other available hosts" );
155- }
134+ @ Override
135+ public List <Host > allocateTo (VirtualMachineProfile vmProfile , DeploymentPlan plan , Type type ,
136+ ExcludeList avoid , List <? extends Host > hosts , int returnUpTo ,
137+ boolean considerReservedCapacity ) {
138+ if (CollectionUtils .isEmpty (hosts )) {
139+ if (s_logger .isDebugEnabled ()) {
140+ s_logger .debug ("Random Allocator found 0 hosts as given host list is empty" );
156141 }
142+ return new ArrayList <Host >();
157143 }
158- if (s_logger .isDebugEnabled ()) {
159- s_logger .debug ("Random Host Allocator returning " + suitableHosts .size () + " suitable hosts" );
160- }
161- return suitableHosts ;
144+ return findSuitableHosts (vmProfile , plan , type , avoid , hosts , returnUpTo , considerReservedCapacity );
145+ }
146+
147+ @ Override
148+ public List <Host > allocateTo (VirtualMachineProfile vmProfile , DeploymentPlan plan ,
149+ Type type , ExcludeList avoid , int returnUpTo , boolean considerReservedCapacity ) {
150+ return findSuitableHosts (vmProfile , plan , type , avoid , null , returnUpTo , considerReservedCapacity );
162151 }
163152
164153 @ Override
0 commit comments